FieldReplicator.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 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 published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. /**
  10. * @class Ext.ux.FieldReplicator
  11. * <p>A plugin for Field Components which creates clones of the Field for as
  12. * long as the user keeps filling them. Leaving the final one blank ends the repeating series.</p>
  13. * <p>Usage:</p>
  14. * <pre><code>
  15. {
  16. xtype: 'combo',
  17. plugins: [ Ext.ux.FieldReplicator ],
  18. triggerAction: 'all',
  19. fieldLabel: 'Select recipient',
  20. store: recipientStore
  21. }
  22. * </code></pre>
  23. */
  24. Ext.define('Ext.ux.FieldReplicator', {
  25. singleton: true,
  26. init: function(field) {
  27. // Assign the field an id grouping it with fields cloned from it. If it already
  28. // has an id that means it is itself a clone.
  29. if (!field.replicatorId) {
  30. field.replicatorId = Ext.id();
  31. }
  32. field.on('blur', this.onBlur, this);
  33. },
  34. onBlur: function(field) {
  35. var ownerCt = field.ownerCt,
  36. replicatorId = field.replicatorId,
  37. isEmpty = Ext.isEmpty(field.getRawValue()),
  38. siblings = ownerCt.query('[replicatorId=' + replicatorId + ']'),
  39. isLastInGroup = siblings[siblings.length - 1] === field,
  40. clone, idx;
  41. // If a field before the final one was blanked out, remove it
  42. if (isEmpty && !isLastInGroup) {
  43. Ext.Function.defer(field.destroy, 10, field); //delay to allow tab key to move focus first
  44. }
  45. // If the field is the last in the list and has a value, add a cloned field after it
  46. else if(!isEmpty && isLastInGroup) {
  47. clone = field.cloneConfig({replicatorId: replicatorId});
  48. idx = ownerCt.items.indexOf(field);
  49. ownerCt.add(idx + 1, clone);
  50. }
  51. }
  52. });