Add Modello Common libraries to web-ui-fw; version up
[profile/ivi/sdk/web-ide-resources.git] / web-ui-fw / 0.0.2 / 0.0.2_Common / original / css / car / components / settings / js / settings.js
1 /* global loadScript, Themes, Wifi, Bluetooth, ko, loadTemplate, $ */
2
3 /**
4  * Settings class provides list view of available Settings options.
5  *
6  * This class requires following components:
7  *
8  * * {{#crossLink "BoxCaption"}}{{/crossLink}} component
9  * * {{#crossLink "Tabs"}}{{/crossLink}} component
10  * * {{#crossLink "Themes"}}{{/crossLink}} component
11  * * {{#crossLink "Wifi"}}{{/crossLink}} component
12  * * {{#crossLink "Bluetooth"}}{{/crossLink}} component
13  *
14  * @class Settings
15  * @module Settings
16  * @constructor
17  */
18 var Settings = (function() {
19         "use strict";
20         /**
21          * @class Settings
22          * @constructor
23          */
24         function Settings() {
25                 console.info("Starting up service Settings");
26
27                 var self = this;
28
29                 /**
30                  * Shows a given Settings option.
31                  *
32                  * @method openSetting
33                  * @param setting {Object} Object representing Setting option to be showed.
34                  */
35                 this.openSetting = function(setting) {
36                         self.selectedSetting = null;
37                         if (!!setting && !self.locked) {
38                                 self.locked = true;
39                                 self.selectedSetting = setting;
40                                 switch (setting.id) {
41                                 case "theme":
42                                         loadScript(self.SETTINGS_JS_PATH + "themes.js", function(path, status) {
43                                                 if (status === "ok") {
44                                                         if (!self.Theme) {
45                                                                 self.Theme = new Themes();
46                                                         }
47                                                         self.Theme.show();
48                                                 }
49                                                 self.locked = false;
50                                         });
51                                         break;
52                                 case "wifinetworks":
53                                         loadScript(self.SETTINGS_JS_PATH + "wifi.js", function(path, status) {
54                                                 if (status === "ok") {
55                                                         if (!self.Wifi) {
56                                                                 self.Wifi = new Wifi();
57                                                                 self.Wifi.init(function(err) {
58                                                                         if (!!err) {
59                                                                                 alert(err);
60                                                                                 self.Wifi = null;
61                                                                         } else {
62                                                                                 self.Wifi.showNetworks();
63                                                                         }
64                                                                         self.locked = false;
65                                                                 });
66                                                         } else {
67                                                                 self.Wifi.showNetworks();
68                                                                 self.locked = false;
69                                                         }
70                                                 } else {
71                                                         self.locked = false;
72                                                 }
73                                         });
74                                         break;
75                                 case "wifitethering":
76                                         loadScript(self.SETTINGS_JS_PATH + "wifi.js", function(path, status) {
77                                                 if (status === "ok") {
78                                                         if (!self.Wifi) {
79                                                                 self.Wifi = new Wifi();
80                                                                 self.Wifi.init(function(err) {
81                                                                         if (!!err) {
82                                                                                 alert(err);
83                                                                                 self.Wifi = null;
84                                                                         } else {
85                                                                                 self.Wifi.showWifiTethering();
86                                                                         }
87                                                                         self.locked = false;
88                                                                 });
89                                                         } else {
90                                                                 self.Wifi.showWifiTethering();
91                                                                 self.locked = false;
92                                                         }
93                                                 } else {
94                                                         self.locked = false;
95                                                 }
96                                         });
97                                         break;
98                                 case "bluetooth":
99                                         loadScript(self.SETTINGS_JS_PATH + "bluetooth.js", function(path, status) {
100                                                 if (status === "ok") {
101                                                         if (!self.Bluetooth) {
102                                                                 self.Bluetooth = new Bluetooth();
103                                                         }
104                                                         self.Bluetooth.show();
105                                                 }
106                                                 self.locked = false;
107                                         });
108                                         break;
109                                 default:
110                                         self.locked = false;
111                                         break;
112                                 }
113                         }
114                 };
115         }
116
117         /**
118          * Defines base path to Settings resources.
119          *
120          * @property SETTINGS_BASEPATH
121          * @public
122          * @type String
123          * @default ./css/car/components/settings/
124          */
125         Settings.prototype.SETTINGS_BASEPATH = "./css/car/components/settings/";
126         /**
127          * Defines path to Settings templates.
128          *
129          * @property SETTINGS_TEMPLATES_PATH
130          * @public
131          * @type String
132          * @default ./css/car/components/settings/templates/
133          */
134         Settings.prototype.SETTINGS_TEMPLATES_PATH = "./css/car/components/settings/templates/";
135         /**
136          * Defines path to Settings javascript files.
137          *
138          * @property SETTINGS_JS_PATH
139          * @public
140          * @type String
141          * @default ./css/car/components/settings/js/
142          */
143         Settings.prototype.SETTINGS_JS_PATH = "./css/car/components/settings/js/";
144         /**
145          * Holds the git revision number.
146          *
147          * @property SETTINGS_REVISION
148          * @public
149          * @type String
150          */
151         Settings.prototype.SETTINGS_REVISION = "@revision@";
152         /**
153          * Instance of Theme class.
154          *
155          * @property Theme
156          * @public
157          * @type Themes
158          */
159         Settings.prototype.Theme = null;
160         /**
161          * Instance of Wifi class.
162          *
163          * @property Wifi
164          * @public
165          * @type Wifi
166          */
167         Settings.prototype.Wifi = null;
168         /**
169          * Instance of Bluetooth class.
170          *
171          * @property Bluetooth
172          * @public
173          * @type Bluetooth
174          */
175         Settings.prototype.Bluetooth = null;
176         /**
177          * jQuery representation of Settings Tabs component.
178          *
179          * @property domElement
180          * @public
181          * @type Any
182          */
183         Settings.prototype.domElement = null;
184         /**
185          * Prevents opening of clicked Settings option more times.
186          *
187          * @property locked
188          * @public
189          * @type Boolean
190          * @default false
191          */
192         Settings.prototype.locked = false;
193
194         /**
195          * Contains array of Settings options.
196          *
197          * @property settingsModel
198          * @type {Array}
199          */
200         Settings.prototype.settingsModel = ko.observableArray([ {
201                 id : "theme",
202                 name : "Theme"
203         }, {
204                 id : "wifinetworks",
205                 name : "Wifi networks"
206         }, {
207                 id : "wifitethering",
208                 name : "Wifi tethering"
209         }, {
210                 id : "bluetooth",
211                 name : "Bluetooth"
212         } ]);
213         /**
214          * Represents opened Settings option.
215          *
216          * @property selectedSetting
217          * @public
218          * @type Any
219          * @default null
220          */
221         Settings.prototype.selectedSetting = null;
222
223         /**
224          * Loads all the javascript and style files, initializes UI components that Settings list view depends on.
225          *
226          * @method init
227          */
228         Settings.prototype.init = function() {
229                 var self = this;
230                 loadScript('./css/car/components/boxCaption/boxCaption.js', function(path, status) {
231                         if (status === "ok") {
232                                 loadScript('./css/car/components/tabs/tabs.js', function(path, status) {
233                                         if (status === "ok") {
234                                                 $("head").append($("<link rel='stylesheet' href='./css/car/components/boxCaption/boxCaption.css' />"));
235                                                 $("head").append($("<link rel='stylesheet' href='./css/car/components/tabs/tabs.css' />"));
236                                                 $("head").append($("<link rel='stylesheet' href='" + self.SETTINGS_BASEPATH + "/css/settings.css' />"));
237
238                                                 if (!$("#settingsTabs").length) {
239                                                         var settings = '<div id="settingsTabs" class="tabs pageBgColorNormalTransparent"></div>';
240                                                         $(settings).appendTo("body");
241                                                         self.domElement = $("#settingsTabs");
242                                                 }
243
244                                                 self.domElement.bind('eventClick_menuItemBtn', function() {
245                                                         self.renderSettingsView();
246                                                 });
247
248                                                 self.domElement.tabs("setSectionTitle", "APPS");
249                                                 var version = typeof tizen === 'undefined' ? "" : tizen.application.getCurrentApplication().appInfo.version;
250                                                 self.domElement.tabs("setSectionHint", "v. " + version + " rev. " + self.SETTINGS_REVISION);
251                                                 self.domElement.tabs("init");
252
253                                                 var tabMenuModel = {
254                                                         Tabs : [ {
255                                                                 text : "SETTINGS",
256                                                                 selected : true
257                                                         } ]
258                                                 };
259
260                                                 self.domElement.tabs("tabMenuTemplateCompile", tabMenuModel, function() {
261                                                         self.renderSettingsView(function() {
262                                                                 self.show();
263                                                         });
264                                                 });
265                                         }
266                                 });
267                         }
268                 });
269         };
270
271         /**
272          * Fades in the Settings.
273          *
274          * @method show
275          */
276         Settings.prototype.show = function() {
277                 var self = this;
278                 self.domElement.tabs("showPage");
279         };
280
281         /**
282          * Shows list view of available Settings options.
283          *
284          * @method renderSettingsView
285          * @param successCallback {Function()} Callback function to be invoked when the rendering ends.
286          */
287         Settings.prototype.renderSettingsView = function(successCallback) {
288                 var self = this;
289                 var settingsContent = "settingsContent";
290                 var templateName = "template-settings";
291                 self.domElement.tabs('closeSubpanel');
292                 self.domElement.tabs("clearContent");
293                 self.domElement.tabs("changeContentClass", settingsContent);
294                 loadTemplate(self.SETTINGS_TEMPLATES_PATH, templateName, function() {
295                         if (!$("#settingsList").length) {
296                                 var settingsList = '<div id="settingsList" data-bind="template: { name: \'';
297                                 settingsList += templateName;
298                                 settingsList += '\', foreach: Settings.settingsModel }"></div>';
299                                 $(settingsList).appendTo($('.' + settingsContent));
300                                 ko.applyBindings(window.Settings);
301                         }
302                         if (!!successCallback) {
303                                 successCallback();
304                         }
305                 });
306                 if (self.domElement.find(".bluetoothPINCode").length) {
307                         self.domElement.find(".bluetoothPINCode").remove();
308                 }
309         };
310
311         window.__settings = undefined === window.__settings ? new Settings() : window.__settings;
312
313         return window.__settings;
314 })();