widget.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*!
  2. * jQuery UI Widget 1.12.1
  3. * http://jqueryui.com
  4. *
  5. * Copyright jQuery Foundation and other contributors
  6. * Released under the MIT license.
  7. * http://jquery.org/license
  8. */
  9. //>>label: Widget
  10. //>>group: Core
  11. //>>description: Provides a factory for creating stateful widgets with a common API.
  12. //>>docs: http://api.jqueryui.com/jQuery.widget/
  13. //>>demos: http://jqueryui.com/widget/
  14. ( function( factory ) {
  15. if ( typeof define === "function" && define.amd ) {
  16. // AMD. Register as an anonymous module.
  17. define( [ "jquery", "./version" ], factory );
  18. } else {
  19. // Browser globals
  20. factory( jQuery );
  21. }
  22. }( function( $ ) {
  23. var widgetUuid = 0;
  24. var widgetSlice = Array.prototype.slice;
  25. $.cleanData = ( function( orig ) {
  26. return function( elems ) {
  27. var events, elem, i;
  28. for ( i = 0; ( elem = elems[ i ] ) != null; i++ ) {
  29. try {
  30. // Only trigger remove when necessary to save time
  31. events = $._data( elem, "events" );
  32. if ( events && events.remove ) {
  33. $( elem ).triggerHandler( "remove" );
  34. }
  35. // Http://bugs.jquery.com/ticket/8235
  36. } catch ( e ) {}
  37. }
  38. orig( elems );
  39. };
  40. } )( $.cleanData );
  41. $.widget = function( name, base, prototype ) {
  42. var existingConstructor, constructor, basePrototype;
  43. // ProxiedPrototype allows the provided prototype to remain unmodified
  44. // so that it can be used as a mixin for multiple widgets (#8876)
  45. var proxiedPrototype = {};
  46. var namespace = name.split( "." )[ 0 ];
  47. name = name.split( "." )[ 1 ];
  48. var fullName = namespace + "-" + name;
  49. if ( !prototype ) {
  50. prototype = base;
  51. base = $.Widget;
  52. }
  53. if ( $.isArray( prototype ) ) {
  54. prototype = $.extend.apply( null, [ {} ].concat( prototype ) );
  55. }
  56. // Create selector for plugin
  57. $.expr[ ":" ][ fullName.toLowerCase() ] = function( elem ) {
  58. return !!$.data( elem, fullName );
  59. };
  60. $[ namespace ] = $[ namespace ] || {};
  61. existingConstructor = $[ namespace ][ name ];
  62. constructor = $[ namespace ][ name ] = function( options, element ) {
  63. // Allow instantiation without "new" keyword
  64. if ( !this._createWidget ) {
  65. return new constructor( options, element );
  66. }
  67. // Allow instantiation without initializing for simple inheritance
  68. // must use "new" keyword (the code above always passes args)
  69. if ( arguments.length ) {
  70. this._createWidget( options, element );
  71. }
  72. };
  73. // Extend with the existing constructor to carry over any static properties
  74. $.extend( constructor, existingConstructor, {
  75. version: prototype.version,
  76. // Copy the object used to create the prototype in case we need to
  77. // redefine the widget later
  78. _proto: $.extend( {}, prototype ),
  79. // Track widgets that inherit from this widget in case this widget is
  80. // redefined after a widget inherits from it
  81. _childConstructors: []
  82. } );
  83. basePrototype = new base();
  84. // We need to make the options hash a property directly on the new instance
  85. // otherwise we'll modify the options hash on the prototype that we're
  86. // inheriting from
  87. basePrototype.options = $.widget.extend( {}, basePrototype.options );
  88. $.each( prototype, function( prop, value ) {
  89. if ( !$.isFunction( value ) ) {
  90. proxiedPrototype[ prop ] = value;
  91. return;
  92. }
  93. proxiedPrototype[ prop ] = ( function() {
  94. function _super() {
  95. return base.prototype[ prop ].apply( this, arguments );
  96. }
  97. function _superApply( args ) {
  98. return base.prototype[ prop ].apply( this, args );
  99. }
  100. return function() {
  101. var __super = this._super;
  102. var __superApply = this._superApply;
  103. var returnValue;
  104. this._super = _super;
  105. this._superApply = _superApply;
  106. returnValue = value.apply( this, arguments );
  107. this._super = __super;
  108. this._superApply = __superApply;
  109. return returnValue;
  110. };
  111. } )();
  112. } );
  113. constructor.prototype = $.widget.extend( basePrototype, {
  114. // TODO: remove support for widgetEventPrefix
  115. // always use the name + a colon as the prefix, e.g., draggable:start
  116. // don't prefix for widgets that aren't DOM-based
  117. widgetEventPrefix: existingConstructor ? ( basePrototype.widgetEventPrefix || name ) : name
  118. }, proxiedPrototype, {
  119. constructor: constructor,
  120. namespace: namespace,
  121. widgetName: name,
  122. widgetFullName: fullName
  123. } );
  124. // If this widget is being redefined then we need to find all widgets that
  125. // are inheriting from it and redefine all of them so that they inherit from
  126. // the new version of this widget. We're essentially trying to replace one
  127. // level in the prototype chain.
  128. if ( existingConstructor ) {
  129. $.each( existingConstructor._childConstructors, function( i, child ) {
  130. var childPrototype = child.prototype;
  131. // Redefine the child widget using the same prototype that was
  132. // originally used, but inherit from the new version of the base
  133. $.widget( childPrototype.namespace + "." + childPrototype.widgetName, constructor,
  134. child._proto );
  135. } );
  136. // Remove the list of existing child constructors from the old constructor
  137. // so the old child constructors can be garbage collected
  138. delete existingConstructor._childConstructors;
  139. } else {
  140. base._childConstructors.push( constructor );
  141. }
  142. $.widget.bridge( name, constructor );
  143. return constructor;
  144. };
  145. $.widget.extend = function( target ) {
  146. var input = widgetSlice.call( arguments, 1 );
  147. var inputIndex = 0;
  148. var inputLength = input.length;
  149. var key;
  150. var value;
  151. for ( ; inputIndex < inputLength; inputIndex++ ) {
  152. for ( key in input[ inputIndex ] ) {
  153. value = input[ inputIndex ][ key ];
  154. if ( input[ inputIndex ].hasOwnProperty( key ) && value !== undefined ) {
  155. // Clone objects
  156. if ( $.isPlainObject( value ) ) {
  157. target[ key ] = $.isPlainObject( target[ key ] ) ?
  158. $.widget.extend( {}, target[ key ], value ) :
  159. // Don't extend strings, arrays, etc. with objects
  160. $.widget.extend( {}, value );
  161. // Copy everything else by reference
  162. } else {
  163. target[ key ] = value;
  164. }
  165. }
  166. }
  167. }
  168. return target;
  169. };
  170. $.widget.bridge = function( name, object ) {
  171. var fullName = object.prototype.widgetFullName || name;
  172. $.fn[ name ] = function( options ) {
  173. var isMethodCall = typeof options === "string";
  174. var args = widgetSlice.call( arguments, 1 );
  175. var returnValue = this;
  176. if ( isMethodCall ) {
  177. // If this is an empty collection, we need to have the instance method
  178. // return undefined instead of the jQuery instance
  179. if ( !this.length && options === "instance" ) {
  180. returnValue = undefined;
  181. } else {
  182. this.each( function() {
  183. var methodValue;
  184. var instance = $.data( this, fullName );
  185. if ( options === "instance" ) {
  186. returnValue = instance;
  187. return false;
  188. }
  189. if ( !instance ) {
  190. return $.error( "cannot call methods on " + name +
  191. " prior to initialization; " +
  192. "attempted to call method '" + options + "'" );
  193. }
  194. if ( !$.isFunction( instance[ options ] ) || options.charAt( 0 ) === "_" ) {
  195. return $.error( "no such method '" + options + "' for " + name +
  196. " widget instance" );
  197. }
  198. methodValue = instance[ options ].apply( instance, args );
  199. if ( methodValue !== instance && methodValue !== undefined ) {
  200. returnValue = methodValue && methodValue.jquery ?
  201. returnValue.pushStack( methodValue.get() ) :
  202. methodValue;
  203. return false;
  204. }
  205. } );
  206. }
  207. } else {
  208. // Allow multiple hashes to be passed on init
  209. if ( args.length ) {
  210. options = $.widget.extend.apply( null, [ options ].concat( args ) );
  211. }
  212. this.each( function() {
  213. var instance = $.data( this, fullName );
  214. if ( instance ) {
  215. instance.option( options || {} );
  216. if ( instance._init ) {
  217. instance._init();
  218. }
  219. } else {
  220. $.data( this, fullName, new object( options, this ) );
  221. }
  222. } );
  223. }
  224. return returnValue;
  225. };
  226. };
  227. $.Widget = function( /* options, element */ ) {};
  228. $.Widget._childConstructors = [];
  229. $.Widget.prototype = {
  230. widgetName: "widget",
  231. widgetEventPrefix: "",
  232. defaultElement: "<div>",
  233. options: {
  234. classes: {},
  235. disabled: false,
  236. // Callbacks
  237. create: null
  238. },
  239. _createWidget: function( options, element ) {
  240. element = $( element || this.defaultElement || this )[ 0 ];
  241. this.element = $( element );
  242. this.uuid = widgetUuid++;
  243. this.eventNamespace = "." + this.widgetName + this.uuid;
  244. this.bindings = $();
  245. this.hoverable = $();
  246. this.focusable = $();
  247. this.classesElementLookup = {};
  248. if ( element !== this ) {
  249. $.data( element, this.widgetFullName, this );
  250. this._on( true, this.element, {
  251. remove: function( event ) {
  252. if ( event.target === element ) {
  253. this.destroy();
  254. }
  255. }
  256. } );
  257. this.document = $( element.style ?
  258. // Element within the document
  259. element.ownerDocument :
  260. // Element is window or document
  261. element.document || element );
  262. this.window = $( this.document[ 0 ].defaultView || this.document[ 0 ].parentWindow );
  263. }
  264. this.options = $.widget.extend( {},
  265. this.options,
  266. this._getCreateOptions(),
  267. options );
  268. this._create();
  269. if ( this.options.disabled ) {
  270. this._setOptionDisabled( this.options.disabled );
  271. }
  272. this._trigger( "create", null, this._getCreateEventData() );
  273. this._init();
  274. },
  275. _getCreateOptions: function() {
  276. return {};
  277. },
  278. _getCreateEventData: $.noop,
  279. _create: $.noop,
  280. _init: $.noop,
  281. destroy: function() {
  282. var that = this;
  283. this._destroy();
  284. $.each( this.classesElementLookup, function( key, value ) {
  285. that._removeClass( value, key );
  286. } );
  287. // We can probably remove the unbind calls in 2.0
  288. // all event bindings should go through this._on()
  289. this.element
  290. .off( this.eventNamespace )
  291. .removeData( this.widgetFullName );
  292. this.widget()
  293. .off( this.eventNamespace )
  294. .removeAttr( "aria-disabled" );
  295. // Clean up events and states
  296. this.bindings.off( this.eventNamespace );
  297. },
  298. _destroy: $.noop,
  299. widget: function() {
  300. return this.element;
  301. },
  302. option: function( key, value ) {
  303. var options = key;
  304. var parts;
  305. var curOption;
  306. var i;
  307. if ( arguments.length === 0 ) {
  308. // Don't return a reference to the internal hash
  309. return $.widget.extend( {}, this.options );
  310. }
  311. if ( typeof key === "string" ) {
  312. // Handle nested keys, e.g., "foo.bar" => { foo: { bar: ___ } }
  313. options = {};
  314. parts = key.split( "." );
  315. key = parts.shift();
  316. if ( parts.length ) {
  317. curOption = options[ key ] = $.widget.extend( {}, this.options[ key ] );
  318. for ( i = 0; i < parts.length - 1; i++ ) {
  319. curOption[ parts[ i ] ] = curOption[ parts[ i ] ] || {};
  320. curOption = curOption[ parts[ i ] ];
  321. }
  322. key = parts.pop();
  323. if ( arguments.length === 1 ) {
  324. return curOption[ key ] === undefined ? null : curOption[ key ];
  325. }
  326. curOption[ key ] = value;
  327. } else {
  328. if ( arguments.length === 1 ) {
  329. return this.options[ key ] === undefined ? null : this.options[ key ];
  330. }
  331. options[ key ] = value;
  332. }
  333. }
  334. this._setOptions( options );
  335. return this;
  336. },
  337. _setOptions: function( options ) {
  338. var key;
  339. for ( key in options ) {
  340. this._setOption( key, options[ key ] );
  341. }
  342. return this;
  343. },
  344. _setOption: function( key, value ) {
  345. if ( key === "classes" ) {
  346. this._setOptionClasses( value );
  347. }
  348. this.options[ key ] = value;
  349. if ( key === "disabled" ) {
  350. this._setOptionDisabled( value );
  351. }
  352. return this;
  353. },
  354. _setOptionClasses: function( value ) {
  355. var classKey, elements, currentElements;
  356. for ( classKey in value ) {
  357. currentElements = this.classesElementLookup[ classKey ];
  358. if ( value[ classKey ] === this.options.classes[ classKey ] ||
  359. !currentElements ||
  360. !currentElements.length ) {
  361. continue;
  362. }
  363. // We are doing this to create a new jQuery object because the _removeClass() call
  364. // on the next line is going to destroy the reference to the current elements being
  365. // tracked. We need to save a copy of this collection so that we can add the new classes
  366. // below.
  367. elements = $( currentElements.get() );
  368. this._removeClass( currentElements, classKey );
  369. // We don't use _addClass() here, because that uses this.options.classes
  370. // for generating the string of classes. We want to use the value passed in from
  371. // _setOption(), this is the new value of the classes option which was passed to
  372. // _setOption(). We pass this value directly to _classes().
  373. elements.addClass( this._classes( {
  374. element: elements,
  375. keys: classKey,
  376. classes: value,
  377. add: true
  378. } ) );
  379. }
  380. },
  381. _setOptionDisabled: function( value ) {
  382. this._toggleClass( this.widget(), this.widgetFullName + "-disabled", null, !!value );
  383. // If the widget is becoming disabled, then nothing is interactive
  384. if ( value ) {
  385. this._removeClass( this.hoverable, null, "ui-state-hover" );
  386. this._removeClass( this.focusable, null, "ui-state-focus" );
  387. }
  388. },
  389. enable: function() {
  390. return this._setOptions( { disabled: false } );
  391. },
  392. disable: function() {
  393. return this._setOptions( { disabled: true } );
  394. },
  395. _classes: function( options ) {
  396. var full = [];
  397. var that = this;
  398. options = $.extend( {
  399. element: this.element,
  400. classes: this.options.classes || {}
  401. }, options );
  402. function processClassString( classes, checkOption ) {
  403. var current, i;
  404. for ( i = 0; i < classes.length; i++ ) {
  405. current = that.classesElementLookup[ classes[ i ] ] || $();
  406. if ( options.add ) {
  407. current = $( $.unique( current.get().concat( options.element.get() ) ) );
  408. } else {
  409. current = $( current.not( options.element ).get() );
  410. }
  411. that.classesElementLookup[ classes[ i ] ] = current;
  412. full.push( classes[ i ] );
  413. if ( checkOption && options.classes[ classes[ i ] ] ) {
  414. full.push( options.classes[ classes[ i ] ] );
  415. }
  416. }
  417. }
  418. this._on( options.element, {
  419. "remove": "_untrackClassesElement"
  420. } );
  421. if ( options.keys ) {
  422. processClassString( options.keys.match( /\S+/g ) || [], true );
  423. }
  424. if ( options.extra ) {
  425. processClassString( options.extra.match( /\S+/g ) || [] );
  426. }
  427. return full.join( " " );
  428. },
  429. _untrackClassesElement: function( event ) {
  430. var that = this;
  431. $.each( that.classesElementLookup, function( key, value ) {
  432. if ( $.inArray( event.target, value ) !== -1 ) {
  433. that.classesElementLookup[ key ] = $( value.not( event.target ).get() );
  434. }
  435. } );
  436. },
  437. _removeClass: function( element, keys, extra ) {
  438. return this._toggleClass( element, keys, extra, false );
  439. },
  440. _addClass: function( element, keys, extra ) {
  441. return this._toggleClass( element, keys, extra, true );
  442. },
  443. _toggleClass: function( element, keys, extra, add ) {
  444. add = ( typeof add === "boolean" ) ? add : extra;
  445. var shift = ( typeof element === "string" || element === null ),
  446. options = {
  447. extra: shift ? keys : extra,
  448. keys: shift ? element : keys,
  449. element: shift ? this.element : element,
  450. add: add
  451. };
  452. options.element.toggleClass( this._classes( options ), add );
  453. return this;
  454. },
  455. _on: function( suppressDisabledCheck, element, handlers ) {
  456. var delegateElement;
  457. var instance = this;
  458. // No suppressDisabledCheck flag, shuffle arguments
  459. if ( typeof suppressDisabledCheck !== "boolean" ) {
  460. handlers = element;
  461. element = suppressDisabledCheck;
  462. suppressDisabledCheck = false;
  463. }
  464. // No element argument, shuffle and use this.element
  465. if ( !handlers ) {
  466. handlers = element;
  467. element = this.element;
  468. delegateElement = this.widget();
  469. } else {
  470. element = delegateElement = $( element );
  471. this.bindings = this.bindings.add( element );
  472. }
  473. $.each( handlers, function( event, handler ) {
  474. function handlerProxy() {
  475. // Allow widgets to customize the disabled handling
  476. // - disabled as an array instead of boolean
  477. // - disabled class as method for disabling individual parts
  478. if ( !suppressDisabledCheck &&
  479. ( instance.options.disabled === true ||
  480. $( this ).hasClass( "ui-state-disabled" ) ) ) {
  481. return;
  482. }
  483. return ( typeof handler === "string" ? instance[ handler ] : handler )
  484. .apply( instance, arguments );
  485. }
  486. // Copy the guid so direct unbinding works
  487. if ( typeof handler !== "string" ) {
  488. handlerProxy.guid = handler.guid =
  489. handler.guid || handlerProxy.guid || $.guid++;
  490. }
  491. var match = event.match( /^([\w:-]*)\s*(.*)$/ );
  492. var eventName = match[ 1 ] + instance.eventNamespace;
  493. var selector = match[ 2 ];
  494. if ( selector ) {
  495. delegateElement.on( eventName, selector, handlerProxy );
  496. } else {
  497. element.on( eventName, handlerProxy );
  498. }
  499. } );
  500. },
  501. _off: function( element, eventName ) {
  502. eventName = ( eventName || "" ).split( " " ).join( this.eventNamespace + " " ) +
  503. this.eventNamespace;
  504. element.off( eventName ).off( eventName );
  505. // Clear the stack to avoid memory leaks (#10056)
  506. this.bindings = $( this.bindings.not( element ).get() );
  507. this.focusable = $( this.focusable.not( element ).get() );
  508. this.hoverable = $( this.hoverable.not( element ).get() );
  509. },
  510. _delay: function( handler, delay ) {
  511. function handlerProxy() {
  512. return ( typeof handler === "string" ? instance[ handler ] : handler )
  513. .apply( instance, arguments );
  514. }
  515. var instance = this;
  516. return setTimeout( handlerProxy, delay || 0 );
  517. },
  518. _hoverable: function( element ) {
  519. this.hoverable = this.hoverable.add( element );
  520. this._on( element, {
  521. mouseenter: function( event ) {
  522. this._addClass( $( event.currentTarget ), null, "ui-state-hover" );
  523. },
  524. mouseleave: function( event ) {
  525. this._removeClass( $( event.currentTarget ), null, "ui-state-hover" );
  526. }
  527. } );
  528. },
  529. _focusable: function( element ) {
  530. this.focusable = this.focusable.add( element );
  531. this._on( element, {
  532. focusin: function( event ) {
  533. this._addClass( $( event.currentTarget ), null, "ui-state-focus" );
  534. },
  535. focusout: function( event ) {
  536. this._removeClass( $( event.currentTarget ), null, "ui-state-focus" );
  537. }
  538. } );
  539. },
  540. _trigger: function( type, event, data ) {
  541. var prop, orig;
  542. var callback = this.options[ type ];
  543. data = data || {};
  544. event = $.Event( event );
  545. event.type = ( type === this.widgetEventPrefix ?
  546. type :
  547. this.widgetEventPrefix + type ).toLowerCase();
  548. // The original event may come from any element
  549. // so we need to reset the target on the new event
  550. event.target = this.element[ 0 ];
  551. // Copy original event properties over to the new event
  552. orig = event.originalEvent;
  553. if ( orig ) {
  554. for ( prop in orig ) {
  555. if ( !( prop in event ) ) {
  556. event[ prop ] = orig[ prop ];
  557. }
  558. }
  559. }
  560. this.element.trigger( event, data );
  561. return !( $.isFunction( callback ) &&
  562. callback.apply( this.element[ 0 ], [ event ].concat( data ) ) === false ||
  563. event.isDefaultPrevented() );
  564. }
  565. };
  566. $.each( { show: "fadeIn", hide: "fadeOut" }, function( method, defaultEffect ) {
  567. $.Widget.prototype[ "_" + method ] = function( element, options, callback ) {
  568. if ( typeof options === "string" ) {
  569. options = { effect: options };
  570. }
  571. var hasOptions;
  572. var effectName = !options ?
  573. method :
  574. options === true || typeof options === "number" ?
  575. defaultEffect :
  576. options.effect || defaultEffect;
  577. options = options || {};
  578. if ( typeof options === "number" ) {
  579. options = { duration: options };
  580. }
  581. hasOptions = !$.isEmptyObject( options );
  582. options.complete = callback;
  583. if ( options.delay ) {
  584. element.delay( options.delay );
  585. }
  586. if ( hasOptions && $.effects && $.effects.effect[ effectName ] ) {
  587. element[ method ]( options );
  588. } else if ( effectName !== method && element[ effectName ] ) {
  589. element[ effectName ]( options.duration, options.easing, callback );
  590. } else {
  591. element.queue( function( next ) {
  592. $( this )[ method ]();
  593. if ( callback ) {
  594. callback.call( element[ 0 ] );
  595. }
  596. next();
  597. } );
  598. }
  599. };
  600. } );
  601. return $.widget;
  602. } ) );