OSDN Git Service

Implemeted 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', 'Console'];
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         private uuid: string; // uuid of Instance of Command directive
68
69         constructor(private APIEndPoint: services.APIEndPoint, 
70                     private $scope: ng.IScope, 
71                     private MyModal: services.MyModal, 
72                     private WebSocket: services.WebSocket,
73                     private $window: ng.IWindowService,
74                     private $rootScope: ng.IRootScopeService,
75                     private Console: services.Console
76                    ) {
77                        var controller = this;
78
79                        // Get OptionControlFile and bind it to $scope.options
80                        this.APIEndPoint
81                        //.getOptionControlFile('mrcImageNoiseAdd')
82                        .getOptionControlFile(this.name)
83                        .$promise
84                        .then(function(result) {
85                            controller.options = result.info;
86                        });
87
88                        this.APIEndPoint
89                        .getDirectories()
90                        .$promise
91                        .then(function(result) {
92                            controller.dirs = result.info;
93                        });
94
95                        this.heading = "[" + this.index + "]: dcdFilePrint";
96                        this.isOpen = true;
97
98                        // When call add button at executeController, all accordion close.
99                        this.$scope.$on('close', () => {
100                            controller.isOpen = false;
101                        });
102
103                        function guid() {
104                            function s4() {
105                                return Math.floor((1 + Math.random()) * 0x10000)
106                                .toString(16)
107                                .substring(1);
108                            }
109                            return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
110                                s4() + '-' + s4() + s4() + s4();
111                        }
112
113                        this.uuid = guid();
114
115                        this.Console.addDirective(this.uuid);
116                        this.Console.showIDs();
117                        /*
118                        this.$rootScope.on(uuid, function(msg) {
119                            console.log(msg);
120                        });
121                        */
122                    }
123
124                    public submit() {
125                        var opt = [];
126                        angular.forEach(this.options, (option) => {
127
128                            var obj = {
129                                name: option.option,
130                                arguments: []
131                            };
132
133                            angular.forEach(option.arg, (arg) => {
134                                if(arg.input) {
135                                    if(typeof arg.input === 'object') {
136                                        obj.arguments.push(arg.input.name);
137                                    } else {
138                                        obj.arguments.push(arg.input);
139                                    }
140                                }
141                            });
142
143                            if(obj.arguments.length > 0) {
144                                opt.push(obj);
145                            }
146                        });
147
148                        var execObj = {
149                            command: this.name,
150                            workspace: this.workspace.fileId,
151                            options: opt
152                        };
153
154                        //console.log(JSON.stringify(execObj,null,'\t'));
155                        this.APIEndPoint
156                        .execute(JSON.stringify(execObj))
157                        .then((result) => {
158                            console.log(result);
159                        })
160                    }
161
162                    public removeMySelf(index:number) {
163                        this.$scope.$destroy();
164                        this.Console.removeDirective(this.uuid);
165                        this.remove()(index,this.list);
166                        this.Console.showIDs();
167                    }
168
169                    private reloadFiles() {
170                        var fileId = this.workspace.fileId;
171                        this.APIEndPoint
172                        .getFiles(fileId)
173                        .$promise
174                        .then((result) => { 
175                            var status = result.status;
176                            if(status === 'success') {
177                                this.files = result.info;
178                            } else {
179                                console.log(result.message);
180                            }
181                        });
182                    }
183
184                    public debug() {
185                        /*   Create console area
186                         * 
187                         */
188
189                        var div = angular.element(this.$window.document).find("div");
190                        var consoleTag;
191                        var parametersTag;
192                        angular.forEach(div,function(v) {
193                            if(v.className === "panel-body console") {
194                                consoleTag = v;
195                            } else if(v.className === "row parameters-console") {
196                                parametersTag = v;
197                            }
198                        });
199
200                        // Adjust console area from CSS styling positoning information
201                        var consoleHeight = parseInt(parametersTag.clientHeight.toString().replace('px','')) - 150 + 'px';
202                        var consoleWidth = parseInt(parametersTag.clientWidth.toString().replace('px','')) / 3 * 2 - 150 + 'px';
203                        consoleTag.style.height = consoleHeight;
204                        consoleTag.style.width = consoleWidth;
205                        /*   Create console area
206                         * 
207                         */
208                    }
209
210                    public help() {
211                        this.APIEndPoint
212                        .help(this.name)
213                        .then((result) => {
214                            console.log(result);
215                        });
216                    }
217
218     } 
219 }