d7e24fd408ca1fb06c915dbe38aa63e6c7069f7c
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / dlux / dlux-web / src / app / core / core.services.js
1 define(['jquery'], function ($) {
2   'use strict';
3
4   var TopBarHelper = function () {
5     var ids = [];
6     var ctrls = [];
7
8     this.addToView = function (url) {
9       $.ajax({
10         url: url,
11         method: 'GET',
12         async: false
13       }).done(function (data) {
14         ids.push(data);
15       });
16     };
17
18     this.getViews = function () {
19       var template = '';
20
21       for (var i = 0; i < ids.length; ++i) {
22         template += ids[i];
23       }
24
25       return template;
26     };
27
28     this.addControllerUrl = function (url) {
29       ctrls.push(url);
30     };
31
32     this.getControllers = function () {
33       return ctrls;
34     };
35
36     this.$get = ['apiToken', function (apiToken) {
37       return new TopBarHelper(apiToken);
38     }];
39   };
40
41   var NavHelper = function () {
42     var ids = [];
43     var ctrls = [];
44     var menu = [];
45
46     function NavHelperProvider() {
47       this.addToView = function (url) {
48         $.ajax({
49           url: url,
50           method: 'GET',
51           async: false
52         }).done(function (data) {
53           ids.push(data);
54         });
55       };
56
57       this.getViews = function () {
58         var template = '';
59
60         for (var i = 0; i < ids.length; ++i) {
61           template += ids[i];
62         }
63
64         return template;
65       };
66
67       this.addControllerUrl = function (url) {
68         ctrls.push(url);
69       };
70
71       this.getControllers = function () {
72         return ctrls;
73       };
74
75       var getMenuWithId = function (menu, level) {
76         if (menu === undefined) {
77           return null;
78         }
79         var currentLevel = level[0];
80
81         var menuItem = $.grep(menu, function (item) {
82           return item.id === currentLevel;
83         })[0];
84
85         if (level.length === 1) {
86           return menuItem;
87         } else {
88           return getMenuWithId(menuItem.submenu, level.slice(1));
89         }
90       };
91
92       this.addToMenu = function (id, obj) {
93         var lvl = id.split('.');
94         obj.id = lvl.pop();
95
96         if (lvl.length === 0) {
97           menu.push(obj);
98         } else {
99           var menuItem = getMenuWithId(menu, lvl);
100
101           if (menuItem) {
102             if (!menuItem.submenu) {
103               menuItem.submenu = [];
104             }
105             menuItem.submenu.push(obj);
106           } else {
107             var submenu = {
108               'id': lvl[0],
109               'title': lvl[0],
110               'active': '',
111               'submenu': [obj]
112             };
113             menu.push(submenu);
114           }
115         }
116       };
117
118       this.getMenu = function () {
119         return menu;
120       };
121
122       this.$get = function NavHelperFactory() {
123         return new NavHelperProvider();
124       };
125     }
126     var persistentProvider = new NavHelperProvider();
127
128     return persistentProvider;
129
130   };
131
132   var ContentHelper = function () {
133     var ids = [];
134     var ctrls = [];
135
136     function ContentHelperProvider() {
137       this.addToView = function (url) {
138         $.ajax({
139           url: url,
140           method: 'GET',
141           async: false
142         }).done(function (data) {
143           ids.push(data);
144         });
145       };
146
147       this.getViews = function () {
148         var template = '';
149
150         for (var i = 0; i < ids.length; ++i) {
151           template += ids[i];
152         }
153
154         return template;
155       };
156
157       this.addControllerUrl = function (url) {
158         ctrls.push(url);
159       };
160
161       this.getControllers = function () {
162         return ctrls;
163       };
164
165       this.$get = function ContentHelperFactory() {
166         return new ContentHelperProvider();
167       };
168     }
169     var persistentProvider = new ContentHelperProvider();
170
171     return persistentProvider;
172
173   };
174
175   return {
176     ContentHelper: ContentHelper,
177     NavHelper: NavHelper,
178     TopBarHelper: TopBarHelper
179   };
180
181 });