jquery.ui.widget.js 6.7 KB

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