source: Dev/trunk/src/client/dijit/form/MultiSelect.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 4.7 KB
Line 
1define([
2        "dojo/_base/array", // array.indexOf, array.map
3        "dojo/_base/declare", // declare
4        "dojo/dom-geometry", // domGeometry.setMarginBox
5        "dojo/has",
6        "dojo/query", // query
7        "./_FormValueWidget"
8], function(array, declare, domGeometry, has, query, _FormValueWidget){
9
10        // module:
11        //              dijit/form/MultiSelect
12
13        var MultiSelect = declare("dijit.form.MultiSelect" + (has("dojo-bidi") ? "_NoBidi" : ""), _FormValueWidget, {
14                // summary:
15                //              Widget version of a `<select multiple=true>` element,
16                //              for selecting multiple options.
17
18                // size: Number
19                //              Number of elements to display on a page
20                //              NOTE: may be removed in version 2.0, since elements may have variable height;
21                //              set the size via style="..." or CSS class names instead.
22                size: 7,
23
24                baseClass: "dijitMultiSelect",
25
26                templateString: "<select multiple='true' ${!nameAttrSetting} data-dojo-attach-point='containerNode,focusNode' data-dojo-attach-event='onchange: _onChange'></select>",
27
28                addSelected: function(/*dijit/form/MultiSelect*/ select){
29                        // summary:
30                        //              Move the selected nodes of a passed Select widget
31                        //              instance to this Select widget.
32                        //
33                        // example:
34                        // |    // move all the selected values from "bar" to "foo"
35                        // |    dijit.byId("foo").addSelected(dijit.byId("bar"));
36
37                        select.getSelected().forEach(function(n){
38                                this.containerNode.appendChild(n);
39                                // scroll to bottom to see item
40                                // cannot use scrollIntoView since <option> tags don't support all attributes
41                                // does not work on IE due to a bug where <select> always shows scrollTop = 0
42                                this.domNode.scrollTop = this.domNode.offsetHeight; // overshoot will be ignored
43                                // scrolling the source select is trickier esp. on safari who forgets to change the scrollbar size
44                                var oldscroll = select.domNode.scrollTop;
45                                select.domNode.scrollTop = 0;
46                                select.domNode.scrollTop = oldscroll;
47                        }, this);
48                        this._set('value', this.get('value'));
49                },
50
51                getSelected: function(){
52                        // summary:
53                        //              Access the NodeList of the selected options directly
54                        return query("option", this.containerNode).filter(function(n){
55                                return n.selected; // Boolean
56                        }); // dojo/NodeList
57                },
58
59                _getValueAttr: function(){
60                        // summary:
61                        //              Hook so get('value') works.
62                        // description:
63                        //              Returns an array of the selected options' values.
64
65                        // Don't call getSelect.map() because it doesn't return a real array,
66                        // and that messes up dojo.toJson() calls like in the Form.html test
67                        return array.map(this.getSelected(), function(n){
68                                return n.value;
69                        });
70                },
71
72                multiple: true, // for Form
73
74                _setValueAttr: function(/*Array*/ values, /*Boolean?*/ priorityChange){
75                        // summary:
76                        //              Hook so set('value', values) works.
77                        // description:
78                        //              Set the value(s) of this Select based on passed values
79                        query("option", this.containerNode).forEach(function(n){
80                                n.selected = (array.indexOf(values, n.value) != -1);
81                        });
82                        this.inherited(arguments);
83                },
84
85                invertSelection: function(/*Boolean?*/ onChange){
86                        // summary:
87                        //              Invert the selection
88                        // onChange: Boolean
89                        //              If false, onChange is not fired.
90                        var val = [];
91                        query("option", this.containerNode).forEach(function(n){
92                                if(!n.selected){
93                                        val.push(n.value);
94                                }
95                        });
96                        this._setValueAttr(val, !(onChange === false || onChange == null));
97                },
98
99                _onChange: function(/*Event*/){
100                        this._handleOnChange(this.get('value'), true);
101                },
102
103                // for layout widgets:
104                resize: function(/*Object*/ size){
105                        if(size){
106                                domGeometry.setMarginBox(this.domNode, size);
107                        }
108                },
109
110                postCreate: function(){
111                        this._set('value', this.get('value'));
112                        this.inherited(arguments);
113                }
114        });
115
116        if(has("dojo-bidi")){
117                MultiSelect = declare("dijit.form.MultiSelect", MultiSelect, {
118                        addSelected: function(/*dijit/form/MultiSelect*/ select){
119                                select.getSelected().forEach(function(n){
120                                        n.text = this.enforceTextDirWithUcc(this.restoreOriginalText(n), n.text);
121                                }, this);
122                                this.inherited(arguments);
123                        },
124
125                        _setTextDirAttr: function(textDir){
126                                // to insure the code executed only when _BidiSupport loaded, and only
127                                // when there was a change in textDir
128                                if((this.textDir != textDir || !this._created) && this.enforceTextDirWithUcc){
129                                        this._set("textDir", textDir);
130
131                                        query("option", this.containerNode).forEach(function(option){
132                                                // If the value wasn't defined explicitly, it the same object as
133                                                // option.text. Since the option.text will be modified (by wrapping of UCC)
134                                                // we want to save the original option.value for form submission.
135                                                if(!this._created && option.value === option.text){
136                                                        option.value = option.text;
137                                                }
138                                                // apply the bidi support
139                                                option.text = this.enforceTextDirWithUcc(option, option.originalText || option.text);
140                                        }, this);
141                                }
142                        }
143                });
144        }
145
146        return MultiSelect;
147});
Note: See TracBrowser for help on using the repository browser.