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