OSDN Git Service

[Develop] /api/v1/debug for debugging websocket messaging
[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
66         constructor(private APIEndPoint: services.APIEndPoint, private $scope: ng.IScope, private MyModal: services.MyModal, private WebSocket: services.WebSocket) {
67             var controller = this;
68
69             // Get OptionControlFile and bind it to $scope.options
70             this.APIEndPoint
71             .getOptionControlFile('mrcImageNoiseAdd')
72             .$promise
73             .then(function(result) {
74                 controller.options = result.info;
75             });
76
77             this.APIEndPoint
78             .getDirectories()
79             .$promise
80             .then(function(result) {
81                 controller.dirs = result.info;
82             });
83
84             this.heading = "[" + this.index + "]: dcdFilePring";
85             this.isOpen = true;
86
87             // When call add button at executeController, all accordion close.
88             this.$scope.$on('close', () => {
89                 controller.isOpen = false;
90             });
91
92             this.WebSocket.on('console', function(msg) {
93                 console.log('CONSOLE!!!');
94                 console.log(msg);
95             });
96
97         }
98
99         public submit() {
100             var opt = [];
101             angular.forEach(this.options, (option) => {
102
103                 var obj = {
104                     name: option.option,
105                     arguments: []
106                 };
107
108                 angular.forEach(option.arg, (arg) => {
109                     if(arg.input) {
110                         if(typeof arg.input === 'object') {
111                             obj.arguments.push(arg.input.name);
112                         } else {
113                             obj.arguments.push(arg.input);
114                         }
115                     }
116                 });
117
118                 if(obj.arguments.length > 0) {
119                     opt.push(obj);
120                 }
121             });
122
123             var execObj = {
124                 command: this.name,
125                 workspace: this.workspace.fileId,
126                 options: opt
127             };
128
129             //console.log(JSON.stringify(execObj,null,'\t'));
130             this.APIEndPoint
131             .execute(JSON.stringify(execObj))
132             .then((result) => {
133                 console.log(result);
134             })
135         }
136
137         public removeMySelf(index:number) {
138             this.remove()(index,this.list);
139         }
140
141         private reloadFiles() {
142             var fileId = this.workspace.fileId;
143             this.APIEndPoint
144             .getFiles(fileId)
145             .$promise
146             .then((result) => { 
147                 var status = result.status;
148                 if(status === 'success') {
149                     this.files = result.info;
150                 } else {
151                     console.log(result.message);
152                 }
153             });
154         }
155
156         public debug() {
157             this.APIEndPoint
158             .debug()
159             .then((result) => {
160                 console.log(result);
161             });
162         }
163         
164     } 
165 }