source: Dev/branches/rest-dojo-ui/client/dijit/form/ComboBoxMixin.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 4.6 KB
Line 
1define([
2        "dojo/_base/declare", // declare
3        "dojo/_base/Deferred",
4        "dojo/_base/kernel", // kernel.deprecated
5        "dojo/_base/lang", // lang.mixin
6        "dojo/store/util/QueryResults", // dojo.store.util.QueryResults
7        "./_AutoCompleterMixin",
8        "./_ComboBoxMenu",
9        "../_HasDropDown",
10        "dojo/text!./templates/DropDownBox.html"
11], function(declare, Deferred, kernel, lang, QueryResults, _AutoCompleterMixin, _ComboBoxMenu, _HasDropDown, template){
12
13/*=====
14        var _AutoCompleterMixin = dijit.form._AutoCompleterMixin;
15        var _ComboBoxMenu = dijit.form._ComboBoxMenu;
16        var _HasDropDown = dijit._HasDropDown;
17=====*/
18
19        // module:
20        //              dijit/form/ComboBoxMixin
21        // summary:
22        //              Provides main functionality of ComboBox widget
23
24        return declare("dijit.form.ComboBoxMixin", [_HasDropDown, _AutoCompleterMixin], {
25                // summary:
26                //              Provides main functionality of ComboBox widget
27
28                // dropDownClass: [protected extension] Function String
29                //              Dropdown widget class used to select a date/time.
30                //              Subclasses should specify this.
31                dropDownClass: _ComboBoxMenu,
32
33                // hasDownArrow: Boolean
34                //              Set this textbox to have a down arrow button, to display the drop down list.
35                //              Defaults to true.
36                hasDownArrow: true,
37
38                templateString: template,
39
40                baseClass: "dijitTextBox dijitComboBox",
41
42                /*=====
43                // store: [const] dojo.store.api.Store || dojo.data.api.Read
44                //              Reference to data provider object used by this ComboBox.
45                //
46                //              Should be dojo.store.api.Store, but dojo.data.api.Read supported
47                //              for backwards compatibility.
48                store: null,
49                =====*/
50
51                // Set classes like dijitDownArrowButtonHover depending on
52                // mouse action over button node
53                cssStateNodes: {
54                        "_buttonNode": "dijitDownArrowButton"
55                },
56
57                _setHasDownArrowAttr: function(/*Boolean*/ val){
58                        this._set("hasDownArrow", val);
59                        this._buttonNode.style.display = val ? "" : "none";
60                },
61
62                _showResultList: function(){
63                        // hide the tooltip
64                        this.displayMessage("");
65                        this.inherited(arguments);
66                },
67
68                _setStoreAttr: function(store){
69                        // For backwards-compatibility, accept dojo.data store in addition to dojo.store.store.  Remove in 2.0.
70                        if(!store.get){
71                                lang.mixin(store, {
72                                        _oldAPI: true,
73                                        get: function(id){
74                                                // summary:
75                                                //              Retrieves an object by it's identity. This will trigger a fetchItemByIdentity.
76                                                //              Like dojo.store.DataStore.get() except returns native item.
77                                                var deferred = new Deferred();
78                                                this.fetchItemByIdentity({
79                                                        identity: id,
80                                                        onItem: function(object){
81                                                                deferred.resolve(object);
82                                                        },
83                                                        onError: function(error){
84                                                                deferred.reject(error);
85                                                        }
86                                                });
87                                                return deferred.promise;
88                                        },
89                                        query: function(query, options){
90                                                // summary:
91                                                //              Queries the store for objects.   Like dojo.store.DataStore.query()
92                                                //              except returned Deferred contains array of native items.
93                                                var deferred = new Deferred(function(){ fetchHandle.abort && fetchHandle.abort(); });
94                                                var fetchHandle = this.fetch(lang.mixin({
95                                                        query: query,
96                                                        onBegin: function(count){
97                                                                deferred.total = count;
98                                                        },
99                                                        onComplete: function(results){
100                                                                deferred.resolve(results);
101                                                        },
102                                                        onError: function(error){
103                                                                deferred.reject(error);
104                                                        }
105                                                }, options));
106                                                return QueryResults(deferred);
107                                        }
108                                });
109                        }
110                        this._set("store", store);
111                },
112
113                postMixInProperties: function(){
114                        // Since _setValueAttr() depends on this.store, _setStoreAttr() needs to execute first.
115                        // Unfortunately, without special code, it ends up executing second.
116                        if(this.params.store){
117                                this._setStoreAttr(this.params.store);
118                        }
119
120                        this.inherited(arguments);
121
122                        // User may try to access this.store.getValue() etc.  in a custom labelFunc() function.
123                        // It's not available with the new data store for handling inline <option> tags, so add it.
124                        if(!this.params.store){
125                                var clazz = this.declaredClass;
126                                lang.mixin(this.store, {
127                                        getValue: function(item, attr){
128                                                kernel.deprecated(clazz + ".store.getValue(item, attr) is deprecated for builtin store.  Use item.attr directly", "", "2.0");
129                                                return item[attr];
130                                        },
131                                        getLabel: function(item){
132                                                kernel.deprecated(clazz + ".store.getLabel(item) is deprecated for builtin store.  Use item.label directly", "", "2.0");
133                                                return item.name;
134                                        },
135                                        fetch: function(args){
136                                                kernel.deprecated(clazz + ".store.fetch() is deprecated for builtin store.", "Use store.query()", "2.0");
137                                                var shim = ["dojo/data/ObjectStore"];   // indirection so it doesn't get rolled into a build
138                                                require(shim, lang.hitch(this, function(ObjectStore){
139                                                        new ObjectStore({objectStore: this}).fetch(args);
140                                                }));
141                                        }
142                                });
143                        }
144                }
145        });
146});
Note: See TracBrowser for help on using the repository browser.