tony-landis (owner)

Revisions

gist: 40145 Download_button fork
public
Public Clone URL: git://gist.github.com/40145.git
Embed All Files: show embed
JavaScript #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* Provides a drop down field with multiple checkboxes
* @author Tony Landis http://www.tonylandis.com/
* @copyright Free for all use and modification. The author and copyright must be remain intact here.
*
* @class Ext.form.MultiSelectField
* @extends Ext.form.TriggerField
*/
Ext.form.MultiSelectField = Ext.extend(Ext.form.TriggerField, {
    triggerClass: 'x-form-trigger',
    defaultAutoCreate: { tag: "input", type: "text", size: "10", autocomplete: "off" },
    readOnly: true,
    lazyInit: false,
    hiddenValue: '',
    value: null,
    valueSeparator: ';',
    textSeparator: ', ',
    loadingText: 'Loading list...',
    containerHeight: '', // leave it empty for auto hegiht.
    containerWidth: '', // leave it empty for auto width.
 
    // store defaults
    store: null,
    mode: 'remote',
    valueField: 'value',
    displayField: 'text',
 
    initComponent: function() {
Ext.QuickTips.init();
        Ext.form.MultiSelectField.superclass.initComponent.call(this);
 
        //auto-configure store from local array data
        if (Ext.isArray(this.store)) {
            if (Ext.isArray(this.store[0])) {
                this.store = new Ext.data.SimpleStore({
                    id: 'value',
                    fields: ['value', 'text'],
                    data: this.store
                });
                this.valueField = 'value';
            } else {
                this.store = new Ext.data.Store({
                    id: 'text',
                    fields: ['text'],
                    data: this.store,
                    expandData: true
                });
                this.valueField = 'text';
            }
            this.displayField = 'text';
            this.mode = 'local';
        }
    },
 
 
    onRender: function(ct, position) {
        Ext.form.MultiSelectField.superclass.onRender.call(this, ct, position);
 
        if (this.hiddenName) {
            this.hiddenField = this.el.insertSibling({ tag: 'input', type: 'hidden', name: this.hiddenName, id: (this.hiddenId || this.hiddenName) },
                    'before', true);
            this.hiddenField.value =
                this.hiddenValue !== undefined ? this.hiddenValue :
                this.value !== undefined ? this.value : '';
 
            // prevent input submission
            this.el.dom.removeAttribute('name');
        }
 
        // build the menu
        if (this.menu == null) {
            this.menu = new Ext.menu.Menu();
            this.menu.on('hide', function() {
                if (this.tmp != this.lastSelectionText && this.lastSelectionText != '') {
                    this.tmp = this.lastSelectionText;
                    this.fireEvent('change', this);
                }
            }, this)
            this.store.each(function(r) {
                this.menu.add(
         new Ext.menu.CheckItem({
         text: r.data[this.displayField],
         value: r.data[this.valueField],
         hideOnClick: false
         })
         ).on('click', this.clickHandler, this);
            }, this);
        }
 
        if (!this.lazyInit) {
            //this.populateList(this.value);
        } else {
            //this.on('focus', this.setValues, this, {single: true});
        }
    },
 
 
 
    onTriggerClick: function() {
        if (this.disabled) {
            return;
        }
 
        this.menu.show(this.el, "bottom");
        this.menu.el.setTop(this.menu.el.getTop() + this.el.getHeight() - 3);
 
        // Correcting container height.
        if (this.containerHeight != '' && this.menu.el.getHeight() > this.containerHeight) {
            this.menu.el.setHeight(this.containerHeight);
            this.menu.el.dom.style.overflowY = 'auto';
        }
 
        // Correcting items' and container width.
        var width = this.containerWidth != '' ? this.containerWidth : (
                        this.width > this.menu.el.getWidth() ? this.width - 1 : this.menu.el.getWidth());
        this.menu.items.each(function(r) {
            r.el.setWidth(width);
            r.el.dom.style.overflowX = 'hidden';
        }, this);
        this.menu.el.setWidth(width);
        this.menu.el.dom.style.overflowX = 'hidden';
 
        this.populateList(this.value);
    },
 
    validateBlur: function() {
        return !this.menu || !this.menu.isVisible();
    },
 
    getValue: function() {
        if (this.hiddenField) {
            return this.hiddenField.value || "";
        } else if (this.valueField) {
            return typeof this.value != 'undefined' ? this.value : '';
        } else {
            return Ext.form.MultiSelectField.superclass.getValue.call(this);
        }
    },
 
    setValue: function(value, text) {
        if (text == undefined && value != undefined) {
            this.setValues(value.split(this.valueSeparator));
            return;
        } if (value == undefined) {
            value = '';
            text = '';
        }
 
        this.lastSelectionText = text;
        if (this.hiddenField) {
            this.hiddenField.value = value;
        }
        Ext.form.MultiSelectField.superclass.setValue.call(this, text);
        this.value = value;
 
        if (text.trim() == '') {
            Ext.QuickTips.getQuickTip().unregister(this.el);
        } else {
            Ext.QuickTips.getQuickTip().register({
                target: this.el,
                text: text
            });
        }
    },
 
    setValues: function(keys) {
        // assemble full text and hidden value
        var text = '';
        var value = '';
        for (var i = 0; i < keys.length; i++) {
            if (keys[i] != undefined && keys[i] != '') {
                // get the full store object
                var item = this.store.query(this.valueField, keys[i]).items[0];
                if (item != undefined) {
                    value += (value != '' ? this.valueSeparator : '') + item.data[this.valueField];
                    text += (text != '' ? this.textSeparator : '') + item.data[this.displayField];
                }
            }
        }
        this.setValue(value, text);
    },
 
    selPush: function(key) {
        // rip current value into array
        var keys = this.value == '' ? new Array() : this.value.split(this.valueSeparator);
        var i = keys.length++;
        keys[i] = key;
        this.setValues(keys);
    },
 
    selDrop: function(key) {
        // rip current value into array
        var keys = this.value.split(this.valueSeparator);
        for (var i = 0; i < keys.length; i++) {
            if (keys[i].toString() == key.toString()) {
                keys[i] = undefined;
            }
        }
        this.setValues(keys);
    },
 
    onDestroy: function() {
        if (this.menu) {
            this.menu.destroy();
        }
        if (this.wrap) {
            this.wrap.remove();
        }
        Ext.form.MultiSelectField.superclass.onDestroy.call(this);
    },
 
    clickHandler: function(i, c) {
        if (i.checked == false) {
            this.selPush(i.value, i.text);
        } else {
            this.selDrop(i.value);
        }
    },
 
    populateList: function(v) {
        if (v == undefined || v == null) v = this.value;
 
        // uncheck everything
        if (this.menu) {
            this.menu.items.each(function(item) {
                item.setChecked(false);
            });
        }
 
        // populate preset values
        if (v != undefined && v != '' && v != null) {
            var sel = v.split(this.valueSeparator);
            for (i = 0; i < sel.length; i++) {
                try {
                    var value = this.store.query(this.valueField, sel[i]).items[0].data[this.valueField];
                    this.menu.items.each(function(mi) {
                        if (mi.value == value) mi.setChecked(true);
                    }, this);
                } catch (e) { }
            }
        }
    }
});
 
Ext.reg('multiselect', Ext.form.MultiSelectField);