You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jquery.ui.widget.js 7.4KB

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