Editor2.html 17 KB

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