jquery.ui.widget.js 22 KB

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