3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
10 * @class Ext.ux.form.MultiSelect
11 * @extends Ext.form.Field
12 * A control that allows selection and form submission of multiple list items.
15 * 2008-06-19 bpm Original code contributed by Toby Stuart (with contributions from Robert Williams)
16 * 2008-06-19 bpm Docs and demo code clean up
19 * Create a new MultiSelect
20 * @param {Object} config Configuration options
23 Ext.ux.form.MultiSelect = Ext.extend(Ext.form.Field, {
25 * @cfg {String} legend Wraps the object with a fieldset and specified legend.
28 * @cfg {Ext.ListView} view The {@link Ext.ListView} used to render the multiselect list.
31 * @cfg {String/Array} dragGroup The ddgroup name(s) for the MultiSelect DragZone (defaults to undefined).
34 * @cfg {String/Array} dropGroup The ddgroup name(s) for the MultiSelect DropZone (defaults to undefined).
37 * @cfg {Boolean} ddReorder Whether the items in the MultiSelect list are drag/drop reorderable (defaults to false).
41 * @cfg {Object/Array} tbar The top toolbar of the control. This can be a {@link Ext.Toolbar} object, a
42 * toolbar config, or an array of buttons/button configs to be added to the toolbar.
45 * @cfg {String} appendOnly True if the list should only allow append drops when drag/drop is enabled
46 * (use for lists which are sorted, defaults to false).
50 * @cfg {Number} width Width in pixels of the control (defaults to 100).
54 * @cfg {Number} height Height in pixels of the control (defaults to 100).
58 * @cfg {String/Number} displayField Name/Index of the desired display field in the dataset (defaults to 0).
62 * @cfg {String/Number} valueField Name/Index of the desired value field in the dataset (defaults to 1).
66 * @cfg {Boolean} allowBlank False to require at least one item in the list to be selected, true to allow no
67 * selection (defaults to true).
71 * @cfg {Number} minSelections Minimum number of selections allowed (defaults to 0).
75 * @cfg {Number} maxSelections Maximum number of selections allowed (defaults to Number.MAX_VALUE).
77 maxSelections:Number.MAX_VALUE,
79 * @cfg {String} blankText Default text displayed when the control contains no items (defaults to the same value as
80 * {@link Ext.form.TextField#blankText}.
82 blankText:Ext.form.TextField.prototype.blankText,
84 * @cfg {String} minSelectionsText Validation message displayed when {@link #minSelections} is not met (defaults to 'Minimum {0}
85 * item(s) required'). The {0} token will be replaced by the value of {@link #minSelections}.
87 minSelectionsText:'Minimum {0} item(s) required',
89 * @cfg {String} maxSelectionsText Validation message displayed when {@link #maxSelections} is not met (defaults to 'Maximum {0}
90 * item(s) allowed'). The {0} token will be replaced by the value of {@link #maxSelections}.
92 maxSelectionsText:'Maximum {0} item(s) allowed',
94 * @cfg {String} delimiter The string used to delimit between items when set or returned as a string of values
99 * @cfg {Ext.data.Store/Array} store The data source to which this MultiSelect is bound (defaults to <tt>undefined</tt>).
100 * Acceptable values for this property are:
101 * <div class="mdetail-params"><ul>
102 * <li><b>any {@link Ext.data.Store Store} subclass</b></li>
103 * <li><b>an Array</b> : Arrays will be converted to a {@link Ext.data.ArrayStore} internally.
104 * <div class="mdetail-params"><ul>
105 * <li><b>1-dimensional array</b> : (e.g., <tt>['Foo','Bar']</tt>)<div class="sub-desc">
106 * A 1-dimensional array will automatically be expanded (each array item will be the combo
107 * {@link #valueField value} and {@link #displayField text})</div></li>
108 * <li><b>2-dimensional array</b> : (e.g., <tt>[['f','Foo'],['b','Bar']]</tt>)<div class="sub-desc">
109 * For a multi-dimensional array, the value in index 0 of each item will be assumed to be the combo
110 * {@link #valueField value}, while the value at index 1 is assumed to be the combo {@link #displayField text}.
111 * </div></li></ul></div></li></ul></div>
115 defaultAutoCreate : {tag: "div"},
118 initComponent: function(){
119 Ext.ux.form.MultiSelect.superclass.initComponent.call(this);
121 if(Ext.isArray(this.store)){
122 if (Ext.isArray(this.store[0])){
123 this.store = new Ext.data.ArrayStore({
124 fields: ['value','text'],
127 this.valueField = 'value';
129 this.store = new Ext.data.ArrayStore({
134 this.valueField = 'text';
136 this.displayField = 'text';
138 this.store = Ext.StoreMgr.lookup(this.store);
150 onRender: function(ct, position){
151 Ext.ux.form.MultiSelect.superclass.onRender.call(this, ct, position);
153 var fs = this.fs = new Ext.form.FieldSet({
161 fs.body.addClass('ux-mselect');
163 this.view = new Ext.ListView({
166 columns: [{ header: 'Value', width: 1, dataIndex: this.displayField }],
172 this.view.on('click', this.onViewClick, this);
173 this.view.on('beforeclick', this.onViewBeforeClick, this);
174 this.view.on('dblclick', this.onViewDblClick, this);
176 this.hiddenName = this.name || Ext.id();
177 var hiddenTag = { tag: "input", type: "hidden", value: "", name: this.hiddenName };
178 this.hiddenField = this.el.createChild(hiddenTag);
179 this.hiddenField.dom.disabled = this.hiddenName != this.name;
184 afterRender: function(){
185 Ext.ux.form.MultiSelect.superclass.afterRender.call(this);
187 if (this.ddReorder && !this.dragGroup && !this.dropGroup){
188 this.dragGroup = this.dropGroup = 'MultiselectDD-' + Ext.id();
191 if (this.draggable || this.dragGroup){
192 this.dragZone = new Ext.ux.form.MultiSelect.DragZone(this, {
193 ddGroup: this.dragGroup
196 if (this.droppable || this.dropGroup){
197 this.dropZone = new Ext.ux.form.MultiSelect.DropZone(this, {
198 ddGroup: this.dropGroup
204 onViewClick: function(vw, index, node, e) {
205 this.fireEvent('change', this, this.getValue(), this.hiddenField.dom.value);
206 this.hiddenField.dom.value = this.getValue();
207 this.fireEvent('click', this, e);
212 onViewBeforeClick: function(vw, index, node, e) {
213 if (this.disabled || this.readOnly) {
219 onViewDblClick : function(vw, index, node, e) {
220 return this.fireEvent('dblclick', vw, index, node, e);
224 * Returns an array of data values for the selected items in the list. The values will be separated
225 * by {@link #delimiter}.
226 * @return {Array} value An array of string data values
228 getValue: function(valueField){
229 var returnArray = [];
230 var selectionsArray = this.view.getSelectedIndexes();
231 if (selectionsArray.length == 0) {return '';}
232 for (var i=0; i<selectionsArray.length; i++) {
233 returnArray.push(this.store.getAt(selectionsArray[i]).get((valueField != null) ? valueField : this.valueField));
235 return returnArray.join(this.delimiter);
239 * Sets a delimited string (using {@link #delimiter}) or array of data values into the list.
240 * @param {String/Array} values The values to set
242 setValue: function(values) {
245 this.view.clearSelections();
246 this.hiddenField.dom.value = '';
248 if (!values || (values == '')) { return; }
250 if (!Ext.isArray(values)) { values = values.split(this.delimiter); }
251 for (var i=0; i<values.length; i++) {
252 index = this.view.store.indexOf(this.view.store.query(this.valueField,
253 new RegExp('^' + values[i] + '$', "i")).itemAt(0));
254 selections.push(index);
256 this.view.select(selections);
257 this.hiddenField.dom.value = this.getValue();
267 getRawValue: function(valueField) {
268 var tmp = this.getValue(valueField);
270 tmp = tmp.split(this.delimiter);
279 setRawValue: function(values){
284 validateValue : function(value){
285 if (value.length < 1) { // if it has no value
286 if (this.allowBlank) {
290 this.markInvalid(this.blankText);
294 if (value.length < this.minSelections) {
295 this.markInvalid(String.format(this.minSelectionsText, this.minSelections));
298 if (value.length > this.maxSelections) {
299 this.markInvalid(String.format(this.maxSelectionsText, this.maxSelections));
307 this.disabled = true;
308 this.hiddenField.dom.disabled = true;
314 this.disabled = false;
315 this.hiddenField.dom.disabled = false;
321 Ext.destroy(this.fs, this.dragZone, this.dropZone);
322 Ext.ux.form.MultiSelect.superclass.destroy.call(this);
327 Ext.reg('multiselect', Ext.ux.form.MultiSelect);
330 Ext.ux.Multiselect = Ext.ux.form.MultiSelect;
333 Ext.ux.form.MultiSelect.DragZone = function(ms, config){
336 var ddGroup = config.ddGroup || 'MultiselectDD';
338 if (Ext.isArray(ddGroup)){
339 dd = ddGroup.shift();
344 Ext.ux.form.MultiSelect.DragZone.superclass.constructor.call(this, this.ms.fs.body, { containerScroll: true, ddGroup: dd });
345 this.setDraggable(ddGroup);
348 Ext.extend(Ext.ux.form.MultiSelect.DragZone, Ext.dd.DragZone, {
349 onInitDrag : function(x, y){
350 var el = Ext.get(this.dragData.ddel.cloneNode(true));
351 this.proxy.update(el.dom);
352 el.setWidth(el.child('em').getWidth());
353 this.onStartDrag(x, y);
358 collectSelection: function(data) {
359 data.repairXY = Ext.fly(this.view.getSelectedNodes()[0]).getXY();
361 this.view.store.each(function(rec){
362 if (this.view.isSelected(i)) {
363 var n = this.view.getNode(i);
364 var dragNode = n.cloneNode(true);
365 dragNode.id = Ext.id();
366 data.ddel.appendChild(dragNode);
367 data.records.push(this.view.store.getAt(i));
368 data.viewNodes.push(n);
375 onEndDrag: function(data, e) {
376 var d = Ext.get(this.dragData.ddel);
377 if (d && d.hasClass("multi-proxy")) {
383 getDragData: function(e){
384 var target = this.view.findItemFromChild(e.getTarget());
386 if (!this.view.isSelected(target) && !e.ctrlKey && !e.shiftKey) {
387 this.view.select(target);
388 this.ms.setValue(this.ms.getValue());
390 if (this.view.getSelectionCount() == 0 || e.ctrlKey || e.shiftKey) return false;
392 sourceView: this.view,
396 if (this.view.getSelectionCount() == 1) {
397 var i = this.view.getSelectedIndexes()[0];
398 var n = this.view.getNode(i);
399 dragData.viewNodes.push(dragData.ddel = n);
400 dragData.records.push(this.view.store.getAt(i));
401 dragData.repairXY = Ext.fly(n).getXY();
403 dragData.ddel = document.createElement('div');
404 dragData.ddel.className = 'multi-proxy';
405 this.collectSelection(dragData);
412 // override the default repairXY.
413 getRepairXY : function(e){
414 return this.dragData.repairXY;
418 setDraggable: function(ddGroup){
419 if (!ddGroup) return;
420 if (Ext.isArray(ddGroup)) {
421 Ext.each(ddGroup, this.setDraggable, this);
424 this.addToGroup(ddGroup);
428 Ext.ux.form.MultiSelect.DropZone = function(ms, config){
431 var ddGroup = config.ddGroup || 'MultiselectDD';
433 if (Ext.isArray(ddGroup)){
434 dd = ddGroup.shift();
439 Ext.ux.form.MultiSelect.DropZone.superclass.constructor.call(this, this.ms.fs.body, { containerScroll: true, ddGroup: dd });
440 this.setDroppable(ddGroup);
443 Ext.extend(Ext.ux.form.MultiSelect.DropZone, Ext.dd.DropZone, {
445 * Part of the Ext.dd.DropZone interface. If no target node is found, the
446 * whole Element becomes the target, and this causes the drop gesture to append.
448 getTargetFromEvent : function(e) {
449 var target = e.getTarget();
454 getDropPoint : function(e, n, dd){
455 if (n == this.ms.fs.body.dom) { return "below"; }
456 var t = Ext.lib.Dom.getY(n), b = t + n.offsetHeight;
457 var c = t + (b - t) / 2;
458 var y = Ext.lib.Event.getPageY(e);
467 isValidDropPoint: function(pt, n, data) {
468 if (!data.viewNodes || (data.viewNodes.length != 1)) {
471 var d = data.viewNodes[0];
475 if ((pt == "below") && (n.nextSibling == d)) {
478 if ((pt == "above") && (n.previousSibling == d)) {
485 onNodeEnter : function(n, dd, e, data){
490 onNodeOver : function(n, dd, e, data){
491 var dragElClass = this.dropNotAllowed;
492 var pt = this.getDropPoint(e, n, dd);
493 if (this.isValidDropPoint(pt, n, data)) {
494 if (this.ms.appendOnly) {
495 return "x-tree-drop-ok-below";
498 // set the insert point style on the target node
502 dragElClass = n.previousSibling ? "x-tree-drop-ok-between" : "x-tree-drop-ok-above";
503 targetElClass = "x-view-drag-insert-above";
505 dragElClass = n.nextSibling ? "x-tree-drop-ok-between" : "x-tree-drop-ok-below";
506 targetElClass = "x-view-drag-insert-below";
508 if (this.lastInsertClass != targetElClass){
509 Ext.fly(n).replaceClass(this.lastInsertClass, targetElClass);
510 this.lastInsertClass = targetElClass;
518 onNodeOut : function(n, dd, e, data){
519 this.removeDropIndicators(n);
523 onNodeDrop : function(n, dd, e, data){
524 if (this.ms.fireEvent("drop", this, n, dd, e, data) === false) {
527 var pt = this.getDropPoint(e, n, dd);
528 if (n != this.ms.fs.body.dom)
529 n = this.view.findItemFromChild(n);
531 if(this.ms.appendOnly) {
532 insertAt = this.view.store.getCount();
534 insertAt = n == this.ms.fs.body.dom ? this.view.store.getCount() - 1 : this.view.indexOf(n);
542 // Validate if dragging within the same MultiSelect
543 if (data.sourceView == this.view) {
544 // If the first element to be inserted below is the target node, remove it
546 if (data.viewNodes[0] == n) {
547 data.viewNodes.shift();
549 } else { // If the last element to be inserted above is the target node, remove it
550 if (data.viewNodes[data.viewNodes.length - 1] == n) {
551 data.viewNodes.pop();
555 // Nothing to drop...
556 if (!data.viewNodes.length) {
560 // If we are moving DOWN, then because a store.remove() takes place first,
561 // the insertAt must be decremented.
562 if (insertAt > this.view.store.indexOf(data.records[0])) {
568 for (var i = 0; i < data.records.length; i++) {
569 var r = data.records[i];
570 if (data.sourceView) {
571 data.sourceView.store.remove(r);
573 this.view.store.insert(dir == 'down' ? insertAt : insertAt++, r);
574 var si = this.view.store.sortInfo;
576 this.view.store.sort(si.field, si.direction);
583 removeDropIndicators : function(n){
585 Ext.fly(n).removeClass([
586 "x-view-drag-insert-above",
587 "x-view-drag-insert-left",
588 "x-view-drag-insert-right",
589 "x-view-drag-insert-below"]);
590 this.lastInsertClass = "_noclass";
595 setDroppable: function(ddGroup){
596 if (!ddGroup) return;
597 if (Ext.isArray(ddGroup)) {
598 Ext.each(ddGroup, this.setDroppable, this);
601 this.addToGroup(ddGroup);