r3510 committed - Pulled labs widget factory into dev branch.

r3510 committed - Pulled labs widget factory into dev branch.

Revision: 3510
Author: scott.gonzalez
Date: Tue Dec 22 19:37:18 2009
Log: Pulled labs widget factory into dev branch.
http://code.google.com/p/jquery-ui/source/detail?r=3510
Modified:
/branches/dev/ui/jquery.ui.draggable.js
/branches/dev/ui/jquery.ui.mouse.js
/branches/dev/ui/jquery.ui.resizable.js
/branches/dev/ui/jquery.ui.selectable.js
/branches/dev/ui/jquery.ui.sortable.js
/branches/dev/ui/jquery.ui.widget.js
=======================================
--- /branches/dev/ui/jquery.ui.draggable.js    Tue Dec 22 13:11:49 2009
+++ /branches/dev/ui/jquery.ui.draggable.js    Tue Dec 22 19:37:18 2009
@@ -14,7 +14,7 @@
*/
(function($) {
-$.widget("ui.draggable", $.extend({}, $.ui.mouse, {
+$.widget("ui.draggable", $.ui.mouse, {
    _init: function() {
@@ -419,7 +419,7 @@
        };
    }
-}));
+});
$.extend($.ui.draggable, {
    version: "@VERSION",
=======================================
--- /branches/dev/ui/jquery.ui.mouse.js    Tue Dec 22 13:11:49 2009
+++ /branches/dev/ui/jquery.ui.mouse.js    Tue Dec 22 19:37:18 2009
@@ -12,7 +12,12 @@
*/
(function($) {
-$.ui.mouse = {
+$.widget("ui.mouse", {
+    options: {
+        cancel: ':input,option',
+        distance: 1,
+        delay: 0
+    },
    _mouseInit: function() {
        var self = this;
@@ -151,13 +156,7 @@
    _mouseDrag: function(event) {},
    _mouseStop: function(event) {},
    _mouseCapture: function(event) { return true; }
-};
-
-$.ui.mouse.defaults = {
-    cancel: ':input,option',
-    distance: 1,
-    delay: 0
-};
+});
})(jQuery);
=======================================
--- /branches/dev/ui/jquery.ui.resizable.js    Tue Dec 22 13:11:49 2009
+++ /branches/dev/ui/jquery.ui.resizable.js    Tue Dec 22 19:37:18 2009
@@ -14,7 +14,7 @@
*/
(function($) {
-$.widget("ui.resizable", $.extend({}, $.ui.mouse, {
+$.widget("ui.resizable", $.ui.mouse, {
    _init: function() {
@@ -498,7 +498,7 @@
        };
    }
-}));
+});
$.extend($.ui.resizable, {
    version: "@VERSION",
=======================================
--- /branches/dev/ui/jquery.ui.selectable.js    Tue Dec 22 13:11:49 2009
+++ /branches/dev/ui/jquery.ui.selectable.js    Tue Dec 22 19:37:18 2009
@@ -14,7 +14,7 @@
*/
(function($) {
-$.widget("ui.selectable", $.extend({}, $.ui.mouse, {
+$.widget("ui.selectable", $.ui.mouse, {
    _init: function() {
        var self = this;
@@ -243,7 +243,7 @@
        return false;
    }
-}));
+});
$.extend($.ui.selectable, {
    version: "@VERSION",
=======================================
--- /branches/dev/ui/jquery.ui.sortable.js    Tue Dec 22 13:11:49 2009
+++ /branches/dev/ui/jquery.ui.sortable.js    Tue Dec 22 19:37:18 2009
@@ -14,7 +14,7 @@
*/
(function($) {
-$.widget("ui.sortable", $.extend({}, $.ui.mouse, {
+$.widget("ui.sortable", $.ui.mouse, {
    _init: function() {
        var o = this.options;
@@ -1018,7 +1018,7 @@
        };
    }
-}));
+});
$.extend($.ui.sortable, {
    version: "@VERSION",
=======================================
--- /branches/dev/ui/jquery.ui.widget.js    Tue Dec 22 13:11:49 2009
+++ /branches/dev/ui/jquery.ui.widget.js    Tue Dec 22 19:37:18 2009
@@ -20,30 +20,43 @@
    return _remove.apply(this, arguments);
};
-// $.widget is a factory to create jQuery plugins
-// taking some boilerplate code out of the plugin code
-$.widget = function(name, prototype) {
+$.widget = function(name, base, prototype) {
    var namespace = name.split(".")[0],
        fullName;
    name = name.split(".")[1];
    fullName = namespace + '-' + name;
+    if (!prototype) {
+        prototype = base;
+        base = $.Widget;
+    }
+
    // create selector for plugin
    $.expr[':'][fullName] = function(elem) {
        return !!$.data(elem, name);
    };
-
-    // create plugin method
+
+    $[namespace] = $[namespace] || {};
+    $[namespace][name] = function(options, element) {
+        // allow instantiation without initializing for simple inheritance
+        (arguments.length && this._widgetInit(options, element));
+    };
+    $[namespace][name].prototype = $.extend(true, new base(), {
+        namespace: namespace,
+        widgetName: name,
+        widgetEventPrefix: $[namespace][name].prototype.widgetEventPrefix ||
name,
+        widgetBaseClass: fullName
+    }, prototype);
+
+    $.widget.bridge(name, $[namespace][name]);
+};
+
+$.widget.bridge = function(name, object) {
    $.fn[name] = function(options) {
        var isMethodCall = (typeof options == 'string'),
            args = Array.prototype.slice.call(arguments, 1),
            returnValue = this;
-        // allow multiple hashes to be passed on init
-        options = !isMethodCall && args.length
-            ? $.extend.apply(null, [true, options].concat(args))
-            : options;
-
        // prevent calls to internal methods
        if (isMethodCall && options.substring(0, 1) == '_') {
            return returnValue;
@@ -61,57 +74,44 @@
                }
            })
            : this.each(function() {
-                ($.data(this, name) ||
-                    $.data(this, name, new $[namespace][name](this, options))._init());
+                ($.data(this, name) || $.data(this, name, new object(options, this)));
            }));
        return returnValue;
    };
-
-    // create widget constructor
-    $[namespace] = $[namespace] || {};
-    $[namespace][name] = function(element, options) {
-        var self = this;
-
-        this.namespace = namespace;
-        this.widgetName = name;
-        this.widgetEventPrefix = $[namespace][name].eventPrefix || name;
-        this.widgetBaseClass = fullName;
-
+};
+
+$.Widget = function(options, element) {
+    // allow instantiation without initializing for simple inheritance
+    (arguments.length && this._widgetInit(options, element));
+};
+
+$.Widget.prototype = {
+    widgetName: 'widget',
+    widgetEventPrefix: '',
+    options: {
+        disabled: false
+    },
+    _widgetInit: function(options, element) {
+        // $.widget.bridge stores the plugin instance, but we do it anyway
+        // so that it's stored even before the _init function runs
+        this.element = $(element).data(this.widgetName, this);
        this.options = $.extend(true, {},
-            $.widget.defaults,
-            $[namespace][name].defaults,
-            $.metadata && $.metadata.get(element)[name],
+            this.options,
+            // DEPRECATED: move defaults to prototype.options
+            $[this.namespace][this.widgetName].defaults,
+            $.metadata && $.metadata.get(element)[this.widgetName],
            options);
-
-        this.element = $(element)
-            .bind('setData.' + name, function(event, key, value) {
-                if (event.target == element) {
-                    return self._setData(key, value);
-                }
-            })
-            .bind('getData.' + name, function(event, key) {
-                if (event.target == element) {
-                    return self._getData(key);
-                }
-            })
-            .bind('remove.' + name, function() {
-                return self.destroy();
-            });
-    };
-
-    // add widget prototype
-    $[namespace][name].prototype = $.extend({}, $.widget.prototype,
prototype);
-};
-
-$.widget.prototype = {
-    _init: function() {},
+        (this._init && this._init(options, element));
+    },
+
    destroy: function() {
-        this.element.removeData(this.widgetName)
-            .removeClass(this.widgetBaseClass + '-disabled' + ' ' + this.namespace
+ '-state-disabled')
-            .removeAttr('aria-disabled');
-
-        return this;
+        this.element
+            .removeData(this.widgetName)
+            .removeAttr('aria-disabled')
+            .removeClass(
+                this.widgetBaseClass + '-disabled ' +
+                this.namespace + '-state-disabled');
    },
    option: function(key, value) {
@@ -120,22 +120,19 @@
        if (typeof key == "string") {
            if (value === undefined) {
-                return this._getData(key);
+                return this.options[key];
            }
            options = {};
            options[key] = value;
        }
        $.each(options, function(key, value) {
-            self._setData(key, value);
+            self._setOption(key, value);
        });
        return self;
    },
-    _getData: function(key) {
-        return this.options[key];
-    },
-    _setData: function(key, value) {
+    _setOption: function(key, value) {
        this.options[key] = value;
        if (key == 'disabled') {
@@ -145,15 +142,15 @@
                    this.namespace + '-state-disabled')
                .attr("aria-disabled", value);
        }
+
+        return this;
    },
    enable: function() {
-        this._setData('disabled', false);
-        return this;
+        return this._setOption('disabled', false);
    },
    disable: function() {
-        this._setData('disabled', true);
-        return this;
+        return this._setOption('disabled', true);
    },
    _trigger: function(type, event, data) {
@@ -181,9 +178,7 @@
    }
};
-$.widget.defaults = {
-    disabled: false
-};
-
+// DEPRECATED: use the plugin's parent widget instead of $.widget
+$.widget.prototype = $.Widget.prototype;
})(jQuery);
--