ui.widget.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*!
  2. * jQuery UI Widget 1.8.5
  3. *
  4. * Copyright 2010, 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.substring( 0, 1 ) === "_" ) {
  84. return returnValue;
  85. }
  86. if ( isMethodCall ) {
  87. this.each(function() {
  88. var instance = $.data( this, name );
  89. if ( !instance ) {
  90. throw "cannot call methods on " + name + " prior to initialization; " +
  91. "attempted to call method '" + options + "'";
  92. }
  93. if ( !$.isFunction( instance[options] ) ) {
  94. throw "no such method '" + options + "' for " + name + " widget instance";
  95. }
  96. var methodValue = instance[ options ].apply( instance, args );
  97. if ( methodValue !== instance && methodValue !== undefined ) {
  98. returnValue = methodValue;
  99. return false;
  100. }
  101. });
  102. } else {
  103. this.each(function() {
  104. var instance = $.data( this, name );
  105. if ( instance ) {
  106. instance.option( options || {} )._init();
  107. } else {
  108. $.data( this, name, new object( options, this ) );
  109. }
  110. });
  111. }
  112. return returnValue;
  113. };
  114. };
  115. $.Widget = function( options, element ) {
  116. // allow instantiation without initializing for simple inheritance
  117. if ( arguments.length ) {
  118. this._createWidget( options, element );
  119. }
  120. };
  121. $.Widget.prototype = {
  122. widgetName: "widget",
  123. widgetEventPrefix: "",
  124. options: {
  125. disabled: false
  126. },
  127. _createWidget: function( options, element ) {
  128. // $.widget.bridge stores the plugin instance, but we do it anyway
  129. // so that it's stored even before the _create function runs
  130. $.data( element, this.widgetName, this );
  131. this.element = $( element );
  132. this.options = $.extend( true, {},
  133. this.options,
  134. $.metadata && $.metadata.get( element )[ this.widgetName ],
  135. options );
  136. var self = this;
  137. this.element.bind( "remove." + this.widgetName, function() {
  138. self.destroy();
  139. });
  140. this._create();
  141. this._init();
  142. },
  143. _create: function() {},
  144. _init: function() {},
  145. destroy: function() {
  146. this.element
  147. .unbind( "." + this.widgetName )
  148. .removeData( this.widgetName );
  149. this.widget()
  150. .unbind( "." + this.widgetName )
  151. .removeAttr( "aria-disabled" )
  152. .removeClass(
  153. this.widgetBaseClass + "-disabled " +
  154. "ui-state-disabled" );
  155. },
  156. widget: function() {
  157. return this.element;
  158. },
  159. option: function( key, value ) {
  160. var options = key,
  161. self = this;
  162. if ( arguments.length === 0 ) {
  163. // don't return a reference to the internal hash
  164. return $.extend( {}, self.options );
  165. }
  166. if (typeof key === "string" ) {
  167. if ( value === undefined ) {
  168. return this.options[ key ];
  169. }
  170. options = {};
  171. options[ key ] = value;
  172. }
  173. $.each( options, function( key, value ) {
  174. self._setOption( key, value );
  175. });
  176. return self;
  177. },
  178. _setOption: function( key, value ) {
  179. this.options[ key ] = value;
  180. if ( key === "disabled" ) {
  181. this.widget()
  182. [ value ? "addClass" : "removeClass"](
  183. this.widgetBaseClass + "-disabled" + " " +
  184. "ui-state-disabled" )
  185. .attr( "aria-disabled", value );
  186. }
  187. return this;
  188. },
  189. enable: function() {
  190. return this._setOption( "disabled", false );
  191. },
  192. disable: function() {
  193. return this._setOption( "disabled", true );
  194. },
  195. _trigger: function( type, event, data ) {
  196. var callback = this.options[ type ];
  197. event = $.Event( event );
  198. event.type = ( type === this.widgetEventPrefix ?
  199. type :
  200. this.widgetEventPrefix + type ).toLowerCase();
  201. data = data || {};
  202. // copy original event properties over to the new event
  203. // this would happen if we could call $.event.fix instead of $.Event
  204. // but we don't have a way to force an event to be fixed multiple times
  205. if ( event.originalEvent ) {
  206. for ( var i = $.event.props.length, prop; i; ) {
  207. prop = $.event.props[ --i ];
  208. event[ prop ] = event.originalEvent[ prop ];
  209. }
  210. }
  211. this.element.trigger( event, data );
  212. return !( $.isFunction(callback) &&
  213. callback.call( this.element[0], event, data ) === false ||
  214. event.isDefaultPrevented() );
  215. }
  216. };
  217. })( jQuery );