widget.js 15 KB

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