OSDN Git Service

[TMP] before create Console service
[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', '$window', '$rootScope'];
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, 
68                     private $scope: ng.IScope, 
69                     private MyModal: services.MyModal, 
70                     private WebSocket: services.WebSocket,
71                     private $window: ng.IWindowService,
72                     private $rootScope: ng.IRootScopeService
73                    ) {
74                        var controller = this;
75
76                        // Get OptionControlFile and bind it to $scope.options
77                        this.APIEndPoint
78                        //.getOptionControlFile('mrcImageNoiseAdd')
79                        .getOptionControlFile(this.name)
80                        .$promise
81                        .then(function(result) {
82                            controller.options = result.info;
83                        });
84
85                        this.APIEndPoint
86                        .getDirectories()
87                        .$promise
88                        .then(function(result) {
89                            controller.dirs = result.info;
90                        });
91
92                        this.heading = "[" + this.index + "]: dcdFilePrint";
93                        this.isOpen = true;
94
95                        // When call add button at executeController, all accordion close.
96                        this.$scope.$on('close', () => {
97                            controller.isOpen = false;
98                        });
99
100                        /** Add EventHandler
101                         * ここではCommandディレクティブが受け取る可能性のあるイベントを登録する。
102                         * 例えば、コマンドのヘルプやEosコマンドの実行時のCLIの出力はWebSocketサービスで受け取るので、
103                         WebSocketサービスからこのCommandディレクティブに対してemitされたメッセージをこのイベントハンドラで受け取る。
104
105                         イベントネームは生成されたCommandディレクティブにユニークなidをディレクティブが生成されるたびに生成し、
106                         Commandディレクティブが削除されるたびにそのイベントハンドラを破棄することとする。
107                         */
108                        function guid() {
109                            function s4() {
110                                return Math.floor((1 + Math.random()) * 0x10000)
111                                .toString(16)
112                                .substring(1);
113                            }
114                            return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
115                                s4() + '-' + s4() + s4() + s4();
116                        }
117
118                        var uuid = guid();
119
120                        /*
121                        this.WebSocket.add(uuid);
122                        this.$rootScope.on(uuid, function(msg) {
123                            console.log(msg);
124                            /*
125                               var messages = msg.split('\n');
126                               if(messages[0].substr(0,6) === 'Usage:') {
127                               controller.messages = msg.split('\n');
128                               }
129                        });
130                             */
131                    }
132
133                    public submit() {
134                        var opt = [];
135                        angular.forEach(this.options, (option) => {
136
137                            var obj = {
138                                name: option.option,
139                                arguments: []
140                            };
141
142                            angular.forEach(option.arg, (arg) => {
143                                if(arg.input) {
144                                    if(typeof arg.input === 'object') {
145                                        obj.arguments.push(arg.input.name);
146                                    } else {
147                                        obj.arguments.push(arg.input);
148                                    }
149                                }
150                            });
151
152                            if(obj.arguments.length > 0) {
153                                opt.push(obj);
154                            }
155                        });
156
157                        var execObj = {
158                            command: this.name,
159                            workspace: this.workspace.fileId,
160                            options: opt
161                        };
162
163                        //console.log(JSON.stringify(execObj,null,'\t'));
164                        this.APIEndPoint
165                        .execute(JSON.stringify(execObj))
166                        .then((result) => {
167                            console.log(result);
168                        })
169                    }
170
171                    public removeMySelf(index:number) {
172                        this.$scope.$destroy();
173                        this.remove()(index,this.list);
174                    }
175
176                    private reloadFiles() {
177                        var fileId = this.workspace.fileId;
178                        this.APIEndPoint
179                        .getFiles(fileId)
180                        .$promise
181                        .then((result) => { 
182                            var status = result.status;
183                            if(status === 'success') {
184                                this.files = result.info;
185                            } else {
186                                console.log(result.message);
187                            }
188                        });
189                    }
190
191                    public debug() {
192                        console.log(this.$rootScope);
193
194                        /*   Create console area
195                         * 
196                         */
197
198                        var div = angular.element(this.$window.document).find("div");
199                        var consoleTag;
200                        var parametersTag;
201                        angular.forEach(div,function(v) {
202                            if(v.className === "panel-body console") {
203                                consoleTag = v;
204                            } else if(v.className === "row parameters-console") {
205                                parametersTag = v;
206                            }
207                        });
208
209                        // Adjust console area from CSS styling positoning information
210                        var consoleHeight = parseInt(parametersTag.clientHeight.toString().replace('px','')) - 150 + 'px';
211                        var consoleWidth = parseInt(parametersTag.clientWidth.toString().replace('px','')) / 3 * 2 - 150 + 'px';
212                        consoleTag.style.height = consoleHeight;
213                        consoleTag.style.width = consoleWidth;
214                        /*   Create console area
215                         * 
216                         */
217                    }
218
219                    public help() {
220                        this.APIEndPoint
221                        .help(this.name)
222                        .then((result) => {
223                            console.log(result);
224                        });
225                    }
226
227     } 
228 }