Merge "Yangman - clear indetifiers, fix boolean type, fix history req sent data"
[dlux.git] / modules / yangman-resources / src / main / resources / yangman / controllers / modules-list.controller.js
1 define([
2     'app/yangman/services/plugins-handler.services',
3 ], function () {
4     'use strict';
5
6     angular.module('app.yangman').controller('ModulesListCtrl', ModulesListCtrl);
7
8     ModulesListCtrl.$inject = ['$scope', '$rootScope', '$mdToast', 'YangUtilsService', 'PluginsHandlerService',
9                                 '$filter', '$timeout'];
10
11     function ModulesListCtrl($scope, $rootScope, $mdToast, YangUtilsService, PluginsHandlerService, $filter, $timeout) {
12         var modulesList = this;
13
14         modulesList.treeApis = [];
15         modulesList.showLoadingBox = true;
16         modulesList.moduleListTitle = '';
17         modulesList.search = '';
18
19         // methods
20         modulesList.clearFilter = clearFilter;
21         modulesList.customSearch = customSearch;
22         modulesList.checkSelectedModule = checkSelectedModule;
23         modulesList.setDataStore = setDataStore;
24         modulesList.setModule = setModule;
25
26         // watchers
27         $scope.$on('YANGMAN_GET_API_TREE_DATA', function (event, args) {
28             (args.cbk || angular.noop)(modulesList.treeApis);
29         });
30
31         // set tree apis data
32         $scope.$on('YANGMAN_SET_API_TREE_DATA', function (event, args) {
33             modulesList.treeApis = args.params;
34             modulesList.showLoadingBox = false;
35             showToastInfoBox('YANGMAN_LOADED_MODULES');
36         });
37
38         // show hide loading box
39         $scope.$on('YANGMAN_SET_LOADING_BOX', function (event, args){
40             modulesList.showLoadingBox = args.params;
41             (args.cbk || angular.noop)();
42         });
43
44         // show info box with custom title
45         $scope.$on('YANGMAN_SHOW_TOAST', function (event, args) {
46             showToastInfoBox(args.params);
47         });
48
49         $scope.$on('YANGMAN_SET_MODULE_LIST_TITLE', function (event, args) {
50             modulesList.moduleListTitle = args.params;
51         });
52
53         /**
54          * Initialization
55          */
56         function init(){
57             loadApis();
58         }
59
60         init();
61
62         /**
63          * Check if module and one of it datastore is selected
64          * @param module
65          * @returns {boolean|*|Function|o}
66          */
67         function checkSelectedModule(module){
68             var haveSelectedDS = [];
69
70             if ( $scope.selectedDatastore && (module === $scope.selectedModule)) {
71                 haveSelectedDS = $scope.selectedModule.children.filter(function(item){
72                    return item === $scope.selectedDatastore;
73                 });
74             }
75
76             return haveSelectedDS.length;
77         }
78
79         /**
80          * Custom search function for searching by api label
81          * @param api
82          */
83         function customSearch(api){
84             return api.label.toLowerCase().indexOf(modulesList.search.toLowerCase()) > -1;
85         }
86
87         /**
88          * Clear current ctrl search value
89          */
90         function clearFilter(){
91             modulesList.search = '';
92         }
93
94         /**
95          * Load apis and modules
96          */
97         function loadApis() {
98             modulesList.allNodes = [];
99             modulesList.treeApis = [];
100
101             modulesList.showLoadingBox = true;
102
103             YangUtilsService.generateNodesToApis(function (apis, allNodes, augGroups) {
104                 $scope.setGlobalParams(apis, augGroups);
105                 modulesList.allNodes = allNodes;
106                 // console.info('INFO :: got data', apis, modulesList.allNodes, modulesList.augmentations);
107                 modulesList.treeApis = YangUtilsService.generateApiTreeData(apis);
108                  //console.info('INFO :: tree api', modulesList.treeApis);
109                 // $scope.processingModulesSuccessCallback();
110                 modulesList.showLoadingBox = false;
111                 showToastInfoBox('YANGMAN_LOADED_MODULES');
112
113                 PluginsHandlerService.plugAll(apis, modulesList);
114                 // $scope.$broadcast('LOAD_REQ_DATA');
115             }, function () {
116                 showToastInfoBox('YANGMAN_LOADED_MODULES_ERROR');
117                 modulesList.showLoadingBox = false;
118             });
119         }
120
121         /**
122          * Set and expand module in tree
123          */
124         function setModule(module, e){
125             if ( $(e.target).hasClass('top-element') ) {
126                 module.expanded = !module.expanded;
127                 //$scope.setModule(module);
128             }
129         }
130
131         /**
132          * Set data store || rpc
133          * @param dataStore
134          * @param module
135          */
136         function setDataStore(dataStore, module){
137             $scope.setModule(module);
138             $scope.setDataStore(dataStore, true, 1);
139         }
140
141         /**
142          * Method for showing toast box
143          * @param text
144          */
145         function showToastInfoBox(text){
146             $timeout(function(){
147                 $mdToast.show(
148                     $mdToast.simple()
149                         .textContent($filter('translate')(text))
150                         .position('bottom left')
151                         .parent(angular.element('.yangmanModule__left-panel'))
152                         .hideDelay(3000)
153                 );
154             }, 500);
155         }
156     }
157
158 });