Make sure invokeOperation is set once
[controller.git] / opendaylight / adsal / web / root / src / main / resources / js / fuelux / radio.js
1 /*
2  * Fuel UX Radio
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         // RADIO CONSTRUCTOR AND PROTOTYPE
15
16         var Radio = function (element, options) {
17                 this.$element = $(element);
18                 this.options = $.extend({}, $.fn.radio.defaults, options);
19
20                 // cache elements
21                 this.$label = this.$element.parent();
22                 this.$icon = this.$label.find('i');
23                 this.$radio = this.$label.find('input[type=radio]');
24                 this.groupName = this.$radio.attr('name');
25
26                 // set default state
27                 this.setState(this.$radio);
28
29                 // handle events
30                 this.$radio.on('change', $.proxy(this.itemchecked, this));
31         };
32
33         Radio.prototype = {
34
35                 constructor: Radio,
36
37                 setState: function ($radio, resetGroupState) {
38                         var checked = $radio.is(':checked');
39                         var disabled = $radio.is(':disabled');
40
41                         // set state of radio
42                         if (checked === true) {
43                                 this.$icon.addClass('checked');
44                         }
45                         if (disabled === true) {
46                                 this.$icon.addClass('disabled');
47                         }
48                 },
49
50                 resetGroup: function () {
51                         // reset all radio buttons in group
52                         $('input[name=' + this.groupName + ']').next().removeClass('checked');
53                 },
54
55                 enable: function () {
56                         this.$radio.attr('disabled', false);
57                         this.$icon.removeClass('disabled');
58                 },
59
60                 disable: function () {
61                         this.$radio.attr('disabled', true);
62                         this.$icon.addClass('disabled');
63                 },
64
65                 itemchecked: function (e) {
66                         var radio = $(e.target);
67
68                         this.resetGroup();
69                         this.setState(radio);
70                 }
71         };
72
73
74         // RADIO PLUGIN DEFINITION
75
76         $.fn.radio = function (option, value) {
77                 var methodReturn;
78
79                 var $set = this.each(function () {
80                         var $this = $(this);
81                         var data = $this.data('radio');
82                         var options = typeof option === 'object' && option;
83
84                         if (!data) $this.data('radio', (data = new Radio(this, options)));
85                         if (typeof option === 'string') methodReturn = data[option](value);
86                 });
87
88                 return (methodReturn === undefined) ? $set : methodReturn;
89         };
90
91         $.fn.radio.defaults = {};
92
93         $.fn.radio.Constructor = Radio;
94
95
96         // RADIO DATA-API
97
98         $(function () {
99                 $(window).on('load', function () {
100                         //$('i.radio').each(function () {
101                         $('.radio-custom > input[type=radio]').each(function () {
102                                 var $this = $(this);
103                                 if ($this.data('radio')) return;
104                                 $this.radio($this.data());
105                         });
106                 });
107         });
108
109 });