jquery.ui.widget.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * jQuery UI Widget 1.9.1+amd
  3. * https://github.com/blueimp/jQuery-File-Upload
  4. *
  5. * Copyright 2012 jQuery Foundation and other contributors
  6. * Released under the MIT license.
  7. * http://jquery.org/license
  8. *
  9. * http://api.jqueryui.com/jQuery.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. var uuid = 0,
  21. slice = Array.prototype.slice,
  22. _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. $.widget = function( name, base, prototype ) {
  33. var fullName, existingConstructor, constructor, basePrototype,
  34. namespace = name.split( "." )[ 0 ];
  35. name = name.split( "." )[ 1 ];
  36. fullName = namespace + "-" + name;
  37. if ( !prototype ) {
  38. prototype = base;
  39. base = $.Widget;
  40. }
  41. // create selector for plugin
  42. $.expr[ ":" ][ fullName.toLowerCase() ] = function( elem ) {
  43. return !!$.data( elem, fullName );
  44. };
  45. $[ namespace ] = $[ namespace ] || {};
  46. existingConstructor = $[ namespace ][ name ];
  47. constructor = $[ namespace ][ name ] = function( options, element ) {
  48. // allow instantiation without "new" keyword
  49. if ( !this._createWidget ) {
  50. return new constructor( options, element );
  51. }
  52. // allow instantiation without initializing for simple inheritance
  53. // must use "new" keyword (the code above always passes args)
  54. if ( arguments.length ) {
  55. this._createWidget( options, element );
  56. }
  57. };
  58. // extend with the existing constructor to carry over any static properties
  59. $.extend( constructor, existingConstructor, {
  60. version: prototype.version,
  61. // copy the object used to create the prototype in case we need to
  62. // redefine the widget later
  63. _proto: $.extend( {}, prototype ),
  64. // track widgets that inherit from this widget in case this widget is
  65. // redefined after a widget inherits from it
  66. _childConstructors: []
  67. });
  68. basePrototype = new base();
  69. // we need to make the options hash a property directly on the new instance
  70. // otherwise we'll modify the options hash on the prototype that we're
  71. // inheriting from
  72. basePrototype.options = $.widget.extend( {}, basePrototype.options );
  73. $.each( prototype, function( prop, value ) {
  74. if ( $.isFunction( value ) ) {
  75. prototype[ prop ] = (function() {
  76. var _super = function() {
  77. return base.prototype[ prop ].apply( this, arguments );
  78. },
  79. _superApply = function( args ) {
  80. return base.prototype[ prop ].apply( this, args );
  81. };
  82. return function() {
  83. var __super = this._super,
  84. __superApply = this._superApply,
  85. returnValue;
  86. this._super = _super;
  87. this._superApply = _superApply;
  88. returnValue = value.apply( this, arguments );
  89. this._super = __super;
  90. this._superApply = __superApply;
  91. return returnValue;
  92. };
  93. })();
  94. }
  95. });
  96. constructor.prototype = $.widget.extend( basePrototype, {
  97. // TODO: remove support for widgetEventPrefix
  98. // always use the name + a colon as the prefix, e.g., draggable:start
  99. // don't prefix for widgets that aren't DOM-based
  100. widgetEventPrefix: basePrototype.widgetEventPrefix || name
  101. }, prototype, {
  102. constructor: constructor,
  103. namespace: namespace,
  104. widgetName: name,
  105. // TODO remove widgetBaseClass, see #8155
  106. widgetBaseClass: fullName,
  107. widgetFullName: fullName
  108. });
  109. // If this widget is being redefined then we need to find all widgets that
  110. // are inheriting from it and redefine all of them so that they inherit from
  111. // the new version of this widget. We're essentially trying to replace one
  112. // level in the prototype chain.
  113. if ( existingConstructor ) {
  114. $.each( existingConstructor._childConstructors, function( i, child ) {
  115. var childPrototype = child.prototype;
  116. // redefine the child widget using the same prototype that was
  117. // originally used, but inherit from the new version of the base
  118. $.widget( childPrototype.namespace + "." + childPrototype.widgetName, constructor, child._proto );
  119. });
  120. // remove the list of existing child constructors from the old constructor
  121. // so the old child constructors can be garbage collected
  122. delete existingConstructor._childConstructors;
  123. } else {
  124. base._childConstructors.push( constructor );
  125. }
  126. $.widget.bridge( name, constructor );
  127. };
  128. $.widget.extend = function( target ) {
  129. var input = slice.call( arguments, 1 ),
  130. inputIndex = 0,
  131. inputLength = input.length,
  132. key,
  133. value;
  134. for ( ; inputIndex < inputLength; inputIndex++ ) {
  135. for ( key in input[ inputIndex ] ) {
  136. value = input[ inputIndex ][ key ];
  137. if ( input[ inputIndex ].hasOwnProperty( key ) && value !== undefined ) {
  138. // Clone objects
  139. if ( $.isPlainObject( value ) ) {
  140. target[ key ] = $.isPlainObject( target[ key ] ) ?
  141. $.widget.extend( {}, target[ key ], value ) :
  142. // Don't extend strings, arrays, etc. with objects
  143. $.widget.extend( {}, value );
  144. // Copy everything else by reference
  145. } else {
  146. target[ key ] = value;
  147. }
  148. }
  149. }
  150. }
  151. return target;
  152. };
  153. $.widget.bridge = function( name, object ) {
  154. var fullName = object.prototype.widgetFullName;
  155. $.fn[ name ] = function( options ) {
  156. var isMethodCall = typeof options === "string",
  157. args = slice.call( arguments, 1 ),
  158. returnValue = this;
  159. // allow multiple hashes to be passed on init
  160. options = !isMethodCall && args.length ?
  161. $.widget.extend.apply( null, [ options ].concat(args) ) :
  162. options;
  163. if ( isMethodCall ) {
  164. this.each(function() {
  165. var methodValue,
  166. instance = $.data( this, fullName );
  167. if ( !instance ) {
  168. return $.error( "cannot call methods on " + name + " prior to initialization; " +
  169. "attempted to call method '" + options + "'" );
  170. }
  171. if ( !$.isFunction( instance[options] ) || options.charAt( 0 ) === "_" ) {
  172. return $.error( "no such method '" + options + "' for " + name + " widget instance" );
  173. }
  174. methodValue = instance[ options ].apply( instance, args );
  175. if ( methodValue !== instance && methodValue !== undefined ) {
  176. returnValue = methodValue && methodValue.jquery ?
  177. returnValue.pushStack( methodValue.get() ) :
  178. methodValue;
  179. return false;
  180. }
  181. });
  182. } else {
  183. this.each(function() {
  184. var instance = $.data( this, fullName );
  185. if ( instance ) {
  186. instance.option( options || {} )._init();
  187. } else {
  188. new object( options, this );
  189. }
  190. });
  191. }
  192. return returnValue;
  193. };
  194. };
  195. $.Widget = function( /* options, element */ ) {};
  196. $.Widget._childConstructors = [];
  197. $.Widget.prototype = {
  198. widgetName: "widget",
  199. widgetEventPrefix: "",
  200. defaultElement: "<div>",
  201. options: {
  202. disabled: false,
  203. // callbacks
  204. create: null
  205. },
  206. _createWidget: function( options, element ) {
  207. element = $( element || this.defaultElement || this )[ 0 ];
  208. this.element = $( element );
  209. this.uuid = uuid++;
  210. this.eventNamespace = "." + this.widgetName + this.uuid;
  211. this.options = $.widget.extend( {},
  212. this.options,
  213. this._getCreateOptions(),
  214. options );
  215. this.bindings = $();
  216. this.hoverable = $();
  217. this.focusable = $();
  218. if ( element !== this ) {
  219. // 1.9 BC for #7810
  220. // TODO remove dual storage
  221. $.data( element, this.widgetName, this );
  222. $.data( element, this.widgetFullName, this );
  223. this._on( this.element, {
  224. remove: function( event ) {
  225. if ( event.target === element ) {
  226. this.destroy();
  227. }
  228. }
  229. });
  230. this.document = $( element.style ?
  231. // element within the document
  232. element.ownerDocument :
  233. // element is window or document
  234. element.document || element );
  235. this.window = $( this.document[0].defaultView || this.document[0].parentWindow );
  236. }
  237. this._create();
  238. this._trigger( "create", null, this._getCreateEventData() );
  239. this._init();
  240. },
  241. _getCreateOptions: $.noop,
  242. _getCreateEventData: $.noop,
  243. _create: $.noop,
  244. _init: $.noop,
  245. destroy: function() {
  246. this._destroy();
  247. // we can probably remove the unbind calls in 2.0
  248. // all event bindings should go through this._on()
  249. this.element
  250. .unbind( this.eventNamespace )
  251. // 1.9 BC for #7810
  252. // TODO remove dual storage
  253. .removeData( this.widgetName )
  254. .removeData( this.widgetFullName )
  255. // support: jquery <1.6.3
  256. // http://bugs.jquery.com/ticket/9413
  257. .removeData( $.camelCase( this.widgetFullName ) );
  258. this.widget()
  259. .unbind( this.eventNamespace )
  260. .removeAttr( "aria-disabled" )
  261. .removeClass(
  262. this.widgetFullName + "-disabled " +
  263. "ui-state-disabled" );
  264. // clean up events and states
  265. this.bindings.unbind( this.eventNamespace );
  266. this.hoverable.removeClass( "ui-state-hover" );
  267. this.focusable.removeClass( "ui-state-focus" );
  268. },
  269. _destroy: $.noop,
  270. widget: function() {
  271. return this.element;
  272. },
  273. option: function( key, value ) {
  274. var options = key,
  275. parts,
  276. curOption,
  277. i;
  278. if ( arguments.length === 0 ) {
  279. // don't return a reference to the internal hash
  280. return $.widget.extend( {}, this.options );
  281. }
  282. if ( typeof key === "string" ) {
  283. // handle nested keys, e.g., "foo.bar" => { foo: { bar: ___ } }
  284. options = {};
  285. parts = key.split( "." );
  286. key = parts.shift();
  287. if ( parts.length ) {
  288. curOption = options[ key ] = $.widget.extend( {}, this.options[ key ] );
  289. for ( i = 0; i < parts.length - 1; i++ ) {
  290. curOption[ parts[ i ] ] = curOption[ parts[ i ] ] || {};
  291. curOption = curOption[ parts[ i ] ];
  292. }
  293. key = parts.pop();
  294. if ( value === undefined ) {
  295. return curOption[ key ] === undefined ? null : curOption[ key ];
  296. }
  297. curOption[ key ] = value;
  298. } else {
  299. if ( value === undefined ) {
  300. return this.options[ key ] === undefined ? null : this.options[ key ];
  301. }
  302. options[ key ] = value;
  303. }
  304. }
  305. this._setOptions( options );
  306. return this;
  307. },
  308. _setOptions: function( options ) {
  309. var key;
  310. for ( key in options ) {
  311. this._setOption( key, options[ key ] );
  312. }
  313. return this;
  314. },
  315. _setOption: function( key, value ) {
  316. this.options[ key ] = value;
  317. if ( key === "disabled" ) {
  318. this.widget()
  319. .toggleClass( this.widgetFullName + "-disabled ui-state-disabled", !!value )
  320. .attr( "aria-disabled", value );
  321. this.hoverable.removeClass( "ui-state-hover" );
  322. this.focusable.removeClass( "ui-state-focus" );
  323. }
  324. return this;
  325. },
  326. enable: function() {
  327. return this._setOption( "disabled", false );
  328. },
  329. disable: function() {
  330. return this._setOption( "disabled", true );
  331. },
  332. _on: function( element, handlers ) {
  333. var delegateElement,
  334. instance = this;
  335. // no element argument, shuffle and use this.element
  336. if ( !handlers ) {
  337. handlers = element;
  338. element = this.element;
  339. delegateElement = this.widget();
  340. } else {
  341. // accept selectors, DOM elements
  342. element = delegateElement = $( element );
  343. this.bindings = this.bindings.add( element );
  344. }
  345. $.each( handlers, function( event, handler ) {
  346. function handlerProxy() {
  347. // allow widgets to customize the disabled handling
  348. // - disabled as an array instead of boolean
  349. // - disabled class as method for disabling individual parts
  350. if ( instance.options.disabled === true ||
  351. $( this ).hasClass( "ui-state-disabled" ) ) {
  352. return;
  353. }
  354. return ( typeof handler === "string" ? instance[ handler ] : handler )
  355. .apply( instance, arguments );
  356. }
  357. // copy the guid so direct unbinding works
  358. if ( typeof handler !== "string" ) {
  359. handlerProxy.guid = handler.guid =
  360. handler.guid || handlerProxy.guid || $.guid++;
  361. }
  362. var match = event.match( /^(\w+)\s*(.*)$/ ),
  363. eventName = match[1] + instance.eventNamespace,
  364. selector = match[2];
  365. if ( selector ) {
  366. delegateElement.delegate( selector, eventName, handlerProxy );
  367. } else {
  368. element.bind( eventName, handlerProxy );
  369. }
  370. });
  371. },
  372. _off: function( element, eventName ) {
  373. eventName = (eventName || "").split( " " ).join( this.eventNamespace + " " ) + this.eventNamespace;
  374. element.unbind( eventName ).undelegate( eventName );
  375. },
  376. _delay: function( handler, delay ) {
  377. function handlerProxy() {
  378. return ( typeof handler === "string" ? instance[ handler ] : handler )
  379. .apply( instance, arguments );
  380. }
  381. var instance = this;
  382. return setTimeout( handlerProxy, delay || 0 );
  383. },
  384. _hoverable: function( element ) {
  385. this.hoverable = this.hoverable.add( element );
  386. this._on( element, {
  387. mouseenter: function( event ) {
  388. $( event.currentTarget ).addClass( "ui-state-hover" );
  389. },
  390. mouseleave: function( event ) {
  391. $( event.currentTarget ).removeClass( "ui-state-hover" );
  392. }
  393. });
  394. },
  395. _focusable: function( element ) {
  396. this.focusable = this.focusable.add( element );
  397. this._on( element, {
  398. focusin: function( event ) {
  399. $( event.currentTarget ).addClass( "ui-state-focus" );
  400. },
  401. focusout: function( event ) {
  402. $( event.currentTarget ).removeClass( "ui-state-focus" );
  403. }
  404. });
  405. },
  406. _trigger: function( type, event, data ) {
  407. var prop, orig,
  408. callback = this.options[ type ];
  409. data = data || {};
  410. event = $.Event( event );
  411. event.type = ( type === this.widgetEventPrefix ?
  412. type :
  413. this.widgetEventPrefix + type ).toLowerCase();
  414. // the original event may come from any element
  415. // so we need to reset the target on the new event
  416. event.target = this.element[ 0 ];
  417. // copy original event properties over to the new event
  418. orig = event.originalEvent;
  419. if ( orig ) {
  420. for ( prop in orig ) {
  421. if ( !( prop in event ) ) {
  422. event[ prop ] = orig[ prop ];
  423. }
  424. }
  425. }
  426. this.element.trigger( event, data );
  427. return !( $.isFunction( callback ) &&
  428. callback.apply( this.element[0], [ event ].concat( data ) ) === false ||
  429. event.isDefaultPrevented() );
  430. }
  431. };
  432. $.each( { show: "fadeIn", hide: "fadeOut" }, function( method, defaultEffect ) {
  433. $.Widget.prototype[ "_" + method ] = function( element, options, callback ) {
  434. if ( typeof options === "string" ) {
  435. options = { effect: options };
  436. }
  437. var hasOptions,
  438. effectName = !options ?
  439. method :
  440. options === true || typeof options === "number" ?
  441. defaultEffect :
  442. options.effect || defaultEffect;
  443. options = options || {};
  444. if ( typeof options === "number" ) {
  445. options = { duration: options };
  446. }
  447. hasOptions = !$.isEmptyObject( options );
  448. options.complete = callback;
  449. if ( options.delay ) {
  450. element.delay( options.delay );
  451. }
  452. if ( hasOptions && $.effects && ( $.effects.effect[ effectName ] || $.uiBackCompat !== false && $.effects[ effectName ] ) ) {
  453. element[ method ]( options );
  454. } else if ( effectName !== method && element[ effectName ] ) {
  455. element[ effectName ]( options.duration, options.easing, callback );
  456. } else {
  457. element.queue(function( next ) {
  458. $( this )[ method ]();
  459. if ( callback ) {
  460. callback.call( element[ 0 ] );
  461. }
  462. next();
  463. });
  464. }
  465. };
  466. });
  467. // DEPRECATED
  468. if ( $.uiBackCompat !== false ) {
  469. $.Widget.prototype._getCreateOptions = function() {
  470. return $.metadata && $.metadata.get( this.element[0] )[ this.widgetName ];
  471. };
  472. }
  473. }));