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('top', '2px');
68         //this.trigger.setStyle('vertical-align', 'top');
69         if (this.adder) {
70             
71         
72             this.adder = this.outerWrap.createChild(
73                 {tag: 'img', src: Ext.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});  
74             var _t = this;
75             this.adder.on('click', function(e) {
76                 _t.fireEvent('adderclick', this, e);
77             }, _t);
78         }
79         //var _t = this;
80         //this.adder.on('click', this.onAddClick, _t);
81         
82     },
83     
84     
85     onResize: function(w, h){
86         Roo.form.ComboBox.superclass.onResize.apply(this, arguments);
87         
88         if(typeof w != 'number'){
89             // we do not handle it!?!?
90             return;
91         }
92         var tw = this.trigger.getWidth();
93         tw += this.addicon ? this.addicon.getWidth() : 0;
94         tw += this.editicon ? this.editicon.getWidth() : 0;
95         var x = w - tw;
96         this.el.setWidth( this.adjustWidth('input', x));
97             
98         this.trigger.setStyle('left', w + 'px');
99         
100         if(this.list && this.listWidth === undefined){
101             var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
102             this.list.setWidth(lw);
103             this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
104         }
105         
106     
107         
108     },
109     
110     addItem: function(rec)
111     {
112         if (this.items.indexOfKey(rec[this.idField]) > -1) {
113             //console.log("GOT " + rec.data.id);
114             return;
115         }
116         
117         var x = new Ext.form.ComboBoxLister.Item({
118             //id : rec[this.idField],
119             data : rec,
120             nameField : this.nameField,
121             tipField : this.tipField,
122             cb : this
123         });
124         // use the 
125         this.items.add(rec[this.idField],x);
126         // add it before the element..
127         this.updateHiddenEl();
128         x.render(this.outerWrap, this.wrap.dom);
129         // add the image handler..
130     },
131     
132     updateHiddenEl : function()
133     {
134         this.validate();
135         if (!this.hiddenEl) {
136             return;
137         }
138         var ar = [];
139         var idField = this.idField;
140         this.items.each(function(f) {
141             ar.push(f.data[idField]);
142            
143         });
144         this.hiddenEl.dom.value = ar.join(',');
145         this.validate();
146     },
147     
148     reset : function()
149     {
150         Roo.form.ComboBoxLister.superclass.reset.call(this); 
151         this.items.each(function(f) {
152            f.remove(); 
153         });
154         if (this.hiddenEl) {
155             this.hiddenEl.dom.value = '';
156         }
157         
158     },
159     getValue: function()
160     {
161         return this.hiddenEl ? this.hiddenEl.dom.value : '';
162     },
163     setValue: function(v) // not a valid action - must use addItems..
164     {
165         if (typeof(v) != 'object') {
166             return;
167         }
168         var _this = this;
169         Roo.each(v, function(l) {
170                 _this.addItem(l);
171         });
172         
173     },
174     validateValue : function(value){
175         return Roo.form.ComboBoxLister.superclass.validateValue.call(this, this.getValue());
176         
177     }
178     
179 });
180
181 Ext.form.ComboBoxLister.Item = function(config) {
182     config.id = Roo.id();
183     Roo.form.ComboBoxLister.Item.superclass.constructor.call(this, config);
184 }
185 Roo.extend(Roo.form.ComboBoxLister.Item, Roo.BoxComponent, {
186     data : {},
187     cb: false,
188     defaultAutoCreate : {tag: 'div', cls: 'p-cblist-item', cn : [ 
189             { tag: 'div' },
190             { tag: 'img', width:16, height : 16, src : Roo.BLANK_IMAGE_URL , align: 'center' }
191         ]
192         
193     },
194     
195  
196     onRender : function(ct, position){
197         Roo.form.Field.superclass.onRender.call(this, ct, position);
198         if(!this.el){
199             var cfg = this.getAutoCreate();
200             this.el = ct.createChild(cfg, position);
201         }
202         this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
203         
204         this.el.child('div').dom.innerHTML = this.cb.renderer ? 
205             this.cb.renderer(this.data) : String.format('{0}',this.data[this.nameField]);
206         this.el.child('div').dom.setAttribute('qtip', String.format('{0}',this.data[this.tipField]));
207         
208         this.el.child('img').on('click', this.remove, this);
209         
210     },
211    
212     remove : function()
213     {
214         
215         this.cb.items.remove(this);
216         this.el.child('img').un('click', this.remove, this);
217         this.el.remove();
218         this.cb.updateHiddenEl();
219     }
220     
221     
222 });