query.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. define("dojo/query", ["./_base/kernel", "./has", "./dom", "./on", "./_base/array", "./_base/lang", "./selector/_loader", "./selector/_loader!default"],
  2. function(dojo, has, dom, on, array, lang, loader, defaultEngine){
  3. "use strict";
  4. has.add("array-extensible", function(){
  5. // test to see if we can extend an array (not supported in old IE)
  6. return lang.delegate([], {length: 1}).length == 1 && !has("bug-for-in-skips-shadowed");
  7. });
  8. var ap = Array.prototype, aps = ap.slice, apc = ap.concat, forEach = array.forEach;
  9. var tnl = function(/*Array*/ a, /*dojo.NodeList?*/ parent, /*Function?*/ NodeListCtor){
  10. // summary:
  11. // decorate an array to make it look like a `dojo.NodeList`.
  12. // a:
  13. // Array of nodes to decorate.
  14. // parent:
  15. // An optional parent NodeList that generated the current
  16. // list of nodes. Used to call _stash() so the parent NodeList
  17. // can be accessed via end() later.
  18. // NodeListCtor:
  19. // An optional constructor function to use for any
  20. // new NodeList calls. This allows a certain chain of
  21. // NodeList calls to use a different object than dojo.NodeList.
  22. var nodeList = new (NodeListCtor || this._NodeListCtor || nl)(a);
  23. return parent ? nodeList._stash(parent) : nodeList;
  24. };
  25. var loopBody = function(f, a, o){
  26. a = [0].concat(aps.call(a, 0));
  27. o = o || dojo.global;
  28. return function(node){
  29. a[0] = node;
  30. return f.apply(o, a);
  31. };
  32. };
  33. // adapters
  34. var adaptAsForEach = function(f, o){
  35. // summary:
  36. // adapts a single node function to be used in the forEach-type
  37. // actions. The initial object is returned from the specialized
  38. // function.
  39. // f: Function
  40. // a function to adapt
  41. // o: Object?
  42. // an optional context for f
  43. return function(){
  44. this.forEach(loopBody(f, arguments, o));
  45. return this; // Object
  46. };
  47. };
  48. var adaptAsMap = function(f, o){
  49. // summary:
  50. // adapts a single node function to be used in the map-type
  51. // actions. The return is a new array of values, as via `dojo.map`
  52. // f: Function
  53. // a function to adapt
  54. // o: Object?
  55. // an optional context for f
  56. return function(){
  57. return this.map(loopBody(f, arguments, o));
  58. };
  59. };
  60. var adaptAsFilter = function(f, o){
  61. // summary:
  62. // adapts a single node function to be used in the filter-type actions
  63. // f: Function
  64. // a function to adapt
  65. // o: Object?
  66. // an optional context for f
  67. return function(){
  68. return this.filter(loopBody(f, arguments, o));
  69. };
  70. };
  71. var adaptWithCondition = function(f, g, o){
  72. // summary:
  73. // adapts a single node function to be used in the map-type
  74. // actions, behaves like forEach() or map() depending on arguments
  75. // f: Function
  76. // a function to adapt
  77. // g: Function
  78. // a condition function, if true runs as map(), otherwise runs as forEach()
  79. // o: Object?
  80. // an optional context for f and g
  81. return function(){
  82. var a = arguments, body = loopBody(f, a, o);
  83. if(g.call(o || dojo.global, a)){
  84. return this.map(body); // self
  85. }
  86. this.forEach(body);
  87. return this; // self
  88. };
  89. };
  90. var NodeList = function(array){
  91. // summary:
  92. // dojo.NodeList is an of Array-like object which adds syntactic
  93. // sugar for chaining, common iteration operations, animation, and
  94. // node manipulation. NodeLists are most often returned as the
  95. // result of dojo.query() calls.
  96. // description:
  97. // dojo.NodeList instances provide many utilities that reflect
  98. // core Dojo APIs for Array iteration and manipulation, DOM
  99. // manipulation, and event handling. Instead of needing to dig up
  100. // functions in the dojo.* namespace, NodeLists generally make the
  101. // full power of Dojo available for DOM manipulation tasks in a
  102. // simple, chainable way.
  103. // example:
  104. // create a node list from a node
  105. // | new dojo.NodeList(dojo.byId("foo"));
  106. // example:
  107. // get a NodeList from a CSS query and iterate on it
  108. // | var l = dojo.query(".thinger");
  109. // | l.forEach(function(node, index, nodeList){
  110. // | console.log(index, node.innerHTML);
  111. // | });
  112. // example:
  113. // use native and Dojo-provided array methods to manipulate a
  114. // NodeList without needing to use dojo.* functions explicitly:
  115. // | var l = dojo.query(".thinger");
  116. // | // since NodeLists are real arrays, they have a length
  117. // | // property that is both readable and writable and
  118. // | // push/pop/shift/unshift methods
  119. // | console.log(l.length);
  120. // | l.push(dojo.create("span"));
  121. // |
  122. // | // dojo's normalized array methods work too:
  123. // | console.log( l.indexOf(dojo.byId("foo")) );
  124. // | // ...including the special "function as string" shorthand
  125. // | console.log( l.every("item.nodeType == 1") );
  126. // |
  127. // | // NodeLists can be [..] indexed, or you can use the at()
  128. // | // function to get specific items wrapped in a new NodeList:
  129. // | var node = l[3]; // the 4th element
  130. // | var newList = l.at(1, 3); // the 2nd and 4th elements
  131. // example:
  132. // the style functions you expect are all there too:
  133. // | // style() as a getter...
  134. // | var borders = dojo.query(".thinger").style("border");
  135. // | // ...and as a setter:
  136. // | dojo.query(".thinger").style("border", "1px solid black");
  137. // | // class manipulation
  138. // | dojo.query("li:nth-child(even)").addClass("even");
  139. // | // even getting the coordinates of all the items
  140. // | var coords = dojo.query(".thinger").coords();
  141. // example:
  142. // DOM manipulation functions from the dojo.* namespace area also
  143. // available:
  144. // | // remove all of the elements in the list from their
  145. // | // parents (akin to "deleting" them from the document)
  146. // | dojo.query(".thinger").orphan();
  147. // | // place all elements in the list at the front of #foo
  148. // | dojo.query(".thinger").place("foo", "first");
  149. // example:
  150. // Event handling couldn't be easier. `dojo.connect` is mapped in,
  151. // and shortcut handlers are provided for most DOM events:
  152. // | // like dojo.connect(), but with implicit scope
  153. // | dojo.query("li").connect("onclick", console, "log");
  154. // |
  155. // | // many common event handlers are already available directly:
  156. // | dojo.query("li").onclick(console, "log");
  157. // | var toggleHovered = dojo.hitch(dojo, "toggleClass", "hovered");
  158. // | dojo.query("p")
  159. // | .onmouseenter(toggleHovered)
  160. // | .onmouseleave(toggleHovered);
  161. // example:
  162. // chainability is a key advantage of NodeLists:
  163. // | dojo.query(".thinger")
  164. // | .onclick(function(e){ /* ... */ })
  165. // | .at(1, 3, 8) // get a subset
  166. // | .style("padding", "5px")
  167. // | .forEach(console.log);
  168. var isNew = this instanceof nl && has("array-extensible");
  169. if(typeof array == "number"){
  170. array = Array(array);
  171. }
  172. var nodeArray = (array && "length" in array) ? array : arguments;
  173. if(isNew || !nodeArray.sort){
  174. // make sure it's a real array before we pass it on to be wrapped
  175. var target = isNew ? this : [],
  176. l = target.length = nodeArray.length;
  177. for(var i = 0; i < l; i++){
  178. target[i] = nodeArray[i];
  179. }
  180. if(isNew){
  181. // called with new operator, this means we are going to use this instance and push
  182. // the nodes on to it. This is usually much faster since the NodeList properties
  183. // don't need to be copied (unless the list of nodes is extremely large).
  184. return target;
  185. }
  186. nodeArray = target;
  187. }
  188. // called without new operator, use a real array and copy prototype properties,
  189. // this is slower and exists for back-compat. Should be removed in 2.0.
  190. lang._mixin(nodeArray, nlp);
  191. nodeArray._NodeListCtor = function(array){
  192. // call without new operator to preserve back-compat behavior
  193. return nl(array);
  194. };
  195. return nodeArray;
  196. };
  197. var nl = NodeList, nlp = nl.prototype =
  198. has("array-extensible") ? [] : {};// extend an array if it is extensible
  199. // expose adapters and the wrapper as private functions
  200. nl._wrap = nlp._wrap = tnl;
  201. nl._adaptAsMap = adaptAsMap;
  202. nl._adaptAsForEach = adaptAsForEach;
  203. nl._adaptAsFilter = adaptAsFilter;
  204. nl._adaptWithCondition = adaptWithCondition;
  205. // mass assignment
  206. // add array redirectors
  207. forEach(["slice", "splice"], function(name){
  208. var f = ap[name];
  209. //Use a copy of the this array via this.slice() to allow .end() to work right in the splice case.
  210. // CANNOT apply ._stash()/end() to splice since it currently modifies
  211. // the existing this array -- it would break backward compatibility if we copy the array before
  212. // the splice so that we can use .end(). So only doing the stash option to this._wrap for slice.
  213. nlp[name] = function(){ return this._wrap(f.apply(this, arguments), name == "slice" ? this : null); };
  214. });
  215. // concat should be here but some browsers with native NodeList have problems with it
  216. // add array.js redirectors
  217. forEach(["indexOf", "lastIndexOf", "every", "some"], function(name){
  218. var f = array[name];
  219. nlp[name] = function(){ return f.apply(dojo, [this].concat(aps.call(arguments, 0))); };
  220. });
  221. /*===== var NodeList = dojo.NodeList; =====*/
  222. lang.extend(NodeList, {
  223. // copy the constructors
  224. constructor: nl,
  225. _NodeListCtor: nl,
  226. toString: function(){
  227. // Array.prototype.toString can't be applied to objects, so we use join
  228. return this.join(",");
  229. },
  230. _stash: function(parent){
  231. // summary:
  232. // private function to hold to a parent NodeList. end() to return the parent NodeList.
  233. //
  234. // example:
  235. // How to make a `dojo.NodeList` method that only returns the third node in
  236. // the dojo.NodeList but allows access to the original NodeList by using this._stash:
  237. // | dojo.extend(dojo.NodeList, {
  238. // | third: function(){
  239. // | var newNodeList = dojo.NodeList(this[2]);
  240. // | return newNodeList._stash(this);
  241. // | }
  242. // | });
  243. // | // then see how _stash applies a sub-list, to be .end()'ed out of
  244. // | dojo.query(".foo")
  245. // | .third()
  246. // | .addClass("thirdFoo")
  247. // | .end()
  248. // | // access to the orig .foo list
  249. // | .removeClass("foo")
  250. // |
  251. //
  252. this._parent = parent;
  253. return this; //dojo.NodeList
  254. },
  255. on: function(eventName, listener){
  256. // summary:
  257. // Listen for events on the nodes in the NodeList. Basic usage is:
  258. // | query(".my-class").on("click", listener);
  259. // This supports event delegation by using selectors as the first argument with the event names as
  260. // pseudo selectors. For example:
  261. // | dojo.query("#my-list").on("li:click", listener);
  262. // This will listen for click events within <li> elements that are inside the #my-list element.
  263. // Because on supports CSS selector syntax, we can use comma-delimited events as well:
  264. // | dojo.query("#my-list").on("li button:mouseover, li:click", listener);
  265. var handles = this.map(function(node){
  266. return on(node, eventName, listener); // TODO: apply to the NodeList so the same selector engine is used for matches
  267. });
  268. handles.remove = function(){
  269. for(var i = 0; i < handles.length; i++){
  270. handles[i].remove();
  271. }
  272. };
  273. return handles;
  274. },
  275. end: function(){
  276. // summary:
  277. // Ends use of the current `dojo.NodeList` by returning the previous dojo.NodeList
  278. // that generated the current dojo.NodeList.
  279. // description:
  280. // Returns the `dojo.NodeList` that generated the current `dojo.NodeList`. If there
  281. // is no parent dojo.NodeList, an empty dojo.NodeList is returned.
  282. // example:
  283. // | dojo.query("a")
  284. // | .filter(".disabled")
  285. // | // operate on the anchors that only have a disabled class
  286. // | .style("color", "grey")
  287. // | .end()
  288. // | // jump back to the list of anchors
  289. // | .style(...)
  290. //
  291. if(this._parent){
  292. return this._parent;
  293. }else{
  294. //Just return empty list.
  295. return new this._NodeListCtor(0);
  296. }
  297. },
  298. // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array#Methods
  299. // FIXME: handle return values for #3244
  300. // http://trac.dojotoolkit.org/ticket/3244
  301. // FIXME:
  302. // need to wrap or implement:
  303. // join (perhaps w/ innerHTML/outerHTML overload for toString() of items?)
  304. // reduce
  305. // reduceRight
  306. /*=====
  307. slice: function(begin, end){
  308. // summary:
  309. // Returns a new NodeList, maintaining this one in place
  310. // description:
  311. // This method behaves exactly like the Array.slice method
  312. // with the caveat that it returns a dojo.NodeList and not a
  313. // raw Array. For more details, see Mozilla's (slice
  314. // documentation)[http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:slice]
  315. // begin: Integer
  316. // Can be a positive or negative integer, with positive
  317. // integers noting the offset to begin at, and negative
  318. // integers denoting an offset from the end (i.e., to the left
  319. // of the end)
  320. // end: Integer?
  321. // Optional parameter to describe what position relative to
  322. // the NodeList's zero index to end the slice at. Like begin,
  323. // can be positive or negative.
  324. return this._wrap(a.slice.apply(this, arguments));
  325. },
  326. splice: function(index, howmany, item){
  327. // summary:
  328. // Returns a new NodeList, manipulating this NodeList based on
  329. // the arguments passed, potentially splicing in new elements
  330. // at an offset, optionally deleting elements
  331. // description:
  332. // This method behaves exactly like the Array.splice method
  333. // with the caveat that it returns a dojo.NodeList and not a
  334. // raw Array. For more details, see Mozilla's (splice
  335. // documentation)[http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:splice]
  336. // For backwards compatibility, calling .end() on the spliced NodeList
  337. // does not return the original NodeList -- splice alters the NodeList in place.
  338. // index: Integer
  339. // begin can be a positive or negative integer, with positive
  340. // integers noting the offset to begin at, and negative
  341. // integers denoting an offset from the end (i.e., to the left
  342. // of the end)
  343. // howmany: Integer?
  344. // Optional parameter to describe what position relative to
  345. // the NodeList's zero index to end the slice at. Like begin,
  346. // can be positive or negative.
  347. // item: Object...?
  348. // Any number of optional parameters may be passed in to be
  349. // spliced into the NodeList
  350. // returns:
  351. // dojo.NodeList
  352. return this._wrap(a.splice.apply(this, arguments));
  353. },
  354. indexOf: function(value, fromIndex){
  355. // summary:
  356. // see dojo.indexOf(). The primary difference is that the acted-on
  357. // array is implicitly this NodeList
  358. // value: Object:
  359. // The value to search for.
  360. // fromIndex: Integer?:
  361. // The location to start searching from. Optional. Defaults to 0.
  362. // description:
  363. // For more details on the behavior of indexOf, see Mozilla's
  364. // (indexOf
  365. // docs)[http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:indexOf]
  366. // returns:
  367. // Positive Integer or 0 for a match, -1 of not found.
  368. return d.indexOf(this, value, fromIndex); // Integer
  369. },
  370. lastIndexOf: function(value, fromIndex){
  371. // summary:
  372. // see dojo.lastIndexOf(). The primary difference is that the
  373. // acted-on array is implicitly this NodeList
  374. // description:
  375. // For more details on the behavior of lastIndexOf, see
  376. // Mozilla's (lastIndexOf
  377. // docs)[http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:lastIndexOf]
  378. // value: Object
  379. // The value to search for.
  380. // fromIndex: Integer?
  381. // The location to start searching from. Optional. Defaults to 0.
  382. // returns:
  383. // Positive Integer or 0 for a match, -1 of not found.
  384. return d.lastIndexOf(this, value, fromIndex); // Integer
  385. },
  386. every: function(callback, thisObject){
  387. // summary:
  388. // see `dojo.every()` and the (Array.every
  389. // docs)[http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:every].
  390. // Takes the same structure of arguments and returns as
  391. // dojo.every() with the caveat that the passed array is
  392. // implicitly this NodeList
  393. // callback: Function: the callback
  394. // thisObject: Object?: the context
  395. return d.every(this, callback, thisObject); // Boolean
  396. },
  397. some: function(callback, thisObject){
  398. // summary:
  399. // Takes the same structure of arguments and returns as
  400. // `dojo.some()` with the caveat that the passed array is
  401. // implicitly this NodeList. See `dojo.some()` and Mozilla's
  402. // (Array.some
  403. // documentation)[http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:some].
  404. // callback: Function: the callback
  405. // thisObject: Object?: the context
  406. return d.some(this, callback, thisObject); // Boolean
  407. },
  408. =====*/
  409. concat: function(item){
  410. // summary:
  411. // Returns a new NodeList comprised of items in this NodeList
  412. // as well as items passed in as parameters
  413. // description:
  414. // This method behaves exactly like the Array.concat method
  415. // with the caveat that it returns a `dojo.NodeList` and not a
  416. // raw Array. For more details, see the (Array.concat
  417. // docs)[http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:concat]
  418. // item: Object?
  419. // Any number of optional parameters may be passed in to be
  420. // spliced into the NodeList
  421. // returns:
  422. // dojo.NodeList
  423. //return this._wrap(apc.apply(this, arguments));
  424. // the line above won't work for the native NodeList :-(
  425. // implementation notes:
  426. // 1) Native NodeList is not an array, and cannot be used directly
  427. // in concat() --- the latter doesn't recognize it as an array, and
  428. // does not inline it, but append as a single entity.
  429. // 2) On some browsers (e.g., Safari) the "constructor" property is
  430. // read-only and cannot be changed. So we have to test for both
  431. // native NodeList and dojo.NodeList in this property to recognize
  432. // the node list.
  433. var t = lang.isArray(this) ? this : aps.call(this, 0),
  434. m = array.map(arguments, function(a){
  435. return a && !lang.isArray(a) &&
  436. (typeof NodeList != "undefined" && a.constructor === NodeList || a.constructor === this._NodeListCtor) ?
  437. aps.call(a, 0) : a;
  438. });
  439. return this._wrap(apc.apply(t, m), this); // dojo.NodeList
  440. },
  441. map: function(/*Function*/ func, /*Function?*/ obj){
  442. // summary:
  443. // see dojo.map(). The primary difference is that the acted-on
  444. // array is implicitly this NodeList and the return is a
  445. // dojo.NodeList (a subclass of Array)
  446. ///return d.map(this, func, obj, d.NodeList); // dojo.NodeList
  447. return this._wrap(array.map(this, func, obj), this); // dojo.NodeList
  448. },
  449. forEach: function(callback, thisObj){
  450. // summary:
  451. // see `dojo.forEach()`. The primary difference is that the acted-on
  452. // array is implicitly this NodeList. If you want the option to break out
  453. // of the forEach loop, use every() or some() instead.
  454. forEach(this, callback, thisObj);
  455. // non-standard return to allow easier chaining
  456. return this; // dojo.NodeList
  457. },
  458. filter: function(/*String|Function*/ filter){
  459. // summary:
  460. // "masks" the built-in javascript filter() method (supported
  461. // in Dojo via `dojo.filter`) to support passing a simple
  462. // string filter in addition to supporting filtering function
  463. // objects.
  464. // filter:
  465. // If a string, a CSS rule like ".thinger" or "div > span".
  466. // example:
  467. // "regular" JS filter syntax as exposed in dojo.filter:
  468. // | dojo.query("*").filter(function(item){
  469. // | // highlight every paragraph
  470. // | return (item.nodeName == "p");
  471. // | }).style("backgroundColor", "yellow");
  472. // example:
  473. // the same filtering using a CSS selector
  474. // | dojo.query("*").filter("p").styles("backgroundColor", "yellow");
  475. var a = arguments, items = this, start = 0;
  476. if(typeof filter == "string"){ // inline'd type check
  477. items = query._filterResult(this, a[0]);
  478. if(a.length == 1){
  479. // if we only got a string query, pass back the filtered results
  480. return items._stash(this); // dojo.NodeList
  481. }
  482. // if we got a callback, run it over the filtered items
  483. start = 1;
  484. }
  485. return this._wrap(array.filter(items, a[start], a[start + 1]), this); // dojo.NodeList
  486. },
  487. instantiate: function(/*String|Object*/ declaredClass, /*Object?*/ properties){
  488. // summary:
  489. // Create a new instance of a specified class, using the
  490. // specified properties and each node in the nodeList as a
  491. // srcNodeRef.
  492. // example:
  493. // Grabs all buttons in the page and converts them to diji.form.Buttons.
  494. // | var buttons = dojo.query("button").instantiate("dijit.form.Button", {showLabel: true});
  495. var c = lang.isFunction(declaredClass) ? declaredClass : lang.getObject(declaredClass);
  496. properties = properties || {};
  497. return this.forEach(function(node){
  498. new c(properties, node);
  499. }); // dojo.NodeList
  500. },
  501. at: function(/*===== index =====*/){
  502. // summary:
  503. // Returns a new NodeList comprised of items in this NodeList
  504. // at the given index or indices.
  505. //
  506. // index: Integer...
  507. // One or more 0-based indices of items in the current
  508. // NodeList. A negative index will start at the end of the
  509. // list and go backwards.
  510. //
  511. // example:
  512. // Shorten the list to the first, second, and third elements
  513. // | dojo.query("a").at(0, 1, 2).forEach(fn);
  514. //
  515. // example:
  516. // Retrieve the first and last elements of a unordered list:
  517. // | dojo.query("ul > li").at(0, -1).forEach(cb);
  518. //
  519. // example:
  520. // Do something for the first element only, but end() out back to
  521. // the original list and continue chaining:
  522. // | dojo.query("a").at(0).onclick(fn).end().forEach(function(n){
  523. // | console.log(n); // all anchors on the page.
  524. // | })
  525. //
  526. // returns:
  527. // dojo.NodeList
  528. var t = new this._NodeListCtor(0);
  529. forEach(arguments, function(i){
  530. if(i < 0){ i = this.length + i; }
  531. if(this[i]){ t.push(this[i]); }
  532. }, this);
  533. return t._stash(this); // dojo.NodeList
  534. }
  535. });
  536. /*=====
  537. dojo.query = function(selector, context){
  538. // summary:
  539. // This modules provides DOM querying functionality. The module export is a function
  540. // that can be used to query for DOM nodes by CSS selector and returns a dojo.NodeList
  541. // representing the matching nodes.
  542. //
  543. // selector: String
  544. // A CSS selector to search for.
  545. // context: String|DomNode?
  546. // An optional context to limit the searching scope. Only nodes under `context` will be
  547. // scanned.
  548. //
  549. // example:
  550. // add an onclick handler to every submit button in the document
  551. // which causes the form to be sent via Ajax instead:
  552. // | define(["dojo/query"], function(query){
  553. // | query("input[type='submit']").on("click", function(e){
  554. // | dojo.stopEvent(e); // prevent sending the form
  555. // | var btn = e.target;
  556. // | dojo.xhrPost({
  557. // | form: btn.form,
  558. // | load: function(data){
  559. // | // replace the form with the response
  560. // | var div = dojo.doc.createElement("div");
  561. // | dojo.place(div, btn.form, "after");
  562. // | div.innerHTML = data;
  563. // | dojo.style(btn.form, "display", "none");
  564. // | }
  565. // | });
  566. // | });
  567. //
  568. // description:
  569. // dojo/query is responsible for loading the appropriate query engine and wrapping
  570. // its results with a `dojo.NodeList`. You can use dojo/query with a specific selector engine
  571. // by using it as a plugin. For example, if you installed the sizzle package, you could
  572. // use it as the selector engine with:
  573. // | define("dojo/query!sizzle", function(query){
  574. // | query("div")...
  575. //
  576. // The id after the ! can be a module id of the selector engine or one of the following values:
  577. // | + acme: This is the default engine used by Dojo base, and will ensure that the full
  578. // | Acme engine is always loaded.
  579. // |
  580. // | + css2: If the browser has a native selector engine, this will be used, otherwise a
  581. // | very minimal lightweight selector engine will be loaded that can do simple CSS2 selectors
  582. // | (by #id, .class, tag, and [name=value] attributes, with standard child or descendant (>)
  583. // | operators) and nothing more.
  584. // |
  585. // | + css2.1: If the browser has a native selector engine, this will be used, otherwise the
  586. // | full Acme engine will be loaded.
  587. // |
  588. // | + css3: If the browser has a native selector engine with support for CSS3 pseudo
  589. // | selectors (most modern browsers except IE8), this will be used, otherwise the
  590. // | full Acme engine will be loaded.
  591. // |
  592. // | + Or the module id of a selector engine can be used to explicitly choose the selector engine
  593. //
  594. // For example, if you are using CSS3 pseudo selectors in module, you can specify that
  595. // you will need support them with:
  596. // | define("dojo/query!css3", function(query){
  597. // | query('#t > h3:nth-child(odd)')...
  598. //
  599. // You can also choose the selector engine/load configuration by setting the <FIXME:what is the configuration setting?>.
  600. // For example:
  601. // | <script data-dojo-config="query-selector:'css3'" src="dojo.js"></script>
  602. //
  603. return new dojo.NodeList(); // dojo.NodeList
  604. };
  605. =====*/
  606. function queryForEngine(engine, NodeList){
  607. var query = function(/*String*/ query, /*String|DOMNode?*/ root){
  608. // summary:
  609. // Returns nodes which match the given CSS selector, searching the
  610. // entire document by default but optionally taking a node to scope
  611. // the search by. Returns an instance of dojo.NodeList.
  612. if(typeof root == "string"){
  613. root = dom.byId(root);
  614. if(!root){
  615. return new NodeList([]);
  616. }
  617. }
  618. var results = typeof query == "string" ? engine(query, root) : query ? query.orphan ? query : [query] : [];
  619. if(results.orphan){
  620. // already wrapped
  621. return results;
  622. }
  623. return new NodeList(results);
  624. };
  625. query.matches = engine.match || function(node, selector, root){
  626. // summary:
  627. // Test to see if a node matches a selector
  628. return query.filter([node], selector, root).length > 0;
  629. };
  630. // the engine provides a filtering function, use it to for matching
  631. query.filter = engine.filter || function(nodes, selector, root){
  632. // summary:
  633. // Filters an array of nodes. Note that this does not guarantee to return a dojo.NodeList, just an array.
  634. return query(selector, root).filter(function(node){
  635. return array.indexOf(nodes, node) > -1;
  636. });
  637. };
  638. if(typeof engine != "function"){
  639. var search = engine.search;
  640. engine = function(selector, root){
  641. // Slick does it backwards (or everyone else does it backwards, probably the latter)
  642. return search(root || document, selector);
  643. };
  644. }
  645. return query;
  646. }
  647. var query = queryForEngine(defaultEngine, NodeList);
  648. // the query that is returned from this module is slightly different than dojo.query,
  649. // because dojo.query has to maintain backwards compatibility with returning a
  650. // true array which has performance problems. The query returned from the module
  651. // does not use true arrays, but rather inherits from Array, making it much faster to
  652. // instantiate.
  653. dojo.query = queryForEngine(defaultEngine, function(array){
  654. // call it without the new operator to invoke the back-compat behavior that returns a true array
  655. return NodeList(array);
  656. });
  657. query.load = /*===== dojo.query.load= ======*/ function(id, parentRequire, loaded, config){
  658. // summary: can be used as AMD plugin to conditionally load new query engine
  659. // example:
  660. // | define(["dojo/query!custom"], function(qsa){
  661. // | // loaded selector/custom.js as engine
  662. // | qsa("#foobar").forEach(...);
  663. // | });
  664. loader.load(id, parentRequire, function(engine){
  665. loaded(queryForEngine(engine, NodeList));
  666. });
  667. };
  668. dojo._filterQueryResult = query._filterResult = function(nodes, selector, root){
  669. return new NodeList(query.filter(nodes, selector, root));
  670. };
  671. dojo.NodeList = query.NodeList = NodeList;
  672. return query;
  673. });