Merge "Yangman - strings to constants"
[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                 modulesList.treeApis = YangUtilsService.generateApiTreeData(apis);
107                 // $scope.processingModulesSuccessCallback();
108                 modulesList.showLoadingBox = false;
109                 showToastInfoBox('YANGMAN_LOADED_MODULES');
110
111                 PluginsHandlerService.plugAll(apis, modulesList);
112                 // $scope.$broadcast('LOAD_REQ_DATA');
113             }, function () {
114                 showToastInfoBox('YANGMAN_LOADED_MODULES_ERROR');
115                 modulesList.showLoadingBox = false;
116             });
117         }
118
119         /**
120          * Set and expand module in tree
121          */
122         function setModule(module, e){
123             if ( $(e.target).hasClass('top-element') ) {
124                 module.expanded = !module.expanded;
125                 //$scope.setModule(module);
126             }
127         }
128
129         /**
130          * Set data store || rpc
131          * @param dataStore
132          * @param module
133          */
134         function setDataStore(dataStore, module){
135             $scope.setModule(module);
136             $scope.setDataStore(dataStore, true, 1);
137         }
138
139         /**
140          * Method for showing toast box
141          * @param text
142          */
143         function showToastInfoBox(text){
144             $timeout(function(){
145                 $mdToast.show(
146                     $mdToast.simple()
147                         .textContent($filter('translate')(text))
148                         .position('bottom left')
149                         .parent(angular.element('.yangmanModule__left-panel'))
150                         .hideDelay(3000)
151                 );
152             }, 500);
153         }
154     }
155
156 });