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