0
0

jquery.ui.widget.js 16 KB

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