You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.2 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. Ext.ns("Ext.ux");
  16. /**
  17. * @class Ext.ux.FieldReplicator
  18. * <p>A plugin for Field Components which creates clones of the Field for as
  19. * long as the user keeps filling them. Leaving the final one blank ends the repeating series.</p>
  20. * <p>Usage:</p>
  21. * <pre><code>
  22. {
  23. xtype: 'combo',
  24. plugins: [ Ext.ux.FieldReplicator ],
  25. triggerAction: 'all',
  26. fieldLabel: 'Select recipient',
  27. store: recipientStore
  28. }
  29. * </code></pre>
  30. */
  31. Ext.ux.FieldReplicator = {
  32. init: function(f) {
  33. f.replicator = this;
  34. f.enableKeyEvents = true;
  35. f.on('change', this.onChange, this);
  36. f.onKeyDown = f.onKeyDown.createInterceptor(this.onKeyDown);
  37. },
  38. // If tabbing out and the change event will be fired, flag that
  39. // the change handler must focus the correct sibling Field.
  40. onKeyDown: function(e) {
  41. if ((e.getKey() == Ext.EventObject.TAB) && (String(this.startValue) !== String(this.getValue()))) {
  42. if (e.shiftKey) {
  43. this.focusPrev = true;
  44. } else if (!e.shiftKey && !e.altKey) {
  45. this.focusNext = true;
  46. }
  47. }
  48. },
  49. // Handle the field either being changed to blank or from blank.
  50. onChange: function(f, n, o) {
  51. // Ensure that "change" is only fired once.
  52. f.startValue = n;
  53. var c = f.ownerCt, l,
  54. ps = f.previousSibling(),
  55. ns = f.nextSibling();
  56. if (Ext.isEmpty(n)) {
  57. if (!Ext.isEmpty(o)) {
  58. // The Field has been blanked, and it is not the only one left, remove it
  59. if ((ps && (ps.replicator === this)) || (ns && (ns.replicator === this))) {
  60. l = f.findParentBy(function(p) {
  61. return !Ext.isDefined(p.ownerCt);
  62. });
  63. c.remove(f);
  64. l.doLayout();
  65. }
  66. }
  67. } else {
  68. if (Ext.isEmpty(o)) {
  69. // Field filled, insert a clone as the next sibling
  70. ns = new f.constructor(f.cloneConfig());
  71. c.insert(c.items.indexOf(f) + 1, ns);
  72. c.doLayout();
  73. l = f.findParentBy(function(p) {
  74. return !Ext.isDefined(p.ownerCt);
  75. });
  76. l.doLayout();
  77. }
  78. }
  79. if (f.focusPrev) {
  80. delete f.focusPrev;
  81. ps.focus(false, true);
  82. } else if (f.focusNext) {
  83. delete f.focusNext;
  84. ns.focus(false, true);
  85. }
  86. }
  87. };