OSDN Git Service

[Development and Manual test] showing help correctly
[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                 var messages = msg.split('\n');
96                 if(messages[0].substr(0,6) === 'Usage:') {
97                     controller.messages = msg.split('\n');
98                 }
99             });
100
101         }
102
103         public submit() {
104             var opt = [];
105             angular.forEach(this.options, (option) => {
106
107                 var obj = {
108                     name: option.option,
109                     arguments: []
110                 };
111
112                 angular.forEach(option.arg, (arg) => {
113                     if(arg.input) {
114                         if(typeof arg.input === 'object') {
115                             obj.arguments.push(arg.input.name);
116                         } else {
117                             obj.arguments.push(arg.input);
118                         }
119                     }
120                 });
121
122                 if(obj.arguments.length > 0) {
123                     opt.push(obj);
124                 }
125             });
126
127             var execObj = {
128                 command: this.name,
129                 workspace: this.workspace.fileId,
130                 options: opt
131             };
132
133             //console.log(JSON.stringify(execObj,null,'\t'));
134             this.APIEndPoint
135             .execute(JSON.stringify(execObj))
136             .then((result) => {
137                 console.log(result);
138             })
139         }
140
141         public removeMySelf(index:number) {
142             this.remove()(index,this.list);
143         }
144
145         private reloadFiles() {
146             var fileId = this.workspace.fileId;
147             this.APIEndPoint
148             .getFiles(fileId)
149             .$promise
150             .then((result) => { 
151                 var status = result.status;
152                 if(status === 'success') {
153                     this.files = result.info;
154                 } else {
155                     console.log(result.message);
156                 }
157             });
158         }
159
160         public debug() {
161             this.APIEndPoint
162             .debug()
163             .then((result) => {
164                 console.log(result);
165             });
166         }
167         
168         public help() {
169             this.APIEndPoint
170             .help(this.name)
171             .then((result) => {
172                 console.log(result);
173             });
174         }
175
176     } 
177 }