OSDN Git Service

Create console area computed from CSS style pixels
[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'];
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                    ) {
73             var controller = this;
74
75             // Get OptionControlFile and bind it to $scope.options
76             this.APIEndPoint
77             //.getOptionControlFile('mrcImageNoiseAdd')
78             .getOptionControlFile(this.name)
79             .$promise
80             .then(function(result) {
81                 controller.options = result.info;
82             });
83
84             this.APIEndPoint
85             .getDirectories()
86             .$promise
87             .then(function(result) {
88                 controller.dirs = result.info;
89             });
90
91             this.heading = "[" + this.index + "]: dcdFilePrint";
92             this.isOpen = true;
93
94             // When call add button at executeController, all accordion close.
95             this.$scope.$on('close', () => {
96                 controller.isOpen = false;
97             });
98
99             this.WebSocket.on('console', function(msg) {
100                 console.log(msg);
101                 var messages = msg.split('\n');
102                 if(messages[0].substr(0,6) === 'Usage:') {
103                     controller.messages = msg.split('\n');
104                 }
105             });
106
107         }
108
109         public submit() {
110             var opt = [];
111             angular.forEach(this.options, (option) => {
112
113                 var obj = {
114                     name: option.option,
115                     arguments: []
116                 };
117
118                 angular.forEach(option.arg, (arg) => {
119                     if(arg.input) {
120                         if(typeof arg.input === 'object') {
121                             obj.arguments.push(arg.input.name);
122                         } else {
123                             obj.arguments.push(arg.input);
124                         }
125                     }
126                 });
127
128                 if(obj.arguments.length > 0) {
129                     opt.push(obj);
130                 }
131             });
132
133             var execObj = {
134                 command: this.name,
135                 workspace: this.workspace.fileId,
136                 options: opt
137             };
138
139             //console.log(JSON.stringify(execObj,null,'\t'));
140             this.APIEndPoint
141             .execute(JSON.stringify(execObj))
142             .then((result) => {
143                 console.log(result);
144             })
145         }
146
147         public removeMySelf(index:number) {
148             this.remove()(index,this.list);
149         }
150
151         private reloadFiles() {
152             var fileId = this.workspace.fileId;
153             this.APIEndPoint
154             .getFiles(fileId)
155             .$promise
156             .then((result) => { 
157                 var status = result.status;
158                 if(status === 'success') {
159                     this.files = result.info;
160                 } else {
161                     console.log(result.message);
162                 }
163             });
164         }
165
166         public debug() {
167             var div = angular.element(this.$window.document).find("div");
168             var consoleTag;
169             var parametersTag;
170             angular.forEach(div,function(v) {
171                 if(v.className === "panel-body console") {
172                     consoleTag = v;
173                 } else if(v.className === "row parameters-console") {
174                     parametersTag = v;
175                 }
176             });
177
178             // Adjust console area from CSS styling positoning information
179             var consoleHeight = parseInt(parametersTag.clientHeight.toString().replace('px','')) - 150 + 'px';
180             var consoleWidth = parseInt(parametersTag.clientWidth.toString().replace('px','')) / 3 * 2 - 150 + 'px';
181             consoleTag.style.height = consoleHeight;
182             consoleTag.style.width = consoleWidth;
183         }
184         
185         public help() {
186             this.APIEndPoint
187             .help(this.name)
188             .then((result) => {
189                 console.log(result);
190             });
191         }
192
193     } 
194 }