/ext-4.1.0_b3/src/Editor.js

https://bitbucket.org/srogerf/javascript · JavaScript · 488 lines · 216 code · 63 blank · 209 comment · 37 complexity · 408e461b30839f7233829377cb04d553 MD5 · raw file

  1. /**
  2. * The Editor class is used to provide inline editing for elements on the page. The editor
  3. * is backed by a {@link Ext.form.field.Field} that will be displayed to edit the underlying content.
  4. * The editor is a floating Component, when the editor is shown it is automatically aligned to
  5. * display over the top of the bound element it is editing. The Editor contains several options
  6. * for how to handle key presses:
  7. *
  8. * - {@link #completeOnEnter}
  9. * - {@link #cancelOnEsc}
  10. * - {@link #swallowKeys}
  11. *
  12. * It also has options for how to use the value once the editor has been activated:
  13. *
  14. * - {@link #revertInvalid}
  15. * - {@link #ignoreNoChange}
  16. * - {@link #updateEl}
  17. *
  18. * Sample usage:
  19. *
  20. * var editor = new Ext.Editor({
  21. * updateEl: true, // update the innerHTML of the bound element when editing completes
  22. * field: {
  23. * xtype: 'textfield'
  24. * }
  25. * });
  26. * var el = Ext.get('my-text'); // The element to 'edit'
  27. * editor.startEdit(el); // The value of the field will be taken as the innerHTML of the element.
  28. *
  29. * {@img Ext.Editor/Ext.Editor.png Ext.Editor component}
  30. *
  31. */
  32. Ext.define('Ext.Editor', {
  33. /* Begin Definitions */
  34. extend: 'Ext.container.Container',
  35. alias: 'widget.editor',
  36. requires: ['Ext.layout.container.Editor'],
  37. /* End Definitions */
  38. layout: 'editor',
  39. /**
  40. * @cfg {Ext.form.field.Field} field
  41. * The Field object (or descendant) or config object for field
  42. */
  43. /**
  44. * @cfg {Boolean} allowBlur
  45. * True to {@link #completeEdit complete the editing process} if in edit mode when the
  46. * field is blurred.
  47. */
  48. allowBlur: true,
  49. /**
  50. * @cfg {Boolean/Object} autoSize
  51. * True for the editor to automatically adopt the size of the underlying field. Otherwise, an object
  52. * can be passed to indicate where to get each dimension. The available properties are 'boundEl' and
  53. * 'field'. If a dimension is not specified, it will use the underlying height/width specified on
  54. * the editor object.
  55. * Examples:
  56. *
  57. * autoSize: true // The editor will be sized to the height/width of the field
  58. *
  59. * height: 21,
  60. * autoSize: {
  61. * width: 'boundEl' // The width will be determined by the width of the boundEl, the height from the editor (21)
  62. * }
  63. *
  64. * autoSize: {
  65. * width: 'field', // Width from the field
  66. * height: 'boundEl' // Height from the boundEl
  67. * }
  68. */
  69. /**
  70. * @cfg {Boolean} revertInvalid
  71. * True to automatically revert the field value and cancel the edit when the user completes an edit and the field
  72. * validation fails
  73. */
  74. revertInvalid: true,
  75. /**
  76. * @cfg {Boolean} [ignoreNoChange=false]
  77. * True to skip the edit completion process (no save, no events fired) if the user completes an edit and
  78. * the value has not changed. Applies only to string values - edits for other data types
  79. * will never be ignored.
  80. */
  81. /**
  82. * @cfg {Boolean} [hideEl=true]
  83. * False to keep the bound element visible while the editor is displayed
  84. */
  85. /**
  86. * @cfg {Object} value
  87. * The data value of the underlying field
  88. */
  89. value : '',
  90. /**
  91. * @cfg {String} alignment
  92. * The position to align to (see {@link Ext.Element#alignTo} for more details).
  93. */
  94. alignment: 'c-c?',
  95. /**
  96. * @cfg {Number[]} offsets
  97. * The offsets to use when aligning (see {@link Ext.Element#alignTo} for more details.
  98. */
  99. offsets: [0, 0],
  100. /**
  101. * @cfg {Boolean/String} shadow
  102. * "sides" for sides/bottom only, "frame" for 4-way shadow, and "drop" for bottom-right shadow.
  103. */
  104. shadow : 'frame',
  105. /**
  106. * @cfg {Boolean} constrain
  107. * True to constrain the editor to the viewport
  108. */
  109. constrain : false,
  110. /**
  111. * @cfg {Boolean} swallowKeys
  112. * Handle the keydown/keypress events so they don't propagate
  113. */
  114. swallowKeys : true,
  115. /**
  116. * @cfg {Boolean} completeOnEnter
  117. * True to complete the edit when the enter key is pressed.
  118. */
  119. completeOnEnter : true,
  120. /**
  121. * @cfg {Boolean} cancelOnEsc
  122. * True to cancel the edit when the escape key is pressed.
  123. */
  124. cancelOnEsc : true,
  125. /**
  126. * @cfg {Boolean} updateEl
  127. * True to update the innerHTML of the bound element when the update completes
  128. */
  129. updateEl : false,
  130. /**
  131. * @cfg {String/HTMLElement/Ext.Element} [parentEl=document.body]
  132. * An element to render to.
  133. */
  134. // private overrides
  135. hidden: true,
  136. baseCls: Ext.baseCSSPrefix + 'editor',
  137. initComponent : function() {
  138. var me = this,
  139. field = me.field = Ext.ComponentManager.create(me.field, 'textfield');
  140. Ext.apply(field, {
  141. inEditor: true,
  142. msgTarget: field.msgTarget == 'title' ? 'title' : 'qtip'
  143. });
  144. me.mon(field, {
  145. scope: me,
  146. blur: {
  147. fn: me.onFieldBlur,
  148. // slight delay to avoid race condition with startEdits (e.g. grid view refresh)
  149. delay: 1
  150. },
  151. specialkey: me.onSpecialKey
  152. });
  153. if (field.grow) {
  154. me.mon(field, 'autosize', me.onFieldAutosize, me, {delay: 1});
  155. }
  156. me.floating = {
  157. constrain: me.constrain
  158. };
  159. me.items = field;
  160. me.callParent(arguments);
  161. me.addEvents(
  162. /**
  163. * @event beforestartedit
  164. * Fires when editing is initiated, but before the value changes. Editing can be canceled by returning
  165. * false from the handler of this event.
  166. * @param {Ext.Editor} this
  167. * @param {Ext.Element} boundEl The underlying element bound to this editor
  168. * @param {Object} value The field value being set
  169. */
  170. 'beforestartedit',
  171. /**
  172. * @event startedit
  173. * Fires when this editor is displayed
  174. * @param {Ext.Editor} this
  175. * @param {Ext.Element} boundEl The underlying element bound to this editor
  176. * @param {Object} value The starting field value
  177. */
  178. 'startedit',
  179. /**
  180. * @event beforecomplete
  181. * Fires after a change has been made to the field, but before the change is reflected in the underlying
  182. * field. Saving the change to the field can be canceled by returning false from the handler of this event.
  183. * Note that if the value has not changed and ignoreNoChange = true, the editing will still end but this
  184. * event will not fire since no edit actually occurred.
  185. * @param {Ext.Editor} this
  186. * @param {Object} value The current field value
  187. * @param {Object} startValue The original field value
  188. */
  189. 'beforecomplete',
  190. /**
  191. * @event complete
  192. * Fires after editing is complete and any changed value has been written to the underlying field.
  193. * @param {Ext.Editor} this
  194. * @param {Object} value The current field value
  195. * @param {Object} startValue The original field value
  196. */
  197. 'complete',
  198. /**
  199. * @event canceledit
  200. * Fires after editing has been canceled and the editor's value has been reset.
  201. * @param {Ext.Editor} this
  202. * @param {Object} value The user-entered field value that was discarded
  203. * @param {Object} startValue The original field value that was set back into the editor after cancel
  204. */
  205. 'canceledit',
  206. /**
  207. * @event specialkey
  208. * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed. You can check
  209. * {@link Ext.EventObject#getKey} to determine which key was pressed.
  210. * @param {Ext.Editor} this
  211. * @param {Ext.form.field.Field} The field attached to this editor
  212. * @param {Ext.EventObject} event The event object
  213. */
  214. 'specialkey'
  215. );
  216. },
  217. // private
  218. onFieldAutosize: function(){
  219. this.updateLayout();
  220. },
  221. // private
  222. afterRender : function(ct, position) {
  223. var me = this,
  224. field = me.field,
  225. inputEl = field.inputEl;
  226. me.callParent(arguments);
  227. // Ensure the field doesn't get submitted as part of any form
  228. if (inputEl) {
  229. inputEl.dom.name = '';
  230. if (me.swallowKeys) {
  231. inputEl.swallowEvent([
  232. 'keypress', // *** Opera
  233. 'keydown' // *** all other browsers
  234. ]);
  235. }
  236. }
  237. },
  238. // private
  239. onSpecialKey : function(field, event) {
  240. var me = this,
  241. key = event.getKey(),
  242. complete = me.completeOnEnter && key == event.ENTER,
  243. cancel = me.cancelOnEsc && key == event.ESC;
  244. if (complete || cancel) {
  245. event.stopEvent();
  246. // Must defer this slightly to prevent exiting edit mode before the field's own
  247. // key nav can handle the enter key, e.g. selecting an item in a combobox list
  248. Ext.defer(function() {
  249. if (complete) {
  250. me.completeEdit();
  251. } else {
  252. me.cancelEdit();
  253. }
  254. if (field.triggerBlur) {
  255. field.triggerBlur();
  256. }
  257. }, 10);
  258. }
  259. me.fireEvent('specialkey', me, field, event);
  260. },
  261. /**
  262. * Starts the editing process and shows the editor.
  263. * @param {String/HTMLElement/Ext.Element} el The element to edit
  264. * @param {String} value (optional) A value to initialize the editor with. If a value is not provided, it defaults
  265. * to the innerHTML of el.
  266. */
  267. startEdit : function(el, value) {
  268. var me = this,
  269. field = me.field;
  270. me.completeEdit();
  271. me.boundEl = Ext.get(el);
  272. value = Ext.isDefined(value) ? value : me.boundEl.dom.innerHTML;
  273. if (!me.rendered) {
  274. me.render(me.parentEl || document.body);
  275. }
  276. if (me.fireEvent('beforestartedit', me, me.boundEl, value) !== false) {
  277. me.startValue = value;
  278. me.show();
  279. // temporarily suspend events on field to prevent the "change" event from firing when reset() and setValue() are called
  280. field.suspendEvents();
  281. field.reset();
  282. field.setValue(value);
  283. field.resumeEvents();
  284. me.realign(true);
  285. field.focus(false, 10);
  286. if (field.autoSize) {
  287. field.autoSize();
  288. }
  289. me.editing = true;
  290. }
  291. },
  292. /**
  293. * Realigns the editor to the bound field based on the current alignment config value.
  294. * @param {Boolean} autoSize (optional) True to size the field to the dimensions of the bound element.
  295. */
  296. realign : function(autoSize) {
  297. var me = this;
  298. if (autoSize === true) {
  299. me.updateLayout();
  300. }
  301. me.alignTo(me.boundEl, me.alignment, me.offsets);
  302. },
  303. /**
  304. * Ends the editing process, persists the changed value to the underlying field, and hides the editor.
  305. * @param {Boolean} [remainVisible=false] Override the default behavior and keep the editor visible after edit
  306. */
  307. completeEdit : function(remainVisible) {
  308. var me = this,
  309. field = me.field,
  310. value;
  311. if (!me.editing) {
  312. return;
  313. }
  314. // Assert combo values first
  315. if (field.assertValue) {
  316. field.assertValue();
  317. }
  318. value = me.getValue();
  319. if (!field.isValid()) {
  320. if (me.revertInvalid !== false) {
  321. me.cancelEdit(remainVisible);
  322. }
  323. return;
  324. }
  325. if (String(value) === String(me.startValue) && me.ignoreNoChange) {
  326. me.hideEdit(remainVisible);
  327. return;
  328. }
  329. if (me.fireEvent('beforecomplete', me, value, me.startValue) !== false) {
  330. // Grab the value again, may have changed in beforecomplete
  331. value = me.getValue();
  332. if (me.updateEl && me.boundEl) {
  333. me.boundEl.update(value);
  334. }
  335. me.hideEdit(remainVisible);
  336. me.fireEvent('complete', me, value, me.startValue);
  337. }
  338. },
  339. // private
  340. onShow : function() {
  341. var me = this;
  342. me.callParent(arguments);
  343. if (me.hideEl !== false) {
  344. me.boundEl.hide();
  345. }
  346. me.fireEvent("startedit", me.boundEl, me.startValue);
  347. },
  348. /**
  349. * Cancels the editing process and hides the editor without persisting any changes. The field value will be
  350. * reverted to the original starting value.
  351. * @param {Boolean} [remainVisible=false] Override the default behavior and keep the editor visible after cancel
  352. */
  353. cancelEdit : function(remainVisible) {
  354. var me = this,
  355. startValue = me.startValue,
  356. field = me.field,
  357. value;
  358. if (me.editing) {
  359. value = me.getValue();
  360. // temporarily suspend events on field to prevent the "change" event from firing when setValue() is called
  361. field.suspendEvents();
  362. me.setValue(startValue);
  363. field.resumeEvents();
  364. me.hideEdit(remainVisible);
  365. me.fireEvent('canceledit', me, value, startValue);
  366. }
  367. },
  368. // private
  369. hideEdit: function(remainVisible) {
  370. if (remainVisible !== true) {
  371. this.editing = false;
  372. this.hide();
  373. }
  374. },
  375. // private
  376. onFieldBlur : function() {
  377. var me = this;
  378. // selectSameEditor flag allows the same editor to be started without onFieldBlur firing on itself
  379. if(me.allowBlur === true && me.editing && me.selectSameEditor !== true) {
  380. me.completeEdit();
  381. }
  382. },
  383. // private
  384. onHide : function() {
  385. var me = this,
  386. field = me.field;
  387. if (me.editing) {
  388. me.completeEdit();
  389. return;
  390. }
  391. if (field.hasFocus) {
  392. field.blur();
  393. }
  394. if (field.collapse) {
  395. field.collapse();
  396. }
  397. //field.hide();
  398. if (me.hideEl !== false) {
  399. me.boundEl.show();
  400. }
  401. me.callParent(arguments);
  402. },
  403. /**
  404. * Sets the data value of the editor
  405. * @param {Object} value Any valid value supported by the underlying field
  406. */
  407. setValue : function(value) {
  408. this.field.setValue(value);
  409. },
  410. /**
  411. * Gets the data value of the editor
  412. * @return {Object} The data value
  413. */
  414. getValue : function() {
  415. return this.field.getValue();
  416. },
  417. beforeDestroy : function() {
  418. var me = this;
  419. Ext.destroy(me.field);
  420. delete me.field;
  421. delete me.parentEl;
  422. delete me.boundEl;
  423. me.callParent(arguments);
  424. }
  425. });