org.onap migration
[vid.git] / vid-app-common / src / main / webapp / app / vid / scripts / modals / attach-test-env-manifest / attach-test-env-manifest.controller.js
1 (function () {
2     'use strict';
3
4     appDS2.controller("attachTestEnvManifestController", ["$uibModalInstance", "$uibModal", "$log", "$scope",
5         attachTestEnvManifestController]);
6
7     function attachTestEnvManifestController($uibModalInstance, $uibModal, $log, $scope) {
8         var vm = this;
9
10         var init = function () {
11             vm.manifest = "";
12             vm.error="";
13         };
14
15         vm.close = function () {
16             $uibModalInstance.close();
17         };
18
19         vm.submit = function () {
20             $uibModalInstance.close(vm.manifest);
21         };
22
23         vm.isSubmitDisabled = function () {
24             return !(vm.manifest);
25         };
26
27
28
29         /*
30         Must be $scope because we bind to the onchange of the html (cannot attached to vm variable).
31         We use scope because angular doesn't support ng-change on input file
32         https://github.com/angular/angular.js/issues/1375
33         https://stackoverflow.com/questions/17922557/angularjs-how-to-check-for-changes-in-file-input-fields
34          */
35         $scope.selectAttachmentManifest = function (fileInput) {
36             if (fileInput && fileInput.id) {
37                 vm.manifest = "";
38                 vm.error="";
39                 var file = fileInput.files[0];
40                 vm.filename=file.name;
41                 var fileReader = new FileReader();
42                 fileReader.onload = function (load) {
43                     try {
44                         var lines = load.target.result;
45                         vm.manifest = JSON.parse(lines);
46                     } catch (error) {
47                         $log.error(error);
48                         vm.error = "file: " + vm.filename + " is not a valid JSON"
49                     }
50                     $scope.$apply();
51                 };
52                 fileReader.readAsText(file);
53             }
54         };
55
56         init();
57     }
58 })();