r2985 commited - Dev widget factory: beginning of first attempt.
Revision: 2985
Author: scott.gonzalez
Date: Sun Jul 26 16:43:28 2009
Log: Dev widget factory: beginning of first attempt.
http://code.google.com/p/jquery-ui/source/detail?r=2985
Modified:
/branches/dev/widget-factory/ui/ui.core.js
=======================================
--- /branches/dev/widget-factory/ui/ui.core.js Sun Jun 14 19:01:11 2009
+++ /branches/dev/widget-factory/ui/ui.core.js Sun Jul 26 16:43:28 2009
@@ -214,19 +214,40 @@
});
-// $.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);
};
-
+
+ $[namespace] = $[namespace] || {};
+ $[namespace][name] = function(options, element) {
+ // allow instantiation without initializing for simple inheritance
+ (arguments.length && this._widgetInit(options, element));
+ };
+ $[namespace][name].prototype = $.extend(new base(), {
+ namespace: namespace,
+ widgetName: name,
+ widgetEventPrefix: $[namespace][name].eventPrefix || name,
+ widgetBaseClass: fullName
+ }, prototype);
+
+ $.widget.bridge(name, $[namespace][name]);
+};
+
+$.widget.bridge = function(name, object) {
+
// create plugin method
$.fn[name] = function(options) {
var isMethodCall = (typeof options == 'string'),
@@ -250,57 +271,40 @@
}
})
: this.each(function() {
- ($.data(this, name) ||
- $.data(this, name, new $[namespace][name](this, options))._init());
+ ($.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: '',
+ defaults: {
+ disabled: false
+ },
+ _widgetInit: function(options, element) {
+ this.element = $(element).data(this.widgetName, this);
this.options = $.extend(true, {},
- $.widget.defaults,
- $[namespace][name].defaults,
- $.metadata && $.metadata.get(element)[name],
+ this.options,
+ $.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', 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) {
@@ -309,22 +313,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') {
@@ -334,15 +335,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) {
@@ -370,9 +371,6 @@
}
};
-$.widget.defaults = {
- disabled: false
-};
/** Mouse Interaction Plugin **/