r2673 - Spinner: syntax cleanup.

r2673 - Spinner: syntax cleanup.


Author: scott.gonzalez
Date: Sun Jun 7 17:22:02 2009
New Revision: 2673
Modified:
branches/dev/spinner/ui/ui.spinner.js
Log:
Spinner: syntax cleanup.
Modified: branches/dev/spinner/ui/ui.spinner.js
==============================================================================
--- branches/dev/spinner/ui/ui.spinner.js    (original)
+++ branches/dev/spinner/ui/ui.spinner.js    Sun Jun 7 17:22:02 2009
@@ -23,10 +23,12 @@
            validChars;
                    
        function parse(val, ifNull) {
-            if (val == null)
+            if (val == null) {
                val = ifNull;
-            if (typeof val == 'string')
+            }
+            if (typeof val == 'string') {
                val = self._parseValue(val, self.options.radix,
self.options.groupSeparator, self.options.radixPoint);
+            }
            return isNaN(val) ? null : val;
        }
    
@@ -48,14 +50,16 @@
        
        // Create list of valid characters used to build regexs
        validChars = '\\-\\' + self.options.radixPoint;
-        if (self.options.groupSeparator)
+        if (self.options.groupSeparator) {
            validChars += '\\' + self.options.groupSeparator;
-        if (self.options.radix < 10)
+        }
+        if (self.options.radix < 10) {
            validChars += '0-' + self.options.radix;
-        else {
-            validChars += '0-9'
-            if (self.options.radix > 10)
+        } else {
+            validChars += '0-9';
+            if (self.options.radix > 10) {
                validChars += 'a-' + String.fromCharCode('a'.charCodeAt(0) +
self.options.radix - 11);
+            }
        }
        self.validChars = validChars;
@@ -68,7 +72,7 @@
        
        // check for precision in steppinng and set _precision as internal
        self._precision = parseInt(self.options.precision, 10);
-        if (self.options.step.toString().indexOf('.') != -1 && self._precision
== 0) {
+        if (self.options.step.toString().indexOf('.') != -1 && self._precision
=== 0) {
            var s = self.options.step.toString();
            self._precision = s.slice(s.indexOf('.')+1, s.length).length;
        }
@@ -130,12 +134,14 @@
            .end()
            .hover(function() {
                self.hovered = true;
-                if (typeof self.options.hide != 'boolean' && !self.focused
&& !self.disabled)
+                if (typeof self.options.hide != 'boolean' && !self.focused
&& !self.disabled) {
                    self._delay(self._show, 100, 'hide', self.options.hide);
+                }
            }, function() {
                self.hovered = false;
-                if (typeof self.options.hide != 'boolean' && !self.focused)
+                if (typeof self.options.hide != 'boolean' && !self.focused) {
                    self._delay(self._hide, 100, 'hide', self.options.hide);
+                }
            });
        
        self.buttons = widget.find('button')
@@ -147,8 +153,9 @@
                    self._mouseup(event);
                }
            });
-        if (self.options.hide)
+        if (self.options.hide) {
            self._hide();
+        }
        // DataList: Set contraints for object length and step size.
        // Manipulate height of spinner.
@@ -191,13 +198,15 @@
            }
            self.focused = true;
-            if (!self.hovered && typeof self.options.hide != 'boolean'
&& !self.disabled)
+            if (!self.hovered && typeof self.options.hide != 'boolean'
&& !self.disabled) {
                self._delay(self._show, 100, 'hide', self.options.hide);
+            }
        })
        .bind('blur'+namespace, function(event) {
            self.focused = false;
-            if (!self.hovered && typeof self.options.hide != 'boolean')
+            if (!self.hovered && typeof self.options.hide != 'boolean') {
                self._delay(self._hide, 100, 'hide', self.options.hide);
+            }
            self._cleanUp();
        });
@@ -206,9 +215,10 @@
        }
        
        // disable spinner if element was already disabled
-        if (self.element.attr("disabled"))
+        if (self.element.attr("disabled")) {
            self.disable();
-            
+        }
+
        self._extend('initComplete');
    },
    
@@ -234,9 +244,10 @@
        }
        // force width if passed through options
-        if (self.options.width)
+        if (self.options.width) {
            self.element.width(self.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(){
@@ -356,16 +367,18 @@
    },
    _parseValue: function(val, radix, groupSeparator, radixPoint) {
        // Because groupSeparator is included in the regex, we must replace it
independently
-        if (groupSeparator)
+        if (groupSeparator) {
            val = val.replace(groupSeparator, '');
-            
+        }
+
        val = val.replace(new RegExp('[^' + this.validChars
+ ']', 'gi'), '').split(radixPoint);
        
        result = parseInt(val[0], radix);
-        if (val.length > 1)
+        if (val.length > 1) {
            result += parseInt(val[1], radix) / Math.pow(radix, val[1].length) *
                // must test first character of val[0] for minus sign because -0 is
parsed as 0 in result
                (val[0][0] == '-' ? -1 : 1);
+        }
        
        return result;
    },
@@ -431,7 +444,7 @@
        
        if (curDelay.i) {
            // don't do anything if resetting the same delay
-            if (curDelay.f === callback) return;
+            if (curDelay.f === callback) { return; }
            clearTimeout(curDelay.i);
        }
        
@@ -443,31 +456,34 @@
    },
    _show: function(speed) {
        var buttons = this.buttons.stop();
-        if (!speed)
+        if (!speed) {
            buttons.css('opacity', 1);
-        else
+        } else {
            buttons.fadeTo(speed, 1);
+        }
        return this;
    },
    _hide: function(speed) {
        // also removeClass(hover) in case it was left despite losing mouse hover
        var buttons = this.buttons.stop().removeClass(hover);
-        if (!speed)
+        if (!speed) {
            buttons.css('opacity', 0);
-        else
+        } else {
            buttons.fadeTo(speed, 0);
+        }
        return this;
    },
    _setData: function(key, value) {
-        if ((key == 'mouseWheel') && (value != this.options.mouseWheel) &&
$.fn.mousewheel)
+        if ((key == 'mouseWheel') && (value != this.options.mouseWheel) &&
$.fn.mousewheel) {
            this.element[value ? 'mousewheel' : 'unmousewheel'](this._mousewheel);
-        else if (key == 'hide') {
-            if (typeof value != 'boolean')
+        } else if (key == 'hide') {
+            if (typeof value != 'boolean') {
                this[this.hovered || this.focused ? '_show' : '_hide']();
-            else if (value)
+            } else if (value) {
                this._hide();
-            else
+            } else {
                this._show();
+            }
        }
        // write attributes back to element if original exist
        else if ($.inArray(key, ['min','max','step']) != -1 &&
this.element.attr(key) && this.options[key] != null) {
@@ -477,7 +493,6 @@
        $.widget.prototype._setData.call(this, key, value);
    },
    
-    plugins: {},
    ui: function(event) {
        return {
            options: this.options,
@@ -487,9 +502,6 @@
        };
    },
    destroy: function() {
-        if (!$.data(this.element[0], 'spinner')) {
-            return;
-        }
        if ($.fn.mousewheel) {
            this.element.unmousewheel();
        }
@@ -533,9 +545,10 @@
        return this;
    },
    value: function(newVal) {
-        if (!arguments.length)
+        if (!arguments.length) {
            return this._getValue();
-            
+        }
+        
        this._setValue(newVal);
        return this;
    },
@@ -548,12 +561,10 @@
        return this;
    },
    pageUp: function(pages) {
-        this.stepUp((pages || 1) * this.options.page);        
-        return this;
+        return this.stepUp((pages || 1) * this.options.page);        
    },
    pageDown: function(pages) {
-        this.stepDown((pages || 1) * this.options.page);        
-        return this;
+        return this.stepDown((pages || 1) * this.options.page);        
    }
});
@@ -593,16 +604,21 @@
                part = Math.floor(((Math.abs(num) - whole) * Math.pow(radix,
dec))).toString(radix),
                regex = /(\d+)(\d{3})/;
            
-            while (regex.test(result) && group)
result=result.replace(regex, '$1'+group+'$2');
+            while (regex.test(result) && group) {
+                result=result.replace(regex, '$1'+group+'$2');
+            }
            
            if (dec > 0) {
-                while (part.length < dec) part = '0' + part;
+                while (part.length < dec) {
+                    part = '0' + part;
+                }
                result += pt + part;
            }
            
-            while (padLength && (result.length < padLength))
+            while (padLength && (result.length < padLength)) {
                result = '0' + result;
-                        
+            }
+            
            return (num < 0 ? '-' : '') + result;
        }
    }