Merge "Yangman - moint points"
[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'];
10
11     function ModulesListCtrl($scope, $rootScope, $mdToast, YangUtilsService, PluginsHandlerService, $filter) {
12         var modulesList = this;
13
14         modulesList.treeApis = [];
15         modulesList.showLoadingBox = true;
16         modulesList.moduleListTitle = '';
17
18         // methods
19         modulesList.setDataStore = setDataStore;
20         modulesList.setModule = setModule;
21
22         // watchers
23         $scope.$on('YANGMAN_GET_API_TREE_DATA', function (event, args) {
24             (args.cbk || angular.noop)(modulesList.treeApis);
25         });
26
27         // set tree apis data
28         $scope.$on('YANGMAN_SET_API_TREE_DATA', function (event, args) {
29             modulesList.treeApis = args.params;
30             modulesList.showLoadingBox = false;
31             showToastInfoBox('YANGMAN_LOADED_MODULES');
32         });
33
34         // show hide loading box
35         $scope.$on('YANGMAN_SET_LOADING_BOX', function (event, args){
36             modulesList.showLoadingBox = args.params;
37             (args.cbk || angular.noop)();
38         });
39
40         // show info box with custom title
41         $scope.$on('YANGMAN_SHOW_TOAST', function (event, args) {
42             showToastInfoBox(args.params);
43         });
44
45         $scope.$on('YANGMAN_SET_MODULE_LIST_TITLE', function (event, args) {
46             modulesList.moduleListTitle = args.params;
47         });
48
49         /**
50          * Initialization
51          */
52         function init(){
53             loadApis();
54         }
55
56         init();
57
58         /**
59          * Load apis and modules
60          */
61         function loadApis() {
62             modulesList.allNodes = [];
63             modulesList.treeApis = [];
64
65             modulesList.showLoadingBox = true;
66
67             YangUtilsService.generateNodesToApis(function (apis, allNodes, augGroups) {
68                 $scope.setGlobalParams(apis, augGroups);
69                 modulesList.allNodes = allNodes;
70                 // console.info('INFO :: got data', apis, modulesList.allNodes, modulesList.augmentations);
71                 modulesList.treeApis = YangUtilsService.generateApiTreeData(apis);
72                  //console.info('INFO :: tree api', modulesList.treeApis);
73                 // $scope.processingModulesSuccessCallback();
74                 modulesList.showLoadingBox = false;
75                 showToastInfoBox('YANGMAN_LOADED_MODULES');
76
77                 PluginsHandlerService.plugAll(apis, modulesList);
78                 // $scope.$broadcast('LOAD_REQ_DATA');
79             }, function () {
80                 showToastInfoBox('YANGMAN_LOADED_MODULES_ERROR');
81                 modulesList.showLoadingBox = false;
82             });
83         }
84
85         /**
86          * Set and expand module in tree
87          */
88         function setModule(module, e){
89             if ( $(e.target).hasClass('top-element') ) {
90                 module.expanded = !module.expanded;
91                 $scope.setModule(module);
92             }
93         }
94
95         /**
96          * Set data store || rpc
97          * @param dataStore
98          * @param module
99          */
100         function setDataStore(dataStore, module){
101             $scope.setModule(module);
102             $scope.setDataStore(dataStore, true, 1);
103         }
104
105         /**
106          * Method for showing toast box
107          * @param text
108          */
109         function showToastInfoBox(text){
110             $mdToast.show(
111                 $mdToast.simple()
112                     .textContent($filter('translate')(text))
113                     .position('bottom left')
114                     .hideDelay(3000)
115             );
116         }
117     }
118
119 });