diff options
Diffstat (limited to 'lib/dojo/data/util')
-rw-r--r-- | lib/dojo/data/util/filter.js | 108 | ||||
-rw-r--r-- | lib/dojo/data/util/simpleFetch.js | 139 | ||||
-rw-r--r-- | lib/dojo/data/util/sorter.js | 146 |
3 files changed, 243 insertions, 150 deletions
diff --git a/lib/dojo/data/util/filter.js b/lib/dojo/data/util/filter.js index dcdc050e6..d23e63ade 100644 --- a/lib/dojo/data/util/filter.js +++ b/lib/dojo/data/util/filter.js @@ -5,48 +5,72 @@ */ -if(!dojo._hasResource["dojo.data.util.filter"]){ -dojo._hasResource["dojo.data.util.filter"]=true; +if(!dojo._hasResource["dojo.data.util.filter"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojo.data.util.filter"] = true; dojo.provide("dojo.data.util.filter"); -dojo.data.util.filter.patternToRegExp=function(_1,_2){ -var _3="^"; -var c=null; -for(var i=0;i<_1.length;i++){ -c=_1.charAt(i); -switch(c){ -case "\\": -_3+=c; -i++; -_3+=_1.charAt(i); -break; -case "*": -_3+=".*"; -break; -case "?": -_3+="."; -break; -case "$": -case "^": -case "/": -case "+": -case ".": -case "|": -case "(": -case ")": -case "{": -case "}": -case "[": -case "]": -_3+="\\"; -default: -_3+=c; -} -} -_3+="$"; -if(_2){ -return new RegExp(_3,"mi"); -}else{ -return new RegExp(_3,"m"); -} + +dojo.data.util.filter.patternToRegExp = function(/*String*/pattern, /*boolean?*/ ignoreCase){ + // summary: + // Helper function to convert a simple pattern to a regular expression for matching. + // description: + // Returns a regular expression object that conforms to the defined conversion rules. + // For example: + // ca* -> /^ca.*$/ + // *ca* -> /^.*ca.*$/ + // *c\*a* -> /^.*c\*a.*$/ + // *c\*a?* -> /^.*c\*a..*$/ + // and so on. + // + // pattern: string + // A simple matching pattern to convert that follows basic rules: + // * Means match anything, so ca* means match anything starting with ca + // ? Means match single character. So, b?b will match to bob and bab, and so on. + // \ is an escape character. So for example, \* means do not treat * as a match, but literal character *. + // To use a \ as a character in the string, it must be escaped. So in the pattern it should be + // represented by \\ to be treated as an ordinary \ character instead of an escape. + // + // ignoreCase: + // An optional flag to indicate if the pattern matching should be treated as case-sensitive or not when comparing + // By default, it is assumed case sensitive. + + var rxp = "^"; + var c = null; + for(var i = 0; i < pattern.length; i++){ + c = pattern.charAt(i); + switch(c){ + case '\\': + rxp += c; + i++; + rxp += pattern.charAt(i); + break; + case '*': + rxp += ".*"; break; + case '?': + rxp += "."; break; + case '$': + case '^': + case '/': + case '+': + case '.': + case '|': + case '(': + case ')': + case '{': + case '}': + case '[': + case ']': + rxp += "\\"; //fallthrough + default: + rxp += c; + } + } + rxp += "$"; + if(ignoreCase){ + return new RegExp(rxp,"mi"); //RegExp + }else{ + return new RegExp(rxp,"m"); //RegExp + } + }; + } diff --git a/lib/dojo/data/util/simpleFetch.js b/lib/dojo/data/util/simpleFetch.js index b3cc365d1..0bfb19ec3 100644 --- a/lib/dojo/data/util/simpleFetch.js +++ b/lib/dojo/data/util/simpleFetch.js @@ -5,60 +5,93 @@ */ -if(!dojo._hasResource["dojo.data.util.simpleFetch"]){ -dojo._hasResource["dojo.data.util.simpleFetch"]=true; +if(!dojo._hasResource["dojo.data.util.simpleFetch"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojo.data.util.simpleFetch"] = true; dojo.provide("dojo.data.util.simpleFetch"); dojo.require("dojo.data.util.sorter"); -dojo.data.util.simpleFetch.fetch=function(_1){ -_1=_1||{}; -if(!_1.store){ -_1.store=this; -} -var _2=this; -var _3=function(_4,_5){ -if(_5.onError){ -var _6=_5.scope||dojo.global; -_5.onError.call(_6,_4,_5); -} -}; -var _7=function(_8,_9){ -var _a=_9.abort||null; -var _b=false; -var _c=_9.start?_9.start:0; -var _d=(_9.count&&(_9.count!==Infinity))?(_c+_9.count):_8.length; -_9.abort=function(){ -_b=true; -if(_a){ -_a.call(_9); -} -}; -var _e=_9.scope||dojo.global; -if(!_9.store){ -_9.store=_2; -} -if(_9.onBegin){ -_9.onBegin.call(_e,_8.length,_9); -} -if(_9.sort){ -_8.sort(dojo.data.util.sorter.createSortFunction(_9.sort,_2)); -} -if(_9.onItem){ -for(var i=_c;(i<_8.length)&&(i<_d);++i){ -var _f=_8[i]; -if(!_b){ -_9.onItem.call(_e,_f,_9); -} -} -} -if(_9.onComplete&&!_b){ -var _10=null; -if(!_9.onItem){ -_10=_8.slice(_c,_d); -} -_9.onComplete.call(_e,_10,_9); -} -}; -this._fetchItems(_1,_7,_3); -return _1; + +dojo.data.util.simpleFetch.fetch = function(/* Object? */ request){ + // summary: + // The simpleFetch mixin is designed to serve as a set of function(s) that can + // be mixed into other datastore implementations to accelerate their development. + // The simpleFetch mixin should work well for any datastore that can respond to a _fetchItems() + // call by returning an array of all the found items that matched the query. The simpleFetch mixin + // is not designed to work for datastores that respond to a fetch() call by incrementally + // loading items, or sequentially loading partial batches of the result + // set. For datastores that mixin simpleFetch, simpleFetch + // implements a fetch method that automatically handles eight of the fetch() + // arguments -- onBegin, onItem, onComplete, onError, start, count, sort and scope + // The class mixing in simpleFetch should not implement fetch(), + // but should instead implement a _fetchItems() method. The _fetchItems() + // method takes three arguments, the keywordArgs object that was passed + // to fetch(), a callback function to be called when the result array is + // available, and an error callback to be called if something goes wrong. + // The _fetchItems() method should ignore any keywordArgs parameters for + // start, count, onBegin, onItem, onComplete, onError, sort, and scope. + // The _fetchItems() method needs to correctly handle any other keywordArgs + // parameters, including the query parameter and any optional parameters + // (such as includeChildren). The _fetchItems() method should create an array of + // result items and pass it to the fetchHandler along with the original request object + // -- or, the _fetchItems() method may, if it wants to, create an new request object + // with other specifics about the request that are specific to the datastore and pass + // that as the request object to the handler. + // + // For more information on this specific function, see dojo.data.api.Read.fetch() + request = request || {}; + if(!request.store){ + request.store = this; + } + var self = this; + + var _errorHandler = function(errorData, requestObject){ + if(requestObject.onError){ + var scope = requestObject.scope || dojo.global; + requestObject.onError.call(scope, errorData, requestObject); + } + }; + + var _fetchHandler = function(items, requestObject){ + var oldAbortFunction = requestObject.abort || null; + var aborted = false; + + var startIndex = requestObject.start?requestObject.start:0; + var endIndex = (requestObject.count && (requestObject.count !== Infinity))?(startIndex + requestObject.count):items.length; + + requestObject.abort = function(){ + aborted = true; + if(oldAbortFunction){ + oldAbortFunction.call(requestObject); + } + }; + + var scope = requestObject.scope || dojo.global; + if(!requestObject.store){ + requestObject.store = self; + } + if(requestObject.onBegin){ + requestObject.onBegin.call(scope, items.length, requestObject); + } + if(requestObject.sort){ + items.sort(dojo.data.util.sorter.createSortFunction(requestObject.sort, self)); + } + if(requestObject.onItem){ + for(var i = startIndex; (i < items.length) && (i < endIndex); ++i){ + var item = items[i]; + if(!aborted){ + requestObject.onItem.call(scope, item, requestObject); + } + } + } + if(requestObject.onComplete && !aborted){ + var subset = null; + if(!requestObject.onItem){ + subset = items.slice(startIndex, endIndex); + } + requestObject.onComplete.call(scope, subset, requestObject); + } + }; + this._fetchItems(request, _fetchHandler, _errorHandler); + return request; // Object }; + } diff --git a/lib/dojo/data/util/sorter.js b/lib/dojo/data/util/sorter.js index ace781274..c0261c848 100644 --- a/lib/dojo/data/util/sorter.js +++ b/lib/dojo/data/util/sorter.js @@ -5,62 +5,98 @@ */ -if(!dojo._hasResource["dojo.data.util.sorter"]){ -dojo._hasResource["dojo.data.util.sorter"]=true; +if(!dojo._hasResource["dojo.data.util.sorter"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojo.data.util.sorter"] = true; dojo.provide("dojo.data.util.sorter"); -dojo.data.util.sorter.basicComparator=function(a,b){ -var r=-1; -if(a===null){ -a=undefined; -} -if(b===null){ -b=undefined; -} -if(a==b){ -r=0; -}else{ -if(a>b||a==null){ -r=1; -} -} -return r; -}; -dojo.data.util.sorter.createSortFunction=function(_1,_2){ -var _3=[]; -function _4(_5,_6,_7,s){ -return function(_8,_9){ -var a=s.getValue(_8,_5); -var b=s.getValue(_9,_5); -return _6*_7(a,b); -}; -}; -var _a; -var _b=_2.comparatorMap; -var bc=dojo.data.util.sorter.basicComparator; -for(var i=0;i<_1.length;i++){ -_a=_1[i]; -var _c=_a.attribute; -if(_c){ -var _d=(_a.descending)?-1:1; -var _e=bc; -if(_b){ -if(typeof _c!=="string"&&("toString" in _c)){ -_c=_c.toString(); -} -_e=_b[_c]||bc; -} -_3.push(_4(_c,_d,_e,_2)); -} -} -return function(_f,_10){ -var i=0; -while(i<_3.length){ -var ret=_3[i++](_f,_10); -if(ret!==0){ -return ret; -} -} -return 0; + +dojo.data.util.sorter.basicComparator = function( /*anything*/ a, + /*anything*/ b){ + // summary: + // Basic comparision function that compares if an item is greater or less than another item + // description: + // returns 1 if a > b, -1 if a < b, 0 if equal. + // 'null' values (null, undefined) are treated as larger values so that they're pushed to the end of the list. + // And compared to each other, null is equivalent to undefined. + + //null is a problematic compare, so if null, we set to undefined. + //Makes the check logic simple, compact, and consistent + //And (null == undefined) === true, so the check later against null + //works for undefined and is less bytes. + var r = -1; + if(a === null){ + a = undefined; + } + if(b === null){ + b = undefined; + } + if(a == b){ + r = 0; + }else if(a > b || a == null){ + r = 1; + } + return r; //int {-1,0,1} }; + +dojo.data.util.sorter.createSortFunction = function( /* attributes array */sortSpec, + /*dojo.data.core.Read*/ store){ + // summary: + // Helper function to generate the sorting function based off the list of sort attributes. + // description: + // The sort function creation will look for a property on the store called 'comparatorMap'. If it exists + // it will look in the mapping for comparisons function for the attributes. If one is found, it will + // use it instead of the basic comparator, which is typically used for strings, ints, booleans, and dates. + // Returns the sorting function for this particular list of attributes and sorting directions. + // + // sortSpec: array + // A JS object that array that defines out what attribute names to sort on and whether it should be descenting or asending. + // The objects should be formatted as follows: + // { + // attribute: "attributeName-string" || attribute, + // descending: true|false; // Default is false. + // } + // store: object + // The datastore object to look up item values from. + // + var sortFunctions=[]; + + function createSortFunction(attr, dir, comp, s){ + //Passing in comp and s (comparator and store), makes this + //function much faster. + return function(itemA, itemB){ + var a = s.getValue(itemA, attr); + var b = s.getValue(itemB, attr); + return dir * comp(a,b); //int + }; + } + var sortAttribute; + var map = store.comparatorMap; + var bc = dojo.data.util.sorter.basicComparator; + for(var i = 0; i < sortSpec.length; i++){ + sortAttribute = sortSpec[i]; + var attr = sortAttribute.attribute; + if(attr){ + var dir = (sortAttribute.descending) ? -1 : 1; + var comp = bc; + if(map){ + if(typeof attr !== "string" && ("toString" in attr)){ + attr = attr.toString(); + } + comp = map[attr] || bc; + } + sortFunctions.push(createSortFunction(attr, + dir, comp, store)); + } + } + return function(rowA, rowB){ + var i=0; + while(i < sortFunctions.length){ + var ret = sortFunctions[i++](rowA, rowB); + if(ret !== 0){ + return ret;//int + } + } + return 0; //int + }; // Function }; + } |