jquery.ui.widget.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*!
  2. * jQuery UI Widget 1.8.23
  3. *
  4. * Copyright 2012, AUTHORS.txt (http://jqueryui.com/about)
  5. * Dual licensed under the MIT or GPL Version 2 licenses.
  6. * http://jquery.org/license
  7. *
  8. * http://docs.jquery.com/UI/Widget
  9. */
  10. (function ($, undefined) {
  11. // jQuery 1.4+
  12. if ($.cleanData) {
  13. var _cleanData = $.cleanData;
  14. $.cleanData = function (elems) {
  15. for (var i = 0, elem; (elem = elems[i]) != null; i++) {
  16. try {
  17. $(elem).triggerHandler("remove");
  18. // http://bugs.jquery.com/ticket/8235
  19. } catch (e) {
  20. }
  21. }
  22. _cleanData(elems);
  23. };
  24. } else {
  25. var _remove = $.fn.remove;
  26. $.fn.remove = function (selector, keepData) {
  27. return this.each(function () {
  28. if (!keepData) {
  29. if (!selector || $.filter(selector, [ this ]).length) {
  30. $("*", this).add([ this ]).each(function () {
  31. try {
  32. $(this).triggerHandler("remove");
  33. // http://bugs.jquery.com/ticket/8235
  34. } catch (e) {
  35. }
  36. });
  37. }
  38. }
  39. return _remove.call($(this), selector, keepData);
  40. });
  41. };
  42. }
  43. $.widget = function (name, base, prototype) {
  44. var namespace = name.split(".")[ 0 ],
  45. fullName;
  46. name = name.split(".")[ 1 ];
  47. fullName = namespace + "-" + name;
  48. if (!prototype) {
  49. prototype = base;
  50. base = $.Widget;
  51. }
  52. // create selector for plugin
  53. $.expr[ ":" ][ fullName ] = function (elem) {
  54. return !!$.data(elem, name);
  55. };
  56. $[ namespace ] = $[ namespace ] || {};
  57. $[ namespace ][ name ] = function (options, element) {
  58. // allow instantiation without initializing for simple inheritance
  59. if (arguments.length) {
  60. this._createWidget(options, element);
  61. }
  62. };
  63. var basePrototype = new base();
  64. // we need to make the options hash a property directly on the new instance
  65. // otherwise we'll modify the options hash on the prototype that we're
  66. // inheriting from
  67. // $.each( basePrototype, function( key, val ) {
  68. // if ( $.isPlainObject(val) ) {
  69. // basePrototype[ key ] = $.extend( {}, val );
  70. // }
  71. // });
  72. basePrototype.options = $.extend(true, {}, basePrototype.options);
  73. $[ namespace ][ name ].prototype = $.extend(true, basePrototype, {
  74. namespace:namespace,
  75. widgetName:name,
  76. widgetEventPrefix:$[ namespace ][ name ].prototype.widgetEventPrefix || name,
  77. widgetBaseClass:fullName
  78. }, prototype);
  79. $.widget.bridge(name, $[ namespace ][ name ]);
  80. };
  81. $.widget.bridge = function (name, object) {
  82. $.fn[ name ] = function (options) {
  83. var isMethodCall = typeof options === "string",
  84. args = Array.prototype.slice.call(arguments, 1),
  85. returnValue = this;
  86. // allow multiple hashes to be passed on init
  87. options = !isMethodCall && args.length ?
  88. $.extend.apply(null, [ true, options ].concat(args)) :
  89. options;
  90. // prevent calls to internal methods
  91. if (isMethodCall && options.charAt(0) === "_") {
  92. return returnValue;
  93. }
  94. if (isMethodCall) {
  95. this.each(function () {
  96. var instance = $.data(this, name),
  97. methodValue = instance && $.isFunction(instance[options]) ?
  98. instance[ options ].apply(instance, args) :
  99. instance;
  100. // TODO: add this back in 1.9 and use $.error() (see #5972)
  101. // if ( !instance ) {
  102. // throw "cannot call methods on " + name + " prior to initialization; " +
  103. // "attempted to call method '" + options + "'";
  104. // }
  105. // if ( !$.isFunction( instance[options] ) ) {
  106. // throw "no such method '" + options + "' for " + name + " widget instance";
  107. // }
  108. // var methodValue = instance[ options ].apply( instance, args );
  109. if (methodValue !== instance && methodValue !== undefined) {
  110. returnValue = methodValue;
  111. return false;
  112. }
  113. });
  114. } else {
  115. this.each(function () {
  116. var instance = $.data(this, name);
  117. if (instance) {
  118. instance.option(options || {})._init();
  119. } else {
  120. $.data(this, name, new object(options, this));
  121. }
  122. });
  123. }
  124. return returnValue;
  125. };
  126. };
  127. $.Widget = function (options, element) {
  128. // allow instantiation without initializing for simple inheritance
  129. if (arguments.length) {
  130. this._createWidget(options, element);
  131. }
  132. };
  133. $.Widget.prototype = {
  134. widgetName:"widget",
  135. widgetEventPrefix:"",
  136. options:{
  137. disabled:false
  138. },
  139. _createWidget:function (options, element) {
  140. // $.widget.bridge stores the plugin instance, but we do it anyway
  141. // so that it's stored even before the _create function runs
  142. $.data(element, this.widgetName, this);
  143. this.element = $(element);
  144. this.options = $.extend(true, {},
  145. this.options,
  146. this._getCreateOptions(),
  147. options);
  148. var self = this;
  149. this.element.bind("remove." + this.widgetName, function () {
  150. self.destroy();
  151. });
  152. this._create();
  153. this._trigger("create");
  154. this._init();
  155. },
  156. _getCreateOptions:function () {
  157. return $.metadata && $.metadata.get(this.element[0])[ this.widgetName ];
  158. },
  159. _create:function () {
  160. },
  161. _init:function () {
  162. },
  163. destroy:function () {
  164. this.element
  165. .unbind("." + this.widgetName)
  166. .removeData(this.widgetName);
  167. this.widget()
  168. .unbind("." + this.widgetName)
  169. .removeAttr("aria-disabled")
  170. .removeClass(
  171. this.widgetBaseClass + "-disabled " +
  172. "ui-state-disabled");
  173. },
  174. widget:function () {
  175. return this.element;
  176. },
  177. option:function (key, value) {
  178. var options = key;
  179. if (arguments.length === 0) {
  180. // don't return a reference to the internal hash
  181. return $.extend({}, this.options);
  182. }
  183. if (typeof key === "string") {
  184. if (value === undefined) {
  185. return this.options[ key ];
  186. }
  187. options = {};
  188. options[ key ] = value;
  189. }
  190. this._setOptions(options);
  191. return this;
  192. },
  193. _setOptions:function (options) {
  194. var self = this;
  195. $.each(options, function (key, value) {
  196. self._setOption(key, value);
  197. });
  198. return this;
  199. },
  200. _setOption:function (key, value) {
  201. this.options[ key ] = value;
  202. if (key === "disabled") {
  203. this.widget()
  204. [ value ? "addClass" : "removeClass"](
  205. this.widgetBaseClass + "-disabled" + " " +
  206. "ui-state-disabled")
  207. .attr("aria-disabled", value);
  208. }
  209. return this;
  210. },
  211. enable:function () {
  212. return this._setOption("disabled", false);
  213. },
  214. disable:function () {
  215. return this._setOption("disabled", true);
  216. },
  217. _trigger:function (type, event, data) {
  218. var prop, orig,
  219. callback = this.options[ type ];
  220. data = data || {};
  221. event = $.Event(event);
  222. event.type = ( type === this.widgetEventPrefix ?
  223. type :
  224. this.widgetEventPrefix + type ).toLowerCase();
  225. // the original event may come from any element
  226. // so we need to reset the target on the new event
  227. event.target = this.element[ 0 ];
  228. // copy original event properties over to the new event
  229. orig = event.originalEvent;
  230. if (orig) {
  231. for (prop in orig) {
  232. if (!( prop in event )) {
  233. event[ prop ] = orig[ prop ];
  234. }
  235. }
  236. }
  237. this.element.trigger(event, data);
  238. return !( $.isFunction(callback) &&
  239. callback.call(this.element[0], event, data) === false ||
  240. event.isDefaultPrevented() );
  241. }
  242. };
  243. })(jQuery);