init
[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('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     addItem: function(rec)
84     {
85         if (this.items.indexOfKey(rec[this.idField]) > -1) {
86             //console.log("GOT " + rec.data.id);
87             return;
88         }
89         
90         var x = new Ext.form.ComboBoxLister.Item({
91             //id : rec[this.idField],
92             data : rec,
93             nameField : this.nameField,
94             tipField : this.tipField,
95             cb : this
96         });
97         // use the 
98         this.items.add(rec[this.idField],x);
99         // add it before the element..
100         this.updateHiddenEl();
101         x.render(this.outerWrap, this.wrap.dom);
102         // add the image handler..
103     },
104     
105     updateHiddenEl : function()
106     {
107         this.validate();
108         if (!this.hiddenEl) {
109             return;
110         }
111         var ar = [];
112         var idField = this.idField;
113         this.items.each(function(f) {
114             ar.push(f.data[idField]);
115            
116         });
117         this.hiddenEl.dom.value = ar.join(',');
118         this.validate();
119     },
120     
121     reset : function()
122     {
123         Roo.form.ComboBoxLister.superclass.reset.call(this); 
124         this.items.each(function(f) {
125            f.remove(); 
126         });
127         if (this.hiddenEl) {
128             this.hiddenEl.dom.value = '';
129         }
130         
131     },
132     getValue: function()
133     {
134         return this.hiddenEl ? this.hiddenEl.dom.value : '';
135     },
136     setValue: function(v) // not a valid action - must use addItems..
137     {
138         if (typeof(v) != 'object') {
139             return;
140         }
141         var _this = this;
142         Roo.each(v, function(l) {
143                 _this.addItem(l);
144         });
145         
146     },
147     validateValue : function(value){
148         return Roo.form.ComboBoxLister.superclass.validateValue.call(this, this.getValue());
149         
150     }
151     
152 });
153
154 Ext.form.ComboBoxLister.Item = function(config) {
155     config.id = Roo.id();
156     Roo.form.ComboBoxLister.Item.superclass.constructor.call(this, config);
157 }
158 Roo.extend(Roo.form.ComboBoxLister.Item, Roo.BoxComponent, {
159     data : {},
160     cb: false,
161     defaultAutoCreate : {tag: 'div', cls: 'p-cblist-item', cn : [ 
162             { tag: 'div' },
163             { tag: 'img', width:16, height : 16, src : Roo.BLANK_IMAGE_URL , align: 'center' }
164         ]
165         
166     },
167     
168  
169     onRender : function(ct, position){
170         Roo.form.Field.superclass.onRender.call(this, ct, position);
171         if(!this.el){
172             var cfg = this.getAutoCreate();
173             this.el = ct.createChild(cfg, position);
174         }
175         this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
176         
177         this.el.child('div').dom.innerHTML = this.cb.renderer ? 
178             this.cb.renderer(this.data) : String.format('{0}',this.data[this.nameField]);
179         this.el.child('div').dom.setAttribute('qtip', String.format('{0}',this.data[this.tipField]));
180         
181         this.el.child('img').on('click', this.remove, this);
182         
183     },
184    
185     remove : function()
186     {
187         
188         this.cb.items.remove(this);
189         this.el.child('img').un('click', this.remove, this);
190         this.el.remove();
191         this.cb.updateHiddenEl();
192     }
193     
194     
195 });