OSDN Git Service

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