| 1 | define([ |
| 2 | "dojo/_base/declare", // declare |
| 3 | "dojo/keys", // keys |
| 4 | "dojo/_base/lang", // lang.clone lang.hitch |
| 5 | "dojo/query", // query |
| 6 | "dojo/string", // string.substitute |
| 7 | "dojo/when", |
| 8 | "../registry" // registry.byId |
| 9 | ], function(declare, keys, lang, query, string, when, registry){ |
| 10 | |
| 11 | // module: |
| 12 | // dijit/form/_SearchMixin |
| 13 | |
| 14 | |
| 15 | return declare("dijit.form._SearchMixin", null, { |
| 16 | // summary: |
| 17 | // A mixin that implements the base functionality to search a store based upon user-entered text such as |
| 18 | // with `dijit/form/ComboBox` or `dijit/form/FilteringSelect` |
| 19 | // tags: |
| 20 | // protected |
| 21 | |
| 22 | // pageSize: Integer |
| 23 | // Argument to data provider. |
| 24 | // Specifies maximum number of search results to return per query |
| 25 | pageSize: Infinity, |
| 26 | |
| 27 | // store: [const] dojo/store/api/Store |
| 28 | // Reference to data provider object used by this ComboBox. |
| 29 | // The store must accept an object hash of properties for its query. See `query` and `queryExpr` for details. |
| 30 | store: null, |
| 31 | |
| 32 | // fetchProperties: Object |
| 33 | // Mixin to the store's fetch. |
| 34 | // For example, to set the sort order of the ComboBox menu, pass: |
| 35 | // | { sort: [{attribute:"name",descending: true}] } |
| 36 | // To override the default queryOptions so that deep=false, do: |
| 37 | // | { queryOptions: {ignoreCase: true, deep: false} } |
| 38 | fetchProperties:{}, |
| 39 | |
| 40 | // query: Object |
| 41 | // A query that can be passed to `store` to initially filter the items. |
| 42 | // ComboBox overwrites any reference to the `searchAttr` and sets it to the `queryExpr` with the user's input substituted. |
| 43 | query: {}, |
| 44 | |
| 45 | // list: [const] String |
| 46 | // Alternate to specifying a store. Id of a dijit/form/DataList widget. |
| 47 | list: "", |
| 48 | _setListAttr: function(list){ |
| 49 | // Avoid having list applied to the DOM node, since it has native meaning in modern browsers |
| 50 | this._set("list", list); |
| 51 | }, |
| 52 | |
| 53 | // searchDelay: Integer |
| 54 | // Delay in milliseconds between when user types something and we start |
| 55 | // searching based on that value |
| 56 | searchDelay: 200, |
| 57 | |
| 58 | // searchAttr: String |
| 59 | // Search for items in the data store where this attribute (in the item) |
| 60 | // matches what the user typed |
| 61 | searchAttr: "name", |
| 62 | |
| 63 | // queryExpr: String |
| 64 | // This specifies what query is sent to the data store, |
| 65 | // based on what the user has typed. Changing this expression will modify |
| 66 | // whether the results are only exact matches, a "starting with" match, |
| 67 | // etc. |
| 68 | // `${0}` will be substituted for the user text. |
| 69 | // `*` is used for wildcards. |
| 70 | // `${0}*` means "starts with", `*${0}*` means "contains", `${0}` means "is" |
| 71 | queryExpr: "${0}*", |
| 72 | |
| 73 | // ignoreCase: Boolean |
| 74 | // Set true if the query should ignore case when matching possible items |
| 75 | ignoreCase: true, |
| 76 | |
| 77 | _patternToRegExp: function(pattern){ |
| 78 | // summary: |
| 79 | // Helper function to convert a simple pattern to a regular expression for matching. |
| 80 | // description: |
| 81 | // Returns a regular expression object that conforms to the defined conversion rules. |
| 82 | // For example: |
| 83 | // |
| 84 | // - ca* -> /^ca.*$/ |
| 85 | // - *ca* -> /^.*ca.*$/ |
| 86 | // - *c\*a* -> /^.*c\*a.*$/ |
| 87 | // - *c\*a?* -> /^.*c\*a..*$/ |
| 88 | // |
| 89 | // and so on. |
| 90 | // pattern: string |
| 91 | // A simple matching pattern to convert that follows basic rules: |
| 92 | // |
| 93 | // - * Means match anything, so ca* means match anything starting with ca |
| 94 | // - ? Means match single character. So, b?b will match to bob and bab, and so on. |
| 95 | // - \ is an escape character. So for example, \* means do not treat * as a match, but literal character *. |
| 96 | // |
| 97 | // To use a \ as a character in the string, it must be escaped. So in the pattern it should be |
| 98 | // represented by \\ to be treated as an ordinary \ character instead of an escape. |
| 99 | |
| 100 | return new RegExp("^" + pattern.replace(/(\\.)|(\*)|(\?)|\W/g, function(str, literal, star, question){ |
| 101 | return star ? ".*" : question ? "." : literal ? literal : "\\" + str; |
| 102 | }) + "$", this.ignoreCase ? "mi" : "m"); |
| 103 | }, |
| 104 | |
| 105 | _abortQuery: function(){ |
| 106 | // stop in-progress query |
| 107 | if(this.searchTimer){ |
| 108 | this.searchTimer = this.searchTimer.remove(); |
| 109 | } |
| 110 | if(this._queryDeferHandle){ |
| 111 | this._queryDeferHandle = this._queryDeferHandle.remove(); |
| 112 | } |
| 113 | if(this._fetchHandle){ |
| 114 | if(this._fetchHandle.abort){ |
| 115 | this._cancelingQuery = true; |
| 116 | this._fetchHandle.abort(); |
| 117 | this._cancelingQuery = false; |
| 118 | } |
| 119 | if(this._fetchHandle.cancel){ |
| 120 | this._cancelingQuery = true; |
| 121 | this._fetchHandle.cancel(); |
| 122 | this._cancelingQuery = false; |
| 123 | } |
| 124 | this._fetchHandle = null; |
| 125 | } |
| 126 | }, |
| 127 | |
| 128 | _processInput: function(/*Event*/ evt){ |
| 129 | // summary: |
| 130 | // Handles input (keyboard/paste) events |
| 131 | if(this.disabled || this.readOnly){ return; } |
| 132 | var key = evt.charOrCode; |
| 133 | |
| 134 | this._prev_key_backspace = false; |
| 135 | |
| 136 | if (key === keys.DELETE || key === keys.BACKSPACE) { |
| 137 | this._prev_key_backspace = true; |
| 138 | this._maskValidSubsetError = true; |
| 139 | } |
| 140 | |
| 141 | // need to wait a tad before start search so that the event |
| 142 | // bubbles through DOM and we have value visible |
| 143 | if(!this.store){ |
| 144 | this.onSearch(); |
| 145 | }else{ |
| 146 | this.searchTimer = this.defer("_startSearchFromInput", 1); |
| 147 | } |
| 148 | }, |
| 149 | |
| 150 | onSearch: function(/*===== results, query, options =====*/){ |
| 151 | // summary: |
| 152 | // Callback when a search completes. |
| 153 | // |
| 154 | // results: Object |
| 155 | // An array of items from the originating _SearchMixin's store. |
| 156 | // |
| 157 | // query: Object |
| 158 | // A copy of the originating _SearchMixin's query property. |
| 159 | // |
| 160 | // options: Object |
| 161 | // The additional parameters sent to the originating _SearchMixin's store, including: start, count, queryOptions. |
| 162 | // |
| 163 | // tags: |
| 164 | // callback |
| 165 | }, |
| 166 | |
| 167 | _startSearchFromInput: function(){ |
| 168 | this._startSearch(this.focusNode.value); |
| 169 | }, |
| 170 | |
| 171 | _startSearch: function(/*String*/ text){ |
| 172 | // summary: |
| 173 | // Starts a search for elements matching text (text=="" means to return all items), |
| 174 | // and calls onSearch(...) when the search completes, to display the results. |
| 175 | |
| 176 | this._abortQuery(); |
| 177 | var |
| 178 | _this = this, |
| 179 | // Setup parameters to be passed to store.query(). |
| 180 | // Create a new query to prevent accidentally querying for a hidden |
| 181 | // value from FilteringSelect's keyField |
| 182 | query = lang.clone(this.query), // #5970 |
| 183 | options = { |
| 184 | start: 0, |
| 185 | count: this.pageSize, |
| 186 | queryOptions: { // remove for 2.0 |
| 187 | ignoreCase: this.ignoreCase, |
| 188 | deep: true |
| 189 | } |
| 190 | }, |
| 191 | qs = string.substitute(this.queryExpr, [text.replace(/([\\\*\?])/g, "\\$1")]), |
| 192 | q, |
| 193 | startQuery = function(){ |
| 194 | var resPromise = _this._fetchHandle = _this.store.query(query, options); |
| 195 | if(_this.disabled || _this.readOnly || (q !== _this._lastQuery)){ |
| 196 | return; |
| 197 | } // avoid getting unwanted notify |
| 198 | when(resPromise, function(res){ |
| 199 | _this._fetchHandle = null; |
| 200 | if(!_this.disabled && !_this.readOnly && (q === _this._lastQuery)){ // avoid getting unwanted notify |
| 201 | when(resPromise.total, function(total){ |
| 202 | res.total = total; |
| 203 | var pageSize = _this.pageSize; |
| 204 | if(isNaN(pageSize) || pageSize > res.total){ pageSize = res.total; } |
| 205 | // Setup method to fetching the next page of results |
| 206 | res.nextPage = function(direction){ |
| 207 | // tell callback the direction of the paging so the screen |
| 208 | // reader knows which menu option to shout |
| 209 | options.direction = direction = direction !== false; |
| 210 | options.count = pageSize; |
| 211 | if(direction){ |
| 212 | options.start += res.length; |
| 213 | if(options.start >= res.total){ |
| 214 | options.count = 0; |
| 215 | } |
| 216 | }else{ |
| 217 | options.start -= pageSize; |
| 218 | if(options.start < 0){ |
| 219 | options.count = Math.max(pageSize + options.start, 0); |
| 220 | options.start = 0; |
| 221 | } |
| 222 | } |
| 223 | if(options.count <= 0){ |
| 224 | res.length = 0; |
| 225 | _this.onSearch(res, query, options); |
| 226 | }else{ |
| 227 | startQuery(); |
| 228 | } |
| 229 | }; |
| 230 | _this.onSearch(res, query, options); |
| 231 | }); |
| 232 | } |
| 233 | }, function(err){ |
| 234 | _this._fetchHandle = null; |
| 235 | if(!_this._cancelingQuery){ // don't treat canceled query as an error |
| 236 | console.error(_this.declaredClass + ' ' + err.toString()); |
| 237 | } |
| 238 | }); |
| 239 | }; |
| 240 | |
| 241 | lang.mixin(options, this.fetchProperties); |
| 242 | |
| 243 | // Generate query |
| 244 | if(this.store._oldAPI){ |
| 245 | // remove this branch for 2.0 |
| 246 | q = qs; |
| 247 | }else{ |
| 248 | // Query on searchAttr is a regex for benefit of dojo/store/Memory, |
| 249 | // but with a toString() method to help dojo/store/JsonRest. |
| 250 | // Search string like "Co*" converted to regex like /^Co.*$/i. |
| 251 | q = this._patternToRegExp(qs); |
| 252 | q.toString = function(){ return qs; }; |
| 253 | } |
| 254 | |
| 255 | // set _lastQuery, *then* start the timeout |
| 256 | // otherwise, if the user types and the last query returns before the timeout, |
| 257 | // _lastQuery won't be set and their input gets rewritten |
| 258 | this._lastQuery = query[this.searchAttr] = q; |
| 259 | this._queryDeferHandle = this.defer(startQuery, this.searchDelay); |
| 260 | }, |
| 261 | |
| 262 | //////////// INITIALIZATION METHODS /////////////////////////////////////// |
| 263 | |
| 264 | constructor: function(){ |
| 265 | this.query={}; |
| 266 | this.fetchProperties={}; |
| 267 | }, |
| 268 | |
| 269 | postMixInProperties: function(){ |
| 270 | if(!this.store){ |
| 271 | var list = this.list; |
| 272 | if(list){ |
| 273 | this.store = registry.byId(list); |
| 274 | } |
| 275 | } |
| 276 | this.inherited(arguments); |
| 277 | } |
| 278 | }); |
| 279 | }); |