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