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 var ChWidget = function(element, options) {
14 this.$element = $(element);
15 this.options = $.extend({}, $.fn.ch_widget.defaults);
16 this.drivingMode = false;
18 if(!(this.$element[0].tagName == 'DIV' && this.$element.hasClass('page'))) {
19 var $page = this.$element.parent().closest('div.page');
20 if ($page.length === 0) {
21 $.cowhide.fatal("#30: every widget must be within a div with class='page'.", this.$element);
23 $page.ch_page('register', this);
28 ChWidget.prototype = $.extend({}, {
29 register: function() {
30 this.guid = $.cowhide.GUID();
31 $.cowhide.register(this);
34 verifyMinWidth: function() {
35 if (this.$element.width() < this.options.minWidth && this.options.minWidth > 0)
36 $.cowhide.fatal("#10: this widget has a minimum allowed width of " +
37 this.options.minWidth + "px", this.$element);
40 verifyMinFontSize: function() {
41 if (this.options.minFontSize > 0) {
42 var current = this.$element.css('font-size');
43 if (parseFloat(current) < this.options.minFontSize) {
44 $.cowhide.fatal("#20: this widget has a minimum allowed font-size of " +
45 this.options.minFontSize + "px", this.$element);
50 verifyMaxFontSize: function() {
51 if (this.options.maxFontSize > 0) {
52 var current = this.$element.css('font-size');
53 if (parseFloat(current) > this.options.maxFontSize) {
54 $.cowhide.fatal("#21: this widget has a maximum allowed font-size of " +
55 this.options.maxFontSize + "px", this.$element);
60 setDrivingMode: function(drivingMode) {
61 if (this.$element.data('ignore-driving-mode') === undefined &&
62 this.options.disableWhenDriving)
64 var wasDisabled = this.$element.attr('disabled') == 'disabled';
67 if (!drivingMode && !this.drivingMode && wasDisabled)
69 // we're not entering driving mode, and
70 // we weren't already in driving mode, and
71 // the widget wasn't already disabled, perhaps for
76 this.$element.attr(d, d);
77 this.$element.disabled = true;
78 this.$element.addClass(d);
80 if (this.onDrivingModeEnter)
81 this.onDrivingModeEnter();
83 this.drivingMode = true;
84 } else if (this.drivingMode) {
85 this.$element.removeAttr(d);
86 this.$element.disabled = false;
87 this.$element.removeClass(d);
89 if (this.onDrivingModeExit)
90 this.onDrivingModeExit();
92 this.drivingMode = false;
97 onDrivingModeEnter: undefined,
98 onDrivingModeExit: undefined
101 $.fn.ch_widget = function() {}
102 $.fn.ch_widget.defaults = {
106 disableWhenDriving: false
108 $.fn.ch_widget.Constructor = ChWidget;