jquery.ui.widget.js 7.1 KB

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