jquery.ui.widget.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * jQuery UI Widget 1.10.3+amd
  3. * https://github.com/blueimp/jQuery-File-Upload
  4. *
  5. * Copyright 2013 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. // proxiedPrototype allows the provided prototype to remain unmodified
  35. // so that it can be used as a mixin for multiple widgets (#8876)
  36. proxiedPrototype = {},
  37. namespace = name.split( "." )[ 0 ];
  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.toLowerCase() ] = function( elem ) {
  46. return !!$.data( elem, fullName );
  47. };
  48. $[ namespace ] = $[ namespace ] || {};
  49. existingConstructor = $[ namespace ][ name ];
  50. constructor = $[ namespace ][ name ] = function( options, element ) {
  51. // allow instantiation without "new" keyword
  52. if ( !this._createWidget ) {
  53. return new constructor( options, element );
  54. }
  55. // allow instantiation without initializing for simple inheritance
  56. // must use "new" keyword (the code above always passes args)
  57. if ( arguments.length ) {
  58. this._createWidget( options, element );
  59. }
  60. };
  61. // extend with the existing constructor to carry over any static properties
  62. $.extend( constructor, existingConstructor, {
  63. version: prototype.version,
  64. // copy the object used to create the prototype in case we need to
  65. // redefine the widget later
  66. _proto: $.extend( {}, prototype ),
  67. // track widgets that inherit from this widget in case this widget is
  68. // redefined after a widget inherits from it
  69. _childConstructors: []
  70. });
  71. basePrototype = new base();
  72. // we need to make the options hash a property directly on the new instance
  73. // otherwise we'll modify the options hash on the prototype that we're
  74. // inheriting from
  75. basePrototype.options = $.widget.extend( {}, basePrototype.options );
  76. $.each( prototype, function( prop, value ) {
  77. if ( !$.isFunction( value ) ) {
  78. proxiedPrototype[ prop ] = value;
  79. return;
  80. }
  81. proxiedPrototype[ prop ] = (function() {
  82. var _super = function() {
  83. return base.prototype[ prop ].apply( this, arguments );
  84. },
  85. _superApply = function( args ) {
  86. return base.prototype[ prop ].apply( this, args );
  87. };
  88. return function() {
  89. var __super = this._super,
  90. __superApply = this._superApply,
  91. returnValue;
  92. this._super = _super;
  93. this._superApply = _superApply;
  94. returnValue = value.apply( this, arguments );
  95. this._super = __super;
  96. this._superApply = __superApply;
  97. return returnValue;
  98. };
  99. })();
  100. });
  101. constructor.prototype = $.widget.extend( basePrototype, {
  102. // TODO: remove support for widgetEventPrefix
  103. // always use the name + a colon as the prefix, e.g., draggable:start
  104. // don't prefix for widgets that aren't DOM-based
  105. widgetEventPrefix: existingConstructor ? basePrototype.widgetEventPrefix : name
  106. }, proxiedPrototype, {
  107. constructor: constructor,
  108. namespace: namespace,
  109. widgetName: name,
  110. widgetFullName: fullName
  111. });
  112. // If this widget is being redefined then we need to find all widgets that
  113. // are inheriting from it and redefine all of them so that they inherit from
  114. // the new version of this widget. We're essentially trying to replace one
  115. // level in the prototype chain.
  116. if ( existingConstructor ) {
  117. $.each( existingConstructor._childConstructors, function( i, child ) {
  118. var childPrototype = child.prototype;
  119. // redefine the child widget using the same prototype that was
  120. // originally used, but inherit from the new version of the base
  121. $.widget( childPrototype.namespace + "." + childPrototype.widgetName, constructor, child._proto );
  122. });
  123. // remove the list of existing child constructors from the old constructor
  124. // so the old child constructors can be garbage collected
  125. delete existingConstructor._childConstructors;
  126. } else {
  127. base._childConstructors.push( constructor );
  128. }
  129. $.widget.bridge( name, constructor );
  130. };
  131. $.widget.extend = function( target ) {
  132. var input = slice.call( arguments, 1 ),
  133. inputIndex = 0,
  134. inputLength = input.length,
  135. key,
  136. value;
  137. for ( ; inputIndex < inputLength; inputIndex++ ) {
  138. for ( key in input[ inputIndex ] ) {
  139. value = input[ inputIndex ][ key ];
  140. if ( input[ inputIndex ].hasOwnProperty( key ) && value !== undefined ) {
  141. // Clone objects
  142. if ( $.isPlainObject( value ) ) {
  143. target[ key ] = $.isPlainObject( target[ key ] ) ?
  144. $.widget.extend( {}, target[ key ], value ) :
  145. // Don't extend strings, arrays, etc. with objects
  146. $.widget.extend( {}, value );
  147. // Copy everything else by reference
  148. } else {
  149. target[ key ] = value;
  150. }
  151. }
  152. }
  153. }
  154. return target;
  155. };
  156. $.widget.bridge = function( name, object ) {
  157. var fullName = object.prototype.widgetFullName || name;
  158. $.fn[ name ] = function( options ) {
  159. var isMethodCall = typeof options === "string",
  160. args = slice.call( arguments, 1 ),
  161. returnValue = this;
  162. // allow multiple hashes to be passed on init
  163. options = !isMethodCall && args.length ?
  164. $.widget.extend.apply( null, [ options ].concat(args) ) :
  165. options;
  166. if ( isMethodCall ) {
  167. this.each(function() {
  168. var methodValue,
  169. instance = $.data( this, fullName );
  170. if ( !instance ) {
  171. return $.error( "cannot call methods on " + name + " prior to initialization; " +
  172. "attempted to call method '" + options + "'" );
  173. }
  174. if ( !$.isFunction( instance[options] ) || options.charAt( 0 ) === "_" ) {
  175. return $.error( "no such method '" + options + "' for " + name + " widget instance" );
  176. }
  177. methodValue = instance[ options ].apply( instance, args );
  178. if ( methodValue !== instance && methodValue !== undefined ) {
  179. returnValue = methodValue && methodValue.jquery ?
  180. returnValue.pushStack( methodValue.get() ) :
  181. methodValue;
  182. return false;
  183. }
  184. });
  185. } else {
  186. this.each(function() {
  187. var instance = $.data( this, fullName );
  188. if ( instance ) {
  189. instance.option( options || {} )._init();
  190. } else {
  191. $.data( this, fullName, new object( options, this ) );
  192. }
  193. });
  194. }
  195. return returnValue;
  196. };
  197. };
  198. $.Widget = function( /* options, element */ ) {};
  199. $.Widget._childConstructors = [];
  200. $.Widget.prototype = {
  201. widgetName: "widget",
  202. widgetEventPrefix: "",
  203. defaultElement: "<div>",
  204. options: {
  205. disabled: false,
  206. // callbacks
  207. create: null
  208. },
  209. _createWidget: function( options, element ) {
  210. element = $( element || this.defaultElement || this )[ 0 ];
  211. this.element = $( element );
  212. this.uuid = uuid++;
  213. this.eventNamespace = "." + this.widgetName + this.uuid;
  214. this.options = $.widget.extend( {},
  215. this.options,
  216. this._getCreateOptions(),
  217. options );
  218. this.bindings = $();
  219. this.hoverable = $();
  220. this.focusable = $();
  221. if ( element !== this ) {
  222. $.data( element, this.widgetFullName, this );
  223. this._on( true, 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( suppressDisabledCheck, element, handlers ) {
  333. var delegateElement,
  334. instance = this;
  335. // no suppressDisabledCheck flag, shuffle arguments
  336. if ( typeof suppressDisabledCheck !== "boolean" ) {
  337. handlers = element;
  338. element = suppressDisabledCheck;
  339. suppressDisabledCheck = false;
  340. }
  341. // no element argument, shuffle and use this.element
  342. if ( !handlers ) {
  343. handlers = element;
  344. element = this.element;
  345. delegateElement = this.widget();
  346. } else {
  347. // accept selectors, DOM elements
  348. element = delegateElement = $( element );
  349. this.bindings = this.bindings.add( element );
  350. }
  351. $.each( handlers, function( event, handler ) {
  352. function handlerProxy() {
  353. // allow widgets to customize the disabled handling
  354. // - disabled as an array instead of boolean
  355. // - disabled class as method for disabling individual parts
  356. if ( !suppressDisabledCheck &&
  357. ( instance.options.disabled === true ||
  358. $( this ).hasClass( "ui-state-disabled" ) ) ) {
  359. return;
  360. }
  361. return ( typeof handler === "string" ? instance[ handler ] : handler )
  362. .apply( instance, arguments );
  363. }
  364. // copy the guid so direct unbinding works
  365. if ( typeof handler !== "string" ) {
  366. handlerProxy.guid = handler.guid =
  367. handler.guid || handlerProxy.guid || $.guid++;
  368. }
  369. var match = event.match( /^(\w+)\s*(.*)$/ ),
  370. eventName = match[1] + instance.eventNamespace,
  371. selector = match[2];
  372. if ( selector ) {
  373. delegateElement.delegate( selector, eventName, handlerProxy );
  374. } else {
  375. element.bind( eventName, handlerProxy );
  376. }
  377. });
  378. },
  379. _off: function( element, eventName ) {
  380. eventName = (eventName || "").split( " " ).join( this.eventNamespace + " " ) + this.eventNamespace;
  381. element.unbind( eventName ).undelegate( eventName );
  382. },
  383. _delay: function( handler, delay ) {
  384. function handlerProxy() {
  385. return ( typeof handler === "string" ? instance[ handler ] : handler )
  386. .apply( instance, arguments );
  387. }
  388. var instance = this;
  389. return setTimeout( handlerProxy, delay || 0 );
  390. },
  391. _hoverable: function( element ) {
  392. this.hoverable = this.hoverable.add( element );
  393. this._on( element, {
  394. mouseenter: function( event ) {
  395. $( event.currentTarget ).addClass( "ui-state-hover" );
  396. },
  397. mouseleave: function( event ) {
  398. $( event.currentTarget ).removeClass( "ui-state-hover" );
  399. }
  400. });
  401. },
  402. _focusable: function( element ) {
  403. this.focusable = this.focusable.add( element );
  404. this._on( element, {
  405. focusin: function( event ) {
  406. $( event.currentTarget ).addClass( "ui-state-focus" );
  407. },
  408. focusout: function( event ) {
  409. $( event.currentTarget ).removeClass( "ui-state-focus" );
  410. }
  411. });
  412. },
  413. _trigger: function( type, event, data ) {
  414. var prop, orig,
  415. callback = this.options[ type ];
  416. data = data || {};
  417. event = $.Event( event );
  418. event.type = ( type === this.widgetEventPrefix ?
  419. type :
  420. this.widgetEventPrefix + type ).toLowerCase();
  421. // the original event may come from any element
  422. // so we need to reset the target on the new event
  423. event.target = this.element[ 0 ];
  424. // copy original event properties over to the new event
  425. orig = event.originalEvent;
  426. if ( orig ) {
  427. for ( prop in orig ) {
  428. if ( !( prop in event ) ) {
  429. event[ prop ] = orig[ prop ];
  430. }
  431. }
  432. }
  433. this.element.trigger( event, data );
  434. return !( $.isFunction( callback ) &&
  435. callback.apply( this.element[0], [ event ].concat( data ) ) === false ||
  436. event.isDefaultPrevented() );
  437. }
  438. };
  439. $.each( { show: "fadeIn", hide: "fadeOut" }, function( method, defaultEffect ) {
  440. $.Widget.prototype[ "_" + method ] = function( element, options, callback ) {
  441. if ( typeof options === "string" ) {
  442. options = { effect: options };
  443. }
  444. var hasOptions,
  445. effectName = !options ?
  446. method :
  447. options === true || typeof options === "number" ?
  448. defaultEffect :
  449. options.effect || defaultEffect;
  450. options = options || {};
  451. if ( typeof options === "number" ) {
  452. options = { duration: options };
  453. }
  454. hasOptions = !$.isEmptyObject( options );
  455. options.complete = callback;
  456. if ( options.delay ) {
  457. element.delay( options.delay );
  458. }
  459. if ( hasOptions && $.effects && $.effects.effect[ effectName ] ) {
  460. element[ method ]( options );
  461. } else if ( effectName !== method && element[ effectName ] ) {
  462. element[ effectName ]( options.duration, options.easing, callback );
  463. } else {
  464. element.queue(function( next ) {
  465. $( this )[ method ]();
  466. if ( callback ) {
  467. callback.call( element[ 0 ] );
  468. }
  469. next();
  470. });
  471. }
  472. };
  473. });
  474. }));