2 * Copyright (c) 2012, Intel Corporation.
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
10 (function($, _, undefined) {
13 $.cowhide = $.cowhide || {}
17 monitorFrameworkRestrictions: false,
26 // List of registered widgets
27 registeredWidgets: [],
31 currentTheme: 'default',
36 var S4 = function () {
38 Math.random() * 0x10000 /* 65536 */
52 register: function(widget) {
54 var guids = _.map(self.registeredWidgets, function(w) {
58 if (_.indexOf(guids, widget.guid) == -1) {
59 self.registeredWidgets.push(widget);
63 // TODO: use `backdrop` from Bootstrap's modal
64 backdrop: function (callback) {
65 var $backdrop = $('<div class="modal-backdrop theme-change-backdrop fade" />');
67 $backdrop.appendTo(document.body);
68 $backdrop[0].offsetWidth; // force reflow
69 $backdrop.addClass('in');
74 initThemeEngine: function(options) {
75 $.extend(this.themeEngineOptions, options);
76 this.currentTheme = this.themeEngineOptions.initial;
78 var $link = $('link#cowhide-theme');
79 if ($link.length === 0) {
80 this.fatal("#40: could not find <link> with id 'cowhide-theme'.");
84 setTheme: function(name, nightMode) {
85 if (name === this.currentTheme && nightMode == this.nightMode) {
89 var $link = $('link#cowhide-theme');
91 this.themeEngineOptions.path +
95 if (nightMode === true || (nightMode === undefined && this.nightMode === true)) {
99 if (this.themeEngineOptions.minified) {
105 var $backdrop = this.backdrop();
106 setTimeout(function() {
107 $link.attr('href', theme);
111 this.currentTheme = name;
112 if (nightMode !== undefined) {
113 this.nightMode = nightMode;
117 setNightMode: function(value) {
118 if (this.nightMode == value)
121 this.setTheme(this.currentTheme, !this.nightMode);
124 toggleNightMode: function() {
125 this.setNightMode(!this.nightMode);
128 toggleDrivingMode: function() {
129 this.setDrivingMode(!this.drivingMode);
132 setDrivingMode: function(value) {
135 if (self.drivingMode == value)
138 self.drivingMode = value;
139 _.each(this.registeredWidgets, function(w) {
140 if (w.setDrivingMode)
141 w.setDrivingMode(self.drivingMode);
146 listenToVehicle: function() {
149 self.vehicle = new window.Vehicle(
151 $(document).on("VehicleSpeed", function(data) {
152 self.setDrivingMode(data.originalEvent.value > 0);
156 self.fatal("There was a problem connecting to AMB's web socket.");
161 verifyFrameworkRestrictions: function() {
162 _.each(this.registeredWidgets, function(w) {
163 w.verifyMinFontSize();
164 w.verifyMaxFontSize();
169 fatal: function(msg, $element) {
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');
179 output += "#(" + id + ")";
181 output += ".(" + classes + ")";
186 output += ": " + msg;
187 throw new Error(output);
192 if ($.cowhide.options.connectToAMB) {
193 $.cowhide.listenToVehicle();
196 if ($.cowhide.options.monitorFrameworkRestrictions) {
197 setInterval(function() {
198 $.cowhide.verifyFrameworkRestrictions();
202 })(window.jQuery, window._);