Make sure invokeOperation is set once
[controller.git] / opendaylight / adsal / web / root / src / main / resources / js / fuelux / spinner.js
1 /*\r
2  * Fuel UX Spinner\r
3  * https://github.com/ExactTarget/fuelux\r
4  *\r
5  * Copyright (c) 2012 ExactTarget\r
6  * Licensed under the MIT license.\r
7  */\r
8 \r
9 define(['require','jquery'],function(require) {\r
10 \r
11         var $ = require('jquery');\r
12 \r
13 \r
14         // SPINNER CONSTRUCTOR AND PROTOTYPE\r
15 \r
16         var Spinner = function (element, options) {\r
17                 this.$element = $(element);\r
18                 this.options = $.extend({}, $.fn.spinner.defaults, options);\r
19                 this.$input = this.$element.find('.spinner-input');\r
20                 this.$element.on('keyup', this.$input, $.proxy(this.change, this));\r
21 \r
22                 if (this.options.hold) {\r
23                         this.$element.on('mousedown', '.spinner-up', $.proxy(function() { this.startSpin(true); } , this));\r
24                         this.$element.on('mouseup', '.spinner-up, .spinner-down', $.proxy(this.stopSpin, this));\r
25                         this.$element.on('mouseout', '.spinner-up, .spinner-down', $.proxy(this.stopSpin, this));\r
26                         this.$element.on('mousedown', '.spinner-down', $.proxy(function() {this.startSpin(false);} , this));\r
27                 } else {\r
28                         this.$element.on('click', '.spinner-up', $.proxy(function() { this.step(true); } , this));\r
29                         this.$element.on('click', '.spinner-down', $.proxy(function() { this.step(false); }, this));\r
30                 }\r
31 \r
32                 this.switches = {\r
33                         count: 1,\r
34                         enabled: true\r
35                 };\r
36 \r
37                 if (this.options.speed === 'medium') {\r
38                         this.switches.speed = 300;\r
39                 } else if (this.options.speed === 'fast') {\r
40                         this.switches.speed = 100;\r
41                 } else {\r
42                         this.switches.speed = 500;\r
43                 }\r
44 \r
45                 this.lastValue = null;\r
46 \r
47                 this.render();\r
48 \r
49                 if (this.options.disabled) {\r
50                         this.disable();\r
51                 }\r
52         };\r
53 \r
54         Spinner.prototype = {\r
55                 constructor: Spinner,\r
56 \r
57                 render: function () {\r
58                         this.$input.val(this.options.value);\r
59                         this.$input.attr('maxlength',(this.options.max + '').split('').length);\r
60                 },\r
61 \r
62                 change: function () {\r
63                         var newVal = this.$input.val();\r
64 \r
65                         if(newVal/1){\r
66                                 this.options.value = newVal/1;\r
67                         }else{\r
68                                 newVal = newVal.replace(/[^0-9]/g,'');\r
69                                 this.$input.val(newVal);\r
70                                 this.options.value = newVal/1;\r
71                         }\r
72 \r
73                         this.triggerChangedEvent();\r
74                 },\r
75 \r
76                 stopSpin: function () {\r
77                         clearTimeout(this.switches.timeout);\r
78                         this.switches.count = 1;\r
79                         this.triggerChangedEvent();\r
80                 },\r
81 \r
82                 triggerChangedEvent: function () {\r
83                         var currentValue = this.value();\r
84                         if (currentValue === this.lastValue) return;\r
85 \r
86                         this.lastValue = currentValue;\r
87 \r
88                         // Primary changed event\r
89                         this.$element.trigger('changed', currentValue);\r
90 \r
91                         // Undocumented, kept for backward compatibility\r
92                         this.$element.trigger('change');\r
93                 },\r
94 \r
95                 startSpin: function (type) {\r
96 \r
97                         if (!this.options.disabled) {\r
98                                 var divisor = this.switches.count;\r
99 \r
100                                 if (divisor === 1) {\r
101                                         this.step(type);\r
102                                         divisor = 1;\r
103                                 } else if (divisor < 3){\r
104                                         divisor = 1.5;\r
105                                 } else if (divisor < 8){\r
106                                         divisor = 2.5;\r
107                                 } else {\r
108                                         divisor = 4;\r
109                                 }\r
110 \r
111                                 this.switches.timeout = setTimeout($.proxy(function() {this.iterator(type);} ,this),this.switches.speed/divisor);\r
112                                 this.switches.count++;\r
113                         }\r
114                 },\r
115 \r
116                 iterator: function (type) {\r
117                         this.step(type);\r
118                         this.startSpin(type);\r
119                 },\r
120 \r
121                 step: function (dir) {\r
122                         var curValue = this.options.value;\r
123                         var limValue = dir ? this.options.max : this.options.min;\r
124 \r
125                         if ((dir ? curValue < limValue : curValue > limValue)) {\r
126                                 var newVal = curValue + (dir ? 1 : -1) * this.options.step;\r
127 \r
128                                 if (dir ? newVal > limValue : newVal < limValue) {\r
129                                         this.value(limValue);\r
130                                 } else {\r
131                                         this.value(newVal);\r
132                                 }\r
133                         }\r
134                 },\r
135 \r
136                 value: function (value) {\r
137                         if (!isNaN(parseFloat(value)) && isFinite(value)) {\r
138                                 value = parseFloat(value);\r
139                                 this.options.value = value;\r
140                                 this.$input.val(value);\r
141                                 return this;\r
142                         } else {\r
143                                 return this.options.value;\r
144                         }\r
145                 },\r
146 \r
147                 disable: function () {\r
148                         this.options.disabled = true;\r
149                         this.$input.attr('disabled','');\r
150                         this.$element.find('button').addClass('disabled');\r
151                 },\r
152 \r
153                 enable: function () {\r
154                         this.options.disabled = false;\r
155                         this.$input.removeAttr("disabled");\r
156                         this.$element.find('button').removeClass('disabled');\r
157                 }\r
158         };\r
159 \r
160 \r
161         // SPINNER PLUGIN DEFINITION\r
162 \r
163         $.fn.spinner = function (option,value) {\r
164                 var methodReturn;\r
165 \r
166                 var $set = this.each(function () {\r
167                         var $this = $(this);\r
168                         var data = $this.data('spinner');\r
169                         var options = typeof option === 'object' && option;\r
170 \r
171                         if (!data) $this.data('spinner', (data = new Spinner(this, options)));\r
172                         if (typeof option === 'string') methodReturn = data[option](value);\r
173                 });\r
174 \r
175                 return (methodReturn === undefined) ? $set : methodReturn;\r
176         };\r
177 \r
178         $.fn.spinner.defaults = {\r
179                 value: 1,\r
180                 min: 1,\r
181                 max: 999,\r
182                 step: 1,\r
183                 hold: true,\r
184                 speed: 'medium',\r
185                 disabled: false\r
186         };\r
187 \r
188         $.fn.spinner.Constructor = Spinner;\r
189 \r
190 \r
191         // SPINNER DATA-API\r
192 \r
193         $(function () {\r
194                 $('body').on('mousedown.spinner.data-api', '.spinner', function (e) {\r
195                         var $this = $(this);\r
196                         if ($this.data('spinner')) return;\r
197                         $this.spinner($this.data());\r
198                 });\r
199         });\r
200 \r
201 });\r