r2926 - spinner: moved a bunch of behavioural code into _draw to slim down _init.

r2926 - spinner: moved a bunch of behavioural code into _draw to slim down _init.


Author: pazu2k@gmail.com
Date: Thu Jul 16 00:30:19 2009
New Revision: 2926
Modified:
branches/dev/spinner/ui/ui.spinner.js
Log:
spinner: moved a bunch of behavioural code into _draw to slim down _init.
Modified: branches/dev/spinner/ui/ui.spinner.js
==============================================================================
--- branches/dev/spinner/ui/ui.spinner.js    (original)
+++ branches/dev/spinner/ui/ui.spinner.js    Thu Jul 16 00:30:19 2009
@@ -77,10 +77,66 @@
        self._setValue( isNaN(self._getValue()) ? options.value :
self._getValue() );
                
        // draw widget
-        self.widget = self._draw();
-                
+        self._draw();
+            
+        if (options.hide) {
+            self._hide();
+        }
+
+        if ($.fn.mousewheel && options.mouseWheel) {
+            self.element.mousewheel(self._mousewheel);
+        }
+        
+        // disable spinner if element was already disabled
+        if (self.element.attr("disabled")) {
+            self.disable();
+        }
+    },
+    
+    _draw: function() {
+        var self = this,
+            options = self.options,
+            dir = options.dir,
+            spinnerClass = options.spinnerClass,
+            spinnerBoxClass = 'ui-spinner-box',
+            widgetClasses = 'ui-spinner ui-state-default ui-widget
ui-widget-content ui-corner-all ui-spinner-' + dir + (spinnerClass ? ' '+
spinnerClass: '');
+        
+        var widget = self.element
+            .addClass(spinnerBoxClass)
+            .attr('autocomplete', 'off') // switch off autocomplete in opera
+            .wrap('<div role="spinbutton">')
+            .parent()
+                .addClass(widgetClasses)
+                .append('<a class="ui-spinner-button ui-spinner-up ui-state-default
ui-corner-t'+ dir.substr(-1,1) +'"><span
class="ui-spinner-button-inner"><span class="ui-icon
ui-icon-triangle-1-n">&#9650;</span></span></a>')
+                .append('<a class="ui-spinner-button ui-spinner-down ui-state-default
ui-corner-b'+ dir.substr(-1,1) +'"><span
class="ui-spinner-button-inner"><span class="ui-icon
ui-icon-triangle-1-s">&#9660;</span></span></a>');
+
+        // TODO: need a better way to exclude IE8 without resorting to
$.browser.version
+        // fix inline-block issues for IE. Since IE8 supports inline-block we
need to exclude it.
+        if (!$.support.opacity && widget.css('display') == 'inline-block' &&
$.browser.version < 8) {
+            widget.css('display', 'inline');
+        }
+
+        // force width if passed through options
+        if (options.width) {
+            self.element.width(options.width);
+        }
+
+        // Give the spinner casing a unique id only if one exists in original
input
+        // - this should aid targetted customisations if a page contains
multiple instances
+        self.element.attr('id', function(){
+            if (this.id) {
+                widget.attr('id', 'ui-spinner-'+ this.id);
+            }
+        });
+
+        // add aria properties
+        widget
+            .attr('aria-valuemin',options.min)
+            .attr('aria-valuemax',options.max)
+            .attr('aria-valuenow',options.value);
+
        // add behaviours
-        self.widget
+        widget
            .hover(function() {
                if (!options.readOnly && !self.disabled) {
                    $(this).addClass(hover);
@@ -97,7 +153,7 @@
                }
            });
        
-        self.buttons = self.widget.find('.ui-spinner-button')
+        self.buttons = widget.find('.ui-spinner-button')
            .bind('mousedown', function(event) {
                if (!self.counter) {
                    self.counter = 1;
@@ -107,7 +163,7 @@
                if (!self.disabled) {
                    $(this).addClass(active);
                    if (!options.readOnly) {
-                        self.widget.addClass(active);
+                        widget.addClass(active);
                    }                    
                }
            })
@@ -137,97 +193,41 @@
                    self._mouseup(event);
                }
            });
-            
-        if (options.hide) {
-            self._hide();
-        }
        self.element
-        .bind('keydown'+namespace, function(event) {
-            if (!self.counter) {
-                self.counter = 1;
-            }
-            return self._keydown.call(self, event);
-        })
-        .bind('keyup'+namespace, function(event) {
-            self.counter = 0;
-            self._trigger('change', event, { value: self._getValue() });
-        })
-        .bind('focus'+namespace, function() {
-            if (options.readOnly) {
-                self.element.blur();
-                return false;
-            }
-            self.widget.addClass(active);
-            self.focused = true;
-            if (!self.hovered && typeof options.hide != 'boolean'
&& !self.disabled) {
-                self._delay(self._show, 100, 'hide', options.hide);
-            }
-        })
-        .bind('blur'+namespace, function(event) {
-            if (!self.hovered) {
-                self.widget.removeClass(active);
-            }        
-            self.focused = false;
-            if (!self.hovered && typeof options.hide != 'boolean') {
-                self._delay(self._hide, 100, 'hide', options.hide);
-            }
-            self._cleanUp();
-        });
-
-        if ($.fn.mousewheel && options.mouseWheel) {
-            self.element.mousewheel(self._mousewheel);
-        }
-        
-        // disable spinner if element was already disabled
-        if (self.element.attr("disabled")) {
-            self.disable();
-        }
-    },
-    
-    _draw: function() {
-        var self = this,
-            options = self.options,
-            dir = options.dir,
-            spinnerClass = options.spinnerClass,
-            spinnerBoxClass = 'ui-spinner-box',
-            widgetClasses = 'ui-spinner ui-state-default ui-widget
ui-widget-content ui-corner-all ui-spinner-' + dir + (spinnerClass ? ' '+
spinnerClass: '');
-        
-        var widget = self.element
-            .addClass(spinnerBoxClass)
-            .attr('autocomplete', 'off') // switch off autocomplete in opera
-            .wrap('<div role="spinbutton">')
-            .parent()
-                .addClass(widgetClasses)
-                .append('<a class="ui-spinner-button ui-spinner-up ui-state-default
ui-corner-t'+ dir.substr(-1,1) +'"><span
class="ui-spinner-button-inner"><span class="ui-icon
ui-icon-triangle-1-n">&#9650;</span></span></a>')
-                .append('<a class="ui-spinner-button ui-spinner-down ui-state-default
ui-corner-b'+ dir.substr(-1,1) +'"><span
class="ui-spinner-button-inner"><span class="ui-icon
ui-icon-triangle-1-s">&#9660;</span></span></a>');
-
-        // TODO: need a better way to exclude IE8 without resorting to
$.browser.version
-        // fix inline-block issues for IE. Since IE8 supports inline-block we
need to exclude it.
-        if (!$.support.opacity && widget.css('display') == 'inline-block' &&
$.browser.version < 8) {
-            widget.css('display', 'inline');
-        }
-
-        // force width if passed through options
-        if (options.width) {
-            self.element.width(options.width);
-        }
-
-        // Give the spinner casing a unique id only if one exists in original
input
-        // - this should aid targetted customisations if a page contains
multiple instances
-        self.element.attr('id', function(){
-            if (this.id) {
-                widget.attr('id', 'ui-spinner-'+ this.id);
-            }
-        });
-
-        // add aria properties
-        widget
-            .attr('aria-valuemin',options.min)
-            .attr('aria-valuemax',options.max)
-            .attr('aria-valuenow',options.value);
+            .bind('keydown'+namespace, function(event) {
+                if (!self.counter) {
+                    self.counter = 1;
+                }
+                return self._keydown.call(self, event);
+            })
+            .bind('keyup'+namespace, function(event) {
+                self.counter = 0;
+                self._trigger('change', event, { value: self._getValue() });
+            })
+            .bind('focus'+namespace, function() {
+                if (options.readOnly) {
+                    self.element.blur();
+                    return false;
+                }
+                widget.addClass(active);
+                self.focused = true;
+                if (!self.hovered && typeof options.hide != 'boolean'
&& !self.disabled) {
+                    self._delay(self._show, 100, 'hide', options.hide);
+                }
+            })
+            .bind('blur'+namespace, function(event) {
+                if (!self.hovered) {
+                    widget.removeClass(active);
+                }        
+                self.focused = false;
+                if (!self.hovered && typeof options.hide != 'boolean') {
+                    self._delay(self._hide, 100, 'hide', options.hide);
+                }
+                self._cleanUp();
+            });
                    
-        return widget;
+        self.widget = widget;
    },
    _constrain: function() {
        if (this.options.min != null && this._getValue() < this.options.min) {
@@ -246,7 +246,7 @@
            return;
        }
-        this._trigger('start', event, { value: this._getValue() });
+        this._trigger('start', event, { value: this.value() });
        
        var value = this._getValue();