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.

83 lines
2.6 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*!
  2. * Ext JS Library 3.3.1
  3. * Copyright(c) 2006-2010 Sencha Inc.
  4. * licensing@sencha.com
  5. * http://www.sencha.com/license
  6. */
  7. Ext.ns("Ext.ux");
  8. /**
  9. * @class Ext.ux.FieldReplicator
  10. * <p>A plugin for Field Components which creates clones of the Field for as
  11. * long as the user keeps filling them. Leaving the final one blank ends the repeating series.</p>
  12. * <p>Usage:</p>
  13. * <pre><code>
  14. {
  15. xtype: 'combo',
  16. plugins: [ Ext.ux.FieldReplicator ],
  17. triggerAction: 'all',
  18. fieldLabel: 'Select recipient',
  19. store: recipientStore
  20. }
  21. * </code></pre>
  22. */
  23. Ext.ux.FieldReplicator = {
  24. init: function(f) {
  25. f.replicator = this;
  26. f.enableKeyEvents = true;
  27. f.on('change', this.onChange, this);
  28. f.onKeyDown = f.onKeyDown.createInterceptor(this.onKeyDown);
  29. },
  30. // If tabbing out and the change event will be fired, flag that
  31. // the change handler must focus the correct sibling Field.
  32. onKeyDown: function(e) {
  33. if ((e.getKey() == Ext.EventObject.TAB) && (String(this.startValue) !== String(this.getValue()))) {
  34. if (e.shiftKey) {
  35. this.focusPrev = true;
  36. } else if (!e.shiftKey && !e.altKey) {
  37. this.focusNext = true;
  38. }
  39. }
  40. },
  41. // Handle the field either being changed to blank or from blank.
  42. onChange: function(f, n, o) {
  43. // Ensure that "change" is only fired once.
  44. f.startValue = n;
  45. var c = f.ownerCt, l,
  46. ps = f.previousSibling(),
  47. ns = f.nextSibling();
  48. if (Ext.isEmpty(n)) {
  49. if (!Ext.isEmpty(o)) {
  50. // The Field has been blanked, and it is not the only one left, remove it
  51. if ((ps && (ps.replicator === this)) || (ns && (ns.replicator === this))) {
  52. l = f.findParentBy(function(p) {
  53. return !Ext.isDefined(p.ownerCt);
  54. });
  55. c.remove(f);
  56. l.doLayout();
  57. }
  58. }
  59. } else {
  60. if (Ext.isEmpty(o)) {
  61. // Field filled, insert a clone as the next sibling
  62. ns = new f.constructor(f.cloneConfig());
  63. c.insert(c.items.indexOf(f) + 1, ns);
  64. c.doLayout();
  65. l = f.findParentBy(function(p) {
  66. return !Ext.isDefined(p.ownerCt);
  67. });
  68. l.doLayout();
  69. }
  70. }
  71. if (f.focusPrev) {
  72. delete f.focusPrev;
  73. ps.focus(false, true);
  74. } else if (f.focusNext) {
  75. delete f.focusNext;
  76. ns.focus(false, true);
  77. }
  78. }
  79. };