123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713 |
- define("dojo/query", ["./_base/kernel", "./has", "./dom", "./on", "./_base/array", "./_base/lang", "./selector/_loader", "./selector/_loader!default"],
- function(dojo, has, dom, on, array, lang, loader, defaultEngine){
- "use strict";
- has.add("array-extensible", function(){
-
- return lang.delegate([], {length: 1}).length == 1 && !has("bug-for-in-skips-shadowed");
- });
-
- var ap = Array.prototype, aps = ap.slice, apc = ap.concat, forEach = array.forEach;
- var tnl = function(/*Array*/ a, /*dojo.NodeList?*/ parent, /*Function?*/ NodeListCtor){
-
-
-
-
-
-
-
-
-
-
-
-
- var nodeList = new (NodeListCtor || this._NodeListCtor || nl)(a);
- return parent ? nodeList._stash(parent) : nodeList;
- };
- var loopBody = function(f, a, o){
- a = [0].concat(aps.call(a, 0));
- o = o || dojo.global;
- return function(node){
- a[0] = node;
- return f.apply(o, a);
- };
- };
-
- var adaptAsForEach = function(f, o){
-
-
-
-
-
-
-
-
- return function(){
- this.forEach(loopBody(f, arguments, o));
- return this;
- };
- };
- var adaptAsMap = function(f, o){
-
-
-
-
-
-
-
- return function(){
- return this.map(loopBody(f, arguments, o));
- };
- };
- var adaptAsFilter = function(f, o){
-
-
-
-
-
-
- return function(){
- return this.filter(loopBody(f, arguments, o));
- };
- };
- var adaptWithCondition = function(f, g, o){
-
-
-
-
-
-
-
-
-
- return function(){
- var a = arguments, body = loopBody(f, a, o);
- if(g.call(o || dojo.global, a)){
- return this.map(body);
- }
- this.forEach(body);
- return this;
- };
- };
- var NodeList = function(array){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var isNew = this instanceof nl && has("array-extensible");
- if(typeof array == "number"){
- array = Array(array);
- }
- var nodeArray = (array && "length" in array) ? array : arguments;
- if(isNew || !nodeArray.sort){
-
- var target = isNew ? this : [],
- l = target.length = nodeArray.length;
- for(var i = 0; i < l; i++){
- target[i] = nodeArray[i];
- }
- if(isNew){
-
-
-
- return target;
- }
- nodeArray = target;
- }
-
-
- lang._mixin(nodeArray, nlp);
- nodeArray._NodeListCtor = function(array){
-
- return nl(array);
- };
- return nodeArray;
- };
-
- var nl = NodeList, nlp = nl.prototype =
- has("array-extensible") ? [] : {};
-
- nl._wrap = nlp._wrap = tnl;
- nl._adaptAsMap = adaptAsMap;
- nl._adaptAsForEach = adaptAsForEach;
- nl._adaptAsFilter = adaptAsFilter;
- nl._adaptWithCondition = adaptWithCondition;
-
-
- forEach(["slice", "splice"], function(name){
- var f = ap[name];
-
-
-
-
- nlp[name] = function(){ return this._wrap(f.apply(this, arguments), name == "slice" ? this : null); };
- });
-
-
- forEach(["indexOf", "lastIndexOf", "every", "some"], function(name){
- var f = array[name];
- nlp[name] = function(){ return f.apply(dojo, [this].concat(aps.call(arguments, 0))); };
- });
-
- lang.extend(NodeList, {
-
- constructor: nl,
- _NodeListCtor: nl,
- toString: function(){
-
- return this.join(",");
- },
- _stash: function(parent){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- this._parent = parent;
- return this;
- },
- on: function(eventName, listener){
-
-
-
-
-
-
-
-
-
- var handles = this.map(function(node){
- return on(node, eventName, listener);
- });
- handles.remove = function(){
- for(var i = 0; i < handles.length; i++){
- handles[i].remove();
- }
- };
- return handles;
- },
- end: function(){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if(this._parent){
- return this._parent;
- }else{
-
- return new this._NodeListCtor(0);
- }
- },
-
-
-
-
-
-
-
-
-
- concat: function(item){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var t = lang.isArray(this) ? this : aps.call(this, 0),
- m = array.map(arguments, function(a){
- return a && !lang.isArray(a) &&
- (typeof NodeList != "undefined" && a.constructor === NodeList || a.constructor === this._NodeListCtor) ?
- aps.call(a, 0) : a;
- });
- return this._wrap(apc.apply(t, m), this);
- },
- map: function(/*Function*/ func, /*Function?*/ obj){
-
-
-
-
-
- return this._wrap(array.map(this, func, obj), this);
- },
- forEach: function(callback, thisObj){
-
-
-
-
- forEach(this, callback, thisObj);
-
- return this;
- },
- filter: function(/*String|Function*/ filter){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var a = arguments, items = this, start = 0;
- if(typeof filter == "string"){
- items = query._filterResult(this, a[0]);
- if(a.length == 1){
-
- return items._stash(this);
- }
-
- start = 1;
- }
- return this._wrap(array.filter(items, a[start], a[start + 1]), this);
- },
- instantiate: function(/*String|Object*/ declaredClass, /*Object?*/ properties){
-
-
-
-
-
-
-
- var c = lang.isFunction(declaredClass) ? declaredClass : lang.getObject(declaredClass);
- properties = properties || {};
- return this.forEach(function(node){
- new c(properties, node);
- });
- },
- at: function(/*===== index =====*/){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var t = new this._NodeListCtor(0);
- forEach(arguments, function(i){
- if(i < 0){ i = this.length + i; }
- if(this[i]){ t.push(this[i]); }
- }, this);
- return t._stash(this);
- }
- });
- function queryForEngine(engine, NodeList){
- var query = function(/*String*/ query, /*String|DOMNode?*/ root){
-
-
-
-
- if(typeof root == "string"){
- root = dom.byId(root);
- if(!root){
- return new NodeList([]);
- }
- }
- var results = typeof query == "string" ? engine(query, root) : query ? query.orphan ? query : [query] : [];
- if(results.orphan){
-
- return results;
- }
- return new NodeList(results);
- };
- query.matches = engine.match || function(node, selector, root){
-
-
- return query.filter([node], selector, root).length > 0;
- };
-
- query.filter = engine.filter || function(nodes, selector, root){
-
-
- return query(selector, root).filter(function(node){
- return array.indexOf(nodes, node) > -1;
- });
- };
- if(typeof engine != "function"){
- var search = engine.search;
- engine = function(selector, root){
-
- return search(root || document, selector);
- };
- }
- return query;
- }
- var query = queryForEngine(defaultEngine, NodeList);
- dojo.query = queryForEngine(defaultEngine, function(array){
-
- return NodeList(array);
- });
- query.load = function(id, parentRequire, loaded, config){
-
-
-
-
-
-
- loader.load(id, parentRequire, function(engine){
- loaded(queryForEngine(engine, NodeList));
- });
- };
- dojo._filterQueryResult = query._filterResult = function(nodes, selector, root){
- return new NodeList(query.filter(nodes, selector, root));
- };
- dojo.NodeList = query.NodeList = NodeList;
- return query;
- });
|