mirror of
https://github.com/jquery/jquery-ui.git
synced 2026-04-20 03:02:41 -04:00
Merged in /branches/dev r3251:3620 (excluding autocomplete, modal, tooltip, menu; including menu static tests).
This commit is contained in:
160
ui/jquery.ui.widget.js
vendored
160
ui/jquery.ui.widget.js
vendored
@@ -20,25 +20,54 @@ $.fn.remove = function() {
|
||||
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));
|
||||
};
|
||||
|
||||
var basePrototype = new base();
|
||||
// we need to make the options hash a property directly on the new instance
|
||||
// otherwise we'll modify the options hash on the prototype that we're
|
||||
// inheriting from
|
||||
// $.each(basePrototype, function(key, val) {
|
||||
// if ($.isPlainObject(val)) {
|
||||
// basePrototype[key] = $.extend({}, val);
|
||||
// }
|
||||
// });
|
||||
basePrototype.options = $.extend({}, basePrototype.options);
|
||||
$[namespace][name].prototype = $.extend(true, basePrototype, {
|
||||
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))
|
||||
@@ -61,99 +90,102 @@ $.widget = function(name, prototype) {
|
||||
}
|
||||
})
|
||||
: 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;
|
||||
|
||||
this.options = $.extend(true, {},
|
||||
$.widget.defaults,
|
||||
$[namespace][name].defaults,
|
||||
$.metadata && $.metadata.get(element)[name],
|
||||
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() {},
|
||||
destroy: function() {
|
||||
this.element.removeData(this.widgetName)
|
||||
.removeClass(this.widgetBaseClass + '-disabled' + ' ' + this.namespace + '-state-disabled')
|
||||
.removeAttr('aria-disabled');
|
||||
$.Widget = function(options, element) {
|
||||
// allow instantiation without initializing for simple inheritance
|
||||
(arguments.length && this._widgetInit(options, element));
|
||||
};
|
||||
|
||||
return this;
|
||||
$.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, {},
|
||||
this.options,
|
||||
// DEPRECATED: move defaults to prototype.options
|
||||
$[this.namespace][this.widgetName].defaults,
|
||||
$.metadata && $.metadata.get(element)[this.widgetName],
|
||||
options);
|
||||
|
||||
// TODO: use bind's scope option when moving to jQuery 1.4
|
||||
var self = this;
|
||||
this.element.bind('remove.' + this.widgetName, function() {
|
||||
self.destroy();
|
||||
});
|
||||
|
||||
(this._init && this._init(options, element));
|
||||
},
|
||||
|
||||
destroy: function() {
|
||||
this.element
|
||||
.unbind('.' + this.widgetName)
|
||||
.removeData(this.widgetName);
|
||||
this.widget()
|
||||
.unbind('.' + this.widgetName)
|
||||
.removeAttr('aria-disabled')
|
||||
.removeClass(
|
||||
this.widgetBaseClass + '-disabled ' +
|
||||
this.namespace + '-state-disabled');
|
||||
},
|
||||
|
||||
widget: function() {
|
||||
return this.element;
|
||||
},
|
||||
|
||||
option: function(key, value) {
|
||||
var options = key,
|
||||
self = this;
|
||||
|
||||
if (arguments.length === 0) {
|
||||
// don't return a reference to the internal hash
|
||||
return $.extend({}, self.options);
|
||||
}
|
||||
|
||||
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') {
|
||||
this.element
|
||||
this.widget()
|
||||
[value ? 'addClass' : 'removeClass'](
|
||||
this.widgetBaseClass + '-disabled' + ' ' +
|
||||
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 +213,7 @@ $.widget.prototype = {
|
||||
}
|
||||
};
|
||||
|
||||
$.widget.defaults = {
|
||||
disabled: false
|
||||
};
|
||||
|
||||
// DEPRECATED: use the plugin's parent widget instead of $.widget
|
||||
$.widget.prototype = $.Widget.prototype;
|
||||
|
||||
})(jQuery);
|
||||
|
||||
Reference in New Issue
Block a user