639300eb65451495ef788dbada87e7bfde8effce
[controller.git] / opendaylight / web / root / src / main / resources / js / fuelux / checkbox.js
1 /*
2  * Fuel UX Checkbox
3  * https://github.com/ExactTarget/fuelux
4  *
5  * Copyright (c) 2012 ExactTarget
6  * Licensed under the MIT license.
7  */
8
9 define(['require','jquery'],function (require) {
10
11         var $ = require('jquery');
12
13
14         // CHECKBOX CONSTRUCTOR AND PROTOTYPE
15
16         var Checkbox = function (element, options) {
17
18                 this.$element = $(element);
19                 this.options = $.extend({}, $.fn.checkbox.defaults, options);
20
21                 // cache elements
22                 this.$label = this.$element.parent();
23                 this.$icon = this.$label.find('i');
24                 this.$chk = this.$label.find('input[type=checkbox]');
25
26                 // set default state
27                 this.setState(this.$chk);
28
29                 // handle events
30                 this.$chk.on('change', $.proxy(this.itemchecked, this));
31         };
32
33         Checkbox.prototype = {
34
35                 constructor: Checkbox,
36
37                 setState: function ($chk) {
38                         var checked = $chk.is(':checked');
39                         var disabled = $chk.is(':disabled');
40
41                         // reset classes
42                         this.$icon.removeClass('checked').removeClass('disabled');
43
44                         // set state of checkbox
45                         if (checked === true) {
46                                 this.$icon.addClass('checked');
47                         }
48                         if (disabled === true) {
49                                 this.$icon.addClass('disabled');
50                         }
51                 },
52
53                 enable: function () {
54                         this.$chk.attr('disabled', false);
55                         this.$icon.removeClass('disabled');
56                 },
57
58                 disable: function () {
59                         this.$chk.attr('disabled', true);
60                         this.$icon.addClass('disabled');
61                 },
62
63                 toggle: function () {
64                         this.$chk.click();
65                 },
66
67                 itemchecked: function (e) {
68                         var chk = $(e.target);
69                         this.setState(chk);
70                 }
71         };
72
73
74         // CHECKBOX PLUGIN DEFINITION
75
76         $.fn.checkbox = function (option, value) {
77                 var methodReturn;
78
79                 var $set = this.each(function () {
80                         var $this = $(this);
81                         var data = $this.data('checkbox');
82                         var options = typeof option === 'object' && option;
83
84                         if (!data) $this.data('checkbox', (data = new Checkbox(this, options)));
85                         if (typeof option === 'string') methodReturn = data[option](value);
86                 });
87
88                 return (methodReturn === undefined) ? $set : methodReturn;
89         };
90
91         $.fn.checkbox.defaults = {};
92
93         $.fn.checkbox.Constructor = Checkbox;
94
95
96         // CHECKBOX DATA-API
97
98         $(function () {
99                 $(window).on('load', function () {
100                         //$('i.checkbox').each(function () {
101                         $('.checkbox-custom > input[type=checkbox]').each(function () {
102                                 var $this = $(this);
103                                 if ($this.data('checkbox')) return;
104                                 $this.checkbox($this.data());
105                         });
106                 });
107         });
108
109 });