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