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