diff --git a/src/scripts/controllers/new.js b/src/scripts/controllers/new.js index 16f5bbd..34da693 100644 --- a/src/scripts/controllers/new.js +++ b/src/scripts/controllers/new.js @@ -129,6 +129,7 @@ $scope.openTorrent = function () { ariaNgFileService.openFileContent({ + scope: $scope, fileFilter: '.torrent', fileType: 'binary' }, function (result) { @@ -142,6 +143,7 @@ $scope.openMetalink = function () { ariaNgFileService.openFileContent({ + scope: $scope, fileFilter: '.meta4,.metalink', fileType: 'binary' }, function (result) { diff --git a/src/scripts/controllers/settings-ariang.js b/src/scripts/controllers/settings-ariang.js index fb60445..c73edea 100644 --- a/src/scripts/controllers/settings-ariang.js +++ b/src/scripts/controllers/settings-ariang.js @@ -209,6 +209,7 @@ $scope.openAriaNgConfigFile = function () { ariaNgFileService.openFileContent({ + scope: $scope, fileFilter: '.json', fileType: 'text' }, function (result) { diff --git a/src/scripts/services/ariaNgFileService.js b/src/scripts/services/ariaNgFileService.js index 4d51286..feee5b1 100644 --- a/src/scripts/services/ariaNgFileService.js +++ b/src/scripts/services/ariaNgFileService.js @@ -63,16 +63,19 @@ } options = angular.extend({ + scope: null, fileFilter: null, - fileType: 'binary' // or 'text' + fileType: 'binary', // or 'text' + successCallback: successCallback, + errorCallback: errorCallback }, options); - var allowedExtensions = getAllowedExtensions(options.fileFilter); - if (!element || !element.change) { element = angular.element(''); } + element.data('options', options); + if (options.fileFilter) { element.attr('accept', options.fileFilter); } @@ -85,12 +88,20 @@ return; } + var thisOptions = element.data('options'); + var allowedExtensions = getAllowedExtensions(thisOptions.fileFilter); var file = this.files[0]; var fileName = file.name; if (!checkFileExtension(fileName, allowedExtensions)) { - if (errorCallback) { - errorCallback('The selected file type is invalid!'); + if (thisOptions.errorCallback) { + if (thisOptions.scope) { + thisOptions.scope.$apply(function () { + thisOptions.errorCallback('The selected file type is invalid!'); + }); + } else { + thisOptions.errorCallback('The selected file type is invalid!'); + } } return; @@ -103,7 +114,7 @@ fileName: fileName }; - switch (options.fileType) { + switch (thisOptions.fileType) { case 'text': result.content = this.result; break; @@ -113,18 +124,30 @@ break; } - if (successCallback) { - successCallback(result); + if (thisOptions.successCallback) { + if (thisOptions.scope) { + thisOptions.scope.$apply(function () { + thisOptions.successCallback(result); + }); + } else { + thisOptions.successCallback(result); + } } }; reader.onerror = function () { - if (errorCallback) { - errorCallback('Failed to load file!'); + if (thisOptions.errorCallback) { + if (thisOptions.scope) { + thisOptions.scope.$apply(function () { + thisOptions.errorCallback('Failed to load file!'); + }); + } else { + thisOptions.errorCallback('Failed to load file!'); + } } }; - switch (options.fileType) { + switch (thisOptions.fileType) { case 'text': reader.readAsText(file); break;