widgets/ComboBoxLister.js
[Pman.Core] / widgets / ComboBoxLister.js
1 //<script type="text/javascript">
2 /**
3  * 
4  * A facebook style adder... for lists of email / people etc...
5  * 
6  * 
7  * 
8  */
9
10
11 Ext.form.ComboBoxLister = function(config){
12     
13     Ext.form.ComboBoxLister.superclass.constructor.call(this, config);
14     this.items = new Roo.util.MixedCollection(false);
15     this.on('select', function(cb, rec, ix) {
16         this.addItem(rec.data);
17         this.setValue('');
18         this.el.dom.value = '';
19         //cb.lastData = rec.data;
20         // add to list
21         
22     });
23     
24 }
25  
26 Ext.extend(Ext.form.ComboBoxLister, Ext.form.ComboBox, { 
27     lastData : false,
28     items  : false,
29     nameField : 'name',
30     tipField : 'email',
31     idField : 'id',
32     renderer : false,
33     hiddenName : false, // set this if you want a , sperated list of values in it for form posting..
34     hiddenListName : false,
35     hiddenEl : false,
36     boxWidth : 200, // use to set the box around the entry..
37     allowBlank: true,
38     disableClear: true,
39     //validateValue : function() { return true; }, // all values are ok!
40     //onAddClick: function() { },
41     
42     onRender : function(ct, position) 
43     {
44          
45         Ext.form.ComboBoxLister.superclass.onRender.call(this, ct, position); 
46         this.wrap.addClass('p-cblist-grp');
47         var cbwrap = this.wrap.createChild(
48             {tag: 'div', cls: 'p-cblist-cb'},
49             this.el.dom
50         );  
51         if (this.hiddenListName) {
52              
53             this.hiddenEl = this.wrap.createChild({
54                 tag: 'input',  type:'hidden' , name: this.hiddenListName, value : ''
55             });
56          //   this.el.dom.removeAttribute("name");
57         }
58         
59         this.outerWrap = this.wrap;
60         this.wrap = cbwrap;
61         this.outerWrap.setWidth(this.boxWidth);
62         this.outerWrap.dom.removeChild(this.el.dom);
63         this.wrap.dom.appendChild(this.el.dom);
64         this.outerWrap.dom.removeChild(this.trigger.dom);
65         this.wrap.dom.appendChild(this.trigger.dom);
66          
67         this.trigger.setStyle('position','relative');
68         this.trigger.setStyle('left', '0');
69         this.trigger.setStyle('top', '7px');
70         //this.trigger.setStyle('vertical-align', 'top');
71         if (this.adder) {
72             
73         
74             this.adder = this.outerWrap.createChild(
75                 {tag: 'img', src: Ext.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});  
76             var _t = this;
77             this.adder.on('click', function(e) {
78                 _t.fireEvent('adderclick', this, e);
79             }, _t);
80         }
81         //var _t = this;
82         //this.adder.on('click', this.onAddClick, _t);
83         
84     },
85     
86     
87     onResize: function(w, h){
88         Roo.form.ComboBox.superclass.onResize.apply(this, arguments);
89         
90         if(typeof w != 'number'){
91             // we do not handle it!?!?
92             return;
93         }
94         var tw = this.trigger.getWidth();
95         tw += this.addicon ? this.addicon.getWidth() : 0;
96         tw += this.editicon ? this.editicon.getWidth() : 0;
97         var x = w - tw;
98         this.el.setWidth( this.adjustWidth('input', x));
99             
100         this.trigger.setStyle('left', '0px');
101         
102         if(this.list && this.listWidth === undefined){
103             var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
104             this.list.setWidth(lw);
105             this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
106         }
107         
108     
109         
110     },
111     
112     addItem: function(rec)
113     {
114         if (this.items.indexOfKey(rec[this.idField]) > -1) {
115             //console.log("GOT " + rec.data.id);
116             return;
117         }
118         
119         var x = new Ext.form.ComboBoxLister.Item({
120             //id : rec[this.idField],
121             data : rec,
122             nameField : this.nameField,
123             tipField : this.tipField,
124             cb : this
125         });
126         // use the 
127         this.items.add(rec[this.idField],x);
128         // add it before the element..
129         this.updateHiddenEl();
130         x.render(this.outerWrap, this.wrap.dom);
131         // add the image handler..
132     },
133     
134     updateHiddenEl : function()
135     {
136         this.validate();
137         if (!this.hiddenEl) {
138             return;
139         }
140         var ar = [];
141         var idField = this.idField;
142         this.items.each(function(f) {
143             ar.push(f.data[idField]);
144            
145         });
146         this.hiddenEl.dom.value = ar.join(',');
147         this.validate();
148     },
149     
150     reset : function()
151     {
152         Roo.form.ComboBoxLister.superclass.reset.call(this); 
153         this.items.each(function(f) {
154            f.remove(); 
155         });
156         if (this.hiddenEl) {
157             this.hiddenEl.dom.value = '';
158         }
159         
160     },
161     getValue: function()
162     {
163         return this.hiddenEl ? this.hiddenEl.dom.value : '';
164     },
165     setValue: function(v) // not a valid action - must use addItems..
166     {
167         if (typeof(v) != 'object') {
168             return;
169         }
170         var _this = this;
171         Roo.each(v, function(l) {
172                 _this.addItem(l);
173         });
174         
175     },
176     validateValue : function(value){
177         return Roo.form.ComboBoxLister.superclass.validateValue.call(this, this.getValue());
178         
179     }
180     
181 });
182
183 Ext.form.ComboBoxLister.Item = function(config) {
184     config.id = Roo.id();
185     Roo.form.ComboBoxLister.Item.superclass.constructor.call(this, config);
186 }
187 Roo.extend(Roo.form.ComboBoxLister.Item, Roo.BoxComponent, {
188     data : {},
189     cb: false,
190     defaultAutoCreate : {tag: 'div', cls: 'p-cblist-item', cn : [ 
191             { tag: 'div' },
192             { tag: 'img', width:16, height : 16, src : Roo.BLANK_IMAGE_URL , align: 'center' }
193         ]
194         
195     },
196     
197  
198     onRender : function(ct, position){
199         Roo.form.Field.superclass.onRender.call(this, ct, position);
200         if(!this.el){
201             var cfg = this.getAutoCreate();
202             this.el = ct.createChild(cfg, position);
203         }
204         this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
205         
206         this.el.child('div').dom.innerHTML = this.cb.renderer ? 
207             this.cb.renderer(this.data) : String.format('{0}',this.data[this.nameField]);
208         this.el.child('div').dom.setAttribute('qtip', String.format('{0}',this.data[this.tipField]));
209         
210         this.el.child('img').on('click', this.remove, this);
211         
212     },
213    
214     remove : function()
215     {
216         
217         this.cb.items.remove(this);
218         this.el.child('img').un('click', this.remove, this);
219         this.el.remove();
220         this.cb.updateHiddenEl();
221     }
222     
223     
224 });