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