PageRenderTime 133ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 1ms

/ext-4.1.0_b3/docs/extjs/examples/kitchensink/all-classes.js

https://bitbucket.org/srogerf/javascript
JavaScript | 14401 lines | 9947 code | 835 blank | 3619 comment | 856 complexity | 1a8790657b1626f7a21dd493d975bf3f MD5 | raw file
  1. /*
  2. Copyright(c) 2011 Sencha
  3. */
  4. /**
  5. * Base class that provides a common interface for publishing events. Subclasses are expected to to have a property
  6. * "events" with all the events defined, and, optionally, a property "listeners" with configured listeners defined.
  7. *
  8. * For example:
  9. *
  10. * Ext.define('Employee', {
  11. * mixins: {
  12. * observable: 'Ext.util.Observable'
  13. * },
  14. *
  15. * constructor: function (config) {
  16. * // The Observable constructor copies all of the properties of `config` on
  17. * // to `this` using {@link Ext#apply}. Further, the `listeners` property is
  18. * // processed to add listeners.
  19. * //
  20. * this.mixins.observable.constructor.call(this, config);
  21. *
  22. * this.addEvents(
  23. * 'fired',
  24. * 'quit'
  25. * );
  26. * }
  27. * });
  28. *
  29. * This could then be used like this:
  30. *
  31. * var newEmployee = new Employee({
  32. * name: employeeName,
  33. * listeners: {
  34. * quit: function() {
  35. * // By default, "this" will be the object that fired the event.
  36. * alert(this.name + " has quit!");
  37. * }
  38. * }
  39. * });
  40. */
  41. Ext.define('Ext.util.Observable', {
  42. /* Begin Definitions */
  43. requires: ['Ext.util.Event'],
  44. statics: {
  45. /**
  46. * Removes **all** added captures from the Observable.
  47. *
  48. * @param {Ext.util.Observable} o The Observable to release
  49. * @static
  50. */
  51. releaseCapture: function(o) {
  52. o.fireEvent = this.prototype.fireEvent;
  53. },
  54. /**
  55. * Starts capture on the specified Observable. All events will be passed to the supplied function with the event
  56. * name + standard signature of the event **before** the event is fired. If the supplied function returns false,
  57. * the event will not fire.
  58. *
  59. * @param {Ext.util.Observable} o The Observable to capture events from.
  60. * @param {Function} fn The function to call when an event is fired.
  61. * @param {Object} scope (optional) The scope (`this` reference) in which the function is executed. Defaults to
  62. * the Observable firing the event.
  63. * @static
  64. */
  65. capture: function(o, fn, scope) {
  66. o.fireEvent = Ext.Function.createInterceptor(o.fireEvent, fn, scope);
  67. },
  68. /**
  69. * Sets observability on the passed class constructor.
  70. *
  71. * This makes any event fired on any instance of the passed class also fire a single event through
  72. * the **class** allowing for central handling of events on many instances at once.
  73. *
  74. * Usage:
  75. *
  76. * Ext.util.Observable.observe(Ext.data.Connection);
  77. * Ext.data.Connection.on('beforerequest', function(con, options) {
  78. * console.log('Ajax request made to ' + options.url);
  79. * });
  80. *
  81. * @param {Function} c The class constructor to make observable.
  82. * @param {Object} listeners An object containing a series of listeners to add. See {@link #addListener}.
  83. * @static
  84. */
  85. observe: function(cls, listeners) {
  86. if (cls) {
  87. if (!cls.isObservable) {
  88. Ext.applyIf(cls, new this());
  89. this.capture(cls.prototype, cls.fireEvent, cls);
  90. }
  91. if (Ext.isObject(listeners)) {
  92. cls.on(listeners);
  93. }
  94. return cls;
  95. }
  96. }
  97. },
  98. /* End Definitions */
  99. /**
  100. * @cfg {Object} listeners
  101. *
  102. * A config object containing one or more event handlers to be added to this object during initialization. This
  103. * should be a valid listeners config object as specified in the {@link #addListener} example for attaching multiple
  104. * handlers at once.
  105. *
  106. * **DOM events from Ext JS {@link Ext.Component Components}**
  107. *
  108. * While _some_ Ext JS Component classes export selected DOM events (e.g. "click", "mouseover" etc), this is usually
  109. * only done when extra value can be added. For example the {@link Ext.view.View DataView}'s **`{@link
  110. * Ext.view.View#itemclick itemclick}`** event passing the node clicked on. To access DOM events directly from a
  111. * child element of a Component, we need to specify the `element` option to identify the Component property to add a
  112. * DOM listener to:
  113. *
  114. * new Ext.panel.Panel({
  115. * width: 400,
  116. * height: 200,
  117. * dockedItems: [{
  118. * xtype: 'toolbar'
  119. * }],
  120. * listeners: {
  121. * click: {
  122. * element: 'el', //bind to the underlying el property on the panel
  123. * fn: function(){ console.log('click el'); }
  124. * },
  125. * dblclick: {
  126. * element: 'body', //bind to the underlying body property on the panel
  127. * fn: function(){ console.log('dblclick body'); }
  128. * }
  129. * }
  130. * });
  131. */
  132. /**
  133. * @property {Boolean} isObservable
  134. * `true` in this class to identify an objact as an instantiated Observable, or subclass thereof.
  135. */
  136. isObservable: true,
  137. constructor: function(config) {
  138. var me = this;
  139. Ext.apply(me, config);
  140. // Hash of event "hasListeners" flags.
  141. // For repeated events in time-critical code, the firing code should use
  142. // if (!me.hasListeners.beforerender || me.fireEvent('beforerender', me) !== false) { //code... }
  143. // Bubbling the events counts as one listener.
  144. // The subclass may have already initialized it.
  145. me.hasListeners = me.hasListeners || {};
  146. me.events = me.events || {};
  147. if (me.listeners) {
  148. me.on(me.listeners);
  149. me.listeners = null; //Set as an instance property to pre-empt the prototype in case any are set there.
  150. }
  151. if (me.bubbleEvents) {
  152. me.enableBubble(me.bubbleEvents);
  153. }
  154. },
  155. // @private
  156. eventOptionsRe : /^(?:scope|delay|buffer|single|stopEvent|preventDefault|stopPropagation|normalized|args|delegate|element|vertical|horizontal|freezeEvent)$/,
  157. /**
  158. * Adds listeners to any Observable object (or Ext.Element) which are automatically removed when this Component is
  159. * destroyed.
  160. *
  161. * @param {Ext.util.Observable/Ext.Element} item The item to which to add a listener/listeners.
  162. * @param {Object/String} ename The event name, or an object containing event name properties.
  163. * @param {Function} fn (optional) If the `ename` parameter was an event name, this is the handler function.
  164. * @param {Object} scope (optional) If the `ename` parameter was an event name, this is the scope (`this` reference)
  165. * in which the handler function is executed.
  166. * @param {Object} opt (optional) If the `ename` parameter was an event name, this is the
  167. * {@link Ext.util.Observable#addListener addListener} options.
  168. */
  169. addManagedListener : function(item, ename, fn, scope, options) {
  170. var me = this,
  171. managedListeners = me.managedListeners = me.managedListeners || [],
  172. config;
  173. if (typeof ename !== 'string') {
  174. options = ename;
  175. for (ename in options) {
  176. if (options.hasOwnProperty(ename)) {
  177. config = options[ename];
  178. if (!me.eventOptionsRe.test(ename)) {
  179. me.addManagedListener(item, ename, config.fn || config, config.scope || options.scope, config.fn ? config : options);
  180. }
  181. }
  182. }
  183. }
  184. else {
  185. managedListeners.push({
  186. item: item,
  187. ename: ename,
  188. fn: fn,
  189. scope: scope,
  190. options: options
  191. });
  192. item.on(ename, fn, scope, options);
  193. }
  194. },
  195. /**
  196. * Removes listeners that were added by the {@link #mon} method.
  197. *
  198. * @param {Ext.util.Observable/Ext.Element} item The item from which to remove a listener/listeners.
  199. * @param {Object/String} ename The event name, or an object containing event name properties.
  200. * @param {Function} fn (optional) If the `ename` parameter was an event name, this is the handler function.
  201. * @param {Object} scope (optional) If the `ename` parameter was an event name, this is the scope (`this` reference)
  202. * in which the handler function is executed.
  203. */
  204. removeManagedListener : function(item, ename, fn, scope) {
  205. var me = this,
  206. options,
  207. config,
  208. managedListeners,
  209. length,
  210. i;
  211. if (typeof ename !== 'string') {
  212. options = ename;
  213. for (ename in options) {
  214. if (options.hasOwnProperty(ename)) {
  215. config = options[ename];
  216. if (!me.eventOptionsRe.test(ename)) {
  217. me.removeManagedListener(item, ename, config.fn || config, config.scope || options.scope);
  218. }
  219. }
  220. }
  221. }
  222. managedListeners = me.managedListeners ? me.managedListeners.slice() : [];
  223. for (i = 0, length = managedListeners.length; i < length; i++) {
  224. me.removeManagedListenerItem(false, managedListeners[i], item, ename, fn, scope);
  225. }
  226. },
  227. /**
  228. * Fires the specified event with the passed parameters (minus the event name, plus the `options` object passed
  229. * to {@link #addListener}).
  230. *
  231. * An event may be set to bubble up an Observable parent hierarchy (See {@link Ext.Component#getBubbleTarget}) by
  232. * calling {@link #enableBubble}.
  233. *
  234. * @param {String} eventName The name of the event to fire.
  235. * @param {Object...} args Variable number of parameters are passed to handlers.
  236. * @return {Boolean} returns false if any of the handlers return false otherwise it returns true.
  237. */
  238. fireEvent: function(eventName) {
  239. eventName = eventName.toLowerCase();
  240. var me = this,
  241. events = me.events,
  242. event = events && events[eventName];
  243. // Only continue firing the event if there are listeners to be informed.
  244. // Bubbled events will always have a listener count, so will be fired.
  245. if (event && me.hasListeners[eventName]) {
  246. return me.continueFireEvent(eventName, Ext.Array.slice(arguments, 1), event.bubble);
  247. }
  248. },
  249. /**
  250. * Continue to fire event.
  251. * @private
  252. *
  253. * @param {String} eventName
  254. * @param {Array} args
  255. * @param {Boolean} bubbles
  256. */
  257. continueFireEvent: function(eventName, args, bubbles) {
  258. var target = this,
  259. queue, event,
  260. ret = true;
  261. do {
  262. if (target.eventsSuspended === true) {
  263. if ((queue = target.eventQueue)) {
  264. queue.push([eventName, args, bubbles]);
  265. }
  266. return ret;
  267. } else {
  268. event = target.events[eventName];
  269. // Continue bubbling if event exists and it is `true` or the handler didn't returns false and it
  270. // configure to bubble.
  271. if (event && event != true) {
  272. if ((ret = event.fire.apply(event, args)) === false) {
  273. break;
  274. }
  275. }
  276. }
  277. } while (bubbles && (target = target.getBubbleParent()));
  278. return ret;
  279. },
  280. /**
  281. * Gets the bubbling parent for an Observable
  282. * @private
  283. * @return {Ext.util.Observable} The bubble parent. null is returned if no bubble target exists
  284. */
  285. getBubbleParent: function(){
  286. var me = this, parent = me.getBubbleTarget && me.getBubbleTarget();
  287. if (parent && parent.isObservable) {
  288. return parent;
  289. }
  290. return null;
  291. },
  292. /**
  293. * Appends an event handler to this object. For example:
  294. *
  295. * myGridPanel.on("mouseover", this.onMouseOver, this);
  296. *
  297. * The method also allows for a single argument to be passed which is a config object
  298. * containing properties which specify multiple events. For example:
  299. *
  300. * myGridPanel.on({
  301. * cellClick: this.onCellClick,
  302. * mouseover: this.onMouseOver,
  303. * mouseout: this.onMouseOut,
  304. * scope: this // Important. Ensure "this" is correct during handler execution
  305. * });
  306. *
  307. * One can also specify options for each event handler separately:
  308. *
  309. * myGridPanel.on({
  310. * cellClick: {fn: this.onCellClick, scope: this, single: true},
  311. * mouseover: {fn: panel.onMouseOver, scope: panel}
  312. * });
  313. *
  314. * *Names* of methods in a specified scope may also be used. Note that
  315. * `scope` MUST be specified to use this option:
  316. *
  317. * myGridPanel.on({
  318. * cellClick: {fn: 'onCellClick', scope: this, single: true},
  319. * mouseover: {fn: 'onMouseOver', scope: panel}
  320. * });
  321. *
  322. * @param {String/Object} eventName The name of the event to listen for.
  323. * May also be an object who's property names are event names.
  324. *
  325. * @param {Function} [fn] The method the event invokes, or *if `scope` is specified, the *name* of the method within
  326. * the specified `scope`. Will be called with arguments
  327. * given to {@link #fireEvent} plus the `options` parameter described below.
  328. *
  329. * @param {Object} [scope] The scope (`this` reference) in which the handler function is
  330. * executed. **If omitted, defaults to the object which fired the event.**
  331. *
  332. * @param {Object} [options] An object containing handler configuration.
  333. *
  334. * **Note:** Unlike in ExtJS 3.x, the options object will also be passed as the last
  335. * argument to every event handler.
  336. *
  337. * This object may contain any of the following properties:
  338. *
  339. * @param {Object} options.scope
  340. * The scope (`this` reference) in which the handler function is executed. **If omitted,
  341. * defaults to the object which fired the event.**
  342. *
  343. * @param {Number} options.delay
  344. * The number of milliseconds to delay the invocation of the handler after the event fires.
  345. *
  346. * @param {Boolean} options.single
  347. * True to add a handler to handle just the next firing of the event, and then remove itself.
  348. *
  349. * @param {Number} options.buffer
  350. * Causes the handler to be scheduled to run in an {@link Ext.util.DelayedTask} delayed
  351. * by the specified number of milliseconds. If the event fires again within that time,
  352. * the original handler is _not_ invoked, but the new handler is scheduled in its place.
  353. *
  354. * @param {Ext.util.Observable} options.target
  355. * Only call the handler if the event was fired on the target Observable, _not_ if the event
  356. * was bubbled up from a child Observable.
  357. *
  358. * @param {String} options.element
  359. * **This option is only valid for listeners bound to {@link Ext.Component Components}.**
  360. * The name of a Component property which references an element to add a listener to.
  361. *
  362. * This option is useful during Component construction to add DOM event listeners to elements of
  363. * {@link Ext.Component Components} which will exist only after the Component is rendered.
  364. * For example, to add a click listener to a Panel's body:
  365. *
  366. * new Ext.panel.Panel({
  367. * title: 'The title',
  368. * listeners: {
  369. * click: this.handlePanelClick,
  370. * element: 'body'
  371. * }
  372. * });
  373. *
  374. * **Combining Options**
  375. *
  376. * Using the options argument, it is possible to combine different types of listeners:
  377. *
  378. * A delayed, one-time listener.
  379. *
  380. * myPanel.on('hide', this.handleClick, this, {
  381. * single: true,
  382. * delay: 100
  383. * });
  384. *
  385. */
  386. addListener: function(ename, fn, scope, options) {
  387. var me = this,
  388. config,
  389. event;
  390. if (typeof ename !== 'string') {
  391. options = ename;
  392. for (ename in options) {
  393. if (options.hasOwnProperty(ename)) {
  394. config = options[ename];
  395. if (!me.eventOptionsRe.test(ename)) {
  396. me.addListener(ename, config.fn || config, config.scope || options.scope, config.fn ? config : options);
  397. }
  398. }
  399. }
  400. }
  401. else {
  402. ename = ename.toLowerCase();
  403. me.events[ename] = me.events[ename] || true;
  404. event = me.events[ename] || true;
  405. if (Ext.isBoolean(event)) {
  406. me.events[ename] = event = new Ext.util.Event(me, ename);
  407. }
  408. // Allow listeners: { click: 'onClick', scope: myObject }
  409. if (typeof fn === 'string') {
  410. fn = scope[fn] || me.fn;
  411. }
  412. event.addListener(fn, scope, Ext.isObject(options) ? options : {});
  413. // Maintain count of listeners for each event name.
  414. // For repeated events in time-critical code, the firing code should use
  415. // if (!me.hasListeners.beforerender || me.fireEvent('beforerender', me) !== false) { //code... }
  416. me.hasListeners[ename] = (me.hasListeners[ename]||0) + 1;
  417. }
  418. },
  419. /**
  420. * Removes an event handler.
  421. *
  422. * @param {String} eventName The type of event the handler was associated with.
  423. * @param {Function} fn The handler to remove. **This must be a reference to the function passed into the
  424. * {@link #addListener} call.**
  425. * @param {Object} scope (optional) The scope originally specified for the handler. It must be the same as the
  426. * scope argument specified in the original call to {@link #addListener} or the listener will not be removed.
  427. */
  428. removeListener: function(ename, fn, scope) {
  429. var me = this,
  430. config,
  431. event,
  432. options;
  433. if (typeof ename !== 'string') {
  434. options = ename;
  435. for (ename in options) {
  436. if (options.hasOwnProperty(ename)) {
  437. config = options[ename];
  438. if (!me.eventOptionsRe.test(ename)) {
  439. me.removeListener(ename, config.fn || config, config.scope || options.scope);
  440. }
  441. }
  442. }
  443. } else {
  444. ename = ename.toLowerCase();
  445. event = me.events[ename];
  446. if (event && event.isEvent) {
  447. event.removeListener(fn, scope);
  448. // Maintain count of listeners for each event name.
  449. // For repeated events in time-critical code, the firing code should use
  450. // if (!me.hasListeners.beforerender || me.fireEvent('beforerender', me) !== false) { //code... }
  451. me.hasListeners[ename]--;
  452. }
  453. }
  454. },
  455. /**
  456. * Removes all listeners for this object including the managed listeners
  457. */
  458. clearListeners: function() {
  459. var events = this.events,
  460. event,
  461. key;
  462. for (key in events) {
  463. if (events.hasOwnProperty(key)) {
  464. event = events[key];
  465. if (event.isEvent) {
  466. event.clearListeners();
  467. }
  468. }
  469. }
  470. this.clearManagedListeners();
  471. },
  472. purgeListeners : function() {
  473. if (Ext.global.console) {
  474. Ext.global.console.warn('Observable: purgeListeners has been deprecated. Please use clearListeners.');
  475. }
  476. return this.clearListeners.apply(this, arguments);
  477. },
  478. /**
  479. * Removes all managed listeners for this object.
  480. */
  481. clearManagedListeners : function() {
  482. var managedListeners = this.managedListeners || [],
  483. i = 0,
  484. len = managedListeners.length;
  485. for (; i < len; i++) {
  486. this.removeManagedListenerItem(true, managedListeners[i]);
  487. }
  488. this.managedListeners = [];
  489. },
  490. /**
  491. * Remove a single managed listener item
  492. * @private
  493. * @param {Boolean} isClear True if this is being called during a clear
  494. * @param {Object} managedListener The managed listener item
  495. * See removeManagedListener for other args
  496. */
  497. removeManagedListenerItem: function(isClear, managedListener, item, ename, fn, scope){
  498. if (isClear || (managedListener.item === item && managedListener.ename === ename && (!fn || managedListener.fn === fn) && (!scope || managedListener.scope === scope))) {
  499. managedListener.item.un(managedListener.ename, managedListener.fn, managedListener.scope);
  500. if (!isClear) {
  501. Ext.Array.remove(this.managedListeners, managedListener);
  502. }
  503. }
  504. },
  505. purgeManagedListeners : function() {
  506. if (Ext.global.console) {
  507. Ext.global.console.warn('Observable: purgeManagedListeners has been deprecated. Please use clearManagedListeners.');
  508. }
  509. return this.clearManagedListeners.apply(this, arguments);
  510. },
  511. /**
  512. * Adds the specified events to the list of events which this Observable may fire.
  513. *
  514. * @param {Object/String...} eventNames Either an object with event names as properties with
  515. * a value of `true`. For example:
  516. *
  517. * this.addEvents({
  518. * storeloaded: true,
  519. * storecleared: true
  520. * });
  521. *
  522. * Or any number of event names as separate parameters. For example:
  523. *
  524. * this.addEvents('storeloaded', 'storecleared');
  525. *
  526. */
  527. addEvents: function(o) {
  528. var me = this,
  529. events = me.events || (me.events = {}),
  530. arg, args, i;
  531. if (typeof o == 'string') {
  532. for (args = arguments, i = args.length; i--; ) {
  533. arg = args[i];
  534. if (!events[arg]) {
  535. events[arg] = true;
  536. }
  537. }
  538. } else {
  539. Ext.applyIf(me.events, o);
  540. }
  541. },
  542. /**
  543. * Checks to see if this object has any listeners for a specified event, or whether the event bubbles. The answer
  544. * indicates whether the event needs firing or not.
  545. *
  546. * @param {String} eventName The name of the event to check for
  547. * @return {Boolean} `true` if the event is being listened for or bubbles, else `false`
  548. */
  549. hasListener: function(ename) {
  550. return !!this.hasListeners[ename.toLowerCase()];
  551. },
  552. /**
  553. * Suspends the firing of all events. (see {@link #resumeEvents})
  554. *
  555. * @param {Boolean} queueSuspended Pass as true to queue up suspended events to be fired
  556. * after the {@link #resumeEvents} call instead of discarding all suspended events.
  557. */
  558. suspendEvents: function(queueSuspended) {
  559. this.eventsSuspended = true;
  560. if (queueSuspended && !this.eventQueue) {
  561. this.eventQueue = [];
  562. }
  563. },
  564. /**
  565. * Resumes firing events (see {@link #suspendEvents}).
  566. *
  567. * If events were suspended using the `queueSuspended` parameter, then all events fired
  568. * during event suspension will be sent to any listeners now.
  569. */
  570. resumeEvents: function() {
  571. var me = this,
  572. queued = me.eventQueue,
  573. qLen, q;
  574. me.eventsSuspended = false;
  575. delete me.eventQueue;
  576. if (queued) {
  577. qLen = queued.length;
  578. for (q = 0; q < qLen; q++) {
  579. me.continueFireEvent.apply(me, queued[q]);
  580. }
  581. }
  582. },
  583. /**
  584. * Relays selected events from the specified Observable as if the events were fired by `this`.
  585. *
  586. * For example if you are extending Grid, you might decide to forward some events from store.
  587. * So you can do this inside your initComponent:
  588. *
  589. * this.relayEvents(this.getStore(), ['load']);
  590. *
  591. * The grid instance will then have an observable 'load' event which will be passed the
  592. * parameters of the store's load event and any function fired with the grid's load event
  593. * would have access to the grid using the `this` keyword.
  594. *
  595. * @param {Object} origin The Observable whose events this object is to relay.
  596. * @param {String[]} events Array of event names to relay.
  597. * @param {String} [prefix] A common prefix to attach to the event names. For example:
  598. *
  599. * this.relayEvents(this.getStore(), ['load', 'clear'], 'store');
  600. *
  601. * Now the grid will forward 'load' and 'clear' events of store as 'storeload' and 'storeclear'.
  602. */
  603. relayEvents : function(origin, events, prefix) {
  604. prefix = prefix || '';
  605. var me = this,
  606. len = events.length,
  607. i = 0,
  608. oldName,
  609. newName;
  610. for (; i < len; i++) {
  611. oldName = events[i];
  612. newName = prefix + oldName;
  613. me.events[newName] = me.events[newName] || true;
  614. origin.on(oldName, me.createRelayer(newName));
  615. }
  616. },
  617. /**
  618. * @private
  619. * Creates an event handling function which refires the event from this object as the passed event name.
  620. * @param newName
  621. * @param {Array} beginEnd (optional) The caller can specify on which indices to slice
  622. * @returns {Function}
  623. */
  624. createRelayer: function(newName, beginEnd){
  625. var me = this;
  626. return function(){
  627. return me.fireEvent.apply(me, [newName].concat(Array.prototype.slice.apply(arguments, beginEnd || [0, -1])));
  628. };
  629. },
  630. /**
  631. * Enables events fired by this Observable to bubble up an owner hierarchy by calling `this.getBubbleTarget()` if
  632. * present. There is no implementation in the Observable base class.
  633. *
  634. * This is commonly used by Ext.Components to bubble events to owner Containers.
  635. * See {@link Ext.Component#getBubbleTarget}. The default implementation in Ext.Component returns the
  636. * Component's immediate owner. But if a known target is required, this can be overridden to access the
  637. * required target more quickly.
  638. *
  639. * Example:
  640. *
  641. * Ext.override(Ext.form.field.Base, {
  642. * // Add functionality to Field's initComponent to enable the change event to bubble
  643. * initComponent : Ext.Function.createSequence(Ext.form.field.Base.prototype.initComponent, function() {
  644. * this.enableBubble('change');
  645. * }),
  646. *
  647. * // We know that we want Field's events to bubble directly to the FormPanel.
  648. * getBubbleTarget : function() {
  649. * if (!this.formPanel) {
  650. * this.formPanel = this.findParentByType('form');
  651. * }
  652. * return this.formPanel;
  653. * }
  654. * });
  655. *
  656. * var myForm = new Ext.formPanel({
  657. * title: 'User Details',
  658. * items: [{
  659. * ...
  660. * }],
  661. * listeners: {
  662. * change: function() {
  663. * // Title goes red if form has been modified.
  664. * myForm.header.setStyle('color', 'red');
  665. * }
  666. * }
  667. * });
  668. *
  669. * @param {String/String[]} eventNames The event name to bubble, or an Array of event names.
  670. */
  671. enableBubble: function(eventNames) {
  672. if (eventNames) {
  673. var me = this,
  674. names = (typeof eventNames == 'string') ? arguments : eventNames,
  675. length = names.length,
  676. events = me.events,
  677. ename, event, i;
  678. for (i = 0; i < length; ++i) {
  679. ename = names[i].toLowerCase();
  680. event = events[ename];
  681. if (!event || typeof event == 'boolean') {
  682. events[ename] = event = new Ext.util.Event(me, ename);
  683. }
  684. // Event must fire if it bubbles (We don't know if anyone up the bubble hierarchy has listeners added)
  685. me.hasListeners[ename] = (me.hasListeners[ename]||0) + 1;
  686. event.bubble = true;
  687. }
  688. }
  689. }
  690. }, function() {
  691. this.createAlias({
  692. /**
  693. * @method
  694. * Shorthand for {@link #addListener}.
  695. * @inheritdoc Ext.util.Observable#addListener
  696. */
  697. on: 'addListener',
  698. /**
  699. * @method
  700. * Shorthand for {@link #removeListener}.
  701. * @inheritdoc Ext.util.Observable#removeListener
  702. */
  703. un: 'removeListener',
  704. /**
  705. * @method
  706. * Shorthand for {@link #addManagedListener}.
  707. * @inheritdoc Ext.util.Observable#addManagedListener
  708. */
  709. mon: 'addManagedListener',
  710. /**
  711. * @method
  712. * Shorthand for {@link #removeManagedListener}.
  713. * @inheritdoc Ext.util.Observable#removeManagedListener
  714. */
  715. mun: 'removeManagedListener'
  716. });
  717. //deprecated, will be removed in 5.0
  718. this.observeClass = this.observe;
  719. Ext.apply(Ext.util.Observable.prototype, function(){
  720. // this is considered experimental (along with beforeMethod, afterMethod, removeMethodListener?)
  721. // allows for easier interceptor and sequences, including cancelling and overwriting the return value of the call
  722. // private
  723. function getMethodEvent(method){
  724. var e = (this.methodEvents = this.methodEvents || {})[method],
  725. returnValue,
  726. v,
  727. cancel,
  728. obj = this;
  729. if (!e) {
  730. this.methodEvents[method] = e = {};
  731. e.originalFn = this[method];
  732. e.methodName = method;
  733. e.before = [];
  734. e.after = [];
  735. var makeCall = function(fn, scope, args){
  736. if((v = fn.apply(scope || obj, args)) !== undefined){
  737. if (typeof v == 'object') {
  738. if(v.returnValue !== undefined){
  739. returnValue = v.returnValue;
  740. }else{
  741. returnValue = v;
  742. }
  743. cancel = !!v.cancel;
  744. }
  745. else
  746. if (v === false) {
  747. cancel = true;
  748. }
  749. else {
  750. returnValue = v;
  751. }
  752. }
  753. };
  754. this[method] = function(){
  755. var args = Array.prototype.slice.call(arguments, 0),
  756. b, i, len;
  757. returnValue = v = undefined;
  758. cancel = false;
  759. for(i = 0, len = e.before.length; i < len; i++){
  760. b = e.before[i];
  761. makeCall(b.fn, b.scope, args);
  762. if (cancel) {
  763. return returnValue;
  764. }
  765. }
  766. if((v = e.originalFn.apply(obj, args)) !== undefined){
  767. returnValue = v;
  768. }
  769. for(i = 0, len = e.after.length; i < len; i++){
  770. b = e.after[i];
  771. makeCall(b.fn, b.scope, args);
  772. if (cancel) {
  773. return returnValue;
  774. }
  775. }
  776. return returnValue;
  777. };
  778. }
  779. return e;
  780. }
  781. return {
  782. // these are considered experimental
  783. // allows for easier interceptor and sequences, including cancelling and overwriting the return value of the call
  784. // adds an 'interceptor' called before the original method
  785. beforeMethod : function(method, fn, scope){
  786. getMethodEvent.call(this, method).before.push({
  787. fn: fn,
  788. scope: scope
  789. });
  790. },
  791. // adds a 'sequence' called after the original method
  792. afterMethod : function(method, fn, scope){
  793. getMethodEvent.call(this, method).after.push({
  794. fn: fn,
  795. scope: scope
  796. });
  797. },
  798. removeMethodListener: function(method, fn, scope){
  799. var e = this.getMethodEvent(method),
  800. i, len;
  801. for(i = 0, len = e.before.length; i < len; i++){
  802. if(e.before[i].fn == fn && e.before[i].scope == scope){
  803. Ext.Array.erase(e.before, i, 1);
  804. return;
  805. }
  806. }
  807. for(i = 0, len = e.after.length; i < len; i++){
  808. if(e.after[i].fn == fn && e.after[i].scope == scope){
  809. Ext.Array.erase(e.after, i, 1);
  810. return;
  811. }
  812. }
  813. },
  814. toggleEventLogging: function(toggle) {
  815. Ext.util.Observable[toggle ? 'capture' : 'releaseCapture'](this, function(en) {
  816. if (Ext.isDefined(Ext.global.console)) {
  817. Ext.global.console.log(en, arguments);
  818. }
  819. });
  820. }
  821. };
  822. }());
  823. });
  824. /**
  825. * @author Ed Spencer
  826. *
  827. * Associations enable you to express relationships between different {@link Ext.data.Model Models}. Let's say we're
  828. * writing an ecommerce system where Users can make Orders - there's a relationship between these Models that we can
  829. * express like this:
  830. *
  831. * Ext.define('User', {
  832. * extend: 'Ext.data.Model',
  833. * fields: ['id', 'name', 'email'],
  834. *
  835. * hasMany: {model: 'Order', name: 'orders'}
  836. * });
  837. *
  838. * Ext.define('Order', {
  839. * extend: 'Ext.data.Model',
  840. * fields: ['id', 'user_id', 'status', 'price'],
  841. *
  842. * belongsTo: 'User'
  843. * });
  844. *
  845. * We've set up two models - User and Order - and told them about each other. You can set up as many associations on
  846. * each Model as you need using the two default types - {@link Ext.data.HasManyAssociation hasMany} and {@link
  847. * Ext.data.BelongsToAssociation belongsTo}. There's much more detail on the usage of each of those inside their
  848. * documentation pages. If you're not familiar with Models already, {@link Ext.data.Model there is plenty on those too}.
  849. *
  850. * **Further Reading**
  851. *
  852. * - {@link Ext.data.association.HasMany hasMany associations}
  853. * - {@link Ext.data.association.BelongsTo belongsTo associations}
  854. * - {@link Ext.data.association.HasOne hasOne associations}
  855. * - {@link Ext.data.Model using Models}
  856. *
  857. * # Self association models
  858. *
  859. * We can also have models that create parent/child associations between the same type. Below is an example, where
  860. * groups can be nested inside other groups:
  861. *
  862. * // Server Data
  863. * {
  864. * "groups": {
  865. * "id": 10,
  866. * "parent_id": 100,
  867. * "name": "Main Group",
  868. * "parent_group": {
  869. * "id": 100,
  870. * "parent_id": null,
  871. * "name": "Parent Group"
  872. * },
  873. * "child_groups": [{
  874. * "id": 2,
  875. * "parent_id": 10,
  876. * "name": "Child Group 1"
  877. * },{
  878. * "id": 3,
  879. * "parent_id": 10,
  880. * "name": "Child Group 2"
  881. * },{
  882. * "id": 4,
  883. * "parent_id": 10,
  884. * "name": "Child Group 3"
  885. * }]
  886. * }
  887. * }
  888. *
  889. * // Client code
  890. * Ext.define('Group', {
  891. * extend: 'Ext.data.Model',
  892. * fields: ['id', 'parent_id', 'name'],
  893. * proxy: {
  894. * type: 'ajax',
  895. * url: 'data.json',
  896. * reader: {
  897. * type: 'json',
  898. * root: 'groups'
  899. * }
  900. * },
  901. * associations: [{
  902. * type: 'hasMany',
  903. * model: 'Group',
  904. * primaryKey: 'id',
  905. * foreignKey: 'parent_id',
  906. * autoLoad: true,
  907. * associationKey: 'child_groups' // read child data from child_groups
  908. * }, {
  909. * type: 'belongsTo',
  910. * model: 'Group',
  911. * primaryKey: 'id',
  912. * foreignKey: 'parent_id',
  913. * associationKey: 'parent_group' // read parent data from parent_group
  914. * }]
  915. * });
  916. *
  917. * Ext.onReady(function(){
  918. *
  919. * Group.load(10, {
  920. * success: function(group){
  921. * console.log(group.getGroup().get('name'));
  922. *
  923. * group.groups().each(function(rec){
  924. * console.log(rec.get('name'));
  925. * });
  926. * }
  927. * });
  928. *
  929. * });
  930. *
  931. */
  932. Ext.define('Ext.data.association.Association', {
  933. alternateClassName: 'Ext.data.Association',
  934. /**
  935. * @cfg {String} ownerModel (required)
  936. * The string name of the model that owns the association.
  937. */
  938. /**
  939. * @cfg {String} associatedModel (required)
  940. * The string name of the model that is being associated with.
  941. */
  942. /**
  943. * @cfg {String} primaryKey
  944. * The name of the primary key on the associated model. In general this will be the
  945. * {@link Ext.data.Model#idProperty} of the Model.
  946. */
  947. primaryKey: 'id',
  948. /**
  949. * @cfg {Ext.data.reader.Reader} reader
  950. * A special reader to read associated data
  951. */
  952. /**
  953. * @cfg {String} associationKey
  954. * The name of the property in the data to read the association from. Defaults to the name of the associated model.
  955. */
  956. defaultReaderType: 'json',
  957. statics: {
  958. create: function(association){
  959. if (!association.isAssociation) {
  960. if (Ext.isString(association)) {
  961. association = {
  962. type: association
  963. };
  964. }
  965. switch (association.type) {
  966. case 'belongsTo':
  967. return new Ext.data.association.BelongsTo(association);
  968. case 'hasMany':
  969. return new Ext.data.association.HasMany(association);
  970. case 'hasOne':
  971. return new Ext.data.association.HasOne(association);
  972. //TODO Add this back when it's fixed
  973. // case 'polymorphic':
  974. // return Ext.create('Ext.data.PolymorphicAssociation', association);
  975. default:
  976. Ext.Error.raise('Unknown Association type: "' + association.type + '"');
  977. }
  978. }
  979. return association;
  980. }
  981. },
  982. /**
  983. * Creates the Association object.
  984. * @param {Object} [config] Config object.
  985. */
  986. constructor: function(config) {
  987. Ext.apply(this, config);
  988. var types = Ext.ModelManager.types,
  989. ownerName = config.ownerModel,
  990. associatedName = config.associatedModel,
  991. ownerModel = types[ownerName],
  992. associatedModel = types[associatedName],
  993. ownerProto;
  994. if (ownerModel === undefined) {
  995. Ext.Error.raise("The configured ownerModel was not valid (you tried " + ownerName + ")");
  996. }
  997. if (associatedModel === undefined) {
  998. Ext.Error.raise("The configured associatedModel was not valid (you tried " + associatedName + ")");
  999. }
  1000. this.ownerModel = ownerModel;
  1001. this.associatedModel = associatedModel;
  1002. /**
  1003. * @property {String} ownerName
  1004. * The name of the model that 'owns' the association
  1005. */
  1006. /**
  1007. * @property {String} associatedName
  1008. * The name of the model is on the other end of the association (e.g. if a User model hasMany Orders, this is
  1009. * 'Order')
  1010. */
  1011. Ext.applyIf(this, {
  1012. ownerName : ownerName,
  1013. associatedName: associatedName
  1014. });
  1015. },
  1016. /**
  1017. * Get a specialized reader for reading associated data
  1018. * @return {Ext.data.reader.Reader} The reader, null if not supplied
  1019. */
  1020. getReader: function(){
  1021. var me = this,
  1022. reader = me.reader,
  1023. model = me.associatedModel;
  1024. if (reader) {
  1025. if (Ext.isString(reader)) {
  1026. reader = {
  1027. type: reader
  1028. };
  1029. }
  1030. if (reader.isReader) {
  1031. reader.setModel(model);
  1032. } else {
  1033. Ext.applyIf(reader, {
  1034. model: model,
  1035. type : me.defaultReaderType
  1036. });
  1037. }
  1038. me.reader = Ext.createByAlias('reader.' + reader.type, reader);
  1039. }
  1040. return me.reader || null;
  1041. }
  1042. });
  1043. /**
  1044. * @author Don Griffin
  1045. *
  1046. * This class is a base for all id generators. It also provides lookup of id generators by
  1047. * their id.
  1048. *
  1049. * Generally, id generators are used to generate a primary key for new model instances. There
  1050. * are different approaches to solving this problem, so this mechanism has both simple use
  1051. * cases and is open to custom implementations. A {@link Ext.data.Model} requests id generation
  1052. * using the {@link Ext.data.Model#idgen} property.
  1053. *
  1054. * # Identity, Type and Shared IdGenerators
  1055. *
  1056. * It is often desirable to share IdGenerators to ensure uniqueness or common configuration.
  1057. * This is done by giving IdGenerator instances an id property by which they can be looked
  1058. * up using the {@link #get} method. To configure two {@link Ext.data.Model Model} classes
  1059. * to share one {@link Ext.data.SequentialIdGenerator sequential} id generator, you simply
  1060. * assign them the same id:
  1061. *
  1062. * Ext.define('MyApp.data.MyModelA', {
  1063. * extend: 'Ext.data.Model',
  1064. * idgen: {
  1065. * type: 'sequential',
  1066. * id: 'foo'
  1067. * }
  1068. * });
  1069. *
  1070. * Ext.define('MyApp.data.MyModelB', {
  1071. * extend: 'Ext.data.Model',
  1072. * idgen: {
  1073. * type: 'sequential',
  1074. * id: 'foo'
  1075. * }
  1076. * });
  1077. *
  1078. * To make this as simple as possible for generator types that are shared by many (or all)
  1079. * Models, the IdGenerator types (such as 'sequential' or 'uuid') are also reserved as
  1080. * generator id's. This is used by the {@link Ext.data.UuidGenerator} which has an id equal
  1081. * to its type ('uuid'). In other words, the following Models share the same generator:
  1082. *
  1083. * Ext.define('MyApp.data.MyModelX', {
  1084. * extend: 'Ext.data.Model',
  1085. * idgen: 'uuid'
  1086. * });
  1087. *
  1088. * Ext.define('MyApp.data.MyModelY', {
  1089. * extend: 'Ext.data.Model',
  1090. * idgen: 'uuid'
  1091. * });
  1092. *
  1093. * This can be overridden (by specifying the id explicitly), but there is no particularly
  1094. * good reason to do so for this generator type.
  1095. *
  1096. * # Creating Custom Generators
  1097. *
  1098. * An id generator should derive from this class and implement the {@link #generate} method.
  1099. * The constructor will apply config properties on new instances, so a constructor is often
  1100. * not necessary.
  1101. *
  1102. * To register an id generator type, a derived class should provide an `alias` like so:
  1103. *
  1104. * Ext.define('MyApp.data.CustomIdGenerator', {
  1105. * extend: 'Ext.data.IdGenerator',
  1106. * alias: 'idgen.custom',
  1107. *
  1108. * configProp: 42, // some config property w/default value
  1109. *
  1110. * generate: function () {
  1111. * return ... // a new id
  1112. * }
  1113. * });
  1114. *
  1115. * Using the custom id generator is then straightforward:
  1116. *
  1117. * Ext.define('MyApp.data.MyModel', {
  1118. * extend: 'Ext.data.Model',
  1119. * idgen: 'custom'
  1120. * });
  1121. * // or...
  1122. *
  1123. * Ext.define('MyApp.data.MyModel', {
  1124. * extend: 'Ext.data.Model',
  1125. * idgen: {
  1126. * type: 'custom',
  1127. * configProp: value
  1128. * }
  1129. * });
  1130. *
  1131. * It is not recommended to mix shared generators with generator configuration. This leads
  1132. * to unpredictable results unless all configurations match (which is also redundant). In
  1133. * such cases, a custom generator with a default id is the best approach.
  1134. *
  1135. * Ext.define('MyApp.data.CustomIdGenerator', {
  1136. * extend: 'Ext.data.SequentialIdGenerator',
  1137. * alias: 'idgen.custom',
  1138. *
  1139. * id: 'custom', // shared by default
  1140. *
  1141. * prefix: 'ID_',
  1142. * seed: 1000
  1143. * });
  1144. *
  1145. * Ext.define('MyApp.data.MyModelX', {
  1146. * extend: 'Ext.data.Model',
  1147. * idgen: 'custom'
  1148. * });
  1149. *
  1150. * Ext.define('MyApp.data.MyModelY', {
  1151. * extend: 'Ext.data.Model',
  1152. * idgen: 'custom'
  1153. * });
  1154. *
  1155. * // the above models share a generator that produces ID_1000, ID_1001, etc..
  1156. *
  1157. */
  1158. Ext.define('Ext.data.IdGenerator', {
  1159. /**
  1160. * @property {Boolean} isGenerator
  1161. * `true` in this class to identify an objact as an instantiated IdGenerator, or subclass thereof.
  1162. */
  1163. isGenerator: true,
  1164. /**
  1165. * Initializes a new instance.
  1166. * @param {Object} config (optional) Configuration object to be applied to the new instance.
  1167. */
  1168. constructor: function(config) {
  1169. var me = this;
  1170. Ext.apply(me, config);
  1171. if (me.id) {
  1172. Ext.data.IdGenerator.all[me.id] = me;
  1173. }
  1174. },
  1175. /**
  1176. * @cfg {String} id
  1177. * The id by which to register a new instance. This instance can be found using the
  1178. * {@link Ext.data.IdGenerator#get} static method.
  1179. */
  1180. getRecId: function (rec) {
  1181. return rec.modelName + '-' + rec.internalId;
  1182. },
  1183. /**
  1184. * Generates and returns the next id. This method must be implemented by the derived
  1185. * class.
  1186. *
  1187. * @return {String} The next id.
  1188. * @method generate
  1189. * @abstract
  1190. */
  1191. statics: {
  1192. /**
  1193. * @property {Object} all
  1194. * This object is keyed by id to lookup instances.
  1195. * @private
  1196. * @static
  1197. */
  1198. all: {},
  1199. /**
  1200. * Returns the IdGenerator given its config description.
  1201. * @param {String/Object} config If this parameter is an IdGenerator instance, it is
  1202. * simply returned. If this is a string, it is first used as an id for lookup and
  1203. * then, if there is no match, as a type to create a new instance. This parameter
  1204. * can also be a config object that contains a `type` property (among others) that
  1205. * are used to create and configure the instance.
  1206. * @static
  1207. */
  1208. get: function (config) {
  1209. var generator,
  1210. id,
  1211. type;
  1212. if (typeof config == 'string') {
  1213. id = type = config;
  1214. config = null;
  1215. } else if (config.isGenerator) {
  1216. return config;
  1217. } else {
  1218. id = config.id || config.type;
  1219. type = config.type;
  1220. }
  1221. generator = this.all[id];
  1222. if (!generator) {
  1223. generator = Ext.create('idgen.' + type, config);
  1224. }
  1225. return generator;
  1226. }
  1227. }
  1228. });
  1229. /**
  1230. * @author Ed Spencer
  1231. *
  1232. * Represents a single read or write operation performed by a {@link Ext.data.proxy.Proxy Proxy}. Operation objects are
  1233. * used to enable communication between Stores and Proxies. Application developers should rarely need to interact with
  1234. * Operation objects directly.
  1235. *
  1236. * Several Operations can be batched together in a {@link Ext.data.Batch batch}.
  1237. */
  1238. Ext.define('Ext.data.Operation', {
  1239. /**
  1240. * @cfg {Boolean} synchronous
  1241. * True if this Operation is to be executed synchronously. This property is inspected by a
  1242. * {@link Ext.data.Batch Batch} to see if a series of Operations can be executed in parallel or not.
  1243. */
  1244. synchronous: true,
  1245. /**
  1246. * @cfg {String} action
  1247. * The action being performed by this Operation. Should be one of 'create', 'read', 'update' or 'destroy'.
  1248. */
  1249. action: undefined,
  1250. /**
  1251. * @cfg {Ext.util.Filter[]} filters
  1252. * Optional array of filter objects. Only applies to 'read' actions.
  1253. */
  1254. filters: undefined,
  1255. /**
  1256. * @cfg {Ext.util.Sorter[]} sorters
  1257. * Optional array of sorter objects. Only applies to 'read' actions.
  1258. */
  1259. sorters: undefined,
  1260. /**
  1261. * @cfg {Ext.util.Grouper[]} groupers
  1262. * Optional grouping configuration. Only applies to 'read' actions where grouping is desired.
  1263. */
  1264. groupers: undefined,
  1265. /**
  1266. * @cfg {Number} start
  1267. * The start index (offset), used in paging when running a 'read' action.
  1268. */
  1269. start: undefined,
  1270. /**
  1271. * @cfg {Number} limit
  1272. * The number of records to load. Used on 'read' actions when paging is being used.
  1273. */
  1274. limit: undefined,
  1275. /**
  1276. * @cfg {Ext.data.Batch} batch
  1277. * The batch that this Operation is a part of.
  1278. */
  1279. batch: undefined,
  1280. /**
  1281. * @cfg {Function} callback
  1282. * Function to execute when operation completed.
  1283. * @cfg {Ext.data.Model[]} callback.records Array of records.
  1284. * @cfg {Ext.data.Operation} callback.operation The Operation itself.
  1285. * @cfg {Boolean} callback.success True when operation completed successfully.
  1286. */
  1287. callback: undefined,
  1288. /**
  1289. * @cfg {Object} scope
  1290. * Scope for the {@link #callback} function.
  1291. */
  1292. scope: undefined,
  1293. /**
  1294. * @property {Boolean} started
  1295. * The start status of this Operation. Use {@link #isStarted}.
  1296. * @readonly
  1297. * @private
  1298. */
  1299. started: false,
  1300. /**
  1301. * @property {Boolean} running
  1302. * The run status of this Operation. Use {@link #isRunning}.
  1303. * @readonly
  1304. * @private
  1305. */
  1306. running: false,
  1307. /**
  1308. * @property {Boolean} complete
  1309. * The completion status of this Operation. Use {@link #isComplete}.
  1310. * @readonly
  1311. * @private
  1312. */
  1313. complete: false,
  1314. /**
  1315. * @property {Boolean} success
  1316. * Whether the Operation was successful or not. This starts as undefined and is set to true
  1317. * or false by the Proxy that is executing the Operation. It is also set to false by {@link #setException}. Use
  1318. * {@link #wasSuccessful} to query success status.
  1319. * @readonly
  1320. * @private
  1321. */
  1322. success: undefined,
  1323. /**
  1324. * @property {Boolean} exception
  1325. * The exception status of this Operation. Use {@link #hasException} and see {@link #getError}.
  1326. * @readonly
  1327. * @private
  1328. */
  1329. exception: false,
  1330. /**
  1331. * @property {String/Object} error
  1332. * The error object passed when {@link #setException} was called. This could be any object or primitive.
  1333. * @private
  1334. */
  1335. error: undefined,
  1336. /**
  1337. * @property {RegExp} actionCommitRecordsRe
  1338. * The RegExp used to categorize actions that require record commits.
  1339. */
  1340. actionCommitRecordsRe: /^(?:create|update)$/i,
  1341. /**
  1342. * @property {RegExp} actionSkipSyncRe
  1343. * The RegExp used to categorize actions that skip local record synchronization. This defaults
  1344. * to match 'destroy'.
  1345. */
  1346. actionSkipSyncRe: /^destroy$/i,
  1347. /**
  1348. * Creates new Operation object.
  1349. * @param {Object} config (optional) Config object.
  1350. */
  1351. constructor: function(config) {
  1352. Ext.apply(this, config || {});
  1353. },
  1354. /**
  1355. * This method is called to commit data to this instance's records given the records in
  1356. * the server response. This is followed by calling {@link Ext.data.Model#commit} on all
  1357. * those records (for 'create' and 'update' actions).
  1358. *
  1359. * If this {@link #action} is 'destroy', any server records are ignored and the
  1360. * {@link Ext.data.Model#commit} method is not called.
  1361. *
  1362. * @param {Ext.data.Model[]} serverRecords An array of {@link Ext.data.Model} objects returned by
  1363. * the server.
  1364. * @markdown
  1365. */
  1366. commitRecords: function (serverRecords) {
  1367. var me = this,
  1368. mc, index, clientRecords, serverRec, clientRec;
  1369. if (!me.actionSkipSyncRe.test(me.action)) {
  1370. clientRecords = me.records;
  1371. if (clientRecords && clientRecords.length) {
  1372. if(clientRecords.length > 1) {
  1373. // if this operation has multiple records, client records need to be matched up with server records
  1374. // so that any data returned from the server can be updated in the client records.
  1375. mc = new Ext.util.MixedCollection();
  1376. mc.addAll(serverRecords);
  1377. for (index = clientRecords.length; index--; ) {
  1378. clientRec = clientRecords[index];
  1379. serverRec = mc.findBy(function(record) {
  1380. var clientRecordId = clientRec.getId();
  1381. if(clientRecordId && record.getId() === clientRecordId) {
  1382. return true;
  1383. }
  1384. // if the server record cannot be found by id, find by internalId.
  1385. // this allows client records that did not previously exist on the server
  1386. // to be updated with the correct server id and data.
  1387. return record.internalId === clientRec.internalId;
  1388. });
  1389. // replace client record data with server record data
  1390. me.updateClientRecord(clientRec, serverRec);
  1391. }
  1392. } else {
  1393. // operation only has one record, so just match the first client record up with the first server record
  1394. clientRec = clientRecords[0];
  1395. serverRec = serverRecords[0];
  1396. // if the client record is not a phantom, make sure the ids match before replacing the client data with server data.
  1397. if(serverRec && (clientRec.phantom || clientRec.getId() === serverRec.getId())) {
  1398. me.updateClientRecord(clientRec, serverRec);
  1399. }
  1400. }
  1401. if (me.actionCommitRecordsRe.test(me.action)) {
  1402. for (index = clientRecords.length; index--; ) {
  1403. clientRecords[index].commit();
  1404. }
  1405. }
  1406. }
  1407. }
  1408. },
  1409. /**
  1410. * Replaces the data in a client record with the data from a server record. If either record is undefined, does nothing.
  1411. * Since non-persistent fields will have default values in the server record, this method only replaces data for persistent
  1412. * fields to avoid overwriting the client record's data with default values from the server record.
  1413. * @private
  1414. * @param {Ext.data.Model} [clientRecord]
  1415. * @param {Ext.data.Model} [serverRecord]
  1416. */
  1417. updateClientRecord: function(clientRecord, serverRecord) {
  1418. if (clientRecord && serverRecord) {
  1419. clientRecord.beginEdit();
  1420. var fields = clientRecord.fields.items,
  1421. fLen = fields.length,
  1422. field, f;
  1423. for (f = 0; f < fLen; f++) {
  1424. field = fields[f];
  1425. if (field.persist) {
  1426. clientRecord.set(field.name, serverRecord.get(field.name));
  1427. }
  1428. }
  1429. if(clientRecord.phantom) {
  1430. clientRecord.setId(serverRecord.getId());
  1431. }
  1432. clientRecord.endEdit(true);
  1433. }
  1434. },
  1435. /**
  1436. * Marks the Operation as started.
  1437. */
  1438. setStarted: function() {
  1439. this.started = true;
  1440. this.running = true;
  1441. },
  1442. /**
  1443. * Marks the Operation as completed.
  1444. */
  1445. setCompleted: function() {
  1446. this.complete = true;
  1447. this.running = false;
  1448. },
  1449. /**
  1450. * Marks the Operation as successful.
  1451. */
  1452. setSuccessful: function() {
  1453. this.success = true;
  1454. },
  1455. /**
  1456. * Marks the Operation as having experienced an exception. Can be supplied with an option error message/object.
  1457. * @param {String/Object} error (optional) error string/object
  1458. */
  1459. setException: function(error) {
  1460. this.exception = true;
  1461. this.success = false;
  1462. this.running = false;
  1463. this.error = error;
  1464. },
  1465. /**
  1466. * Returns true if this Operation encountered an exception (see also {@link #getError})
  1467. * @return {Boolean} True if there was an exception
  1468. */
  1469. hasException: function() {
  1470. return this.exception === true;
  1471. },
  1472. /**
  1473. * Returns the error string or object that was set using {@link #setException}
  1474. * @return {String/Object} The error object
  1475. */
  1476. getError: function() {
  1477. return this.error;
  1478. },
  1479. /**
  1480. * Returns the {@link Ext.data.Model record}s associated with this operation. For read operations the records as set by the {@link Ext.data.proxy.Proxy Proxy} will be returned (returns `null` if the proxy has not yet set the records).
  1481. * For create, update, and destroy operations the operation's initially configured records will be returned, although the proxy may modify these records' data at some point after the operation is initialized.
  1482. * @return {Ext.data.Model[]}
  1483. */
  1484. getRecords: function() {
  1485. var resultSet = this.getResultSet();
  1486. return this.records || (resultSet ? resultSet.records : null);
  1487. },
  1488. /**
  1489. * Returns the ResultSet object (if set by the Proxy). This object will contain the {@link Ext.data.Model model}
  1490. * instances as well as meta data such as number of instances fetched, number available etc
  1491. * @return {Ext.data.ResultSet} The ResultSet object
  1492. */
  1493. getResultSet: function() {
  1494. return this.resultSet;
  1495. },
  1496. /**
  1497. * Returns true if the Operation has been started. Note that the Operation may have started AND completed, see
  1498. * {@link #isRunning} to test if the Operation is currently running.
  1499. * @return {Boolean} True if the Operation has started
  1500. */
  1501. isStarted: function() {
  1502. return this.started === true;
  1503. },
  1504. /**
  1505. * Returns true if the Operation has been started but has not yet completed.
  1506. * @return {Boolean} True if the Operation is currently running
  1507. */
  1508. isRunning: function() {
  1509. return this.running === true;
  1510. },
  1511. /**
  1512. * Returns true if the Operation has been completed
  1513. * @return {Boolean} True if the Operation is complete
  1514. */
  1515. isComplete: function() {
  1516. return this.complete === true;
  1517. },
  1518. /**
  1519. * Returns true if the Operation has completed and was successful
  1520. * @return {Boolean} True if successful
  1521. */
  1522. wasSuccessful: function() {
  1523. return this.isComplete() && this.success === true;
  1524. },
  1525. /**
  1526. * @private
  1527. * Associates this Operation with a Batch
  1528. * @param {Ext.data.Batch} batch The batch
  1529. */
  1530. setBatch: function(batch) {
  1531. this.batch = batch;
  1532. },
  1533. /**
  1534. * Checks whether this operation should cause writing to occur.
  1535. * @return {Boolean} Whether the operation should cause a write to occur.
  1536. */
  1537. allowWrite: function() {
  1538. return this.action != 'read';
  1539. }
  1540. });
  1541. /**
  1542. * @author Ed Spencer
  1543. *
  1544. * This singleton contains a set of validation functions that can be used to validate any type of data. They are most
  1545. * often used in {@link Ext.data.Model Models}, where they are automatically set up and executed.
  1546. */
  1547. Ext.define('Ext.data.validations', {
  1548. singleton: true,
  1549. /**
  1550. * @property {String} presenceMessage
  1551. * The default error message used when a presence validation fails.
  1552. */
  1553. presenceMessage: 'must be present',
  1554. /**
  1555. * @property {String} lengthMessage
  1556. * The default error message used when a length validation fails.
  1557. */
  1558. lengthMessage: 'is the wrong length',
  1559. /**
  1560. * @property {Boolean} formatMessage
  1561. * The default error message used when a format validation fails.
  1562. */
  1563. formatMessage: 'is the wrong format',
  1564. /**
  1565. * @property {String} inclusionMessage
  1566. * The default error message used when an inclusion validation fails.
  1567. */
  1568. inclusionMessage: 'is not included in the list of acceptable values',
  1569. /**
  1570. * @property {String} exclusionMessage
  1571. * The default error message used when an exclusion validation fails.
  1572. */
  1573. exclusionMessage: 'is not an acceptable value',
  1574. /**
  1575. * @property {String} emailMessage
  1576. * The default error message used when an email validation fails
  1577. */
  1578. emailMessage: 'is not a valid email address',
  1579. /**
  1580. * @property {RegExp} emailRe
  1581. * The regular expression used to validate email addresses
  1582. */
  1583. emailRe: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
  1584. /**
  1585. * Validates that the given value is present.
  1586. * For example:
  1587. *
  1588. * validations: [{type: 'presence', field: 'age'}]
  1589. *
  1590. * @param {Object} config Config object
  1591. * @param {Object} value The value to validate
  1592. * @return {Boolean} True if validation passed
  1593. */
  1594. presence: function(config, value) {
  1595. if (value === undefined) {
  1596. value = config;
  1597. }
  1598. //we need an additional check for zero here because zero is an acceptable form of present data
  1599. return !!value || value === 0;
  1600. },
  1601. /**
  1602. * Returns true if the given value is between the configured min and max values.
  1603. * For example:
  1604. *
  1605. * validations: [{type: 'length', field: 'name', min: 2}]
  1606. *
  1607. * @param {Object} config Config object
  1608. * @param {String} value The value to validate
  1609. * @return {Boolean} True if the value passes validation
  1610. */
  1611. length: function(config, value) {
  1612. if (value === undefined || value === null) {
  1613. return false;
  1614. }
  1615. var length = value.length,
  1616. min = config.min,
  1617. max = config.max;
  1618. if ((min && length < min) || (max && length > max)) {
  1619. return false;
  1620. } else {
  1621. return true;
  1622. }
  1623. },
  1624. /**
  1625. * Validates that an email string is in the correct format
  1626. * @param {Object} config Config object
  1627. * @param {String} email The email address
  1628. * @return {Boolean} True if the value passes validation
  1629. */
  1630. email: function(config, email) {
  1631. return Ext.data.validations.emailRe.test(email);
  1632. },
  1633. /**
  1634. * Returns true if the given value passes validation against the configured `matcher` regex.
  1635. * For example:
  1636. *
  1637. * validations: [{type: 'format', field: 'username', matcher: /([a-z]+)[0-9]{2,3}/}]
  1638. *
  1639. * @param {Object} config Config object
  1640. * @param {String} value The value to validate
  1641. * @return {Boolean} True if the value passes the format validation
  1642. */
  1643. format: function(config, value) {
  1644. return !!(config.matcher && config.matcher.test(value));
  1645. },
  1646. /**
  1647. * Validates that the given value is present in the configured `list`.
  1648. * For example:
  1649. *
  1650. * validations: [{type: 'inclusion', field: 'gender', list: ['Male', 'Female']}]
  1651. *
  1652. * @param {Object} config Config object
  1653. * @param {String} value The value to validate
  1654. * @return {Boolean} True if the value is present in the list
  1655. */
  1656. inclusion: function(config, value) {
  1657. return config.list && Ext.Array.indexOf(config.list,value) != -1;
  1658. },
  1659. /**
  1660. * Validates that the given value is present in the configured `list`.
  1661. * For example:
  1662. *
  1663. * validations: [{type: 'exclusion', field: 'username', list: ['Admin', 'Operator']}]
  1664. *
  1665. * @param {Object} config Config object
  1666. * @param {String} value The value to validate
  1667. * @return {Boolean} True if the value is not present in the list
  1668. */
  1669. exclusion: function(config, value) {
  1670. return config.list && Ext.Array.indexOf(config.list,value) == -1;
  1671. }
  1672. });
  1673. /**
  1674. * @class Ext.util.HashMap
  1675. * <p>
  1676. * Represents a collection of a set of key and value pairs. Each key in the HashMap
  1677. * must be unique, the same key cannot exist twice. Access to items is provided via
  1678. * the key only. Sample usage:
  1679. * <pre><code>
  1680. var map = new Ext.util.HashMap();
  1681. map.add('key1', 1);
  1682. map.add('key2', 2);
  1683. map.add('key3', 3);
  1684. map.each(function(key, value, length){
  1685. console.log(key, value, length);
  1686. });
  1687. * </code></pre>
  1688. * </p>
  1689. *
  1690. * <p>The HashMap is an unordered class,
  1691. * there is no guarantee when iterating over the items that they will be in any particular
  1692. * order. If this is required, then use a {@link Ext.util.MixedCollection}.
  1693. * </p>
  1694. */
  1695. Ext.define('Ext.util.HashMap', {
  1696. mixins: {
  1697. observable: 'Ext.util.Observable'
  1698. },
  1699. /**
  1700. * @cfg {Function} keyFn A function that is used to retrieve a default key for a passed object.
  1701. * A default is provided that returns the <b>id</b> property on the object. This function is only used
  1702. * if the add method is called with a single argument.
  1703. */
  1704. /**
  1705. * Creates new HashMap.
  1706. * @param {Object} config (optional) Config object.
  1707. */
  1708. constructor: function(config) {
  1709. config = config || {};
  1710. var me = this,
  1711. keyFn = config.keyFn;
  1712. me.addEvents(
  1713. /**
  1714. * @event add
  1715. * Fires when a new item is added to the hash
  1716. * @param {Ext.util.HashMap} this.
  1717. * @param {String} key The key of the added item.
  1718. * @param {Object} value The value of the added item.
  1719. */
  1720. 'add',
  1721. /**
  1722. * @event clear
  1723. * Fires when the hash is cleared.
  1724. * @param {Ext.util.HashMap} this.
  1725. */
  1726. 'clear',
  1727. /**
  1728. * @event remove
  1729. * Fires when an item is removed from the hash.
  1730. * @param {Ext.util.HashMap} this.
  1731. * @param {String} key The key of the removed item.
  1732. * @param {Object} value The value of the removed item.
  1733. */
  1734. 'remove',
  1735. /**
  1736. * @event replace
  1737. * Fires when an item is replaced in the hash.
  1738. * @param {Ext.util.HashMap} this.
  1739. * @param {String} key The key of the replaced item.
  1740. * @param {Object} value The new value for the item.
  1741. * @param {Object} old The old value for the item.
  1742. */
  1743. 'replace'
  1744. );
  1745. me.mixins.observable.constructor.call(me, config);
  1746. me.clear(true);
  1747. if (keyFn) {
  1748. me.getKey = keyFn;
  1749. }
  1750. },
  1751. /**
  1752. * Gets the number of items in the hash.
  1753. * @return {Number} The number of items in the hash.
  1754. */
  1755. getCount: function() {
  1756. return this.length;
  1757. },
  1758. /**
  1759. * Implementation for being able to extract the key from an object if only
  1760. * a single argument is passed.
  1761. * @private
  1762. * @param {String} key The key
  1763. * @param {Object} value The value
  1764. * @return {Array} [key, value]
  1765. */
  1766. getData: function(key, value) {
  1767. // if we have no value, it means we need to get the key from the object
  1768. if (value === undefined) {
  1769. value = key;
  1770. key = this.getKey(value);
  1771. }
  1772. return [key, value];
  1773. },
  1774. /**
  1775. * Extracts the key from an object. This is a default implementation, it may be overridden
  1776. * @param {Object} o The object to get the key from
  1777. * @return {String} The key to use.
  1778. */
  1779. getKey: function(o) {
  1780. return o.id;
  1781. },
  1782. /**
  1783. * Adds an item to the collection. Fires the {@link #event-add} event when complete.
  1784. * @param {String} key <p>The key to associate with the item, or the new item.</p>
  1785. * <p>If a {@link #getKey} implementation was specified for this HashMap,
  1786. * or if the key of the stored items is in a property called <tt><b>id</b></tt>,
  1787. * the HashMap will be able to <i>derive</i> the key for the new item.
  1788. * In this case just pass the new item in this parameter.</p>
  1789. * @param {Object} o The item to add.
  1790. * @return {Object} The item added.
  1791. */
  1792. add: function(key, value) {
  1793. var me = this,
  1794. data;
  1795. if (arguments.length === 1) {
  1796. value = key;
  1797. key = me.getKey(value);
  1798. }
  1799. if (me.containsKey(key)) {
  1800. return me.replace(key, value);
  1801. }
  1802. data = me.getData(key, value);
  1803. key = data[0];
  1804. value = data[1];
  1805. me.map[key] = value;
  1806. ++me.length;
  1807. if (me.hasListeners.add) {
  1808. me.fireEvent('add', me, key, value);
  1809. }
  1810. return value;
  1811. },
  1812. /**
  1813. * Replaces an item in the hash. If the key doesn't exist, the
  1814. * {@link #method-add} method will be used.
  1815. * @param {String} key The key of the item.
  1816. * @param {Object} value The new value for the item.
  1817. * @return {Object} The new value of the item.
  1818. */
  1819. replace: function(key, value) {
  1820. var me = this,
  1821. map = me.map,
  1822. old;
  1823. if (!me.containsKey(key)) {
  1824. me.add(key, value);
  1825. }
  1826. old = map[key];
  1827. map[key] = value;
  1828. if (me.hasListeners.replace) {
  1829. me.fireEvent('replace', me, key, value, old);
  1830. }
  1831. return value;
  1832. },
  1833. /**
  1834. * Remove an item from the hash.
  1835. * @param {Object} o The value of the item to remove.
  1836. * @return {Boolean} True if the item was successfully removed.
  1837. */
  1838. remove: function(o) {
  1839. var key = this.findKey(o);
  1840. if (key !== undefined) {
  1841. return this.removeAtKey(key);
  1842. }
  1843. return false;
  1844. },
  1845. /**
  1846. * Remove an item from the hash.
  1847. * @param {String} key The key to remove.
  1848. * @return {Boolean} True if the item was successfully removed.
  1849. */
  1850. removeAtKey: function(key) {
  1851. var me = this,
  1852. value;
  1853. if (me.containsKey(key)) {
  1854. value = me.map[key];
  1855. delete me.map[key];
  1856. --me.length;
  1857. if (me.hasListeners.remove) {
  1858. me.fireEvent('remove', me, key, value);
  1859. }
  1860. return true;
  1861. }
  1862. return false;
  1863. },
  1864. /**
  1865. * Retrieves an item with a particular key.
  1866. * @param {String} key The key to lookup.
  1867. * @return {Object} The value at that key. If it doesn't exist, <tt>undefined</tt> is returned.
  1868. */
  1869. get: function(key) {
  1870. return this.map[key];
  1871. },
  1872. /**
  1873. * Removes all items from the hash.
  1874. * @return {Ext.util.HashMap} this
  1875. */
  1876. clear: function(/* private */ initial) {
  1877. var me = this;
  1878. me.map = {};
  1879. me.length = 0;
  1880. if (initial !== true && me.hasListeners.clear) {
  1881. me.fireEvent('clear', me);
  1882. }
  1883. return me;
  1884. },
  1885. /**
  1886. * Checks whether a key exists in the hash.
  1887. * @param {String} key The key to check for.
  1888. * @return {Boolean} True if they key exists in the hash.
  1889. */
  1890. containsKey: function(key) {
  1891. return this.map[key] !== undefined;
  1892. },
  1893. /**
  1894. * Checks whether a value exists in the hash.
  1895. * @param {Object} value The value to check for.
  1896. * @return {Boolean} True if the value exists in the dictionary.
  1897. */
  1898. contains: function(value) {
  1899. return this.containsKey(this.findKey(value));
  1900. },
  1901. /**
  1902. * Return all of the keys in the hash.
  1903. * @return {Array} An array of keys.
  1904. */
  1905. getKeys: function() {
  1906. return this.getArray(true);
  1907. },
  1908. /**
  1909. * Return all of the values in the hash.
  1910. * @return {Array} An array of values.
  1911. */
  1912. getValues: function() {
  1913. return this.getArray(false);
  1914. },
  1915. /**
  1916. * Gets either the keys/values in an array from the hash.
  1917. * @private
  1918. * @param {Boolean} isKey True to extract the keys, otherwise, the value
  1919. * @return {Array} An array of either keys/values from the hash.
  1920. */
  1921. getArray: function(isKey) {
  1922. var arr = [],
  1923. key,
  1924. map = this.map;
  1925. for (key in map) {
  1926. if (map.hasOwnProperty(key)) {
  1927. arr.push(isKey ? key: map[key]);
  1928. }
  1929. }
  1930. return arr;
  1931. },
  1932. /**
  1933. * Executes the specified function once for each item in the hash.
  1934. * Returning false from the function will cease iteration.
  1935. *
  1936. * The paramaters passed to the function are:
  1937. * <div class="mdetail-params"><ul>
  1938. * <li><b>key</b> : String<p class="sub-desc">The key of the item</p></li>
  1939. * <li><b>value</b> : Number<p class="sub-desc">The value of the item</p></li>
  1940. * <li><b>length</b> : Number<p class="sub-desc">The total number of items in the hash</p></li>
  1941. * </ul></div>
  1942. * @param {Function} fn The function to execute.
  1943. * @param {Object} scope The scope to execute in. Defaults to <tt>this</tt>.
  1944. * @return {Ext.util.HashMap} this
  1945. */
  1946. each: function(fn, scope) {
  1947. // copy items so they may be removed during iteration.
  1948. var items = Ext.apply({}, this.map),
  1949. key,
  1950. length = this.length;
  1951. scope = scope || this;
  1952. for (key in items) {
  1953. if (items.hasOwnProperty(key)) {
  1954. if (fn.call(scope, key, items[key], length) === false) {
  1955. break;
  1956. }
  1957. }
  1958. }
  1959. return this;
  1960. },
  1961. /**
  1962. * Performs a shallow copy on this hash.
  1963. * @return {Ext.util.HashMap} The new hash object.
  1964. */
  1965. clone: function() {
  1966. var hash = new this.self(),
  1967. map = this.map,
  1968. key;
  1969. hash.suspendEvents();
  1970. for (key in map) {
  1971. if (map.hasOwnProperty(key)) {
  1972. hash.add(key, map[key]);
  1973. }
  1974. }
  1975. hash.resumeEvents();
  1976. return hash;
  1977. },
  1978. /**
  1979. * @private
  1980. * Find the key for a value.
  1981. * @param {Object} value The value to find.
  1982. * @return {Object} The value of the item. Returns <tt>undefined</tt> if not found.
  1983. */
  1984. findKey: function(value) {
  1985. var key,
  1986. map = this.map;
  1987. for (key in map) {
  1988. if (map.hasOwnProperty(key) && map[key] === value) {
  1989. return key;
  1990. }
  1991. }
  1992. return undefined;
  1993. }
  1994. });
  1995. /**
  1996. * @class Ext.data.SortTypes
  1997. * This class defines a series of static methods that are used on a
  1998. * {@link Ext.data.Field} for performing sorting. The methods cast the
  1999. * underlying values into a data type that is appropriate for sorting on
  2000. * that particular field. If a {@link Ext.data.Field#type} is specified,
  2001. * the sortType will be set to a sane default if the sortType is not
  2002. * explicitly defined on the field. The sortType will make any necessary
  2003. * modifications to the value and return it.
  2004. * <ul>
  2005. * <li><b>asText</b> - Removes any tags and converts the value to a string</li>
  2006. * <li><b>asUCText</b> - Removes any tags and converts the value to an uppercase string</li>
  2007. * <li><b>asUCText</b> - Converts the value to an uppercase string</li>
  2008. * <li><b>asDate</b> - Converts the value into Unix epoch time</li>
  2009. * <li><b>asFloat</b> - Converts the value to a floating point number</li>
  2010. * <li><b>asInt</b> - Converts the value to an integer number</li>
  2011. * </ul>
  2012. * <p>
  2013. * It is also possible to create a custom sortType that can be used throughout
  2014. * an application.
  2015. * <pre><code>
  2016. Ext.apply(Ext.data.SortTypes, {
  2017. asPerson: function(person){
  2018. // expects an object with a first and last name property
  2019. return person.lastName.toUpperCase() + person.firstName.toLowerCase();
  2020. }
  2021. });
  2022. Ext.define('Employee', {
  2023. extend: 'Ext.data.Model',
  2024. fields: [{
  2025. name: 'person',
  2026. sortType: 'asPerson'
  2027. }, {
  2028. name: 'salary',
  2029. type: 'float' // sortType set to asFloat
  2030. }]
  2031. });
  2032. * </code></pre>
  2033. * </p>
  2034. * @singleton
  2035. * @docauthor Evan Trimboli <evan@sencha.com>
  2036. */
  2037. Ext.define('Ext.data.SortTypes', {
  2038. singleton: true,
  2039. /**
  2040. * Default sort that does nothing
  2041. * @param {Object} s The value being converted
  2042. * @return {Object} The comparison value
  2043. */
  2044. none : function(s) {
  2045. return s;
  2046. },
  2047. /**
  2048. * The regular expression used to strip tags
  2049. * @type {RegExp}
  2050. * @property
  2051. */
  2052. stripTagsRE : /<\/?[^>]+>/gi,
  2053. /**
  2054. * Strips all HTML tags to sort on text only
  2055. * @param {Object} s The value being converted
  2056. * @return {String} The comparison value
  2057. */
  2058. asText : function(s) {
  2059. return String(s).replace(this.stripTagsRE, "");
  2060. },
  2061. /**
  2062. * Strips all HTML tags to sort on text only - Case insensitive
  2063. * @param {Object} s The value being converted
  2064. * @return {String} The comparison value
  2065. */
  2066. asUCText : function(s) {
  2067. return String(s).toUpperCase().replace(this.stripTagsRE, "");
  2068. },
  2069. /**
  2070. * Case insensitive string
  2071. * @param {Object} s The value being converted
  2072. * @return {String} The comparison value
  2073. */
  2074. asUCString : function(s) {
  2075. return String(s).toUpperCase();
  2076. },
  2077. /**
  2078. * Date sorting
  2079. * @param {Object} s The value being converted
  2080. * @return {Number} The comparison value
  2081. */
  2082. asDate : function(s) {
  2083. if(!s){
  2084. return 0;
  2085. }
  2086. if(Ext.isDate(s)){
  2087. return s.getTime();
  2088. }
  2089. return Date.parse(String(s));
  2090. },
  2091. /**
  2092. * Float sorting
  2093. * @param {Object} s The value being converted
  2094. * @return {Number} The comparison value
  2095. */
  2096. asFloat : function(s) {
  2097. var val = parseFloat(String(s).replace(/,/g, ""));
  2098. return isNaN(val) ? 0 : val;
  2099. },
  2100. /**
  2101. * Integer sorting
  2102. * @param {Object} s The value being converted
  2103. * @return {Number} The comparison value
  2104. */
  2105. asInt : function(s) {
  2106. var val = parseInt(String(s).replace(/,/g, ""), 10);
  2107. return isNaN(val) ? 0 : val;
  2108. }
  2109. });
  2110. /**
  2111. * A mixin to add floating capability to a Component.
  2112. */
  2113. Ext.define('Ext.util.Floating', {
  2114. uses: ['Ext.Layer', 'Ext.window.Window'],
  2115. /**
  2116. * @cfg {Boolean} focusOnToFront
  2117. * Specifies whether the floated component should be automatically {@link Ext.Component#method-focus focused} when
  2118. * it is {@link #toFront brought to the front}.
  2119. */
  2120. focusOnToFront: true,
  2121. /**
  2122. * @cfg {String/Boolean} shadow
  2123. * Specifies whether the floating component should be given a shadow. Set to true to automatically create an
  2124. * {@link Ext.Shadow}, or a string indicating the shadow's display {@link Ext.Shadow#mode}. Set to false to
  2125. * disable the shadow.
  2126. */
  2127. shadow: 'sides',
  2128. /**
  2129. * @cfg {String/Boolean} shadowOffset
  2130. * Number of pixels to offset the shadow.
  2131. */
  2132. constructor: function (dom) {
  2133. var me = this;
  2134. me.el = new Ext.Layer(Ext.apply({
  2135. hideMode : me.hideMode,
  2136. hidden : me.hidden,
  2137. shadow : (typeof me.shadow != 'undefined') ? me.shadow : 'sides',
  2138. shadowOffset : me.shadowOffset,
  2139. constrain : false,
  2140. shim : (me.shim === false) ? false : undefined
  2141. }, me.floating), dom);
  2142. // release config object (if it was one)
  2143. me.floating = true;
  2144. // Register with the configured ownerCt.
  2145. // With this we acquire a floatParent for relative positioning, and a zIndexParent which is an
  2146. // ancestor floater which provides zIndex management.
  2147. me.registerWithOwnerCt();
  2148. },
  2149. registerWithOwnerCt: function() {
  2150. var me = this;
  2151. if (me.zIndexParent) {
  2152. me.zIndexParent.unregisterFloatingItem(me);
  2153. }
  2154. // Acquire a zIndexParent by traversing the ownerCt axis for the nearest floating ancestor
  2155. me.zIndexParent = me.up('[floating]');
  2156. me.setFloatParent(me.ownerCt);
  2157. delete me.ownerCt;
  2158. if (me.zIndexParent) {
  2159. me.zIndexParent.registerFloatingItem(me);
  2160. } else {
  2161. Ext.WindowManager.register(me);
  2162. }
  2163. },
  2164. setFloatParent: function(floatParent) {
  2165. var me = this;
  2166. // Remove listeners from previous floatParent
  2167. if (me.floatParent) {
  2168. me.mun(me.floatParent, {
  2169. hide: me.onFloatParentHide,
  2170. show: me.onFloatParentShow,
  2171. scope: me
  2172. });
  2173. }
  2174. me.floatParent = floatParent;
  2175. // Floating Components as children of Containers must hide when their parent hides.
  2176. if (floatParent) {
  2177. me.mon(me.floatParent, {
  2178. hide: me.onFloatParentHide,
  2179. show: me.onFloatParentShow,
  2180. scope: me
  2181. });
  2182. }
  2183. // If a floating Component is configured to be constrained, but has no configured
  2184. // constrainTo setting, set its constrainTo to be it's ownerCt before rendering.
  2185. if ((me.constrain || me.constrainHeader) && !me.constrainTo) {
  2186. me.constrainTo = floatParent ? floatParent.getTargetEl() : me.container;
  2187. }
  2188. },
  2189. onFloatParentHide: function() {
  2190. var me = this;
  2191. if (me.hideOnParentHide !== false && me.isVisible()) {
  2192. me.hide();
  2193. me.showOnParentShow = true;
  2194. }
  2195. },
  2196. onFloatParentShow: function() {
  2197. if (this.showOnParentShow) {
  2198. delete this.showOnParentShow;
  2199. this.show();
  2200. }
  2201. },
  2202. // private
  2203. // z-index is managed by the zIndexManager and may be overwritten at any time.
  2204. // Returns the next z-index to be used.
  2205. // If this is a Container, then it will have rebased any managed floating Components,
  2206. // and so the next available z-index will be approximately 10000 above that.
  2207. setZIndex: function(index) {
  2208. var me = this;
  2209. me.el.setZIndex(index);
  2210. // Next item goes 10 above;
  2211. index += 10;
  2212. // When a Container with floating items has its z-index set, it rebases any floating items it is managing.
  2213. // The returned value is a round number approximately 10000 above the last z-index used.
  2214. if (me.floatingItems) {
  2215. index = Math.floor(me.floatingItems.setBase(index) / 100) * 100 + 10000;
  2216. }
  2217. return index;
  2218. },
  2219. /**
  2220. * Moves this floating Component into a constrain region.
  2221. *
  2222. * By default, this Component is constrained to be within the container it was added to, or the element it was
  2223. * rendered to.
  2224. *
  2225. * An alternative constraint may be passed.
  2226. * @param {String/HTMLElement/Ext.Element/Ext.util.Region} [constrainTo] The Element or {@link Ext.util.Region Region}
  2227. * into which this Component is to be constrained. Defaults to the element into which this floating Component
  2228. * was rendered.
  2229. */
  2230. doConstrain: function(constrainTo) {
  2231. var me = this,
  2232. // Calculate the constrain vector to coerce our position to within our
  2233. // constrainTo setting. getConstrainVector will provide a default constraint
  2234. // region if there is no explicit constrainTo, *and* there is no floatParent owner Component.
  2235. vector = me.getConstrainVector(constrainTo),
  2236. xy;
  2237. if (vector) {
  2238. xy = me.getPosition();
  2239. xy[0] += vector[0];
  2240. xy[1] += vector[1];
  2241. me.setPosition(xy);
  2242. }
  2243. },
  2244. /**
  2245. * Gets the x/y offsets to constrain this float
  2246. * @private
  2247. * @param {String/HTMLElement/Ext.Element/Ext.util.Region} [constrainTo] The Element or {@link Ext.util.Region Region}
  2248. * into which this Component is to be constrained.
  2249. * @return {Number[]} The x/y constraints
  2250. */
  2251. getConstrainVector: function(constrainTo){
  2252. var me = this;
  2253. if (me.constrain || me.constrainHeader) {
  2254. constrainTo = constrainTo || (me.floatParent && me.floatParent.getTargetEl()) || me.container || me.el.getScopeParent();
  2255. return (me.constrainHeader ? me.header.el : me.el).getConstrainVector(constrainTo);
  2256. }
  2257. },
  2258. /**
  2259. * Aligns this floating Component to the specified element
  2260. *
  2261. * @param {Ext.Component/Ext.Element/HTMLElement/String} element
  2262. * The element or {@link Ext.Component} to align to. If passing a component, it must be a
  2263. * component instance. If a string id is passed, it will be used as an element id.
  2264. * @param {String} [position="tl-bl?"] The position to align to
  2265. * (see {@link Ext.Element#alignTo} for more details).
  2266. * @param {Number[]} [offsets] Offset the positioning by [x, y]
  2267. * @return {Ext.Component} this
  2268. */
  2269. alignTo: function(element, position, offsets) {
  2270. // element may be a Component, so first attempt to use its el to align to.
  2271. // When aligning to an Element's X,Y position, we must use setPagePosition which disregards any floatParent
  2272. this.setPagePosition(this.el.getAlignToXY(element.el || element, position, offsets));
  2273. return this;
  2274. },
  2275. /**
  2276. * Brings this floating Component to the front of any other visible, floating Components managed by the same
  2277. * {@link Ext.ZIndexManager ZIndexManager}
  2278. *
  2279. * If this Component is modal, inserts the modal mask just below this Component in the z-index stack.
  2280. *
  2281. * @param {Boolean} [preventFocus=false] Specify `true` to prevent the Component from being focused.
  2282. * @return {Ext.Component} this
  2283. */
  2284. toFront: function(preventFocus) {
  2285. var me = this;
  2286. // Find the floating Component which provides the base for this Component's zIndexing.
  2287. // That must move to front to then be able to rebase its zIndex stack and move this to the front
  2288. if (me.zIndexParent && me.bringParentToFront !== false) {
  2289. me.zIndexParent.toFront(true);
  2290. }
  2291. if (!Ext.isDefined(preventFocus)) {
  2292. preventFocus = !me.focusOnToFront;
  2293. }
  2294. if (preventFocus) {
  2295. me.preventFocusOnActivate = true;
  2296. }
  2297. if (me.zIndexManager.bringToFront(me)) {
  2298. if (!preventFocus) {
  2299. // Kick off a delayed focus request.
  2300. // If another floating Component is toFronted before the delay expires
  2301. // this will not receive focus.
  2302. me.focus(false, true);
  2303. }
  2304. }
  2305. delete me.preventFocusOnActivate;
  2306. return me;
  2307. },
  2308. /**
  2309. * This method is called internally by {@link Ext.ZIndexManager} to signal that a floating Component has either been
  2310. * moved to the top of its zIndex stack, or pushed from the top of its zIndex stack.
  2311. *
  2312. * If a _Window_ is superceded by another Window, deactivating it hides its shadow.
  2313. *
  2314. * This method also fires the {@link Ext.Component#activate activate} or
  2315. * {@link Ext.Component#deactivate deactivate} event depending on which action occurred.
  2316. *
  2317. * @param {Boolean} [active=false] True to activate the Component, false to deactivate it.
  2318. * @param {Ext.Component} [newActive] The newly active Component which is taking over topmost zIndex position.
  2319. */
  2320. setActive: function(active, newActive) {
  2321. var me = this;
  2322. if (active) {
  2323. if (me.el.shadow && !me.maximized) {
  2324. me.el.enableShadow(true);
  2325. }
  2326. if (me.modal && !me.preventFocusOnActivate) {
  2327. me.focus(false, true);
  2328. }
  2329. me.fireEvent('activate', me);
  2330. } else {
  2331. // Only the *Windows* in a zIndex stack share a shadow. All other types of floaters
  2332. // can keep their shadows all the time
  2333. if (me.isWindow && (newActive && newActive.isWindow)) {
  2334. me.el.disableShadow();
  2335. }
  2336. me.fireEvent('deactivate', me);
  2337. }
  2338. },
  2339. /**
  2340. * Sends this Component to the back of (lower z-index than) any other visible windows
  2341. * @return {Ext.Component} this
  2342. */
  2343. toBack: function() {
  2344. this.zIndexManager.sendToBack(this);
  2345. return this;
  2346. },
  2347. /**
  2348. * Center this Component in its container.
  2349. * @return {Ext.Component} this
  2350. */
  2351. center: function() {
  2352. var me = this,
  2353. xy;
  2354. if (me.isVisible()) {
  2355. xy = me.el.getAlignToXY(me.container, 'c-c');
  2356. me.setPagePosition(xy);
  2357. } else {
  2358. me.needsCenter = true;
  2359. }
  2360. return me;
  2361. },
  2362. onFloatShow: function(){
  2363. if (this.needsCenter) {
  2364. this.center();
  2365. }
  2366. delete this.needsCenter;
  2367. },
  2368. // private
  2369. syncShadow : function(){
  2370. if (this.floating) {
  2371. this.el.sync(true);
  2372. }
  2373. },
  2374. // private
  2375. fitContainer: function() {
  2376. var parent = this.floatParent,
  2377. container = parent ? parent.getTargetEl() : this.container,
  2378. size = container.getViewSize(false);
  2379. this.setSize(size);
  2380. }
  2381. });
  2382. /**
  2383. * The Connection class encapsulates a connection to the page's originating domain, allowing requests to be made either
  2384. * to a configured URL, or to a URL specified at request time.
  2385. *
  2386. * Requests made by this class are asynchronous, and will return immediately. No data from the server will be available
  2387. * to the statement immediately following the {@link #request} call. To process returned data, use a success callback
  2388. * in the request options object, or an {@link #requestcomplete event listener}.
  2389. *
  2390. * # File Uploads
  2391. *
  2392. * File uploads are not performed using normal "Ajax" techniques, that is they are not performed using XMLHttpRequests.
  2393. * Instead the form is submitted in the standard manner with the DOM &lt;form&gt; element temporarily modified to have its
  2394. * target set to refer to a dynamically generated, hidden &lt;iframe&gt; which is inserted into the document but removed
  2395. * after the return data has been gathered.
  2396. *
  2397. * The server response is parsed by the browser to create the document for the IFRAME. If the server is using JSON to
  2398. * send the return object, then the Content-Type header must be set to "text/html" in order to tell the browser to
  2399. * insert the text unchanged into the document body.
  2400. *
  2401. * Characters which are significant to an HTML parser must be sent as HTML entities, so encode `<` as `&lt;`, `&` as
  2402. * `&amp;` etc.
  2403. *
  2404. * The response text is retrieved from the document, and a fake XMLHttpRequest object is created containing a
  2405. * responseText property in order to conform to the requirements of event handlers and callbacks.
  2406. *
  2407. * Be aware that file upload packets are sent with the content type multipart/form and some server technologies
  2408. * (notably JEE) may require some custom processing in order to retrieve parameter names and parameter values from the
  2409. * packet content.
  2410. *
  2411. * Also note that it's not possible to check the response code of the hidden iframe, so the success handler will ALWAYS fire.
  2412. */
  2413. Ext.define('Ext.data.Connection', {
  2414. mixins: {
  2415. observable: 'Ext.util.Observable'
  2416. },
  2417. statics: {
  2418. requestId: 0
  2419. },
  2420. url: null,
  2421. async: true,
  2422. method: null,
  2423. username: '',
  2424. password: '',
  2425. /**
  2426. * @cfg {Boolean} disableCaching
  2427. * True to add a unique cache-buster param to GET requests.
  2428. */
  2429. disableCaching: true,
  2430. /**
  2431. * @cfg {Boolean} withCredentials
  2432. * True to set `withCredentials = true` on the XHR object
  2433. */
  2434. withCredentials: false,
  2435. /**
  2436. * @cfg {Boolean} cors
  2437. * True to enable CORS support on the XHR object. Currently the only effect of this option
  2438. * is to use the XDomainRequest object instead of XMLHttpRequest if the browser is IE8 or above.
  2439. */
  2440. cors: false,
  2441. /**
  2442. * @cfg {String} disableCachingParam
  2443. * Change the parameter which is sent went disabling caching through a cache buster.
  2444. */
  2445. disableCachingParam: '_dc',
  2446. /**
  2447. * @cfg {Number} timeout
  2448. * The timeout in milliseconds to be used for requests.
  2449. */
  2450. timeout : 30000,
  2451. /**
  2452. * @cfg {Object} extraParams
  2453. * Any parameters to be appended to the request.
  2454. */
  2455. useDefaultHeader : true,
  2456. defaultPostHeader : 'application/x-www-form-urlencoded; charset=UTF-8',
  2457. useDefaultXhrHeader : true,
  2458. defaultXhrHeader : 'XMLHttpRequest',
  2459. constructor : function(config) {
  2460. config = config || {};
  2461. Ext.apply(this, config);
  2462. /**
  2463. * @event beforerequest
  2464. * Fires before a network request is made to retrieve a data object.
  2465. * @param {Ext.data.Connection} conn This Connection object.
  2466. * @param {Object} options The options config object passed to the {@link #request} method.
  2467. */
  2468. /**
  2469. * @event requestcomplete
  2470. * Fires if the request was successfully completed.
  2471. * @param {Ext.data.Connection} conn This Connection object.
  2472. * @param {Object} response The XHR object containing the response data.
  2473. * See [The XMLHttpRequest Object](http://www.w3.org/TR/XMLHttpRequest/) for details.
  2474. * @param {Object} options The options config object passed to the {@link #request} method.
  2475. */
  2476. /**
  2477. * @event requestexception
  2478. * Fires if an error HTTP status was returned from the server.
  2479. * See [HTTP Status Code Definitions](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)
  2480. * for details of HTTP status codes.
  2481. * @param {Ext.data.Connection} conn This Connection object.
  2482. * @param {Object} response The XHR object containing the response data.
  2483. * See [The XMLHttpRequest Object](http://www.w3.org/TR/XMLHttpRequest/) for details.
  2484. * @param {Object} options The options config object passed to the {@link #request} method.
  2485. */
  2486. this.requests = {};
  2487. this.mixins.observable.constructor.call(this);
  2488. },
  2489. /**
  2490. * Sends an HTTP request to a remote server.
  2491. *
  2492. * **Important:** Ajax server requests are asynchronous, and this call will
  2493. * return before the response has been received. Process any returned data
  2494. * in a callback function.
  2495. *
  2496. * Ext.Ajax.request({
  2497. * url: 'ajax_demo/sample.json',
  2498. * success: function(response, opts) {
  2499. * var obj = Ext.decode(response.responseText);
  2500. * console.dir(obj);
  2501. * },
  2502. * failure: function(response, opts) {
  2503. * console.log('server-side failure with status code ' + response.status);
  2504. * }
  2505. * });
  2506. *
  2507. * To execute a callback function in the correct scope, use the `scope` option.
  2508. *
  2509. * @param {Object} options An object which may contain the following properties:
  2510. *
  2511. * (The options object may also contain any other property which might be needed to perform
  2512. * postprocessing in a callback because it is passed to callback functions.)
  2513. *
  2514. * @param {String/Function} options.url The URL to which to send the request, or a function
  2515. * to call which returns a URL string. The scope of the function is specified by the `scope` option.
  2516. * Defaults to the configured `url`.
  2517. *
  2518. * @param {Object/String/Function} options.params An object containing properties which are
  2519. * used as parameters to the request, a url encoded string or a function to call to get either. The scope
  2520. * of the function is specified by the `scope` option.
  2521. *
  2522. * @param {String} options.method The HTTP method to use
  2523. * for the request. Defaults to the configured method, or if no method was configured,
  2524. * "GET" if no parameters are being sent, and "POST" if parameters are being sent. Note that
  2525. * the method name is case-sensitive and should be all caps.
  2526. *
  2527. * @param {Function} options.callback The function to be called upon receipt of the HTTP response.
  2528. * The callback is called regardless of success or failure and is passed the following parameters:
  2529. * @param {Object} options.callback.options The parameter to the request call.
  2530. * @param {Boolean} options.callback.success True if the request succeeded.
  2531. * @param {Object} options.callback.response The XMLHttpRequest object containing the response data.
  2532. * See [www.w3.org/TR/XMLHttpRequest/](http://www.w3.org/TR/XMLHttpRequest/) for details about
  2533. * accessing elements of the response.
  2534. *
  2535. * @param {Function} options.success The function to be called upon success of the request.
  2536. * The callback is passed the following parameters:
  2537. * @param {Object} options.success.response The XMLHttpRequest object containing the response data.
  2538. * @param {Object} options.success.options The parameter to the request call.
  2539. *
  2540. * @param {Function} options.failure The function to be called upon success of the request.
  2541. * The callback is passed the following parameters:
  2542. * @param {Object} options.failure.response The XMLHttpRequest object containing the response data.
  2543. * @param {Object} options.failure.options The parameter to the request call.
  2544. *
  2545. * @param {Object} options.scope The scope in which to execute the callbacks: The "this" object for
  2546. * the callback function. If the `url`, or `params` options were specified as functions from which to
  2547. * draw values, then this also serves as the scope for those function calls. Defaults to the browser
  2548. * window.
  2549. *
  2550. * @param {Number} options.timeout The timeout in milliseconds to be used for this request.
  2551. * Defaults to 30 seconds.
  2552. *
  2553. * @param {Ext.Element/HTMLElement/String} options.form The `<form>` Element or the id of the `<form>`
  2554. * to pull parameters from.
  2555. *
  2556. * @param {Boolean} options.isUpload **Only meaningful when used with the `form` option.**
  2557. *
  2558. * True if the form object is a file upload (will be set automatically if the form was configured
  2559. * with **`enctype`** `"multipart/form-data"`).
  2560. *
  2561. * File uploads are not performed using normal "Ajax" techniques, that is they are **not**
  2562. * performed using XMLHttpRequests. Instead the form is submitted in the standard manner with the
  2563. * DOM `<form>` element temporarily modified to have its [target][] set to refer to a dynamically
  2564. * generated, hidden `<iframe>` which is inserted into the document but removed after the return data
  2565. * has been gathered.
  2566. *
  2567. * The server response is parsed by the browser to create the document for the IFRAME. If the
  2568. * server is using JSON to send the return object, then the [Content-Type][] header must be set to
  2569. * "text/html" in order to tell the browser to insert the text unchanged into the document body.
  2570. *
  2571. * The response text is retrieved from the document, and a fake XMLHttpRequest object is created
  2572. * containing a `responseText` property in order to conform to the requirements of event handlers
  2573. * and callbacks.
  2574. *
  2575. * Be aware that file upload packets are sent with the content type [multipart/form][] and some server
  2576. * technologies (notably JEE) may require some custom processing in order to retrieve parameter names
  2577. * and parameter values from the packet content.
  2578. *
  2579. * [target]: http://www.w3.org/TR/REC-html40/present/frames.html#adef-target
  2580. * [Content-Type]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17
  2581. * [multipart/form]: http://www.faqs.org/rfcs/rfc2388.html
  2582. *
  2583. * @param {Object} options.headers Request headers to set for the request.
  2584. *
  2585. * @param {Object} options.xmlData XML document to use for the post. Note: This will be used instead
  2586. * of params for the post data. Any params will be appended to the URL.
  2587. *
  2588. * @param {Object/String} options.jsonData JSON data to use as the post. Note: This will be used
  2589. * instead of params for the post data. Any params will be appended to the URL.
  2590. *
  2591. * @param {Boolean} options.disableCaching True to add a unique cache-buster param to GET requests.
  2592. *
  2593. * @param {Boolean} options.withCredentials True to add the withCredentials property to the XHR object
  2594. *
  2595. * @return {Object} The request object. This may be used to cancel the request.
  2596. */
  2597. request : function(options) {
  2598. options = options || {};
  2599. var me = this,
  2600. scope = options.scope || window,
  2601. username = options.username || me.username,
  2602. password = options.password || me.password || '',
  2603. async,
  2604. requestOptions,
  2605. request,
  2606. headers,
  2607. xhr;
  2608. if (me.fireEvent('beforerequest', me, options) !== false) {
  2609. requestOptions = me.setOptions(options, scope);
  2610. if (me.isFormUpload(options)) {
  2611. me.upload(options.form, requestOptions.url, requestOptions.data, options);
  2612. return null;
  2613. }
  2614. // if autoabort is set, cancel the current transactions
  2615. if (options.autoAbort || me.autoAbort) {
  2616. me.abort();
  2617. }
  2618. // create a connection object
  2619. async = options.async !== false ? (options.async || me.async) : false;
  2620. xhr = me.openRequest(options, requestOptions, async, username, password);
  2621. headers = me.setupHeaders(xhr, options, requestOptions.data, requestOptions.params);
  2622. // create the transaction object
  2623. request = {
  2624. id: ++Ext.data.Connection.requestId,
  2625. xhr: xhr,
  2626. headers: headers,
  2627. options: options,
  2628. async: async,
  2629. timeout: setTimeout(function() {
  2630. request.timedout = true;
  2631. me.abort(request);
  2632. }, options.timeout || me.timeout)
  2633. };
  2634. me.requests[request.id] = request;
  2635. me.latestId = request.id;
  2636. // bind our statechange listener
  2637. if (async) {
  2638. xhr.onreadystatechange = Ext.Function.bind(me.onStateChange, me, [request]);
  2639. }
  2640. // start the request!
  2641. xhr.send(requestOptions.data);
  2642. if (!async) {
  2643. return me.onComplete(request);
  2644. }
  2645. return request;
  2646. } else {
  2647. Ext.callback(options.callback, options.scope, [options, undefined, undefined]);
  2648. return null;
  2649. }
  2650. },
  2651. /**
  2652. * Uploads a form using a hidden iframe.
  2653. * @param {String/HTMLElement/Ext.Element} form The form to upload
  2654. * @param {String} url The url to post to
  2655. * @param {String} params Any extra parameters to pass
  2656. * @param {Object} options The initial options
  2657. */
  2658. upload: function(form, url, params, options) {
  2659. form = Ext.getDom(form);
  2660. options = options || {};
  2661. var id = Ext.id(),
  2662. frame = document.createElement('iframe'),
  2663. hiddens = [],
  2664. encoding = 'multipart/form-data',
  2665. buf = {
  2666. target: form.target,
  2667. method: form.method,
  2668. encoding: form.encoding,
  2669. enctype: form.enctype,
  2670. action: form.action
  2671. },
  2672. addField = function(name, value) {
  2673. hiddenItem = document.createElement('input');
  2674. Ext.fly(hiddenItem).set({
  2675. type: 'hidden',
  2676. value: value,
  2677. name: name
  2678. });
  2679. form.appendChild(hiddenItem);
  2680. hiddens.push(hiddenItem);
  2681. },
  2682. hiddenItem, obj, value, name, vLen, v, hLen, h;
  2683. /*
  2684. * Originally this behaviour was modified for Opera 10 to apply the secure URL after
  2685. * the frame had been added to the document. It seems this has since been corrected in
  2686. * Opera so the behaviour has been reverted, the URL will be set before being added.
  2687. */
  2688. Ext.fly(frame).set({
  2689. id: id,
  2690. name: id,
  2691. cls: Ext.baseCSSPrefix + 'hide-display',
  2692. src: Ext.SSL_SECURE_URL
  2693. });
  2694. document.body.appendChild(frame);
  2695. // This is required so that IE doesn't pop the response up in a new window.
  2696. if (document.frames) {
  2697. document.frames[id].name = id;
  2698. }
  2699. Ext.fly(form).set({
  2700. target: id,
  2701. method: 'POST',
  2702. enctype: encoding,
  2703. encoding: encoding,
  2704. action: url || buf.action
  2705. });
  2706. // add dynamic params
  2707. if (params) {
  2708. obj = Ext.Object.fromQueryString(params) || {};
  2709. for (name in obj) {
  2710. value = obj[name];
  2711. if (obj.hasOwnProperty(value)) {
  2712. if (Ext.isArray(value)) {
  2713. vLen = value.length;
  2714. for (v = 0; v < vLen; v++) {
  2715. addField(name, value[v]);
  2716. }
  2717. } else {
  2718. addField(name, value);
  2719. }
  2720. }
  2721. }
  2722. }
  2723. Ext.fly(frame).on('load', Ext.Function.bind(this.onUploadComplete, this, [frame, options]), null, {single: true});
  2724. form.submit();
  2725. Ext.fly(form).set(buf);
  2726. hLen = hiddens.length;
  2727. for (h = 0; h < hLen; h++) {
  2728. Ext.removeNode(hiddens[h]);
  2729. }
  2730. },
  2731. /**
  2732. * @private
  2733. * Callback handler for the upload function. After we've submitted the form via the iframe this creates a bogus
  2734. * response object to simulate an XHR and populates its responseText from the now-loaded iframe's document body
  2735. * (or a textarea inside the body). We then clean up by removing the iframe
  2736. */
  2737. onUploadComplete: function(frame, options) {
  2738. var me = this,
  2739. // bogus response object
  2740. response = {
  2741. responseText: '',
  2742. responseXML: null
  2743. }, doc, firstChild;
  2744. try {
  2745. doc = frame.contentWindow.document || frame.contentDocument || window.frames[frame.id].document;
  2746. if (doc) {
  2747. if (doc.body) {
  2748. if (/textarea/i.test((firstChild = doc.body.firstChild || {}).tagName)) { // json response wrapped in textarea
  2749. response.responseText = firstChild.value;
  2750. } else {
  2751. response.responseText = doc.body.innerHTML;
  2752. }
  2753. }
  2754. //in IE the document may still have a body even if returns XML.
  2755. response.responseXML = doc.XMLDocument || doc;
  2756. }
  2757. } catch (e) {
  2758. }
  2759. me.fireEvent('requestcomplete', me, response, options);
  2760. Ext.callback(options.success, options.scope, [response, options]);
  2761. Ext.callback(options.callback, options.scope, [options, true, response]);
  2762. setTimeout(function() {
  2763. Ext.removeNode(frame);
  2764. }, 100);
  2765. },
  2766. /**
  2767. * Detects whether the form is intended to be used for an upload.
  2768. * @private
  2769. */
  2770. isFormUpload: function(options) {
  2771. var form = this.getForm(options);
  2772. if (form) {
  2773. return (options.isUpload || (/multipart\/form-data/i).test(form.getAttribute('enctype')));
  2774. }
  2775. return false;
  2776. },
  2777. /**
  2778. * Gets the form object from options.
  2779. * @private
  2780. * @param {Object} options The request options
  2781. * @return {HTMLElement} The form, null if not passed
  2782. */
  2783. getForm: function(options) {
  2784. return Ext.getDom(options.form) || null;
  2785. },
  2786. /**
  2787. * Sets various options such as the url, params for the request
  2788. * @param {Object} options The initial options
  2789. * @param {Object} scope The scope to execute in
  2790. * @return {Object} The params for the request
  2791. */
  2792. setOptions: function(options, scope) {
  2793. var me = this,
  2794. params = options.params || {},
  2795. extraParams = me.extraParams,
  2796. urlParams = options.urlParams,
  2797. url = options.url || me.url,
  2798. jsonData = options.jsonData,
  2799. method,
  2800. disableCache,
  2801. data;
  2802. // allow params to be a method that returns the params object
  2803. if (Ext.isFunction(params)) {
  2804. params = params.call(scope, options);
  2805. }
  2806. // allow url to be a method that returns the actual url
  2807. if (Ext.isFunction(url)) {
  2808. url = url.call(scope, options);
  2809. }
  2810. url = this.setupUrl(options, url);
  2811. if (!url) {
  2812. Ext.Error.raise({
  2813. options: options,
  2814. msg: 'No URL specified'
  2815. });
  2816. }
  2817. // check for xml or json data, and make sure json data is encoded
  2818. data = options.rawData || options.xmlData || jsonData || null;
  2819. if (jsonData && !Ext.isPrimitive(jsonData)) {
  2820. data = Ext.encode(data);
  2821. }
  2822. // make sure params are a url encoded string and include any extraParams if specified
  2823. if (Ext.isObject(params)) {
  2824. params = Ext.Object.toQueryString(params);
  2825. }
  2826. if (Ext.isObject(extraParams)) {
  2827. extraParams = Ext.Object.toQueryString(extraParams);
  2828. }
  2829. params = params + ((extraParams) ? ((params) ? '&' : '') + extraParams : '');
  2830. urlParams = Ext.isObject(urlParams) ? Ext.Object.toQueryString(urlParams) : urlParams;
  2831. params = this.setupParams(options, params);
  2832. // decide the proper method for this request
  2833. method = (options.method || me.method || ((params || data) ? 'POST' : 'GET')).toUpperCase();
  2834. this.setupMethod(options, method);
  2835. disableCache = options.disableCaching !== false ? (options.disableCaching || me.disableCaching) : false;
  2836. // if the method is get append date to prevent caching
  2837. if (method === 'GET' && disableCache) {
  2838. url = Ext.urlAppend(url, (options.disableCachingParam || me.disableCachingParam) + '=' + (new Date().getTime()));
  2839. }
  2840. // if the method is get or there is json/xml data append the params to the url
  2841. if ((method == 'GET' || data) && params) {
  2842. url = Ext.urlAppend(url, params);
  2843. params = null;
  2844. }
  2845. // allow params to be forced into the url
  2846. if (urlParams) {
  2847. url = Ext.urlAppend(url, urlParams);
  2848. }
  2849. return {
  2850. url: url,
  2851. method: method,
  2852. data: data || params || null
  2853. };
  2854. },
  2855. /**
  2856. * Template method for overriding url
  2857. * @template
  2858. * @private
  2859. * @param {Object} options
  2860. * @param {String} url
  2861. * @return {String} The modified url
  2862. */
  2863. setupUrl: function(options, url) {
  2864. var form = this.getForm(options);
  2865. if (form) {
  2866. url = url || form.action;
  2867. }
  2868. return url;
  2869. },
  2870. /**
  2871. * Template method for overriding params
  2872. * @template
  2873. * @private
  2874. * @param {Object} options
  2875. * @param {String} params
  2876. * @return {String} The modified params
  2877. */
  2878. setupParams: function(options, params) {
  2879. var form = this.getForm(options),
  2880. serializedForm;
  2881. if (form && !this.isFormUpload(options)) {
  2882. serializedForm = Ext.Element.serializeForm(form);
  2883. params = params ? (params + '&' + serializedForm) : serializedForm;
  2884. }
  2885. return params;
  2886. },
  2887. /**
  2888. * Template method for overriding method
  2889. * @template
  2890. * @private
  2891. * @param {Object} options
  2892. * @param {String} method
  2893. * @return {String} The modified method
  2894. */
  2895. setupMethod: function(options, method) {
  2896. if (this.isFormUpload(options)) {
  2897. return 'POST';
  2898. }
  2899. return method;
  2900. },
  2901. /**
  2902. * Setup all the headers for the request
  2903. * @private
  2904. * @param {Object} xhr The xhr object
  2905. * @param {Object} options The options for the request
  2906. * @param {Object} data The data for the request
  2907. * @param {Object} params The params for the request
  2908. */
  2909. setupHeaders: function(xhr, options, data, params) {
  2910. var me = this,
  2911. headers = Ext.apply({}, options.headers || {}, me.defaultHeaders || {}),
  2912. contentType = me.defaultPostHeader,
  2913. jsonData = options.jsonData,
  2914. xmlData = options.xmlData,
  2915. key,
  2916. header;
  2917. if (!headers['Content-Type'] && (data || params)) {
  2918. if (data) {
  2919. if (options.rawData) {
  2920. contentType = 'text/plain';
  2921. } else {
  2922. if (xmlData && Ext.isDefined(xmlData)) {
  2923. contentType = 'text/xml';
  2924. } else if (jsonData && Ext.isDefined(jsonData)) {
  2925. contentType = 'application/json';
  2926. }
  2927. }
  2928. }
  2929. headers['Content-Type'] = contentType;
  2930. }
  2931. if (me.useDefaultXhrHeader && !headers['X-Requested-With']) {
  2932. headers['X-Requested-With'] = me.defaultXhrHeader;
  2933. }
  2934. // set up all the request headers on the xhr object
  2935. try {
  2936. for (key in headers) {
  2937. if (headers.hasOwnProperty(key)) {
  2938. header = headers[key];
  2939. xhr.setRequestHeader(key, header);
  2940. }
  2941. }
  2942. } catch(e) {
  2943. me.fireEvent('exception', key, header);
  2944. }
  2945. return headers;
  2946. },
  2947. /**
  2948. * Creates the appropriate XHR transport for a given request on this browser. On IE
  2949. * this may be an `XDomainRequest` rather than an `XMLHttpRequest`.
  2950. * @private
  2951. */
  2952. newRequest: function (options) {
  2953. var xhr;
  2954. if ((options.cors || this.cors) && Ext.isIE && Ext.ieVersion >= 8) {
  2955. xhr = new XDomainRequest();
  2956. } else {
  2957. xhr = this.getXhrInstance();
  2958. }
  2959. return xhr;
  2960. },
  2961. /**
  2962. * Creates and opens an appropriate XHR transport for a given request on this browser.
  2963. * This logic is contained in an individual method to allow for overrides to process all
  2964. * of the parameters and options and return a suitable, open connection.
  2965. * @private
  2966. */
  2967. openRequest: function (options, requestOptions, async, username, password) {
  2968. var xhr = this.newRequest(options);
  2969. if (username) {
  2970. xhr.open(requestOptions.method, requestOptions.url, async, username, password);
  2971. } else {
  2972. xhr.open(requestOptions.method, requestOptions.url, async);
  2973. }
  2974. if (options.withCredentials || this.withCredentials) {
  2975. xhr.withCredentials = true;
  2976. }
  2977. return xhr;
  2978. },
  2979. /**
  2980. * Creates the appropriate XHR transport for this browser.
  2981. * @private
  2982. */
  2983. getXhrInstance: (function() {
  2984. var options = [function() {
  2985. return new XMLHttpRequest();
  2986. }, function() {
  2987. return new ActiveXObject('MSXML2.XMLHTTP.3.0');
  2988. }, function() {
  2989. return new ActiveXObject('MSXML2.XMLHTTP');
  2990. }, function() {
  2991. return new ActiveXObject('Microsoft.XMLHTTP');
  2992. }], i = 0,
  2993. len = options.length,
  2994. xhr;
  2995. for (; i < len; ++i) {
  2996. try {
  2997. xhr = options[i];
  2998. xhr();
  2999. break;
  3000. } catch(e) {
  3001. }
  3002. }
  3003. return xhr;
  3004. })(),
  3005. /**
  3006. * Determines whether this object has a request outstanding.
  3007. * @param {Object} [request] Defaults to the last transaction
  3008. * @return {Boolean} True if there is an outstanding request.
  3009. */
  3010. isLoading : function(request) {
  3011. if (!request) {
  3012. request = this.getLatest();
  3013. }
  3014. if (!(request && request.xhr)) {
  3015. return false;
  3016. }
  3017. // if there is a connection and readyState is not 0 or 4
  3018. var state = request.xhr.readyState;
  3019. return !(state === 0 || state == 4);
  3020. },
  3021. /**
  3022. * Aborts an active request.
  3023. * @param {Object} [request] Defaults to the last request
  3024. */
  3025. abort : function(request) {
  3026. var me = this,
  3027. xhr;
  3028. if (!request) {
  3029. request = me.getLatest();
  3030. }
  3031. if (request && me.isLoading(request)) {
  3032. /*
  3033. * Clear out the onreadystatechange here, this allows us
  3034. * greater control, the browser may/may not fire the function
  3035. * depending on a series of conditions.
  3036. */
  3037. xhr = request.xhr;
  3038. try {
  3039. xhr.onreadystatechange = null;
  3040. } catch (e) {
  3041. // Setting onreadystatechange to null can cause problems in IE, see
  3042. // http://www.quirksmode.org/blog/archives/2005/09/xmlhttp_notes_a_1.html
  3043. xhr = Ext.emptyFn;
  3044. }
  3045. xhr.abort();
  3046. me.clearTimeout(request);
  3047. if (!request.timedout) {
  3048. request.aborted = true;
  3049. }
  3050. me.onComplete(request);
  3051. me.cleanup(request);
  3052. }
  3053. },
  3054. /**
  3055. * Aborts all active requests
  3056. */
  3057. abortAll: function(){
  3058. var requests = this.requests,
  3059. id;
  3060. for (id in requests) {
  3061. if (requests.hasOwnProperty(id)) {
  3062. this.abort(requests[id]);
  3063. }
  3064. }
  3065. },
  3066. /**
  3067. * Gets the most recent request
  3068. * @private
  3069. * @return {Object} The request. Null if there is no recent request
  3070. */
  3071. getLatest: function(){
  3072. var id = this.latestId,
  3073. request;
  3074. if (id) {
  3075. request = this.requests[id];
  3076. }
  3077. return request || null;
  3078. },
  3079. /**
  3080. * Fires when the state of the xhr changes
  3081. * @private
  3082. * @param {Object} request The request
  3083. */
  3084. onStateChange : function(request) {
  3085. if (request.xhr.readyState == 4) {
  3086. this.clearTimeout(request);
  3087. this.onComplete(request);
  3088. this.cleanup(request);
  3089. }
  3090. },
  3091. /**
  3092. * Clears the timeout on the request
  3093. * @private
  3094. * @param {Object} The request
  3095. */
  3096. clearTimeout: function(request) {
  3097. clearTimeout(request.timeout);
  3098. delete request.timeout;
  3099. },
  3100. /**
  3101. * Cleans up any left over information from the request
  3102. * @private
  3103. * @param {Object} The request
  3104. */
  3105. cleanup: function(request) {
  3106. request.xhr = null;
  3107. delete request.xhr;
  3108. },
  3109. /**
  3110. * To be called when the request has come back from the server
  3111. * @private
  3112. * @param {Object} request
  3113. * @return {Object} The response
  3114. */
  3115. onComplete : function(request) {
  3116. var me = this,
  3117. options = request.options,
  3118. result,
  3119. success,
  3120. response;
  3121. try {
  3122. result = me.parseStatus(request.xhr.status);
  3123. } catch (e) {
  3124. // in some browsers we can't access the status if the readyState is not 4, so the request has failed
  3125. result = {
  3126. success : false,
  3127. isException : false
  3128. };
  3129. }
  3130. success = result.success;
  3131. if (success) {
  3132. response = me.createResponse(request);
  3133. me.fireEvent('requestcomplete', me, response, options);
  3134. Ext.callback(options.success, options.scope, [response, options]);
  3135. } else {
  3136. if (result.isException || request.aborted || request.timedout) {
  3137. response = me.createException(request);
  3138. } else {
  3139. response = me.createResponse(request);
  3140. }
  3141. me.fireEvent('requestexception', me, response, options);
  3142. Ext.callback(options.failure, options.scope, [response, options]);
  3143. }
  3144. Ext.callback(options.callback, options.scope, [options, success, response]);
  3145. delete me.requests[request.id];
  3146. return response;
  3147. },
  3148. /**
  3149. * Checks if the response status was successful
  3150. * @param {Number} status The status code
  3151. * @return {Object} An object containing success/status state
  3152. */
  3153. parseStatus: function(status) {
  3154. // see: https://prototype.lighthouseapp.com/projects/8886/tickets/129-ie-mangles-http-response-status-code-204-to-1223
  3155. status = status == 1223 ? 204 : status;
  3156. var success = (status >= 200 && status < 300) || status == 304,
  3157. isException = false;
  3158. if (!success) {
  3159. switch (status) {
  3160. case 12002:
  3161. case 12029:
  3162. case 12030:
  3163. case 12031:
  3164. case 12152:
  3165. case 13030:
  3166. isException = true;
  3167. break;
  3168. }
  3169. }
  3170. return {
  3171. success: success,
  3172. isException: isException
  3173. };
  3174. },
  3175. /**
  3176. * Creates the response object
  3177. * @private
  3178. * @param {Object} request
  3179. */
  3180. createResponse : function(request) {
  3181. var xhr = request.xhr,
  3182. headers = {},
  3183. lines = xhr.getAllResponseHeaders().replace(/\r\n/g, '\n').split('\n'),
  3184. count = lines.length,
  3185. line, index, key, value, response;
  3186. while (count--) {
  3187. line = lines[count];
  3188. index = line.indexOf(':');
  3189. if (index >= 0) {
  3190. key = line.substr(0, index).toLowerCase();
  3191. if (line.charAt(index + 1) == ' ') {
  3192. ++index;
  3193. }
  3194. headers[key] = line.substr(index + 1);
  3195. }
  3196. }
  3197. request.xhr = null;
  3198. delete request.xhr;
  3199. response = {
  3200. request: request,
  3201. requestId : request.id,
  3202. status : xhr.status,
  3203. statusText : xhr.statusText,
  3204. getResponseHeader : function(header) {
  3205. return headers[header.toLowerCase()];
  3206. },
  3207. getAllResponseHeaders : function() {
  3208. return headers;
  3209. },
  3210. responseText : xhr.responseText,
  3211. responseXML : xhr.responseXML
  3212. };
  3213. // If we don't explicitly tear down the xhr reference, IE6/IE7 will hold this in the closure of the
  3214. // functions created with getResponseHeader/getAllResponseHeaders
  3215. xhr = null;
  3216. return response;
  3217. },
  3218. /**
  3219. * Creates the exception object
  3220. * @private
  3221. * @param {Object} request
  3222. */
  3223. createException : function(request) {
  3224. return {
  3225. request : request,
  3226. requestId : request.id,
  3227. status : request.aborted ? -1 : 0,
  3228. statusText : request.aborted ? 'transaction aborted' : 'communication failure',
  3229. aborted: request.aborted,
  3230. timedout: request.timedout
  3231. };
  3232. }
  3233. });
  3234. /**
  3235. * Represents a filter that can be applied to a {@link Ext.util.MixedCollection MixedCollection}. Can either simply
  3236. * filter on a property/value pair or pass in a filter function with custom logic. Filters are always used in the
  3237. * context of MixedCollections, though {@link Ext.data.Store Store}s frequently create them when filtering and searching
  3238. * on their records. Example usage:
  3239. *
  3240. * //set up a fictional MixedCollection containing a few people to filter on
  3241. * var allNames = new Ext.util.MixedCollection();
  3242. * allNames.addAll([
  3243. * {id: 1, name: 'Ed', age: 25},
  3244. * {id: 2, name: 'Jamie', age: 37},
  3245. * {id: 3, name: 'Abe', age: 32},
  3246. * {id: 4, name: 'Aaron', age: 26},
  3247. * {id: 5, name: 'David', age: 32}
  3248. * ]);
  3249. *
  3250. * var ageFilter = new Ext.util.Filter({
  3251. * property: 'age',
  3252. * value : 32
  3253. * });
  3254. *
  3255. * var longNameFilter = new Ext.util.Filter({
  3256. * filterFn: function(item) {
  3257. * return item.name.length > 4;
  3258. * }
  3259. * });
  3260. *
  3261. * //a new MixedCollection with the 3 names longer than 4 characters
  3262. * var longNames = allNames.filter(longNameFilter);
  3263. *
  3264. * //a new MixedCollection with the 2 people of age 24:
  3265. * var youngFolk = allNames.filter(ageFilter);
  3266. *
  3267. */
  3268. Ext.define('Ext.util.Filter', {
  3269. /* Begin Definitions */
  3270. /* End Definitions */
  3271. /**
  3272. * @cfg {String} property
  3273. * The property to filter on. Required unless a {@link #filterFn} is passed
  3274. */
  3275. /**
  3276. * @cfg {Function} filterFn
  3277. * A custom filter function which is passed each item in the {@link Ext.util.MixedCollection} in turn. Should return
  3278. * true to accept each item or false to reject it
  3279. */
  3280. /**
  3281. * @cfg {Boolean} anyMatch
  3282. * True to allow any match - no regex start/end line anchors will be added.
  3283. */
  3284. anyMatch: false,
  3285. /**
  3286. * @cfg {Boolean} exactMatch
  3287. * True to force exact match (^ and $ characters added to the regex). Ignored if anyMatch is true.
  3288. */
  3289. exactMatch: false,
  3290. /**
  3291. * @cfg {Boolean} caseSensitive
  3292. * True to make the regex case sensitive (adds 'i' switch to regex).
  3293. */
  3294. caseSensitive: false,
  3295. /**
  3296. * @cfg {String} root
  3297. * Optional root property. This is mostly useful when filtering a Store, in which case we set the root to 'data' to
  3298. * make the filter pull the {@link #property} out of the data object of each item
  3299. */
  3300. /**
  3301. * Creates new Filter.
  3302. * @param {Object} [config] Config object
  3303. */
  3304. constructor: function(config) {
  3305. var me = this;
  3306. Ext.apply(me, config);
  3307. //we're aliasing filter to filterFn mostly for API cleanliness reasons, despite the fact it dirties the code here.
  3308. //Ext.util.Sorter takes a sorterFn property but allows .sort to be called - we do the same here
  3309. me.filter = me.filter || me.filterFn;
  3310. if (me.filter === undefined) {
  3311. if (me.property === undefined || me.value === undefined) {
  3312. // Commented this out temporarily because it stops us using string ids in models. TODO: Remove this once
  3313. // Model has been updated to allow string ids
  3314. // Ext.Error.raise("A Filter requires either a property or a filterFn to be set");
  3315. } else {
  3316. me.filter = me.createFilterFn();
  3317. }
  3318. me.filterFn = me.filter;
  3319. }
  3320. },
  3321. /**
  3322. * @private
  3323. * Creates a filter function for the configured property/value/anyMatch/caseSensitive options for this Filter
  3324. */
  3325. createFilterFn: function() {
  3326. var me = this,
  3327. matcher = me.createValueMatcher(),
  3328. property = me.property;
  3329. return function(item) {
  3330. var value = me.getRoot.call(me, item)[property];
  3331. return matcher === null ? value === null : matcher.test(value);
  3332. };
  3333. },
  3334. /**
  3335. * @private
  3336. * Returns the root property of the given item, based on the configured {@link #root} property
  3337. * @param {Object} item The item
  3338. * @return {Object} The root property of the object
  3339. */
  3340. getRoot: function(item) {
  3341. var root = this.root;
  3342. return root === undefined ? item : item[root];
  3343. },
  3344. /**
  3345. * @private
  3346. * Returns a regular expression based on the given value and matching options
  3347. */
  3348. createValueMatcher : function() {
  3349. var me = this,
  3350. value = me.value,
  3351. anyMatch = me.anyMatch,
  3352. exactMatch = me.exactMatch,
  3353. caseSensitive = me.caseSensitive,
  3354. escapeRe = Ext.String.escapeRegex;
  3355. if (value === null) {
  3356. return value;
  3357. }
  3358. if (!value.exec) { // not a regex
  3359. value = String(value);
  3360. if (anyMatch === true) {
  3361. value = escapeRe(value);
  3362. } else {
  3363. value = '^' + escapeRe(value);
  3364. if (exactMatch === true) {
  3365. value += '$';
  3366. }
  3367. }
  3368. value = new RegExp(value, caseSensitive ? '' : 'i');
  3369. }
  3370. return value;
  3371. }
  3372. });
  3373. /**
  3374. * Represents a single sorter that can be applied to a Store. The sorter is used
  3375. * to compare two values against each other for the purpose of ordering them. Ordering
  3376. * is achieved by specifying either:
  3377. *
  3378. * - {@link #property A sorting property}
  3379. * - {@link #sorterFn A sorting function}
  3380. *
  3381. * As a contrived example, we can specify a custom sorter that sorts by rank:
  3382. *
  3383. * Ext.define('Person', {
  3384. * extend: 'Ext.data.Model',
  3385. * fields: ['name', 'rank']
  3386. * });
  3387. *
  3388. * Ext.create('Ext.data.Store', {
  3389. * model: 'Person',
  3390. * proxy: 'memory',
  3391. * sorters: [{
  3392. * sorterFn: function(o1, o2){
  3393. * var getRank = function(o){
  3394. * var name = o.get('rank');
  3395. * if (name === 'first') {
  3396. * return 1;
  3397. * } else if (name === 'second') {
  3398. * return 2;
  3399. * } else {
  3400. * return 3;
  3401. * }
  3402. * },
  3403. * rank1 = getRank(o1),
  3404. * rank2 = getRank(o2);
  3405. *
  3406. * if (rank1 === rank2) {
  3407. * return 0;
  3408. * }
  3409. *
  3410. * return rank1 < rank2 ? -1 : 1;
  3411. * }
  3412. * }],
  3413. * data: [{
  3414. * name: 'Person1',
  3415. * rank: 'second'
  3416. * }, {
  3417. * name: 'Person2',
  3418. * rank: 'third'
  3419. * }, {
  3420. * name: 'Person3',
  3421. * rank: 'first'
  3422. * }]
  3423. * });
  3424. */
  3425. Ext.define('Ext.util.Sorter', {
  3426. /**
  3427. * @cfg {String} property
  3428. * The property to sort by. Required unless {@link #sorterFn} is provided. The property is extracted from the object
  3429. * directly and compared for sorting using the built in comparison operators.
  3430. */
  3431. /**
  3432. * @cfg {Function} sorterFn
  3433. * A specific sorter function to execute. Can be passed instead of {@link #property}. This sorter function allows
  3434. * for any kind of custom/complex comparisons. The sorterFn receives two arguments, the objects being compared. The
  3435. * function should return:
  3436. *
  3437. * - -1 if o1 is "less than" o2
  3438. * - 0 if o1 is "equal" to o2
  3439. * - 1 if o1 is "greater than" o2
  3440. */
  3441. /**
  3442. * @cfg {String} root
  3443. * Optional root property. This is mostly useful when sorting a Store, in which case we set the root to 'data' to
  3444. * make the filter pull the {@link #property} out of the data object of each item
  3445. */
  3446. /**
  3447. * @cfg {Function} transform
  3448. * A function that will be run on each value before it is compared in the sorter. The function will receive a single
  3449. * argument, the value.
  3450. */
  3451. /**
  3452. * @cfg {String} direction
  3453. * The direction to sort by.
  3454. */
  3455. direction: "ASC",
  3456. constructor: function(config) {
  3457. var me = this;
  3458. Ext.apply(me, config);
  3459. if (me.property === undefined && me.sorterFn === undefined) {
  3460. Ext.Error.raise("A Sorter requires either a property or a sorter function");
  3461. }
  3462. me.updateSortFunction();
  3463. },
  3464. /**
  3465. * @private
  3466. * Creates and returns a function which sorts an array by the given property and direction
  3467. * @return {Function} A function which sorts by the property/direction combination provided
  3468. */
  3469. createSortFunction: function(sorterFn) {
  3470. var me = this,
  3471. property = me.property,
  3472. direction = me.direction || "ASC",
  3473. modifier = direction.toUpperCase() == "DESC" ? -1 : 1;
  3474. //create a comparison function. Takes 2 objects, returns 1 if object 1 is greater,
  3475. //-1 if object 2 is greater or 0 if they are equal
  3476. return function(o1, o2) {
  3477. return modifier * sorterFn.call(me, o1, o2);
  3478. };
  3479. },
  3480. /**
  3481. * @private
  3482. * Basic default sorter function that just compares the defined property of each object
  3483. */
  3484. defaultSorterFn: function(o1, o2) {
  3485. var me = this,
  3486. transform = me.transform,
  3487. v1 = me.getRoot(o1)[me.property],
  3488. v2 = me.getRoot(o2)[me.property];
  3489. if (transform) {
  3490. v1 = transform(v1);
  3491. v2 = transform(v2);
  3492. }
  3493. return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
  3494. },
  3495. /**
  3496. * @private
  3497. * Returns the root property of the given item, based on the configured {@link #root} property
  3498. * @param {Object} item The item
  3499. * @return {Object} The root property of the object
  3500. */
  3501. getRoot: function(item) {
  3502. return this.root === undefined ? item : item[this.root];
  3503. },
  3504. /**
  3505. * Set the sorting direction for this sorter.
  3506. * @param {String} direction The direction to sort in. Should be either 'ASC' or 'DESC'.
  3507. */
  3508. setDirection: function(direction) {
  3509. var me = this;
  3510. me.direction = direction;
  3511. me.updateSortFunction();
  3512. },
  3513. /**
  3514. * Toggles the sorting direction for this sorter.
  3515. */
  3516. toggle: function() {
  3517. var me = this;
  3518. me.direction = Ext.String.toggle(me.direction, "ASC", "DESC");
  3519. me.updateSortFunction();
  3520. },
  3521. /**
  3522. * Update the sort function for this sorter.
  3523. * @param {Function} [fn] A new sorter function for this sorter. If not specified it will use the default
  3524. * sorting function.
  3525. */
  3526. updateSortFunction: function(fn) {
  3527. var me = this;
  3528. fn = fn || me.sorterFn || me.defaultSorterFn;
  3529. me.sort = me.createSortFunction(fn);
  3530. }
  3531. });
  3532. /**
  3533. * This animation class is a mixin.
  3534. *
  3535. * Ext.util.Animate provides an API for the creation of animated transitions of properties and styles.
  3536. * This class is used as a mixin and currently applied to {@link Ext.Element}, {@link Ext.CompositeElement},
  3537. * {@link Ext.draw.Sprite}, {@link Ext.draw.CompositeSprite}, and {@link Ext.Component}. Note that Components
  3538. * have a limited subset of what attributes can be animated such as top, left, x, y, height, width, and
  3539. * opacity (color, paddings, and margins can not be animated).
  3540. *
  3541. * ## Animation Basics
  3542. *
  3543. * All animations require three things - `easing`, `duration`, and `to` (the final end value for each property)
  3544. * you wish to animate. Easing and duration are defaulted values specified below.
  3545. * Easing describes how the intermediate values used during a transition will be calculated.
  3546. * {@link Ext.fx.Anim#easing Easing} allows for a transition to change speed over its duration.
  3547. * You may use the defaults for easing and duration, but you must always set a
  3548. * {@link Ext.fx.Anim#to to} property which is the end value for all animations.
  3549. *
  3550. * Popular element 'to' configurations are:
  3551. *
  3552. * - opacity
  3553. * - x
  3554. * - y
  3555. * - color
  3556. * - height
  3557. * - width
  3558. *
  3559. * Popular sprite 'to' configurations are:
  3560. *
  3561. * - translation
  3562. * - path
  3563. * - scale
  3564. * - stroke
  3565. * - rotation
  3566. *
  3567. * The default duration for animations is 250 (which is a 1/4 of a second). Duration is denoted in
  3568. * milliseconds. Therefore 1 second is 1000, 1 minute would be 60000, and so on. The default easing curve
  3569. * used for all animations is 'ease'. Popular easing functions are included and can be found in {@link Ext.fx.Anim#easing Easing}.
  3570. *
  3571. * For example, a simple animation to fade out an element with a default easing and duration:
  3572. *
  3573. * var p1 = Ext.get('myElementId');
  3574. *
  3575. * p1.animate({
  3576. * to: {
  3577. * opacity: 0
  3578. * }
  3579. * });
  3580. *
  3581. * To make this animation fade out in a tenth of a second:
  3582. *
  3583. * var p1 = Ext.get('myElementId');
  3584. *
  3585. * p1.animate({
  3586. * duration: 100,
  3587. * to: {
  3588. * opacity: 0
  3589. * }
  3590. * });
  3591. *
  3592. * ## Animation Queues
  3593. *
  3594. * By default all animations are added to a queue which allows for animation via a chain-style API.
  3595. * For example, the following code will queue 4 animations which occur sequentially (one right after the other):
  3596. *
  3597. * p1.animate({
  3598. * to: {
  3599. * x: 500
  3600. * }
  3601. * }).animate({
  3602. * to: {
  3603. * y: 150
  3604. * }
  3605. * }).animate({
  3606. * to: {
  3607. * backgroundColor: '#f00' //red
  3608. * }
  3609. * }).animate({
  3610. * to: {
  3611. * opacity: 0
  3612. * }
  3613. * });
  3614. *
  3615. * You can change this behavior by calling the {@link Ext.util.Animate#syncFx syncFx} method and all
  3616. * subsequent animations for the specified target will be run concurrently (at the same time).
  3617. *
  3618. * p1.syncFx(); //this will make all animations run at the same time
  3619. *
  3620. * p1.animate({
  3621. * to: {
  3622. * x: 500
  3623. * }
  3624. * }).animate({
  3625. * to: {
  3626. * y: 150
  3627. * }
  3628. * }).animate({
  3629. * to: {
  3630. * backgroundColor: '#f00' //red
  3631. * }
  3632. * }).animate({
  3633. * to: {
  3634. * opacity: 0
  3635. * }
  3636. * });
  3637. *
  3638. * This works the same as:
  3639. *
  3640. * p1.animate({
  3641. * to: {
  3642. * x: 500,
  3643. * y: 150,
  3644. * backgroundColor: '#f00' //red
  3645. * opacity: 0
  3646. * }
  3647. * });
  3648. *
  3649. * The {@link Ext.util.Animate#stopAnimation stopAnimation} method can be used to stop any
  3650. * currently running animations and clear any queued animations.
  3651. *
  3652. * ## Animation Keyframes
  3653. *
  3654. * You can also set up complex animations with {@link Ext.fx.Anim#keyframes keyframes} which follow the
  3655. * CSS3 Animation configuration pattern. Note rotation, translation, and scaling can only be done for sprites.
  3656. * The previous example can be written with the following syntax:
  3657. *
  3658. * p1.animate({
  3659. * duration: 1000, //one second total
  3660. * keyframes: {
  3661. * 25: { //from 0 to 250ms (25%)
  3662. * x: 0
  3663. * },
  3664. * 50: { //from 250ms to 500ms (50%)
  3665. * y: 0
  3666. * },
  3667. * 75: { //from 500ms to 750ms (75%)
  3668. * backgroundColor: '#f00' //red
  3669. * },
  3670. * 100: { //from 750ms to 1sec
  3671. * opacity: 0
  3672. * }
  3673. * }
  3674. * });
  3675. *
  3676. * ## Animation Events
  3677. *
  3678. * Each animation you create has events for {@link Ext.fx.Anim#beforeanimate beforeanimate},
  3679. * {@link Ext.fx.Anim#afteranimate afteranimate}, and {@link Ext.fx.Anim#lastframe lastframe}.
  3680. * Keyframed animations adds an additional {@link Ext.fx.Animator#keyframe keyframe} event which
  3681. * fires for each keyframe in your animation.
  3682. *
  3683. * All animations support the {@link Ext.util.Observable#listeners listeners} configuration to attact functions to these events.
  3684. *
  3685. * startAnimate: function() {
  3686. * var p1 = Ext.get('myElementId');
  3687. * p1.animate({
  3688. * duration: 100,
  3689. * to: {
  3690. * opacity: 0
  3691. * },
  3692. * listeners: {
  3693. * beforeanimate: function() {
  3694. * // Execute my custom method before the animation
  3695. * this.myBeforeAnimateFn();
  3696. * },
  3697. * afteranimate: function() {
  3698. * // Execute my custom method after the animation
  3699. * this.myAfterAnimateFn();
  3700. * },
  3701. * scope: this
  3702. * });
  3703. * },
  3704. * myBeforeAnimateFn: function() {
  3705. * // My custom logic
  3706. * },
  3707. * myAfterAnimateFn: function() {
  3708. * // My custom logic
  3709. * }
  3710. *
  3711. * Due to the fact that animations run asynchronously, you can determine if an animation is currently
  3712. * running on any target by using the {@link Ext.util.Animate#getActiveAnimation getActiveAnimation}
  3713. * method. This method will return false if there are no active animations or return the currently
  3714. * running {@link Ext.fx.Anim} instance.
  3715. *
  3716. * In this example, we're going to wait for the current animation to finish, then stop any other
  3717. * queued animations before we fade our element's opacity to 0:
  3718. *
  3719. * var curAnim = p1.getActiveAnimation();
  3720. * if (curAnim) {
  3721. * curAnim.on('afteranimate', function() {
  3722. * p1.stopAnimation();
  3723. * p1.animate({
  3724. * to: {
  3725. * opacity: 0
  3726. * }
  3727. * });
  3728. * });
  3729. * }
  3730. */
  3731. Ext.define('Ext.util.Animate', {
  3732. uses: ['Ext.fx.Manager', 'Ext.fx.Anim'],
  3733. /**
  3734. * Perform custom animation on this object.
  3735. *
  3736. * This method is applicable to both the {@link Ext.Component Component} class and the {@link Ext.Element Element}
  3737. * class. It performs animated transitions of certain properties of this object over a specified timeline.
  3738. *
  3739. * The sole parameter is an object which specifies start property values, end property values, and properties which
  3740. * describe the timeline.
  3741. *
  3742. * ### Animating an {@link Ext.Element Element}
  3743. *
  3744. * When animating an Element, the following properties may be specified in `from`, `to`, and `keyframe` objects:
  3745. *
  3746. * - `x` - The page X position in pixels.
  3747. *
  3748. * - `y` - The page Y position in pixels
  3749. *
  3750. * - `left` - The element's CSS `left` value. Units must be supplied.
  3751. *
  3752. * - `top` - The element's CSS `top` value. Units must be supplied.
  3753. *
  3754. * - `width` - The element's CSS `width` value. Units must be supplied.
  3755. *
  3756. * - `height` - The element's CSS `height` value. Units must be supplied.
  3757. *
  3758. * - `scrollLeft` - The element's `scrollLeft` value.
  3759. *
  3760. * - `scrollTop` - The element's `scrollLeft` value.
  3761. *
  3762. * - `opacity` - The element's `opacity` value. This must be a value between `0` and `1`.
  3763. *
  3764. * **Be aware than animating an Element which is being used by an Ext Component without in some way informing the
  3765. * Component about the changed element state will result in incorrect Component behaviour. This is because the
  3766. * Component will be using the old state of the element. To avoid this problem, it is now possible to directly
  3767. * animate certain properties of Components.**
  3768. *
  3769. * ### Animating a {@link Ext.Component Component}
  3770. *
  3771. * When animating a Component, the following properties may be specified in `from`, `to`, and `keyframe` objects:
  3772. *
  3773. * - `x` - The Component's page X position in pixels.
  3774. *
  3775. * - `y` - The Component's page Y position in pixels
  3776. *
  3777. * - `left` - The Component's `left` value in pixels.
  3778. *
  3779. * - `top` - The Component's `top` value in pixels.
  3780. *
  3781. * - `width` - The Component's `width` value in pixels.
  3782. *
  3783. * - `width` - The Component's `width` value in pixels.
  3784. *
  3785. * - `dynamic` - Specify as true to update the Component's layout (if it is a Container) at every frame of the animation.
  3786. * *Use sparingly as laying out on every intermediate size change is an expensive operation.*
  3787. *
  3788. * For example, to animate a Window to a new size, ensuring that its internal layout, and any shadow is correct:
  3789. *
  3790. * myWindow = Ext.create('Ext.window.Window', {
  3791. * title: 'Test Component animation',
  3792. * width: 500,
  3793. * height: 300,
  3794. * layout: {
  3795. * type: 'hbox',
  3796. * align: 'stretch'
  3797. * },
  3798. * items: [{
  3799. * title: 'Left: 33%',
  3800. * margins: '5 0 5 5',
  3801. * flex: 1
  3802. * }, {
  3803. * title: 'Left: 66%',
  3804. * margins: '5 5 5 5',
  3805. * flex: 2
  3806. * }]
  3807. * });
  3808. * myWindow.show();
  3809. * myWindow.header.el.on('click', function() {
  3810. * myWindow.animate({
  3811. * to: {
  3812. * width: (myWindow.getWidth() == 500) ? 700 : 500,
  3813. * height: (myWindow.getHeight() == 300) ? 400 : 300,
  3814. * }
  3815. * });
  3816. * });
  3817. *
  3818. * For performance reasons, by default, the internal layout is only updated when the Window reaches its final `"to"`
  3819. * size. If dynamic updating of the Window's child Components is required, then configure the animation with
  3820. * `dynamic: true` and the two child items will maintain their proportions during the animation.
  3821. *
  3822. * @param {Object} config An object containing properties which describe the animation's start and end states, and
  3823. * the timeline of the animation. Of the properties listed below, only **`to`** is mandatory.
  3824. *
  3825. * Properties include:
  3826. *
  3827. * @param {Object} config.from
  3828. * An object which specifies start values for the properties being animated. If not supplied, properties are
  3829. * animated from current settings. The actual properties which may be animated depend upon ths object being
  3830. * animated. See the sections below on Element and Component animation.
  3831. *
  3832. * @param {Object} config.to
  3833. * An object which specifies end values for the properties being animated.
  3834. *
  3835. * @param {Number} config.duration
  3836. * The duration **in milliseconds** for which the animation will run.
  3837. *
  3838. * @param {String} config.easing
  3839. * A string value describing an easing type to modify the rate of change from the default linear to non-linear.
  3840. * Values may be one of:
  3841. *
  3842. * - ease
  3843. * - easeIn
  3844. * - easeOut
  3845. * - easeInOut
  3846. * - backIn
  3847. * - backOut
  3848. * - elasticIn
  3849. * - elasticOut
  3850. * - bounceIn
  3851. * - bounceOut
  3852. *
  3853. * @param {Object} config.keyframes
  3854. * This is an object which describes the state of animated properties at certain points along the timeline. it is an
  3855. * object containing properties who's names are the percentage along the timeline being described and who's values
  3856. * specify the animation state at that point.
  3857. *
  3858. * @param {Object} config.listeners
  3859. * This is a standard {@link Ext.util.Observable#listeners listeners} configuration object which may be used to
  3860. * inject behaviour at either the `beforeanimate` event or the `afteranimate` event.
  3861. *
  3862. * @return {Object} this
  3863. */
  3864. animate: function(animObj) {
  3865. var me = this;
  3866. if (Ext.fx.Manager.hasFxBlock(me.id)) {
  3867. return me;
  3868. }
  3869. Ext.fx.Manager.queueFx(new Ext.fx.Anim(me.anim(animObj)));
  3870. return this;
  3871. },
  3872. // @private - process the passed fx configuration.
  3873. anim: function(config) {
  3874. if (!Ext.isObject(config)) {
  3875. return (config) ? {} : false;
  3876. }
  3877. var me = this;
  3878. if (config.stopAnimation) {
  3879. me.stopAnimation();
  3880. }
  3881. Ext.applyIf(config, Ext.fx.Manager.getFxDefaults(me.id));
  3882. return Ext.apply({
  3883. target: me,
  3884. paused: true
  3885. }, config);
  3886. },
  3887. /**
  3888. * Stops any running effects and clears this object's internal effects queue if it contains any additional effects
  3889. * that haven't started yet.
  3890. * @deprecated 4.0 Replaced by {@link #stopAnimation}
  3891. * @return {Ext.Element} The Element
  3892. * @method
  3893. */
  3894. stopFx: Ext.Function.alias(Ext.util.Animate, 'stopAnimation'),
  3895. /**
  3896. * Stops any running effects and clears this object's internal effects queue if it contains any additional effects
  3897. * that haven't started yet.
  3898. * @return {Ext.Element} The Element
  3899. */
  3900. stopAnimation: function() {
  3901. Ext.fx.Manager.stopAnimation(this.id);
  3902. return this;
  3903. },
  3904. /**
  3905. * Ensures that all effects queued after syncFx is called on this object are run concurrently. This is the opposite
  3906. * of {@link #sequenceFx}.
  3907. * @return {Object} this
  3908. */
  3909. syncFx: function() {
  3910. Ext.fx.Manager.setFxDefaults(this.id, {
  3911. concurrent: true
  3912. });
  3913. return this;
  3914. },
  3915. /**
  3916. * Ensures that all effects queued after sequenceFx is called on this object are run in sequence. This is the
  3917. * opposite of {@link #syncFx}.
  3918. * @return {Object} this
  3919. */
  3920. sequenceFx: function() {
  3921. Ext.fx.Manager.setFxDefaults(this.id, {
  3922. concurrent: false
  3923. });
  3924. return this;
  3925. },
  3926. /**
  3927. * @deprecated 4.0 Replaced by {@link #getActiveAnimation}
  3928. * @inheritdoc Ext.util.Animate#getActiveAnimation
  3929. * @method
  3930. */
  3931. hasActiveFx: Ext.Function.alias(Ext.util.Animate, 'getActiveAnimation'),
  3932. /**
  3933. * Returns the current animation if this object has any effects actively running or queued, else returns false.
  3934. * @return {Ext.fx.Anim/Boolean} Anim if element has active effects, else false
  3935. */
  3936. getActiveAnimation: function() {
  3937. return Ext.fx.Manager.getActiveAnimation(this.id);
  3938. }
  3939. }, function(){
  3940. // Apply Animate mixin manually until Element is defined in the proper 4.x way
  3941. Ext.applyIf(Ext.Element.prototype, this.prototype);
  3942. // We need to call this again so the animation methods get copied over to CE
  3943. Ext.CompositeElementLite.importElementMethods();
  3944. });
  3945. /**
  3946. * This mixin enables classes to declare relationships to child elements and provides the
  3947. * mechanics for acquiring the {@link Ext.Element elements} and storing them on an object
  3948. * instance as properties.
  3949. *
  3950. * This class is used by {@link Ext.Component components} and {@link Ext.layout.container.Container container layouts} to
  3951. * manage their child elements.
  3952. *
  3953. * A typical component that uses these features might look something like this:
  3954. *
  3955. * Ext.define('Ext.ux.SomeComponent', {
  3956. * extend: 'Ext.Component',
  3957. *
  3958. * childEls: [
  3959. * 'bodyEl'
  3960. * ],
  3961. *
  3962. * renderTpl: [
  3963. * '&lt;div id="{id}-bodyEl"&gt;&lt;/div&gt;'
  3964. * ],
  3965. *
  3966. * // ...
  3967. * });
  3968. *
  3969. * The `childEls` array lists one or more relationships to child elements managed by the
  3970. * component. The items in this array can be either of the following types:
  3971. *
  3972. * - String: the id suffix and property name in one. For example, "bodyEl" in the above
  3973. * example means a "bodyEl" property will be added to the instance with the result of
  3974. * {@link Ext#get} given "componentId-bodyEl" where "componentId" is the component instance's
  3975. * id.
  3976. * - Object: with a `name` property that names the instance property for the element, and
  3977. * one of the following additional properties:
  3978. * - `id`: The full id of the child element.
  3979. * - `itemId`: The suffix part of the id to which "componentId-" is prepended.
  3980. * - `select`: A selector that will be passed to {@link Ext#select}.
  3981. * - `selectNode`: A selector that will be passed to {@link Ext.DomQuery#selectNode}.
  3982. *
  3983. * The example above could have used this instead to achieve the same result:
  3984. *
  3985. * childEls: [
  3986. * { name: 'bodyEl', itemId: 'bodyEl' }
  3987. * ]
  3988. *
  3989. * When using `select`, the property will be an instance of {@link Ext.CompositeElement}. In
  3990. * all other cases, the property will be an {@link Ext.Element} or `null` if not found.
  3991. *
  3992. * Care should be taken when using `select` or `selectNode` to find child elements. The
  3993. * following issues should be considered:
  3994. *
  3995. * - Performance: using selectors can be slower than id lookup by a factor 10x or more.
  3996. * - Over-selecting: selectors are applied after the DOM elements for all children have
  3997. * been rendered, so selectors can match elements from child components (including nested
  3998. * versions of the same component) accidentally.
  3999. *
  4000. * This above issues are most important when using `select` since it returns multiple
  4001. * elements.
  4002. *
  4003. * **IMPORTANT**
  4004. * Unlike a `renderTpl` where there is a single value for an instance, `childEls` are aggregated
  4005. * up the class hierarchy so that they are effectively inherited. In other words, if a
  4006. * class where to derive from `Ext.ux.SomeComponent` in the example above, it could also
  4007. * have a `childEls` property in the same way as `Ext.ux.SomeComponent`.
  4008. *
  4009. * Ext.define('Ext.ux.AnotherComponent', {
  4010. * extend: 'Ext.ux.SomeComponent',
  4011. *
  4012. * childEls: [
  4013. * // 'bodyEl' is inherited
  4014. * 'innerEl'
  4015. * ],
  4016. *
  4017. * renderTpl: [
  4018. * '&lt;div id="{id}-bodyEl"&gt;'
  4019. * '&lt;div id="{id}-innerEl"&gt;&lt;/div&gt;'
  4020. * '&lt;/div&gt;'
  4021. * ],
  4022. *
  4023. * // ...
  4024. * });
  4025. *
  4026. * The `renderTpl` contains both child elements and unites them in the desired markup, but
  4027. * the `childEls` only contains the new child element. The {@link #applyChildEls} method
  4028. * takes care of looking up all `childEls` for an instance and considers `childEls`
  4029. * properties on all the super classes and mixins.
  4030. *
  4031. * @private
  4032. */
  4033. Ext.define('Ext.util.ElementContainer', {
  4034. childEls: [
  4035. // empty - this solves a couple problems:
  4036. // 1. It ensures that all classes have a childEls (avoid null ptr)
  4037. // 2. It prevents mixins from smashing on their own childEls (these are gathered
  4038. // specifically)
  4039. ],
  4040. constructor: function () {
  4041. var me = this,
  4042. childEls;
  4043. // if we have configured childEls, we need to merge them with those from this
  4044. // class, its bases and the set of mixins...
  4045. if (me.hasOwnProperty('childEls')) {
  4046. childEls = me.childEls;
  4047. delete me.childEls;
  4048. me.addChildEls.apply(me, childEls);
  4049. }
  4050. },
  4051. destroy: function () {
  4052. var me = this,
  4053. childEls = me.getChildEls(),
  4054. child, childName, i, k;
  4055. for (i = childEls.length; i--; ) {
  4056. childName = childEls[i];
  4057. if (typeof childName != 'string') {
  4058. childName = childName.name;
  4059. }
  4060. child = me[childName];
  4061. if (child) {
  4062. me[childName] = null; // better than delete since that changes the "shape"
  4063. child.remove();
  4064. }
  4065. }
  4066. },
  4067. /**
  4068. * Adds each argument passed to this method to the {@link #childEls} array.
  4069. */
  4070. addChildEls: function () {
  4071. var me = this,
  4072. args = arguments;;
  4073. if (me.hasOwnProperty('childEls')) {
  4074. me.childEls.push.apply(me.childEls, args);
  4075. } else {
  4076. me.childEls = me.getChildEls().concat(Array.prototype.slice.call(args));
  4077. }
  4078. me.prune(me.childEls, false);
  4079. },
  4080. /**
  4081. * Sets references to elements inside the component.
  4082. * @private
  4083. */
  4084. applyChildEls: function(el, id) {
  4085. var me = this,
  4086. childEls = me.getChildEls(),
  4087. baseId, childName, i, selector, value;
  4088. baseId = (id || me.id) + '-';
  4089. for (i = childEls.length; i--; ) {
  4090. childName = childEls[i];
  4091. if (typeof childName == 'string') {
  4092. // We don't use Ext.get because that is 3x (or more) slower on IE6-8. Since
  4093. // we know the el's are children of our el we use getById instead:
  4094. value = el.getById(baseId + childName);
  4095. } else {
  4096. if ((selector = childName.select)) {
  4097. value = Ext.select(selector, true, el.dom); // a CompositeElement
  4098. } else if ((selector = childName.selectNode)) {
  4099. value = Ext.get(Ext.DomQuery.selectNode(selector, el.dom));
  4100. } else {
  4101. // see above re:getById...
  4102. value = el.getById(childName.id || (baseId + childName.itemId));
  4103. }
  4104. childName = childName.name;
  4105. }
  4106. me[childName] = value;
  4107. }
  4108. },
  4109. getChildEls: function () {
  4110. var me = this,
  4111. self;
  4112. // If an instance has its own childEls, that is the complete set:
  4113. if (me.hasOwnProperty('childEls')) {
  4114. return me.childEls;
  4115. }
  4116. // Typically, however, the childEls is a class-level concept, so check to see if
  4117. // we have cached the complete set on the class:
  4118. self = me.self;
  4119. return self.$childEls || me.getClassChildEls(self);
  4120. },
  4121. getClassChildEls: function (cls) {
  4122. var me = this,
  4123. result = cls.$childEls,
  4124. childEls, i, length, forked, mixin, mixins, name, parts, proto, supr, superMixins;
  4125. if (!result) {
  4126. // We put the various childEls arrays into parts in the order of superclass,
  4127. // new mixins and finally from cls. These parts can be null or undefined and
  4128. // we will skip them later.
  4129. supr = cls.superclass;
  4130. if (supr) {
  4131. supr = supr.self;
  4132. parts = [supr.$childEls || me.getClassChildEls(supr)]; // super+mixins
  4133. superMixins = supr.prototype.mixins || {};
  4134. } else {
  4135. parts = [];
  4136. superMixins = {};
  4137. }
  4138. proto = cls.prototype;
  4139. mixins = proto.mixins; // since we are a mixin, there will be at least us
  4140. for (name in mixins) {
  4141. if (mixins.hasOwnProperty(name) && !superMixins.hasOwnProperty(name)) {
  4142. mixin = mixins[name].self;
  4143. parts.push(mixin.$childEls || me.getClassChildEls(mixin));
  4144. }
  4145. }
  4146. parts.push(proto.hasOwnProperty('childEls') && proto.childEls);
  4147. for (i = 0, length = parts.length; i < length; ++i) {
  4148. childEls = parts[i];
  4149. if (childEls && childEls.length) {
  4150. if (!result) {
  4151. result = childEls;
  4152. } else {
  4153. if (!forked) {
  4154. forked = true;
  4155. result = result.slice(0);
  4156. }
  4157. result.push.apply(result, childEls);
  4158. }
  4159. }
  4160. }
  4161. cls.$childEls = result = (result ? me.prune(result, !forked) : []);
  4162. }
  4163. return result;
  4164. },
  4165. prune: function (childEls, shared) {
  4166. var index = childEls.length,
  4167. map = {},
  4168. name;
  4169. while (index--) {
  4170. name = childEls[index];
  4171. if (typeof name != 'string') {
  4172. name = name.name;
  4173. }
  4174. if (!map[name]) {
  4175. map[name] = 1;
  4176. } else {
  4177. if (shared) {
  4178. shared = false;
  4179. childEls = childEls.slice(0);
  4180. }
  4181. Ext.Array.erase(childEls, index, 1);
  4182. }
  4183. }
  4184. return childEls;
  4185. },
  4186. /**
  4187. * Removes items in the childEls array based on the return value of a supplied test
  4188. * function. The function is called with a entry in childEls and if the test function
  4189. * return true, that entry is removed. If false, that entry is kept.
  4190. *
  4191. * @param {Function} testFn The test function.
  4192. */
  4193. removeChildEls: function (testFn) {
  4194. var me = this,
  4195. old = me.getChildEls(),
  4196. keepers = (me.childEls = []),
  4197. n, i, cel;
  4198. for (i = 0, n = old.length; i < n; ++i) {
  4199. cel = old[i];
  4200. if (!testFn(cel)) {
  4201. keepers.push(cel);
  4202. }
  4203. }
  4204. }
  4205. });
  4206. /**
  4207. * Given a component hierarchy of this:
  4208. *
  4209. * {
  4210. * xtype: 'panel',
  4211. * id: 'ContainerA',
  4212. * layout: 'hbox',
  4213. * renderTo: Ext.getBody(),
  4214. * items: [
  4215. * {
  4216. * id: 'ContainerB',
  4217. * xtype: 'container',
  4218. * items: [
  4219. * { id: 'ComponentA' }
  4220. * ]
  4221. * }
  4222. * ]
  4223. * }
  4224. *
  4225. * The rendering of the above proceeds roughly like this:
  4226. *
  4227. * - ContainerA's initComponent calls #render passing the `renderTo` property as the
  4228. * container argument.
  4229. * - `render` calls the `getRenderTree` method to get a complete {@link Ext.DomHelper} spec.
  4230. * - `getRenderTree` fires the "beforerender" event and calls the #beforeRender
  4231. * method. Its result is obtained by calling #getElConfig.
  4232. * - The #getElConfig method uses the `renderTpl` and its render data as the content
  4233. * of the `autoEl` described element.
  4234. * - The result of `getRenderTree` is passed to {@link Ext.DomHelper#append}.
  4235. * - The `renderTpl` contains calls to render things like docked items, container items
  4236. * and raw markup (such as the `html` or `tpl` config properties). These calls are to
  4237. * methods added to the {@link Ext.XTemplate} instance by #setupRenderTpl.
  4238. * - The #setupRenderTpl method adds methods such as `renderItems`, `renderContent`, etc.
  4239. * to the template. These are directed to "doRenderItems", "doRenderContent" etc..
  4240. * - The #setupRenderTpl calls traverse from components to their {@link Ext.layout.Layout}
  4241. * object.
  4242. * - When a container is rendered, it also has a `renderTpl`. This is processed when the
  4243. * `renderContainer` method is called in the component's `renderTpl`. This call goes to
  4244. * Ext.layout.container.Container#doRenderContainer. This method repeats this
  4245. * process for all components in the container.
  4246. * - After the top-most component's markup is generated and placed in to the DOM, the next
  4247. * step is to link elements to their components and finish calling the component methods
  4248. * `onRender` and `afterRender` as well as fire the corresponding events.
  4249. * - The first step in this is to call #finishRender. This method descends the
  4250. * component hierarchy and calls `onRender` and fires the `render` event. These calls
  4251. * are delivered top-down to approximate the timing of these calls/events from previous
  4252. * versions.
  4253. * - During the pass, the component's `el` is set. Likewise, the `renderSelectors` and
  4254. * `childEls` are applied to capture references to the component's elements.
  4255. * - These calls are also made on the {@link Ext.layout.container.Container} layout to
  4256. * capture its elements. Both of these classes use {@link Ext.util.ElementContainer} to
  4257. * handle `childEls` processing.
  4258. * - Once this is complete, a similar pass is made by calling #finishAfterRender.
  4259. * This call also descends the component hierarchy, but this time the calls are made in
  4260. * a bottom-up order to `afterRender`.
  4261. *
  4262. * @private
  4263. */
  4264. Ext.define('Ext.util.Renderable', {
  4265. requires: [
  4266. 'Ext.dom.Element'
  4267. ],
  4268. frameCls: Ext.baseCSSPrefix + 'frame',
  4269. frameIdRegex: /[\-]frame\d+[TMB][LCR]$/,
  4270. frameElementCls: {
  4271. tl: [],
  4272. tc: [],
  4273. tr: [],
  4274. ml: [],
  4275. mc: [],
  4276. mr: [],
  4277. bl: [],
  4278. bc: [],
  4279. br: []
  4280. },
  4281. frameElNames: ['TL','TC','TR','ML','MC','MR','BL','BC','BR'],
  4282. frameTpl: [
  4283. '{%this.renderDockedItems(out,values,0);%}',
  4284. '<tpl if="top">',
  4285. '<tpl if="left"><div id="{fgid}TL" class="{frameCls}-tl {baseCls}-tl {baseCls}-{ui}-tl<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-tl</tpl></tpl>" style="background-position: {tl}; padding-left: {frameWidth}px" role="presentation"></tpl>',
  4286. '<tpl if="right"><div id="{fgid}TR" class="{frameCls}-tr {baseCls}-tr {baseCls}-{ui}-tr<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-tr</tpl></tpl>" style="background-position: {tr}; padding-right: {frameWidth}px" role="presentation"></tpl>',
  4287. '<div id="{fgid}TC" class="{frameCls}-tc {baseCls}-tc {baseCls}-{ui}-tc<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-tc</tpl></tpl>" style="background-position: {tc}; height: {frameWidth}px" role="presentation"></div>',
  4288. '<tpl if="right"></div></tpl>',
  4289. '<tpl if="left"></div></tpl>',
  4290. '</tpl>',
  4291. '<tpl if="left"><div id="{fgid}ML" class="{frameCls}-ml {baseCls}-ml {baseCls}-{ui}-ml<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-ml</tpl></tpl>" style="background-position: {ml}; padding-left: {frameWidth}px" role="presentation"></tpl>',
  4292. '<tpl if="right"><div id="{fgid}MR" class="{frameCls}-mr {baseCls}-mr {baseCls}-{ui}-mr<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-mr</tpl></tpl>" style="background-position: {mr}; padding-right: {frameWidth}px" role="presentation"></tpl>',
  4293. '<div id="{fgid}MC" class="{frameCls}-mc {baseCls}-mc {baseCls}-{ui}-mc<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-mc</tpl></tpl>" role="presentation">',
  4294. '{%this.applyRenderTpl(out, values)%}',
  4295. '</div>',
  4296. '<tpl if="right"></div></tpl>',
  4297. '<tpl if="left"></div></tpl>',
  4298. '<tpl if="bottom">',
  4299. '<tpl if="left"><div id="{fgid}BL" class="{frameCls}-bl {baseCls}-bl {baseCls}-{ui}-bl<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-bl</tpl></tpl>" style="background-position: {bl}; padding-left: {frameWidth}px" role="presentation"></tpl>',
  4300. '<tpl if="right"><div id="{fgid}BR" class="{frameCls}-br {baseCls}-br {baseCls}-{ui}-br<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-br</tpl></tpl>" style="background-position: {br}; padding-right: {frameWidth}px" role="presentation"></tpl>',
  4301. '<div id="{fgid}BC" class="{frameCls}-bc {baseCls}-bc {baseCls}-{ui}-bc<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-bc</tpl></tpl>" style="background-position: {bc}; height: {frameWidth}px" role="presentation"></div>',
  4302. '<tpl if="right"></div></tpl>',
  4303. '<tpl if="left"></div></tpl>',
  4304. '</tpl>',
  4305. '{%this.renderDockedItems(out,values,1);%}'
  4306. ],
  4307. frameTableTpl: [
  4308. '{%this.renderDockedItems(out,values,0);%}',
  4309. '<table><tbody>',
  4310. '<tpl if="top">',
  4311. '<tr>',
  4312. '<tpl if="left"><td id="{fgid}TL" class="{frameCls}-tl {baseCls}-tl {baseCls}-{ui}-tl<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-tl</tpl></tpl>" style="background-position: {tl}; padding-left:{frameWidth}px" role="presentation"></td></tpl>',
  4313. '<td id="{fgid}TC" class="{frameCls}-tc {baseCls}-tc {baseCls}-{ui}-tc<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-tc</tpl></tpl>" style="background-position: {tc}; height: {frameWidth}px" role="presentation"></td>',
  4314. '<tpl if="right"><td id="{fgid}TR" class="{frameCls}-tr {baseCls}-tr {baseCls}-{ui}-tr<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-tr</tpl></tpl>" style="background-position: {tr}; padding-left: {frameWidth}px" role="presentation"></td></tpl>',
  4315. '</tr>',
  4316. '</tpl>',
  4317. '<tr>',
  4318. '<tpl if="left"><td id="{fgid}ML" class="{frameCls}-ml {baseCls}-ml {baseCls}-{ui}-ml<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-ml</tpl></tpl>" style="background-position: {ml}; padding-left: {frameWidth}px" role="presentation"></td></tpl>',
  4319. '<td id="{fgid}MC" class="{frameCls}-mc {baseCls}-mc {baseCls}-{ui}-mc<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-mc</tpl></tpl>" style="background-position: 0 0;" role="presentation">',
  4320. '{%this.applyRenderTpl(out, values)%}',
  4321. '</td>',
  4322. '<tpl if="right"><td id="{fgid}MR" class="{frameCls}-mr {baseCls}-mr {baseCls}-{ui}-mr<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-mr</tpl></tpl>" style="background-position: {mr}; padding-left: {frameWidth}px" role="presentation"></td></tpl>',
  4323. '</tr>',
  4324. '<tpl if="bottom">',
  4325. '<tr>',
  4326. '<tpl if="left"><td id="{fgid}BL" class="{frameCls}-bl {baseCls}-bl {baseCls}-{ui}-bl<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-bl</tpl></tpl>" style="background-position: {bl}; padding-left: {frameWidth}px" role="presentation"></td></tpl>',
  4327. '<td id="{fgid}BC" class="{frameCls}-bc {baseCls}-bc {baseCls}-{ui}-bc<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-bc</tpl></tpl>" style="background-position: {bc}; height: {frameWidth}px" role="presentation"></td>',
  4328. '<tpl if="right"><td id="{fgid}BR" class="{frameCls}-br {baseCls}-br {baseCls}-{ui}-br<tpl if="uiCls"><tpl for="uiCls"> {parent.baseCls}-{parent.ui}-{.}-br</tpl></tpl>" style="background-position: {br}; padding-left: {frameWidth}px" role="presentation"></td></tpl>',
  4329. '</tr>',
  4330. '</tpl>',
  4331. '</tbody></table>',
  4332. '{%this.renderDockedItems(out,values,1);%}'
  4333. ],
  4334. /**
  4335. * Allows addition of behavior after rendering is complete. At this stage the Component’s Element
  4336. * will have been styled according to the configuration, will have had any configured CSS class
  4337. * names added, and will be in the configured visibility and the configured enable state.
  4338. *
  4339. * @template
  4340. * @protected
  4341. */
  4342. afterRender : function() {
  4343. var me = this,
  4344. data = {},
  4345. protoEl = me.protoEl,
  4346. target = me.getTargetEl(),
  4347. item;
  4348. me.finishRenderChildren();
  4349. if (me.styleHtmlContent) {
  4350. target.addCls(me.styleHtmlCls);
  4351. }
  4352. protoEl.writeTo(data);
  4353. // Here we apply any styles that were set on the protoEl during the rendering phase
  4354. // A majority of times this will not happen, but we still need to handle it
  4355. item = data.removed;
  4356. if (item) {
  4357. target.removeCls(item);
  4358. }
  4359. item = data.cls;
  4360. if (item.length) {
  4361. target.addCls(item);
  4362. }
  4363. item = data.style;
  4364. if (data.style) {
  4365. target.setStyle(item);
  4366. }
  4367. me.protoEl = null;
  4368. // If this is the outermost Container, lay it out as soon as it is rendered.
  4369. if (!me.ownerCt) {
  4370. me.updateLayout();
  4371. }
  4372. },
  4373. afterFirstLayout : function() {
  4374. var me = this,
  4375. hasX = Ext.isDefined(me.x),
  4376. hasY = Ext.isDefined(me.y),
  4377. pos, xy;
  4378. // For floaters, calculate x and y if they aren't defined by aligning
  4379. // the sized element to the center of either the container or the ownerCt
  4380. if (me.floating && (!hasX || !hasY)) {
  4381. if (me.floatParent) {
  4382. xy = me.el.getAlignToXY(me.floatParent.getTargetEl(), 'c-c');
  4383. pos = me.floatParent.getTargetEl().translatePoints(xy[0], xy[1]);
  4384. } else {
  4385. xy = me.el.getAlignToXY(me.container, 'c-c');
  4386. pos = me.container.translatePoints(xy[0], xy[1]);
  4387. }
  4388. me.x = hasX ? me.x : pos.left;
  4389. me.y = hasY ? me.y : pos.top;
  4390. hasX = hasY = true;
  4391. }
  4392. if (hasX || hasY) {
  4393. me.setPosition(me.x, me.y);
  4394. }
  4395. me.onBoxReady();
  4396. if (me.hasListeners.boxready) {
  4397. me.fireEvent('boxready', me);
  4398. }
  4399. },
  4400. onBoxReady: Ext.emptyFn,
  4401. /**
  4402. * Sets references to elements inside the component. This applies {@link #renderSelectors}
  4403. * as well as {@link #childEls}.
  4404. * @private
  4405. */
  4406. applyRenderSelectors: function() {
  4407. var me = this,
  4408. selectors = me.renderSelectors,
  4409. el = me.el,
  4410. dom = el.dom,
  4411. selector;
  4412. me.applyChildEls(el);
  4413. // We still support renderSelectors. There are a few places in the framework that
  4414. // need them and they are a documented part of the API. In fact, we support mixing
  4415. // childEls and renderSelectors (no reason not to).
  4416. if (selectors) {
  4417. for (selector in selectors) {
  4418. if (selectors.hasOwnProperty(selector) && selectors[selector]) {
  4419. me[selector] = Ext.get(Ext.DomQuery.selectNode(selectors[selector], dom));
  4420. }
  4421. }
  4422. }
  4423. },
  4424. beforeRender: function () {
  4425. var me = this,
  4426. layout = me.getComponentLayout();
  4427. if (!layout.initialized) {
  4428. layout.initLayout();
  4429. }
  4430. me.setUI(me.ui);
  4431. if (me.disabled) {
  4432. // pass silent so the event doesn't fire the first time.
  4433. me.disable(true);
  4434. }
  4435. },
  4436. /**
  4437. * @private
  4438. * Called from the selected frame generation template to insert this Component's inner structure inside the framing structure.
  4439. *
  4440. * When framing is used, a selected frame generation template is used as the primary template of the #getElConfig instead
  4441. * of the configured {@link #renderTpl}. The {@link #renderTpl} is invoked by this method which is injected into the framing template.
  4442. */
  4443. doApplyRenderTpl: function(out, values) {
  4444. // Careful! This method is bolted on to the frameTpl so all we get for context is
  4445. // the renderData! The "this" pointer is the frameTpl instance!
  4446. var me = values.$comp,
  4447. tpl;
  4448. // Don't do this if the component is already rendered:
  4449. if (!me.rendered) {
  4450. tpl = me.initRenderTpl();
  4451. tpl.applyOut(values.renderData, out);
  4452. }
  4453. },
  4454. /**
  4455. * Handles autoRender.
  4456. * Floating Components may have an ownerCt. If they are asking to be constrained, constrain them within that
  4457. * ownerCt, and have their z-index managed locally. Floating Components are always rendered to document.body
  4458. */
  4459. doAutoRender: function() {
  4460. var me = this;
  4461. if (!me.rendered) {
  4462. if (me.floating) {
  4463. me.render(document.body);
  4464. } else {
  4465. me.render(Ext.isBoolean(me.autoRender) ? Ext.getBody() : me.autoRender);
  4466. }
  4467. }
  4468. },
  4469. doRenderContent: function (out, renderData) {
  4470. // Careful! This method is bolted on to the renderTpl so all we get for context is
  4471. // the renderData! The "this" pointer is the renderTpl instance!
  4472. var me = renderData.$comp;
  4473. if (me.html) {
  4474. Ext.DomHelper.generateMarkup(me.html, out);
  4475. delete me.html;
  4476. }
  4477. if (me.tpl) {
  4478. // Make sure this.tpl is an instantiated XTemplate
  4479. if (!me.tpl.isTemplate) {
  4480. me.tpl = new Ext.XTemplate(me.tpl);
  4481. }
  4482. if (me.data) {
  4483. //me.tpl[me.tplWriteMode](target, me.data);
  4484. me.tpl.applyOut(me.data, out);
  4485. delete me.data;
  4486. }
  4487. }
  4488. },
  4489. doRenderFramingDockedItems: function (out, renderData, after) {
  4490. // Careful! This method is bolted on to the frameTpl so all we get for context is
  4491. // the renderData! The "this" pointer is the frameTpl instance!
  4492. var me = renderData.$comp;
  4493. // Most components don't have dockedItems, so check for doRenderDockedItems on the
  4494. // component (also, don't do this if the component is already rendered):
  4495. if (!me.rendered && me.doRenderDockedItems) {
  4496. // The "renderData" property is placed in scope for the renderTpl, but we don't
  4497. // want to render docked items at that level in addition to the framing level:
  4498. renderData.renderData.$skipDockedItems = true;
  4499. // doRenderDockedItems requires the $comp property on renderData, but this is
  4500. // set on the frameTpl's renderData as well:
  4501. me.doRenderDockedItems.call(this, out, renderData, after);
  4502. }
  4503. },
  4504. /**
  4505. * This method visits the rendered component tree in a "top-down" order. That is, this
  4506. * code runs on a parent component before running on a child. This method calls the
  4507. * {@link #onRender} method of each component.
  4508. * @param {Number} containerIdx The index into the Container items of this Component.
  4509. *
  4510. * @private
  4511. */
  4512. finishRender: function(containerIdx) {
  4513. var me = this,
  4514. tpl, data, contentEl, el, pre, hide, target;
  4515. // We are typically called w/me.el==null as a child of some ownerCt that is being
  4516. // rendered. We are also called by render for a normal component (w/o a configured
  4517. // me.el). In this case, render sets me.el and me.rendering (indirectly). Lastly
  4518. // we are also called on a component (like a Viewport) that has a configured me.el
  4519. // (body for a Viewport) when render is called. In this case, it is not flagged as
  4520. // "me.rendering" yet becasue it does not produce a renderTree. We use this to know
  4521. // not to regen the renderTpl.
  4522. if (!me.el || me.$pid) {
  4523. if (me.container) {
  4524. el = me.container.getById(me.id, true);
  4525. } else {
  4526. el = Ext.getDom(me.id);
  4527. }
  4528. if (!me.el) {
  4529. // Typical case: we produced the el during render
  4530. me.wrapPrimaryEl(el);
  4531. } else {
  4532. // We were configured with an el and created a proxy, so now we can swap
  4533. // the proxy for me.el:
  4534. delete me.$pid;
  4535. if (!me.el.dom) {
  4536. // make sure me.el is an Element
  4537. me.wrapPrimaryEl(me.el);
  4538. }
  4539. el.parentNode.insertBefore(me.el.dom, el);
  4540. Ext.removeNode(el); // remove placeholder el
  4541. // TODO - what about class/style?
  4542. }
  4543. } else if (!me.rendering) {
  4544. // We were configured with an el and then told to render (e.g., Viewport). We
  4545. // need to generate the proper DOM. Insert first because the layout system
  4546. // insists that child Component elements indices match the Component indices.
  4547. tpl = me.initRenderTpl();
  4548. if (tpl) {
  4549. data = me.initRenderData();
  4550. tpl.insertFirst(me.getTargetEl(), data);
  4551. }
  4552. }
  4553. // else we are rendering
  4554. if (!me.container) {
  4555. // top-level rendered components will already have me.container set up
  4556. me.container = Ext.get(me.el.dom.parentNode);
  4557. }
  4558. if (me.ctCls) {
  4559. me.container.addCls(me.ctCls);
  4560. }
  4561. // Sets the rendered flag and clears the redering flag
  4562. me.onRender(me.container, containerIdx);
  4563. // Initialize with correct overflow attributes
  4564. target = me.getTargetEl();
  4565. target.setStyle(me.getOverflowStyle());
  4566. // Tell the encapsulating element to hide itself in the way the Component is configured to hide
  4567. // This means DISPLAY, VISIBILITY or OFFSETS.
  4568. me.el.setVisibilityMode(Ext.Element[me.hideMode.toUpperCase()]);
  4569. if (me.overCls) {
  4570. me.el.hover(me.addOverCls, me.removeOverCls, me);
  4571. }
  4572. if (me.hasListeners.render) {
  4573. me.fireEvent('render', me);
  4574. }
  4575. if (me.contentEl) {
  4576. pre = Ext.baseCSSPrefix;
  4577. hide = pre + 'hide-';
  4578. contentEl = Ext.get(me.contentEl);
  4579. contentEl.removeCls([pre+'hidden', hide+'display', hide+'offsets', hide+'nosize']);
  4580. target.appendChild(contentEl.dom);
  4581. }
  4582. me.afterRender(); // this can cause a layout
  4583. if (me.hasListeners.afterrender) {
  4584. me.fireEvent('afterrender', me);
  4585. }
  4586. me.initEvents();
  4587. if (me.hidden) {
  4588. // Hiding during the render process should not perform any ancillary
  4589. // actions that the full hide process does; It is not hiding, it begins in a hidden state.'
  4590. // So just make the element hidden according to the configured hideMode
  4591. me.el.hide();
  4592. }
  4593. },
  4594. finishRenderChildren: function () {
  4595. var layout = this.getComponentLayout();
  4596. layout.finishRender();
  4597. },
  4598. getElConfig : function() {
  4599. var me = this,
  4600. autoEl = me.autoEl,
  4601. frameInfo = me.getFrameInfo(),
  4602. config = {
  4603. tag: 'div',
  4604. id: me.id,
  4605. tpl: frameInfo ? me.initFramingTpl(frameInfo.table) : me.initRenderTpl()
  4606. };
  4607. me.initStyles(me.protoEl);
  4608. me.protoEl.writeTo(config);
  4609. me.protoEl.flush();
  4610. if (Ext.isString(autoEl)) {
  4611. config.tag = autoEl;
  4612. } else {
  4613. Ext.apply(config, autoEl); // harmless if !autoEl
  4614. }
  4615. if (config.tpl) {
  4616. // Use the framingTpl as the main content creating template. It will call out to this.applyRenderTpl(out, values)
  4617. if (frameInfo) {
  4618. var i,
  4619. frameElNames = me.frameElNames,
  4620. len = frameElNames.length,
  4621. suffix,
  4622. frameGenId = me.id + '-frame1';
  4623. me.frameGenId = 1;
  4624. config.tplData = Ext.apply({}, {
  4625. $comp: me,
  4626. fgid: frameGenId,
  4627. ui: me.ui,
  4628. uiCls: me.uiCls,
  4629. frameCls: me.frameCls,
  4630. baseCls: me.baseCls,
  4631. frameWidth: frameInfo.maxWidth,
  4632. top: !!frameInfo.top,
  4633. left: !!frameInfo.left,
  4634. right: !!frameInfo.right,
  4635. bottom: !!frameInfo.bottom,
  4636. renderData: me.initRenderData()
  4637. }, me.getFramePositions(frameInfo));
  4638. // Add the childEls for each of the frame elements
  4639. for (i = 0; i < len; i++) {
  4640. suffix = frameElNames[i];
  4641. me.addChildEls({ name: 'frame' + suffix, id: frameGenId + suffix });
  4642. }
  4643. // Panel must have a frameBody
  4644. me.addChildEls({
  4645. name: 'frameBody',
  4646. id: frameGenId + 'MC'
  4647. });
  4648. } else {
  4649. config.tplData = me.initRenderData();
  4650. }
  4651. }
  4652. return config;
  4653. },
  4654. // Create the framingTpl from the string.
  4655. // Poke in a reference to applyRenderTpl(frameInfo, out)
  4656. initFramingTpl: function(table) {
  4657. var tpl = table ? this.getTpl('frameTableTpl') : this.getTpl('frameTpl');
  4658. if (tpl && !tpl.applyRenderTpl) {
  4659. this.setupFramingTpl(tpl);
  4660. }
  4661. return tpl;
  4662. },
  4663. /**
  4664. * @private
  4665. * Inject a reference to the function which applies the render template into the framing template. The framing template
  4666. * wraps the content.
  4667. */
  4668. setupFramingTpl: function(frameTpl) {
  4669. frameTpl.applyRenderTpl = this.doApplyRenderTpl;
  4670. frameTpl.renderDockedItems = this.doRenderFramingDockedItems;
  4671. },
  4672. /**
  4673. * This function takes the position argument passed to onRender and returns a
  4674. * DOM element that you can use in the insertBefore.
  4675. * @param {String/Number/Ext.dom.Element/HTMLElement} position Index, element id or element you want
  4676. * to put this component before.
  4677. * @return {HTMLElement} DOM element that you can use in the insertBefore
  4678. */
  4679. getInsertPosition: function(position) {
  4680. // Convert the position to an element to insert before
  4681. if (position !== undefined) {
  4682. if (Ext.isNumber(position)) {
  4683. position = this.container.dom.childNodes[position];
  4684. }
  4685. else {
  4686. position = Ext.getDom(position);
  4687. }
  4688. }
  4689. return position;
  4690. },
  4691. getRenderTree: function() {
  4692. var me = this;
  4693. me.beforeRender();
  4694. if (!me.hasListeners.beforerender || me.fireEvent('beforerender', me) !== false) {
  4695. // Flag to let the layout's finishRenderItems and afterFinishRenderItems
  4696. // know which items to process
  4697. me.rendering = true;
  4698. if (me.el) {
  4699. // Since we are producing a render tree, we produce a "proxy el" that will
  4700. // sit in the rendered DOM precisely where me.el belongs. We replace the
  4701. // proxy el in the finishRender phase.
  4702. return {
  4703. tag: 'div',
  4704. id: (me.$pid = Ext.id())
  4705. };
  4706. }
  4707. return me.getElConfig();
  4708. }
  4709. return null;
  4710. },
  4711. initContainer: function(container) {
  4712. var me = this;
  4713. // If you render a component specifying the el, we get the container
  4714. // of the el, and make sure we dont move the el around in the dom
  4715. // during the render
  4716. if (!container && me.el) {
  4717. container = me.el.dom.parentNode;
  4718. me.allowDomMove = false;
  4719. }
  4720. me.container = container.dom ? container : Ext.get(container);
  4721. return me.container;
  4722. },
  4723. /**
  4724. * Initialized the renderData to be used when rendering the renderTpl.
  4725. * @return {Object} Object with keys and values that are going to be applied to the renderTpl
  4726. * @private
  4727. */
  4728. initRenderData: function() {
  4729. var me = this;
  4730. return Ext.apply({
  4731. $comp: me,
  4732. id: me.id,
  4733. ui: me.ui,
  4734. uiCls: me.uiCls,
  4735. baseCls: me.baseCls,
  4736. componentCls: me.componentCls,
  4737. frame: me.frame
  4738. }, me.renderData);
  4739. },
  4740. /**
  4741. * Initializes the renderTpl.
  4742. * @return {Ext.XTemplate} The renderTpl XTemplate instance.
  4743. * @private
  4744. */
  4745. initRenderTpl: function() {
  4746. var tpl = this.getTpl('renderTpl');
  4747. if (tpl && !tpl.renderContent) {
  4748. this.setupRenderTpl(tpl);
  4749. }
  4750. return tpl;
  4751. },
  4752. /**
  4753. * Template method called when this Component's DOM structure is created.
  4754. *
  4755. * At this point, this Component's (and all descendants') DOM structure *exists* but it has not
  4756. * been layed out (positioned and sized).
  4757. *
  4758. * Subclasses which override this to gain access to the structure at render time should
  4759. * call the parent class's method before attempting to access any child elements of the Component.
  4760. *
  4761. * @param {Ext.core.Element} parentNode The parent Element in which this Component's encapsulating element is contained.
  4762. * @param {Number} containerIdx The index within the parent Container's child collection of this Component.
  4763. *
  4764. * @template
  4765. * @protected
  4766. */
  4767. onRender: function(parentNode, containerIdx) {
  4768. var me = this,
  4769. x = me.x,
  4770. y = me.y,
  4771. lastBox, width, height,
  4772. el = me.el;
  4773. // After the container property has been collected, we can wrap the Component in a reset wraper if necessary
  4774. if (Ext.scopeResetCSS && !me.ownerCt) {
  4775. // If this component's el is the body element, we add the reset class to the html tag
  4776. if (el.dom == Ext.getBody().dom) {
  4777. el.parent().addCls(Ext.resetCls);
  4778. }
  4779. else {
  4780. // Else we wrap this element in an element that adds the reset class.
  4781. me.resetEl = el.wrap({
  4782. cls: Ext.resetCls
  4783. });
  4784. }
  4785. }
  4786. me.applyRenderSelectors();
  4787. // Flag set on getRenderTree to flag to the layout's postprocessing routine that
  4788. // the Component is in the process of being rendered and needs postprocessing.
  4789. delete me.rendering;
  4790. me.rendered = true;
  4791. // We need to remember these to avoid writing them during the initial layout:
  4792. lastBox = null;
  4793. if (x !== undefined) {
  4794. lastBox = lastBox || {};
  4795. lastBox.x = x;
  4796. }
  4797. if (y !== undefined) {
  4798. lastBox = lastBox || {};
  4799. lastBox.y = y;
  4800. }
  4801. // Framed components need their width/height to apply to the frame, which is
  4802. // best handled in layout at present.
  4803. // If we're using the content box model, we also cannot assign initial sizes since we do not know the border widths to subtract
  4804. if (!me.getFrameInfo() && Ext.isBorderBox) {
  4805. width = me.width;
  4806. height = me.height;
  4807. if (typeof width == 'number') {
  4808. lastBox = lastBox || {};
  4809. lastBox.width = width;
  4810. }
  4811. if (typeof height == 'number') {
  4812. lastBox = lastBox || {};
  4813. lastBox.height = height;
  4814. }
  4815. }
  4816. me.lastBox = me.el.lastBox = lastBox;
  4817. },
  4818. render: function(container, position) {
  4819. var me = this,
  4820. el = me.el && (me.el = Ext.get(me.el)), // ensure me.el is wrapped
  4821. tree,
  4822. nextSibling;
  4823. Ext.suspendLayouts();
  4824. container = me.initContainer(container);
  4825. nextSibling = me.getInsertPosition(position);
  4826. if (!el) {
  4827. tree = me.getRenderTree();
  4828. if (nextSibling) {
  4829. el = Ext.DomHelper.insertBefore(nextSibling, tree);
  4830. } else {
  4831. el = Ext.DomHelper.append(container, tree);
  4832. }
  4833. me.wrapPrimaryEl(el);
  4834. } else {
  4835. // Set configured styles on pre-rendered Component's element
  4836. me.initStyles(el);
  4837. if (me.allowDomMove !== false) {
  4838. //debugger; // TODO
  4839. if (nextSibling) {
  4840. container.dom.insertBefore(el.dom, nextSibling);
  4841. } else {
  4842. container.dom.appendChild(el.dom);
  4843. }
  4844. }
  4845. }
  4846. me.finishRender(position);
  4847. Ext.resumeLayouts(!container.isDetachedBody);
  4848. },
  4849. /**
  4850. * Ensures that this component is attached to `document.body`. If the component was
  4851. * rendered to {@link Ext#getDetachedBody}, then it will be appended to `document.body`.
  4852. * Any configured position is also restored.
  4853. * @param {Boolean} [runLayout=false] True to run the component's layout.
  4854. */
  4855. ensureAttachedToBody: function (runLayout) {
  4856. var comp = this,
  4857. body;
  4858. while (comp.ownerCt) {
  4859. comp = comp.ownerCt;
  4860. }
  4861. if (comp.container.isDetachedBody) {
  4862. comp.container = body = Ext.getBody();
  4863. body.appendChild(comp.el.dom);
  4864. if (runLayout) {
  4865. comp.updateLayout();
  4866. }
  4867. if (typeof comp.x == 'number' || typeof comp.y == 'number') {
  4868. comp.setPosition(comp.x, comp.y);
  4869. }
  4870. }
  4871. },
  4872. setupRenderTpl: function (renderTpl) {
  4873. renderTpl.renderBody = renderTpl.renderContent = this.doRenderContent;
  4874. },
  4875. wrapPrimaryEl: function (dom) {
  4876. this.el = Ext.get(dom, true);
  4877. },
  4878. /**
  4879. * @private
  4880. */
  4881. initFrame : function() {
  4882. if (Ext.supports.CSS3BorderRadius) {
  4883. return;
  4884. }
  4885. var me = this,
  4886. frameInfo = me.getFrameInfo(),
  4887. frameWidth, frameTpl, frameGenId,
  4888. i,
  4889. frameElNames = me.frameElNames,
  4890. len = frameElNames.length,
  4891. suffix;
  4892. if (frameInfo) {
  4893. frameWidth = frameInfo.maxWidth;
  4894. frameTpl = me.getFrameTpl(frameInfo.table);
  4895. // since we render id's into the markup and id's NEED to be unique, we have a
  4896. // simple strategy for numbering their generations.
  4897. me.frameGenId = frameGenId = (me.frameGenId || 0) + 1;
  4898. frameGenId = me.id + '-frame' + frameGenId;
  4899. // Here we render the frameTpl to this component. This inserts the 9point div or the table framing.
  4900. frameTpl.insertFirst(me.el, Ext.apply({
  4901. $comp: me,
  4902. fgid: frameGenId,
  4903. ui: me.ui,
  4904. uiCls: me.uiCls,
  4905. frameCls: me.frameCls,
  4906. baseCls: me.baseCls,
  4907. frameWidth: frameWidth,
  4908. top: !!frameInfo.top,
  4909. left: !!frameInfo.left,
  4910. right: !!frameInfo.right,
  4911. bottom: !!frameInfo.bottom
  4912. }, me.getFramePositions(frameInfo)));
  4913. // The frameBody is returned in getTargetEl, so that layouts render items to the correct target.
  4914. me.frameBody = me.el.down('.' + me.frameCls + '-mc');
  4915. // Clean out the childEls for the old frame elements (the majority of the els)
  4916. me.removeChildEls(function (c) {
  4917. return c.id && me.frameIdRegex.test(c.id);
  4918. });
  4919. // Grab references to the childEls for each of the new frame elements
  4920. for (i = 0; i < len; i++) {
  4921. suffix = frameElNames[i];
  4922. me['frame' + suffix] = me.el.getById(frameGenId + suffix);
  4923. }
  4924. }
  4925. },
  4926. updateFrame: function() {
  4927. if (Ext.supports.CSS3BorderRadius) {
  4928. return;
  4929. }
  4930. var me = this,
  4931. wasTable = this.frameSize && this.frameSize.table,
  4932. oldFrameTL = this.frameTL,
  4933. oldFrameBL = this.frameBL,
  4934. oldFrameML = this.frameML,
  4935. oldFrameMC = this.frameMC,
  4936. newMCClassName;
  4937. this.initFrame();
  4938. if (oldFrameMC) {
  4939. if (me.frame) {
  4940. // Store the class names set on the new MC
  4941. newMCClassName = this.frameMC.dom.className;
  4942. // Framing elements have been selected in initFrame, no need to run applyRenderSelectors
  4943. // Replace the new mc with the old mc
  4944. oldFrameMC.insertAfter(this.frameMC);
  4945. this.frameMC.remove();
  4946. // Restore the reference to the old frame mc as the framebody
  4947. this.frameBody = this.frameMC = oldFrameMC;
  4948. // Apply the new mc classes to the old mc element
  4949. oldFrameMC.dom.className = newMCClassName;
  4950. // Remove the old framing
  4951. if (wasTable) {
  4952. me.el.query('> table')[1].remove();
  4953. }
  4954. else {
  4955. if (oldFrameTL) {
  4956. oldFrameTL.remove();
  4957. }
  4958. if (oldFrameBL) {
  4959. oldFrameBL.remove();
  4960. }
  4961. if (oldFrameML) {
  4962. oldFrameML.remove();
  4963. }
  4964. }
  4965. }
  4966. else {
  4967. // We were framed but not anymore. Move all content from the old frame to the body
  4968. }
  4969. }
  4970. else if (me.frame) {
  4971. this.applyRenderSelectors();
  4972. }
  4973. },
  4974. /**
  4975. * @private
  4976. * On render, reads an encoded style attribute, "background-position" from the style of this Component's element.
  4977. * This information is memoized based upon the CSS class name of this Component's element.
  4978. * Because child Components are rendered as textual HTML as part of the topmost Container, a dummy div is inserted
  4979. * into the document to receive the document element's CSS class name, and therefore style attributes.
  4980. */
  4981. getFrameInfo: function() {
  4982. // If native framing can be used, or this Component is not configured (or written) to be framed,
  4983. // then do not attempt to read CSS framing info.
  4984. if (Ext.supports.CSS3BorderRadius) {
  4985. return false;
  4986. }
  4987. var me = this,
  4988. frameInfoCache = me.frameInfoCache,
  4989. el = me.el || me.protoEl,
  4990. cls = el.dom ? el.dom.className : el.classList.join(' '),
  4991. frameInfo = frameInfoCache[cls],
  4992. styleEl, left, top, info;
  4993. if (frameInfo == null) {
  4994. // Get the singleton frame style proxy with our el class name stamped into it.
  4995. styleEl = Ext.fly(me.getStyleProxy(cls), 'frame-style-el');
  4996. left = styleEl.getStyle('background-position-x');
  4997. top = styleEl.getStyle('background-position-y');
  4998. // Some browsers don't support background-position-x and y, so for those
  4999. // browsers let's split background-position into two parts.
  5000. if (!left && !top) {
  5001. info = styleEl.getStyle('background-position').split(' ');
  5002. left = info[0];
  5003. top = info[1];
  5004. }
  5005. frameInfo = me.calculateFrame(left, top);
  5006. if (frameInfo) {
  5007. // Just to be sure we set the background image of the el to none.
  5008. el.setStyle('background-image', 'none');
  5009. }
  5010. // This happens when you set frame: true explicitly without using the x-frame mixin in sass.
  5011. // This way IE can't figure out what sizes to use and thus framing can't work.
  5012. if (me.frame === true && !frameInfo) {
  5013. Ext.log.error('You have set frame: true explicity on this component (' + me.getXType() + ') and it ' +
  5014. 'does not have any framing defined in the CSS template. In this case IE cannot figure out ' +
  5015. 'what sizes to use and thus framing on this component will be disabled.');
  5016. }
  5017. frameInfoCache[cls] = frameInfo;
  5018. }
  5019. me.frame = !!frameInfo;
  5020. me.frameSize = frameInfo;
  5021. return frameInfo;
  5022. },
  5023. calculateFrame: function(left, top){
  5024. // We actually pass a string in the form of '[type][tl][tr]px [direction][br][bl]px' as
  5025. // the background position of this.el from the CSS to indicate to IE that this component needs
  5026. // framing. We parse it here.
  5027. if (!(parseInt(left, 10) >= 1000000 && parseInt(top, 10) >= 1000000)) {
  5028. return false;
  5029. }
  5030. var max = Math.max,
  5031. tl = parseInt(left.substr(3, 2), 10),
  5032. tr = parseInt(left.substr(5, 2), 10),
  5033. br = parseInt(top.substr(3, 2), 10),
  5034. bl = parseInt(top.substr(5, 2), 10),
  5035. frameInfo = {
  5036. // Table markup starts with 110, div markup with 100.
  5037. table: left.substr(0, 3) == '110',
  5038. // Determine if we are dealing with a horizontal or vertical component
  5039. vertical: top.substr(0, 3) == '110',
  5040. // Get and parse the different border radius sizes
  5041. top: max(tl, tr),
  5042. right: max(tr, br),
  5043. bottom: max(bl, br),
  5044. left: max(tl, bl)
  5045. };
  5046. frameInfo.maxWidth = max(frameInfo.top, frameInfo.right, frameInfo.bottom, frameInfo.left);
  5047. frameInfo.width = frameInfo.left + frameInfo.right;
  5048. frameInfo.height = frameInfo.top + frameInfo.bottom;
  5049. return frameInfo;
  5050. },
  5051. /**
  5052. * @private
  5053. * Returns an offscreen div with the same class name as the element this is being rendered.
  5054. * This is because child item rendering takes place in a detached div which, being ot part of the document, has no styling.
  5055. */
  5056. getStyleProxy: function(cls) {
  5057. var result = this.styleProxyEl || (Ext.AbstractComponent.prototype.styleProxyEl = Ext.getBody().createChild({
  5058. style: {
  5059. position: 'absolute',
  5060. top: '-10000px'
  5061. }
  5062. }, null, true));
  5063. result.className = cls;
  5064. return result;
  5065. },
  5066. getFramePositions: function(frameInfo) {
  5067. var me = this,
  5068. frameWidth = frameInfo.maxWidth,
  5069. dock = me.dock,
  5070. positions, tc, bc, ml, mr;
  5071. if (frameInfo.vertical) {
  5072. tc = '0 -' + (frameWidth * 0) + 'px';
  5073. bc = '0 -' + (frameWidth * 1) + 'px';
  5074. if (dock && dock == "right") {
  5075. tc = 'right -' + (frameWidth * 0) + 'px';
  5076. bc = 'right -' + (frameWidth * 1) + 'px';
  5077. }
  5078. positions = {
  5079. tl: '0 -' + (frameWidth * 0) + 'px',
  5080. tr: '0 -' + (frameWidth * 1) + 'px',
  5081. bl: '0 -' + (frameWidth * 2) + 'px',
  5082. br: '0 -' + (frameWidth * 3) + 'px',
  5083. ml: '-' + (frameWidth * 1) + 'px 0',
  5084. mr: 'right 0',
  5085. tc: tc,
  5086. bc: bc
  5087. };
  5088. } else {
  5089. ml = '-' + (frameWidth * 0) + 'px 0';
  5090. mr = 'right 0';
  5091. if (dock && dock == "bottom") {
  5092. ml = 'left bottom';
  5093. mr = 'right bottom';
  5094. }
  5095. positions = {
  5096. tl: '0 -' + (frameWidth * 2) + 'px',
  5097. tr: 'right -' + (frameWidth * 3) + 'px',
  5098. bl: '0 -' + (frameWidth * 4) + 'px',
  5099. br: 'right -' + (frameWidth * 5) + 'px',
  5100. ml: ml,
  5101. mr: mr,
  5102. tc: '0 -' + (frameWidth * 0) + 'px',
  5103. bc: '0 -' + (frameWidth * 1) + 'px'
  5104. };
  5105. }
  5106. return positions;
  5107. },
  5108. /**
  5109. * @private
  5110. */
  5111. getFrameTpl : function(table) {
  5112. return this.getTpl(table ? 'frameTableTpl' : 'frameTpl');
  5113. },
  5114. // Cache the frame information object so as not to cause style recalculations
  5115. frameInfoCache: {}
  5116. });
  5117. /**
  5118. * Provides searching of Components within Ext.ComponentManager (globally) or a specific
  5119. * Ext.container.Container on the document with a similar syntax to a CSS selector.
  5120. *
  5121. * Components can be retrieved by using their {@link Ext.Component xtype} with an optional . prefix
  5122. *
  5123. * - `component` or `.component`
  5124. * - `gridpanel` or `.gridpanel`
  5125. *
  5126. * An itemId or id must be prefixed with a #
  5127. *
  5128. * - `#myContainer`
  5129. *
  5130. * Attributes must be wrapped in brackets
  5131. *
  5132. * - `component[autoScroll]`
  5133. * - `panel[title="Test"]`
  5134. *
  5135. * Member expressions from candidate Components may be tested. If the expression returns a *truthy* value,
  5136. * the candidate Component will be included in the query:
  5137. *
  5138. * var disabledFields = myFormPanel.query("{isDisabled()}");
  5139. *
  5140. * Pseudo classes may be used to filter results in the same way as in {@link Ext.DomQuery DomQuery}:
  5141. *
  5142. * // Function receives array and returns a filtered array.
  5143. * Ext.ComponentQuery.pseudos.invalid = function(items) {
  5144. * var i = 0, l = items.length, c, result = [];
  5145. * for (; i < l; i++) {
  5146. * if (!(c = items[i]).isValid()) {
  5147. * result.push(c);
  5148. * }
  5149. * }
  5150. * return result;
  5151. * };
  5152. *
  5153. * var invalidFields = myFormPanel.query('field:invalid');
  5154. * if (invalidFields.length) {
  5155. * invalidFields[0].getEl().scrollIntoView(myFormPanel.body);
  5156. * for (var i = 0, l = invalidFields.length; i < l; i++) {
  5157. * invalidFields[i].getEl().frame("red");
  5158. * }
  5159. * }
  5160. *
  5161. * Default pseudos include:
  5162. *
  5163. * - not
  5164. * - last
  5165. *
  5166. * Queries return an array of components.
  5167. * Here are some example queries.
  5168. *
  5169. * // retrieve all Ext.Panels in the document by xtype
  5170. * var panelsArray = Ext.ComponentQuery.query('panel');
  5171. *
  5172. * // retrieve all Ext.Panels within the container with an id myCt
  5173. * var panelsWithinmyCt = Ext.ComponentQuery.query('#myCt panel');
  5174. *
  5175. * // retrieve all direct children which are Ext.Panels within myCt
  5176. * var directChildPanel = Ext.ComponentQuery.query('#myCt > panel');
  5177. *
  5178. * // retrieve all grids and trees
  5179. * var gridsAndTrees = Ext.ComponentQuery.query('gridpanel, treepanel');
  5180. *
  5181. * For easy access to queries based from a particular Container see the {@link Ext.container.Container#query},
  5182. * {@link Ext.container.Container#down} and {@link Ext.container.Container#child} methods. Also see
  5183. * {@link Ext.Component#up}.
  5184. */
  5185. Ext.define('Ext.ComponentQuery', {
  5186. singleton: true,
  5187. uses: ['Ext.ComponentManager']
  5188. }, function() {
  5189. var cq = this,
  5190. // A function source code pattern with a placeholder which accepts an expression which yields a truth value when applied
  5191. // as a member on each item in the passed array.
  5192. filterFnPattern = [
  5193. 'var r = [],',
  5194. 'i = 0,',
  5195. 'it = items,',
  5196. 'l = it.length,',
  5197. 'c;',
  5198. 'for (; i < l; i++) {',
  5199. 'c = it[i];',
  5200. 'if (c.{0}) {',
  5201. 'r.push(c);',
  5202. '}',
  5203. '}',
  5204. 'return r;'
  5205. ].join(''),
  5206. filterItems = function(items, operation) {
  5207. // Argument list for the operation is [ itemsArray, operationArg1, operationArg2...]
  5208. // The operation's method loops over each item in the candidate array and
  5209. // returns an array of items which match its criteria
  5210. return operation.method.apply(this, [ items ].concat(operation.args));
  5211. },
  5212. getItems = function(items, mode) {
  5213. var result = [],
  5214. i = 0,
  5215. length = items.length,
  5216. candidate,
  5217. deep = mode !== '>';
  5218. for (; i < length; i++) {
  5219. candidate = items[i];
  5220. if (candidate.getRefItems) {
  5221. result = result.concat(candidate.getRefItems(deep));
  5222. }
  5223. }
  5224. return result;
  5225. },
  5226. getAncestors = function(items) {
  5227. var result = [],
  5228. i = 0,
  5229. length = items.length,
  5230. candidate;
  5231. for (; i < length; i++) {
  5232. candidate = items[i];
  5233. while (!!(candidate = (candidate.ownerCt || candidate.floatParent))) {
  5234. result.push(candidate);
  5235. }
  5236. }
  5237. return result;
  5238. },
  5239. // Filters the passed candidate array and returns only items which match the passed xtype
  5240. filterByXType = function(items, xtype, shallow) {
  5241. if (xtype === '*') {
  5242. return items.slice();
  5243. }
  5244. else {
  5245. var result = [],
  5246. i = 0,
  5247. length = items.length,
  5248. candidate;
  5249. for (; i < length; i++) {
  5250. candidate = items[i];
  5251. if (candidate.isXType(xtype, shallow)) {
  5252. result.push(candidate);
  5253. }
  5254. }
  5255. return result;
  5256. }
  5257. },
  5258. // Filters the passed candidate array and returns only items which have the passed className
  5259. filterByClassName = function(items, className) {
  5260. var EA = Ext.Array,
  5261. result = [],
  5262. i = 0,
  5263. length = items.length,
  5264. candidate;
  5265. for (; i < length; i++) {
  5266. candidate = items[i];
  5267. if (candidate.hasCls(className)) {
  5268. result.push(candidate);
  5269. }
  5270. }
  5271. return result;
  5272. },
  5273. // Filters the passed candidate array and returns only items which have the specified property match
  5274. filterByAttribute = function(items, property, operator, value) {
  5275. var result = [],
  5276. i = 0,
  5277. length = items.length,
  5278. candidate;
  5279. for (; i < length; i++) {
  5280. candidate = items[i];
  5281. if (!value ? !!candidate[property] : (String(candidate[property]) === value)) {
  5282. result.push(candidate);
  5283. }
  5284. }
  5285. return result;
  5286. },
  5287. // Filters the passed candidate array and returns only items which have the specified itemId or id
  5288. filterById = function(items, id) {
  5289. var result = [],
  5290. i = 0,
  5291. length = items.length,
  5292. candidate;
  5293. for (; i < length; i++) {
  5294. candidate = items[i];
  5295. if (candidate.getItemId() === id) {
  5296. result.push(candidate);
  5297. }
  5298. }
  5299. return result;
  5300. },
  5301. // Filters the passed candidate array and returns only items which the named pseudo class matcher filters in
  5302. filterByPseudo = function(items, name, value) {
  5303. return cq.pseudos[name](items, value);
  5304. },
  5305. // Determines leading mode
  5306. // > for direct child, and ^ to switch to ownerCt axis
  5307. modeRe = /^(\s?([>\^])\s?|\s|$)/,
  5308. // Matches a token with possibly (true|false) appended for the "shallow" parameter
  5309. tokenRe = /^(#)?([\w\-]+|\*)(?:\((true|false)\))?/,
  5310. matchers = [{
  5311. // Checks for .xtype with possibly (true|false) appended for the "shallow" parameter
  5312. re: /^\.([\w\-]+)(?:\((true|false)\))?/,
  5313. method: filterByXType
  5314. },{
  5315. // checks for [attribute=value]
  5316. re: /^(?:[\[](?:@)?([\w\-]+)\s?(?:(=|.=)\s?['"]?(.*?)["']?)?[\]])/,
  5317. method: filterByAttribute
  5318. }, {
  5319. // checks for #cmpItemId
  5320. re: /^#([\w\-]+)/,
  5321. method: filterById
  5322. }, {
  5323. // checks for :<pseudo_class>(<selector>)
  5324. re: /^\:([\w\-]+)(?:\(((?:\{[^\}]+\})|(?:(?!\{)[^\s>\/]*?(?!\})))\))?/,
  5325. method: filterByPseudo
  5326. }, {
  5327. // checks for {<member_expression>}
  5328. re: /^(?:\{([^\}]+)\})/,
  5329. method: filterFnPattern
  5330. }];
  5331. // Internal class Ext.ComponentQuery.Query
  5332. cq.Query = Ext.extend(Object, {
  5333. constructor: function(cfg) {
  5334. cfg = cfg || {};
  5335. Ext.apply(this, cfg);
  5336. },
  5337. // Executes this Query upon the selected root.
  5338. // The root provides the initial source of candidate Component matches which are progressively
  5339. // filtered by iterating through this Query's operations cache.
  5340. // If no root is provided, all registered Components are searched via the ComponentManager.
  5341. // root may be a Container who's descendant Components are filtered
  5342. // root may be a Component with an implementation of getRefItems which provides some nested Components such as the
  5343. // docked items within a Panel.
  5344. // root may be an array of candidate Components to filter using this Query.
  5345. execute : function(root) {
  5346. var operations = this.operations,
  5347. i = 0,
  5348. length = operations.length,
  5349. operation,
  5350. workingItems;
  5351. // no root, use all Components in the document
  5352. if (!root) {
  5353. workingItems = Ext.ComponentManager.all.getArray();
  5354. }
  5355. // Root is a candidate Array
  5356. else if (Ext.isArray(root)) {
  5357. workingItems = root;
  5358. }
  5359. // We are going to loop over our operations and take care of them
  5360. // one by one.
  5361. for (; i < length; i++) {
  5362. operation = operations[i];
  5363. // The mode operation requires some custom handling.
  5364. // All other operations essentially filter down our current
  5365. // working items, while mode replaces our current working
  5366. // items by getting children from each one of our current
  5367. // working items. The type of mode determines the type of
  5368. // children we get. (e.g. > only gets direct children)
  5369. if (operation.mode === '^') {
  5370. workingItems = getAncestors(workingItems || [root]);
  5371. }
  5372. else if (operation.mode) {
  5373. workingItems = getItems(workingItems || [root], operation.mode);
  5374. }
  5375. else {
  5376. workingItems = filterItems(workingItems || getItems([root]), operation);
  5377. }
  5378. // If this is the last operation, it means our current working
  5379. // items are the final matched items. Thus return them!
  5380. if (i === length -1) {
  5381. return workingItems;
  5382. }
  5383. }
  5384. return [];
  5385. },
  5386. is: function(component) {
  5387. var operations = this.operations,
  5388. components = Ext.isArray(component) ? component : [component],
  5389. originalLength = components.length,
  5390. lastOperation = operations[operations.length-1],
  5391. ln, i;
  5392. components = filterItems(components, lastOperation);
  5393. if (components.length === originalLength) {
  5394. if (operations.length > 1) {
  5395. for (i = 0, ln = components.length; i < ln; i++) {
  5396. if (Ext.Array.indexOf(this.execute(), components[i]) === -1) {
  5397. return false;
  5398. }
  5399. }
  5400. }
  5401. return true;
  5402. }
  5403. return false;
  5404. }
  5405. });
  5406. Ext.apply(this, {
  5407. // private cache of selectors and matching ComponentQuery.Query objects
  5408. cache: {},
  5409. // private cache of pseudo class filter functions
  5410. pseudos: {
  5411. not: function(components, selector){
  5412. var CQ = Ext.ComponentQuery,
  5413. i = 0,
  5414. length = components.length,
  5415. results = [],
  5416. index = -1,
  5417. component;
  5418. for(; i < length; ++i) {
  5419. component = components[i];
  5420. if (!CQ.is(component, selector)) {
  5421. results[++index] = component;
  5422. }
  5423. }
  5424. return results;
  5425. },
  5426. last: function(components) {
  5427. return components[components.length - 1];
  5428. }
  5429. },
  5430. /**
  5431. * Returns an array of matched Components from within the passed root object.
  5432. *
  5433. * This method filters returned Components in a similar way to how CSS selector based DOM
  5434. * queries work using a textual selector string.
  5435. *
  5436. * See class summary for details.
  5437. *
  5438. * @param {String} selector The selector string to filter returned Components
  5439. * @param {Ext.container.Container} root The Container within which to perform the query.
  5440. * If omitted, all Components within the document are included in the search.
  5441. *
  5442. * This parameter may also be an array of Components to filter according to the selector.</p>
  5443. * @returns {Ext.Component[]} The matched Components.
  5444. *
  5445. * @member Ext.ComponentQuery
  5446. */
  5447. query: function(selector, root) {
  5448. var selectors = selector.split(','),
  5449. length = selectors.length,
  5450. i = 0,
  5451. results = [],
  5452. noDupResults = [],
  5453. dupMatcher = {},
  5454. query, resultsLn, cmp;
  5455. for (; i < length; i++) {
  5456. selector = Ext.String.trim(selectors[i]);
  5457. query = this.cache[selector];
  5458. if (!query) {
  5459. this.cache[selector] = query = this.parse(selector);
  5460. }
  5461. results = results.concat(query.execute(root));
  5462. }
  5463. // multiple selectors, potential to find duplicates
  5464. // lets filter them out.
  5465. if (length > 1) {
  5466. resultsLn = results.length;
  5467. for (i = 0; i < resultsLn; i++) {
  5468. cmp = results[i];
  5469. if (!dupMatcher[cmp.id]) {
  5470. noDupResults.push(cmp);
  5471. dupMatcher[cmp.id] = true;
  5472. }
  5473. }
  5474. results = noDupResults;
  5475. }
  5476. return results;
  5477. },
  5478. /**
  5479. * Tests whether the passed Component matches the selector string.
  5480. * @param {Ext.Component} component The Component to test
  5481. * @param {String} selector The selector string to test against.
  5482. * @return {Boolean} True if the Component matches the selector.
  5483. * @member Ext.ComponentQuery
  5484. */
  5485. is: function(component, selector) {
  5486. if (!selector) {
  5487. return true;
  5488. }
  5489. var query = this.cache[selector];
  5490. if (!query) {
  5491. this.cache[selector] = query = this.parse(selector);
  5492. }
  5493. return query.is(component);
  5494. },
  5495. parse: function(selector) {
  5496. var operations = [],
  5497. length = matchers.length,
  5498. lastSelector,
  5499. tokenMatch,
  5500. matchedChar,
  5501. modeMatch,
  5502. selectorMatch,
  5503. i, matcher, method;
  5504. // We are going to parse the beginning of the selector over and
  5505. // over again, slicing off the selector any portions we converted into an
  5506. // operation, until it is an empty string.
  5507. while (selector && lastSelector !== selector) {
  5508. lastSelector = selector;
  5509. // First we check if we are dealing with a token like #, * or an xtype
  5510. tokenMatch = selector.match(tokenRe);
  5511. if (tokenMatch) {
  5512. matchedChar = tokenMatch[1];
  5513. // If the token is prefixed with a # we push a filterById operation to our stack
  5514. if (matchedChar === '#') {
  5515. operations.push({
  5516. method: filterById,
  5517. args: [Ext.String.trim(tokenMatch[2])]
  5518. });
  5519. }
  5520. // If the token is prefixed with a . we push a filterByClassName operation to our stack
  5521. // FIXME: Not enabled yet. just needs \. adding to the tokenRe prefix
  5522. else if (matchedChar === '.') {
  5523. operations.push({
  5524. method: filterByClassName,
  5525. args: [Ext.String.trim(tokenMatch[2])]
  5526. });
  5527. }
  5528. // If the token is a * or an xtype string, we push a filterByXType
  5529. // operation to the stack.
  5530. else {
  5531. operations.push({
  5532. method: filterByXType,
  5533. args: [Ext.String.trim(tokenMatch[2]), Boolean(tokenMatch[3])]
  5534. });
  5535. }
  5536. // Now we slice of the part we just converted into an operation
  5537. selector = selector.replace(tokenMatch[0], '');
  5538. }
  5539. // If the next part of the query is not a space or > or ^, it means we
  5540. // are going to check for more things that our current selection
  5541. // has to comply to.
  5542. while (!(modeMatch = selector.match(modeRe))) {
  5543. // Lets loop over each type of matcher and execute it
  5544. // on our current selector.
  5545. for (i = 0; selector && i < length; i++) {
  5546. matcher = matchers[i];
  5547. selectorMatch = selector.match(matcher.re);
  5548. method = matcher.method;
  5549. // If we have a match, add an operation with the method
  5550. // associated with this matcher, and pass the regular
  5551. // expression matches are arguments to the operation.
  5552. if (selectorMatch) {
  5553. operations.push({
  5554. method: Ext.isString(matcher.method)
  5555. // Turn a string method into a function by formatting the string with our selector matche expression
  5556. // A new method is created for different match expressions, eg {id=='textfield-1024'}
  5557. // Every expression may be different in different selectors.
  5558. ? Ext.functionFactory('items', Ext.String.format.apply(Ext.String, [method].concat(selectorMatch.slice(1))))
  5559. : matcher.method,
  5560. args: selectorMatch.slice(1)
  5561. });
  5562. selector = selector.replace(selectorMatch[0], '');
  5563. break; // Break on match
  5564. }
  5565. // Exhausted all matches: It's an error
  5566. if (i === (length - 1)) {
  5567. Ext.Error.raise('Invalid ComponentQuery selector: "' + arguments[0] + '"');
  5568. }
  5569. }
  5570. }
  5571. // Now we are going to check for a mode change. This means a space
  5572. // or a > to determine if we are going to select all the children
  5573. // of the currently matched items, or a ^ if we are going to use the
  5574. // ownerCt axis as the candidate source.
  5575. if (modeMatch[1]) { // Assignment, and test for truthiness!
  5576. operations.push({
  5577. mode: modeMatch[2]||modeMatch[1]
  5578. });
  5579. selector = selector.replace(modeMatch[0], '');
  5580. }
  5581. }
  5582. // Now that we have all our operations in an array, we are going
  5583. // to create a new Query using these operations.
  5584. return new cq.Query({
  5585. operations: operations
  5586. });
  5587. }
  5588. });
  5589. });
  5590. /**
  5591. * Manages certain element-like data prior to rendering. These values are passed
  5592. * on to the render process. This is currently used to manage the "class" and "style" attributes
  5593. * of a component's primary el as well as the bodyEl of panels. This allows things like
  5594. * addBodyCls in Panel to share logic with addCls in AbstractComponent.
  5595. * @private
  5596. */
  5597. /*
  5598. * The dirty implementation in this class is quite naive. The reasoning for this is that the dirty state
  5599. * will only be used in very specific circumstances, specifically, after the render process has begun but
  5600. * the component is not yet rendered to the DOM. As such, we want it to perform as quickly as possible
  5601. * so it's not as fully featured as you may expect.
  5602. */
  5603. Ext.define('Ext.util.ProtoElement', function () {
  5604. var splitWords = Ext.String.splitWords,
  5605. toMap = Ext.Array.toMap;
  5606. return {
  5607. isProtoEl: true,
  5608. /**
  5609. * The property name for the className on the data object passed to {@link #writeTo}.
  5610. */
  5611. clsProp: 'cls',
  5612. /**
  5613. * The property name for the style on the data object passed to {@link #writeTo}.
  5614. */
  5615. styleProp: 'style',
  5616. /**
  5617. * The property name for the removed classes on the data object passed to {@link #writeTo}.
  5618. */
  5619. removedProp: 'removed',
  5620. /**
  5621. * True if the style must be converted to text during {@link #writeTo}. When used to
  5622. * populate tpl data, this will be true. When used to populate {@link Ext.DomHelper}
  5623. * specs, this will be false (the default).
  5624. */
  5625. styleIsText: false,
  5626. constructor: function (config) {
  5627. var me = this;
  5628. Ext.apply(me, config);
  5629. me.classList = splitWords(me.cls);
  5630. me.classMap = toMap(me.classList);
  5631. delete me.cls;
  5632. if (Ext.isFunction(me.style)) {
  5633. me.styleFn = me.style;
  5634. delete me.style;
  5635. } else if (typeof me.style == 'string') {
  5636. me.style = Ext.Element.parseStyles(me.style);
  5637. } else if (me.style) {
  5638. me.style = Ext.apply({}, me.style); // don't edit the given object
  5639. }
  5640. },
  5641. /**
  5642. * Indicates that the current state of the object has been flushed to the DOM, so we need
  5643. * to track any subsequent changes
  5644. */
  5645. flush: function(){
  5646. this.flushClassList = [];
  5647. this.removedClasses = {};
  5648. // clear the style, it will be recreated if we add anything new
  5649. delete this.style;
  5650. },
  5651. /**
  5652. * Adds class to the element.
  5653. * @param {String} cls One or more classnames separated with spaces.
  5654. * @return {Ext.util.ProtoElement} this
  5655. */
  5656. addCls: function (cls) {
  5657. var me = this,
  5658. add = splitWords(cls),
  5659. length = add.length,
  5660. list = me.classList,
  5661. map = me.classMap,
  5662. flushList = me.flushClassList,
  5663. i = 0,
  5664. c;
  5665. for (; i < length; ++i) {
  5666. c = add[i];
  5667. if (!map[c]) {
  5668. map[c] = true;
  5669. list.push(c);
  5670. if (flushList) {
  5671. flushList.push(c);
  5672. delete me.removedClasses[c];
  5673. }
  5674. }
  5675. }
  5676. return me;
  5677. },
  5678. /**
  5679. * True if the element has given class.
  5680. * @param {String} cls
  5681. * @return {Boolean}
  5682. */
  5683. hasCls: function (cls) {
  5684. return cls in this.classMap;
  5685. },
  5686. /**
  5687. * Removes class from the element.
  5688. * @param {String} cls One or more classnames separated with spaces.
  5689. * @return {Ext.util.ProtoElement} this
  5690. */
  5691. removeCls: function (cls) {
  5692. var me = this,
  5693. list = me.classList,
  5694. newList = (me.classList = []),
  5695. remove = toMap(splitWords(cls)),
  5696. length = list.length,
  5697. map = me.classMap,
  5698. removedClasses = me.removedClasses,
  5699. i, c;
  5700. for (i = 0; i < length; ++i) {
  5701. c = list[i];
  5702. if (remove[c]) {
  5703. if (removedClasses) {
  5704. if (map[c]) {
  5705. removedClasses[c] = true;
  5706. Ext.Array.remove(me.flushClassList, c);
  5707. }
  5708. }
  5709. delete map[c];
  5710. } else {
  5711. newList.push(c);
  5712. }
  5713. }
  5714. return me;
  5715. },
  5716. /**
  5717. * Adds styles to the element.
  5718. * @param {String/Object} prop The style property to be set, or an object of multiple styles.
  5719. * @param {String} [value] The value to apply to the given property.
  5720. * @return {Ext.util.ProtoElement} this
  5721. */
  5722. setStyle: function (prop, value) {
  5723. var me = this,
  5724. style = me.style || (me.style = {});
  5725. if (typeof prop == 'string') {
  5726. if (arguments.length === 1) {
  5727. me.setStyle(Ext.Element.parseStyles(prop));
  5728. } else {
  5729. style[prop] = value;
  5730. }
  5731. } else {
  5732. Ext.apply(style, prop);
  5733. }
  5734. return me;
  5735. },
  5736. /**
  5737. * Writes style and class properties to given object.
  5738. * Styles will be written to {@link #styleProp} and class names to {@link #clsProp}.
  5739. * @param {Object} to
  5740. * @return {Object} to
  5741. */
  5742. writeTo: function (to) {
  5743. var me = this,
  5744. classList = me.flushClassList || me.classList,
  5745. removedClasses = me.removedClasses,
  5746. style;
  5747. if (me.styleFn) {
  5748. style = Ext.apply({}, me.styleFn());
  5749. Ext.apply(style, me.style);
  5750. } else {
  5751. style = me.style;
  5752. }
  5753. to[me.clsProp] = classList.join(' ');
  5754. if (style) {
  5755. to[me.styleProp] = me.styleIsText ? Ext.DomHelper.generateStyles(style) : style;
  5756. }
  5757. if (removedClasses) {
  5758. removedClasses = Ext.Object.getKeys(removedClasses);
  5759. if (removedClasses.length) {
  5760. to[me.removedProp] = removedClasses.join(' ');
  5761. }
  5762. }
  5763. return to;
  5764. }
  5765. };
  5766. }());
  5767. /**
  5768. * @author Ed Spencer
  5769. *
  5770. * Base Writer class used by most subclasses of {@link Ext.data.proxy.Server}. This class is responsible for taking a
  5771. * set of {@link Ext.data.Operation} objects and a {@link Ext.data.Request} object and modifying that request based on
  5772. * the Operations.
  5773. *
  5774. * For example a Ext.data.writer.Json would format the Operations and their {@link Ext.data.Model} instances based on
  5775. * the config options passed to the JsonWriter's constructor.
  5776. *
  5777. * Writers are not needed for any kind of local storage - whether via a {@link Ext.data.proxy.WebStorage Web Storage
  5778. * proxy} (see {@link Ext.data.proxy.LocalStorage localStorage} and {@link Ext.data.proxy.SessionStorage
  5779. * sessionStorage}) or just in memory via a {@link Ext.data.proxy.Memory MemoryProxy}.
  5780. */
  5781. Ext.define('Ext.data.writer.Writer', {
  5782. alias: 'writer.base',
  5783. alternateClassName: ['Ext.data.DataWriter', 'Ext.data.Writer'],
  5784. /**
  5785. * @cfg {Boolean} writeAllFields
  5786. * True to write all fields from the record to the server. If set to false it will only send the fields that were
  5787. * modified. Note that any fields that have {@link Ext.data.Field#persist} set to false will still be ignored.
  5788. */
  5789. writeAllFields: true,
  5790. /**
  5791. * @cfg {String} nameProperty
  5792. * This property is used to read the key for each value that will be sent to the server. For example:
  5793. *
  5794. * Ext.define('Person', {
  5795. * extend: 'Ext.data.Model',
  5796. * fields: [{
  5797. * name: 'first',
  5798. * mapping: 'firstName'
  5799. * }, {
  5800. * name: 'last',
  5801. * mapping: 'lastName'
  5802. * }, {
  5803. * name: 'age'
  5804. * }]
  5805. * });
  5806. * new Ext.data.writer.Writer({
  5807. * writeAllFields: true,
  5808. * nameProperty: 'mapping'
  5809. * });
  5810. *
  5811. * // This will be sent to the server
  5812. * {
  5813. * firstName: 'first name value',
  5814. * lastName: 'last name value',
  5815. * age: 1
  5816. * }
  5817. *
  5818. * If the value is not present, the field name will always be used.
  5819. */
  5820. nameProperty: 'name',
  5821. /**
  5822. * Creates new Writer.
  5823. * @param {Object} [config] Config object.
  5824. */
  5825. constructor: function(config) {
  5826. Ext.apply(this, config);
  5827. },
  5828. /**
  5829. * Prepares a Proxy's Ext.data.Request object
  5830. * @param {Ext.data.Request} request The request object
  5831. * @return {Ext.data.Request} The modified request object
  5832. */
  5833. write: function(request) {
  5834. var operation = request.operation,
  5835. records = operation.records || [],
  5836. len = records.length,
  5837. i = 0,
  5838. data = [];
  5839. for (; i < len; i++) {
  5840. data.push(this.getRecordData(records[i], operation));
  5841. }
  5842. return this.writeRecords(request, data);
  5843. },
  5844. /**
  5845. * Formats the data for each record before sending it to the server. This
  5846. * method should be overridden to format the data in a way that differs from the default.
  5847. * @param {Ext.data.Model} record The record that we are writing to the server.
  5848. * @param {Ext.data.Operation} [operation] An operation object.
  5849. * @return {Object} An object literal of name/value keys to be written to the server.
  5850. * By default this method returns the data property on the record.
  5851. */
  5852. getRecordData: function(record, operation) {
  5853. var isPhantom = record.phantom === true,
  5854. writeAll = this.writeAllFields || isPhantom,
  5855. nameProperty = this.nameProperty,
  5856. fields = record.fields,
  5857. fieldItems = fields.items,
  5858. data = {},
  5859. changes,
  5860. name,
  5861. field,
  5862. key,
  5863. f, fLen;
  5864. if (writeAll) {
  5865. fLen = fieldItems.length;
  5866. for (f = 0; f < fLen; f++) {
  5867. field = fieldItems[f];
  5868. if (field.persist) {
  5869. name = field[nameProperty] || field.name;
  5870. data[name] = record.get(field.name);
  5871. }
  5872. }
  5873. } else {
  5874. // Only write the changes
  5875. changes = record.getChanges();
  5876. for (key in changes) {
  5877. if (changes.hasOwnProperty(key)) {
  5878. field = fields.get(key);
  5879. name = field[nameProperty] || field.name;
  5880. data[name] = changes[key];
  5881. }
  5882. }
  5883. }
  5884. if(isPhantom) {
  5885. if(operation && operation.records.length > 1) {
  5886. // include clientId for phantom records, if multiple records are being written to the server in one operation.
  5887. // The server can then return the clientId with each record so the operation can match the server records with the client records
  5888. data[record.clientIdProperty] = record.internalId;
  5889. }
  5890. } else {
  5891. // always include the id for non phantoms
  5892. data[record.idProperty] = record.getId();
  5893. }
  5894. return data;
  5895. }
  5896. });
  5897. /**
  5898. * Handles mapping key events to handling functions for an element or a Component. One KeyMap can be used for multiple
  5899. * actions.
  5900. *
  5901. * A KeyMap must be configured with a {@link #target} as an event source which may be an Element or a Component.
  5902. *
  5903. * If the target is an element, then the `keydown` event will trigger the invocation of {@link #binding}s.
  5904. *
  5905. * It is possible to configure the KeyMap with a custom {@link #eventName} to listen for. This may be useful when the
  5906. * {@link #target} is a Component.
  5907. *
  5908. * The KeyMap's event handling requires that the first parameter passed is a key event. So if the Component's event
  5909. * signature is different, specify a {@link #processEvent} configuration which accepts the event's parameters and
  5910. * returns a key event.
  5911. *
  5912. * Functions specified in {@link #binding}s are called with this signature : `(String key, Ext.EventObject e)` (if the
  5913. * match is a multi-key combination the callback will still be called only once). A KeyMap can also handle a string
  5914. * representation of keys. By default KeyMap starts enabled.
  5915. *
  5916. * Usage:
  5917. *
  5918. * // map one key by key code
  5919. * var map = new Ext.util.KeyMap({
  5920. * target: "my-element",
  5921. * key: 13, // or Ext.EventObject.ENTER
  5922. * fn: myHandler,
  5923. * scope: myObject
  5924. * });
  5925. *
  5926. * // map multiple keys to one action by string
  5927. * var map = new Ext.util.KeyMap({
  5928. * target: "my-element",
  5929. * key: "a\r\n\t",
  5930. * fn: myHandler,
  5931. * scope: myObject
  5932. * });
  5933. *
  5934. * // map multiple keys to multiple actions by strings and array of codes
  5935. * var map = new Ext.util.KeyMap({
  5936. * target: "my-element",
  5937. * binding: [{
  5938. * key: [10,13],
  5939. * fn: function(){ alert("Return was pressed"); }
  5940. * }, {
  5941. * key: "abc",
  5942. * fn: function(){ alert('a, b or c was pressed'); }
  5943. * }, {
  5944. * key: "\t",
  5945. * ctrl:true,
  5946. * shift:true,
  5947. * fn: function(){ alert('Control + shift + tab was pressed.'); }
  5948. * }]
  5949. * });
  5950. *
  5951. * Since 4.1.0, KeyMaps can bind to Components and process key-based events fired by Components.
  5952. *
  5953. * To bind to a Component, use the single parameter form of constructor:
  5954. *
  5955. * var map = new Ext.util.KeyMap({
  5956. * target: myGridView,
  5957. * eventName: 'itemkeydown',
  5958. * processEvent: function(view, record, node, index, event) {
  5959. *
  5960. * // Load the event with the extra information needed by the mappings
  5961. * event.view = view;
  5962. * event.store = view.getStore();
  5963. * event.record = record;
  5964. * event.index = index;
  5965. * return event;
  5966. * },
  5967. * binding: {
  5968. * key: Ext.EventObject.DELETE,
  5969. * fn: function(keyCode, e) {
  5970. * e.store.remove(e.record);
  5971. *
  5972. * // Attempt to select the record that's now in its place
  5973. * e.view.getSelectionModel().select(e,index);
  5974. * e.view.el.focus();
  5975. * }
  5976. * }
  5977. * });
  5978. */
  5979. Ext.define('Ext.util.KeyMap', {
  5980. alternateClassName: 'Ext.KeyMap',
  5981. /**
  5982. * @cfg {Ext.Component/Ext.Element/HTMLElement/String} target
  5983. * The object on which to listen for the event specified by the {@link #eventName} config option.
  5984. */
  5985. /**
  5986. * @cfg {Object/Object[][]} binding
  5987. * Either a single object describing a handling function for s specified key (or set of keys), or
  5988. * an array of such objects.
  5989. * @cfg {String/String[]} binding.key A single keycode or an array of keycodes to handle
  5990. * @cfg {Boolean} binding.shift True to handle key only when shift is pressed, False to handle the
  5991. * key only when shift is not pressed (defaults to undefined)
  5992. * @cfg {Boolean} binding.ctrl True to handle key only when ctrl is pressed, False to handle the
  5993. * key only when ctrl is not pressed (defaults to undefined)
  5994. * @cfg {Boolean} binding.alt True to handle key only when alt is pressed, False to handle the key
  5995. * only when alt is not pressed (defaults to undefined)
  5996. * @cfg {Function} binding.handler The function to call when KeyMap finds the expected key combination
  5997. * @cfg {Function} binding.fn Alias of handler (for backwards-compatibility)
  5998. * @cfg {Object} binding.scope The scope of the callback function
  5999. * @cfg {String} binding.defaultEventAction A default action to apply to the event. Possible values
  6000. * are: stopEvent, stopPropagation, preventDefault. If no value is set no action is performed.
  6001. */
  6002. /**
  6003. * @cfg {Object} [processEventScope=this]
  6004. * The scope (`this` context) in which the {@link #processEvent} method is executed.
  6005. */
  6006. /**
  6007. * @cfg {String} eventName
  6008. * The event to listen for to pick up key events.
  6009. */
  6010. eventName: 'keydown',
  6011. constructor: function(config) {
  6012. var me = this;
  6013. // Handle legacy arg list in which the first argument is the target.
  6014. // TODO: Deprecate in V5
  6015. if ((arguments.length !== 1) || (typeof config === 'string') || config.dom || config.tagName || config === document || config.isComponent) {
  6016. me.legacyConstructor.apply(me, arguments);
  6017. return;
  6018. }
  6019. Ext.apply(me, config);
  6020. me.bindings = [];
  6021. if (!me.target.isComponent) {
  6022. me.target = Ext.get(me.target);
  6023. }
  6024. if (me.binding) {
  6025. me.addBinding(me.binding);
  6026. } else if (config.key) {
  6027. me.addBinding(config);
  6028. }
  6029. me.enable();
  6030. },
  6031. /**
  6032. * @private
  6033. * Old constructor signature
  6034. * @param {String/HTMLElement/Ext.Element/Ext.Component} el The element or its ID, or Component to bind to
  6035. * @param {Object} binding The binding (see {@link #addBinding})
  6036. * @param {String} [eventName="keydown"] The event to bind to
  6037. */
  6038. legacyConstructor: function(el, binding, eventName){
  6039. var me = this;
  6040. Ext.apply(me, {
  6041. target: Ext.get(el),
  6042. eventName: eventName || me.eventName,
  6043. bindings: []
  6044. });
  6045. if (binding) {
  6046. me.addBinding(binding);
  6047. }
  6048. me.enable();
  6049. },
  6050. /**
  6051. * Add a new binding to this KeyMap.
  6052. *
  6053. * Usage:
  6054. *
  6055. * // Create a KeyMap
  6056. * var map = new Ext.util.KeyMap(document, {
  6057. * key: Ext.EventObject.ENTER,
  6058. * fn: handleKey,
  6059. * scope: this
  6060. * });
  6061. *
  6062. * //Add a new binding to the existing KeyMap later
  6063. * map.addBinding({
  6064. * key: 'abc',
  6065. * shift: true,
  6066. * fn: handleKey,
  6067. * scope: this
  6068. * });
  6069. *
  6070. * @param {Object/Object[]} binding A single KeyMap config or an array of configs.
  6071. * The following config object properties are supported:
  6072. * @param {String/Array} binding.key A single keycode or an array of keycodes to handle.
  6073. * @param {Boolean} binding.shift True to handle key only when shift is pressed,
  6074. * False to handle the keyonly when shift is not pressed (defaults to undefined).
  6075. * @param {Boolean} binding.ctrl True to handle key only when ctrl is pressed,
  6076. * False to handle the key only when ctrl is not pressed (defaults to undefined).
  6077. * @param {Boolean} binding.alt True to handle key only when alt is pressed,
  6078. * False to handle the key only when alt is not pressed (defaults to undefined).
  6079. * @param {Function} binding.handler The function to call when KeyMap finds the
  6080. * expected key combination.
  6081. * @param {Function} binding.fn Alias of handler (for backwards-compatibility).
  6082. * @param {Object} binding.scope The scope of the callback function.
  6083. * @param {String} binding.defaultEventAction A default action to apply to the event.
  6084. * Possible values are: stopEvent, stopPropagation, preventDefault. If no value is
  6085. * set no action is performed..
  6086. */
  6087. addBinding : function(binding){
  6088. var keyCode = binding.key,
  6089. processed = false,
  6090. key,
  6091. keys,
  6092. keyString,
  6093. i,
  6094. len;
  6095. if (Ext.isArray(binding)) {
  6096. for (i = 0, len = binding.length; i < len; i++) {
  6097. this.addBinding(binding[i]);
  6098. }
  6099. return;
  6100. }
  6101. if (Ext.isString(keyCode)) {
  6102. keys = [];
  6103. keyString = keyCode.toUpperCase();
  6104. for (i = 0, len = keyString.length; i < len; ++i){
  6105. keys.push(keyString.charCodeAt(i));
  6106. }
  6107. keyCode = keys;
  6108. processed = true;
  6109. }
  6110. if (!Ext.isArray(keyCode)) {
  6111. keyCode = [keyCode];
  6112. }
  6113. if (!processed) {
  6114. for (i = 0, len = keyCode.length; i < len; ++i) {
  6115. key = keyCode[i];
  6116. if (Ext.isString(key)) {
  6117. keyCode[i] = key.toUpperCase().charCodeAt(0);
  6118. }
  6119. }
  6120. }
  6121. this.bindings.push(Ext.apply({
  6122. keyCode: keyCode
  6123. }, binding));
  6124. },
  6125. /**
  6126. * Process any keydown events on the element
  6127. * @private
  6128. * @param {Ext.EventObject} event
  6129. */
  6130. handleKeyDown: function(event) {
  6131. var me = this;
  6132. if (this.enabled) { //just in case
  6133. var bindings = this.bindings,
  6134. i = 0,
  6135. len = bindings.length;
  6136. // Process the event
  6137. event = me.processEvent.apply(me||me.processEventScope, arguments);
  6138. // If the processor does not return a keyEvent, we can't process it.
  6139. // Allow them to return false to cancel processing of the event
  6140. if (!event.getKey) {
  6141. return event;
  6142. }
  6143. for(; i < len; ++i){
  6144. this.processBinding(bindings[i], event);
  6145. }
  6146. }
  6147. },
  6148. /**
  6149. * @cfg {Function} processEvent
  6150. * An optional event processor function which accepts the argument list provided by the
  6151. * {@link #eventName configured event} of the {@link #target}, and returns a keyEvent for processing by the KeyMap.
  6152. *
  6153. * This may be useful when the {@link #target} is a Component with s complex event signature. Extra information from
  6154. * the event arguments may be injected into the event for use by the handler functions before returning it.
  6155. */
  6156. processEvent: function(event){
  6157. return event;
  6158. },
  6159. /**
  6160. * Process a particular binding and fire the handler if necessary.
  6161. * @private
  6162. * @param {Object} binding The binding information
  6163. * @param {Ext.EventObject} event
  6164. */
  6165. processBinding: function(binding, event){
  6166. if (this.checkModifiers(binding, event)) {
  6167. var key = event.getKey(),
  6168. handler = binding.fn || binding.handler,
  6169. scope = binding.scope || this,
  6170. keyCode = binding.keyCode,
  6171. defaultEventAction = binding.defaultEventAction,
  6172. i,
  6173. len,
  6174. keydownEvent = new Ext.EventObjectImpl(event);
  6175. for (i = 0, len = keyCode.length; i < len; ++i) {
  6176. if (key === keyCode[i]) {
  6177. if (handler.call(scope, key, event) !== true && defaultEventAction) {
  6178. keydownEvent[defaultEventAction]();
  6179. }
  6180. break;
  6181. }
  6182. }
  6183. }
  6184. },
  6185. /**
  6186. * Check if the modifiers on the event match those on the binding
  6187. * @private
  6188. * @param {Object} binding
  6189. * @param {Ext.EventObject} event
  6190. * @return {Boolean} True if the event matches the binding
  6191. */
  6192. checkModifiers: function(binding, e) {
  6193. var keys = ['shift', 'ctrl', 'alt'],
  6194. i = 0,
  6195. len = keys.length,
  6196. val, key;
  6197. for (; i < len; ++i){
  6198. key = keys[i];
  6199. val = binding[key];
  6200. if (!(val === undefined || (val === e[key + 'Key']))) {
  6201. return false;
  6202. }
  6203. }
  6204. return true;
  6205. },
  6206. /**
  6207. * Shorthand for adding a single key listener.
  6208. *
  6209. * @param {Number/Number[]/Object} key Either the numeric key code, array of key codes or an object with the
  6210. * following options: `{key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}`
  6211. * @param {Function} fn The function to call
  6212. * @param {Object} [scope] The scope (`this` reference) in which the function is executed.
  6213. * Defaults to the browser window.
  6214. */
  6215. on: function(key, fn, scope) {
  6216. var keyCode, shift, ctrl, alt;
  6217. if (Ext.isObject(key) && !Ext.isArray(key)) {
  6218. keyCode = key.key;
  6219. shift = key.shift;
  6220. ctrl = key.ctrl;
  6221. alt = key.alt;
  6222. } else {
  6223. keyCode = key;
  6224. }
  6225. this.addBinding({
  6226. key: keyCode,
  6227. shift: shift,
  6228. ctrl: ctrl,
  6229. alt: alt,
  6230. fn: fn,
  6231. scope: scope
  6232. });
  6233. },
  6234. /**
  6235. * Returns true if this KeyMap is enabled
  6236. * @return {Boolean}
  6237. */
  6238. isEnabled : function() {
  6239. return this.enabled;
  6240. },
  6241. /**
  6242. * Enables this KeyMap
  6243. */
  6244. enable: function() {
  6245. var me = this;
  6246. if (!me.enabled) {
  6247. me.target.on(me.eventName, me.handleKeyDown, me);
  6248. me.enabled = true;
  6249. }
  6250. },
  6251. /**
  6252. * Disable this KeyMap
  6253. */
  6254. disable: function() {
  6255. var me = this;
  6256. if (me.enabled) {
  6257. me.target.removeListener(me.eventName, me.handleKeyDown, me);
  6258. me.enabled = false;
  6259. }
  6260. },
  6261. /**
  6262. * Convenience function for setting disabled/enabled by boolean.
  6263. * @param {Boolean} disabled
  6264. */
  6265. setDisabled : function(disabled) {
  6266. if (disabled) {
  6267. this.disable();
  6268. } else {
  6269. this.enable();
  6270. }
  6271. },
  6272. /**
  6273. * Destroys the KeyMap instance and removes all handlers.
  6274. * @param {Boolean} removeTarget True to also remove the {@link #target}
  6275. */
  6276. destroy: function(removeTarget) {
  6277. var me = this;
  6278. me.bindings = [];
  6279. me.disable();
  6280. if (removeTarget === true) {
  6281. me.target.isComponent ? me.target.destroy() : me.target.remove();
  6282. }
  6283. delete me.target;
  6284. }
  6285. });
  6286. /**
  6287. * @class Ext.util.Memento
  6288. * This class manages a set of captured properties from an object. These captured properties
  6289. * can later be restored to an object.
  6290. */
  6291. Ext.define('Ext.util.Memento', function () {
  6292. function captureOne (src, target, prop, prefix) {
  6293. src[prefix ? prefix + prop : prop] = target[prop];
  6294. }
  6295. function removeOne (src, target, prop) {
  6296. delete src[prop];
  6297. }
  6298. function restoreOne (src, target, prop, prefix) {
  6299. var name = prefix ? prefix + prop : prop,
  6300. value = src[name];
  6301. if (value || src.hasOwnProperty(name)) {
  6302. restoreValue(target, prop, value);
  6303. }
  6304. }
  6305. function restoreValue (target, prop, value) {
  6306. if (Ext.isDefined(value)) {
  6307. target[prop] = value;
  6308. } else {
  6309. delete target[prop];
  6310. }
  6311. }
  6312. function doMany (doOne, src, target, props, prefix) {
  6313. if (src) {
  6314. if (Ext.isArray(props)) {
  6315. var p, pLen = props.length;
  6316. for (p = 0; p < pLen; p++) {
  6317. doOne(src, target, props[p], prefix);
  6318. }
  6319. } else {
  6320. doOne(src, target, props, prefix);
  6321. }
  6322. }
  6323. }
  6324. return {
  6325. /**
  6326. * @property data
  6327. * The collection of captured properties.
  6328. * @private
  6329. */
  6330. data: null,
  6331. /**
  6332. * @property target
  6333. * The default target object for capture/restore (passed to the constructor).
  6334. */
  6335. target: null,
  6336. /**
  6337. * Creates a new memento and optionally captures properties from the target object.
  6338. * @param {Object} target The target from which to capture properties. If specified in the
  6339. * constructor, this target becomes the default target for all other operations.
  6340. * @param {String/String[]} props The property or array of properties to capture.
  6341. */
  6342. constructor: function (target, props) {
  6343. if (target) {
  6344. this.target = target;
  6345. if (props) {
  6346. this.capture(props);
  6347. }
  6348. }
  6349. },
  6350. /**
  6351. * Captures the specified properties from the target object in this memento.
  6352. * @param {String/String[]} props The property or array of properties to capture.
  6353. * @param {Object} target The object from which to capture properties.
  6354. */
  6355. capture: function (props, target, prefix) {
  6356. var me = this;
  6357. doMany(captureOne, me.data || (me.data = {}), target || me.target, props, prefix);
  6358. },
  6359. /**
  6360. * Removes the specified properties from this memento. These properties will not be
  6361. * restored later without re-capturing their values.
  6362. * @param {String/String[]} props The property or array of properties to remove.
  6363. */
  6364. remove: function (props) {
  6365. doMany(removeOne, this.data, null, props);
  6366. },
  6367. /**
  6368. * Restores the specified properties from this memento to the target object.
  6369. * @param {String/String[]} props The property or array of properties to restore.
  6370. * @param {Boolean} clear True to remove the restored properties from this memento or
  6371. * false to keep them (default is true).
  6372. * @param {Object} target The object to which to restore properties.
  6373. */
  6374. restore: function (props, clear, target, prefix) {
  6375. doMany(restoreOne, this.data, target || this.target, props, prefix);
  6376. if (clear !== false) {
  6377. this.remove(props);
  6378. }
  6379. },
  6380. /**
  6381. * Restores all captured properties in this memento to the target object.
  6382. * @param {Boolean} clear True to remove the restored properties from this memento or
  6383. * false to keep them (default is true).
  6384. * @param {Object} target The object to which to restore properties.
  6385. */
  6386. restoreAll: function (clear, target) {
  6387. var me = this,
  6388. t = target || this.target,
  6389. data = me.data,
  6390. prop;
  6391. for (prop in data) {
  6392. if (data.hasOwnProperty(prop)) {
  6393. restoreValue(t, prop, data[prop]);
  6394. }
  6395. }
  6396. if (clear !== false) {
  6397. delete me.data;
  6398. }
  6399. }
  6400. };
  6401. }());
  6402. /**
  6403. * @class Ext.state.Provider
  6404. * <p>Abstract base class for state provider implementations. The provider is responsible
  6405. * for setting values and extracting values to/from the underlying storage source. The
  6406. * storage source can vary and the details should be implemented in a subclass. For example
  6407. * a provider could use a server side database or the browser localstorage where supported.</p>
  6408. *
  6409. * <p>This class provides methods for encoding and decoding <b>typed</b> variables including
  6410. * dates and defines the Provider interface. By default these methods put the value and the
  6411. * type information into a delimited string that can be stored. These should be overridden in
  6412. * a subclass if you want to change the format of the encoded value and subsequent decoding.</p>
  6413. */
  6414. Ext.define('Ext.state.Provider', {
  6415. mixins: {
  6416. observable: 'Ext.util.Observable'
  6417. },
  6418. /**
  6419. * @cfg {String} prefix A string to prefix to items stored in the underlying state store.
  6420. * Defaults to <tt>'ext-'</tt>
  6421. */
  6422. prefix: 'ext-',
  6423. constructor : function(config){
  6424. config = config || {};
  6425. var me = this;
  6426. Ext.apply(me, config);
  6427. /**
  6428. * @event statechange
  6429. * Fires when a state change occurs.
  6430. * @param {Ext.state.Provider} this This state provider
  6431. * @param {String} key The state key which was changed
  6432. * @param {String} value The encoded value for the state
  6433. */
  6434. me.addEvents("statechange");
  6435. me.state = {};
  6436. me.mixins.observable.constructor.call(me);
  6437. },
  6438. /**
  6439. * Returns the current value for a key
  6440. * @param {String} name The key name
  6441. * @param {Object} defaultValue A default value to return if the key's value is not found
  6442. * @return {Object} The state data
  6443. */
  6444. get : function(name, defaultValue){
  6445. return typeof this.state[name] == "undefined" ?
  6446. defaultValue : this.state[name];
  6447. },
  6448. /**
  6449. * Clears a value from the state
  6450. * @param {String} name The key name
  6451. */
  6452. clear : function(name){
  6453. var me = this;
  6454. delete me.state[name];
  6455. me.fireEvent("statechange", me, name, null);
  6456. },
  6457. /**
  6458. * Sets the value for a key
  6459. * @param {String} name The key name
  6460. * @param {Object} value The value to set
  6461. */
  6462. set : function(name, value){
  6463. var me = this;
  6464. me.state[name] = value;
  6465. me.fireEvent("statechange", me, name, value);
  6466. },
  6467. /**
  6468. * Decodes a string previously encoded with {@link #encodeValue}.
  6469. * @param {String} value The value to decode
  6470. * @return {Object} The decoded value
  6471. */
  6472. decodeValue : function(value){
  6473. // a -> Array
  6474. // n -> Number
  6475. // d -> Date
  6476. // b -> Boolean
  6477. // s -> String
  6478. // o -> Object
  6479. // -> Empty (null)
  6480. var me = this,
  6481. re = /^(a|n|d|b|s|o|e)\:(.*)$/,
  6482. matches = re.exec(unescape(value)),
  6483. all,
  6484. type,
  6485. value,
  6486. keyValue,
  6487. values,
  6488. vLen,
  6489. v;
  6490. if(!matches || !matches[1]){
  6491. return; // non state
  6492. }
  6493. type = matches[1];
  6494. value = matches[2];
  6495. switch (type) {
  6496. case 'e':
  6497. return null;
  6498. case 'n':
  6499. return parseFloat(value);
  6500. case 'd':
  6501. return new Date(Date.parse(value));
  6502. case 'b':
  6503. return (value == '1');
  6504. case 'a':
  6505. all = [];
  6506. if(value != ''){
  6507. values = value.split('^');
  6508. vLen = values.length;
  6509. for (v = 0; v < vLen; v++) {
  6510. value = values[v];
  6511. all.push(me.decodeValue(value));
  6512. }
  6513. }
  6514. return all;
  6515. case 'o':
  6516. all = {};
  6517. if(value != ''){
  6518. values = value.split('^');
  6519. vLen = values.length;
  6520. for (v = 0; v < vLen; v++) {
  6521. value = values[v];
  6522. keyValue = value.split('=');
  6523. all[keyValue[0]] = me.decodeValue(keyValue[1]);
  6524. }
  6525. }
  6526. return all;
  6527. default:
  6528. return value;
  6529. }
  6530. },
  6531. /**
  6532. * Encodes a value including type information. Decode with {@link #decodeValue}.
  6533. * @param {Object} value The value to encode
  6534. * @return {String} The encoded value
  6535. */
  6536. encodeValue : function(value){
  6537. var flat = '',
  6538. i = 0,
  6539. enc,
  6540. len,
  6541. key;
  6542. if (value == null) {
  6543. return 'e:1';
  6544. } else if(typeof value == 'number') {
  6545. enc = 'n:' + value;
  6546. } else if(typeof value == 'boolean') {
  6547. enc = 'b:' + (value ? '1' : '0');
  6548. } else if(Ext.isDate(value)) {
  6549. enc = 'd:' + value.toGMTString();
  6550. } else if(Ext.isArray(value)) {
  6551. for (len = value.length; i < len; i++) {
  6552. flat += this.encodeValue(value[i]);
  6553. if (i != len - 1) {
  6554. flat += '^';
  6555. }
  6556. }
  6557. enc = 'a:' + flat;
  6558. } else if (typeof value == 'object') {
  6559. for (key in value) {
  6560. if (typeof value[key] != 'function' && value[key] !== undefined) {
  6561. flat += key + '=' + this.encodeValue(value[key]) + '^';
  6562. }
  6563. }
  6564. enc = 'o:' + flat.substring(0, flat.length-1);
  6565. } else {
  6566. enc = 's:' + value;
  6567. }
  6568. return escape(enc);
  6569. }
  6570. });
  6571. /**
  6572. * @author Ed Spencer
  6573. *
  6574. * Simple wrapper class that represents a set of records returned by a Proxy.
  6575. */
  6576. Ext.define('Ext.data.ResultSet', {
  6577. /**
  6578. * @cfg {Boolean} loaded
  6579. * True if the records have already been loaded. This is only meaningful when dealing with
  6580. * SQL-backed proxies.
  6581. */
  6582. loaded: true,
  6583. /**
  6584. * @cfg {Number} count
  6585. * The number of records in this ResultSet. Note that total may differ from this number.
  6586. */
  6587. count: 0,
  6588. /**
  6589. * @cfg {Number} total
  6590. * The total number of records reported by the data source. This ResultSet may form a subset of
  6591. * those records (see {@link #count}).
  6592. */
  6593. total: 0,
  6594. /**
  6595. * @cfg {Boolean} success
  6596. * True if the ResultSet loaded successfully, false if any errors were encountered.
  6597. */
  6598. success: false,
  6599. /**
  6600. * @cfg {Ext.data.Model[]} records (required)
  6601. * The array of record instances.
  6602. */
  6603. /**
  6604. * Creates the resultSet
  6605. * @param {Object} [config] Config object.
  6606. */
  6607. constructor: function(config) {
  6608. Ext.apply(this, config);
  6609. /**
  6610. * @property {Number} totalRecords
  6611. * Copy of this.total.
  6612. * @deprecated Will be removed in Ext JS 5.0. Use {@link #total} instead.
  6613. */
  6614. this.totalRecords = this.total;
  6615. if (config.count === undefined) {
  6616. this.count = this.records.length;
  6617. }
  6618. }
  6619. });
  6620. /**
  6621. * @class Ext.fx.CubicBezier
  6622. * @ignore
  6623. */
  6624. Ext.define('Ext.fx.CubicBezier', {
  6625. /* Begin Definitions */
  6626. singleton: true,
  6627. /* End Definitions */
  6628. cubicBezierAtTime: function(t, p1x, p1y, p2x, p2y, duration) {
  6629. var cx = 3 * p1x,
  6630. bx = 3 * (p2x - p1x) - cx,
  6631. ax = 1 - cx - bx,
  6632. cy = 3 * p1y,
  6633. by = 3 * (p2y - p1y) - cy,
  6634. ay = 1 - cy - by;
  6635. function sampleCurveX(t) {
  6636. return ((ax * t + bx) * t + cx) * t;
  6637. }
  6638. function solve(x, epsilon) {
  6639. var t = solveCurveX(x, epsilon);
  6640. return ((ay * t + by) * t + cy) * t;
  6641. }
  6642. function solveCurveX(x, epsilon) {
  6643. var t0, t1, t2, x2, d2, i;
  6644. for (t2 = x, i = 0; i < 8; i++) {
  6645. x2 = sampleCurveX(t2) - x;
  6646. if (Math.abs(x2) < epsilon) {
  6647. return t2;
  6648. }
  6649. d2 = (3 * ax * t2 + 2 * bx) * t2 + cx;
  6650. if (Math.abs(d2) < 1e-6) {
  6651. break;
  6652. }
  6653. t2 = t2 - x2 / d2;
  6654. }
  6655. t0 = 0;
  6656. t1 = 1;
  6657. t2 = x;
  6658. if (t2 < t0) {
  6659. return t0;
  6660. }
  6661. if (t2 > t1) {
  6662. return t1;
  6663. }
  6664. while (t0 < t1) {
  6665. x2 = sampleCurveX(t2);
  6666. if (Math.abs(x2 - x) < epsilon) {
  6667. return t2;
  6668. }
  6669. if (x > x2) {
  6670. t0 = t2;
  6671. } else {
  6672. t1 = t2;
  6673. }
  6674. t2 = (t1 - t0) / 2 + t0;
  6675. }
  6676. return t2;
  6677. }
  6678. return solve(t, 1 / (200 * duration));
  6679. },
  6680. cubicBezier: function(x1, y1, x2, y2) {
  6681. var fn = function(pos) {
  6682. return Ext.fx.CubicBezier.cubicBezierAtTime(pos, x1, y1, x2, y2, 1);
  6683. };
  6684. fn.toCSS3 = function() {
  6685. return 'cubic-bezier(' + [x1, y1, x2, y2].join(',') + ')';
  6686. };
  6687. fn.reverse = function() {
  6688. return Ext.fx.CubicBezier.cubicBezier(1 - x2, 1 - y2, 1 - x1, 1 - y1);
  6689. };
  6690. return fn;
  6691. }
  6692. });
  6693. /**
  6694. * A custom drag proxy implementation specific to {@link Ext.panel.Panel}s. This class
  6695. * is primarily used internally for the Panel's drag drop implementation, and
  6696. * should never need to be created directly.
  6697. * @private
  6698. */
  6699. Ext.define('Ext.panel.Proxy', {
  6700. alternateClassName: 'Ext.dd.PanelProxy',
  6701. /**
  6702. * @cfg {Boolean} [moveOnDrag=true]
  6703. * True to move the panel to the dragged position when dropped
  6704. */
  6705. moveOnDrag: true,
  6706. /**
  6707. * Creates new panel proxy.
  6708. * @param {Ext.panel.Panel} panel The {@link Ext.panel.Panel} to proxy for
  6709. * @param {Object} [config] Config object
  6710. */
  6711. constructor: function(panel, config){
  6712. var me = this;
  6713. /**
  6714. * @property panel
  6715. * @type Ext.panel.Panel
  6716. */
  6717. me.panel = panel;
  6718. me.id = me.panel.id +'-ddproxy';
  6719. Ext.apply(me, config);
  6720. },
  6721. /**
  6722. * @cfg {Boolean} insertProxy
  6723. * True to insert a placeholder proxy element while dragging the panel, false to drag with no proxy.
  6724. * Most Panels are not absolute positioned and therefore we need to reserve this space.
  6725. */
  6726. insertProxy: true,
  6727. // private overrides
  6728. setStatus: Ext.emptyFn,
  6729. reset: Ext.emptyFn,
  6730. update: Ext.emptyFn,
  6731. stop: Ext.emptyFn,
  6732. sync: Ext.emptyFn,
  6733. /**
  6734. * Gets the proxy's element
  6735. * @return {Ext.Element} The proxy's element
  6736. */
  6737. getEl: function(){
  6738. return this.ghost.el;
  6739. },
  6740. /**
  6741. * Gets the proxy's ghost Panel
  6742. * @return {Ext.panel.Panel} The proxy's ghost Panel
  6743. */
  6744. getGhost: function(){
  6745. return this.ghost;
  6746. },
  6747. /**
  6748. * Gets the proxy element. This is the element that represents where the
  6749. * Panel was before we started the drag operation.
  6750. * @return {Ext.Element} The proxy's element
  6751. */
  6752. getProxy: function(){
  6753. return this.proxy;
  6754. },
  6755. /**
  6756. * Hides the proxy
  6757. */
  6758. hide : function(){
  6759. var me = this;
  6760. if (me.ghost) {
  6761. if (me.proxy) {
  6762. me.proxy.remove();
  6763. delete me.proxy;
  6764. }
  6765. // Unghost the Panel, do not move the Panel to where the ghost was
  6766. me.panel.unghost(null, me.moveOnDrag);
  6767. delete me.ghost;
  6768. }
  6769. },
  6770. /**
  6771. * Shows the proxy
  6772. */
  6773. show: function(){
  6774. var me = this,
  6775. panelSize;
  6776. if (!me.ghost) {
  6777. panelSize = me.panel.getSize();
  6778. me.panel.el.setVisibilityMode(Ext.Element.DISPLAY);
  6779. me.ghost = me.panel.ghost();
  6780. if (me.insertProxy) {
  6781. // bc Panels aren't absolute positioned we need to take up the space
  6782. // of where the panel previously was
  6783. me.proxy = me.panel.el.insertSibling({cls: Ext.baseCSSPrefix + 'panel-dd-spacer'});
  6784. me.proxy.setSize(panelSize);
  6785. }
  6786. }
  6787. },
  6788. // private
  6789. repair: function(xy, callback, scope) {
  6790. this.hide();
  6791. Ext.callback(callback, scope || this);
  6792. },
  6793. /**
  6794. * Moves the proxy to a different position in the DOM. This is typically
  6795. * called while dragging the Panel to keep the proxy sync'd to the Panel's
  6796. * location.
  6797. * @param {HTMLElement} parentNode The proxy's parent DOM node
  6798. * @param {HTMLElement} [before] The sibling node before which the
  6799. * proxy should be inserted. Defaults to the parent's last child if not
  6800. * specified.
  6801. */
  6802. moveProxy : function(parentNode, before){
  6803. if (this.proxy) {
  6804. parentNode.insertBefore(this.proxy.dom, before);
  6805. }
  6806. }
  6807. });
  6808. /**
  6809. * Represents an HTML fragment template. Templates may be {@link #compile precompiled} for greater performance.
  6810. *
  6811. * An instance of this class may be created by passing to the constructor either a single argument, or multiple
  6812. * arguments:
  6813. *
  6814. * # Single argument: String/Array
  6815. *
  6816. * The single argument may be either a String or an Array:
  6817. *
  6818. * - String:
  6819. *
  6820. * var t = new Ext.Template("<div>Hello {0}.</div>");
  6821. * t.{@link #append}('some-element', ['foo']);
  6822. *
  6823. * - Array:
  6824. *
  6825. * An Array will be combined with `join('')`.
  6826. *
  6827. * var t = new Ext.Template([
  6828. * '<div name="{id}">',
  6829. * '<span class="{cls}">{name:trim} {value:ellipsis(10)}</span>',
  6830. * '</div>',
  6831. * ]);
  6832. * t.{@link #compile}();
  6833. * t.{@link #append}('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
  6834. *
  6835. * # Multiple arguments: String, Object, Array, ...
  6836. *
  6837. * Multiple arguments will be combined with `join('')`.
  6838. *
  6839. * var t = new Ext.Template(
  6840. * '<div name="{id}">',
  6841. * '<span class="{cls}">{name} {value}</span>',
  6842. * '</div>',
  6843. * // a configuration object:
  6844. * {
  6845. * compiled: true, // {@link #compile} immediately
  6846. * }
  6847. * );
  6848. *
  6849. * # Notes
  6850. *
  6851. * - For a list of available format functions, see {@link Ext.util.Format}.
  6852. * - `disableFormats` reduces `{@link #apply}` time when no formatting is required.
  6853. */
  6854. Ext.define('Ext.Template', {
  6855. /* Begin Definitions */
  6856. requires: ['Ext.dom.Helper', 'Ext.util.Format'],
  6857. inheritableStatics: {
  6858. /**
  6859. * Creates a template from the passed element's value (_display:none_ textarea, preferred) or innerHTML.
  6860. * @param {String/HTMLElement} el A DOM element or its id
  6861. * @param {Object} config (optional) Config object
  6862. * @return {Ext.Template} The created template
  6863. * @static
  6864. * @inheritable
  6865. */
  6866. from: function(el, config) {
  6867. el = Ext.getDom(el);
  6868. return new this(el.value || el.innerHTML, config || '');
  6869. }
  6870. },
  6871. /* End Definitions */
  6872. /**
  6873. * Creates new template.
  6874. *
  6875. * @param {String...} html List of strings to be concatenated into template.
  6876. * Alternatively an array of strings can be given, but then no config object may be passed.
  6877. * @param {Object} config (optional) Config object
  6878. */
  6879. constructor: function(html) {
  6880. var me = this,
  6881. args = arguments,
  6882. buffer = [],
  6883. i = 0,
  6884. length = args.length,
  6885. value;
  6886. me.initialConfig = {};
  6887. if (length > 1) {
  6888. for (; i < length; i++) {
  6889. value = args[i];
  6890. if (typeof value == 'object') {
  6891. Ext.apply(me.initialConfig, value);
  6892. Ext.apply(me, value);
  6893. } else {
  6894. buffer.push(value);
  6895. }
  6896. }
  6897. html = buffer.join('');
  6898. } else {
  6899. if (Ext.isArray(html)) {
  6900. buffer.push(html.join(''));
  6901. } else {
  6902. buffer.push(html);
  6903. }
  6904. }
  6905. // @private
  6906. me.html = buffer.join('');
  6907. if (me.compiled) {
  6908. me.compile();
  6909. }
  6910. },
  6911. /**
  6912. * @property {Boolean} isTemplate
  6913. * `true` in this class to identify an objact as an instantiated Template, or subclass thereof.
  6914. */
  6915. isTemplate: true,
  6916. /**
  6917. * @cfg {Boolean} compiled
  6918. * True to immediately compile the template. Defaults to false.
  6919. */
  6920. /**
  6921. * @cfg {Boolean} disableFormats
  6922. * True to disable format functions in the template. If the template doesn't contain
  6923. * format functions, setting disableFormats to true will reduce apply time. Defaults to false.
  6924. */
  6925. disableFormats: false,
  6926. re: /\{([\w\-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
  6927. /**
  6928. * Returns an HTML fragment of this template with the specified values applied.
  6929. *
  6930. * @param {Object/Array} values The template values. Can be an array if your params are numeric:
  6931. *
  6932. * var tpl = new Ext.Template('Name: {0}, Age: {1}');
  6933. * tpl.apply(['John', 25]);
  6934. *
  6935. * or an object:
  6936. *
  6937. * var tpl = new Ext.Template('Name: {name}, Age: {age}');
  6938. * tpl.apply({name: 'John', age: 25});
  6939. *
  6940. * @return {String} The HTML fragment
  6941. */
  6942. apply: function(values) {
  6943. var me = this,
  6944. useFormat = me.disableFormats !== true,
  6945. fm = Ext.util.Format,
  6946. tpl = me,
  6947. ret;
  6948. if (me.compiled) {
  6949. return me.compiled(values).join('');
  6950. }
  6951. function fn(m, name, format, args) {
  6952. if (format && useFormat) {
  6953. if (args) {
  6954. args = [values[name]].concat(Ext.functionFactory('return ['+ args +'];')());
  6955. } else {
  6956. args = [values[name]];
  6957. }
  6958. if (format.substr(0, 5) == "this.") {
  6959. return tpl[format.substr(5)].apply(tpl, args);
  6960. }
  6961. else {
  6962. return fm[format].apply(fm, args);
  6963. }
  6964. }
  6965. else {
  6966. return values[name] !== undefined ? values[name] : "";
  6967. }
  6968. }
  6969. ret = me.html.replace(me.re, fn);
  6970. return ret;
  6971. },
  6972. /**
  6973. * Appends the result of this template to the provided output array.
  6974. * @param {Object/Array} values The template values. See {@link #apply}.
  6975. * @param {Array} out The array to which output is pushed.
  6976. * @return {Array} The given out array.
  6977. */
  6978. applyOut: function(values, out) {
  6979. var me = this;
  6980. if (me.compiled) {
  6981. out.push.apply(out, me.compiled(values));
  6982. } else {
  6983. out.push(me.apply(values));
  6984. }
  6985. return out;
  6986. },
  6987. /**
  6988. * @method applyTemplate
  6989. * @member Ext.Template
  6990. * Alias for {@link #apply}.
  6991. * @inheritdoc Ext.Template#apply
  6992. */
  6993. applyTemplate: function () {
  6994. return this.apply.apply(this, arguments);
  6995. },
  6996. /**
  6997. * Sets the HTML used as the template and optionally compiles it.
  6998. * @param {String} html
  6999. * @param {Boolean} compile (optional) True to compile the template.
  7000. * @return {Ext.Template} this
  7001. */
  7002. set: function(html, compile) {
  7003. var me = this;
  7004. me.html = html;
  7005. me.compiled = null;
  7006. return compile ? me.compile() : me;
  7007. },
  7008. compileARe: /\\/g,
  7009. compileBRe: /(\r\n|\n)/g,
  7010. compileCRe: /'/g,
  7011. /**
  7012. * Compiles the template into an internal function, eliminating the RegEx overhead.
  7013. * @return {Ext.Template} this
  7014. */
  7015. compile: function() {
  7016. var me = this,
  7017. fm = Ext.util.Format,
  7018. useFormat = me.disableFormats !== true,
  7019. body, bodyReturn;
  7020. function fn(m, name, format, args) {
  7021. if (format && useFormat) {
  7022. args = args ? ',' + args: "";
  7023. if (format.substr(0, 5) != "this.") {
  7024. format = "fm." + format + '(';
  7025. }
  7026. else {
  7027. format = 'this.' + format.substr(5) + '(';
  7028. }
  7029. }
  7030. else {
  7031. args = '';
  7032. format = "(values['" + name + "'] == undefined ? '' : ";
  7033. }
  7034. return "'," + format + "values['" + name + "']" + args + ") ,'";
  7035. }
  7036. bodyReturn = me.html.replace(me.compileARe, '\\\\').replace(me.compileBRe, '\\n').replace(me.compileCRe, "\\'").replace(me.re, fn);
  7037. body = "this.compiled = function(values){ return ['" + bodyReturn + "'];};";
  7038. eval(body);
  7039. return me;
  7040. },
  7041. /**
  7042. * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
  7043. *
  7044. * @param {String/HTMLElement/Ext.Element} el The context element
  7045. * @param {Object/Array} values The template values. See {@link #applyTemplate} for details.
  7046. * @param {Boolean} returnElement (optional) true to return a Ext.Element.
  7047. * @return {HTMLElement/Ext.Element} The new node or Element
  7048. */
  7049. insertFirst: function(el, values, returnElement) {
  7050. return this.doInsert('afterBegin', el, values, returnElement);
  7051. },
  7052. /**
  7053. * Applies the supplied values to the template and inserts the new node(s) before el.
  7054. *
  7055. * @param {String/HTMLElement/Ext.Element} el The context element
  7056. * @param {Object/Array} values The template values. See {@link #applyTemplate} for details.
  7057. * @param {Boolean} returnElement (optional) true to return a Ext.Element.
  7058. * @return {HTMLElement/Ext.Element} The new node or Element
  7059. */
  7060. insertBefore: function(el, values, returnElement) {
  7061. return this.doInsert('beforeBegin', el, values, returnElement);
  7062. },
  7063. /**
  7064. * Applies the supplied values to the template and inserts the new node(s) after el.
  7065. *
  7066. * @param {String/HTMLElement/Ext.Element} el The context element
  7067. * @param {Object/Array} values The template values. See {@link #applyTemplate} for details.
  7068. * @param {Boolean} returnElement (optional) true to return a Ext.Element.
  7069. * @return {HTMLElement/Ext.Element} The new node or Element
  7070. */
  7071. insertAfter: function(el, values, returnElement) {
  7072. return this.doInsert('afterEnd', el, values, returnElement);
  7073. },
  7074. /**
  7075. * Applies the supplied `values` to the template and appends the new node(s) to the specified `el`.
  7076. *
  7077. * For example usage see {@link Ext.Template Ext.Template class docs}.
  7078. *
  7079. * @param {String/HTMLElement/Ext.Element} el The context element
  7080. * @param {Object/Array} values The template values. See {@link #applyTemplate} for details.
  7081. * @param {Boolean} returnElement (optional) true to return an Ext.Element.
  7082. * @return {HTMLElement/Ext.Element} The new node or Element
  7083. */
  7084. append: function(el, values, returnElement) {
  7085. return this.doInsert('beforeEnd', el, values, returnElement);
  7086. },
  7087. doInsert: function(where, el, values, returnEl) {
  7088. el = Ext.getDom(el);
  7089. var newNode = Ext.DomHelper.insertHtml(where, el, this.apply(values));
  7090. return returnEl ? Ext.get(newNode, true) : newNode;
  7091. },
  7092. /**
  7093. * Applies the supplied values to the template and overwrites the content of el with the new node(s).
  7094. *
  7095. * @param {String/HTMLElement/Ext.Element} el The context element
  7096. * @param {Object/Array} values The template values. See {@link #applyTemplate} for details.
  7097. * @param {Boolean} returnElement (optional) true to return a Ext.Element.
  7098. * @return {HTMLElement/Ext.Element} The new node or Element
  7099. */
  7100. overwrite: function(el, values, returnElement) {
  7101. el = Ext.getDom(el);
  7102. el.innerHTML = this.apply(values);
  7103. return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
  7104. }
  7105. });
  7106. /**
  7107. * @class Ext.fx.Queue
  7108. * Animation Queue mixin to handle chaining and queueing by target.
  7109. * @private
  7110. */
  7111. Ext.define('Ext.fx.Queue', {
  7112. requires: ['Ext.util.HashMap'],
  7113. constructor: function() {
  7114. this.targets = new Ext.util.HashMap();
  7115. this.fxQueue = {};
  7116. },
  7117. // @private
  7118. getFxDefaults: function(targetId) {
  7119. var target = this.targets.get(targetId);
  7120. if (target) {
  7121. return target.fxDefaults;
  7122. }
  7123. return {};
  7124. },
  7125. // @private
  7126. setFxDefaults: function(targetId, obj) {
  7127. var target = this.targets.get(targetId);
  7128. if (target) {
  7129. target.fxDefaults = Ext.apply(target.fxDefaults || {}, obj);
  7130. }
  7131. },
  7132. // @private
  7133. stopAnimation: function(targetId) {
  7134. var me = this,
  7135. queue = me.getFxQueue(targetId),
  7136. ln = queue.length;
  7137. while (ln) {
  7138. queue[ln - 1].end();
  7139. ln--;
  7140. }
  7141. },
  7142. /**
  7143. * @private
  7144. * Returns current animation object if the element has any effects actively running or queued, else returns false.
  7145. */
  7146. getActiveAnimation: function(targetId) {
  7147. var queue = this.getFxQueue(targetId);
  7148. return (queue && !!queue.length) ? queue[0] : false;
  7149. },
  7150. // @private
  7151. hasFxBlock: function(targetId) {
  7152. var queue = this.getFxQueue(targetId);
  7153. return queue && queue[0] && queue[0].block;
  7154. },
  7155. // @private get fx queue for passed target, create if needed.
  7156. getFxQueue: function(targetId) {
  7157. if (!targetId) {
  7158. return false;
  7159. }
  7160. var me = this,
  7161. queue = me.fxQueue[targetId],
  7162. target = me.targets.get(targetId);
  7163. if (!target) {
  7164. return false;
  7165. }
  7166. if (!queue) {
  7167. me.fxQueue[targetId] = [];
  7168. // GarbageCollector will need to clean up Elements since they aren't currently observable
  7169. if (target.type != 'element') {
  7170. target.target.on('destroy', function() {
  7171. me.fxQueue[targetId] = [];
  7172. });
  7173. }
  7174. }
  7175. return me.fxQueue[targetId];
  7176. },
  7177. // @private
  7178. queueFx: function(anim) {
  7179. var me = this,
  7180. target = anim.target,
  7181. queue, ln;
  7182. if (!target) {
  7183. return;
  7184. }
  7185. queue = me.getFxQueue(target.getId());
  7186. ln = queue.length;
  7187. if (ln) {
  7188. if (anim.concurrent) {
  7189. anim.paused = false;
  7190. }
  7191. else {
  7192. queue[ln - 1].on('afteranimate', function() {
  7193. anim.paused = false;
  7194. });
  7195. }
  7196. }
  7197. else {
  7198. anim.paused = false;
  7199. }
  7200. anim.on('afteranimate', function() {
  7201. Ext.Array.remove(queue, anim);
  7202. if (anim.remove) {
  7203. if (target.type == 'element') {
  7204. var el = Ext.get(target.id);
  7205. if (el) {
  7206. el.remove();
  7207. }
  7208. }
  7209. }
  7210. }, this);
  7211. queue.push(anim);
  7212. }
  7213. });
  7214. /**
  7215. * This class parses the XTemplate syntax and calls abstract methods to process the parts.
  7216. * @private
  7217. */
  7218. Ext.define('Ext.XTemplateParser', {
  7219. constructor: function (config) {
  7220. Ext.apply(this, config);
  7221. },
  7222. /**
  7223. * @property {Number} level The 'for' loop context level. This is adjusted up by one
  7224. * prior to calling {@link #doFor} and down by one after calling the corresponding
  7225. * {@link #doEnd} that closes the loop. This will be 1 on the first {@link #doFor}
  7226. * call.
  7227. */
  7228. /**
  7229. * This method is called to process a piece of raw text from the tpl.
  7230. * @param {String} text
  7231. * @method doText
  7232. */
  7233. // doText: function (text)
  7234. /**
  7235. * This method is called to process expressions (like `{[expr]}`).
  7236. * @param {String} expr The body of the expression (inside "{[" and "]}").
  7237. * @method doExpr
  7238. */
  7239. // doExpr: function (expr)
  7240. /**
  7241. * This method is called to process simple tags (like `{tag}`).
  7242. * @method doTag
  7243. */
  7244. // doTag: function (tag)
  7245. /**
  7246. * This method is called to process `<tpl else>`.
  7247. * @method doElse
  7248. */
  7249. // doElse: function ()
  7250. /**
  7251. * This method is called to process `{% text %}`.
  7252. * @param {String} text
  7253. * @method doEval
  7254. */
  7255. // doEval: function (text)
  7256. /**
  7257. * This method is called to process `<tpl if="action">`. If there are other attributes,
  7258. * these are passed in the actions object.
  7259. * @param {String} action
  7260. * @param {Object} actions Other actions keyed by the attribute name (such as 'exec').
  7261. * @method doIf
  7262. */
  7263. // doIf: function (action, actions)
  7264. /**
  7265. * This method is called to process `<tpl elseif="action">`. If there are other attributes,
  7266. * these are passed in the actions object.
  7267. * @param {String} action
  7268. * @param {Object} actions Other actions keyed by the attribute name (such as 'exec').
  7269. * @method doElseIf
  7270. */
  7271. // doElseIf: function (action, actions)
  7272. /**
  7273. * This method is called to process `<tpl switch="action">`. If there are other attributes,
  7274. * these are passed in the actions object.
  7275. * @param {String} action
  7276. * @param {Object} actions Other actions keyed by the attribute name (such as 'exec').
  7277. * @method doSwitch
  7278. */
  7279. // doSwitch: function (action, actions)
  7280. /**
  7281. * This method is called to process `<tpl case="action">`. If there are other attributes,
  7282. * these are passed in the actions object.
  7283. * @param {String} action
  7284. * @param {Object} actions Other actions keyed by the attribute name (such as 'exec').
  7285. * @method doCase
  7286. */
  7287. // doCase: function (action, actions)
  7288. /**
  7289. * This method is called to process `<tpl default>`.
  7290. * @method doDefault
  7291. */
  7292. // doDefault: function ()
  7293. /**
  7294. * This method is called to process `</tpl>`. It is given the action type that started
  7295. * the tpl and the set of additional actions.
  7296. * @param {String} type The type of action that is being ended.
  7297. * @param {Object} actions The other actions keyed by the attribute name (such as 'exec').
  7298. * @method doEnd
  7299. */
  7300. // doEnd: function (type, actions)
  7301. /**
  7302. * This method is called to process `<tpl for="action">`. If there are other attributes,
  7303. * these are passed in the actions object.
  7304. * @param {String} action
  7305. * @param {Object} actions Other actions keyed by the attribute name (such as 'exec').
  7306. * @method doFor
  7307. */
  7308. // doFor: function (action, actions)
  7309. /**
  7310. * This method is called to process `<tpl exec="action">`. If there are other attributes,
  7311. * these are passed in the actions object.
  7312. * @param {String} action
  7313. * @param {Object} actions Other actions keyed by the attribute name.
  7314. * @method doExec
  7315. */
  7316. // doExec: function (action, actions)
  7317. /**
  7318. * This method is called to process an empty `<tpl>`. This is unlikely to need to be
  7319. * implemented, so a default (do nothing) version is provided.
  7320. * @method
  7321. */
  7322. doTpl: Ext.emptyFn,
  7323. parse: function (str) {
  7324. var me = this,
  7325. len = str.length,
  7326. aliases = { elseif: 'elif' },
  7327. topRe = me.topRe,
  7328. actionsRe = me.actionsRe,
  7329. index, stack, s, m, t, prev, frame, subMatch, begin, end, actions;
  7330. me.level = 0;
  7331. me.stack = stack = [];
  7332. for (index = 0; index < len; index = end) {
  7333. topRe.lastIndex = index;
  7334. m = topRe.exec(str);
  7335. if (!m) {
  7336. me.doText(str.substring(index, len));
  7337. break;
  7338. }
  7339. begin = m.index;
  7340. end = topRe.lastIndex;
  7341. if (index < begin) {
  7342. me.doText(str.substring(index, begin));
  7343. }
  7344. if (m[1]) {
  7345. end = str.indexOf('%}', begin+2);
  7346. me.doEval(str.substring(begin+2, end));
  7347. end += 2;
  7348. } else if (m[2]) {
  7349. end = str.indexOf(']}', begin+2);
  7350. me.doExpr(str.substring(begin+2, end));
  7351. end += 2;
  7352. } else if (m[3]) { // if ('{' token)
  7353. me.doTag(m[3]);
  7354. } else if (m[4]) { // content of a <tpl xxxxxx> tag
  7355. actions = null;
  7356. while ((subMatch = actionsRe.exec(m[4])) !== null) {
  7357. s = subMatch[2] || subMatch[3];
  7358. if (s) {
  7359. s = Ext.String.htmlDecode(s); // decode attr value
  7360. t = subMatch[1];
  7361. t = aliases[t] || t;
  7362. actions = actions || {};
  7363. prev = actions[t];
  7364. if (typeof prev == 'string') {
  7365. actions[t] = [prev, s];
  7366. } else if (prev) {
  7367. actions[t].push(s);
  7368. } else {
  7369. actions[t] = s;
  7370. }
  7371. }
  7372. }
  7373. if (!actions) {
  7374. if (me.elseRe.test(m[4])) {
  7375. me.doElse();
  7376. } else if (me.defaultRe.test(m[4])) {
  7377. me.doDefault();
  7378. } else {
  7379. me.doTpl();
  7380. stack.push({ type: 'tpl' });
  7381. }
  7382. }
  7383. else if (actions['if']) {
  7384. me.doIf(actions['if'], actions)
  7385. stack.push({ type: 'if' });
  7386. }
  7387. else if (actions['switch']) {
  7388. me.doSwitch(actions['switch'], actions)
  7389. stack.push({ type: 'switch' });
  7390. }
  7391. else if (actions['case']) {
  7392. me.doCase(actions['case'], actions);
  7393. }
  7394. else if (actions['elif']) {
  7395. me.doElseIf(actions['elif'], actions);
  7396. }
  7397. else if (actions['for']) {
  7398. ++me.level;
  7399. me.doFor(actions['for'], actions);
  7400. stack.push({ type: 'for', actions: actions });
  7401. }
  7402. else if (actions.exec) {
  7403. me.doExec(actions.exec, actions);
  7404. stack.push({ type: 'exec', actions: actions });
  7405. }
  7406. /*
  7407. else {
  7408. // todo - error
  7409. }
  7410. /**/
  7411. } else {
  7412. frame = stack.pop();
  7413. me.doEnd(frame.type, frame.actions);
  7414. if (frame.type == 'for') {
  7415. --me.level;
  7416. }
  7417. }
  7418. }
  7419. },
  7420. // Internal regexes
  7421. topRe: /(?:(\{\%)|(\{\[)|\{([^{}]*)\})|(?:<tpl([^>]*)\>)|(?:<\/tpl>)/g,
  7422. actionsRe: /\s*(elif|elseif|if|for|exec|switch|case|eval)\s*\=\s*(?:(?:["]([^"]*)["])|(?:[']([^']*)[']))\s*/g,
  7423. defaultRe: /^\s*default\s*$/,
  7424. elseRe: /^\s*else\s*$/
  7425. });
  7426. /**
  7427. * A class that manages a group of {@link Ext.Component#floating} Components and provides z-order management,
  7428. * and Component activation behavior, including masking below the active (topmost) Component.
  7429. *
  7430. * {@link Ext.Component#floating Floating} Components which are rendered directly into the document (such as
  7431. * {@link Ext.window.Window Window}s) which are {@link Ext.Component#method-show show}n are managed by a
  7432. * {@link Ext.WindowManager global instance}.
  7433. *
  7434. * {@link Ext.Component#floating Floating} Components which are descendants of {@link Ext.Component#floating floating}
  7435. * *Containers* (for example a {@link Ext.view.BoundList BoundList} within an {@link Ext.window.Window Window},
  7436. * or a {@link Ext.menu.Menu Menu}), are managed by a ZIndexManager owned by that floating Container. Therefore
  7437. * ComboBox dropdowns within Windows will have managed z-indices guaranteed to be correct, relative to the Window.
  7438. */
  7439. Ext.define('Ext.ZIndexManager', {
  7440. alternateClassName: 'Ext.WindowGroup',
  7441. statics: {
  7442. zBase : 9000
  7443. },
  7444. constructor: function(container) {
  7445. var me = this;
  7446. me.list = {};
  7447. me.zIndexStack = [];
  7448. me.front = null;
  7449. if (container) {
  7450. // This is the ZIndexManager for an Ext.container.Container, base its zseed on the zIndex of the Container's element
  7451. if (container.isContainer) {
  7452. container.on('resize', me._onContainerResize, me);
  7453. me.zseed = Ext.Number.from(me.rendered ? container.getEl().getStyle('zIndex') : undefined, me.getNextZSeed());
  7454. // The containing element we will be dealing with (eg masking) is the content target
  7455. me.targetEl = container.getTargetEl();
  7456. me.container = container;
  7457. }
  7458. // This is the ZIndexManager for a DOM element
  7459. else {
  7460. Ext.EventManager.onWindowResize(me._onContainerResize, me);
  7461. me.zseed = me.getNextZSeed();
  7462. me.targetEl = Ext.get(container);
  7463. }
  7464. }
  7465. // No container passed means we are the global WindowManager. Our target is the doc body.
  7466. // DOM must be ready to collect that ref.
  7467. else {
  7468. Ext.EventManager.onWindowResize(me._onContainerResize, me);
  7469. me.zseed = me.getNextZSeed();
  7470. Ext.onDocumentReady(function() {
  7471. me.targetEl = Ext.getBody();
  7472. });
  7473. }
  7474. },
  7475. getNextZSeed: function() {
  7476. return (Ext.ZIndexManager.zBase += 10000);
  7477. },
  7478. setBase: function(baseZIndex) {
  7479. this.zseed = baseZIndex;
  7480. var result = this.assignZIndices();
  7481. this._activateLast();
  7482. return result;
  7483. },
  7484. // private
  7485. assignZIndices: function() {
  7486. var a = this.zIndexStack,
  7487. len = a.length,
  7488. i = 0,
  7489. zIndex = this.zseed,
  7490. comp;
  7491. for (; i < len; i++) {
  7492. comp = a[i];
  7493. if (comp && !comp.hidden) {
  7494. // Setting the zIndex of a Component returns the topmost zIndex consumed by
  7495. // that Component.
  7496. // If it's just a plain floating Component such as a BoundList, then the
  7497. // return value is the passed value plus 10, ready for the next item.
  7498. // If a floating *Container* has its zIndex set, it re-orders its managed
  7499. // floating children, starting from that new base, and returns a value 10000 above
  7500. // the highest zIndex which it allocates.
  7501. zIndex = comp.setZIndex(zIndex);
  7502. }
  7503. }
  7504. // Activate new topmost
  7505. this._activateLast();
  7506. return zIndex;
  7507. },
  7508. // private
  7509. _setActiveChild: function(comp, oldFront) {
  7510. var front = this.front;
  7511. if (comp !== front) {
  7512. if (front && !front.destroying) {
  7513. front.setActive(false, comp);
  7514. }
  7515. this.front = comp;
  7516. if (comp && comp != oldFront) {
  7517. comp.setActive(true);
  7518. if (comp.modal) {
  7519. this._showModalMask(comp);
  7520. }
  7521. }
  7522. }
  7523. },
  7524. onComponentHide: function(comp){
  7525. comp.setActive(false);
  7526. this._activateLast();
  7527. },
  7528. // private
  7529. _activateLast: function() {
  7530. var me = this,
  7531. stack = me.zIndexStack,
  7532. i = stack.length - 1,
  7533. oldFront = me.front,
  7534. comp;
  7535. // There may be no visible floater to activate
  7536. me.front = undefined;
  7537. // Go down through the z-index stack.
  7538. // Activate the next visible one down.
  7539. // If that was modal, then we're done
  7540. for (; i >= 0 && stack[i].hidden; --i);
  7541. if ((comp = stack[i])) {
  7542. me._setActiveChild(comp, oldFront);
  7543. if (comp.modal) {
  7544. return;
  7545. }
  7546. }
  7547. // If the new top one was not modal, keep going down to find the next visible
  7548. // modal one to shift the modal mask down under
  7549. for (; i >= 0; --i) {
  7550. comp = stack[i];
  7551. // If we find a visible modal further down the zIndex stack, move the mask to just under it.
  7552. if (comp.isVisible() && comp.modal) {
  7553. me._showModalMask(comp);
  7554. return;
  7555. }
  7556. }
  7557. // No visible modal Component was found in the run down the stack.
  7558. // So hide the modal mask
  7559. me._hideModalMask();
  7560. },
  7561. _showModalMask: function(comp) {
  7562. var me = this,
  7563. zIndex = comp.el.getStyle('zIndex') - 4,
  7564. maskTarget = comp.floatParent ? comp.floatParent.getTargetEl() : comp.container,
  7565. viewSize = maskTarget.getBox();
  7566. if (maskTarget.dom === document.body) {
  7567. viewSize.height = Math.max(document.body.scrollHeight, Ext.dom.Element.getDocumentHeight());
  7568. viewSize.width = Math.max(document.body.scrollWidth, viewSize.width);
  7569. }
  7570. if (!me.mask) {
  7571. me.mask = Ext.getBody().createChild({
  7572. cls: Ext.baseCSSPrefix + 'mask'
  7573. });
  7574. me.mask.setVisibilityMode(Ext.Element.DISPLAY);
  7575. me.mask.on('click', me._onMaskClick, me);
  7576. }
  7577. me.mask.maskTarget = maskTarget;
  7578. maskTarget.addCls(Ext.baseCSSPrefix + 'body-masked');
  7579. me.mask.setBox(viewSize);
  7580. me.mask.setStyle('zIndex', zIndex);
  7581. me.mask.show();
  7582. },
  7583. _hideModalMask: function() {
  7584. var mask = this.mask;
  7585. if (mask && mask.isVisible()) {
  7586. mask.maskTarget.removeCls(Ext.baseCSSPrefix + 'body-masked');
  7587. mask.maskTarget = undefined;
  7588. mask.hide();
  7589. }
  7590. },
  7591. _onMaskClick: function() {
  7592. if (this.front) {
  7593. this.front.focus();
  7594. }
  7595. },
  7596. _onContainerResize: function() {
  7597. var mask = this.mask,
  7598. maskTarget,
  7599. viewSize;
  7600. if (mask && mask.isVisible()) {
  7601. // At the new container size, the mask might be *causing* the scrollbar, so to find the valid
  7602. // client size to mask, we must temporarily unmask the parent node.
  7603. mask.hide();
  7604. maskTarget = mask.maskTarget;
  7605. if (maskTarget.dom === document.body) {
  7606. viewSize = {
  7607. height: Math.max(document.body.scrollHeight, Ext.dom.Element.getDocumentHeight()),
  7608. width: Math.max(document.body.scrollWidth, document.documentElement.clientWidth)
  7609. }
  7610. } else {
  7611. viewSize = maskTarget.getViewSize(true);
  7612. }
  7613. mask.setSize(viewSize);
  7614. mask.show();
  7615. }
  7616. },
  7617. /**
  7618. * Registers a floating {@link Ext.Component} with this ZIndexManager. This should not
  7619. * need to be called under normal circumstances. Floating Components (such as Windows,
  7620. * BoundLists and Menus) are automatically registered with a
  7621. * {@link Ext.Component#zIndexManager zIndexManager} at render time.
  7622. *
  7623. * Where this may be useful is moving Windows between two ZIndexManagers. For example,
  7624. * to bring the Ext.MessageBox dialog under the same manager as the Desktop's
  7625. * ZIndexManager in the desktop sample app:
  7626. *
  7627. * MyDesktop.getDesktop().getManager().register(Ext.MessageBox);
  7628. *
  7629. * @param {Ext.Component} comp The Component to register.
  7630. */
  7631. register : function(comp) {
  7632. var me = this;
  7633. if (comp.zIndexManager) {
  7634. comp.zIndexManager.unregister(comp);
  7635. }
  7636. comp.zIndexManager = me;
  7637. me.list[comp.id] = comp;
  7638. me.zIndexStack.push(comp);
  7639. comp.on('hide', me.onComponentHide, me);
  7640. },
  7641. /**
  7642. * Unregisters a {@link Ext.Component} from this ZIndexManager. This should not
  7643. * need to be called. Components are automatically unregistered upon destruction.
  7644. * See {@link #register}.
  7645. * @param {Ext.Component} comp The Component to unregister.
  7646. */
  7647. unregister : function(comp) {
  7648. var me = this,
  7649. list = me.list;
  7650. delete comp.zIndexManager;
  7651. if (list && list[comp.id]) {
  7652. delete list[comp.id];
  7653. comp.un('hide', me.onComponentHide);
  7654. Ext.Array.remove(me.zIndexStack, comp);
  7655. // Destruction requires that the topmost visible floater be activated. Same as hiding.
  7656. me._activateLast();
  7657. }
  7658. },
  7659. /**
  7660. * Gets a registered Component by id.
  7661. * @param {String/Object} id The id of the Component or a {@link Ext.Component} instance
  7662. * @return {Ext.Component}
  7663. */
  7664. get : function(id) {
  7665. return id.isComponent ? id : this.list[id];
  7666. },
  7667. /**
  7668. * Brings the specified Component to the front of any other active Components in this ZIndexManager.
  7669. * @param {String/Object} comp The id of the Component or a {@link Ext.Component} instance
  7670. * @return {Boolean} True if the dialog was brought to the front, else false
  7671. * if it was already in front
  7672. */
  7673. bringToFront : function(comp) {
  7674. var me = this,
  7675. result = false;
  7676. comp = me.get(comp);
  7677. if (comp !== me.front) {
  7678. Ext.Array.remove(me.zIndexStack, comp);
  7679. me.zIndexStack.push(comp);
  7680. me.assignZIndices();
  7681. result = true;
  7682. this.front = comp;
  7683. }
  7684. if (result && comp.modal) {
  7685. me._showModalMask(comp);
  7686. }
  7687. return result;
  7688. },
  7689. /**
  7690. * Sends the specified Component to the back of other active Components in this ZIndexManager.
  7691. * @param {String/Object} comp The id of the Component or a {@link Ext.Component} instance
  7692. * @return {Ext.Component} The Component
  7693. */
  7694. sendToBack : function(comp) {
  7695. var me = this;
  7696. comp = me.get(comp);
  7697. Ext.Array.remove(me.zIndexStack, comp);
  7698. me.zIndexStack.unshift(comp);
  7699. me.assignZIndices();
  7700. this._activateLast();
  7701. return comp;
  7702. },
  7703. /**
  7704. * Hides all Components managed by this ZIndexManager.
  7705. */
  7706. hideAll : function() {
  7707. var list = this.list,
  7708. item,
  7709. id;
  7710. for (id in list) {
  7711. if (list.hasOwnProperty(id)) {
  7712. item = list[id];
  7713. if (item.isComponent && item.isVisible()) {
  7714. item.hide();
  7715. }
  7716. }
  7717. }
  7718. },
  7719. /**
  7720. * @private
  7721. * Temporarily hides all currently visible managed Components. This is for when
  7722. * dragging a Window which may manage a set of floating descendants in its ZIndexManager;
  7723. * they should all be hidden just for the duration of the drag.
  7724. */
  7725. hide: function() {
  7726. var i = 0,
  7727. stack = this.zIndexStack,
  7728. len = stack.length,
  7729. comp;
  7730. this.tempHidden = [];
  7731. for (; i < len; i++) {
  7732. comp = stack[i];
  7733. if (comp.isVisible()) {
  7734. this.tempHidden.push(comp);
  7735. comp.el.hide();
  7736. }
  7737. }
  7738. },
  7739. /**
  7740. * @private
  7741. * Restores temporarily hidden managed Components to visibility.
  7742. */
  7743. show: function() {
  7744. var i = 0,
  7745. tempHidden = this.tempHidden,
  7746. len = tempHidden ? tempHidden.length : 0,
  7747. comp;
  7748. for (; i < len; i++) {
  7749. comp = tempHidden[i];
  7750. comp.el.show();
  7751. comp.setPosition(comp.x, comp.y);
  7752. }
  7753. delete this.tempHidden;
  7754. },
  7755. /**
  7756. * Gets the currently-active Component in this ZIndexManager.
  7757. * @return {Ext.Component} The active Component
  7758. */
  7759. getActive : function() {
  7760. return this.front;
  7761. },
  7762. /**
  7763. * Returns zero or more Components in this ZIndexManager using the custom search function passed to this method.
  7764. * The function should accept a single {@link Ext.Component} reference as its only argument and should
  7765. * return true if the Component matches the search criteria, otherwise it should return false.
  7766. * @param {Function} fn The search function
  7767. * @param {Object} [scope] The scope (this reference) in which the function is executed.
  7768. * Defaults to the Component being tested. That gets passed to the function if not specified.
  7769. * @return {Array} An array of zero or more matching windows
  7770. */
  7771. getBy : function(fn, scope) {
  7772. var r = [],
  7773. i = 0,
  7774. stack = this.zIndexStack,
  7775. len = stack.length,
  7776. comp;
  7777. for (; i < len; i++) {
  7778. comp = stack[i];
  7779. if (fn.call(scope||comp, comp) !== false) {
  7780. r.push(comp);
  7781. }
  7782. }
  7783. return r;
  7784. },
  7785. /**
  7786. * Executes the specified function once for every Component in this ZIndexManager, passing each
  7787. * Component as the only parameter. Returning false from the function will stop the iteration.
  7788. * @param {Function} fn The function to execute for each item
  7789. * @param {Object} [scope] The scope (this reference) in which the function
  7790. * is executed. Defaults to the current Component in the iteration.
  7791. */
  7792. each : function(fn, scope) {
  7793. var list = this.list,
  7794. id,
  7795. comp;
  7796. for (id in list) {
  7797. if (list.hasOwnProperty(id)) {
  7798. comp = list[id];
  7799. if (comp.isComponent && fn.call(scope || comp, comp) === false) {
  7800. return;
  7801. }
  7802. }
  7803. }
  7804. },
  7805. /**
  7806. * Executes the specified function once for every Component in this ZIndexManager, passing each
  7807. * Component as the only parameter. Returning false from the function will stop the iteration.
  7808. * The components are passed to the function starting at the bottom and proceeding to the top.
  7809. * @param {Function} fn The function to execute for each item
  7810. * @param {Object} scope (optional) The scope (this reference) in which the function
  7811. * is executed. Defaults to the current Component in the iteration.
  7812. */
  7813. eachBottomUp: function (fn, scope) {
  7814. var stack = this.zIndexStack,
  7815. i = 0,
  7816. len = stack.length,
  7817. comp;
  7818. for (; i < len; i++) {
  7819. comp = stack[i];
  7820. if (comp.isComponent && fn.call(scope || comp, comp) === false) {
  7821. return;
  7822. }
  7823. }
  7824. },
  7825. /**
  7826. * Executes the specified function once for every Component in this ZIndexManager, passing each
  7827. * Component as the only parameter. Returning false from the function will stop the iteration.
  7828. * The components are passed to the function starting at the top and proceeding to the bottom.
  7829. * @param {Function} fn The function to execute for each item
  7830. * @param {Object} [scope] The scope (this reference) in which the function
  7831. * is executed. Defaults to the current Component in the iteration.
  7832. */
  7833. eachTopDown: function (fn, scope) {
  7834. var stack = this.zIndexStack,
  7835. i = stack.length,
  7836. comp;
  7837. for (; i-- > 0; ) {
  7838. comp = stack[i];
  7839. if (comp.isComponent && fn.call(scope || comp, comp) === false) {
  7840. return;
  7841. }
  7842. }
  7843. },
  7844. destroy: function() {
  7845. var me = this,
  7846. list = me.list,
  7847. comp;
  7848. for (var id in list) {
  7849. if (list.hasOwnProperty(id)) {
  7850. comp = list[id];
  7851. if (comp.isComponent) {
  7852. comp.destroy();
  7853. }
  7854. }
  7855. }
  7856. delete me.zIndexStack;
  7857. delete me.list;
  7858. delete me.container;
  7859. delete me.targetEl;
  7860. }
  7861. }, function() {
  7862. /**
  7863. * @class Ext.WindowManager
  7864. * @extends Ext.ZIndexManager
  7865. *
  7866. * The default global floating Component group that is available automatically.
  7867. *
  7868. * This manages instances of floating Components which were rendered programatically without
  7869. * being added to a {@link Ext.container.Container Container}, and for floating Components
  7870. * which were added into non-floating Containers.
  7871. *
  7872. * *Floating* Containers create their own instance of ZIndexManager, and floating Components
  7873. * added at any depth below there are managed by that ZIndexManager.
  7874. *
  7875. * @singleton
  7876. */
  7877. Ext.WindowManager = Ext.WindowMgr = new this();
  7878. });
  7879. /**
  7880. * @class Ext.fx.target.Target
  7881. This class specifies a generic target for an animation. It provides a wrapper around a
  7882. series of different types of objects to allow for a generic animation API.
  7883. A target can be a single object or a Composite object containing other objects that are
  7884. to be animated. This class and it's subclasses are generally not created directly, the
  7885. underlying animation will create the appropriate Ext.fx.target.Target object by passing
  7886. the instance to be animated.
  7887. The following types of objects can be animated:
  7888. - {@link Ext.fx.target.Component Components}
  7889. - {@link Ext.fx.target.Element Elements}
  7890. - {@link Ext.fx.target.Sprite Sprites}
  7891. * @markdown
  7892. * @abstract
  7893. */
  7894. Ext.define('Ext.fx.target.Target', {
  7895. isAnimTarget: true,
  7896. /**
  7897. * Creates new Target.
  7898. * @param {Ext.Component/Ext.Element/Ext.draw.Sprite} target The object to be animated
  7899. */
  7900. constructor: function(target) {
  7901. this.target = target;
  7902. this.id = this.getId();
  7903. },
  7904. getId: function() {
  7905. return this.target.id;
  7906. }
  7907. });
  7908. /**
  7909. * Represents an RGB color and provides helper functions get
  7910. * color components in HSL color space.
  7911. */
  7912. Ext.define('Ext.draw.Color', {
  7913. /* Begin Definitions */
  7914. /* End Definitions */
  7915. colorToHexRe: /(.*?)rgb\((\d+),\s*(\d+),\s*(\d+)\)/,
  7916. rgbRe: /\s*rgb\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)\s*/,
  7917. hexRe: /\s*#([0-9a-fA-F][0-9a-fA-F]?)([0-9a-fA-F][0-9a-fA-F]?)([0-9a-fA-F][0-9a-fA-F]?)\s*/,
  7918. /**
  7919. * @cfg {Number} lightnessFactor
  7920. *
  7921. * The default factor to compute the lighter or darker color. Defaults to 0.2.
  7922. */
  7923. lightnessFactor: 0.2,
  7924. /**
  7925. * Creates new Color.
  7926. * @param {Number} red Red component (0..255)
  7927. * @param {Number} green Green component (0..255)
  7928. * @param {Number} blue Blue component (0..255)
  7929. */
  7930. constructor : function(red, green, blue) {
  7931. var me = this,
  7932. clamp = Ext.Number.constrain;
  7933. me.r = clamp(red, 0, 255);
  7934. me.g = clamp(green, 0, 255);
  7935. me.b = clamp(blue, 0, 255);
  7936. },
  7937. /**
  7938. * Get the red component of the color, in the range 0..255.
  7939. * @return {Number}
  7940. */
  7941. getRed: function() {
  7942. return this.r;
  7943. },
  7944. /**
  7945. * Get the green component of the color, in the range 0..255.
  7946. * @return {Number}
  7947. */
  7948. getGreen: function() {
  7949. return this.g;
  7950. },
  7951. /**
  7952. * Get the blue component of the color, in the range 0..255.
  7953. * @return {Number}
  7954. */
  7955. getBlue: function() {
  7956. return this.b;
  7957. },
  7958. /**
  7959. * Get the RGB values.
  7960. * @return {Number[]}
  7961. */
  7962. getRGB: function() {
  7963. var me = this;
  7964. return [me.r, me.g, me.b];
  7965. },
  7966. /**
  7967. * Get the equivalent HSL components of the color.
  7968. * @return {Number[]}
  7969. */
  7970. getHSL: function() {
  7971. var me = this,
  7972. r = me.r / 255,
  7973. g = me.g / 255,
  7974. b = me.b / 255,
  7975. max = Math.max(r, g, b),
  7976. min = Math.min(r, g, b),
  7977. delta = max - min,
  7978. h,
  7979. s = 0,
  7980. l = 0.5 * (max + min);
  7981. // min==max means achromatic (hue is undefined)
  7982. if (min != max) {
  7983. s = (l < 0.5) ? delta / (max + min) : delta / (2 - max - min);
  7984. if (r == max) {
  7985. h = 60 * (g - b) / delta;
  7986. } else if (g == max) {
  7987. h = 120 + 60 * (b - r) / delta;
  7988. } else {
  7989. h = 240 + 60 * (r - g) / delta;
  7990. }
  7991. if (h < 0) {
  7992. h += 360;
  7993. }
  7994. if (h >= 360) {
  7995. h -= 360;
  7996. }
  7997. }
  7998. return [h, s, l];
  7999. },
  8000. /**
  8001. * Return a new color that is lighter than this color.
  8002. * @param {Number} factor Lighter factor (0..1), default to 0.2
  8003. * @return Ext.draw.Color
  8004. */
  8005. getLighter: function(factor) {
  8006. var hsl = this.getHSL();
  8007. factor = factor || this.lightnessFactor;
  8008. hsl[2] = Ext.Number.constrain(hsl[2] + factor, 0, 1);
  8009. return this.fromHSL(hsl[0], hsl[1], hsl[2]);
  8010. },
  8011. /**
  8012. * Return a new color that is darker than this color.
  8013. * @param {Number} factor Darker factor (0..1), default to 0.2
  8014. * @return Ext.draw.Color
  8015. */
  8016. getDarker: function(factor) {
  8017. factor = factor || this.lightnessFactor;
  8018. return this.getLighter(-factor);
  8019. },
  8020. /**
  8021. * Return the color in the hex format, i.e. '#rrggbb'.
  8022. * @return {String}
  8023. */
  8024. toString: function() {
  8025. var me = this,
  8026. round = Math.round,
  8027. r = round(me.r).toString(16),
  8028. g = round(me.g).toString(16),
  8029. b = round(me.b).toString(16);
  8030. r = (r.length == 1) ? '0' + r : r;
  8031. g = (g.length == 1) ? '0' + g : g;
  8032. b = (b.length == 1) ? '0' + b : b;
  8033. return ['#', r, g, b].join('');
  8034. },
  8035. /**
  8036. * Convert a color to hexadecimal format.
  8037. *
  8038. * **Note:** This method is both static and instance.
  8039. *
  8040. * @param {String/String[]} color The color value (i.e 'rgb(255, 255, 255)', 'color: #ffffff').
  8041. * Can also be an Array, in this case the function handles the first member.
  8042. * @returns {String} The color in hexadecimal format.
  8043. * @static
  8044. */
  8045. toHex: function(color) {
  8046. if (Ext.isArray(color)) {
  8047. color = color[0];
  8048. }
  8049. if (!Ext.isString(color)) {
  8050. return '';
  8051. }
  8052. if (color.substr(0, 1) === '#') {
  8053. return color;
  8054. }
  8055. var digits = this.colorToHexRe.exec(color);
  8056. if (Ext.isArray(digits)) {
  8057. var red = parseInt(digits[2], 10),
  8058. green = parseInt(digits[3], 10),
  8059. blue = parseInt(digits[4], 10),
  8060. rgb = blue | (green << 8) | (red << 16);
  8061. return digits[1] + '#' + ("000000" + rgb.toString(16)).slice(-6);
  8062. }
  8063. else {
  8064. return color;
  8065. }
  8066. },
  8067. /**
  8068. * Parse the string and create a new color.
  8069. *
  8070. * Supported formats: '#rrggbb', '#rgb', and 'rgb(r,g,b)'.
  8071. *
  8072. * If the string is not recognized, an undefined will be returned instead.
  8073. *
  8074. * **Note:** This method is both static and instance.
  8075. *
  8076. * @param {String} str Color in string.
  8077. * @returns Ext.draw.Color
  8078. * @static
  8079. */
  8080. fromString: function(str) {
  8081. var values, r, g, b,
  8082. parse = parseInt;
  8083. if ((str.length == 4 || str.length == 7) && str.substr(0, 1) === '#') {
  8084. values = str.match(this.hexRe);
  8085. if (values) {
  8086. r = parse(values[1], 16) >> 0;
  8087. g = parse(values[2], 16) >> 0;
  8088. b = parse(values[3], 16) >> 0;
  8089. if (str.length == 4) {
  8090. r += (r * 16);
  8091. g += (g * 16);
  8092. b += (b * 16);
  8093. }
  8094. }
  8095. }
  8096. else {
  8097. values = str.match(this.rgbRe);
  8098. if (values) {
  8099. r = values[1];
  8100. g = values[2];
  8101. b = values[3];
  8102. }
  8103. }
  8104. return (typeof r == 'undefined') ? undefined : new Ext.draw.Color(r, g, b);
  8105. },
  8106. /**
  8107. * Returns the gray value (0 to 255) of the color.
  8108. *
  8109. * The gray value is calculated using the formula r*0.3 + g*0.59 + b*0.11.
  8110. *
  8111. * @returns {Number}
  8112. */
  8113. getGrayscale: function() {
  8114. // http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
  8115. return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
  8116. },
  8117. /**
  8118. * Create a new color based on the specified HSL values.
  8119. *
  8120. * **Note:** This method is both static and instance.
  8121. *
  8122. * @param {Number} h Hue component (0..359)
  8123. * @param {Number} s Saturation component (0..1)
  8124. * @param {Number} l Lightness component (0..1)
  8125. * @returns Ext.draw.Color
  8126. * @static
  8127. */
  8128. fromHSL: function(h, s, l) {
  8129. var C, X, m, i, rgb = [],
  8130. abs = Math.abs,
  8131. floor = Math.floor;
  8132. if (s == 0 || h == null) {
  8133. // achromatic
  8134. rgb = [l, l, l];
  8135. }
  8136. else {
  8137. // http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSL
  8138. // C is the chroma
  8139. // X is the second largest component
  8140. // m is the lightness adjustment
  8141. h /= 60;
  8142. C = s * (1 - abs(2 * l - 1));
  8143. X = C * (1 - abs(h - 2 * floor(h / 2) - 1));
  8144. m = l - C / 2;
  8145. switch (floor(h)) {
  8146. case 0:
  8147. rgb = [C, X, 0];
  8148. break;
  8149. case 1:
  8150. rgb = [X, C, 0];
  8151. break;
  8152. case 2:
  8153. rgb = [0, C, X];
  8154. break;
  8155. case 3:
  8156. rgb = [0, X, C];
  8157. break;
  8158. case 4:
  8159. rgb = [X, 0, C];
  8160. break;
  8161. case 5:
  8162. rgb = [C, 0, X];
  8163. break;
  8164. }
  8165. rgb = [rgb[0] + m, rgb[1] + m, rgb[2] + m];
  8166. }
  8167. return new Ext.draw.Color(rgb[0] * 255, rgb[1] * 255, rgb[2] * 255);
  8168. }
  8169. }, function() {
  8170. var prototype = this.prototype;
  8171. //These functions are both static and instance. TODO: find a more elegant way of copying them
  8172. this.addStatics({
  8173. fromHSL: function() {
  8174. return prototype.fromHSL.apply(prototype, arguments);
  8175. },
  8176. fromString: function() {
  8177. return prototype.fromString.apply(prototype, arguments);
  8178. },
  8179. toHex: function() {
  8180. return prototype.toHex.apply(prototype, arguments);
  8181. }
  8182. });
  8183. });
  8184. /**
  8185. * @private
  8186. * Base class for Box Layout overflow handlers. These specialized classes are invoked when a Box Layout
  8187. * (either an HBox or a VBox) has child items that are either too wide (for HBox) or too tall (for VBox)
  8188. * for its container.
  8189. */
  8190. Ext.define('Ext.layout.container.boxOverflow.None', {
  8191. alternateClassName: 'Ext.layout.boxOverflow.None',
  8192. constructor: function(layout, config) {
  8193. this.layout = layout;
  8194. Ext.apply(this, config);
  8195. },
  8196. handleOverflow: Ext.emptyFn,
  8197. clearOverflow: Ext.emptyFn,
  8198. beginLayout: Ext.emptyFn,
  8199. beginLayoutCycle: Ext.emptyFn,
  8200. completeLayout: function (ownerContext) {
  8201. var me = this,
  8202. plan = ownerContext.state.boxPlan,
  8203. overflow;
  8204. if (plan && plan.tooNarrow) {
  8205. overflow = me.handleOverflow(ownerContext);
  8206. if (overflow) {
  8207. if (overflow.reservedSpace) {
  8208. me.layout.publishInnerCtSize(ownerContext, overflow.reservedSpace);
  8209. }
  8210. // TODO: If we need to use the code below then we will need to pass along
  8211. // the new targetSize as state and use it calculate somehow...
  8212. //
  8213. //if (overflow.recalculate) {
  8214. // ownerContext.invalidate({
  8215. // state: {
  8216. // overflow: overflow
  8217. // }
  8218. // });
  8219. //}
  8220. }
  8221. } else {
  8222. me.clearOverflow();
  8223. }
  8224. },
  8225. onRemove: Ext.emptyFn,
  8226. /**
  8227. * @private
  8228. * Normalizes an item reference, string id or numerical index into a reference to the item
  8229. * @param {Ext.Component/String/Number} item The item reference, id or index
  8230. * @return {Ext.Component} The item
  8231. */
  8232. getItem: function(item) {
  8233. return this.layout.owner.getComponent(item);
  8234. },
  8235. getOwnerType: function(owner){
  8236. var type = '';
  8237. if (owner.is('toolbar')) {
  8238. type = 'toolbar';
  8239. } else if (owner.is('tabbar')) {
  8240. type = 'tabbar';
  8241. } else {
  8242. type = owner.getXType();
  8243. }
  8244. return type;
  8245. },
  8246. getPrefixConfig: Ext.emptyFn,
  8247. getSuffixConfig: Ext.emptyFn,
  8248. getOverflowCls: function() {
  8249. return '';
  8250. }
  8251. });
  8252. /**
  8253. * @class Ext.util.Offset
  8254. * @ignore
  8255. */
  8256. Ext.define('Ext.util.Offset', {
  8257. /* Begin Definitions */
  8258. statics: {
  8259. fromObject: function(obj) {
  8260. return new this(obj.x, obj.y);
  8261. }
  8262. },
  8263. /* End Definitions */
  8264. constructor: function(x, y) {
  8265. this.x = (x != null && !isNaN(x)) ? x : 0;
  8266. this.y = (y != null && !isNaN(y)) ? y : 0;
  8267. return this;
  8268. },
  8269. copy: function() {
  8270. return new Ext.util.Offset(this.x, this.y);
  8271. },
  8272. copyFrom: function(p) {
  8273. this.x = p.x;
  8274. this.y = p.y;
  8275. },
  8276. toString: function() {
  8277. return "Offset[" + this.x + "," + this.y + "]";
  8278. },
  8279. equals: function(offset) {
  8280. if(!(offset instanceof this.statics())) {
  8281. Ext.Error.raise('Offset must be an instance of Ext.util.Offset');
  8282. }
  8283. return (this.x == offset.x && this.y == offset.y);
  8284. },
  8285. round: function(to) {
  8286. if (!isNaN(to)) {
  8287. var factor = Math.pow(10, to);
  8288. this.x = Math.round(this.x * factor) / factor;
  8289. this.y = Math.round(this.y * factor) / factor;
  8290. } else {
  8291. this.x = Math.round(this.x);
  8292. this.y = Math.round(this.y);
  8293. }
  8294. },
  8295. isZero: function() {
  8296. return this.x == 0 && this.y == 0;
  8297. }
  8298. });
  8299. /**
  8300. * A wrapper class which can be applied to any element. Fires a "click" event while the
  8301. * mouse is pressed. The interval between firings may be specified in the config but
  8302. * defaults to 20 milliseconds.
  8303. *
  8304. * Optionally, a CSS class may be applied to the element during the time it is pressed.
  8305. */
  8306. Ext.define('Ext.util.ClickRepeater', {
  8307. extend: 'Ext.util.Observable',
  8308. /**
  8309. * Creates new ClickRepeater.
  8310. * @param {String/HTMLElement/Ext.Element} el The element or its ID to listen on
  8311. * @param {Object} [config] Config object.
  8312. */
  8313. constructor : function(el, config){
  8314. var me = this;
  8315. me.el = Ext.get(el);
  8316. me.el.unselectable();
  8317. Ext.apply(me, config);
  8318. me.callParent();
  8319. me.addEvents(
  8320. /**
  8321. * @event mousedown
  8322. * Fires when the mouse button is depressed.
  8323. * @param {Ext.util.ClickRepeater} this
  8324. * @param {Ext.EventObject} e
  8325. */
  8326. "mousedown",
  8327. /**
  8328. * @event click
  8329. * Fires on a specified interval during the time the element is pressed.
  8330. * @param {Ext.util.ClickRepeater} this
  8331. * @param {Ext.EventObject} e
  8332. */
  8333. "click",
  8334. /**
  8335. * @event mouseup
  8336. * Fires when the mouse key is released.
  8337. * @param {Ext.util.ClickRepeater} this
  8338. * @param {Ext.EventObject} e
  8339. */
  8340. "mouseup"
  8341. );
  8342. if(!me.disabled){
  8343. me.disabled = true;
  8344. me.enable();
  8345. }
  8346. // allow inline handler
  8347. if(me.handler){
  8348. me.on("click", me.handler, me.scope || me);
  8349. }
  8350. },
  8351. /**
  8352. * @cfg {String/HTMLElement/Ext.Element} el
  8353. * The element to act as a button.
  8354. */
  8355. /**
  8356. * @cfg {String} pressedCls
  8357. * A CSS class name to be applied to the element while pressed.
  8358. */
  8359. /**
  8360. * @cfg {Boolean} accelerate
  8361. * True if autorepeating should start slowly and accelerate.
  8362. * "interval" and "delay" are ignored.
  8363. */
  8364. /**
  8365. * @cfg {Number} interval
  8366. * The interval between firings of the "click" event (in milliseconds).
  8367. */
  8368. interval : 20,
  8369. /**
  8370. * @cfg {Number} delay
  8371. * The initial delay before the repeating event begins firing.
  8372. * Similar to an autorepeat key delay.
  8373. */
  8374. delay: 250,
  8375. /**
  8376. * @cfg {Boolean} preventDefault
  8377. * True to prevent the default click event
  8378. */
  8379. preventDefault : true,
  8380. /**
  8381. * @cfg {Boolean} stopDefault
  8382. * True to stop the default click event
  8383. */
  8384. stopDefault : false,
  8385. timer : 0,
  8386. /**
  8387. * Enables the repeater and allows events to fire.
  8388. */
  8389. enable: function(){
  8390. if(this.disabled){
  8391. this.el.on('mousedown', this.handleMouseDown, this);
  8392. if (Ext.isIE){
  8393. this.el.on('dblclick', this.handleDblClick, this);
  8394. }
  8395. if(this.preventDefault || this.stopDefault){
  8396. this.el.on('click', this.eventOptions, this);
  8397. }
  8398. }
  8399. this.disabled = false;
  8400. },
  8401. /**
  8402. * Disables the repeater and stops events from firing.
  8403. */
  8404. disable: function(/* private */ force){
  8405. if(force || !this.disabled){
  8406. clearTimeout(this.timer);
  8407. if(this.pressedCls){
  8408. this.el.removeCls(this.pressedCls);
  8409. }
  8410. Ext.getDoc().un('mouseup', this.handleMouseUp, this);
  8411. this.el.removeAllListeners();
  8412. }
  8413. this.disabled = true;
  8414. },
  8415. /**
  8416. * Convenience function for setting disabled/enabled by boolean.
  8417. * @param {Boolean} disabled
  8418. */
  8419. setDisabled: function(disabled){
  8420. this[disabled ? 'disable' : 'enable']();
  8421. },
  8422. eventOptions: function(e){
  8423. if(this.preventDefault){
  8424. e.preventDefault();
  8425. }
  8426. if(this.stopDefault){
  8427. e.stopEvent();
  8428. }
  8429. },
  8430. // private
  8431. destroy : function() {
  8432. this.disable(true);
  8433. Ext.destroy(this.el);
  8434. this.clearListeners();
  8435. },
  8436. handleDblClick : function(e){
  8437. clearTimeout(this.timer);
  8438. this.el.blur();
  8439. this.fireEvent("mousedown", this, e);
  8440. this.fireEvent("click", this, e);
  8441. },
  8442. // private
  8443. handleMouseDown : function(e){
  8444. clearTimeout(this.timer);
  8445. this.el.blur();
  8446. if(this.pressedCls){
  8447. this.el.addCls(this.pressedCls);
  8448. }
  8449. this.mousedownTime = new Date();
  8450. Ext.getDoc().on("mouseup", this.handleMouseUp, this);
  8451. this.el.on("mouseout", this.handleMouseOut, this);
  8452. this.fireEvent("mousedown", this, e);
  8453. this.fireEvent("click", this, e);
  8454. // Do not honor delay or interval if acceleration wanted.
  8455. if (this.accelerate) {
  8456. this.delay = 400;
  8457. }
  8458. // Re-wrap the event object in a non-shared object, so it doesn't lose its context if
  8459. // the global shared EventObject gets a new Event put into it before the timer fires.
  8460. e = new Ext.EventObjectImpl(e);
  8461. this.timer = Ext.defer(this.click, this.delay || this.interval, this, [e]);
  8462. },
  8463. // private
  8464. click : function(e){
  8465. this.fireEvent("click", this, e);
  8466. this.timer = Ext.defer(this.click, this.accelerate ?
  8467. this.easeOutExpo(Ext.Date.getElapsed(this.mousedownTime),
  8468. 400,
  8469. -390,
  8470. 12000) :
  8471. this.interval, this, [e]);
  8472. },
  8473. easeOutExpo : function (t, b, c, d) {
  8474. return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  8475. },
  8476. // private
  8477. handleMouseOut : function(){
  8478. clearTimeout(this.timer);
  8479. if(this.pressedCls){
  8480. this.el.removeCls(this.pressedCls);
  8481. }
  8482. this.el.on("mouseover", this.handleMouseReturn, this);
  8483. },
  8484. // private
  8485. handleMouseReturn : function(){
  8486. this.el.un("mouseover", this.handleMouseReturn, this);
  8487. if(this.pressedCls){
  8488. this.el.addCls(this.pressedCls);
  8489. }
  8490. this.click();
  8491. },
  8492. // private
  8493. handleMouseUp : function(e){
  8494. clearTimeout(this.timer);
  8495. this.el.un("mouseover", this.handleMouseReturn, this);
  8496. this.el.un("mouseout", this.handleMouseOut, this);
  8497. Ext.getDoc().un("mouseup", this.handleMouseUp, this);
  8498. if(this.pressedCls){
  8499. this.el.removeCls(this.pressedCls);
  8500. }
  8501. this.fireEvent("mouseup", this, e);
  8502. }
  8503. });
  8504. /**
  8505. * Provides precise pixel measurements for blocks of text so that you can determine exactly how high and
  8506. * wide, in pixels, a given block of text will be. Note that when measuring text, it should be plain text and
  8507. * should not contain any HTML, otherwise it may not be measured correctly.
  8508. *
  8509. * The measurement works by copying the relevant CSS styles that can affect the font related display,
  8510. * then checking the size of an element that is auto-sized. Note that if the text is multi-lined, you must
  8511. * provide a **fixed width** when doing the measurement.
  8512. *
  8513. * If multiple measurements are being done on the same element, you create a new instance to initialize
  8514. * to avoid the overhead of copying the styles to the element repeatedly.
  8515. */
  8516. Ext.define('Ext.util.TextMetrics', {
  8517. statics: {
  8518. shared: null,
  8519. /**
  8520. * Measures the size of the specified text
  8521. * @param {String/HTMLElement} el The element, dom node or id from which to copy existing CSS styles
  8522. * that can affect the size of the rendered text
  8523. * @param {String} text The text to measure
  8524. * @param {Number} fixedWidth (optional) If the text will be multiline, you have to set a fixed width
  8525. * in order to accurately measure the text height
  8526. * @return {Object} An object containing the text's size `{width: (width), height: (height)}`
  8527. * @static
  8528. */
  8529. measure: function(el, text, fixedWidth){
  8530. var me = this,
  8531. shared = me.shared;
  8532. if(!shared){
  8533. shared = me.shared = new me(el, fixedWidth);
  8534. }
  8535. shared.bind(el);
  8536. shared.setFixedWidth(fixedWidth || 'auto');
  8537. return shared.getSize(text);
  8538. },
  8539. /**
  8540. * Destroy the TextMetrics instance created by {@link #measure}.
  8541. * @static
  8542. */
  8543. destroy: function(){
  8544. var me = this;
  8545. Ext.destroy(me.shared);
  8546. me.shared = null;
  8547. }
  8548. },
  8549. /**
  8550. * Creates new TextMetrics.
  8551. * @param {String/HTMLElement/Ext.Element} bindTo The element or its ID to bind to.
  8552. * @param {Number} [fixedWidth] A fixed width to apply to the measuring element.
  8553. */
  8554. constructor: function(bindTo, fixedWidth){
  8555. var measure = this.measure = Ext.getBody().createChild({
  8556. cls: Ext.baseCSSPrefix + 'textmetrics'
  8557. });
  8558. this.el = Ext.get(bindTo);
  8559. measure.position('absolute');
  8560. measure.setLeftTop(-1000, -1000);
  8561. measure.hide();
  8562. if (fixedWidth) {
  8563. measure.setWidth(fixedWidth);
  8564. }
  8565. },
  8566. /**
  8567. * Returns the size of the specified text based on the internal element's style and width properties
  8568. * @param {String} text The text to measure
  8569. * @return {Object} An object containing the text's size `{width: (width), height: (height)}`
  8570. */
  8571. getSize: function(text){
  8572. var measure = this.measure,
  8573. size;
  8574. measure.update(text);
  8575. size = measure.getSize();
  8576. measure.update('');
  8577. return size;
  8578. },
  8579. /**
  8580. * Binds this TextMetrics instance to a new element
  8581. * @param {String/HTMLElement/Ext.Element} el The element or its ID.
  8582. */
  8583. bind: function(el){
  8584. var me = this;
  8585. me.el = Ext.get(el);
  8586. me.measure.setStyle(
  8587. me.el.getStyles('font-size','font-style', 'font-weight', 'font-family','line-height', 'text-transform', 'letter-spacing')
  8588. );
  8589. },
  8590. /**
  8591. * Sets a fixed width on the internal measurement element. If the text will be multiline, you have
  8592. * to set a fixed width in order to accurately measure the text height.
  8593. * @param {Number} width The width to set on the element
  8594. */
  8595. setFixedWidth : function(width){
  8596. this.measure.setWidth(width);
  8597. },
  8598. /**
  8599. * Returns the measured width of the specified text
  8600. * @param {String} text The text to measure
  8601. * @return {Number} width The width in pixels
  8602. */
  8603. getWidth : function(text){
  8604. this.measure.dom.style.width = 'auto';
  8605. return this.getSize(text).width;
  8606. },
  8607. /**
  8608. * Returns the measured height of the specified text
  8609. * @param {String} text The text to measure
  8610. * @return {Number} height The height in pixels
  8611. */
  8612. getHeight : function(text){
  8613. return this.getSize(text).height;
  8614. },
  8615. /**
  8616. * Destroy this instance
  8617. */
  8618. destroy: function(){
  8619. var me = this;
  8620. me.measure.remove();
  8621. delete me.el;
  8622. delete me.measure;
  8623. }
  8624. }, function(){
  8625. Ext.Element.addMethods({
  8626. /**
  8627. * Returns the width in pixels of the passed text, or the width of the text in this Element.
  8628. * @param {String} text The text to measure. Defaults to the innerHTML of the element.
  8629. * @param {Number} [min] The minumum value to return.
  8630. * @param {Number} [max] The maximum value to return.
  8631. * @return {Number} The text width in pixels.
  8632. * @member Ext.dom.Element
  8633. */
  8634. getTextWidth : function(text, min, max){
  8635. return Ext.Number.constrain(Ext.util.TextMetrics.measure(this.dom, Ext.value(text, this.dom.innerHTML, true)).width, min || 0, max || 1000000);
  8636. }
  8637. });
  8638. });
  8639. /**
  8640. * @class Ext.app.Controller
  8641. *
  8642. * Controllers are the glue that binds an application together. All they really do is listen for events (usually from
  8643. * views) and take some action. Here's how we might create a Controller to manage Users:
  8644. *
  8645. * Ext.define('MyApp.controller.Users', {
  8646. * extend: 'Ext.app.Controller',
  8647. *
  8648. * init: function() {
  8649. * console.log('Initialized Users! This happens before the Application launch function is called');
  8650. * }
  8651. * });
  8652. *
  8653. * The init function is a special method that is called when your application boots. It is called before the
  8654. * {@link Ext.app.Application Application}'s launch function is executed so gives a hook point to run any code before
  8655. * your Viewport is created.
  8656. *
  8657. * The init function is a great place to set up how your controller interacts with the view, and is usually used in
  8658. * conjunction with another Controller function - {@link Ext.app.Controller#control control}. The control function
  8659. * makes it easy to listen to events on your view classes and take some action with a handler function. Let's update
  8660. * our Users controller to tell us when the panel is rendered:
  8661. *
  8662. * Ext.define('MyApp.controller.Users', {
  8663. * extend: 'Ext.app.Controller',
  8664. *
  8665. * init: function() {
  8666. * this.control({
  8667. * 'viewport > panel': {
  8668. * render: this.onPanelRendered
  8669. * }
  8670. * });
  8671. * },
  8672. *
  8673. * onPanelRendered: function() {
  8674. * console.log('The panel was rendered');
  8675. * }
  8676. * });
  8677. *
  8678. * We've updated the init function to use this.control to set up listeners on views in our application. The control
  8679. * function uses the new ComponentQuery engine to quickly and easily get references to components on the page. If you
  8680. * are not familiar with ComponentQuery yet, be sure to check out the {@link Ext.ComponentQuery documentation}. In brief though,
  8681. * it allows us to pass a CSS-like selector that will find every matching component on the page.
  8682. *
  8683. * In our init function above we supplied 'viewport > panel', which translates to "find me every Panel that is a direct
  8684. * child of a Viewport". We then supplied an object that maps event names (just 'render' in this case) to handler
  8685. * functions. The overall effect is that whenever any component that matches our selector fires a 'render' event, our
  8686. * onPanelRendered function is called.
  8687. *
  8688. * <u>Using refs</u>
  8689. *
  8690. * One of the most useful parts of Controllers is the new ref system. These use the new {@link Ext.ComponentQuery} to
  8691. * make it really easy to get references to Views on your page. Let's look at an example of this now:
  8692. *
  8693. * Ext.define('MyApp.controller.Users', {
  8694. * extend: 'Ext.app.Controller',
  8695. *
  8696. * refs: [
  8697. * {
  8698. * ref: 'list',
  8699. * selector: 'grid'
  8700. * }
  8701. * ],
  8702. *
  8703. * init: function() {
  8704. * this.control({
  8705. * 'button': {
  8706. * click: this.refreshGrid
  8707. * }
  8708. * });
  8709. * },
  8710. *
  8711. * refreshGrid: function() {
  8712. * this.getList().store.load();
  8713. * }
  8714. * });
  8715. *
  8716. * This example assumes the existence of a {@link Ext.grid.Panel Grid} on the page, which contains a single button to
  8717. * refresh the Grid when clicked. In our refs array, we set up a reference to the grid. There are two parts to this -
  8718. * the 'selector', which is a {@link Ext.ComponentQuery ComponentQuery} selector which finds any grid on the page and
  8719. * assigns it to the reference 'list'.
  8720. *
  8721. * By giving the reference a name, we get a number of things for free. The first is the getList function that we use in
  8722. * the refreshGrid method above. This is generated automatically by the Controller based on the name of our ref, which
  8723. * was capitalized and prepended with get to go from 'list' to 'getList'.
  8724. *
  8725. * The way this works is that the first time getList is called by your code, the ComponentQuery selector is run and the
  8726. * first component that matches the selector ('grid' in this case) will be returned. All future calls to getList will
  8727. * use a cached reference to that grid. Usually it is advised to use a specific ComponentQuery selector that will only
  8728. * match a single View in your application (in the case above our selector will match any grid on the page).
  8729. *
  8730. * Bringing it all together, our init function is called when the application boots, at which time we call this.control
  8731. * to listen to any click on a {@link Ext.button.Button button} and call our refreshGrid function (again, this will
  8732. * match any button on the page so we advise a more specific selector than just 'button', but have left it this way for
  8733. * simplicity). When the button is clicked we use out getList function to refresh the grid.
  8734. *
  8735. * You can create any number of refs and control any number of components this way, simply adding more functions to
  8736. * your Controller as you go. For an example of real-world usage of Controllers see the Feed Viewer example in the
  8737. * examples/app/feed-viewer folder in the SDK download.
  8738. *
  8739. * <u>Generated getter methods</u>
  8740. *
  8741. * Refs aren't the only thing that generate convenient getter methods. Controllers often have to deal with Models and
  8742. * Stores so the framework offers a couple of easy ways to get access to those too. Let's look at another example:
  8743. *
  8744. * Ext.define('MyApp.controller.Users', {
  8745. * extend: 'Ext.app.Controller',
  8746. *
  8747. * models: ['User'],
  8748. * stores: ['AllUsers', 'AdminUsers'],
  8749. *
  8750. * init: function() {
  8751. * var User = this.getUserModel(),
  8752. * allUsers = this.getAllUsersStore();
  8753. *
  8754. * var ed = new User({name: 'Ed'});
  8755. * allUsers.add(ed);
  8756. * }
  8757. * });
  8758. *
  8759. * By specifying Models and Stores that the Controller cares about, it again dynamically loads them from the appropriate
  8760. * locations (app/model/User.js, app/store/AllUsers.js and app/store/AdminUsers.js in this case) and creates getter
  8761. * functions for them all. The example above will create a new User model instance and add it to the AllUsers Store.
  8762. * Of course, you could do anything in this function but in this case we just did something simple to demonstrate the
  8763. * functionality.
  8764. *
  8765. * <u>Further Reading</u>
  8766. *
  8767. * For more information about writing Ext JS 4 applications, please see the
  8768. * [application architecture guide](#/guide/application_architecture). Also see the {@link Ext.app.Application} documentation.
  8769. *
  8770. * @docauthor Ed Spencer
  8771. */
  8772. Ext.define('Ext.app.Controller', {
  8773. mixins: {
  8774. observable: 'Ext.util.Observable'
  8775. },
  8776. /**
  8777. * @cfg {String} id The id of this controller. You can use this id when dispatching.
  8778. */
  8779. /**
  8780. * @cfg {String[]} models
  8781. * Array of models to require from AppName.model namespace. For example:
  8782. *
  8783. * Ext.define("MyApp.controller.Foo", {
  8784. * extend: "Ext.app.Controller",
  8785. * models: ['User', 'Vehicle']
  8786. * });
  8787. *
  8788. * This is equivalent of:
  8789. *
  8790. * Ext.define("MyApp.controller.Foo", {
  8791. * extend: "Ext.app.Controller",
  8792. * requires: ['MyApp.model.User', 'MyApp.model.Vehicle']
  8793. * });
  8794. *
  8795. */
  8796. /**
  8797. * @cfg {String[]} views
  8798. * Array of views to require from AppName.view namespace. For example:
  8799. *
  8800. * Ext.define("MyApp.controller.Foo", {
  8801. * extend: "Ext.app.Controller",
  8802. * views: ['List', 'Detail']
  8803. * });
  8804. *
  8805. * This is equivalent of:
  8806. *
  8807. * Ext.define("MyApp.controller.Foo", {
  8808. * extend: "Ext.app.Controller",
  8809. * requires: ['MyApp.view.List', 'MyApp.view.Detail']
  8810. * });
  8811. *
  8812. */
  8813. /**
  8814. * @cfg {String[]} stores
  8815. * Array of stores to require from AppName.store namespace. For example:
  8816. *
  8817. * Ext.define("MyApp.controller.Foo", {
  8818. * extend: "Ext.app.Controller",
  8819. * stores: ['Users', 'Vehicles']
  8820. * });
  8821. *
  8822. * This is equivalent of:
  8823. *
  8824. * Ext.define("MyApp.controller.Foo", {
  8825. * extend: "Ext.app.Controller",
  8826. * requires: ['MyApp.store.Users', 'MyApp.store.Vehicles']
  8827. * });
  8828. *
  8829. */
  8830. /**
  8831. * @cfg {Object[]} refs
  8832. * Array of configs to build up references to views on page. For example:
  8833. *
  8834. * Ext.define("MyApp.controller.Foo", {
  8835. * extend: "Ext.app.Controller",
  8836. * refs: [
  8837. * {
  8838. * ref: 'list',
  8839. * selector: 'grid'
  8840. * }
  8841. * ],
  8842. * });
  8843. *
  8844. * This will add method `getList` to the controller which will internally use
  8845. * Ext.ComponentQuery to reference the grid component on page.
  8846. */
  8847. onClassExtended: function(cls, data, hooks) {
  8848. var className = Ext.getClassName(cls),
  8849. match = className.match(/^(.*)\.controller\./);
  8850. if (match !== null) {
  8851. var namespace = Ext.Loader.getPrefix(className) || match[1],
  8852. onBeforeClassCreated = hooks.onBeforeCreated,
  8853. requires = [],
  8854. modules = ['model', 'view', 'store'],
  8855. prefix;
  8856. hooks.onBeforeCreated = function(cls, data) {
  8857. var i, ln, module,
  8858. items, j, subLn, item;
  8859. for (i = 0,ln = modules.length; i < ln; i++) {
  8860. module = modules[i];
  8861. items = Ext.Array.from(data[module + 's']);
  8862. for (j = 0,subLn = items.length; j < subLn; j++) {
  8863. item = items[j];
  8864. prefix = Ext.Loader.getPrefix(item);
  8865. if (prefix === '' || prefix === item) {
  8866. requires.push(namespace + '.' + module + '.' + item);
  8867. }
  8868. else {
  8869. requires.push(item);
  8870. }
  8871. }
  8872. }
  8873. Ext.require(requires, Ext.Function.pass(onBeforeClassCreated, arguments, this));
  8874. };
  8875. }
  8876. },
  8877. /**
  8878. * Creates new Controller.
  8879. * @param {Object} config (optional) Config object.
  8880. */
  8881. constructor: function(config) {
  8882. this.mixins.observable.constructor.call(this, config);
  8883. Ext.apply(this, config || {});
  8884. this.createGetters('model', this.models);
  8885. this.createGetters('store', this.stores);
  8886. this.createGetters('view', this.views);
  8887. if (this.refs) {
  8888. this.ref(this.refs);
  8889. }
  8890. },
  8891. /**
  8892. * A template method that is called when your application boots. It is called before the
  8893. * {@link Ext.app.Application Application}'s launch function is executed so gives a hook point to run any code before
  8894. * your Viewport is created.
  8895. *
  8896. * @param {Ext.app.Application} application
  8897. * @template
  8898. */
  8899. init: function(application) {},
  8900. /**
  8901. * A template method like {@link #init}, but called after the viewport is created.
  8902. * This is called after the {@link Ext.app.Application#launch launch} method of Application is executed.
  8903. *
  8904. * @param {Ext.app.Application} application
  8905. * @template
  8906. */
  8907. onLaunch: function(application) {},
  8908. createGetters: function(type, refs) {
  8909. type = Ext.String.capitalize(type);
  8910. var i = 0,
  8911. length = (refs) ? refs.length : 0,
  8912. fn, ref, parts, x, numparts;
  8913. for (; i < length; i++) {
  8914. fn = 'get';
  8915. ref = refs[i];
  8916. parts = ref.split('.');
  8917. numParts = parts.length;
  8918. // Handle namespaced class names. E.g. feed.Add becomes getFeedAddView etc.
  8919. for (x = 0 ; x < numParts; x++) {
  8920. fn += Ext.String.capitalize(parts[x]);
  8921. }
  8922. fn += type;
  8923. if (!this[fn]) {
  8924. this[fn] = Ext.Function.pass(this['get' + type], [ref], this);
  8925. }
  8926. // Execute it right away
  8927. this[fn](ref);
  8928. }
  8929. },
  8930. ref: function(refs) {
  8931. refs = Ext.Array.from(refs);
  8932. var me = this,
  8933. i = 0,
  8934. length = refs.length,
  8935. info, ref, fn;
  8936. for (; i < length; i++) {
  8937. info = refs[i];
  8938. ref = info.ref;
  8939. fn = 'get' + Ext.String.capitalize(ref);
  8940. if (!me[fn]) {
  8941. me[fn] = Ext.Function.pass(me.getRef, [ref, info], me);
  8942. }
  8943. me.references = me.references || [];
  8944. me.references.push(ref.toLowerCase());
  8945. }
  8946. },
  8947. addRef: function(ref) {
  8948. return this.ref([ref]);
  8949. },
  8950. getRef: function(ref, info, config) {
  8951. this.refCache = this.refCache || {};
  8952. info = info || {};
  8953. config = config || {};
  8954. Ext.apply(info, config);
  8955. if (info.forceCreate) {
  8956. return Ext.ComponentManager.create(info, 'component');
  8957. }
  8958. var me = this,
  8959. cached = me.refCache[ref];
  8960. if (!cached) {
  8961. me.refCache[ref] = cached = Ext.ComponentQuery.query(info.selector)[0];
  8962. if (!cached && info.autoCreate) {
  8963. me.refCache[ref] = cached = Ext.ComponentManager.create(info, 'component');
  8964. }
  8965. if (cached) {
  8966. cached.on('beforedestroy', function() {
  8967. me.refCache[ref] = null;
  8968. });
  8969. }
  8970. }
  8971. return cached;
  8972. },
  8973. hasRef: function(ref) {
  8974. return this.references && this.references.indexOf(ref.toLowerCase()) !== -1;
  8975. },
  8976. /**
  8977. * Adds listeners to components selected via {@link Ext.ComponentQuery}. Accepts an
  8978. * object containing component paths mapped to a hash of listener functions.
  8979. *
  8980. * In the following example the `updateUser` function is mapped to to the `click`
  8981. * event on a button component, which is a child of the `useredit` component.
  8982. *
  8983. * Ext.define('AM.controller.Users', {
  8984. * init: function() {
  8985. * this.control({
  8986. * 'useredit button[action=save]': {
  8987. * click: this.updateUser
  8988. * }
  8989. * });
  8990. * },
  8991. *
  8992. * updateUser: function(button) {
  8993. * console.log('clicked the Save button');
  8994. * }
  8995. * });
  8996. *
  8997. * See {@link Ext.ComponentQuery} for more information on component selectors.
  8998. *
  8999. * @param {String/Object} selectors If a String, the second argument is used as the
  9000. * listeners, otherwise an object of selectors -> listeners is assumed
  9001. * @param {Object} listeners
  9002. */
  9003. control: function(selectors, listeners) {
  9004. this.application.control(selectors, listeners, this);
  9005. },
  9006. /**
  9007. * Returns instance of a {@link Ext.app.Controller controller} with the given name.
  9008. * When controller doesn't exist yet, it's created.
  9009. * @param {String} name
  9010. * @return {Ext.app.Controller} a controller instance.
  9011. */
  9012. getController: function(name) {
  9013. return this.application.getController(name);
  9014. },
  9015. /**
  9016. * Returns instance of a {@link Ext.data.Store Store} with the given name.
  9017. * When store doesn't exist yet, it's created.
  9018. * @param {String} name
  9019. * @return {Ext.data.Store} a store instance.
  9020. */
  9021. getStore: function(name) {
  9022. return this.application.getStore(name);
  9023. },
  9024. /**
  9025. * Returns a {@link Ext.data.Model Model} class with the given name.
  9026. * A shorthand for using {@link Ext.ModelManager#getModel}.
  9027. * @param {String} name
  9028. * @return {Ext.data.Model} a model class.
  9029. */
  9030. getModel: function(model) {
  9031. return this.application.getModel(model);
  9032. },
  9033. /**
  9034. * Returns a View class with the given name. To create an instance of the view,
  9035. * you can use it like it's used by Application to create the Viewport:
  9036. *
  9037. * this.getView('Viewport').create();
  9038. *
  9039. * @param {String} name
  9040. * @return {Ext.Base} a view class.
  9041. */
  9042. getView: function(view) {
  9043. return this.application.getView(view);
  9044. }
  9045. });
  9046. /**
  9047. * Base Manager class
  9048. */
  9049. Ext.define('Ext.AbstractManager', {
  9050. /* Begin Definitions */
  9051. requires: ['Ext.util.HashMap'],
  9052. /* End Definitions */
  9053. typeName: 'type',
  9054. constructor: function(config) {
  9055. Ext.apply(this, config || {});
  9056. /**
  9057. * @property {Ext.util.HashMap} all
  9058. * Contains all of the items currently managed
  9059. */
  9060. this.all = new Ext.util.HashMap();
  9061. this.types = {};
  9062. },
  9063. /**
  9064. * Returns an item by id.
  9065. * For additional details see {@link Ext.util.HashMap#get}.
  9066. * @param {String} id The id of the item
  9067. * @return {Object} The item, undefined if not found.
  9068. */
  9069. get : function(id) {
  9070. return this.all.get(id);
  9071. },
  9072. /**
  9073. * Registers an item to be managed
  9074. * @param {Object} item The item to register
  9075. */
  9076. register: function(item) {
  9077. var all = this.all,
  9078. key = all.getKey(item);
  9079. if (all.containsKey(key)) {
  9080. Ext.Error.raise('Registering duplicate id "' + key + '" with this manager');
  9081. }
  9082. this.all.add(item);
  9083. },
  9084. /**
  9085. * Unregisters an item by removing it from this manager
  9086. * @param {Object} item The item to unregister
  9087. */
  9088. unregister: function(item) {
  9089. this.all.remove(item);
  9090. },
  9091. /**
  9092. * Registers a new item constructor, keyed by a type key.
  9093. * @param {String} type The mnemonic string by which the class may be looked up.
  9094. * @param {Function} cls The new instance class.
  9095. */
  9096. registerType : function(type, cls) {
  9097. this.types[type] = cls;
  9098. cls[this.typeName] = type;
  9099. },
  9100. /**
  9101. * Checks if an item type is registered.
  9102. * @param {String} type The mnemonic string by which the class may be looked up
  9103. * @return {Boolean} Whether the type is registered.
  9104. */
  9105. isRegistered : function(type){
  9106. return this.types[type] !== undefined;
  9107. },
  9108. /**
  9109. * Creates and returns an instance of whatever this manager manages, based on the supplied type and
  9110. * config object.
  9111. * @param {Object} config The config object
  9112. * @param {String} defaultType If no type is discovered in the config object, we fall back to this type
  9113. * @return {Object} The instance of whatever this manager is managing
  9114. */
  9115. create: function(config, defaultType) {
  9116. var type = config[this.typeName] || config.type || defaultType,
  9117. Constructor = this.types[type];
  9118. if (Constructor === undefined) {
  9119. Ext.Error.raise("The '" + type + "' type has not been registered with this manager");
  9120. }
  9121. return new Constructor(config);
  9122. },
  9123. /**
  9124. * Registers a function that will be called when an item with the specified id is added to the manager.
  9125. * This will happen on instantiation.
  9126. * @param {String} id The item id
  9127. * @param {Function} fn The callback function. Called with a single parameter, the item.
  9128. * @param {Object} scope The scope (this reference) in which the callback is executed.
  9129. * Defaults to the item.
  9130. */
  9131. onAvailable : function(id, fn, scope){
  9132. var all = this.all,
  9133. item;
  9134. if (all.containsKey(id)) {
  9135. item = all.get(id);
  9136. fn.call(scope || item, item);
  9137. } else {
  9138. all.on('add', function(map, key, item){
  9139. if (key == id) {
  9140. fn.call(scope || item, item);
  9141. all.un('add', fn, scope);
  9142. }
  9143. });
  9144. }
  9145. },
  9146. /**
  9147. * Executes the specified function once for each item in the collection.
  9148. * @param {Function} fn The function to execute.
  9149. * @param {String} fn.key The key of the item
  9150. * @param {Number} fn.value The value of the item
  9151. * @param {Number} fn.length The total number of items in the collection
  9152. * @param {Boolean} fn.return False to cease iteration.
  9153. * @param {Object} scope The scope to execute in. Defaults to `this`.
  9154. */
  9155. each: function(fn, scope){
  9156. this.all.each(fn, scope || this);
  9157. },
  9158. /**
  9159. * Gets the number of items in the collection.
  9160. * @return {Number} The number of items in the collection.
  9161. */
  9162. getCount: function(){
  9163. return this.all.getCount();
  9164. }
  9165. });
  9166. /**
  9167. * @author Ed Spencer
  9168. * @class Ext.ModelManager
  9169. The ModelManager keeps track of all {@link Ext.data.Model} types defined in your application.
  9170. __Creating Model Instances__
  9171. Model instances can be created by using the {@link Ext#create Ext.create} method. Ext.create replaces
  9172. the deprecated {@link #create Ext.ModelManager.create} method. It is also possible to create a model instance
  9173. this by using the Model type directly. The following 3 snippets are equivalent:
  9174. Ext.define('User', {
  9175. extend: 'Ext.data.Model',
  9176. fields: ['first', 'last']
  9177. });
  9178. // method 1, create using Ext.create (recommended)
  9179. Ext.create('User', {
  9180. first: 'Ed',
  9181. last: 'Spencer'
  9182. });
  9183. // method 2, create through the manager (deprecated)
  9184. Ext.ModelManager.create({
  9185. first: 'Ed',
  9186. last: 'Spencer'
  9187. }, 'User');
  9188. // method 3, create on the type directly
  9189. new User({
  9190. first: 'Ed',
  9191. last: 'Spencer'
  9192. });
  9193. __Accessing Model Types__
  9194. A reference to a Model type can be obtained by using the {@link #getModel} function. Since models types
  9195. are normal classes, you can access the type directly. The following snippets are equivalent:
  9196. Ext.define('User', {
  9197. extend: 'Ext.data.Model',
  9198. fields: ['first', 'last']
  9199. });
  9200. // method 1, access model type through the manager
  9201. var UserType = Ext.ModelManager.getModel('User');
  9202. // method 2, reference the type directly
  9203. var UserType = User;
  9204. * @markdown
  9205. * @singleton
  9206. */
  9207. Ext.define('Ext.ModelManager', {
  9208. extend: 'Ext.AbstractManager',
  9209. alternateClassName: 'Ext.ModelMgr',
  9210. requires: ['Ext.data.association.Association'],
  9211. singleton: true,
  9212. typeName: 'mtype',
  9213. /**
  9214. * Private stack of associations that must be created once their associated model has been defined
  9215. * @property {Ext.data.association.Association[]} associationStack
  9216. */
  9217. associationStack: [],
  9218. /**
  9219. * Registers a model definition. All model plugins marked with isDefault: true are bootstrapped
  9220. * immediately, as are any addition plugins defined in the model config.
  9221. * @private
  9222. */
  9223. registerType: function(name, config) {
  9224. var proto = config.prototype,
  9225. model;
  9226. if (proto && proto.isModel) {
  9227. // registering an already defined model
  9228. model = config;
  9229. } else {
  9230. // passing in a configuration
  9231. if (!config.extend) {
  9232. config.extend = 'Ext.data.Model';
  9233. }
  9234. model = Ext.define(name, config);
  9235. }
  9236. this.types[name] = model;
  9237. return model;
  9238. },
  9239. /**
  9240. * @private
  9241. * Private callback called whenever a model has just been defined. This sets up any associations
  9242. * that were waiting for the given model to be defined
  9243. * @param {Function} model The model that was just created
  9244. */
  9245. onModelDefined: function(model) {
  9246. var stack = this.associationStack,
  9247. length = stack.length,
  9248. create = [],
  9249. association, i, created;
  9250. for (i = 0; i < length; i++) {
  9251. association = stack[i];
  9252. if (association.associatedModel == model.modelName) {
  9253. create.push(association);
  9254. }
  9255. }
  9256. for (i = 0, length = create.length; i < length; i++) {
  9257. created = create[i];
  9258. this.types[created.ownerModel].prototype.associations.add(Ext.data.association.Association.create(created));
  9259. Ext.Array.remove(stack, created);
  9260. }
  9261. },
  9262. /**
  9263. * Registers an association where one of the models defined doesn't exist yet.
  9264. * The ModelManager will check when new models are registered if it can link them
  9265. * together
  9266. * @private
  9267. * @param {Ext.data.association.Association} association The association
  9268. */
  9269. registerDeferredAssociation: function(association){
  9270. this.associationStack.push(association);
  9271. },
  9272. /**
  9273. * Returns the {@link Ext.data.Model} for a given model name
  9274. * @param {String/Object} id The id of the model or the model instance.
  9275. * @return {Ext.data.Model} a model class.
  9276. */
  9277. getModel: function(id) {
  9278. var model = id;
  9279. if (typeof model == 'string') {
  9280. model = this.types[model];
  9281. }
  9282. return model;
  9283. },
  9284. /**
  9285. * Creates a new instance of a Model using the given data.
  9286. *
  9287. * This method is deprecated. Use {@link Ext#create Ext.create} instead. For example:
  9288. *
  9289. * Ext.create('User', {
  9290. * first: 'Ed',
  9291. * last: 'Spencer'
  9292. * });
  9293. *
  9294. * @param {Object} data Data to initialize the Model's fields with
  9295. * @param {String} name The name of the model to create
  9296. * @param {Number} id (Optional) unique id of the Model instance (see {@link Ext.data.Model})
  9297. */
  9298. create: function(config, name, id) {
  9299. var con = typeof name == 'function' ? name : this.types[name || config.name];
  9300. return new con(config, id);
  9301. }
  9302. }, function() {
  9303. /**
  9304. * Old way for creating Model classes. Instead use:
  9305. *
  9306. * Ext.define("MyModel", {
  9307. * extend: "Ext.data.Model",
  9308. * fields: []
  9309. * });
  9310. *
  9311. * @param {String} name Name of the Model class.
  9312. * @param {Object} config A configuration object for the Model you wish to create.
  9313. * @return {Ext.data.Model} The newly registered Model
  9314. * @member Ext
  9315. * @deprecated 4.0.0 Use {@link Ext#define} instead.
  9316. */
  9317. Ext.regModel = function() {
  9318. if (Ext.isDefined(Ext.global.console)) {
  9319. Ext.global.console.warn('Ext.regModel has been deprecated. Models can now be created by extending Ext.data.Model: Ext.define("MyModel", {extend: "Ext.data.Model", fields: []});.');
  9320. }
  9321. return this.ModelManager.registerType.apply(this.ModelManager, arguments);
  9322. };
  9323. });
  9324. /**
  9325. * @class Ext.ComponentManager
  9326. * <p>Provides a registry of all Components (instances of {@link Ext.Component} or any subclass
  9327. * thereof) on a page so that they can be easily accessed by {@link Ext.Component component}
  9328. * {@link Ext.Component#id id} (see {@link #get}, or the convenience method {@link Ext#getCmp Ext.getCmp}).</p>
  9329. * <p>This object also provides a registry of available Component <i>classes</i>
  9330. * indexed by a mnemonic code known as the Component's {@link Ext.Component#xtype xtype}.
  9331. * The <code>xtype</code> provides a way to avoid instantiating child Components
  9332. * when creating a full, nested config object for a complete Ext page.</p>
  9333. * <p>A child Component may be specified simply as a <i>config object</i>
  9334. * as long as the correct <code>{@link Ext.Component#xtype xtype}</code> is specified so that if and when the Component
  9335. * needs rendering, the correct type can be looked up for lazy instantiation.</p>
  9336. * <p>For a list of all available <code>{@link Ext.Component#xtype xtypes}</code>, see {@link Ext.Component}.</p>
  9337. * @singleton
  9338. */
  9339. Ext.define('Ext.ComponentManager', {
  9340. extend: 'Ext.AbstractManager',
  9341. alternateClassName: 'Ext.ComponentMgr',
  9342. singleton: true,
  9343. typeName: 'xtype',
  9344. /**
  9345. * Creates a new Component from the specified config object using the
  9346. * config object's xtype to determine the class to instantiate.
  9347. * @param {Object} config A configuration object for the Component you wish to create.
  9348. * @param {String} defaultType (optional) The xtype to use if the config object does not
  9349. * contain a <code>xtype</code>. (Optional if the config contains a <code>xtype</code>).
  9350. * @return {Ext.Component} The newly instantiated Component.
  9351. */
  9352. create: function(component, defaultType){
  9353. if (typeof component == 'string') {
  9354. return Ext.widget(component);
  9355. }
  9356. if (component.isComponent) {
  9357. return component;
  9358. }
  9359. return Ext.widget(component.xtype || defaultType, component);
  9360. },
  9361. registerType: function(type, cls) {
  9362. this.types[type] = cls;
  9363. cls[this.typeName] = type;
  9364. cls.prototype[this.typeName] = type;
  9365. }
  9366. });
  9367. /**
  9368. * @class Ext.data.Types
  9369. * <p>This is a static class containing the system-supplied data types which may be given to a {@link Ext.data.Field Field}.<p/>
  9370. * <p>The properties in this class are used as type indicators in the {@link Ext.data.Field Field} class, so to
  9371. * test whether a Field is of a certain type, compare the {@link Ext.data.Field#type type} property against properties
  9372. * of this class.</p>
  9373. * <p>Developers may add their own application-specific data types to this class. Definition names must be UPPERCASE.
  9374. * each type definition must contain three properties:</p>
  9375. * <div class="mdetail-params"><ul>
  9376. * <li><code>convert</code> : <i>Function</i><div class="sub-desc">A function to convert raw data values from a data block into the data
  9377. * to be stored in the Field. The function is passed the collowing parameters:
  9378. * <div class="mdetail-params"><ul>
  9379. * <li><b>v</b> : Mixed<div class="sub-desc">The data value as read by the Reader, if undefined will use
  9380. * the configured <tt>{@link Ext.data.Field#defaultValue defaultValue}</tt>.</div></li>
  9381. * <li><b>rec</b> : Mixed<div class="sub-desc">The data object containing the row as read by the Reader.
  9382. * Depending on the Reader type, this could be an Array ({@link Ext.data.reader.Array ArrayReader}), an object
  9383. * ({@link Ext.data.reader.Json JsonReader}), or an XML element.</div></li>
  9384. * </ul></div></div></li>
  9385. * <li><code>sortType</code> : <i>Function</i> <div class="sub-desc">A function to convert the stored data into comparable form, as defined by {@link Ext.data.SortTypes}.</div></li>
  9386. * <li><code>type</code> : <i>String</i> <div class="sub-desc">A textual data type name.</div></li>
  9387. * </ul></div>
  9388. * <p>For example, to create a VELatLong field (See the Microsoft Bing Mapping API) containing the latitude/longitude value of a datapoint on a map from a JsonReader data block
  9389. * which contained the properties <code>lat</code> and <code>long</code>, you would define a new data type like this:</p>
  9390. *<pre><code>
  9391. // Add a new Field data type which stores a VELatLong object in the Record.
  9392. Ext.data.Types.VELATLONG = {
  9393. convert: function(v, data) {
  9394. return new VELatLong(data.lat, data.long);
  9395. },
  9396. sortType: function(v) {
  9397. return v.Latitude; // When sorting, order by latitude
  9398. },
  9399. type: 'VELatLong'
  9400. };
  9401. </code></pre>
  9402. * <p>Then, when declaring a Model, use: <pre><code>
  9403. var types = Ext.data.Types; // allow shorthand type access
  9404. Ext.define('Unit',
  9405. extend: 'Ext.data.Model',
  9406. fields: [
  9407. { name: 'unitName', mapping: 'UnitName' },
  9408. { name: 'curSpeed', mapping: 'CurSpeed', type: types.INT },
  9409. { name: 'latitude', mapping: 'lat', type: types.FLOAT },
  9410. { name: 'longitude', mapping: 'long', type: types.FLOAT },
  9411. { name: 'position', type: types.VELATLONG }
  9412. ]
  9413. });
  9414. </code></pre>
  9415. * @singleton
  9416. */
  9417. Ext.define('Ext.data.Types', {
  9418. singleton: true,
  9419. requires: ['Ext.data.SortTypes']
  9420. }, function() {
  9421. var st = Ext.data.SortTypes;
  9422. Ext.apply(Ext.data.Types, {
  9423. /**
  9424. * @property {RegExp} stripRe
  9425. * A regular expression for stripping non-numeric characters from a numeric value. Defaults to <tt>/[\$,%]/g</tt>.
  9426. * This should be overridden for localization.
  9427. */
  9428. stripRe: /[\$,%]/g,
  9429. /**
  9430. * @property {Object} AUTO
  9431. * This data type means that no conversion is applied to the raw data before it is placed into a Record.
  9432. */
  9433. AUTO: {
  9434. sortType: st.none,
  9435. type: 'auto'
  9436. },
  9437. /**
  9438. * @property {Object} STRING
  9439. * This data type means that the raw data is converted into a String before it is placed into a Record.
  9440. */
  9441. STRING: {
  9442. convert: function(v) {
  9443. var defaultValue = this.useNull ? null : '';
  9444. return (v === undefined || v === null) ? defaultValue : String(v);
  9445. },
  9446. sortType: st.asUCString,
  9447. type: 'string'
  9448. },
  9449. /**
  9450. * @property {Object} INT
  9451. * This data type means that the raw data is converted into an integer before it is placed into a Record.
  9452. * <p>The synonym <code>INTEGER</code> is equivalent.</p>
  9453. */
  9454. INT: {
  9455. convert: function(v) {
  9456. return v !== undefined && v !== null && v !== '' ?
  9457. parseInt(String(v).replace(Ext.data.Types.stripRe, ''), 10) : (this.useNull ? null : 0);
  9458. },
  9459. sortType: st.none,
  9460. type: 'int'
  9461. },
  9462. /**
  9463. * @property {Object} FLOAT
  9464. * This data type means that the raw data is converted into a number before it is placed into a Record.
  9465. * <p>The synonym <code>NUMBER</code> is equivalent.</p>
  9466. */
  9467. FLOAT: {
  9468. convert: function(v) {
  9469. return v !== undefined && v !== null && v !== '' ?
  9470. parseFloat(String(v).replace(Ext.data.Types.stripRe, ''), 10) : (this.useNull ? null : 0);
  9471. },
  9472. sortType: st.none,
  9473. type: 'float'
  9474. },
  9475. /**
  9476. * @property {Object} BOOL
  9477. * <p>This data type means that the raw data is converted into a boolean before it is placed into
  9478. * a Record. The string "true" and the number 1 are converted to boolean <code>true</code>.</p>
  9479. * <p>The synonym <code>BOOLEAN</code> is equivalent.</p>
  9480. */
  9481. BOOL: {
  9482. convert: function(v) {
  9483. if (this.useNull && (v === undefined || v === null || v === '')) {
  9484. return null;
  9485. }
  9486. return v === true || v === 'true' || v == 1;
  9487. },
  9488. sortType: st.none,
  9489. type: 'bool'
  9490. },
  9491. /**
  9492. * @property {Object} DATE
  9493. * This data type means that the raw data is converted into a Date before it is placed into a Record.
  9494. * The date format is specified in the constructor of the {@link Ext.data.Field} to which this type is
  9495. * being applied.
  9496. */
  9497. DATE: {
  9498. convert: function(v) {
  9499. var df = this.dateFormat,
  9500. parsed;
  9501. if (!v) {
  9502. return null;
  9503. }
  9504. if (Ext.isDate(v)) {
  9505. return v;
  9506. }
  9507. if (df) {
  9508. if (df == 'timestamp') {
  9509. return new Date(v*1000);
  9510. }
  9511. if (df == 'time') {
  9512. return new Date(parseInt(v, 10));
  9513. }
  9514. return Ext.Date.parse(v, df);
  9515. }
  9516. parsed = Date.parse(v);
  9517. return parsed ? new Date(parsed) : null;
  9518. },
  9519. sortType: st.asDate,
  9520. type: 'date'
  9521. }
  9522. });
  9523. Ext.apply(Ext.data.Types, {
  9524. /**
  9525. * @property {Object} BOOLEAN
  9526. * <p>This data type means that the raw data is converted into a boolean before it is placed into
  9527. * a Record. The string "true" and the number 1 are converted to boolean <code>true</code>.</p>
  9528. * <p>The synonym <code>BOOL</code> is equivalent.</p>
  9529. */
  9530. BOOLEAN: this.BOOL,
  9531. /**
  9532. * @property {Object} INTEGER
  9533. * This data type means that the raw data is converted into an integer before it is placed into a Record.
  9534. * <p>The synonym <code>INT</code> is equivalent.</p>
  9535. */
  9536. INTEGER: this.INT,
  9537. /**
  9538. * @property {Object} NUMBER
  9539. * This data type means that the raw data is converted into a number before it is placed into a Record.
  9540. * <p>The synonym <code>FLOAT</code> is equivalent.</p>
  9541. */
  9542. NUMBER: this.FLOAT
  9543. });
  9544. });
  9545. /**
  9546. * @author Ed Spencer
  9547. *
  9548. * Fields are used to define what a Model is. They aren't instantiated directly - instead, when we create a class that
  9549. * extends {@link Ext.data.Model}, it will automatically create a Field instance for each field configured in a {@link
  9550. * Ext.data.Model Model}. For example, we might set up a model like this:
  9551. *
  9552. * Ext.define('User', {
  9553. * extend: 'Ext.data.Model',
  9554. * fields: [
  9555. * 'name', 'email',
  9556. * {name: 'age', type: 'int'},
  9557. * {name: 'gender', type: 'string', defaultValue: 'Unknown'}
  9558. * ]
  9559. * });
  9560. *
  9561. * Four fields will have been created for the User Model - name, email, age and gender. Note that we specified a couple
  9562. * of different formats here; if we only pass in the string name of the field (as with name and email), the field is set
  9563. * up with the 'auto' type. It's as if we'd done this instead:
  9564. *
  9565. * Ext.define('User', {
  9566. * extend: 'Ext.data.Model',
  9567. * fields: [
  9568. * {name: 'name', type: 'auto'},
  9569. * {name: 'email', type: 'auto'},
  9570. * {name: 'age', type: 'int'},
  9571. * {name: 'gender', type: 'string', defaultValue: 'Unknown'}
  9572. * ]
  9573. * });
  9574. *
  9575. * # Types and conversion
  9576. *
  9577. * The {@link #type} is important - it's used to automatically convert data passed to the field into the correct format.
  9578. * In our example above, the name and email fields used the 'auto' type and will just accept anything that is passed
  9579. * into them. The 'age' field had an 'int' type however, so if we passed 25.4 this would be rounded to 25.
  9580. *
  9581. * Sometimes a simple type isn't enough, or we want to perform some processing when we load a Field's data. We can do
  9582. * this using a {@link #convert} function. Here, we're going to create a new field based on another:
  9583. *
  9584. * Ext.define('User', {
  9585. * extend: 'Ext.data.Model',
  9586. * fields: [
  9587. * 'name', 'email',
  9588. * {name: 'age', type: 'int'},
  9589. * {name: 'gender', type: 'string', defaultValue: 'Unknown'},
  9590. *
  9591. * {
  9592. * name: 'firstName',
  9593. * convert: function(value, record) {
  9594. * var fullName = record.get('name'),
  9595. * splits = fullName.split(" "),
  9596. * firstName = splits[0];
  9597. *
  9598. * return firstName;
  9599. * }
  9600. * }
  9601. * ]
  9602. * });
  9603. *
  9604. * Now when we create a new User, the firstName is populated automatically based on the name:
  9605. *
  9606. * var ed = Ext.create('User', {name: 'Ed Spencer'});
  9607. *
  9608. * console.log(ed.get('firstName')); //logs 'Ed', based on our convert function
  9609. *
  9610. * In fact, if we log out all of the data inside ed, we'll see this:
  9611. *
  9612. * console.log(ed.data);
  9613. *
  9614. * //outputs this:
  9615. * {
  9616. * age: 0,
  9617. * email: "",
  9618. * firstName: "Ed",
  9619. * gender: "Unknown",
  9620. * name: "Ed Spencer"
  9621. * }
  9622. *
  9623. * The age field has been given a default of zero because we made it an int type. As an auto field, email has defaulted
  9624. * to an empty string. When we registered the User model we set gender's {@link #defaultValue} to 'Unknown' so we see
  9625. * that now. Let's correct that and satisfy ourselves that the types work as we expect:
  9626. *
  9627. * ed.set('gender', 'Male');
  9628. * ed.get('gender'); //returns 'Male'
  9629. *
  9630. * ed.set('age', 25.4);
  9631. * ed.get('age'); //returns 25 - we wanted an int, not a float, so no decimal places allowed
  9632. */
  9633. Ext.define('Ext.data.Field', {
  9634. requires: ['Ext.data.Types', 'Ext.data.SortTypes'],
  9635. alias: 'data.field',
  9636. constructor : function(config) {
  9637. if (Ext.isString(config)) {
  9638. config = {name: config};
  9639. }
  9640. Ext.apply(this, config);
  9641. var types = Ext.data.Types,
  9642. st = this.sortType,
  9643. t;
  9644. if (this.type) {
  9645. if (Ext.isString(this.type)) {
  9646. this.type = types[this.type.toUpperCase()] || types.AUTO;
  9647. }
  9648. } else {
  9649. this.type = types.AUTO;
  9650. }
  9651. // named sortTypes are supported, here we look them up
  9652. if (Ext.isString(st)) {
  9653. this.sortType = Ext.data.SortTypes[st];
  9654. } else if(Ext.isEmpty(st)) {
  9655. this.sortType = this.type.sortType;
  9656. }
  9657. if (!this.convert) {
  9658. this.convert = this.type.convert;
  9659. }
  9660. },
  9661. /**
  9662. * @cfg {String} name
  9663. *
  9664. * The name by which the field is referenced within the Model. This is referenced by, for example, the `dataIndex`
  9665. * property in column definition objects passed to {@link Ext.grid.property.HeaderContainer}.
  9666. *
  9667. * Note: In the simplest case, if no properties other than `name` are required, a field definition may consist of
  9668. * just a String for the field name.
  9669. */
  9670. /**
  9671. * @cfg {String/Object} type
  9672. *
  9673. * The data type for automatic conversion from received data to the *stored* value if
  9674. * `{@link Ext.data.Field#convert convert}` has not been specified. This may be specified as a string value.
  9675. * Possible values are
  9676. *
  9677. * - auto (Default, implies no conversion)
  9678. * - string
  9679. * - int
  9680. * - float
  9681. * - boolean
  9682. * - date
  9683. *
  9684. * This may also be specified by referencing a member of the {@link Ext.data.Types} class.
  9685. *
  9686. * Developers may create their own application-specific data types by defining new members of the {@link
  9687. * Ext.data.Types} class.
  9688. */
  9689. /**
  9690. * @cfg {Function} convert
  9691. *
  9692. * A function which converts the value provided by the Reader into an object that will be stored in the Model.
  9693. * It is passed the following parameters:
  9694. *
  9695. * - **v** : Mixed
  9696. *
  9697. * The data value as read by the Reader, if undefined will use the configured `{@link Ext.data.Field#defaultValue
  9698. * defaultValue}`.
  9699. *
  9700. * - **rec** : Ext.data.Model
  9701. *
  9702. * The data object containing the Model as read so far by the Reader. Note that the Model may not be fully populated
  9703. * at this point as the fields are read in the order that they are defined in your
  9704. * {@link Ext.data.Model#cfg-fields fields} array.
  9705. *
  9706. * Example of convert functions:
  9707. *
  9708. * function fullName(v, record){
  9709. * return record.name.last + ', ' + record.name.first;
  9710. * }
  9711. *
  9712. * function location(v, record){
  9713. * return !record.city ? '' : (record.city + ', ' + record.state);
  9714. * }
  9715. *
  9716. * Ext.define('Dude', {
  9717. * extend: 'Ext.data.Model',
  9718. * fields: [
  9719. * {name: 'fullname', convert: fullName},
  9720. * {name: 'firstname', mapping: 'name.first'},
  9721. * {name: 'lastname', mapping: 'name.last'},
  9722. * {name: 'city', defaultValue: 'homeless'},
  9723. * 'state',
  9724. * {name: 'location', convert: location}
  9725. * ]
  9726. * });
  9727. *
  9728. * // create the data store
  9729. * var store = Ext.create('Ext.data.Store', {
  9730. * reader: {
  9731. * type: 'json',
  9732. * model: 'Dude',
  9733. * idProperty: 'key',
  9734. * root: 'daRoot',
  9735. * totalProperty: 'total'
  9736. * }
  9737. * });
  9738. *
  9739. * var myData = [
  9740. * { key: 1,
  9741. * name: { first: 'Fat', last: 'Albert' }
  9742. * // notice no city, state provided in data object
  9743. * },
  9744. * { key: 2,
  9745. * name: { first: 'Barney', last: 'Rubble' },
  9746. * city: 'Bedrock', state: 'Stoneridge'
  9747. * },
  9748. * { key: 3,
  9749. * name: { first: 'Cliff', last: 'Claven' },
  9750. * city: 'Boston', state: 'MA'
  9751. * }
  9752. * ];
  9753. */
  9754. /**
  9755. * @cfg {String} dateFormat
  9756. *
  9757. * Used when converting received data into a Date when the {@link #type} is specified as `"date"`.
  9758. *
  9759. * A format string for the {@link Ext.Date#parse Ext.Date.parse} function, or "timestamp" if the value provided by
  9760. * the Reader is a UNIX timestamp, or "time" if the value provided by the Reader is a javascript millisecond
  9761. * timestamp. See {@link Ext.Date}.
  9762. */
  9763. dateFormat: null,
  9764. /**
  9765. * @cfg {Boolean} useNull
  9766. *
  9767. * Use when converting received data into a Number type (either int or float). If the value cannot be
  9768. * parsed, null will be used if useNull is true, otherwise the value will be 0. Defaults to false.
  9769. */
  9770. useNull: false,
  9771. /**
  9772. * @cfg {Object} defaultValue
  9773. *
  9774. * The default value used **when a Model is being created by a {@link Ext.data.reader.Reader Reader}**
  9775. * when the item referenced by the `{@link Ext.data.Field#mapping mapping}` does not exist in the data object
  9776. * (i.e. undefined). Defaults to "".
  9777. */
  9778. defaultValue: "",
  9779. /**
  9780. * @cfg {String/Number} mapping
  9781. *
  9782. * (Optional) A path expression for use by the {@link Ext.data.reader.Reader} implementation that is creating the
  9783. * {@link Ext.data.Model Model} to extract the Field value from the data object. If the path expression is the same
  9784. * as the field name, the mapping may be omitted.
  9785. *
  9786. * The form of the mapping expression depends on the Reader being used.
  9787. *
  9788. * - {@link Ext.data.reader.Json}
  9789. *
  9790. * The mapping is a string containing the javascript expression to reference the data from an element of the data
  9791. * item's {@link Ext.data.reader.Json#root root} Array. Defaults to the field name.
  9792. *
  9793. * - {@link Ext.data.reader.Xml}
  9794. *
  9795. * The mapping is an {@link Ext.DomQuery} path to the data item relative to the DOM element that represents the
  9796. * {@link Ext.data.reader.Xml#record record}. Defaults to the field name.
  9797. *
  9798. * - {@link Ext.data.reader.Array}
  9799. *
  9800. * The mapping is a number indicating the Array index of the field's value. Defaults to the field specification's
  9801. * Array position.
  9802. *
  9803. * If a more complex value extraction strategy is required, then configure the Field with a {@link #convert}
  9804. * function. This is passed the whole row object, and may interrogate it in whatever way is necessary in order to
  9805. * return the desired data.
  9806. */
  9807. mapping: null,
  9808. /**
  9809. * @cfg {Function} sortType
  9810. *
  9811. * A function which converts a Field's value to a comparable value in order to ensure correct sort ordering.
  9812. * Predefined functions are provided in {@link Ext.data.SortTypes}. A custom sort example:
  9813. *
  9814. * // current sort after sort we want
  9815. * // +-+------+ +-+------+
  9816. * // |1|First | |1|First |
  9817. * // |2|Last | |3|Second|
  9818. * // |3|Second| |2|Last |
  9819. * // +-+------+ +-+------+
  9820. *
  9821. * sortType: function(value) {
  9822. * switch (value.toLowerCase()) // native toLowerCase():
  9823. * {
  9824. * case 'first': return 1;
  9825. * case 'second': return 2;
  9826. * default: return 3;
  9827. * }
  9828. * }
  9829. */
  9830. sortType : null,
  9831. /**
  9832. * @cfg {String} sortDir
  9833. *
  9834. * Initial direction to sort (`"ASC"` or `"DESC"`). Defaults to `"ASC"`.
  9835. */
  9836. sortDir : "ASC",
  9837. /**
  9838. * @cfg {Boolean} allowBlank
  9839. * @private
  9840. *
  9841. * Used for validating a {@link Ext.data.Model model}. Defaults to true. An empty value here will cause
  9842. * {@link Ext.data.Model}.{@link Ext.data.Model#isValid isValid} to evaluate to false.
  9843. */
  9844. allowBlank : true,
  9845. /**
  9846. * @cfg {Boolean} persist
  9847. *
  9848. * False to exclude this field from the {@link Ext.data.Model#modified} fields in a model. This will also exclude
  9849. * the field from being written using a {@link Ext.data.writer.Writer}. This option is useful when model fields are
  9850. * used to keep state on the client but do not need to be persisted to the server. Defaults to true.
  9851. */
  9852. persist: true
  9853. });
  9854. /**
  9855. * @class Ext.Ajax
  9856. * @singleton
  9857. * @markdown
  9858. A singleton instance of an {@link Ext.data.Connection}. This class
  9859. is used to communicate with your server side code. It can be used as follows:
  9860. Ext.Ajax.request({
  9861. url: 'page.php',
  9862. params: {
  9863. id: 1
  9864. },
  9865. success: function(response){
  9866. var text = response.responseText;
  9867. // process server response here
  9868. }
  9869. });
  9870. Default options for all requests can be set by changing a property on the Ext.Ajax class:
  9871. Ext.Ajax.timeout = 60000; // 60 seconds
  9872. Any options specified in the request method for the Ajax request will override any
  9873. defaults set on the Ext.Ajax class. In the code sample below, the timeout for the
  9874. request will be 60 seconds.
  9875. Ext.Ajax.timeout = 120000; // 120 seconds
  9876. Ext.Ajax.request({
  9877. url: 'page.aspx',
  9878. timeout: 60000
  9879. });
  9880. In general, this class will be used for all Ajax requests in your application.
  9881. The main reason for creating a separate {@link Ext.data.Connection} is for a
  9882. series of requests that share common settings that are different to all other
  9883. requests in the application.
  9884. */
  9885. Ext.define('Ext.Ajax', {
  9886. extend: 'Ext.data.Connection',
  9887. singleton: true,
  9888. /**
  9889. * @cfg {Object} extraParams @hide
  9890. */
  9891. /**
  9892. * @cfg {Object} defaultHeaders @hide
  9893. */
  9894. /**
  9895. * @cfg {String} method (Optional) @hide
  9896. */
  9897. /**
  9898. * @cfg {Number} timeout (Optional) @hide
  9899. */
  9900. /**
  9901. * @cfg {Boolean} autoAbort (Optional) @hide
  9902. */
  9903. /**
  9904. * @cfg {Boolean} disableCaching (Optional) @hide
  9905. */
  9906. /**
  9907. * @property {Boolean} disableCaching
  9908. * True to add a unique cache-buster param to GET requests. Defaults to true.
  9909. */
  9910. /**
  9911. * @property {String} url
  9912. * The default URL to be used for requests to the server.
  9913. * If the server receives all requests through one URL, setting this once is easier than
  9914. * entering it on every request.
  9915. */
  9916. /**
  9917. * @property {Object} extraParams
  9918. * An object containing properties which are used as extra parameters to each request made
  9919. * by this object. Session information and other data that you need
  9920. * to pass with each request are commonly put here.
  9921. */
  9922. /**
  9923. * @property {Object} defaultHeaders
  9924. * An object containing request headers which are added to each request made by this object.
  9925. */
  9926. /**
  9927. * @property {String} method
  9928. * The default HTTP method to be used for requests. Note that this is case-sensitive and
  9929. * should be all caps (if not set but params are present will use
  9930. * <tt>"POST"</tt>, otherwise will use <tt>"GET"</tt>.)
  9931. */
  9932. /**
  9933. * @property {Number} timeout
  9934. * The timeout in milliseconds to be used for requests. Defaults to 30000.
  9935. */
  9936. /**
  9937. * @property {Boolean} autoAbort
  9938. * Whether a new request should abort any pending requests.
  9939. */
  9940. autoAbort : false
  9941. });
  9942. /**
  9943. * @class Ext.util.AbstractMixedCollection
  9944. * @private
  9945. */
  9946. Ext.define('Ext.util.AbstractMixedCollection', {
  9947. requires: ['Ext.util.Filter'],
  9948. mixins: {
  9949. observable: 'Ext.util.Observable'
  9950. },
  9951. /**
  9952. * @private Mutation counter which is incremented upon add and remove.
  9953. */
  9954. generation: 0,
  9955. constructor: function(allowFunctions, keyFn) {
  9956. var me = this;
  9957. me.items = [];
  9958. me.map = {};
  9959. me.keys = [];
  9960. me.length = 0;
  9961. /**
  9962. * @event clear
  9963. * Fires when the collection is cleared.
  9964. */
  9965. /**
  9966. * @event add
  9967. * Fires when an item is added to the collection.
  9968. * @param {Number} index The index at which the item was added.
  9969. * @param {Object} o The item added.
  9970. * @param {String} key The key associated with the added item.
  9971. */
  9972. /**
  9973. * @event replace
  9974. * Fires when an item is replaced in the collection.
  9975. * @param {String} key he key associated with the new added.
  9976. * @param {Object} old The item being replaced.
  9977. * @param {Object} new The new item.
  9978. */
  9979. /**
  9980. * @event remove
  9981. * Fires when an item is removed from the collection.
  9982. * @param {Object} o The item being removed.
  9983. * @param {String} key (optional) The key associated with the removed item.
  9984. */
  9985. me.allowFunctions = allowFunctions === true;
  9986. if (keyFn) {
  9987. me.getKey = keyFn;
  9988. }
  9989. me.mixins.observable.constructor.call(me);
  9990. },
  9991. /**
  9992. * @cfg {Boolean} allowFunctions Specify <tt>true</tt> if the {@link #addAll}
  9993. * function should add function references to the collection. Defaults to
  9994. * <tt>false</tt>.
  9995. */
  9996. allowFunctions : false,
  9997. /**
  9998. * Adds an item to the collection. Fires the {@link #event-add} event when complete.
  9999. * @param {String} key <p>The key to associate with the item, or the new item.</p>
  10000. * <p>If a {@link #getKey} implementation was specified for this MixedCollection,
  10001. * or if the key of the stored items is in a property called <tt><b>id</b></tt>,
  10002. * the MixedCollection will be able to <i>derive</i> the key for the new item.
  10003. * In this case just pass the new item in this parameter.</p>
  10004. * @param {Object} o The item to add.
  10005. * @return {Object} The item added.
  10006. */
  10007. add : function(key, obj){
  10008. var me = this,
  10009. myObj = obj,
  10010. myKey = key,
  10011. old;
  10012. if (arguments.length == 1) {
  10013. myObj = myKey;
  10014. myKey = me.getKey(myObj);
  10015. }
  10016. if (typeof myKey != 'undefined' && myKey !== null) {
  10017. old = me.map[myKey];
  10018. if (typeof old != 'undefined') {
  10019. return me.replace(myKey, myObj);
  10020. }
  10021. me.map[myKey] = myObj;
  10022. }
  10023. me.generation++;
  10024. me.length++;
  10025. me.items.push(myObj);
  10026. me.keys.push(myKey);
  10027. if (me.hasListeners.add) {
  10028. me.fireEvent('add', me.length - 1, myObj, myKey);
  10029. }
  10030. return myObj;
  10031. },
  10032. /**
  10033. * MixedCollection has a generic way to fetch keys if you implement getKey. The default implementation
  10034. * simply returns <b><code>item.id</code></b> but you can provide your own implementation
  10035. * to return a different value as in the following examples:<pre><code>
  10036. // normal way
  10037. var mc = new Ext.util.MixedCollection();
  10038. mc.add(someEl.dom.id, someEl);
  10039. mc.add(otherEl.dom.id, otherEl);
  10040. //and so on
  10041. // using getKey
  10042. var mc = new Ext.util.MixedCollection();
  10043. mc.getKey = function(el){
  10044. return el.dom.id;
  10045. };
  10046. mc.add(someEl);
  10047. mc.add(otherEl);
  10048. // or via the constructor
  10049. var mc = new Ext.util.MixedCollection(false, function(el){
  10050. return el.dom.id;
  10051. });
  10052. mc.add(someEl);
  10053. mc.add(otherEl);
  10054. * </code></pre>
  10055. * @param {Object} item The item for which to find the key.
  10056. * @return {Object} The key for the passed item.
  10057. */
  10058. getKey : function(o){
  10059. return o.id;
  10060. },
  10061. /**
  10062. * Replaces an item in the collection. Fires the {@link #event-replace} event when complete.
  10063. * @param {String} key <p>The key associated with the item to replace, or the replacement item.</p>
  10064. * <p>If you supplied a {@link #getKey} implementation for this MixedCollection, or if the key
  10065. * of your stored items is in a property called <tt><b>id</b></tt>, then the MixedCollection
  10066. * will be able to <i>derive</i> the key of the replacement item. If you want to replace an item
  10067. * with one having the same key value, then just pass the replacement item in this parameter.</p>
  10068. * @param o {Object} o (optional) If the first parameter passed was a key, the item to associate
  10069. * with that key.
  10070. * @return {Object} The new item.
  10071. */
  10072. replace : function(key, o){
  10073. var me = this,
  10074. old,
  10075. index;
  10076. if (arguments.length == 1) {
  10077. o = arguments[0];
  10078. key = me.getKey(o);
  10079. }
  10080. old = me.map[key];
  10081. if (typeof key == 'undefined' || key === null || typeof old == 'undefined') {
  10082. return me.add(key, o);
  10083. }
  10084. me.generation++;
  10085. index = me.indexOfKey(key);
  10086. me.items[index] = o;
  10087. me.map[key] = o;
  10088. if (me.hasListeners.replace) {
  10089. me.fireEvent('replace', key, old, o);
  10090. }
  10091. return o;
  10092. },
  10093. /**
  10094. * Adds all elements of an Array or an Object to the collection.
  10095. * @param {Object/Array} objs An Object containing properties which will be added
  10096. * to the collection, or an Array of values, each of which are added to the collection.
  10097. * Functions references will be added to the collection if <code>{@link #allowFunctions}</code>
  10098. * has been set to <tt>true</tt>.
  10099. */
  10100. addAll : function(objs){
  10101. var me = this,
  10102. i = 0,
  10103. args,
  10104. len,
  10105. key;
  10106. if (arguments.length > 1 || Ext.isArray(objs)) {
  10107. args = arguments.length > 1 ? arguments : objs;
  10108. for (len = args.length; i < len; i++) {
  10109. me.add(args[i]);
  10110. }
  10111. } else {
  10112. for (key in objs) {
  10113. if (objs.hasOwnProperty(key)) {
  10114. if (me.allowFunctions || typeof objs[key] != 'function') {
  10115. me.add(key, objs[key]);
  10116. }
  10117. }
  10118. }
  10119. }
  10120. },
  10121. /**
  10122. * Executes the specified function once for every item in the collection, passing the following arguments:
  10123. * <div class="mdetail-params"><ul>
  10124. * <li><b>item</b> : Mixed<p class="sub-desc">The collection item</p></li>
  10125. * <li><b>index</b> : Number<p class="sub-desc">The item's index</p></li>
  10126. * <li><b>length</b> : Number<p class="sub-desc">The total number of items in the collection</p></li>
  10127. * </ul></div>
  10128. * The function should return a boolean value. Returning false from the function will stop the iteration.
  10129. * @param {Function} fn The function to execute for each item.
  10130. * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the current item in the iteration.
  10131. */
  10132. each : function(fn, scope){
  10133. var items = [].concat(this.items), // each safe for removal
  10134. i = 0,
  10135. len = items.length,
  10136. item;
  10137. for (; i < len; i++) {
  10138. item = items[i];
  10139. if (fn.call(scope || item, item, i, len) === false) {
  10140. break;
  10141. }
  10142. }
  10143. },
  10144. /**
  10145. * Executes the specified function once for every key in the collection, passing each
  10146. * key, and its associated item as the first two parameters.
  10147. * @param {Function} fn The function to execute for each item.
  10148. * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.
  10149. */
  10150. eachKey : function(fn, scope){
  10151. var keys = this.keys,
  10152. items = this.items,
  10153. i = 0,
  10154. len = keys.length;
  10155. for (; i < len; i++) {
  10156. fn.call(scope || window, keys[i], items[i], i, len);
  10157. }
  10158. },
  10159. /**
  10160. * Returns the first item in the collection which elicits a true return value from the
  10161. * passed selection function.
  10162. * @param {Function} fn The selection function to execute for each item.
  10163. * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.
  10164. * @return {Object} The first item in the collection which returned true from the selection function, or null if none was found
  10165. */
  10166. findBy : function(fn, scope) {
  10167. var keys = this.keys,
  10168. items = this.items,
  10169. i = 0,
  10170. len = items.length;
  10171. for (; i < len; i++) {
  10172. if (fn.call(scope || window, items[i], keys[i])) {
  10173. return items[i];
  10174. }
  10175. }
  10176. return null;
  10177. },
  10178. find : function() {
  10179. if (Ext.isDefined(Ext.global.console)) {
  10180. Ext.global.console.warn('Ext.util.MixedCollection: find has been deprecated. Use findBy instead.');
  10181. }
  10182. return this.findBy.apply(this, arguments);
  10183. },
  10184. /**
  10185. * Inserts an item at the specified index in the collection. Fires the {@link #event-add} event when complete.
  10186. * @param {Number} index The index to insert the item at.
  10187. * @param {String} key The key to associate with the new item, or the item itself.
  10188. * @param {Object} o (optional) If the second parameter was a key, the new item.
  10189. * @return {Object} The item inserted.
  10190. */
  10191. insert : function(index, key, obj){
  10192. var me = this,
  10193. myKey = key,
  10194. myObj = obj;
  10195. if (arguments.length == 2) {
  10196. myObj = myKey;
  10197. myKey = me.getKey(myObj);
  10198. }
  10199. if (me.containsKey(myKey)) {
  10200. me.suspendEvents();
  10201. me.removeAtKey(myKey);
  10202. me.resumeEvents();
  10203. }
  10204. if (index >= me.length) {
  10205. return me.add(myKey, myObj);
  10206. }
  10207. me.generation++;
  10208. me.length++;
  10209. Ext.Array.splice(me.items, index, 0, myObj);
  10210. if (typeof myKey != 'undefined' && myKey !== null) {
  10211. me.map[myKey] = myObj;
  10212. }
  10213. Ext.Array.splice(me.keys, index, 0, myKey);
  10214. if (me.hasListeners.add) {
  10215. me.fireEvent('add', index, myObj, myKey);
  10216. }
  10217. return myObj;
  10218. },
  10219. /**
  10220. * Remove an item from the collection.
  10221. * @param {Object} o The item to remove.
  10222. * @return {Object} The item removed or false if no item was removed.
  10223. */
  10224. remove : function(o) {
  10225. this.generation++;
  10226. return this.removeAt(this.indexOf(o));
  10227. },
  10228. /**
  10229. * Remove all items in the passed array from the collection.
  10230. * @param {Array} items An array of items to be removed.
  10231. * @return {Ext.util.MixedCollection} this object
  10232. */
  10233. removeAll : function(items) {
  10234. items = [].concat(items);
  10235. var i, iLen = items.length;
  10236. for (i = 0; i < iLen; i++) {
  10237. this.remove(items[i]);
  10238. }
  10239. return this;
  10240. },
  10241. /**
  10242. * Remove an item from a specified index in the collection. Fires the {@link #event-remove} event when complete.
  10243. * @param {Number} index The index within the collection of the item to remove.
  10244. * @return {Object} The item removed or false if no item was removed.
  10245. */
  10246. removeAt : function(index) {
  10247. var me = this,
  10248. o,
  10249. key;
  10250. if (index < me.length && index >= 0) {
  10251. me.length--;
  10252. o = me.items[index];
  10253. Ext.Array.erase(me.items, index, 1);
  10254. key = me.keys[index];
  10255. if (typeof key != 'undefined') {
  10256. delete me.map[key];
  10257. }
  10258. Ext.Array.erase(me.keys, index, 1);
  10259. if (me.hasListeners.remove) {
  10260. me.fireEvent('remove', o, key);
  10261. }
  10262. me.generation++;
  10263. return o;
  10264. }
  10265. return false;
  10266. },
  10267. /**
  10268. * Removed an item associated with the passed key fom the collection.
  10269. * @param {String} key The key of the item to remove.
  10270. * @return {Object} The item removed or false if no item was removed.
  10271. */
  10272. removeAtKey : function(key){
  10273. return this.removeAt(this.indexOfKey(key));
  10274. },
  10275. /**
  10276. * Returns the number of items in the collection.
  10277. * @return {Number} the number of items in the collection.
  10278. */
  10279. getCount : function(){
  10280. return this.length;
  10281. },
  10282. /**
  10283. * Returns index within the collection of the passed Object.
  10284. * @param {Object} o The item to find the index of.
  10285. * @return {Number} index of the item. Returns -1 if not found.
  10286. */
  10287. indexOf : function(o){
  10288. return Ext.Array.indexOf(this.items, o);
  10289. },
  10290. /**
  10291. * Returns index within the collection of the passed key.
  10292. * @param {String} key The key to find the index of.
  10293. * @return {Number} index of the key.
  10294. */
  10295. indexOfKey : function(key){
  10296. return Ext.Array.indexOf(this.keys, key);
  10297. },
  10298. /**
  10299. * Returns the item associated with the passed key OR index.
  10300. * Key has priority over index. This is the equivalent
  10301. * of calling {@link #getByKey} first, then if nothing matched calling {@link #getAt}.
  10302. * @param {String/Number} key The key or index of the item.
  10303. * @return {Object} If the item is found, returns the item. If the item was not found, returns <tt>undefined</tt>.
  10304. * If an item was found, but is a Class, returns <tt>null</tt>.
  10305. */
  10306. get : function(key) {
  10307. var me = this,
  10308. mk = me.map[key],
  10309. item = mk !== undefined ? mk : (typeof key == 'number') ? me.items[key] : undefined;
  10310. return typeof item != 'function' || me.allowFunctions ? item : null; // for prototype!
  10311. },
  10312. /**
  10313. * Returns the item at the specified index.
  10314. * @param {Number} index The index of the item.
  10315. * @return {Object} The item at the specified index.
  10316. */
  10317. getAt : function(index) {
  10318. return this.items[index];
  10319. },
  10320. /**
  10321. * Returns the item associated with the passed key.
  10322. * @param {String/Number} key The key of the item.
  10323. * @return {Object} The item associated with the passed key.
  10324. */
  10325. getByKey : function(key) {
  10326. return this.map[key];
  10327. },
  10328. /**
  10329. * Returns true if the collection contains the passed Object as an item.
  10330. * @param {Object} o The Object to look for in the collection.
  10331. * @return {Boolean} True if the collection contains the Object as an item.
  10332. */
  10333. contains : function(o){
  10334. return typeof this.map[this.getKey(o)] != 'undefined';
  10335. },
  10336. /**
  10337. * Returns true if the collection contains the passed Object as a key.
  10338. * @param {String} key The key to look for in the collection.
  10339. * @return {Boolean} True if the collection contains the Object as a key.
  10340. */
  10341. containsKey : function(key){
  10342. return typeof this.map[key] != 'undefined';
  10343. },
  10344. /**
  10345. * Removes all items from the collection. Fires the {@link #event-clear} event when complete.
  10346. */
  10347. clear : function(){
  10348. var me = this;
  10349. me.length = 0;
  10350. me.items = [];
  10351. me.keys = [];
  10352. me.map = {};
  10353. me.generation++;
  10354. if (me.hasListeners.clear) {
  10355. me.fireEvent('clear');
  10356. }
  10357. },
  10358. /**
  10359. * Returns the first item in the collection.
  10360. * @return {Object} the first item in the collection..
  10361. */
  10362. first : function() {
  10363. return this.items[0];
  10364. },
  10365. /**
  10366. * Returns the last item in the collection.
  10367. * @return {Object} the last item in the collection..
  10368. */
  10369. last : function() {
  10370. return this.items[this.length - 1];
  10371. },
  10372. /**
  10373. * Collects all of the values of the given property and returns their sum
  10374. * @param {String} property The property to sum by
  10375. * @param {String} [root] 'root' property to extract the first argument from. This is used mainly when
  10376. * summing fields in records, where the fields are all stored inside the 'data' object
  10377. * @param {Number} [start=0] The record index to start at
  10378. * @param {Number} [end=-1] The record index to end at
  10379. * @return {Number} The total
  10380. */
  10381. sum: function(property, root, start, end) {
  10382. var values = this.extractValues(property, root),
  10383. length = values.length,
  10384. sum = 0,
  10385. i;
  10386. start = start || 0;
  10387. end = (end || end === 0) ? end : length - 1;
  10388. for (i = start; i <= end; i++) {
  10389. sum += values[i];
  10390. }
  10391. return sum;
  10392. },
  10393. /**
  10394. * Collects unique values of a particular property in this MixedCollection
  10395. * @param {String} property The property to collect on
  10396. * @param {String} root (optional) 'root' property to extract the first argument from. This is used mainly when
  10397. * summing fields in records, where the fields are all stored inside the 'data' object
  10398. * @param {Boolean} allowBlank (optional) Pass true to allow null, undefined or empty string values
  10399. * @return {Array} The unique values
  10400. */
  10401. collect: function(property, root, allowNull) {
  10402. var values = this.extractValues(property, root),
  10403. length = values.length,
  10404. hits = {},
  10405. unique = [],
  10406. value, strValue, i;
  10407. for (i = 0; i < length; i++) {
  10408. value = values[i];
  10409. strValue = String(value);
  10410. if ((allowNull || !Ext.isEmpty(value)) && !hits[strValue]) {
  10411. hits[strValue] = true;
  10412. unique.push(value);
  10413. }
  10414. }
  10415. return unique;
  10416. },
  10417. /**
  10418. * @private
  10419. * Extracts all of the given property values from the items in the MC. Mainly used as a supporting method for
  10420. * functions like sum and collect.
  10421. * @param {String} property The property to extract
  10422. * @param {String} root (optional) 'root' property to extract the first argument from. This is used mainly when
  10423. * extracting field data from Model instances, where the fields are stored inside the 'data' object
  10424. * @return {Array} The extracted values
  10425. */
  10426. extractValues: function(property, root) {
  10427. var values = this.items;
  10428. if (root) {
  10429. values = Ext.Array.pluck(values, root);
  10430. }
  10431. return Ext.Array.pluck(values, property);
  10432. },
  10433. /**
  10434. * Returns a range of items in this collection
  10435. * @param {Number} startIndex (optional) The starting index. Defaults to 0.
  10436. * @param {Number} endIndex (optional) The ending index. Defaults to the last item.
  10437. * @return {Array} An array of items
  10438. */
  10439. getRange : function(start, end){
  10440. var me = this,
  10441. items = me.items,
  10442. range = [],
  10443. i;
  10444. if (items.length < 1) {
  10445. return range;
  10446. }
  10447. start = start || 0;
  10448. end = Math.min(typeof end == 'undefined' ? me.length - 1 : end, me.length - 1);
  10449. if (start <= end) {
  10450. for (i = start; i <= end; i++) {
  10451. range[range.length] = items[i];
  10452. }
  10453. } else {
  10454. for (i = start; i >= end; i--) {
  10455. range[range.length] = items[i];
  10456. }
  10457. }
  10458. return range;
  10459. },
  10460. /**
  10461. * <p>Filters the objects in this collection by a set of {@link Ext.util.Filter Filter}s, or by a single
  10462. * property/value pair with optional parameters for substring matching and case sensitivity. See
  10463. * {@link Ext.util.Filter Filter} for an example of using Filter objects (preferred). Alternatively,
  10464. * MixedCollection can be easily filtered by property like this:</p>
  10465. <pre><code>
  10466. //create a simple store with a few people defined
  10467. var people = new Ext.util.MixedCollection();
  10468. people.addAll([
  10469. {id: 1, age: 25, name: 'Ed'},
  10470. {id: 2, age: 24, name: 'Tommy'},
  10471. {id: 3, age: 24, name: 'Arne'},
  10472. {id: 4, age: 26, name: 'Aaron'}
  10473. ]);
  10474. //a new MixedCollection containing only the items where age == 24
  10475. var middleAged = people.filter('age', 24);
  10476. </code></pre>
  10477. *
  10478. *
  10479. * @param {Ext.util.Filter[]/String} property A property on your objects, or an array of {@link Ext.util.Filter Filter} objects
  10480. * @param {String/RegExp} value Either string that the property values
  10481. * should start with or a RegExp to test against the property
  10482. * @param {Boolean} [anyMatch=false] True to match any part of the string, not just the beginning
  10483. * @param {Boolean} [caseSensitive=false] True for case sensitive comparison.
  10484. * @return {Ext.util.MixedCollection} The new filtered collection
  10485. */
  10486. filter : function(property, value, anyMatch, caseSensitive) {
  10487. var filters = [],
  10488. filterFn;
  10489. //support for the simple case of filtering by property/value
  10490. if (Ext.isString(property)) {
  10491. filters.push(new Ext.util.Filter({
  10492. property : property,
  10493. value : value,
  10494. anyMatch : anyMatch,
  10495. caseSensitive: caseSensitive
  10496. }));
  10497. } else if (Ext.isArray(property) || property instanceof Ext.util.Filter) {
  10498. filters = filters.concat(property);
  10499. }
  10500. //at this point we have an array of zero or more Ext.util.Filter objects to filter with,
  10501. //so here we construct a function that combines these filters by ANDing them together
  10502. filterFn = function(record) {
  10503. var isMatch = true,
  10504. length = filters.length,
  10505. i;
  10506. for (i = 0; i < length; i++) {
  10507. var filter = filters[i],
  10508. fn = filter.filterFn,
  10509. scope = filter.scope;
  10510. isMatch = isMatch && fn.call(scope, record);
  10511. }
  10512. return isMatch;
  10513. };
  10514. return this.filterBy(filterFn);
  10515. },
  10516. /**
  10517. * Filter by a function. Returns a <i>new</i> collection that has been filtered.
  10518. * The passed function will be called with each object in the collection.
  10519. * If the function returns true, the value is included otherwise it is filtered.
  10520. * @param {Function} fn The function to be called, it will receive the args o (the object), k (the key)
  10521. * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this MixedCollection.
  10522. * @return {Ext.util.MixedCollection} The new filtered collection
  10523. */
  10524. filterBy : function(fn, scope) {
  10525. var me = this,
  10526. newMC = new this.self(),
  10527. keys = me.keys,
  10528. items = me.items,
  10529. length = items.length,
  10530. i;
  10531. newMC.getKey = me.getKey;
  10532. for (i = 0; i < length; i++) {
  10533. if (fn.call(scope || me, items[i], keys[i])) {
  10534. newMC.add(keys[i], items[i]);
  10535. }
  10536. }
  10537. return newMC;
  10538. },
  10539. /**
  10540. * Finds the index of the first matching object in this collection by a specific property/value.
  10541. * @param {String} property The name of a property on your objects.
  10542. * @param {String/RegExp} value A string that the property values
  10543. * should start with or a RegExp to test against the property.
  10544. * @param {Number} [start=0] The index to start searching at.
  10545. * @param {Boolean} [anyMatch=false] True to match any part of the string, not just the beginning.
  10546. * @param {Boolean} [caseSensitive=false] True for case sensitive comparison.
  10547. * @return {Number} The matched index or -1
  10548. */
  10549. findIndex : function(property, value, start, anyMatch, caseSensitive){
  10550. if(Ext.isEmpty(value, false)){
  10551. return -1;
  10552. }
  10553. value = this.createValueMatcher(value, anyMatch, caseSensitive);
  10554. return this.findIndexBy(function(o){
  10555. return o && value.test(o[property]);
  10556. }, null, start);
  10557. },
  10558. /**
  10559. * Find the index of the first matching object in this collection by a function.
  10560. * If the function returns <i>true</i> it is considered a match.
  10561. * @param {Function} fn The function to be called, it will receive the args o (the object), k (the key).
  10562. * @param {Object} [scope] The scope (<code>this</code> reference) in which the function is executed. Defaults to this MixedCollection.
  10563. * @param {Number} [start=0] The index to start searching at.
  10564. * @return {Number} The matched index or -1
  10565. */
  10566. findIndexBy : function(fn, scope, start){
  10567. var me = this,
  10568. keys = me.keys,
  10569. items = me.items,
  10570. i = start || 0,
  10571. len = items.length;
  10572. for (; i < len; i++) {
  10573. if (fn.call(scope || me, items[i], keys[i])) {
  10574. return i;
  10575. }
  10576. }
  10577. return -1;
  10578. },
  10579. /**
  10580. * Returns a regular expression based on the given value and matching options. This is used internally for finding and filtering,
  10581. * and by Ext.data.Store#filter
  10582. * @private
  10583. * @param {String} value The value to create the regex for. This is escaped using Ext.escapeRe
  10584. * @param {Boolean} anyMatch True to allow any match - no regex start/end line anchors will be added. Defaults to false
  10585. * @param {Boolean} caseSensitive True to make the regex case sensitive (adds 'i' switch to regex). Defaults to false.
  10586. * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false. Ignored if anyMatch is true.
  10587. */
  10588. createValueMatcher : function(value, anyMatch, caseSensitive, exactMatch) {
  10589. if (!value.exec) { // not a regex
  10590. var er = Ext.String.escapeRegex;
  10591. value = String(value);
  10592. if (anyMatch === true) {
  10593. value = er(value);
  10594. } else {
  10595. value = '^' + er(value);
  10596. if (exactMatch === true) {
  10597. value += '$';
  10598. }
  10599. }
  10600. value = new RegExp(value, caseSensitive ? '' : 'i');
  10601. }
  10602. return value;
  10603. },
  10604. /**
  10605. * Creates a shallow copy of this collection
  10606. * @return {Ext.util.MixedCollection}
  10607. */
  10608. clone : function() {
  10609. var me = this,
  10610. copy = new this.self(),
  10611. keys = me.keys,
  10612. items = me.items,
  10613. i = 0,
  10614. len = items.length;
  10615. for(; i < len; i++){
  10616. copy.add(keys[i], items[i]);
  10617. }
  10618. copy.getKey = me.getKey;
  10619. return copy;
  10620. }
  10621. });
  10622. /**
  10623. * @docauthor Tommy Maintz <tommy@sencha.com>
  10624. *
  10625. * A mixin which allows a data component to be sorted. This is used by e.g. {@link Ext.data.Store} and {@link Ext.data.TreeStore}.
  10626. *
  10627. * **NOTE**: This mixin is mainly for internal use and most users should not need to use it directly. It
  10628. * is more likely you will want to use one of the component classes that import this mixin, such as
  10629. * {@link Ext.data.Store} or {@link Ext.data.TreeStore}.
  10630. */
  10631. Ext.define("Ext.util.Sortable", {
  10632. /**
  10633. * @property {Boolean} isSortable
  10634. * `true` in this class to identify an objact as an instantiated Sortable, or subclass thereof.
  10635. */
  10636. isSortable: true,
  10637. /**
  10638. * @property {String} defaultSortDirection
  10639. * The default sort direction to use if one is not specified.
  10640. */
  10641. defaultSortDirection: "ASC",
  10642. requires: [
  10643. 'Ext.util.Sorter'
  10644. ],
  10645. /**
  10646. * @property {String} sortRoot
  10647. * The property in each item that contains the data to sort.
  10648. */
  10649. /**
  10650. * Performs initialization of this mixin. Component classes using this mixin should call this method during their
  10651. * own initialization.
  10652. */
  10653. initSortable: function() {
  10654. var me = this,
  10655. sorters = me.sorters;
  10656. /**
  10657. * @property {Ext.util.MixedCollection} sorters
  10658. * The collection of {@link Ext.util.Sorter Sorters} currently applied to this Store
  10659. */
  10660. me.sorters = new Ext.util.AbstractMixedCollection(false, function(item) {
  10661. return item.id || item.property;
  10662. });
  10663. if (sorters) {
  10664. me.sorters.addAll(me.decodeSorters(sorters));
  10665. }
  10666. },
  10667. /**
  10668. * Sorts the data in the Store by one or more of its properties. Example usage:
  10669. *
  10670. * //sort by a single field
  10671. * myStore.sort('myField', 'DESC');
  10672. *
  10673. * //sorting by multiple fields
  10674. * myStore.sort([
  10675. * {
  10676. * property : 'age',
  10677. * direction: 'ASC'
  10678. * },
  10679. * {
  10680. * property : 'name',
  10681. * direction: 'DESC'
  10682. * }
  10683. * ]);
  10684. *
  10685. * Internally, Store converts the passed arguments into an array of {@link Ext.util.Sorter} instances, and delegates
  10686. * the actual sorting to its internal {@link Ext.util.MixedCollection}.
  10687. *
  10688. * When passing a single string argument to sort, Store maintains a ASC/DESC toggler per field, so this code:
  10689. *
  10690. * store.sort('myField');
  10691. * store.sort('myField');
  10692. *
  10693. * Is equivalent to this code, because Store handles the toggling automatically:
  10694. *
  10695. * store.sort('myField', 'ASC');
  10696. * store.sort('myField', 'DESC');
  10697. *
  10698. * @param {String/Ext.util.Sorter[]} sorters Either a string name of one of the fields in this Store's configured
  10699. * {@link Ext.data.Model Model}, or an array of sorter configurations.
  10700. * @param {String} direction The overall direction to sort the data by. Defaults to "ASC".
  10701. * @return {Ext.util.Sorter[]}
  10702. */
  10703. sort: function(sorters, direction, where, doSort) {
  10704. var me = this,
  10705. sorter, sorterFn,
  10706. newSorters;
  10707. if (Ext.isArray(sorters)) {
  10708. doSort = where;
  10709. where = direction;
  10710. newSorters = sorters;
  10711. }
  10712. else if (Ext.isObject(sorters)) {
  10713. doSort = where;
  10714. where = direction;
  10715. newSorters = [sorters];
  10716. }
  10717. else if (Ext.isString(sorters)) {
  10718. sorter = me.sorters.get(sorters);
  10719. if (!sorter) {
  10720. sorter = {
  10721. property : sorters,
  10722. direction: direction
  10723. };
  10724. newSorters = [sorter];
  10725. }
  10726. else if (direction === undefined) {
  10727. sorter.toggle();
  10728. }
  10729. else {
  10730. sorter.setDirection(direction);
  10731. }
  10732. }
  10733. if (newSorters && newSorters.length) {
  10734. newSorters = me.decodeSorters(newSorters);
  10735. if (Ext.isString(where)) {
  10736. if (where === 'prepend') {
  10737. sorters = me.sorters.clone().items;
  10738. me.sorters.clear();
  10739. me.sorters.addAll(newSorters);
  10740. me.sorters.addAll(sorters);
  10741. }
  10742. else {
  10743. me.sorters.addAll(newSorters);
  10744. }
  10745. }
  10746. else {
  10747. me.sorters.clear();
  10748. me.sorters.addAll(newSorters);
  10749. }
  10750. }
  10751. if (doSort !== false) {
  10752. me.onBeforeSort(newSorters);
  10753. sorters = me.sorters.items;
  10754. if (sorters.length) {
  10755. // Sort using a generated sorter function which combines all of the Sorters passed
  10756. me.doSort(me.generateComparator());
  10757. }
  10758. }
  10759. return sorters;
  10760. },
  10761. /**
  10762. * <p>Returns a comparator function which compares two items and returns -1, 0, or 1 depending
  10763. * on the currently defined set of {@link #sorters}.</p>
  10764. * <p>If there are no {@link #sorters} defined, it returns a function which returns <code>0</code> meaning that no sorting will occur.</p>
  10765. */
  10766. generateComparator: function() {
  10767. return (this.sorters.items.length) ? (function(sorters) {
  10768. return function(r1, r2) {
  10769. var result = sorters[0].sort(r1, r2),
  10770. length = sorters.length,
  10771. i;
  10772. // if we have more than one sorter, OR any additional sorter functions together
  10773. for (i = 1; i < length; i++) {
  10774. result = result || sorters[i].sort.call(this, r1, r2);
  10775. }
  10776. return result;
  10777. };
  10778. })(this.sorters.items) : function() {
  10779. return 0;
  10780. };
  10781. },
  10782. onBeforeSort: Ext.emptyFn,
  10783. /**
  10784. * @private
  10785. * Normalizes an array of sorter objects, ensuring that they are all Ext.util.Sorter instances
  10786. * @param {Object[]} sorters The sorters array
  10787. * @return {Ext.util.Sorter[]} Array of Ext.util.Sorter objects
  10788. */
  10789. decodeSorters: function(sorters) {
  10790. if (!Ext.isArray(sorters)) {
  10791. if (sorters === undefined) {
  10792. sorters = [];
  10793. } else {
  10794. sorters = [sorters];
  10795. }
  10796. }
  10797. var length = sorters.length,
  10798. Sorter = Ext.util.Sorter,
  10799. fields = this.model ? this.model.prototype.fields : null,
  10800. field,
  10801. config, i;
  10802. for (i = 0; i < length; i++) {
  10803. config = sorters[i];
  10804. if (!(config instanceof Sorter)) {
  10805. if (Ext.isString(config)) {
  10806. config = {
  10807. property: config
  10808. };
  10809. }
  10810. Ext.applyIf(config, {
  10811. root : this.sortRoot,
  10812. direction: "ASC"
  10813. });
  10814. //support for 3.x style sorters where a function can be defined as 'fn'
  10815. if (config.fn) {
  10816. config.sorterFn = config.fn;
  10817. }
  10818. //support a function to be passed as a sorter definition
  10819. if (typeof config == 'function') {
  10820. config = {
  10821. sorterFn: config
  10822. };
  10823. }
  10824. // ensure sortType gets pushed on if necessary
  10825. if (fields && !config.transform) {
  10826. field = fields.get(config.property);
  10827. config.transform = field ? field.sortType : undefined;
  10828. }
  10829. sorters[i] = new Ext.util.Sorter(config);
  10830. }
  10831. }
  10832. return sorters;
  10833. },
  10834. getSorters: function() {
  10835. return this.sorters.items;
  10836. }
  10837. });
  10838. /**
  10839. * @class Ext.util.MixedCollection
  10840. * <p>
  10841. * Represents a collection of a set of key and value pairs. Each key in the MixedCollection
  10842. * must be unique, the same key cannot exist twice. This collection is ordered, items in the
  10843. * collection can be accessed by index or via the key. Newly added items are added to
  10844. * the end of the collection. This class is similar to {@link Ext.util.HashMap} however it
  10845. * is heavier and provides more functionality. Sample usage:
  10846. * <pre><code>
  10847. var coll = new Ext.util.MixedCollection();
  10848. coll.add('key1', 'val1');
  10849. coll.add('key2', 'val2');
  10850. coll.add('key3', 'val3');
  10851. console.log(coll.get('key1')); // prints 'val1'
  10852. console.log(coll.indexOfKey('key3')); // prints 2
  10853. * </code></pre>
  10854. *
  10855. * <p>
  10856. * The MixedCollection also has support for sorting and filtering of the values in the collection.
  10857. * <pre><code>
  10858. var coll = new Ext.util.MixedCollection();
  10859. coll.add('key1', 100);
  10860. coll.add('key2', -100);
  10861. coll.add('key3', 17);
  10862. coll.add('key4', 0);
  10863. var biggerThanZero = coll.filterBy(function(value){
  10864. return value > 0;
  10865. });
  10866. console.log(biggerThanZero.getCount()); // prints 2
  10867. * </code></pre>
  10868. * </p>
  10869. */
  10870. Ext.define('Ext.util.MixedCollection', {
  10871. extend: 'Ext.util.AbstractMixedCollection',
  10872. mixins: {
  10873. sortable: 'Ext.util.Sortable'
  10874. },
  10875. /**
  10876. * Creates new MixedCollection.
  10877. * @param {Boolean} allowFunctions Specify <tt>true</tt> if the {@link #addAll}
  10878. * function should add function references to the collection. Defaults to
  10879. * <tt>false</tt>.
  10880. * @param {Function} keyFn A function that can accept an item of the type(s) stored in this MixedCollection
  10881. * and return the key value for that item. This is used when available to look up the key on items that
  10882. * were passed without an explicit key parameter to a MixedCollection method. Passing this parameter is
  10883. * equivalent to providing an implementation for the {@link #getKey} method.
  10884. */
  10885. constructor: function() {
  10886. var me = this;
  10887. me.callParent(arguments);
  10888. me.addEvents('sort');
  10889. me.mixins.sortable.initSortable.call(me);
  10890. },
  10891. doSort: function(sorterFn) {
  10892. this.sortBy(sorterFn);
  10893. },
  10894. /**
  10895. * @private
  10896. * Performs the actual sorting based on a direction and a sorting function. Internally,
  10897. * this creates a temporary array of all items in the MixedCollection, sorts it and then writes
  10898. * the sorted array data back into this.items and this.keys
  10899. * @param {String} property Property to sort by ('key', 'value', or 'index')
  10900. * @param {String} dir (optional) Direction to sort 'ASC' or 'DESC'. Defaults to 'ASC'.
  10901. * @param {Function} fn (optional) Comparison function that defines the sort order.
  10902. * Defaults to sorting by numeric value.
  10903. */
  10904. _sort : function(property, dir, fn){
  10905. var me = this,
  10906. i, len,
  10907. dsc = String(dir).toUpperCase() == 'DESC' ? -1 : 1,
  10908. //this is a temporary array used to apply the sorting function
  10909. c = [],
  10910. keys = me.keys,
  10911. items = me.items;
  10912. //default to a simple sorter function if one is not provided
  10913. fn = fn || function(a, b) {
  10914. return a - b;
  10915. };
  10916. //copy all the items into a temporary array, which we will sort
  10917. for(i = 0, len = items.length; i < len; i++){
  10918. c[c.length] = {
  10919. key : keys[i],
  10920. value: items[i],
  10921. index: i
  10922. };
  10923. }
  10924. //sort the temporary array
  10925. Ext.Array.sort(c, function(a, b){
  10926. var v = fn(a[property], b[property]) * dsc;
  10927. if(v === 0){
  10928. v = (a.index < b.index ? -1 : 1);
  10929. }
  10930. return v;
  10931. });
  10932. //copy the temporary array back into the main this.items and this.keys objects
  10933. for(i = 0, len = c.length; i < len; i++){
  10934. items[i] = c[i].value;
  10935. keys[i] = c[i].key;
  10936. }
  10937. me.fireEvent('sort', me);
  10938. },
  10939. /**
  10940. * Sorts the collection by a single sorter function
  10941. * @param {Function} sorterFn The function to sort by
  10942. */
  10943. sortBy: function(sorterFn) {
  10944. var me = this,
  10945. items = me.items,
  10946. keys = me.keys,
  10947. length = items.length,
  10948. temp = [],
  10949. i;
  10950. //first we create a copy of the items array so that we can sort it
  10951. for (i = 0; i < length; i++) {
  10952. temp[i] = {
  10953. key : keys[i],
  10954. value: items[i],
  10955. index: i
  10956. };
  10957. }
  10958. Ext.Array.sort(temp, function(a, b) {
  10959. var v = sorterFn(a.value, b.value);
  10960. if (v === 0) {
  10961. v = (a.index < b.index ? -1 : 1);
  10962. }
  10963. return v;
  10964. });
  10965. //copy the temporary array back into the main this.items and this.keys objects
  10966. for (i = 0; i < length; i++) {
  10967. items[i] = temp[i].value;
  10968. keys[i] = temp[i].key;
  10969. }
  10970. me.fireEvent('sort', me, items, keys);
  10971. },
  10972. /**
  10973. * Calculates the insertion index of the new item based upon the comparison function passed, or the current sort order.
  10974. * @param {Object} newItem The new object to find the insertion position of.
  10975. * @param {Function} [sorterFn] The function to sort by. This is the same as the sorting function
  10976. * passed to {@link #sortBy}. It accepts 2 items from this MixedCollection, and returns -1 0, or 1
  10977. * depending on the relative sort positions of the 2 compared items.
  10978. *
  10979. * If omitted, a function {@link #generateComparator generated} from the currently defined set of
  10980. * {@link #sorters} will be used.
  10981. *
  10982. * @return {Number} The insertion point to add the new item into this MixedCollection at using {@link #insert}
  10983. */
  10984. findInsertionIndex: function(newItem, sorterFn) {
  10985. var me = this,
  10986. items = me.items,
  10987. start = 0,
  10988. end = items.length - 1,
  10989. middle,
  10990. comparison;
  10991. if (!sorterFn) {
  10992. sorterFn = me.generateComparator();
  10993. }
  10994. while (start <= end) {
  10995. middle = (start + end) >> 1;
  10996. comparison = sorterFn(newItem, items[middle]);
  10997. if (comparison >= 0) {
  10998. start = middle + 1;
  10999. } else if (comparison < 0) {
  11000. end = middle - 1;
  11001. }
  11002. }
  11003. return start;
  11004. },
  11005. /**
  11006. * Reorders each of the items based on a mapping from old index to new index. Internally this
  11007. * just translates into a sort. The 'sort' event is fired whenever reordering has occured.
  11008. * @param {Object} mapping Mapping from old item index to new item index
  11009. */
  11010. reorder: function(mapping) {
  11011. var me = this,
  11012. items = me.items,
  11013. index = 0,
  11014. length = items.length,
  11015. order = [],
  11016. remaining = [],
  11017. oldIndex;
  11018. me.suspendEvents();
  11019. //object of {oldPosition: newPosition} reversed to {newPosition: oldPosition}
  11020. for (oldIndex in mapping) {
  11021. order[mapping[oldIndex]] = items[oldIndex];
  11022. }
  11023. for (index = 0; index < length; index++) {
  11024. if (mapping[index] == undefined) {
  11025. remaining.push(items[index]);
  11026. }
  11027. }
  11028. for (index = 0; index < length; index++) {
  11029. if (order[index] == undefined) {
  11030. order[index] = remaining.shift();
  11031. }
  11032. }
  11033. me.clear();
  11034. me.addAll(order);
  11035. me.resumeEvents();
  11036. me.fireEvent('sort', me);
  11037. },
  11038. /**
  11039. * Sorts this collection by <b>key</b>s.
  11040. * @param {String} direction (optional) 'ASC' or 'DESC'. Defaults to 'ASC'.
  11041. * @param {Function} fn (optional) Comparison function that defines the sort order.
  11042. * Defaults to sorting by case insensitive string.
  11043. */
  11044. sortByKey : function(dir, fn){
  11045. this._sort('key', dir, fn || function(a, b){
  11046. var v1 = String(a).toUpperCase(), v2 = String(b).toUpperCase();
  11047. return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
  11048. });
  11049. }
  11050. });
  11051. /**
  11052. * @docauthor Evan Trimboli <evan@sencha.com>
  11053. *
  11054. * Contains a collection of all stores that are created that have an identifier. An identifier can be assigned by
  11055. * setting the {@link Ext.data.AbstractStore#storeId storeId} property. When a store is in the StoreManager, it can be
  11056. * referred to via it's identifier:
  11057. *
  11058. * Ext.create('Ext.data.Store', {
  11059. * model: 'SomeModel',
  11060. * storeId: 'myStore'
  11061. * });
  11062. *
  11063. * var store = Ext.data.StoreManager.lookup('myStore');
  11064. *
  11065. * Also note that the {@link #lookup} method is aliased to {@link Ext#getStore} for convenience.
  11066. *
  11067. * If a store is registered with the StoreManager, you can also refer to the store by it's identifier when registering
  11068. * it with any Component that consumes data from a store:
  11069. *
  11070. * Ext.create('Ext.data.Store', {
  11071. * model: 'SomeModel',
  11072. * storeId: 'myStore'
  11073. * });
  11074. *
  11075. * Ext.create('Ext.view.View', {
  11076. * store: 'myStore',
  11077. * // other configuration here
  11078. * });
  11079. *
  11080. */
  11081. Ext.define('Ext.data.StoreManager', {
  11082. extend: 'Ext.util.MixedCollection',
  11083. alternateClassName: ['Ext.StoreMgr', 'Ext.data.StoreMgr', 'Ext.StoreManager'],
  11084. singleton: true,
  11085. uses: ['Ext.data.ArrayStore'],
  11086. /**
  11087. * @cfg {Object} listeners
  11088. * @private
  11089. */
  11090. /**
  11091. * Registers one or more Stores with the StoreManager. You do not normally need to register stores manually. Any
  11092. * store initialized with a {@link Ext.data.Store#storeId} will be auto-registered.
  11093. * @param {Ext.data.Store...} stores Any number of Store instances
  11094. */
  11095. register : function() {
  11096. for (var i = 0, s; (s = arguments[i]); i++) {
  11097. this.add(s);
  11098. }
  11099. },
  11100. /**
  11101. * Unregisters one or more Stores with the StoreManager
  11102. * @param {String/Object...} stores Any number of Store instances or ID-s
  11103. */
  11104. unregister : function() {
  11105. for (var i = 0, s; (s = arguments[i]); i++) {
  11106. this.remove(this.lookup(s));
  11107. }
  11108. },
  11109. /**
  11110. * Gets a registered Store by id
  11111. * @param {String/Object} store The id of the Store, or a Store instance, or a store configuration
  11112. * @return {Ext.data.Store}
  11113. */
  11114. lookup : function(store) {
  11115. // handle the case when we are given an array or an array of arrays.
  11116. if (Ext.isArray(store)) {
  11117. var fields = ['field1'],
  11118. expand = !Ext.isArray(store[0]),
  11119. data = store,
  11120. i,
  11121. len;
  11122. if(expand){
  11123. data = [];
  11124. for (i = 0, len = store.length; i < len; ++i) {
  11125. data.push([store[i]]);
  11126. }
  11127. } else {
  11128. for(i = 2, len = store[0].length; i <= len; ++i){
  11129. fields.push('field' + i);
  11130. }
  11131. }
  11132. return new Ext.data.ArrayStore({
  11133. data : data,
  11134. fields: fields,
  11135. autoDestroy: true,
  11136. autoCreated: true,
  11137. expanded: expand
  11138. });
  11139. }
  11140. if (Ext.isString(store)) {
  11141. // store id
  11142. return this.get(store);
  11143. } else {
  11144. // store instance or store config
  11145. return Ext.data.AbstractStore.create(store);
  11146. }
  11147. },
  11148. // getKey implementation for MixedCollection
  11149. getKey : function(o) {
  11150. return o.storeId;
  11151. }
  11152. }, function() {
  11153. /**
  11154. * Creates a new store for the given id and config, then registers it with the {@link Ext.data.StoreManager Store Mananger}.
  11155. * Sample usage:
  11156. *
  11157. * Ext.regStore('AllUsers', {
  11158. * model: 'User'
  11159. * });
  11160. *
  11161. * // the store can now easily be used throughout the application
  11162. * new Ext.List({
  11163. * store: 'AllUsers',
  11164. * ... other config
  11165. * });
  11166. *
  11167. * @param {String} id The id to set on the new store
  11168. * @param {Object} config The store config
  11169. * @member Ext
  11170. * @method regStore
  11171. */
  11172. Ext.regStore = function(name, config) {
  11173. var store;
  11174. if (Ext.isObject(name)) {
  11175. config = name;
  11176. } else {
  11177. config.storeId = name;
  11178. }
  11179. if (config instanceof Ext.data.Store) {
  11180. store = config;
  11181. } else {
  11182. store = new Ext.data.Store(config);
  11183. }
  11184. return Ext.data.StoreManager.register(store);
  11185. };
  11186. /**
  11187. * Shortcut to {@link Ext.data.StoreManager#lookup}.
  11188. * @member Ext
  11189. * @method getStore
  11190. * @inheritdoc Ext.data.StoreManager#lookup
  11191. */
  11192. Ext.getStore = function(name) {
  11193. return Ext.data.StoreManager.lookup(name);
  11194. };
  11195. });
  11196. /**
  11197. * @author Ed Spencer
  11198. * @class Ext.data.Errors
  11199. *
  11200. * <p>Wraps a collection of validation error responses and provides convenient functions for
  11201. * accessing and errors for specific fields.</p>
  11202. *
  11203. * <p>Usually this class does not need to be instantiated directly - instances are instead created
  11204. * automatically when {@link Ext.data.Model#validate validate} on a model instance:</p>
  11205. *
  11206. <pre><code>
  11207. //validate some existing model instance - in this case it returned 2 failures messages
  11208. var errors = myModel.validate();
  11209. errors.isValid(); //false
  11210. errors.length; //2
  11211. errors.getByField('name'); // [{field: 'name', message: 'must be present'}]
  11212. errors.getByField('title'); // [{field: 'title', message: 'is too short'}]
  11213. </code></pre>
  11214. */
  11215. Ext.define('Ext.data.Errors', {
  11216. extend: 'Ext.util.MixedCollection',
  11217. /**
  11218. * Returns true if there are no errors in the collection
  11219. * @return {Boolean}
  11220. */
  11221. isValid: function() {
  11222. return this.length === 0;
  11223. },
  11224. /**
  11225. * Returns all of the errors for the given field
  11226. * @param {String} fieldName The field to get errors for
  11227. * @return {Object[]} All errors for the given field
  11228. */
  11229. getByField: function(fieldName) {
  11230. var errors = [],
  11231. error, field, i;
  11232. for (i = 0; i < this.length; i++) {
  11233. error = this.items[i];
  11234. if (error.field == fieldName) {
  11235. errors.push(error);
  11236. }
  11237. }
  11238. return errors;
  11239. }
  11240. });
  11241. /**
  11242. * @class Ext.data.writer.Json
  11243. This class is used to write {@link Ext.data.Model} data to the server in a JSON format.
  11244. The {@link #allowSingle} configuration can be set to false to force the records to always be
  11245. encoded in an array, even if there is only a single record being sent.
  11246. * @markdown
  11247. */
  11248. Ext.define('Ext.data.writer.Json', {
  11249. extend: 'Ext.data.writer.Writer',
  11250. alternateClassName: 'Ext.data.JsonWriter',
  11251. alias: 'writer.json',
  11252. /**
  11253. * @cfg {String} root The key under which the records in this Writer will be placed. Defaults to <tt>undefined</tt>.
  11254. * Example generated request, using root: 'records':
  11255. <pre><code>
  11256. {'records': [{name: 'my record'}, {name: 'another record'}]}
  11257. </code></pre>
  11258. */
  11259. root: undefined,
  11260. /**
  11261. * @cfg {Boolean} encode True to use Ext.encode() on the data before sending. Defaults to <tt>false</tt>.
  11262. * The encode option should only be set to true when a {@link #root} is defined, because the values will be
  11263. * sent as part of the request parameters as opposed to a raw post. The root will be the name of the parameter
  11264. * sent to the server.
  11265. */
  11266. encode: false,
  11267. /**
  11268. * @cfg {Boolean} allowSingle False to ensure that records are always wrapped in an array, even if there is only
  11269. * one record being sent. When there is more than one record, they will always be encoded into an array.
  11270. * Defaults to <tt>true</tt>. Example:
  11271. * <pre><code>
  11272. // with allowSingle: true
  11273. "root": {
  11274. "first": "Mark",
  11275. "last": "Corrigan"
  11276. }
  11277. // with allowSingle: false
  11278. "root": [{
  11279. "first": "Mark",
  11280. "last": "Corrigan"
  11281. }]
  11282. * </code></pre>
  11283. */
  11284. allowSingle: true,
  11285. //inherit docs
  11286. writeRecords: function(request, data) {
  11287. var root = this.root;
  11288. if (this.allowSingle && data.length == 1) {
  11289. // convert to single object format
  11290. data = data[0];
  11291. }
  11292. if (this.encode) {
  11293. if (root) {
  11294. // sending as a param, need to encode
  11295. request.params[root] = Ext.encode(data);
  11296. } else {
  11297. Ext.Error.raise('Must specify a root when using encode');
  11298. }
  11299. } else {
  11300. // send as jsonData
  11301. request.jsonData = request.jsonData || {};
  11302. if (root) {
  11303. request.jsonData[root] = data;
  11304. } else {
  11305. request.jsonData = data;
  11306. }
  11307. }
  11308. return request;
  11309. }
  11310. });
  11311. /**
  11312. * @class Ext.state.Manager
  11313. * This is the global state manager. By default all components that are "state aware" check this class
  11314. * for state information if you don't pass them a custom state provider. In order for this class
  11315. * to be useful, it must be initialized with a provider when your application initializes. Example usage:
  11316. <pre><code>
  11317. // in your initialization function
  11318. init : function(){
  11319. Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
  11320. var win = new Window(...);
  11321. win.restoreState();
  11322. }
  11323. </code></pre>
  11324. * This class passes on calls from components to the underlying {@link Ext.state.Provider} so that
  11325. * there is a common interface that can be used without needing to refer to a specific provider instance
  11326. * in every component.
  11327. * @singleton
  11328. * @docauthor Evan Trimboli <evan@sencha.com>
  11329. */
  11330. Ext.define('Ext.state.Manager', {
  11331. singleton: true,
  11332. requires: ['Ext.state.Provider'],
  11333. constructor: function() {
  11334. this.provider = new Ext.state.Provider();
  11335. },
  11336. /**
  11337. * Configures the default state provider for your application
  11338. * @param {Ext.state.Provider} stateProvider The state provider to set
  11339. */
  11340. setProvider : function(stateProvider){
  11341. this.provider = stateProvider;
  11342. },
  11343. /**
  11344. * Returns the current value for a key
  11345. * @param {String} name The key name
  11346. * @param {Object} defaultValue The default value to return if the key lookup does not match
  11347. * @return {Object} The state data
  11348. */
  11349. get : function(key, defaultValue){
  11350. return this.provider.get(key, defaultValue);
  11351. },
  11352. /**
  11353. * Sets the value for a key
  11354. * @param {String} name The key name
  11355. * @param {Object} value The state data
  11356. */
  11357. set : function(key, value){
  11358. this.provider.set(key, value);
  11359. },
  11360. /**
  11361. * Clears a value from the state
  11362. * @param {String} name The key name
  11363. */
  11364. clear : function(key){
  11365. this.provider.clear(key);
  11366. },
  11367. /**
  11368. * Gets the currently configured state provider
  11369. * @return {Ext.state.Provider} The state provider
  11370. */
  11371. getProvider : function(){
  11372. return this.provider;
  11373. }
  11374. });
  11375. /**
  11376. * @class Ext.state.Stateful
  11377. * A mixin for being able to save the state of an object to an underlying
  11378. * {@link Ext.state.Provider}.
  11379. */
  11380. Ext.define('Ext.state.Stateful', {
  11381. /* Begin Definitions */
  11382. mixins: {
  11383. observable: 'Ext.util.Observable'
  11384. },
  11385. requires: ['Ext.state.Manager'],
  11386. /* End Definitions */
  11387. /**
  11388. * @cfg {Boolean} stateful
  11389. * <p>A flag which causes the object to attempt to restore the state of
  11390. * internal properties from a saved state on startup. The object must have
  11391. * a <code>{@link #stateId}</code> for state to be managed.
  11392. * Auto-generated ids are not guaranteed to be stable across page loads and
  11393. * cannot be relied upon to save and restore the same state for a object.<p>
  11394. * <p>For state saving to work, the state manager's provider must have been
  11395. * set to an implementation of {@link Ext.state.Provider} which overrides the
  11396. * {@link Ext.state.Provider#set set} and {@link Ext.state.Provider#get get}
  11397. * methods to save and recall name/value pairs. A built-in implementation,
  11398. * {@link Ext.state.CookieProvider} is available.</p>
  11399. * <p>To set the state provider for the current page:</p>
  11400. * <pre><code>
  11401. Ext.state.Manager.setProvider(new Ext.state.CookieProvider({
  11402. expires: new Date(new Date().getTime()+(1000*60*60*24*7)), //7 days from now
  11403. }));
  11404. * </code></pre>
  11405. * <p>A stateful object attempts to save state when one of the events
  11406. * listed in the <code>{@link #stateEvents}</code> configuration fires.</p>
  11407. * <p>To save state, a stateful object first serializes its state by
  11408. * calling <b><code>{@link #getState}</code></b>. By default, this function does
  11409. * nothing. The developer must provide an implementation which returns an
  11410. * object hash which represents the restorable state of the object.</p>
  11411. * <p>The value yielded by getState is passed to {@link Ext.state.Manager#set}
  11412. * which uses the configured {@link Ext.state.Provider} to save the object
  11413. * keyed by the <code>{@link #stateId}</code>.</p>
  11414. * <p>During construction, a stateful object attempts to <i>restore</i>
  11415. * its state by calling {@link Ext.state.Manager#get} passing the
  11416. * <code>{@link #stateId}</code></p>
  11417. * <p>The resulting object is passed to <b><code>{@link #applyState}</code></b>.
  11418. * The default implementation of <code>{@link #applyState}</code> simply copies
  11419. * properties into the object, but a developer may override this to support
  11420. * more behaviour.</p>
  11421. * <p>You can perform extra processing on state save and restore by attaching
  11422. * handlers to the {@link #beforestaterestore}, {@link #staterestore},
  11423. * {@link #beforestatesave} and {@link #statesave} events.</p>
  11424. */
  11425. stateful: false,
  11426. /**
  11427. * @cfg {String} stateId
  11428. * The unique id for this object to use for state management purposes.
  11429. * <p>See {@link #stateful} for an explanation of saving and restoring state.</p>
  11430. */
  11431. /**
  11432. * @cfg {String[]} stateEvents
  11433. * <p>An array of events that, when fired, should trigger this object to
  11434. * save its state. Defaults to none. <code>stateEvents</code> may be any type
  11435. * of event supported by this object, including browser or custom events
  11436. * (e.g., <tt>['click', 'customerchange']</tt>).</p>
  11437. * <p>See <code>{@link #stateful}</code> for an explanation of saving and
  11438. * restoring object state.</p>
  11439. */
  11440. /**
  11441. * @cfg {Number} saveDelay
  11442. * A buffer to be applied if many state events are fired within a short period.
  11443. */
  11444. saveDelay: 100,
  11445. constructor: function(config) {
  11446. var me = this;
  11447. config = config || {};
  11448. if (config.stateful !== undefined) {
  11449. me.stateful = config.stateful;
  11450. }
  11451. if (config.saveDelay !== undefined) {
  11452. me.saveDelay = config.saveDelay;
  11453. }
  11454. me.stateId = me.stateId || config.stateId;
  11455. if (!me.stateEvents) {
  11456. me.stateEvents = [];
  11457. }
  11458. if (config.stateEvents) {
  11459. me.stateEvents.concat(config.stateEvents);
  11460. }
  11461. this.addEvents(
  11462. /**
  11463. * @event beforestaterestore
  11464. * Fires before the state of the object is restored. Return false from an event handler to stop the restore.
  11465. * @param {Ext.state.Stateful} this
  11466. * @param {Object} state The hash of state values returned from the StateProvider. If this
  11467. * event is not vetoed, then the state object is passed to <b><tt>applyState</tt></b>. By default,
  11468. * that simply copies property values into this object. The method maybe overriden to
  11469. * provide custom state restoration.
  11470. */
  11471. 'beforestaterestore',
  11472. /**
  11473. * @event staterestore
  11474. * Fires after the state of the object is restored.
  11475. * @param {Ext.state.Stateful} this
  11476. * @param {Object} state The hash of state values returned from the StateProvider. This is passed
  11477. * to <b><tt>applyState</tt></b>. By default, that simply copies property values into this
  11478. * object. The method maybe overriden to provide custom state restoration.
  11479. */
  11480. 'staterestore',
  11481. /**
  11482. * @event beforestatesave
  11483. * Fires before the state of the object is saved to the configured state provider. Return false to stop the save.
  11484. * @param {Ext.state.Stateful} this
  11485. * @param {Object} state The hash of state values. This is determined by calling
  11486. * <b><tt>getState()</tt></b> on the object. This method must be provided by the
  11487. * developer to return whetever representation of state is required, by default, Ext.state.Stateful
  11488. * has a null implementation.
  11489. */
  11490. 'beforestatesave',
  11491. /**
  11492. * @event statesave
  11493. * Fires after the state of the object is saved to the configured state provider.
  11494. * @param {Ext.state.Stateful} this
  11495. * @param {Object} state The hash of state values. This is determined by calling
  11496. * <b><tt>getState()</tt></b> on the object. This method must be provided by the
  11497. * developer to return whetever representation of state is required, by default, Ext.state.Stateful
  11498. * has a null implementation.
  11499. */
  11500. 'statesave'
  11501. );
  11502. me.mixins.observable.constructor.call(me);
  11503. if (me.stateful !== false) {
  11504. me.addStateEvents(me.stateEvents);
  11505. me.initState();
  11506. }
  11507. },
  11508. /**
  11509. * Add events that will trigger the state to be saved. If the first argument is an
  11510. * array, each element of that array is the name of a state event. Otherwise, each
  11511. * argument passed to this method is the name of a state event.
  11512. *
  11513. * @param {String/String[]} events The event name or an array of event names.
  11514. */
  11515. addStateEvents: function (events) {
  11516. var me = this,
  11517. i, event, stateEventsByName;
  11518. if (me.stateful && me.getStateId()) {
  11519. if (typeof events == 'string') {
  11520. events = Array.prototype.slice.call(arguments, 0);
  11521. }
  11522. stateEventsByName = me.stateEventsByName || (me.stateEventsByName = {});
  11523. for (i = events.length; i--; ) {
  11524. event = events[i];
  11525. if (!stateEventsByName[event]) {
  11526. stateEventsByName[event] = 1;
  11527. me.on(event, me.onStateChange, me);
  11528. }
  11529. }
  11530. }
  11531. },
  11532. /**
  11533. * This method is called when any of the {@link #stateEvents} are fired.
  11534. * @private
  11535. */
  11536. onStateChange: function(){
  11537. var me = this,
  11538. delay = me.saveDelay,
  11539. statics, runner;
  11540. if (!me.stateful) {
  11541. return;
  11542. }
  11543. if (delay) {
  11544. if (!me.stateTask) {
  11545. statics = Ext.state.Stateful;
  11546. runner = statics.runner || (statics.runner = new Ext.util.TaskRunner());
  11547. me.stateTask = runner.newTask({
  11548. run: me.saveState,
  11549. scope: me,
  11550. interval: delay,
  11551. repeat: 1
  11552. });
  11553. }
  11554. me.stateTask.start();
  11555. } else {
  11556. me.saveState();
  11557. }
  11558. },
  11559. /**
  11560. * Saves the state of the object to the persistence store.
  11561. */
  11562. saveState: function() {
  11563. var me = this,
  11564. id = me.stateful && me.getStateId(),
  11565. hasListeners = me.hasListeners,
  11566. state;
  11567. if (id) {
  11568. state = me.getState() || {}; //pass along for custom interactions
  11569. if (!hasListeners.beforestatesave || me.fireEvent('beforestatesave', me, state) !== false) {
  11570. Ext.state.Manager.set(id, state);
  11571. if (hasListeners.statesave) {
  11572. me.fireEvent('statesave', me, state);
  11573. }
  11574. }
  11575. }
  11576. },
  11577. /**
  11578. * Gets the current state of the object. By default this function returns null,
  11579. * it should be overridden in subclasses to implement methods for getting the state.
  11580. * @return {Object} The current state
  11581. */
  11582. getState: function(){
  11583. return null;
  11584. },
  11585. /**
  11586. * Applies the state to the object. This should be overridden in subclasses to do
  11587. * more complex state operations. By default it applies the state properties onto
  11588. * the current object.
  11589. * @param {Object} state The state
  11590. */
  11591. applyState: function(state) {
  11592. if (state) {
  11593. Ext.apply(this, state);
  11594. }
  11595. },
  11596. /**
  11597. * Gets the state id for this object.
  11598. * @return {String} The 'stateId' or the implicit 'id' specified by component configuration.
  11599. * @private
  11600. */
  11601. getStateId: function() {
  11602. var me = this;
  11603. return me.stateId || (me.autoGenId ? null : me.id);
  11604. },
  11605. /**
  11606. * Initializes the state of the object upon construction.
  11607. * @private
  11608. */
  11609. initState: function(){
  11610. var me = this,
  11611. id = me.stateful && me.getStateId(),
  11612. hasListeners = me.hasListeners,
  11613. state;
  11614. if (id) {
  11615. state = Ext.state.Manager.get(id);
  11616. if (state) {
  11617. state = Ext.apply({}, state);
  11618. if (!hasListeners.beforestaterestore || me.fireEvent('beforestaterestore', me, state) !== false) {
  11619. me.applyState(state);
  11620. if (hasListeners.staterestore) {
  11621. me.fireEvent('staterestore', me, state);
  11622. }
  11623. }
  11624. }
  11625. }
  11626. },
  11627. /**
  11628. * Conditionally saves a single property from this object to the given state object.
  11629. * The idea is to only save state which has changed from the initial state so that
  11630. * current software settings do not override future software settings. Only those
  11631. * values that are user-changed state should be saved.
  11632. *
  11633. * @param {String} propName The name of the property to save.
  11634. * @param {Object} state The state object in to which to save the property.
  11635. * @param {String} stateName (optional) The name to use for the property in state.
  11636. * @return {Boolean} True if the property was saved, false if not.
  11637. */
  11638. savePropToState: function (propName, state, stateName) {
  11639. var me = this,
  11640. value = me[propName],
  11641. config = me.initialConfig;
  11642. if (me.hasOwnProperty(propName)) {
  11643. if (!config || config[propName] !== value) {
  11644. if (state) {
  11645. state[stateName || propName] = value;
  11646. }
  11647. return true;
  11648. }
  11649. }
  11650. return false;
  11651. },
  11652. /**
  11653. * Gathers additional named properties of the instance and adds their current values
  11654. * to the passed state object.
  11655. * @param {String/String[]} propNames The name (or array of names) of the property to save.
  11656. * @param {Object} state The state object in to which to save the property values.
  11657. * @return {Object} state
  11658. */
  11659. savePropsToState: function (propNames, state) {
  11660. var me = this,
  11661. i, n;
  11662. if (typeof propNames == 'string') {
  11663. me.savePropToState(propNames, state);
  11664. } else {
  11665. for (i = 0, n = propNames.length; i < n; ++i) {
  11666. me.savePropToState(propNames[i], state);
  11667. }
  11668. }
  11669. return state;
  11670. },
  11671. /**
  11672. * Destroys this stateful object.
  11673. */
  11674. destroy: function(){
  11675. var me = this,
  11676. task = me.stateTask;
  11677. if (task) {
  11678. task.destroy();
  11679. me.stateTask = null;
  11680. }
  11681. me.clearListeners();
  11682. }
  11683. });
  11684. /**
  11685. * An abstract base class which provides shared methods for Components across the Sencha product line.
  11686. *
  11687. * Please refer to sub class's documentation
  11688. * @private
  11689. */
  11690. Ext.define('Ext.AbstractComponent', {
  11691. /* Begin Definitions */
  11692. requires: [
  11693. 'Ext.ComponentQuery',
  11694. 'Ext.ComponentManager',
  11695. 'Ext.util.ProtoElement'
  11696. ],
  11697. mixins: {
  11698. observable: 'Ext.util.Observable',
  11699. animate: 'Ext.util.Animate',
  11700. elementCt: 'Ext.util.ElementContainer',
  11701. renderable: 'Ext.util.Renderable',
  11702. state: 'Ext.state.Stateful'
  11703. },
  11704. // The "uses" property specifies class which are used in an instantiated AbstractComponent.
  11705. // They do *not* have to be loaded before this class may be defined - that is what "requires" is for.
  11706. uses: [
  11707. 'Ext.PluginManager',
  11708. 'Ext.Element',
  11709. 'Ext.DomHelper',
  11710. 'Ext.XTemplate',
  11711. 'Ext.ComponentQuery',
  11712. 'Ext.ComponentLoader',
  11713. 'Ext.EventManager',
  11714. 'Ext.layout.Context',
  11715. 'Ext.layout.Layout',
  11716. 'Ext.layout.component.Auto',
  11717. 'Ext.LoadMask',
  11718. 'Ext.ZIndexManager'
  11719. ],
  11720. statics: {
  11721. AUTO_ID: 1000,
  11722. pendingLayouts: null,
  11723. layoutSuspendCount: 0,
  11724. cancelLayout: function(comp) {
  11725. var context = this.runningLayoutContext || this.pendingLayouts;
  11726. if (context) {
  11727. context.cancelComponent(comp);
  11728. }
  11729. },
  11730. flushLayouts: function () {
  11731. var me = this,
  11732. context = me.pendingLayouts;
  11733. if (context && context.invalidQueue.length) {
  11734. me.pendingLayouts = null;
  11735. me.runningLayoutContext = context;
  11736. context.hookMethods({
  11737. runComplete: function () {
  11738. // we need to release the layout queue before running any of the
  11739. // finishedLayout calls because they call afterComponentLayout
  11740. // which can re-enter by calling doLayout/doComponentLayout.
  11741. me.runningLayoutContext = null;
  11742. return this.callParent(); // not "me" here!
  11743. }
  11744. });
  11745. context.run();
  11746. }
  11747. },
  11748. resumeLayouts: function (flush) {
  11749. if (this.layoutSuspendCount && ! --this.layoutSuspendCount) {
  11750. if (flush) {
  11751. this.flushLayouts();
  11752. }
  11753. }
  11754. },
  11755. suspendLayouts: function () {
  11756. ++this.layoutSuspendCount;
  11757. },
  11758. updateLayout: function (comp, defer) {
  11759. var me = this,
  11760. running = me.runningLayoutContext,
  11761. pending;
  11762. if (running) {
  11763. running.queueInvalidate(comp);
  11764. } else {
  11765. pending = me.pendingLayouts || (me.pendingLayouts = new Ext.layout.Context());
  11766. pending.queueInvalidate(comp);
  11767. if (!defer && !me.layoutSuspendCount && !comp.isLayoutSuspended()) {
  11768. me.flushLayouts();
  11769. }
  11770. }
  11771. }
  11772. },
  11773. /* End Definitions */
  11774. /**
  11775. * @property {Boolean} isComponent
  11776. * `true` in this class to identify an objact as an instantiated Component, or subclass thereof.
  11777. */
  11778. isComponent: true,
  11779. /**
  11780. * @private
  11781. */
  11782. getAutoId: function() {
  11783. this.autoGenId = true;
  11784. return ++Ext.AbstractComponent.AUTO_ID;
  11785. },
  11786. deferLayouts: false,
  11787. /**
  11788. * @cfg {String} id
  11789. * The **unique id of this component instance.**
  11790. *
  11791. * It should not be necessary to use this configuration except for singleton objects in your application. Components
  11792. * created with an id may be accessed globally using {@link Ext#getCmp Ext.getCmp}.
  11793. *
  11794. * Instead of using assigned ids, use the {@link #itemId} config, and {@link Ext.ComponentQuery ComponentQuery}
  11795. * which provides selector-based searching for Sencha Components analogous to DOM querying. The {@link
  11796. * Ext.container.Container Container} class contains {@link Ext.container.Container#down shortcut methods} to query
  11797. * its descendant Components by selector.
  11798. *
  11799. * Note that this id will also be used as the element id for the containing HTML element that is rendered to the
  11800. * page for this component. This allows you to write id-based CSS rules to style the specific instance of this
  11801. * component uniquely, and also to select sub-elements using this component's id as the parent.
  11802. *
  11803. * **Note**: to avoid complications imposed by a unique id also see `{@link #itemId}`.
  11804. *
  11805. * **Note**: to access the container of a Component see `{@link #ownerCt}`.
  11806. *
  11807. * Defaults to an {@link #getId auto-assigned id}.
  11808. */
  11809. /**
  11810. * @property {Boolean} autoGenId
  11811. * `true` indicates an id was auto-generated rather than provided by configuration.
  11812. * @private
  11813. */
  11814. autoGenId: false,
  11815. /**
  11816. * @cfg {String} itemId
  11817. * An itemId can be used as an alternative way to get a reference to a component when no object reference is
  11818. * available. Instead of using an `{@link #id}` with {@link Ext}.{@link Ext#getCmp getCmp}, use `itemId` with
  11819. * {@link Ext.container.Container}.{@link Ext.container.Container#getComponent getComponent} which will retrieve
  11820. * `itemId`'s or {@link #id}'s. Since `itemId`'s are an index to the container's internal MixedCollection, the
  11821. * `itemId` is scoped locally to the container -- avoiding potential conflicts with {@link Ext.ComponentManager}
  11822. * which requires a **unique** `{@link #id}`.
  11823. *
  11824. * var c = new Ext.panel.Panel({ //
  11825. * {@link Ext.Component#height height}: 300,
  11826. * {@link #renderTo}: document.body,
  11827. * {@link Ext.container.Container#layout layout}: 'auto',
  11828. * {@link Ext.container.Container#cfg-items items}: [
  11829. * {
  11830. * itemId: 'p1',
  11831. * {@link Ext.panel.Panel#title title}: 'Panel 1',
  11832. * {@link Ext.Component#height height}: 150
  11833. * },
  11834. * {
  11835. * itemId: 'p2',
  11836. * {@link Ext.panel.Panel#title title}: 'Panel 2',
  11837. * {@link Ext.Component#height height}: 150
  11838. * }
  11839. * ]
  11840. * })
  11841. * p1 = c.{@link Ext.container.Container#getComponent getComponent}('p1'); // not the same as {@link Ext#getCmp Ext.getCmp()}
  11842. * p2 = p1.{@link #ownerCt}.{@link Ext.container.Container#getComponent getComponent}('p2'); // reference via a sibling
  11843. *
  11844. * Also see {@link #id}, `{@link Ext.container.Container#query}`, `{@link Ext.container.Container#down}` and
  11845. * `{@link Ext.container.Container#child}`.
  11846. *
  11847. * **Note**: to access the container of an item see {@link #ownerCt}.
  11848. */
  11849. /**
  11850. * @property {Ext.Container} ownerCt
  11851. * This Component's owner {@link Ext.container.Container Container} (is set automatically
  11852. * when this Component is added to a Container).
  11853. *
  11854. * **Note**: to access items within the Container see {@link #itemId}.
  11855. * @readonly
  11856. */
  11857. /**
  11858. * @cfg {String/Object} autoEl
  11859. * A tag name or {@link Ext.DomHelper DomHelper} spec used to create the {@link #getEl Element} which will
  11860. * encapsulate this Component.
  11861. *
  11862. * You do not normally need to specify this. For the base classes {@link Ext.Component} and
  11863. * {@link Ext.container.Container}, this defaults to **'div'**. The more complex Sencha classes use a more
  11864. * complex DOM structure specified by their own {@link #renderTpl}s.
  11865. *
  11866. * This is intended to allow the developer to create application-specific utility Components encapsulated by
  11867. * different DOM elements. Example usage:
  11868. *
  11869. * {
  11870. * xtype: 'component',
  11871. * autoEl: {
  11872. * tag: 'img',
  11873. * src: 'http://www.example.com/example.jpg'
  11874. * }
  11875. * }, {
  11876. * xtype: 'component',
  11877. * autoEl: {
  11878. * tag: 'blockquote',
  11879. * html: 'autoEl is cool!'
  11880. * }
  11881. * }, {
  11882. * xtype: 'container',
  11883. * autoEl: 'ul',
  11884. * cls: 'ux-unordered-list',
  11885. * items: {
  11886. * xtype: 'component',
  11887. * autoEl: 'li',
  11888. * html: 'First list item'
  11889. * }
  11890. * }
  11891. */
  11892. /**
  11893. * @cfg {Ext.XTemplate/String/String[]} renderTpl
  11894. * An {@link Ext.XTemplate XTemplate} used to create the internal structure inside this Component's encapsulating
  11895. * {@link #getEl Element}.
  11896. *
  11897. * You do not normally need to specify this. For the base classes {@link Ext.Component} and
  11898. * {@link Ext.container.Container}, this defaults to **`null`** which means that they will be initially rendered
  11899. * with no internal structure; they render their {@link #getEl Element} empty. The more specialized ExtJS and Touch
  11900. * classes which use a more complex DOM structure, provide their own template definitions.
  11901. *
  11902. * This is intended to allow the developer to create application-specific utility Components with customized
  11903. * internal structure.
  11904. *
  11905. * Upon rendering, any created child elements may be automatically imported into object properties using the
  11906. * {@link #renderSelectors} and {@link #childEls} options.
  11907. * @protected
  11908. */
  11909. renderTpl: '{%this.renderContent(out,values)%}',
  11910. /**
  11911. * @cfg {Object} renderData
  11912. *
  11913. * The data used by {@link #renderTpl} in addition to the following property values of the component:
  11914. *
  11915. * - id
  11916. * - ui
  11917. * - uiCls
  11918. * - baseCls
  11919. * - componentCls
  11920. * - frame
  11921. *
  11922. * See {@link #renderSelectors} and {@link #childEls} for usage examples.
  11923. */
  11924. /**
  11925. * @cfg {Object} renderSelectors
  11926. * An object containing properties specifying {@link Ext.DomQuery DomQuery} selectors which identify child elements
  11927. * created by the render process.
  11928. *
  11929. * After the Component's internal structure is rendered according to the {@link #renderTpl}, this object is iterated through,
  11930. * and the found Elements are added as properties to the Component using the `renderSelector` property name.
  11931. *
  11932. * For example, a Component which renderes a title and description into its element:
  11933. *
  11934. * Ext.create('Ext.Component', {
  11935. * renderTo: Ext.getBody(),
  11936. * renderTpl: [
  11937. * '<h1 class="title">{title}</h1>',
  11938. * '<p>{desc}</p>'
  11939. * ],
  11940. * renderData: {
  11941. * title: "Error",
  11942. * desc: "Something went wrong"
  11943. * },
  11944. * renderSelectors: {
  11945. * titleEl: 'h1.title',
  11946. * descEl: 'p'
  11947. * },
  11948. * listeners: {
  11949. * afterrender: function(cmp){
  11950. * // After rendering the component will have a titleEl and descEl properties
  11951. * cmp.titleEl.setStyle({color: "red"});
  11952. * }
  11953. * }
  11954. * });
  11955. *
  11956. * For a faster, but less flexible, alternative that achieves the same end result (properties for child elements on the
  11957. * Component after render), see {@link #childEls} and {@link #addChildEls}.
  11958. */
  11959. /**
  11960. * @cfg {Object[]} childEls
  11961. * An array describing the child elements of the Component. Each member of the array
  11962. * is an object with these properties:
  11963. *
  11964. * - `name` - The property name on the Component for the child element.
  11965. * - `itemId` - The id to combine with the Component's id that is the id of the child element.
  11966. * - `id` - The id of the child element.
  11967. *
  11968. * If the array member is a string, it is equivalent to `{ name: m, itemId: m }`.
  11969. *
  11970. * For example, a Component which renders a title and body text:
  11971. *
  11972. * Ext.create('Ext.Component', {
  11973. * renderTo: Ext.getBody(),
  11974. * renderTpl: [
  11975. * '<h1 id="{id}-title">{title}</h1>',
  11976. * '<p>{msg}</p>',
  11977. * ],
  11978. * renderData: {
  11979. * title: "Error",
  11980. * msg: "Something went wrong"
  11981. * },
  11982. * childEls: ["title"],
  11983. * listeners: {
  11984. * afterrender: function(cmp){
  11985. * // After rendering the component will have a title property
  11986. * cmp.title.setStyle({color: "red"});
  11987. * }
  11988. * }
  11989. * });
  11990. *
  11991. * A more flexible, but somewhat slower, approach is {@link #renderSelectors}.
  11992. */
  11993. /**
  11994. * @cfg {String/HTMLElement/Ext.Element} renderTo
  11995. * Specify the id of the element, a DOM element or an existing Element that this component will be rendered into.
  11996. *
  11997. * **Notes:**
  11998. *
  11999. * Do *not* use this option if the Component is to be a child item of a {@link Ext.container.Container Container}.
  12000. * It is the responsibility of the {@link Ext.container.Container Container}'s
  12001. * {@link Ext.container.Container#layout layout manager} to render and manage its child items.
  12002. *
  12003. * When using this config, a call to render() is not required.
  12004. *
  12005. * See `{@link #render}` also.
  12006. */
  12007. /**
  12008. * @cfg {Boolean} frame
  12009. * Specify as `true` to have the Component inject framing elements within the Component at render time to provide a
  12010. * graphical rounded frame around the Component content.
  12011. *
  12012. * This is only necessary when running on outdated, or non standard-compliant browsers such as Microsoft's Internet
  12013. * Explorer prior to version 9 which do not support rounded corners natively.
  12014. *
  12015. * The extra space taken up by this framing is available from the read only property {@link #frameSize}.
  12016. */
  12017. /**
  12018. * @property {Object} frameSize
  12019. * @readonly
  12020. * Indicates the width of any framing elements which were added within the encapsulating element
  12021. * to provide graphical, rounded borders. See the {@link #frame} config.
  12022. *
  12023. * This is an object containing the frame width in pixels for all four sides of the Component containing the
  12024. * following properties:
  12025. *
  12026. * @property {Number} frameSize.top The width of the top framing element in pixels.
  12027. * @property {Number} frameSize.right The width of the right framing element in pixels.
  12028. * @property {Number} frameSize.bottom The width of the bottom framing element in pixels.
  12029. * @property {Number} frameSize.left The width of the left framing element in pixels.
  12030. */
  12031. /**
  12032. * @cfg {String/Object} componentLayout
  12033. * The sizing and positioning of a Component's internal Elements is the responsibility of the Component's layout
  12034. * manager which sizes a Component's internal structure in response to the Component being sized.
  12035. *
  12036. * Generally, developers will not use this configuration as all provided Components which need their internal
  12037. * elements sizing (Such as {@link Ext.form.field.Base input fields}) come with their own componentLayout managers.
  12038. *
  12039. * The {@link Ext.layout.container.Auto default layout manager} will be used on instances of the base Ext.Component
  12040. * class which simply sizes the Component's encapsulating element to the height and width specified in the
  12041. * {@link #setSize} method.
  12042. */
  12043. /**
  12044. * @cfg {Ext.XTemplate/Ext.Template/String/String[]} tpl
  12045. * An {@link Ext.Template}, {@link Ext.XTemplate} or an array of strings to form an Ext.XTemplate. Used in
  12046. * conjunction with the `{@link #data}` and `{@link #tplWriteMode}` configurations.
  12047. */
  12048. /**
  12049. * @cfg {Object} data
  12050. * The initial set of data to apply to the `{@link #tpl}` to update the content area of the Component.
  12051. */
  12052. /**
  12053. * @cfg {String} xtype
  12054. * This property provides a shorter alternative to creating objects than using a full
  12055. * class name. Using `xtype` is the most common way to define component instances,
  12056. * especially in a container. For example, the items in a form containing text fields
  12057. * could be created explicitly like so:
  12058. *
  12059. * items: [
  12060. * Ext.create('Ext.form.field.Text', {
  12061. * fieldLabel: 'Foo'
  12062. * }),
  12063. * Ext.create('Ext.form.field.Text', {
  12064. * fieldLabel: 'Bar'
  12065. * }),
  12066. * Ext.create('Ext.form.field.Number', {
  12067. * fieldLabel: 'Num'
  12068. * })
  12069. * ]
  12070. *
  12071. * But by using `xtype`, the above becomes:
  12072. *
  12073. * items: [
  12074. * {
  12075. * xtype: 'textfield',
  12076. * fieldLabel: 'Foo'
  12077. * },
  12078. * {
  12079. * xtype: 'textfield',
  12080. * fieldLabel: 'Bar'
  12081. * },
  12082. * {
  12083. * xtype: 'numberfield',
  12084. * fieldLabel: 'Num'
  12085. * }
  12086. * ]
  12087. *
  12088. * When the `xtype` is common to many items, {@link Ext.container.AbstractContainer#defaultType}
  12089. * is another way to specify the `xtype` for all items that don't have an explicit `xtype`:
  12090. *
  12091. * defaultType: 'textfield',
  12092. * items: [
  12093. * { fieldLabel: 'Foo' },
  12094. * { fieldLabel: 'Bar' },
  12095. * { fieldLabel: 'Num', xtype: 'numberfield' }
  12096. * ]
  12097. *
  12098. * Each member of the `items` array is now just a "configuration object". These objects
  12099. * are used to create and configure component instances. A configuration object can be
  12100. * manually used to instantiate a component using {@link Ext#widget}:
  12101. *
  12102. * var text1 = Ext.create('Ext.form.field.Text', {
  12103. * fieldLabel: 'Foo'
  12104. * });
  12105. *
  12106. * // or alternatively:
  12107. *
  12108. * var text1 = Ext.widget({
  12109. * xtype: 'textfield',
  12110. * fieldLabel: 'Foo'
  12111. * });
  12112. *
  12113. * This conversion of configuration objects into instantiated components is done when
  12114. * a container is created as part of its {Ext.container.AbstractContainer#initComponent}
  12115. * process. As part of the same process, the `items` array is converted from its raw
  12116. * array form into a {@link Ext.util.MixedCollection} instance.
  12117. *
  12118. * You can define your own `xtype` on a custom {@link Ext.Component component} by specifying
  12119. * the `xtype` property in {@link Ext#define}. For example:
  12120. *
  12121. * Ext.define('MyApp.PressMeButton', {
  12122. * extend: 'Ext.button.Button',
  12123. * xtype: 'pressmebutton',
  12124. * text: 'Press Me'
  12125. * });
  12126. *
  12127. * Care should be taken when naming an `xtype` in a custom component because there is
  12128. * a single, shared scope for all xtypes. Third part components should consider using
  12129. * a prefix to avoid collisions.
  12130. *
  12131. * Ext.define('Foo.form.CoolButton', {
  12132. * extend: 'Ext.button.Button',
  12133. * xtype: 'ux-coolbutton',
  12134. * text: 'Cool!'
  12135. * });
  12136. */
  12137. /**
  12138. * @cfg {String} tplWriteMode
  12139. * The Ext.(X)Template method to use when updating the content area of the Component.
  12140. * See `{@link Ext.XTemplate#overwrite}` for information on default mode.
  12141. */
  12142. tplWriteMode: 'overwrite',
  12143. /**
  12144. * @cfg {String} [baseCls='x-component']
  12145. * The base CSS class to apply to this components's element. This will also be prepended to elements within this
  12146. * component like Panel's body will get a class x-panel-body. This means that if you create a subclass of Panel, and
  12147. * you want it to get all the Panels styling for the element and the body, you leave the baseCls x-panel and use
  12148. * componentCls to add specific styling for this component.
  12149. */
  12150. baseCls: Ext.baseCSSPrefix + 'component',
  12151. /**
  12152. * @cfg {String} componentCls
  12153. * CSS Class to be added to a components root level element to give distinction to it via styling.
  12154. */
  12155. /**
  12156. * @cfg {String} [cls='']
  12157. * An optional extra CSS class that will be added to this component's Element. This can be useful
  12158. * for adding customized styles to the component or any of its children using standard CSS rules.
  12159. */
  12160. /**
  12161. * @cfg {String} [overCls='']
  12162. * An optional extra CSS class that will be added to this component's Element when the mouse moves over the Element,
  12163. * and removed when the mouse moves out. This can be useful for adding customized 'active' or 'hover' styles to the
  12164. * component or any of its children using standard CSS rules.
  12165. */
  12166. /**
  12167. * @cfg {String} [disabledCls='x-item-disabled']
  12168. * CSS class to add when the Component is disabled. Defaults to 'x-item-disabled'.
  12169. */
  12170. disabledCls: Ext.baseCSSPrefix + 'item-disabled',
  12171. /**
  12172. * @cfg {String/String[]} ui
  12173. * A set style for a component. Can be a string or an Array of multiple strings (UIs)
  12174. */
  12175. ui: 'default',
  12176. /**
  12177. * @cfg {String[]} uiCls
  12178. * An array of of classNames which are currently applied to this component
  12179. * @private
  12180. */
  12181. uiCls: [],
  12182. /**
  12183. * @cfg {String/Object} style
  12184. * A custom style specification to be applied to this component's Element. Should be a valid argument to
  12185. * {@link Ext.Element#applyStyles}.
  12186. *
  12187. * new Ext.panel.Panel({
  12188. * title: 'Some Title',
  12189. * renderTo: Ext.getBody(),
  12190. * width: 400, height: 300,
  12191. * layout: 'form',
  12192. * items: [{
  12193. * xtype: 'textarea',
  12194. * style: {
  12195. * width: '95%',
  12196. * marginBottom: '10px'
  12197. * }
  12198. * },
  12199. * new Ext.button.Button({
  12200. * text: 'Send',
  12201. * minWidth: '100',
  12202. * style: {
  12203. * marginBottom: '10px'
  12204. * }
  12205. * })
  12206. * ]
  12207. * });
  12208. */
  12209. /**
  12210. * @cfg {Number} width
  12211. * The width of this component in pixels.
  12212. */
  12213. /**
  12214. * @cfg {Number} height
  12215. * The height of this component in pixels.
  12216. */
  12217. /**
  12218. * @cfg {Number/String} border
  12219. * Specifies the border for this component. The border can be a single numeric value to apply to all sides or it can
  12220. * be a CSS style specification for each style, for example: '10 5 3 10'.
  12221. */
  12222. /**
  12223. * @cfg {Number/String} padding
  12224. * Specifies the padding for this component. The padding can be a single numeric value to apply to all sides or it
  12225. * can be a CSS style specification for each style, for example: '10 5 3 10'.
  12226. */
  12227. /**
  12228. * @cfg {Number/String} margin
  12229. * Specifies the margin for this component. The margin can be a single numeric value to apply to all sides or it can
  12230. * be a CSS style specification for each style, for example: '10 5 3 10'.
  12231. */
  12232. /**
  12233. * @cfg {Boolean} hidden
  12234. * True to hide the component.
  12235. */
  12236. hidden: false,
  12237. /**
  12238. * @cfg {Boolean} disabled
  12239. * True to disable the component.
  12240. */
  12241. disabled: false,
  12242. /**
  12243. * @cfg {Boolean} [draggable=false]
  12244. * Allows the component to be dragged.
  12245. */
  12246. /**
  12247. * @property {Boolean} draggable
  12248. * Indicates whether or not the component can be dragged.
  12249. * @readonly
  12250. */
  12251. draggable: false,
  12252. /**
  12253. * @cfg {Boolean} floating
  12254. * Create the Component as a floating and use absolute positioning.
  12255. *
  12256. * The z-index of floating Components is handled by a ZIndexManager. If you simply render a floating Component into the DOM, it will be managed
  12257. * by the global {@link Ext.WindowManager WindowManager}.
  12258. *
  12259. * If you include a floating Component as a child item of a Container, then upon render, ExtJS will seek an ancestor floating Component to house a new
  12260. * ZIndexManager instance to manage its descendant floaters. If no floating ancestor can be found, the global WindowManager will be used.
  12261. *
  12262. * When a floating Component which has a ZindexManager managing descendant floaters is destroyed, those descendant floaters will also be destroyed.
  12263. */
  12264. floating: false,
  12265. /**
  12266. * @cfg {String} hideMode
  12267. * A String which specifies how this Component's encapsulating DOM element will be hidden. Values may be:
  12268. *
  12269. * - `'display'` : The Component will be hidden using the `display: none` style.
  12270. * - `'visibility'` : The Component will be hidden using the `visibility: hidden` style.
  12271. * - `'offsets'` : The Component will be hidden by absolutely positioning it out of the visible area of the document.
  12272. * This is useful when a hidden Component must maintain measurable dimensions. Hiding using `display` results in a
  12273. * Component having zero dimensions.
  12274. */
  12275. hideMode: 'display',
  12276. /**
  12277. * @cfg {String} contentEl
  12278. * Specify an existing HTML element, or the `id` of an existing HTML element to use as the content for this component.
  12279. *
  12280. * This config option is used to take an existing HTML element and place it in the layout element of a new component
  12281. * (it simply moves the specified DOM element _after the Component is rendered_ to use as the content.
  12282. *
  12283. * **Notes:**
  12284. *
  12285. * The specified HTML element is appended to the layout element of the component _after any configured
  12286. * {@link #html HTML} has been inserted_, and so the document will not contain this element at the time
  12287. * the {@link #render} event is fired.
  12288. *
  12289. * The specified HTML element used will not participate in any **`{@link Ext.container.Container#layout layout}`**
  12290. * scheme that the Component may use. It is just HTML. Layouts operate on child
  12291. * **`{@link Ext.container.Container#cfg-items items}`**.
  12292. *
  12293. * Add either the `x-hidden` or the `x-hide-display` CSS class to prevent a brief flicker of the content before it
  12294. * is rendered to the panel.
  12295. */
  12296. /**
  12297. * @cfg {String/Object} [html='']
  12298. * An HTML fragment, or a {@link Ext.DomHelper DomHelper} specification to use as the layout element content.
  12299. * The HTML content is added after the component is rendered, so the document will not contain this HTML at the time
  12300. * the {@link #render} event is fired. This content is inserted into the body _before_ any configured {@link #contentEl}
  12301. * is appended.
  12302. */
  12303. /**
  12304. * @cfg {Boolean} styleHtmlContent
  12305. * True to automatically style the html inside the content target of this component (body for panels).
  12306. */
  12307. styleHtmlContent: false,
  12308. /**
  12309. * @cfg {String} [styleHtmlCls='x-html']
  12310. * The class that is added to the content target when you set styleHtmlContent to true.
  12311. */
  12312. styleHtmlCls: Ext.baseCSSPrefix + 'html',
  12313. /**
  12314. * @cfg {Number} minHeight
  12315. * The minimum value in pixels which this Component will set its height to.
  12316. *
  12317. * **Warning:** This will override any size management applied by layout managers.
  12318. */
  12319. /**
  12320. * @cfg {Number} minWidth
  12321. * The minimum value in pixels which this Component will set its width to.
  12322. *
  12323. * **Warning:** This will override any size management applied by layout managers.
  12324. */
  12325. /**
  12326. * @cfg {Number} maxHeight
  12327. * The maximum value in pixels which this Component will set its height to.
  12328. *
  12329. * **Warning:** This will override any size management applied by layout managers.
  12330. */
  12331. /**
  12332. * @cfg {Number} maxWidth
  12333. * The maximum value in pixels which this Component will set its width to.
  12334. *
  12335. * **Warning:** This will override any size management applied by layout managers.
  12336. */
  12337. /**
  12338. * @cfg {Ext.ComponentLoader/Object} loader
  12339. * A configuration object or an instance of a {@link Ext.ComponentLoader} to load remote content for this Component.
  12340. */
  12341. /**
  12342. * @cfg {Boolean} autoShow
  12343. * True to automatically show the component upon creation. This config option may only be used for
  12344. * {@link #floating} components or components that use {@link #autoRender}. Defaults to false.
  12345. */
  12346. autoShow: false,
  12347. /**
  12348. * @cfg {Boolean/String/HTMLElement/Ext.Element} autoRender
  12349. * This config is intended mainly for non-{@link #floating} Components which may or may not be shown. Instead of using
  12350. * {@link #renderTo} in the configuration, and rendering upon construction, this allows a Component to render itself
  12351. * upon first _{@link #method-show}_. If {@link #floating} is true, the value of this config is omited as if it is `true`.
  12352. *
  12353. * Specify as `true` to have this Component render to the document body upon first show.
  12354. *
  12355. * Specify as an element, or the ID of an element to have this Component render to a specific element upon first
  12356. * show.
  12357. */
  12358. autoRender: false,
  12359. // @private
  12360. allowDomMove: true,
  12361. /**
  12362. * @cfg {Object/Object[]} plugins
  12363. * An object or array of objects that will provide custom functionality for this component. The only requirement for
  12364. * a valid plugin is that it contain an init method that accepts a reference of type Ext.Component. When a component
  12365. * is created, if any plugins are available, the component will call the init method on each plugin, passing a
  12366. * reference to itself. Each plugin can then call methods or respond to events on the component as needed to provide
  12367. * its functionality.
  12368. */
  12369. /**
  12370. * @property {Boolean} rendered
  12371. * Indicates whether or not the component has been rendered.
  12372. * @readonly
  12373. */
  12374. rendered: false,
  12375. /**
  12376. * @property {Number} componentLayoutCounter
  12377. * @private
  12378. * The number of component layout calls made on this object.
  12379. */
  12380. componentLayoutCounter: 0,
  12381. /**
  12382. * @cfg {Boolean/Number} [shrinkWrap=2]
  12383. *
  12384. * If this property is a number, it is interpreted as follows:
  12385. *
  12386. * - 0: Neither width nor height depend on content. This is equivalent to `false`.
  12387. * - 1: Width depends on content (shrink wraps), but height does not.
  12388. * - 2: Height depends on content (shrink wraps), but width does not. The default.
  12389. * - 3: Both width and height depend on content (shrink wrap). This is equivalent to `true`.
  12390. *
  12391. * In CSS terms, shrink-wrap width is analogous to an inline-block element as opposed
  12392. * to a block-level element. Some container layouts always shrink-wrap their children,
  12393. * effectively ignoring this property (e.g., {@link Ext.layout.container.HBox},
  12394. * {@link Ext.layout.container.VBox}, {@link Ext.layout.component.Dock}).
  12395. */
  12396. shrinkWrap: 2,
  12397. weight: 0,
  12398. /**
  12399. * @property {Boolean} maskOnDisable
  12400. * This is an internal flag that you use when creating custom components. By default this is set to true which means
  12401. * that every component gets a mask when its disabled. Components like FieldContainer, FieldSet, Field, Button, Tab
  12402. * override this property to false since they want to implement custom disable logic.
  12403. */
  12404. maskOnDisable: true,
  12405. /**
  12406. * @property {Boolean} [_isLayoutRoot=false]
  12407. * Setting this property to `true` causes the {@link #isLayoutRoot} method to return
  12408. * `true` and stop the search for the top-most component for a layout.
  12409. * @protected
  12410. */
  12411. _isLayoutRoot: false,
  12412. /**
  12413. * Creates new Component.
  12414. * @param {Object} config (optional) Config object.
  12415. */
  12416. constructor : function(config) {
  12417. var me = this,
  12418. i, len, xhooks;
  12419. if (config) {
  12420. Ext.apply(me, config);
  12421. xhooks = me.xhooks;
  12422. if (xhooks) {
  12423. me.hookMethods(xhooks);
  12424. delete me.xhooks;
  12425. }
  12426. } else {
  12427. config = {};
  12428. }
  12429. me.initialConfig = config;
  12430. me.mixins.elementCt.constructor.call(me);
  12431. me.addEvents(
  12432. /**
  12433. * @event beforeactivate
  12434. * Fires before a Component has been visually activated. Returning false from an event listener can prevent
  12435. * the activate from occurring.
  12436. * @param {Ext.Component} this
  12437. */
  12438. 'beforeactivate',
  12439. /**
  12440. * @event activate
  12441. * Fires after a Component has been visually activated.
  12442. * @param {Ext.Component} this
  12443. */
  12444. 'activate',
  12445. /**
  12446. * @event beforedeactivate
  12447. * Fires before a Component has been visually deactivated. Returning false from an event listener can
  12448. * prevent the deactivate from occurring.
  12449. * @param {Ext.Component} this
  12450. */
  12451. 'beforedeactivate',
  12452. /**
  12453. * @event deactivate
  12454. * Fires after a Component has been visually deactivated.
  12455. * @param {Ext.Component} this
  12456. */
  12457. 'deactivate',
  12458. /**
  12459. * @event added
  12460. * Fires after a Component had been added to a Container.
  12461. * @param {Ext.Component} this
  12462. * @param {Ext.container.Container} container Parent Container
  12463. * @param {Number} pos position of Component
  12464. */
  12465. 'added',
  12466. /**
  12467. * @event disable
  12468. * Fires after the component is disabled.
  12469. * @param {Ext.Component} this
  12470. */
  12471. 'disable',
  12472. /**
  12473. * @event enable
  12474. * Fires after the component is enabled.
  12475. * @param {Ext.Component} this
  12476. */
  12477. 'enable',
  12478. /**
  12479. * @event beforeshow
  12480. * Fires before the component is shown when calling the {@link #show} method. Return false from an event
  12481. * handler to stop the show.
  12482. * @param {Ext.Component} this
  12483. */
  12484. 'beforeshow',
  12485. /**
  12486. * @event show
  12487. * Fires after the component is shown when calling the {@link #show} method.
  12488. * @param {Ext.Component} this
  12489. */
  12490. 'show',
  12491. /**
  12492. * @event beforehide
  12493. * Fires before the component is hidden when calling the {@link #hide} method. Return false from an event
  12494. * handler to stop the hide.
  12495. * @param {Ext.Component} this
  12496. */
  12497. 'beforehide',
  12498. /**
  12499. * @event hide
  12500. * Fires after the component is hidden. Fires after the component is hidden when calling the {@link #hide}
  12501. * method.
  12502. * @param {Ext.Component} this
  12503. */
  12504. 'hide',
  12505. /**
  12506. * @event removed
  12507. * Fires when a component is removed from an Ext.container.Container
  12508. * @param {Ext.Component} this
  12509. * @param {Ext.container.Container} ownerCt Container which holds the component
  12510. */
  12511. 'removed',
  12512. /**
  12513. * @event beforerender
  12514. * Fires before the component is {@link #rendered}. Return false from an event handler to stop the
  12515. * {@link #render}.
  12516. * @param {Ext.Component} this
  12517. */
  12518. 'beforerender',
  12519. /**
  12520. * @event render
  12521. * Fires after the component markup is {@link #rendered}.
  12522. * @param {Ext.Component} this
  12523. */
  12524. 'render',
  12525. /**
  12526. * @event afterrender
  12527. * Fires after the component rendering is finished.
  12528. *
  12529. * The afterrender event is fired after this Component has been {@link #rendered}, been postprocesed by any
  12530. * afterRender method defined for the Component.
  12531. * @param {Ext.Component} this
  12532. */
  12533. 'afterrender',
  12534. /**
  12535. * @event beforedestroy
  12536. * Fires before the component is {@link #method-destroy}ed. Return false from an event handler to stop the
  12537. * {@link #method-destroy}.
  12538. * @param {Ext.Component} this
  12539. */
  12540. 'beforedestroy',
  12541. /**
  12542. * @event destroy
  12543. * Fires after the component is {@link #method-destroy}ed.
  12544. * @param {Ext.Component} this
  12545. */
  12546. 'destroy',
  12547. /**
  12548. * @event resize
  12549. * Fires after the component is resized.
  12550. * @param {Ext.Component} this
  12551. * @param {Number} width The new width that was set
  12552. * @param {Number} height The new height that was set
  12553. * @param {Number} oldWidth The previous width
  12554. * @param {Number} oldHeight The previous height
  12555. */
  12556. 'resize',
  12557. /**
  12558. * @event move
  12559. * Fires after the component is moved.
  12560. * @param {Ext.Component} this
  12561. * @param {Number} x The new x position
  12562. * @param {Number} y The new y position
  12563. */
  12564. 'move',
  12565. /**
  12566. * @event focus
  12567. * Fires when this Component receives focus.
  12568. * @param {Ext.Component} this
  12569. * @param {Ext.EventObject} The focus event.
  12570. */
  12571. 'focus',
  12572. /**
  12573. * @event blur
  12574. * Fires when this Component loses focus.
  12575. * @param {Ext.Component} this
  12576. * @param {Ext.EventObject} The blur event.
  12577. */
  12578. 'blur'
  12579. );
  12580. me.getId();
  12581. me.setupProtoEl();
  12582. // initComponent, beforeRender, or event handlers may have set the style or cls property since the protoEl was set up
  12583. // so we must apply styles and classes here too.
  12584. if (me.cls) {
  12585. me.initialCls = me.cls;
  12586. me.protoEl.addCls(me.cls);
  12587. }
  12588. if (me.style) {
  12589. me.initialStyle = me.style;
  12590. me.protoEl.setStyle(me.style);
  12591. }
  12592. me.mons = [];
  12593. me.renderData = me.renderData || {};
  12594. me.renderSelectors = me.renderSelectors || {};
  12595. if (me.plugins) {
  12596. me.plugins = [].concat(me.plugins);
  12597. me.constructPlugins();
  12598. }
  12599. // Hash of event "hasListeners" flags.
  12600. // For repeated events in time-critical code, the firing code should use
  12601. // if (!me.hasListeners.beforerender || me.fireEvent('beforerender', me) !== false) { //code... }
  12602. // Bubbling the events counts as one listener. initComponent may add listeners, so needs setting up now.
  12603. me.hasListeners = {};
  12604. me.initComponent();
  12605. // ititComponent gets a chance to change the id property before registering
  12606. Ext.ComponentManager.register(me);
  12607. // Dont pass the config so that it is not applied to 'this' again
  12608. me.mixins.observable.constructor.call(me);
  12609. me.mixins.state.constructor.call(me, config);
  12610. // Save state on resize.
  12611. this.addStateEvents('resize');
  12612. // Move this into Observable?
  12613. if (me.plugins) {
  12614. me.plugins = [].concat(me.plugins);
  12615. for (i = 0, len = me.plugins.length; i < len; i++) {
  12616. me.plugins[i] = me.initPlugin(me.plugins[i]);
  12617. }
  12618. }
  12619. me.loader = me.getLoader();
  12620. if (me.renderTo) {
  12621. me.render(me.renderTo);
  12622. // EXTJSIV-1935 - should be a way to do afterShow or something, but that
  12623. // won't work. Likewise, rendering hidden and then showing (w/autoShow) has
  12624. // implications to afterRender so we cannot do that.
  12625. }
  12626. if (me.autoShow) {
  12627. me.show();
  12628. }
  12629. if (Ext.isDefined(me.disabledClass)) {
  12630. if (Ext.isDefined(Ext.global.console)) {
  12631. Ext.global.console.warn('Ext.Component: disabledClass has been deprecated. Please use disabledCls.');
  12632. }
  12633. me.disabledCls = me.disabledClass;
  12634. delete me.disabledClass;
  12635. }
  12636. },
  12637. initComponent: function () {
  12638. // This is called again here to allow derived classes to add plugin configs to the
  12639. // plugins array before calling down to this, the base initComponent.
  12640. this.constructPlugins();
  12641. // this will properly (ignore or) constrain the configured width/height to their
  12642. // min/max values for consistency.
  12643. this.setSize(this.width, this.height);
  12644. },
  12645. /**
  12646. * The supplied default state gathering method for the AbstractComponent class.
  12647. *
  12648. * This method returns dimension settings such as `flex`, `anchor`, `width` and `height` along with `collapsed`
  12649. * state.
  12650. *
  12651. * Subclasses which implement more complex state should call the superclass's implementation, and apply their state
  12652. * to the result if this basic state is to be saved.
  12653. *
  12654. * Note that Component state will only be saved if the Component has a {@link #stateId} and there as a StateProvider
  12655. * configured for the document.
  12656. *
  12657. * @return {Object}
  12658. */
  12659. getState: function() {
  12660. var me = this,
  12661. state = null,
  12662. sizeModel = me.getSizeModel();
  12663. if (sizeModel.width.configured) {
  12664. state = me.addPropertyToState(state, 'width');
  12665. }
  12666. if (sizeModel.height.configured) {
  12667. state = me.addPropertyToState(state, 'height');
  12668. }
  12669. return state;
  12670. },
  12671. /**
  12672. * Save a property to the given state object if it is not its default or configured
  12673. * value.
  12674. *
  12675. * @param {Object} state The state object
  12676. * @param {String} propName The name of the property on this object to save.
  12677. * @param {String} [value] The value of the state property (defaults to `this[propName]`).
  12678. * @return {Boolean} The state object or a new object if state was null and the property
  12679. * was saved.
  12680. * @protected
  12681. */
  12682. addPropertyToState: function (state, propName, value) {
  12683. var me = this,
  12684. len = arguments.length;
  12685. // If the property is inherited, it is a default and we don't want to save it to
  12686. // the state, however if we explicitly specify a value, always save it
  12687. if (len == 3 || me.hasOwnProperty(propName)) {
  12688. if (len < 3) {
  12689. value = me[propName];
  12690. }
  12691. // If the property has the same value as was initially configured, again, we
  12692. // don't want to save it.
  12693. if (value !== me.initialConfig[propName]) {
  12694. (state || (state = {}))[propName] = value;
  12695. }
  12696. }
  12697. return state;
  12698. },
  12699. show: Ext.emptyFn,
  12700. animate: function(animObj) {
  12701. var me = this,
  12702. hasToWidth,
  12703. hasToHeight,
  12704. toHeight,
  12705. toWidth,
  12706. to,
  12707. clearWidth,
  12708. clearHeight;
  12709. animObj = animObj || {};
  12710. to = animObj.to || {};
  12711. if (Ext.fx.Manager.hasFxBlock(me.id)) {
  12712. return me;
  12713. }
  12714. hasToWidth = Ext.isDefined(to.width);
  12715. if (hasToWidth) {
  12716. toWidth = Ext.Number.constrain(to.width, me.minWidth, me.maxWidth);
  12717. }
  12718. hasToHeight = Ext.isDefined(to.height);
  12719. if (hasToHeight) {
  12720. toHeight = Ext.Number.constrain(to.height, me.minHeight, me.maxHeight);
  12721. }
  12722. // Special processing for animating Component dimensions.
  12723. if (!animObj.dynamic && (hasToWidth || hasToHeight)) {
  12724. var curWidth = (animObj.from ? animObj.from.width : undefined) || me.getWidth(),
  12725. w = curWidth,
  12726. curHeight = (animObj.from ? animObj.from.height : undefined) || me.getHeight(),
  12727. h = curHeight,
  12728. needsResize = false;
  12729. if (hasToHeight && toHeight > curHeight) {
  12730. h = toHeight;
  12731. needsResize = true;
  12732. }
  12733. if (hasToWidth && toWidth > curWidth) {
  12734. w = toWidth;
  12735. needsResize = true;
  12736. }
  12737. // If any dimensions are being increased, we must resize the internal structure
  12738. // of the Component, but then clip it by sizing its encapsulating element back to original dimensions.
  12739. // The animation will then progressively reveal the larger content.
  12740. if (needsResize) {
  12741. clearWidth = !Ext.isNumber(me.width);
  12742. clearHeight = !Ext.isNumber(me.height);
  12743. me.setSize(w, h);
  12744. me.el.setSize(curWidth, curHeight);
  12745. if (clearWidth) {
  12746. delete me.width;
  12747. }
  12748. if (clearHeight) {
  12749. delete me.height;
  12750. }
  12751. }
  12752. if (hasToWidth) {
  12753. to.width = toWidth;
  12754. }
  12755. if (hasToHeight) {
  12756. to.height = toHeight;
  12757. }
  12758. }
  12759. return me.mixins.animate.animate.apply(me, arguments);
  12760. },
  12761. onHide: function() {
  12762. this.updateLayout({ isRoot: false });
  12763. },
  12764. onShow : function() {
  12765. this.updateLayout({ isRoot: false });
  12766. },
  12767. constructPlugin: function(plugin) {
  12768. if (plugin.ptype && typeof plugin.init != 'function') {
  12769. plugin.cmp = this;
  12770. plugin = Ext.PluginManager.create(plugin);
  12771. }
  12772. else if (typeof plugin == 'string') {
  12773. plugin = Ext.PluginManager.create({
  12774. ptype: plugin,
  12775. cmp: this
  12776. });
  12777. }
  12778. return plugin;
  12779. },
  12780. /**
  12781. * Ensures that the plugins array contains fully constructed plugin instances. This converts any configs into their
  12782. * appropriate instances.
  12783. */
  12784. constructPlugins: function() {
  12785. var me = this,
  12786. plugins = me.plugins,
  12787. i, len;
  12788. if (plugins) {
  12789. for (i = 0, len = plugins.length; i < len; i++) {
  12790. // this just returns already-constructed plugin instances...
  12791. plugins[i] = me.constructPlugin(plugins[i]);
  12792. }
  12793. }
  12794. },
  12795. // @private
  12796. initPlugin : function(plugin) {
  12797. plugin.init(this);
  12798. return plugin;
  12799. },
  12800. /**
  12801. * @private
  12802. * Injected as an override by Ext.Aria.initialize
  12803. */
  12804. updateAria: Ext.emptyFn,
  12805. /**
  12806. * Called by Component#doAutoRender
  12807. *
  12808. * Register a Container configured `floating: true` with this Component's {@link Ext.ZIndexManager ZIndexManager}.
  12809. *
  12810. * Components added in ths way will not participate in any layout, but will be rendered
  12811. * upon first show in the way that {@link Ext.window.Window Window}s are.
  12812. */
  12813. registerFloatingItem: function(cmp) {
  12814. var me = this;
  12815. if (!me.floatingItems) {
  12816. me.floatingItems = new Ext.ZIndexManager(me);
  12817. }
  12818. me.floatingItems.register(cmp);
  12819. },
  12820. unregisterFloatingItem: function(cmp) {
  12821. var me = this;
  12822. if (me.floatingItems) {
  12823. me.floatingItems.unregister(cmp);
  12824. }
  12825. },
  12826. layoutSuspendCount: 0,
  12827. suspendLayouts: function () {
  12828. var me = this;
  12829. if (!me.rendered) {
  12830. return;
  12831. }
  12832. if (++me.layoutSuspendCount == 1) {
  12833. me.suspendLayout = true;
  12834. }
  12835. },
  12836. resumeLayouts: function (flushOptions) {
  12837. var me = this;
  12838. if (!me.rendered) {
  12839. return;
  12840. }
  12841. if (! --me.layoutSuspendCount) {
  12842. me.suspendLayout = false;
  12843. if (flushOptions && !me.isLayoutSuspended()) {
  12844. me.updateLayout(flushOptions);
  12845. }
  12846. }
  12847. },
  12848. setupProtoEl: function() {
  12849. var me = this,
  12850. cls = [ me.baseCls, me.getComponentLayout().targetCls ];
  12851. if (Ext.isDefined(me.cmpCls)) {
  12852. if (Ext.isDefined(Ext.global.console)) {
  12853. Ext.global.console.warn('Ext.Component: cmpCls has been deprecated. Please use componentCls.');
  12854. }
  12855. me.componentCls = me.cmpCls;
  12856. delete me.cmpCls;
  12857. }
  12858. if (me.componentCls) {
  12859. cls.push(me.componentCls);
  12860. } else {
  12861. me.componentCls = me.baseCls;
  12862. }
  12863. me.protoEl = new Ext.util.ProtoElement({
  12864. cls: cls.join(' ') // in case any of the parts have multiple classes
  12865. });
  12866. },
  12867. /**
  12868. * Sets the UI for the component. This will remove any existing UIs on the component. It will also loop through any
  12869. * uiCls set on the component and rename them so they include the new UI
  12870. * @param {String} ui The new UI for the component
  12871. */
  12872. setUI: function(ui) {
  12873. var me = this,
  12874. oldUICls = Ext.Array.clone(me.uiCls),
  12875. newUICls = [],
  12876. classes = [],
  12877. cls,
  12878. i;
  12879. //loop through all exisiting uiCls and update the ui in them
  12880. for (i = 0; i < oldUICls.length; i++) {
  12881. cls = oldUICls[i];
  12882. classes = classes.concat(me.removeClsWithUI(cls, true));
  12883. newUICls.push(cls);
  12884. }
  12885. if (classes.length) {
  12886. me.removeCls(classes);
  12887. }
  12888. //remove the UI from the element
  12889. me.removeUIFromElement();
  12890. //set the UI
  12891. me.ui = ui;
  12892. //add the new UI to the elemend
  12893. me.addUIToElement();
  12894. //loop through all exisiting uiCls and update the ui in them
  12895. classes = [];
  12896. for (i = 0; i < newUICls.length; i++) {
  12897. cls = newUICls[i];
  12898. classes = classes.concat(me.addClsWithUI(cls, true));
  12899. }
  12900. if (classes.length) {
  12901. me.addCls(classes);
  12902. }
  12903. },
  12904. /**
  12905. * Adds