Updated Modello Common libraries
[profile/ivi/sdk/web-ide-resources.git] / web-ui-fw / 0.0.2 / 0.0.2_Common / original / css / car / components / topBarIcons / topBarIcons.js
1 /*global ThemeEngine, loadScript, template, changeCssBgImageColor, ThemeKeyColorSelected, ThemeKeyColor, Speech, Settings */
2
3 /**
4  * @module CarTheme
5  **/
6 /**
7  * Array of applications that will be rendered to the top bar panel.
8  * @property topBarAplicationsModel
9  * @type {Array}
10  * @for window
11  */
12
13 var topBarAplicationsModel = [];
14
15 /**
16  * Array of applications that would not fit the top bar panel.
17  * @property extraAppsModel
18  * @type {Array}
19  * @for global
20  */
21 var extraAppsModel = [];
22
23 var appToHide;
24
25 /**
26  * The callback to be invoked when the application was not launched successfully.
27  *
28  * @method onError
29  * @for window
30  * @param err {Any} An error message.
31  */
32 function onError(err) {
33         "use strict";
34         console.error(err);
35         alert("An error occured while launching the application: " + err.message);
36 }
37
38 /**
39  * Returns the application object by application id from topBarAplicationsModel.
40  *
41  * @method getAppByID
42  * @for window
43  * @param id {String} Application ID.
44  * @return {Object} Object representing basic application's information.
45  */
46 function getAppByID(id) {
47         "use strict";
48         var j, i = 0;
49         for (j = 0; j < topBarAplicationsModel.length; ++j) {
50                 if (id === topBarAplicationsModel[j].id) {
51                         return topBarAplicationsModel[j];
52                 }
53         }
54         for (i = 0; i < extraAppsModel.length; ++i) {
55                 if (id === extraAppsModel[i].id) {
56                         return extraAppsModel[i];
57                 }
58         }
59
60         return null;
61 }
62
63 /**
64  * Returns the application object by application name from topBarAplicationsModel.
65  *
66  * @method getAppByName
67  * @for window
68  * @param appName {String} Application name.
69  * @return {Object} Object representing basic application's information.
70  */
71 function getAppByName(appName) {
72         "use strict";
73         for (var j = 0; j < topBarAplicationsModel.length; ++j) {
74                 if (appName.toString().trim().toLowerCase() === topBarAplicationsModel[j].appName.toString().trim().toLowerCase()) {
75                         return topBarAplicationsModel[j];
76                 }
77         }
78         for (var i = 0; i < extraAppsModel.length; ++i) {
79                 if (appName.toString().trim().toLowerCase() === extraAppsModel[i].appName.toString().trim().toLowerCase()) {
80                         return extraAppsModel[i];
81                 }
82         }
83
84         return null;
85 }
86
87 /**
88  * The callback to be invoked when the application was launched successfully.
89  *
90  * @method onAppInfoSuccess
91  * @for window
92  */
93 function onLaunchSuccess() {
94         "use strict";
95         console.log("App launched succesfully...");
96
97         if (appToHide && appToHide.name !== "ModelloHomescreen")
98                 appToHide.hide();
99 }
100
101 /**
102  * Launches an application with the given application ID.
103  *
104  * @method launchApplication
105  * @for window
106  * @param id {String} Application ID.
107  */
108 function launchApplication(id) {
109         "use strict";
110         if (id === "http://com.intel.tizen/settings") {
111                 if (typeof Settings === 'undefined') {
112                         loadScript('./css/car/components/settings/js/settings.js', function(path, status) {
113                                 if (status === "ok") {
114                                         Settings.init();
115                                 }
116                         });
117                 } else {
118                         Settings.show();
119                 }
120                 return;
121         }
122
123         var app = getAppByID(id);
124         if ( !! app) {
125                 if (app.installed && !app.running) {
126                         appToHide = tizen.application.getCurrentApplication();
127                         tizen.application.launch(app.id, onLaunchSuccess, onError);
128                 } else if (app.running) {
129                         console.log("Application is running!");
130                 }
131         } else {
132                 console.log("Application is not installed!");
133                 alert("Application is not installed!");
134         }
135 }
136
137 (function($) {
138         "use strict";
139         /**
140          * Represents top bar UI control element that is used to navigate between predefined set of applications. Applications displayed in this list must have
141          * [application ID](https://developer.tizen.org/dev-guide/2.2.1/org.tizen.web.device.apireference/tizen/application.html#::Application::ApplicationId)
142          * starting with `intelPoC` prefix (e.g. `html5POC04.HomeScreen`). Use following snippet to include component in your `index.html` file:
143      *
144      *     <script type='text/javascript' src='./css/car/components/topBarIcons/topBarIcons.js'></script>
145          *     <link rel="stylesheet" href="./css/car/components/topBarIcons/topBarIcons.css" />
146      *
147      * and following code to initialize:
148      *
149      *     $("#topBarIcons").topBarIconsPlugin('init');
150      *
151          * @class TopBarIcons
152          * @constructor
153          */
154         var TopBarIcons = {
155                 /**
156                  * Name of the application that is used to highlight only the icon of currently running application.
157                  * @property runningAppName
158                  * @type {String}
159                  * @default ""
160                  */
161                 runningAppName: "",
162
163                 /**
164                  * Initializes the element and gets the list of installed application.
165                  *
166                  * @method init
167                  * @param appName {String} Name of running application.
168                  */
169                 init: function() {
170                         TopBarIcons.initLaunchingAppsByVoiceRecognition();
171                         TopBarIcons.runningAppName = typeof(tizen.application.getCurrentApplication) === 'undefined' ? "" : tizen.application.getCurrentApplication().appInfo.id;
172
173                         this.empty();
174                         this.addClass("topBarIcons");
175
176                         if (typeof ThemeEngine !== 'undefined') {
177                                 ThemeEngine.addStatusListener(function(themeId) {
178                                         TopBarIcons.renderApps();
179                                 });
180                         } else {
181                                 console.error("ThemeEngine API is not available.");
182                         }
183
184                         TopBarIcons._getApps();
185                 },
186
187                 _getApps: function() {
188                         try {
189                                 if (typeof(tizen.application.getAppsInfo) !== 'undefined') {
190                                         tizen.application.getAppsInfo(TopBarIcons.onAppInfoSuccess, function(err) {
191                                                 // Workaround due to https://bugs.tizen.org/jira/browse/TIVI-2018
192                                                 window.setTimeout(function() {
193                                                         TopBarIcons._getApps();
194                                                 }, 1000);
195
196                                                 onError(err);
197                                         });
198                                 }
199                         } catch (exc) {
200                                 console.error(exc.message);
201                         }
202                 },
203                 appList: [],
204                 prevThemeColor: "",
205                 /**
206                  * The callback to be invoked when the installed application list is retrieved. Compares list of installed applications to predefined list and sets installed and running properties, that are used when launching an application.
207                  * Calls render application icons function.
208                  * @method onAppInfoSuccess
209                  * @param list {Array} Array of installed application.
210                  */
211                 onAppInfoSuccess: function(list) {
212                         try {
213                                 var appListLenght;
214                                         var i = 0,
215                                                 j = 0;
216                                         var modelData = [];
217                                         var homeScreenApp = {};
218
219                                         for (i = 0; i < list.length; i++) {
220                                                 var app = list[i];
221                                                 var subName = app.name.substr(7);
222                                                 subName = subName.replace(/\./g, "_").replace(/\ /g, "_");
223                                                 if (app.name.indexOf('Modello') >= 0) {
224                                                         var newApp = {
225                                                                 id: app.id,
226                                                                 appName: app.name,
227                                                                 style: "background-image: url('icons/" + subName + "_icon.png');",
228                                                                 iconPath: app.iconPath,
229                                                                 css: "app_" + app.id.replace(/\./g, "_").replace(/\ /g, "_"),
230                                                                 installed: true,
231                                                                 running: TopBarIcons.runningAppName === app.id
232                                                         };
233
234                                                         if (app.name === "ModelloHomescreen") {
235                                                                 homeScreenApp = newApp;
236                                                         } else {
237                                                                 //app filter to block adding some apps into topbar
238                                                                 if (app.name === 'ModelloAMBSimulator') {
239                                                                         extraAppsModel.push(newApp);
240                                                                 } else if (modelData.length < 7) {
241                                                                         modelData.push(newApp);
242                                                                 }
243                                                         }
244                                                 }
245                                         }
246
247                                         modelData.sort(function(x, y) {
248                                                 return x.appName > y.appName ? 1 : -1;
249                                         });
250
251                                         modelData.unshift(homeScreenApp);
252                                         while (modelData.length < 8) {
253                                                 modelData.push({});
254                                         }
255
256                                         modelData.push({
257                                                 id: "http://com.intel.tizen/settings",
258                                                 appName: "settings",
259                                                 css: "appIconNo7",
260                                                 installed: true,
261                                                 lastIcon: true
262                                         });
263                                         var equals = modelData.length === topBarAplicationsModel.length;
264
265                                         if (equals) {
266                                                 for (j = 0; j < modelData.length; j++) {
267                                                         equals = modelData[j].id === topBarAplicationsModel[j].id ? equals : false;
268                                                         equals = modelData[j].appName === topBarAplicationsModel[j].appName ? equals : false;
269                                                         equals = modelData[j].css === topBarAplicationsModel[j].css ? equals : false;
270                                                         equals = modelData[j].iconPath === topBarAplicationsModel[j].iconPath ? equals : false;
271                                                 }
272                                         }
273
274                                         if (!equals) {
275                                                 topBarAplicationsModel = modelData;
276                                                 if (appListLenght !== list.length) {
277                                                         loadScript('./css/car/components/jsViews/jsrender.js', function(path, status) {
278                                                                 if (status === "ok") {
279                                                                         loadScript('./css/car/components/jsViews/template.js', function(path, status) {
280                                                                                 if (status === "ok") {
281                                                                                         TopBarIcons.renderApps();
282
283                                                                                         // jsRender adds additional properties so we need to store it once again for comparsion in next round
284                                                                                         topBarAplicationsModel = modelData;
285                                                                                 }
286                                                                         });
287                                                                 }
288                                                         });
289                                                 }
290                                         }
291                         } catch (exc) {
292                                 console.error(exc.message);
293                         }
294                 },
295
296                 /**
297                  * Renders the array of predefined applications into the panel utilizing template mechanism.
298                  *
299                  * @method renderApps
300                  * @param caption {String} Caption text.
301                  */
302                 renderApps: function() {
303                         $(".topBarIcons").empty();
304
305                         template.compile(topBarAplicationsModel, "./css/car/components/topBarIcons/templates/topBarIconsDelegate.html", ".topBarIcons", function() {
306                                 var j = 0;
307                                 for (j = 0; j < topBarAplicationsModel.length; ++j) {
308                                         if (topBarAplicationsModel[j].running) {
309                                                 changeCssBgImageColor("." + topBarAplicationsModel[j].css, ThemeKeyColorSelected);
310                                         } else {
311                                                 changeCssBgImageColor("." + topBarAplicationsModel[j].css, ThemeKeyColor);
312                                         }
313                                 }
314                         });
315                 },
316                 initLaunchingAppsByVoiceRecognition: function() {
317                         if (typeof(Speech) !== 'undefined') {
318                                 Speech.addVoiceRecognitionListener({
319                                         onapplicationlaunch: function(appName) {
320                                                 // check if app is in foreground
321                                                 //if (!document. webkitHidden) {
322                                                 launchApplication(getAppByName(appName).id);
323                                                 //}
324                                         }
325                                 });
326                         } else {
327                                 console.warn("Speech API is not available.");
328                         }
329                 }
330         };
331
332         /**
333          * jQuery extension method for class {{#crossLink "TopBarIcons"}}{{/crossLink}} plugin.
334          * @param method {Object|jQuery selector} Identificator (name) of method or jQuery selector.
335          * @method topBarIconsPlugin
336          * @for jQuery
337          * @return Result of called method.
338          */
339         $.fn.topBarIconsPlugin = function(method) {
340                 // Method calling logic
341                 if (TopBarIcons[method]) {
342                         return TopBarIcons[method].apply(this, Array.prototype.slice.call(arguments, 1));
343                 } else if (typeof method === 'object' || !method) {
344                         return TopBarIcons.init.apply(this, arguments);
345                 } else {
346                         $.error('Method ' + method + ' does not exist on jQuery.boxCaptionPlugin');
347                 }
348         };
349 }(jQuery));