Removes copied dependencies, bootstrap unit tests, custom scrollbars.
[profile/ivi/cowhide.git] / src / javascripts / cowhide-widget.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     var ChWidget = function(element, options) {
14         this.$element = $(element);
15         this.options = $.extend({}, $.fn.ch_widget.defaults);
16         this.drivingMode = false;
17
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);
22             } else {
23                 $page.ch_page('register', this);
24             }
25         }
26     };
27
28     ChWidget.prototype = $.extend({}, {
29         register: function() {
30             this.guid = $.cowhide.GUID();
31             $.cowhide.register(this);
32         },
33
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);
38         },
39
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);
46                 }
47             }
48         },
49
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);
56                 }
57             }
58         },
59
60         setDrivingMode: function(drivingMode) {
61             if (this.$element.data('ignore-driving-mode') === undefined &&
62                 this.options.disableWhenDriving)
63             {
64                 var wasDisabled = this.$element.attr('disabled') == 'disabled';
65                 var d = 'disabled';
66
67                 if (!drivingMode && !this.drivingMode && wasDisabled)
68                     // If
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
72                     //   other reasons.
73                     return;
74
75                 if (drivingMode) {
76                     this.$element.attr(d, d);
77                     this.$element.disabled = true;
78                     this.$element.addClass(d);
79
80                     if (this.onDrivingModeEnter)
81                         this.onDrivingModeEnter();
82
83                     this.drivingMode = true;
84                 } else if (this.drivingMode) {
85                     this.$element.removeAttr(d);
86                     this.$element.disabled = false;
87                     this.$element.removeClass(d);
88
89                     if (this.onDrivingModeExit)
90                         this.onDrivingModeExit();
91
92                     this.drivingMode = false;
93                 }
94             }
95         },
96
97         onDrivingModeEnter: undefined,
98         onDrivingModeExit: undefined
99      });
100
101     $.fn.ch_widget = function() {}
102     $.fn.ch_widget.defaults = {
103         minWidth: 0,
104         minFontSize: 0,
105         maxFontSize: 0,
106         disableWhenDriving: false
107     };
108     $.fn.ch_widget.Constructor = ChWidget;
109 })(window.jQuery);