OSDN Git Service

[Develop and Manual Test] show help with websocket
[eos/zephyr.git] / front-end / app / scripts / directives / Command.ts
1 namespace app.directives {
2     export class Command implements ng.IDirective {
3
4         public restrict: string;
5         public replace: boolean;
6         public templateUrl: string;
7         public scope: boolean;
8         public controller: string;
9         public controllerAs: string;
10         public bindToController: Object;
11
12         /** 
13          * constructor
14          *
15          * @param
16          */
17         constructor() {
18             this.restrict = 'E';
19             this.replace = true; 
20             this.scope = true;
21             this.controller = 'commandController';
22             this.controllerAs = 'ctrl';
23             this.bindToController = {
24                 index: '=',
25                 name: '=',
26                 remove: '&',
27                 list: '='
28             };
29             this.templateUrl = 'templates/command.html';
30         }
31
32         /**
33          * instance生成
34          * @returns {function(): HeaderMenu}
35          * @constructor
36          */
37         public static Factory(): ng.IDirectiveFactory {
38             var directive = () => {
39                 return new Command();
40             };
41             directive.$inject = [];
42             return directive;
43         }
44     }
45
46     export class CommandController {
47
48         public static $inject = ['APIEndPoint', '$scope', 'MyModal', 'WebSocket'];
49
50         // From parent directive bindToController property
51         private index: string;
52         private name: string;
53         private remove: Function;
54         private list: declares.CommandInfo[];
55         private workspace: declares.IFileInfo;
56
57         // property of ui.bootstrap uib-accordion directive
58         private isOpen: boolean;
59         private heading: string;
60
61         // property about Option directive
62         private files: any;
63         private options: declares.IOption[];
64         private dirs: any;
65         private messages: any;
66
67         constructor(private APIEndPoint: services.APIEndPoint, private $scope: ng.IScope, private MyModal: services.MyModal, private WebSocket: services.WebSocket) {
68             var controller = this;
69
70             // Get OptionControlFile and bind it to $scope.options
71             this.APIEndPoint
72             //.getOptionControlFile('mrcImageNoiseAdd')
73             .getOptionControlFile(this.name)
74             .$promise
75             .then(function(result) {
76                 controller.options = result.info;
77             });
78
79             this.APIEndPoint
80             .getDirectories()
81             .$promise
82             .then(function(result) {
83                 controller.dirs = result.info;
84             });
85
86             this.heading = "[" + this.index + "]: dcdFilePrint";
87             this.isOpen = true;
88
89             // When call add button at executeController, all accordion close.
90             this.$scope.$on('close', () => {
91                 controller.isOpen = false;
92             });
93
94             this.WebSocket.on('console', function(msg) {
95                 controller.messages = msg.split('\n');
96             });
97
98         }
99
100         public submit() {
101             var opt = [];
102             angular.forEach(this.options, (option) => {
103
104                 var obj = {
105                     name: option.option,
106                     arguments: []
107                 };
108
109                 angular.forEach(option.arg, (arg) => {
110                     if(arg.input) {
111                         if(typeof arg.input === 'object') {
112                             obj.arguments.push(arg.input.name);
113                         } else {
114                             obj.arguments.push(arg.input);
115                         }
116                     }
117                 });
118
119                 if(obj.arguments.length > 0) {
120                     opt.push(obj);
121                 }
122             });
123
124             var execObj = {
125                 command: this.name,
126                 workspace: this.workspace.fileId,
127                 options: opt
128             };
129
130             //console.log(JSON.stringify(execObj,null,'\t'));
131             this.APIEndPoint
132             .execute(JSON.stringify(execObj))
133             .then((result) => {
134                 console.log(result);
135             })
136         }
137
138         public removeMySelf(index:number) {
139             this.remove()(index,this.list);
140         }
141
142         private reloadFiles() {
143             var fileId = this.workspace.fileId;
144             this.APIEndPoint
145             .getFiles(fileId)
146             .$promise
147             .then((result) => { 
148                 var status = result.status;
149                 if(status === 'success') {
150                     this.files = result.info;
151                 } else {
152                     console.log(result.message);
153                 }
154             });
155         }
156
157         public debug() {
158             this.APIEndPoint
159             .debug()
160             .then((result) => {
161                 console.log(result);
162             });
163         }
164         
165         public help() {
166             this.APIEndPoint
167             .help(this.name)
168             .then((result) => {
169                 console.log(result);
170             });
171         }
172
173     } 
174 }