Fix Driving/NightMode; remove 'use strict'.
[profile/ivi/cowhide.git] / src / javascripts / cowhide-page.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     var ChPage = function(element, options) {
12         $.fn.ch_widget.Constructor(element, options);
13         this.$element = $(element);
14         this.options = $.extend(
15             options,
16             $.fn.ch_widget.defaults,
17             {
18                 maxWidgets: 0
19             });
20
21         var $parent_page = this.$element.parent().closest('div.page');
22         if ($parent_page.length !== 0) {
23             $.cowhide.fatal('#31: pages cannot be nested.');
24         }
25     };
26
27     ChPage.prototype = $.extend(
28         {},
29         $.fn.ch_widget.Constructor.prototype,
30         {
31             constructor: ChPage,
32
33             registeredWidgets: 0,
34             registerWidget: function(widget) {
35                 this.registeredWidgets++;
36                 if (this.options.maxWidgets > 0 &&
37                     this.registeredWidgets > this.options.maxWidgets)
38                 {
39                     $.cowhide.fatal("#32: a page cannot have more than " +
40                                     this.options.maxWidgets +
41                                     " widgets.");
42                 }
43             },
44
45             setMaxWidgets: function(value) {
46               this.options.maxWidgets = value;
47             }
48         }
49     );
50
51
52     /* CHPAGE PLUGIN DEFINITION
53      * ======================== */
54
55     $.fn.ch_page = function(option, value) {
56         return this.each(function() {
57             var $this = $(this),
58                 data = $this.data('ch_page'),
59                 options = typeof option == 'object' && option;
60
61             if (!data) {
62                 $this.data('ch_page', (data = new ChPage(this, options)));
63                 data.register();
64             }
65
66             if(option == 'register') {
67                 data.registerWidget(value);
68             }
69
70             if(option == 'setMaxWidgets') {
71               data.setMaxWidgets(value);
72             }
73         });
74     };
75
76     $.fn.ch_page.Constructor = ChPage;
77
78
79     /* CHPAGE DATA-API
80      * ================= */
81
82     $(function() {
83         $('div.page').ch_page();
84     })
85 })(window.jQuery);