Removes copied dependencies, bootstrap unit tests, custom scrollbars.
[profile/ivi/cowhide.git] / src / javascripts / cowhide-core.js
1 /*
2  * Copyright (c) 2012, Intel Corporation.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 (function($, _, undefined) {
11         'use strict';
12
13     $.cowhide = $.cowhide || {}
14     $.extend($.cowhide, {
15         version: '0.0.1',
16         options: {
17           monitorFrameworkRestrictions: false,
18           connectToAMB: false
19         },
20         themeEngineOptions: {
21             path: 'css',
22             initial: 'default',
23             minified: false
24         },
25
26         // List of registered widgets
27         registeredWidgets: [],
28
29         drivingMode: false,
30         nightMode: false,
31         currentTheme: 'default',
32
33         vehicle: null,
34
35         GUID: function() {
36             var S4 = function () {
37                 return Math.floor(
38                     Math.random() * 0x10000 /* 65536 */
39                     ).toString(16);
40             };
41
42             return (
43                 S4() + S4() + "-" +
44                 S4() + "-" +
45                 S4() + "-" +
46                 S4() + "-" +
47                 S4() + S4() + S4()
48                 );
49         },
50
51         // Registers a widget
52         register: function(widget) {
53             var self = this;
54             var guids = _.map(self.registeredWidgets, function(w) {
55                 return w.guid;
56             });
57
58             if (_.indexOf(guids, widget.guid) == -1) {
59                 self.registeredWidgets.push(widget);
60             }
61         },
62
63         // TODO: use `backdrop` from Bootstrap's modal
64         backdrop: function (callback) {
65             var $backdrop = $('<div class="modal-backdrop theme-change-backdrop fade" />');
66
67             $backdrop.appendTo(document.body);
68             $backdrop[0].offsetWidth; // force reflow
69             $backdrop.addClass('in');
70
71             return $backdrop;
72         },
73
74         initThemeEngine: function(options) {
75             $.extend(this.themeEngineOptions, options);
76             this.currentTheme = this.themeEngineOptions.initial;
77
78             var $link = $('link#cowhide-theme');
79             if ($link.length === 0) {
80                 this.fatal("#40: could not find <link> with id 'cowhide-theme'.");
81             }
82         },
83
84         setTheme: function(name, nightMode) {
85             if (name === this.currentTheme && nightMode == this.nightMode) {
86                 return;
87             }
88
89             var $link = $('link#cowhide-theme');
90             var theme =
91                 this.themeEngineOptions.path +
92                 '/cowhide-' +
93                 name || 'default';
94
95             if (nightMode === true || (nightMode === undefined && this.nightMode === true)) {
96                 theme += '-night';
97             }
98
99             if (this.themeEngineOptions.minified) {
100                 theme += '.min';
101             }
102
103             theme += '.css';
104
105             var $backdrop = this.backdrop();
106             setTimeout(function() {
107                 $link.attr('href', theme);
108                 $backdrop.remove();
109             }, 200);
110
111             this.currentTheme = name;
112             if (nightMode !== undefined) {
113                 this.nightMode = nightMode;
114             }
115         },
116
117         setNightMode: function(value) {
118           if (this.nightMode == value)
119             return;
120
121           this.setTheme(this.currentTheme, !this.nightMode);
122         },
123
124         toggleNightMode: function() {
125             this.setNightMode(!this.nightMode);
126         },
127
128         toggleDrivingMode: function() {
129             this.setDrivingMode(!this.drivingMode);
130         },
131
132         setDrivingMode: function(value) {
133             var self = this;
134
135             if (self.drivingMode == value)
136                 return;
137
138             self.drivingMode = value;
139             _.each(this.registeredWidgets, function(w) {
140                 if (w.setDrivingMode)
141                     w.setDrivingMode(self.drivingMode);
142             });
143         },
144
145
146         listenToVehicle: function() {
147             var self = this;
148
149             self.vehicle = new window.Vehicle(
150                 function() {
151                     $(document).on("VehicleSpeed", function(data) {
152                         self.setDrivingMode(data.originalEvent.value > 0);
153                     });
154                 },
155                 function() {
156                     self.fatal("There was a problem connecting to AMB's web socket.");
157                 }
158             );
159         },
160
161         verifyFrameworkRestrictions: function() {
162             _.each(this.registeredWidgets, function(w) {
163                 w.verifyMinFontSize();
164                 w.verifyMaxFontSize();
165                 w.verifyMinWidth();
166             });
167         },
168
169         fatal: function(msg, $element) {
170             var output = "";
171
172             output += "[Cowhide] Fatal error";
173             if ($element !== undefined) {
174                 output += " (offending widget: ";
175                 var id = $element.attr('id');
176                 var classes = $element.attr('class');
177
178                 if (id)
179                     output += "#(" + id + ")";
180                 if (classes)
181                     output += ".(" + classes + ")";
182
183                 output += ")";
184             }
185
186             output += ": " + msg;
187             throw new Error(output);
188         }
189     });
190
191     $(function() {
192         if ($.cowhide.options.connectToAMB) {
193           $.cowhide.listenToVehicle();
194         }
195
196         if ($.cowhide.options.monitorFrameworkRestrictions) {
197           setInterval(function() {
198               $.cowhide.verifyFrameworkRestrictions();
199           }, 1000);
200         }
201     });
202 })(window.jQuery, window._);