Yangman - bugfixes and improvements
[dlux.git] / modules / yangman-resources / src / main / resources / yangman / controllers / yangman.controller.js
1 define([
2     'app/yangman/yangman.filters',
3     'app/yangman/controllers/modules-list.controller',
4     'app/yangman/controllers/module-detail.controller',
5     'app/yangman/controllers/yang-form.controller',
6     'app/yangman/controllers/requests-list.controller',
7     'app/yangman/controllers/request-header.controller',
8     'app/yangman/controllers/request-data.controller',
9     'app/yangman/services/yangman.services',
10     'app/yangman/services/handle-file.services',
11     'app/yangman/services/yangman-design.services',
12     'app/yangman/services/requests.services',
13     'app/yangman/services/parameters.services',
14     'app/yangman/services/plugins-unsetter.services',
15     'app/yangman/services/history-settings.services',
16     'app/yangman/directives/ui-codemirror.directive',
17     'app/yangman/directives/read_file.directive',
18     'app/yangman/directives/height-watcher.directive',
19 ], function () {
20     'use strict';
21
22     angular.module('app.yangman').controller('YangmanCtrl', YangmanCtrl);
23
24     YangmanCtrl.$inject = [
25         '$scope', '$rootScope', 'HistorySettingsService', 'YangmanDesignService', 'RequestBuilderService',
26         'EventDispatcherService', 'constants', 'ParametersService', 'PathUtilsService', 'PluginsUnsetterService',
27         '$timeout',
28     ];
29
30     function YangmanCtrl(
31         $scope, $rootScope, HistorySettingsService, YangmanDesignService, RequestBuilderService, EventDispatcherService,
32         constants, ParametersService, PathUtilsService, PluginsUnsetterService, $timeout
33     ) {
34         var main = this;
35
36         $rootScope.section_logo = 'assets/images/logo_yangman.png';
37         $scope.globalViewPath = 'src/app/yangman/views/';
38
39         $scope.selectedModule = null;
40         $scope.selectedDatastore = null;
41         $scope.selectedPlugin = false;
42         $scope.apis = [];
43         $scope.node = null;
44         $scope.rightPanelSection = constants.DISPLAY_TYPE_REQ_DATA;
45         $scope.augmentations = {};
46         $scope.selectedApi = null;
47         $scope.selectedSubApi = null;
48         $scope.historyReqsSelected = false;
49         $scope.requestToShow = null;
50         $scope.requestDataToShow = '';
51         $scope.parametersList = ParametersService.createEmptyParametersList('yangman_parameters');
52         $scope.shownCMHint = false;
53         $scope.historySettings = HistorySettingsService.createHistorySettings().loadFromStorage();
54
55         main.selectedMainTab = 0;
56         main.leftPanelTab = 0;
57         main.jsonView = {
58             received: true,
59             sent: false,
60         };
61         main.executingRequestProgress = false;
62         main.constants = constants;
63
64         main.init = init;
65         main.initModuleDetailHeight = initModuleDetailHeight;
66         main.switchedTab = switchedTab;
67         main.toggleLeftPanel = toggleLeftPanel;
68         main.leftPanelShowModule = leftPanelShowModule;
69         main.modulesTreeDisplayed = modulesTreeDisplayed;
70
71         // scope global methods
72         $scope.buildRootRequest = buildRootRequest;
73         $scope.checkAddingListElement = checkAddingListElement;
74         $scope.clearCM = clearCM;
75         $scope.rootBroadcast = rootBroadcast;
76         $scope.setApi = setApi;
77         $scope.setDataStore = setDataStore;
78         $scope.setGlobalParams = setGlobalParams;
79         $scope.setJsonView = setJsonView;
80         $scope.setLeftPanel = setLeftPanel;
81         $scope.setModule = setModule;
82         $scope.setNode = setNode;
83         $scope.setRequestToShow = setRequestToShow;
84         $scope.setRightPanelSection = setRightPanelSection;
85         $scope.switchSection = switchSection;
86         $scope.setParametersList = setParametersList;
87         $scope.unsetPlugin = unsetPlugin;
88         $scope.setSelectedPlugin = setSelectedPlugin;
89
90
91         init();
92
93
94         function setSelectedPlugin(selected) {
95             $scope.selectedPlugin = selected;
96         }
97
98         /**
99          * Start showing progressbar in request header view
100          */
101         function startExecutingRequestProgress() {
102             main.executingRequestProgress = true;
103         }
104
105
106         /**
107          * Stop showing progressbar in request header view
108          */
109         function stopExecutingRequestProgress() {
110             main.executingRequestProgress = false;
111         }
112
113         /**
114          * Set parametersList to $scope to be available for all controllers directly
115          * @param parametersList
116          */
117         function setParametersList(parametersList) {
118             $scope.parametersList = parametersList;
119         }
120
121
122         /**
123          * Initialization
124          */
125         function init(){
126             $scope.$on(constants.YANGMAN_EXECUTING_REQUEST_PROGRESS_START, startExecutingRequestProgress);
127             $scope.$on(constants.YANGMAN_EXECUTING_REQUEST_PROGRESS_STOP, stopExecutingRequestProgress);
128
129             YangmanDesignService.hideMainMenu();
130             YangmanDesignService.setDraggableLeftPanel();
131             YangmanDesignService.setJsonSplitter(forceCMsRefresh);
132
133             EventDispatcherService.registerHandler(constants.EV_FILL_PATH, fillPathIdentifiersByKey);
134             EventDispatcherService.registerHandler(constants.EV_LIST_CHANGED, fillPathIdentifiersByListData);
135         }
136
137         /**
138          * Initialize module detail height, with timeout
139          */
140         function initModuleDetailHeight(){
141             $timeout(function () {
142                 YangmanDesignService.setModuleDetailHeight();
143             }, 1500);
144         }
145
146         /**
147          * Method for fill key into request path, used by yangutils via event dispatching
148          * @param inputs
149          */
150         function fillPathIdentifiersByKey(inputs) {
151             var node = inputs[0],
152                 value = inputs[1] || '';
153
154             if ($scope.selectedSubApi && node.parent && $scope.selectedSubApi.node.id === node.parent.id) {
155                 var identifiers =
156                     $scope.selectedSubApi.pathArray[$scope.selectedSubApi.pathArray.length - 1].identifiers;
157
158                 PathUtilsService.fillIdentifiers(identifiers, node.label, value);
159             }
160         }
161
162
163         /**
164          * Method for bulk filling path identifiers, used by yangutils via event dispatching
165          * @param inputs
166          */
167         function fillPathIdentifiersByListData(inputs) {
168             var node = inputs[0];
169
170             if ($scope.selectedSubApi && node && $scope.selectedSubApi.node.id === node.id) {
171                 var identifiers =
172                         $scope.selectedSubApi.pathArray[$scope.selectedSubApi.pathArray.length - 1].identifiers,
173                     keys = node.refKey;
174
175                 keys.forEach(function (key) {
176                     PathUtilsService.fillIdentifiers(identifiers, key.label, key.value);
177                 });
178             }
179         }
180
181
182         /**
183          * Check if the main tab containing tree with modules is displayed
184          * @returns {boolean}
185          */
186         function modulesTreeDisplayed() {
187             return main.selectedMainTab === 0;
188         }
189
190         /**
191          * Set switched tab index
192          */
193         function switchedTab(index){
194             main.selectedMainTab = index;
195         }
196
197         /**
198          * Switcher between modules list and module detail
199          */
200         function toggleLeftPanel(){
201             main.leftPanelTab = (main.leftPanelTab + 1) % 2;
202         }
203
204         /**
205          * Method for opening model detail tab
206          */
207         function leftPanelShowModule() {
208             if ($scope.node) {
209                 main.leftPanelTab = 1;
210             }
211         }
212
213         /**
214          * General method for switching different section in application
215          * @param param
216          * @param section
217          */
218         function switchSection(param, section){
219             $scope[param] = section;
220         }
221
222         /**
223          * Genereal method for clearing code mirror - both sent and received data, needs to be in $scope to be available
224          * everywhere
225          */
226         function clearCM(){
227             $scope.rootBroadcast(constants.YANGMAN_SET_CODEMIRROR_DATA_RECEIVED, { data: JSON.stringify({}) });
228             $scope.rootBroadcast(constants.YANGMAN_SET_CODEMIRROR_DATA_SENT, { data: JSON.stringify({}) });
229         }
230
231
232         /**
233          * Switcher for module detail and module list
234          * @param value
235          */
236         function setLeftPanel(value) {
237             if ( !angular.isUndefined(value) ) {
238                 main.leftPanelTab = value;
239             }
240         }
241
242         /**
243          * Set global necessary params
244          * @param apis
245          * @param augmentations
246          */
247         function setGlobalParams(apis, augmentations){
248             $scope.apis = apis;
249             $scope.augmentations = augmentations;
250         }
251
252         /**
253          * Set node to global param
254          * @param node
255          */
256         function setNode(node){
257             $scope.node = node;
258
259             if ( $scope.node ) {
260                 $scope.node.clear();
261                 $scope.$broadcast(constants.YANGMAN_DISABLE_ADDING_LIST_ELEMENT);
262             }
263         }
264
265         /**
266          * Set module to global param
267          * @param module
268          */
269         function setModule(module){
270             $scope.selectedModule = module;
271         }
272
273         /**
274          * Set dataStore to global param, open module detail in left panel
275          * @param dataStore
276          * @param expand
277          * @param leftPanel
278          */
279         function setDataStore(dataStore, expand, leftPanel){
280             $scope.selectedDatastore = dataStore;
281
282             if ( expand ) {
283                 $scope.node = null;
284                 setLeftPanel(leftPanel);
285                 $scope.$broadcast(constants.YANGMAN_MODULE_D_INIT);
286             } else {
287                 if ( $scope.node ) {
288                     $scope.node.clear();
289                 }
290             }
291         }
292
293         /**
294          * Build request json from root node
295          */
296         function buildRootRequest() {
297             var obj = {};
298             $scope.node.buildRequest(RequestBuilderService, obj, $scope.node.module);
299             return obj;
300         }
301
302         /**
303          * Set api and sub api to global param
304          * @param api
305          * @param subApi
306          * @param setUrl
307          * @param clearPathArray
308          */
309         function setApi(api, subApi, setUrl, clearPathArray){
310             var oldSubApiPathArray = $scope.selectedSubApi ? angular.copy($scope.selectedSubApi.pathArray) : [];
311             $scope.selectedApi = api;
312             $scope.selectedSubApi = subApi;
313
314             if ( clearPathArray ){
315                 PathUtilsService.clearPath($scope.selectedSubApi.pathArray);
316
317                 // todo: move to pathUtils service
318                 $scope.selectedSubApi.pathArray.forEach(function (subApiPathElem) {
319                     oldSubApiPathArray.forEach(function (oldSubApiPathElem){
320                         if (oldSubApiPathElem.module === subApiPathElem.module && oldSubApiPathElem.name === subApiPathElem.name) {
321                             oldSubApiPathElem.identifiers.forEach(function (oldPathElemIdentifier, oldPathElemIdentifierKey){
322                                 subApiPathElem.identifiers.forEach(function (pathElemIdentifier) {
323                                     if (oldPathElemIdentifier.label === pathElemIdentifier.label) {
324                                         pathElemIdentifier.value = oldPathElemIdentifier.value;
325                                         oldSubApiPathElem.identifiers.splice(oldPathElemIdentifierKey, 1);
326
327                                     }
328                                 });
329                             });
330                         }
331                     });
332                 });
333
334
335
336             }
337
338             $scope.$broadcast(constants.SET_SEL_OPERATIONS, subApi ? $scope.selectedSubApi.operations : [], setUrl);
339         }
340
341         /**
342          * Call broadcast from root to child controllers
343          * @param broadcast
344          * @param params
345          * @param cbk
346          */
347         function rootBroadcast(broadcast, params, cbk){
348             $scope.$broadcast(broadcast, { params: params, cbk: cbk });
349         }
350
351         /**
352          * Set request from history or collections to show data in code mirror
353          * @param reqObj
354          * @param {'sentData'|'receivedData'} dataType
355          */
356         function setRequestToShow(reqObj, dataType) {
357             $scope.requestToShow = reqObj;
358             $scope.requestDataToShow = dataType;
359         }
360
361         /**
362          * Set rightPanelSection to display current section in right panel
363          * @param section 'form', 'req-data'
364          */
365         function setRightPanelSection(section) {
366             $scope.rightPanelSection = section;
367         }
368
369         /**
370          * Which codemirror instances will be displayed
371          * @param received
372          * @param sent
373          */
374         function setJsonView(received, sent){
375             main.jsonView.received = received;
376             main.jsonView.sent = sent;
377             forceCMsRefresh();
378             YangmanDesignService.setJsonSplitter(forceCMsRefresh);
379         }
380
381         /**
382          * Force refresh of all codemirror instances
383          */
384         function forceCMsRefresh(){
385             var elems = angular.element(document).find('.CodeMirror');
386             for (var i = 0; i < elems.length; i++){
387                 var cmInstance = elems[i].CodeMirror;
388                 cmInstance._handlers.changes[0](cmInstance);
389             }
390         }
391
392         /**
393          * Global method for unset plugin
394          * @param selectedPlugin
395          * @param controller
396          */
397         function unsetPlugin(controller){
398             PluginsUnsetterService.unset($scope, controller);
399         }
400
401         /**
402          * Checks if the element list should be disabled
403          */
404         function checkAddingListElement(node) {
405             return $scope.node === node && $scope.node.type === 'list' &&
406                 $scope.node.refKey && $scope.node.refKey.length;
407         }
408     }
409
410 });