df9836c26b1f0b527d08e7e1071faade4b7c658a
[profile/ivi/cowhide.git] / src / javascripts / cowhide-header.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 ChHeader = function(element, options) {
14         $.fn.ch_widget.Constructor(element, options);
15         this.$element = $(element);
16         this.options = $.extend({}, options);
17     };
18
19     ChHeader.prototype = $.extend(
20         {},
21         $.fn.ch_widget.Constructor.prototype,
22         {
23             constructor: ChHeader,
24
25             show: function() {
26                 var $this = this.$element,
27                     $h = $('<h1/>').text(this.$element.text());
28
29                $this.html($h);
30
31                if (this.options.show_back_button) {
32                     var $back = $('<button/>').addClass('btn');
33                     var $icon = $('<i/>').addClass('icon-backward');
34
35                     $back.html($icon);
36
37                     $this.append($back);
38
39                     $back.click(function(e) {
40                         e.preventDefault();
41                         $this.trigger($.Event('back'));
42                     });
43                 }
44             }
45         }
46     );
47
48
49     /* CHHEADER PLUGIN DEFINITION
50      * ========================== */
51
52     var old = $.fn.ch_header;
53
54     $.fn.ch_header = function(option) {
55         return this.each(function() {
56             var $this = $(this),
57                 data = $this.data('ch_header'),
58                 options = typeof option == 'object' && option;
59
60             if ($this.data('show-back-button')) {
61                 options = $.extend(options, {show_back_button: true});
62             }
63
64             if (!data) {
65                 $this.data('ch_header', (data = new ChHeader(this, options)));
66                 data.register();
67             }
68
69             if (typeof option == 'string')
70                 data[option]();
71         });
72     };
73
74     $.fn.ch_button.Constructor = ChHeader;
75
76
77     /* CHHEADER NO CONFLICT
78      * ==================== */
79
80     $.fn.ch_header.noConflict = function() {
81         $.fn.ch_header = old;
82         return this;
83     };
84
85
86     /* CHHEADER DATA-API
87      * ================= */
88
89     $(function() {
90         $('.ch-header').ch_header('show');
91     })
92 })(window.jQuery);