3 * https://github.com/ExactTarget/fuelux
\r
5 * Copyright (c) 2012 ExactTarget
\r
6 * Licensed under the MIT license.
\r
9 define(['require','jquery'],function(require) {
\r
11 var $ = require('jquery');
\r
14 // SPINNER CONSTRUCTOR AND PROTOTYPE
\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
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
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
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
42 this.switches.speed = 500;
\r
45 this.lastValue = null;
\r
49 if (this.options.disabled) {
\r
54 Spinner.prototype = {
\r
55 constructor: Spinner,
\r
57 render: function () {
\r
58 this.$input.val(this.options.value);
\r
59 this.$input.attr('maxlength',(this.options.max + '').split('').length);
\r
62 change: function () {
\r
63 var newVal = this.$input.val();
\r
66 this.options.value = newVal/1;
\r
68 newVal = newVal.replace(/[^0-9]/g,'');
\r
69 this.$input.val(newVal);
\r
70 this.options.value = newVal/1;
\r
73 this.triggerChangedEvent();
\r
76 stopSpin: function () {
\r
77 clearTimeout(this.switches.timeout);
\r
78 this.switches.count = 1;
\r
79 this.triggerChangedEvent();
\r
82 triggerChangedEvent: function () {
\r
83 var currentValue = this.value();
\r
84 if (currentValue === this.lastValue) return;
\r
86 this.lastValue = currentValue;
\r
88 // Primary changed event
\r
89 this.$element.trigger('changed', currentValue);
\r
91 // Undocumented, kept for backward compatibility
\r
92 this.$element.trigger('change');
\r
95 startSpin: function (type) {
\r
97 if (!this.options.disabled) {
\r
98 var divisor = this.switches.count;
\r
100 if (divisor === 1) {
\r
103 } else if (divisor < 3){
\r
105 } else if (divisor < 8){
\r
111 this.switches.timeout = setTimeout($.proxy(function() {this.iterator(type);} ,this),this.switches.speed/divisor);
\r
112 this.switches.count++;
\r
116 iterator: function (type) {
\r
118 this.startSpin(type);
\r
121 step: function (dir) {
\r
122 var curValue = this.options.value;
\r
123 var limValue = dir ? this.options.max : this.options.min;
\r
125 if ((dir ? curValue < limValue : curValue > limValue)) {
\r
126 var newVal = curValue + (dir ? 1 : -1) * this.options.step;
\r
128 if (dir ? newVal > limValue : newVal < limValue) {
\r
129 this.value(limValue);
\r
131 this.value(newVal);
\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
143 return this.options.value;
\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
153 enable: function () {
\r
154 this.options.disabled = false;
\r
155 this.$input.removeAttr("disabled");
\r
156 this.$element.find('button').removeClass('disabled');
\r
161 // SPINNER PLUGIN DEFINITION
\r
163 $.fn.spinner = function (option,value) {
\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
171 if (!data) $this.data('spinner', (data = new Spinner(this, options)));
\r
172 if (typeof option === 'string') methodReturn = data[option](value);
\r
175 return (methodReturn === undefined) ? $set : methodReturn;
\r
178 $.fn.spinner.defaults = {
\r
188 $.fn.spinner.Constructor = Spinner;
\r
191 // SPINNER DATA-API
\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