jq.js 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.jq"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.jq"] = true;
  8. //For jQuery 1.3.2
  9. dojo.provide("dojox.jq");
  10. dojo.require("dojo.NodeList-traverse");
  11. dojo.require("dojo.NodeList-manipulate");
  12. dojo.require("dojo.io.script");
  13. /*
  14. To get jquery tests to pass:
  15. - add spaces between span>form selectors, other ones like one,two
  16. - .last() instead of :last
  17. - $("<div>").find("#foo") does not work unless the div is attached to the body.
  18. - trigger .test not work
  19. - No jquery.Event thing.
  20. - jQuery.ajax() modifies incoming arguments?
  21. - test framework not good for our io methods, async, poll.
  22. - Dojo ajax is async: we fire ajaxStop more than jquery.
  23. - jquery makes assumptions of a value to set for an element
  24. by inserting an element and checking display. Does not seem to
  25. account for nested styles, only captures generic tag name style off
  26. of body. Why can't we just set display to empty?
  27. OK for failures:
  28. - test("jQuery.ajax - beforeSend, cancel request (#2688)"
  29. We cancel the deferred which triggers error and complete callbacks.
  30. Looked at jquery code for:
  31. - how it handled text(): did not use textContent/innerText, but use recursive look over childNodes and nodeValue,
  32. so it may have impact on how <br> is serialized, but it has uniform behavior across browsers.
  33. - Looked at trigger: how it triggered actions on dom nodes. This seemed unsafe.
  34. */
  35. /*
  36. dojo.query differences that cause some tests to fail:
  37. - does not support XML queries
  38. - $("#sap>form") does not find a match but $("#sap > form") does. Similar issue with comma instead of > (see is() tests)
  39. - "$("form:last") should return the last form object, not if that particular form is that last related
  40. to its siblings? Same issue with :first?
  41. - $("p").filter("#ap, #sndp"): filter does not work.
  42. - dojo.NodeList uses d.NodeList a few places in the code. Would be nice to use a ctor that can be configured.
  43. That would make the filter function operate better.
  44. - filterQueryResult, cannot handle queries like "p, div"? See andSelf test with parents().
  45. - adjacent "p + p" not supported?
  46. = a:only-child not supported?
  47. - nth(1)
  48. - even/odd
  49. - eq/gt/lt
  50. - #form :radio:checked does not run the first :radio psuedo selector? Seems to be a general issue where only the last pseud
  51. is run. For example, "#form :checked:radio" does only the radio pseudo.
  52. */
  53. (function(){
  54. //Enable io topic publishing
  55. dojo.config.ioPublish = true;
  56. //Support stuff for toDom
  57. var selfClosedTags = "|img|meta|hr|br|input|";
  58. function toDom(/*String*/html, /*Document?*/doc){
  59. //summary converts HTML string into DOM nodes.
  60. //Make sure html is a string.
  61. html += "";
  62. //Convert <tag/> into <tag></tag>
  63. html = html.replace(/<\s*(\w+)([^\/\>]*)\/\s*>/g, function(tag, name, contents){
  64. if(selfClosedTags.indexOf("|" + name + "|") == -1){
  65. return "<" + name + contents + "></" + name + ">";
  66. }else{
  67. return tag;
  68. }
  69. });
  70. return dojo._toDom(html, doc);
  71. }
  72. function cssNameToJs(name){
  73. var index = name.indexOf("-");
  74. if(index != -1){
  75. //Strip off beginning dash
  76. if(index == 0){
  77. name = name.substring(1);
  78. }
  79. name = name.replace(/-(\w)/g, function(match, match1){
  80. return match1.toUpperCase();
  81. });
  82. }
  83. return name;
  84. }
  85. var _old$ = dojo.global.$;
  86. var _oldJQuery = dojo.global.jQuery;
  87. var $ = dojo.global.$ = dojo.global.jQuery = function(){
  88. var arg = arguments[0];
  89. if(!arg){
  90. return $._wrap([], null, $);
  91. }else if(dojo.isString(arg)){
  92. if(arg.charAt(0) == "<"){
  93. //String of html that needs nodes created.
  94. arg = toDom(arg);
  95. //If a DocumentFragment, convert to use its children
  96. //since we want to treat all top level nodes as elements
  97. //in the NodeList array.
  98. if(arg.nodeType == 11){
  99. arg = arg.childNodes;
  100. }else{
  101. return $._wrap([arg], null, $);
  102. }
  103. //Use end case of nodelist to finish work.
  104. }else{
  105. //Normal dojo.query selector.
  106. //Switch out query's NodeList constructor to be our specialized
  107. //NodeList constructor.
  108. var listCtor = dojo._NodeListCtor;
  109. dojo._NodeListCtor = $;
  110. //If the second arg is one of our fake NodeLists then
  111. //use the first parent for the call.
  112. var arg2 = arguments[1];
  113. if(arg2 && arg2._is$){
  114. arg2 = arg2[0];
  115. }else if(dojo.isString(arg2)){
  116. arg2 = dojo.query(arg2)[0];
  117. }
  118. var nl = dojo.query.call(this, arg, arg2);
  119. dojo._NodeListCtor = listCtor;
  120. return nl;
  121. }
  122. }else if(dojo.isFunction(arg)){
  123. //The addOnLoad case
  124. $.ready(arg);
  125. return $;
  126. }else if(arg == document || arg == window){
  127. //If the arg is the document or window,
  128. //then just use it directly.
  129. return $._wrap([arg], null, $);
  130. }else if(dojo.isArray(arg)){
  131. //Input is a plain array.
  132. //Filter out duplicates.
  133. var ary = [];
  134. for(var i = 0; i < arg.length; i++){
  135. if(dojo.indexOf(ary, arg[i]) == -1){
  136. ary.push(arg[i]);
  137. }
  138. }
  139. return $._wrap(arg, null, $);
  140. }else if("nodeType" in arg){
  141. //A DOM Node
  142. return $._wrap([arg], null, $);
  143. }
  144. //A native NodeList that does not conform to dojo.isArray().
  145. //Convert it to a workable array and create new NodeList.
  146. return $._wrap(dojo._toArray(arg), null, $);
  147. };
  148. //Set up plugin extension point.
  149. var nlProto = dojo.NodeList.prototype;
  150. //Need a delegate, because at least one method conflicts with jquery
  151. //API: attr(name) in jQuery only returns a single, first value, where
  152. //dojo.attr will return an array.
  153. var f = $.fn = $.prototype = dojo.delegate(nlProto);
  154. //_wrap is required for proper use in dojo.query, but the _adaptAs* methods
  155. //do not have to placed on $ -- they can be used directly off dojo.NodeList.
  156. $._wrap = dojo.NodeList._wrap;
  157. //Add in some pseudos selectors
  158. var headerRegExp = /^H\d/i;
  159. var pseudos = dojo.query.pseudos;
  160. dojo.mixin(pseudos, {
  161. has: function(name, condition){
  162. return function(elem){
  163. return $(condition, elem).length;
  164. }
  165. },
  166. visible: function(name, condition){
  167. return function(elem){
  168. return dojo.style(elem, "visible") != "hidden" && dojo.style(elem, "display") != "none";
  169. }
  170. },
  171. hidden: function(name, condition){
  172. return function(elem){
  173. return elem.type == "hidden" || dojo.style(elem, "visible") == "hidden" || dojo.style(elem, "display") == "none";
  174. }
  175. },
  176. selected: function(name, condition){
  177. return function(elem){
  178. return elem.selected;
  179. }
  180. },
  181. checked: function(name, condition){
  182. return function(elem){
  183. return elem.nodeName.toUpperCase() == "INPUT" && elem.checked;
  184. }
  185. },
  186. disabled: function(name, condition){
  187. return function(elem){
  188. return elem.getAttribute("disabled");
  189. }
  190. },
  191. enabled: function(name, condition){
  192. return function(elem){
  193. return !elem.getAttribute("disabled");
  194. }
  195. },
  196. input: function(name, condition){
  197. return function(elem){
  198. var n = elem.nodeName.toUpperCase();
  199. return n == "INPUT" || n == "SELECT" || n == "TEXTAREA" || n == "BUTTON";
  200. }
  201. },
  202. button: function(name, condition){
  203. return function(elem){
  204. return (elem.nodeName.toUpperCase() == "INPUT" && elem.type == "button") || elem.nodeName.toUpperCase() == "BUTTON";
  205. }
  206. },
  207. header: function(name, condition){
  208. return function(elem){
  209. return elem.nodeName.match(headerRegExp);
  210. }
  211. }
  212. //TODO: implement :animated
  213. });
  214. //Add the input type selectors to pseudos
  215. var inputPseudos = {};
  216. dojo.forEach([
  217. "text", "password", "radio", "checkbox", "submit", "image", "reset", "file"
  218. ], function(type) {
  219. inputPseudos[type] = function(name, condition){
  220. return function(elem){
  221. return elem.nodeName.toUpperCase() == "INPUT" && elem.type == type;
  222. }
  223. };
  224. });
  225. dojo.mixin(pseudos, inputPseudos);
  226. //Set up browser sniff.
  227. $.browser = {
  228. mozilla: dojo.isMoz,
  229. msie: dojo.isIE,
  230. opera: dojo.isOpera,
  231. safari: dojo.isSafari
  232. };
  233. $.browser.version = dojo.isIE || dojo.isMoz || dojo.isOpera || dojo.isSafari || dojo.isWebKit;
  234. //Map back into dojo
  235. //Hmm maybe this is not so good. Dojo
  236. //modules may still be holding on to old
  237. //dojo (example: the d._NodeListCtor in query.js)
  238. //dojo = dojo.mixin($, dojo);
  239. // Add $.ready
  240. $.ready = $.fn.ready = function(callback){
  241. dojo.addOnLoad(dojo.hitch(null, callback, $));
  242. return this;
  243. }
  244. //START jquery Core API methods
  245. //http://docs.jquery.com/Core
  246. f._is$ = true;
  247. f.size = function(){return this.length; };
  248. $.prop = function(node, propCheck){
  249. //TODO: not sure about this one, could not find the docs?
  250. if(dojo.isFunction(propCheck)){
  251. return propCheck.call(node);
  252. }else{
  253. return propCheck;
  254. }
  255. }
  256. $.className = {
  257. add: dojo.addClass,
  258. remove: dojo.removeClass,
  259. has: dojo.hasClass
  260. };
  261. $.makeArray = function(thing){
  262. if(typeof thing == "undefined"){
  263. return [];
  264. }else if(thing.length && !dojo.isString(thing) && !("location" in thing)){
  265. //Location check was for excluding window objects that have a length.
  266. return dojo._toArray(thing);
  267. }else{
  268. return [thing];
  269. }
  270. }
  271. $.merge = function(ary1, ary2){
  272. //Alters first array by adding in the element.
  273. var args = [ary1.length, 0];
  274. args = args.concat(ary2);
  275. ary1.splice.apply(ary1, args);
  276. return ary1;
  277. }
  278. $.each = function(/*Array||ArrayLike*/list, /*Function*/cb){
  279. //each differs from dojo.NodeList.forEach in that
  280. //"this" is the current cycled node. Breaking
  281. //the loop is also possible. Also, index is first arg
  282. //to the callback.
  283. if(dojo.isArrayLike(list)){
  284. for(var i = 0; i < list.length; i++){
  285. if(cb.call(list[i], i, list[i]) === false){
  286. break;
  287. }
  288. }
  289. }else if(dojo.isObject(list)){
  290. for(var param in list){
  291. if(cb.call(list[param], param, list[param]) === false){
  292. break;
  293. }
  294. }
  295. }
  296. return this;
  297. };
  298. f.each = function(/*Function*/cb){
  299. return $.each.call(this, this, cb);
  300. };
  301. //f.length already implemented by NodeList
  302. f.eq = function(){
  303. //Direct copy of dojo.NodeList.at, but want
  304. //to use our NodeList class.
  305. var nl = $();
  306. dojo.forEach(arguments, function(i) { if(this[i]) { nl.push(this[i]); } }, this);
  307. return nl; // dojo.NodeList
  308. };
  309. f.get = function(/*Number*/index){
  310. if(index || index == 0){
  311. return this[index];
  312. }
  313. return this;
  314. };
  315. f.index = function(arg){
  316. //Hmm, allows passing in a $ nodelist. Apparently in that
  317. //case take the first item in that array and match
  318. if(arg._is$){
  319. arg = arg[0];
  320. }
  321. return this.indexOf(arg);
  322. }
  323. //.data implementation
  324. var dataStore = [];
  325. var dataId = 0;
  326. var dataAttr = dojo._scopeName + "DataId";
  327. var getDataId = function(node){
  328. var id = node.getAttribute(dataAttr);
  329. if(!id){
  330. id = dataId++;
  331. node.setAttribute(dataAttr, id);
  332. }
  333. }
  334. var getData = function(node){
  335. var data = {};
  336. if(node.nodeType == 1){
  337. var id = getDataId(node);
  338. data = dataStore[id];
  339. if(!data){
  340. data = dataStore[id] = {};
  341. }
  342. }
  343. return data;
  344. }
  345. $.data = function(/*DOMNode*/node, /*String*/name, /*String*/value){
  346. var result = null;
  347. if(name == "events"){
  348. //Special case "events", since jquery tests seem to use it to
  349. //get the event handler storage for jquery. So for jquery apps
  350. //"events" is probably a reserved word anyway.
  351. result = listeners[node.getAttribute(eventAttr)];
  352. var isEmpty = true;
  353. if(result){
  354. for(var param in result){
  355. isEmpty = false;
  356. break;
  357. }
  358. }
  359. return isEmpty ? null : result;
  360. }
  361. var data = getData(node);
  362. if(typeof value != "undefined"){
  363. data[name] = value;
  364. }else{
  365. result = data[name];
  366. }
  367. return value ? this : result;
  368. }
  369. $.removeData = function(/*DOMNode*/node, /*String*/name){
  370. var data = getData(node);
  371. delete data[name];
  372. if(node.nodeType == 1){
  373. var isEmpty = true;
  374. for(var param in data){
  375. isEmpty = false;
  376. break;
  377. }
  378. if(isEmpty){
  379. node.removeAttribute(dataAttr);
  380. }
  381. }
  382. return this;
  383. }
  384. f.data = function(/*String*/name, /*String*/value){
  385. var result = null;
  386. this.forEach(function(node){
  387. result = $.data(node, name, value);
  388. });
  389. return value ? this : result;
  390. }
  391. f.removeData = function(/*String*/name){
  392. this.forEach(function(node){
  393. $.removeData(node, name);
  394. });
  395. return this;
  396. }
  397. function jqMix(obj, props){
  398. //summary: an attempt at a mixin that follows
  399. //jquery's .extend rules. Seems odd. Not sure how
  400. //to resolve this with dojo.mixin and what the use
  401. //cases are for the jquery version.
  402. //Copying some code from dojo._mixin.
  403. if(obj == props){
  404. return obj;
  405. }
  406. var tobj = {};
  407. for(var x in props){
  408. // the "tobj" condition avoid copying properties in "props"
  409. // inherited from Object.prototype. For example, if obj has a custom
  410. // toString() method, don't overwrite it with the toString() method
  411. // that props inherited from Object.prototype
  412. if((tobj[x] === undefined || tobj[x] != props[x]) && props[x] !== undefined && obj != props[x]){
  413. if(dojo.isObject(obj[x]) && dojo.isObject(props[x])){
  414. if(dojo.isArray(props[x])){
  415. obj[x] = props[x];
  416. }else{
  417. obj[x] = jqMix(obj[x], props[x]);
  418. }
  419. }else{
  420. obj[x] = props[x];
  421. }
  422. }
  423. }
  424. // IE doesn't recognize custom toStrings in for..in
  425. if(dojo.isIE && props){
  426. var p = props.toString;
  427. if(typeof p == "function" && p != obj.toString && p != tobj.toString &&
  428. p != "\nfunction toString() {\n [native code]\n}\n"){
  429. obj.toString = props.toString;
  430. }
  431. }
  432. return obj; // Object
  433. }
  434. f.extend = function(){
  435. var args = [this];
  436. args = args.concat(arguments);
  437. return $.extend.apply($, args);
  438. }
  439. $.extend = function(){
  440. //Could have multiple args to mix in. Similar to dojo.mixin,
  441. //but has some different rules, and the mixins all get applied
  442. //to the first arg.
  443. var args = arguments, finalObj;
  444. for(var i = 0; i < args.length; i++){
  445. var obj = args[i];
  446. if(obj && dojo.isObject(obj)){
  447. if(!finalObj){
  448. finalObj = obj;
  449. }else{
  450. jqMix(finalObj, obj);
  451. }
  452. }
  453. }
  454. return finalObj;
  455. }
  456. $.noConflict = function(/*Boolean*/extreme){
  457. var me = $;
  458. dojo.global.$ = _old$;
  459. if(extreme){
  460. dojo.global.jQuery = _oldJQuery;
  461. }
  462. return me;
  463. }
  464. //END jquery Core API methods
  465. //START jquery Attribute API methods
  466. //http://docs.jquery.com/Attributes
  467. f.attr = function(name, value){
  468. //The isObject tests below are to weed out where something
  469. //like a form node has an input called "action" but we really
  470. //want to get the attribute "action". But in general, favor
  471. //a property value over a DOM attribute value.
  472. if(arguments.length == 1 && dojo.isString(arguments[0])){
  473. //The get case, return first match.
  474. var first = this[0];
  475. //Weed out empty nodes
  476. if(!first){
  477. return null;
  478. }
  479. var arg = arguments[0];
  480. //favor properties over attributes.
  481. var attr = dojo.attr(first, arg);
  482. var prop = first[arg];
  483. if((arg in first) && !dojo.isObject(prop) && name != "href"){
  484. return prop;
  485. }else{
  486. return attr || prop;
  487. }
  488. }else if(dojo.isObject(name)){
  489. //A setter, using an object.
  490. for(var param in name){
  491. this.attr(param, name[param]);
  492. }
  493. return this;
  494. }else{
  495. //The setter case. Figure out if value is a function.
  496. var isFunc = dojo.isFunction(value);
  497. this.forEach(function(node, index){
  498. var prop = node[name];
  499. if((name in node) && !dojo.isObject(prop) && name != "href"){
  500. node[name] = (isFunc ? value.call(node, index) : value);
  501. }else if(node.nodeType == 1){
  502. dojo.attr(node, name, (isFunc ? value.call(node, index) : value));
  503. }
  504. });
  505. return this;
  506. }
  507. }
  508. f.removeAttr = function(name){
  509. this.forEach(function(node, index){
  510. var prop = node[name];
  511. if((name in node) && !dojo.isObject(prop) && name != "href"){
  512. delete node[name];
  513. }else if(node.nodeType == 1){
  514. if(name == "class"){
  515. //TODO: push this fix into dojo.removeAttr
  516. node.removeAttribute(name);
  517. }else{
  518. dojo.removeAttr(node, name);
  519. }
  520. }
  521. });
  522. return this;
  523. }
  524. //addClass, removeClass exist in dojo.NodeList. toggleClass in jQuery case
  525. //just means add/remove the classname if it missing/exists. So need custom override.
  526. f.toggleClass = function(/*String*/name, /*Expression?*/condition){
  527. var hasCondition = arguments.length > 1;
  528. this.forEach(function(node){
  529. dojo.toggleClass(node, name, hasCondition ? condition : !dojo.hasClass(node, name));
  530. });
  531. return this;
  532. }
  533. //Action depends on arguments: if an array of functions do one thing,
  534. //If no args, do a display toggle,
  535. //If an expression, something that evaluates to true or false,
  536. //then toggle display accordingly.
  537. //If first arg is a String/Number, then do animation. Second arg
  538. //is an optional callback.
  539. f.toggle = function(){
  540. //If more than two args and we have a function as first arg, then
  541. //probably the onclick toggle variant: takes variable args that are
  542. //functions and cycles through them on click actions.
  543. var args = arguments;
  544. if(arguments.length > 1 && dojo.isFunction(arguments[0])){
  545. var index = 0;
  546. var func = function(){
  547. var result = args[index].apply(this, arguments);
  548. index += 1;
  549. if(index > args.length - 1){
  550. index = 0;
  551. }
  552. };
  553. return this.bind("click", func);
  554. }else{
  555. //The display/hide/show case.
  556. var condition = arguments.length == 1 ? arguments[0] : undefined;
  557. this.forEach(function(node){
  558. var result = typeof condition == "undefined" ? dojo.style(node, "display") == "none" : condition;
  559. var action = (result ? "show" : "hide");
  560. var nl = $(node);
  561. nl[action].apply(nl, args);
  562. });
  563. return this;
  564. }
  565. }
  566. //hasClass just returns true if any of the nodes has the class.
  567. f.hasClass = function(/*String*/name){
  568. return this.some(function(node){
  569. return dojo.hasClass(node, name);
  570. });
  571. }
  572. //use the html method from dojo.NodeList-manipulate.
  573. f.html = f.innerHTML;
  574. //END jquery Attribute API methods
  575. //START jquery Traversing API methods
  576. //http://docs.jquery.com/Traversing
  577. dojo.forEach(["filter", "slice"], function(item){
  578. f[item] = function(){
  579. //Convert the "this" value for functions passed in:
  580. var nl;
  581. if(dojo.isFunction(arguments[0])){
  582. var origFunc = arguments[0];
  583. arguments[0] = function(item, index){
  584. return origFunc.call(item, item, index);
  585. }
  586. }
  587. if(item == "filter" && dojo.isString(arguments[0])){
  588. var nl = this._filterQueryResult(this, arguments[0]);
  589. }else{
  590. var oldCtor = dojo._NodeListCtor;
  591. dojo._NodeListCtor = f;
  592. //Need to wrap in a $() call since internally some
  593. //dojo.NodeList functions reference dojo.NodeList directly.
  594. //Need to get a configurable constructor for dojo.NodeList.
  595. nl = $(nlProto[item].apply(this, arguments));
  596. dojo._NodeListCtor = oldCtor;
  597. }
  598. return nl._stash(this);
  599. }
  600. });
  601. f.map = function(/*Function*/callback){
  602. //Hmm, this is not like array map/dojo.map where you get one item back for
  603. //each input.
  604. return this._buildArrayFromCallback(callback);
  605. }
  606. $.map = function(/*Array*/ary, /*Function*/callback){
  607. //Hmm, this is not like array map/dojo.map where you get one item back for
  608. //each input.
  609. return f._buildArrayFromCallback.call(ary, callback);
  610. }
  611. $.inArray = function(value, /*Array*/ary){
  612. return dojo.indexOf(ary, value);
  613. }
  614. f.is = function(query){
  615. return (query ? !!this.filter(query).length : false);
  616. }
  617. //TODO: probably a better way to do this.
  618. f.not = function(){
  619. var notList = $.apply($, arguments);
  620. //TODO: another place where if dojo.NodeList can configure a constructor,
  621. //then we could avoid the $() wrapper below.
  622. var nl = $(nlProto.filter.call(this, function(node){
  623. return notList.indexOf(node) == -1;
  624. }));
  625. return nl._stash(this);
  626. }
  627. f.add = function(){
  628. return this.concat.apply(this, arguments);
  629. }
  630. function iframeDoc(/*DOMNode*/iframeNode){
  631. //summary: Returns the document object associated with the iframe DOM Node argument.
  632. //Taken from dojo.io.iframe.doc(). Needed for contents() function below.
  633. var doc = iframeNode.contentDocument || // W3
  634. (
  635. (
  636. (iframeNode.name) && (iframeNode.document) &&
  637. (document.getElementsByTagName("iframe")[iframeNode.name].contentWindow) &&
  638. (document.getElementsByTagName("iframe")[iframeNode.name].contentWindow.document)
  639. )
  640. ) || // IE
  641. (
  642. (iframeNode.name)&&(document.frames[iframeNode.name])&&
  643. (document.frames[iframeNode.name].document)
  644. ) || null;
  645. return doc;
  646. }
  647. f.contents = function(){
  648. var ary = [];
  649. this.forEach(function(node){
  650. if(node.nodeName.toUpperCase() == "IFRAME"){
  651. var doc = iframeDoc(node);
  652. if(doc){
  653. ary.push(doc);
  654. }
  655. }else{
  656. //TODO: code similar to children() function. Refactor?
  657. var children = node.childNodes;
  658. //Using for loop for better speed.
  659. for(var i = 0; i < children.length; i++){
  660. ary.push(children[i]);
  661. }
  662. }
  663. });
  664. return this._wrap(ary)._stash(this);
  665. }
  666. f.find = function(/*String*/query){
  667. var ary = [];
  668. this.forEach(function(node){
  669. if(node.nodeType == 1){
  670. ary = ary.concat(dojo._toArray($(query, node)));
  671. }
  672. });
  673. return this._getUniqueAsNodeList(ary)._stash(this);
  674. }
  675. f.andSelf = function(){
  676. return this.add(this._parent);
  677. }
  678. //END jquery Traversing API methods
  679. //START jquery Manipulation API methods
  680. //http://docs.jquery.com/Manipulation
  681. f.remove = function(/*String?*/query){
  682. //Override NodeList-manipulate's remove so we can remove data.
  683. var nl = (query ? this._filterQueryResult(this, query) : this);
  684. //Remove data
  685. nl.removeData();
  686. //Remove event listeners.
  687. //TODO! do this, once event stuff is built out.
  688. //Remove the items from the DOM, but keep them in this
  689. //node list.
  690. nl.forEach(function(node){
  691. node.parentNode.removeChild(node);
  692. });
  693. return this;
  694. }
  695. //START jquery CSS API methods
  696. //http://docs.jquery.com/CSS
  697. $.css = function(/*DOMNode*/node, /*String|Object*/name, /*String|Number?*/value){
  698. name = cssNameToJs(name);
  699. //Hmm, dojo.style does an arguments. length check.
  700. var result = (value ? dojo.style(node, name, value) : dojo.style(node, name));
  701. return result;
  702. }
  703. f.css = function(/*String|Object*/name, /*String|Number?*/value){
  704. if(dojo.isString(name)){
  705. //Convert name to JS name if needed.
  706. name = cssNameToJs(name);
  707. if(arguments.length == 2){
  708. //set the value. Cannot directly delegate to
  709. //this.style, since non-element nodes may be in the mix?
  710. //this.contents() in particular will return some funky stuff.
  711. //Need to be sure to add "px" if appropriate.
  712. if(!dojo.isString(value) && name != "zIndex"){
  713. value = value + "px";
  714. }
  715. this.forEach(function(node){
  716. if(node.nodeType == 1){
  717. dojo.style(node, name, value);
  718. }
  719. });
  720. return this;
  721. }else{
  722. //return the value
  723. value = dojo.style(this[0], name);
  724. //Need to be sure to add "px" if appropriate.
  725. if(!dojo.isString(value) && name != "zIndex"){
  726. value = value + "px";
  727. }
  728. return value;
  729. }
  730. }else{
  731. for(var param in name){
  732. this.css(param, name[param]);
  733. }
  734. return this;
  735. }
  736. }
  737. function doBox(/*NodeList*/nl, /*String*/boxType, /*String*/prop, /*String||Number*/value){;
  738. if(value){
  739. //Set height for all elements.
  740. var mod = {};
  741. mod[prop] = value;
  742. nl.forEach(function(node){
  743. dojo[boxType](node, mod);
  744. });
  745. return nl;
  746. }else{
  747. //Just get first node's height.
  748. //Hmm. width is negative when element is display none in FF3?
  749. return Math.abs(Math.round(dojo[boxType](nl[0])[prop]));
  750. }
  751. }
  752. f.height = function(value){
  753. return doBox(this, "contentBox", "h", value);
  754. }
  755. f.width = function(value){
  756. return doBox(this, "contentBox", "w", value);
  757. }
  758. function getDimensions(/*DOMNode*/node, /*String*/type, /*Boolean*/usePadding, /*Boolean*/useBorder, /*Boolean*/useMargin){
  759. //summary: sums up the different parts of the width/height based on arguments.
  760. //If hidden, temporarily show it, do measurements then close.
  761. var rehide = false;
  762. if((rehide = node.style.display == "none")){
  763. node.style.display = "block";
  764. }
  765. var cs = dojo.getComputedStyle(node);
  766. var content = Math.abs(Math.round(dojo._getContentBox(node, cs)[type]));
  767. var pad = usePadding ? Math.abs(Math.round(dojo._getPadExtents(node, cs)[type])) : 0;
  768. var border = useBorder ? Math.abs(Math.round(dojo._getBorderExtents(node, cs)[type])) : 0;
  769. var margin = useMargin ? Math.abs(Math.round(dojo._getMarginExtents(node, cs)[type])) : 0;
  770. if(rehide){
  771. node.style.display = "none";
  772. }
  773. return pad + content + border + margin;
  774. }
  775. f.innerHeight = function(){
  776. return getDimensions(this[0], "h", true);
  777. }
  778. f.innerWidth = function(){
  779. return getDimensions(this[0], "w", true);
  780. }
  781. f.outerHeight = function(useMargin){
  782. return getDimensions(this[0], "h", true, true, useMargin);
  783. }
  784. f.outerWidth = function(useMargin){
  785. return getDimensions(this[0], "w", true, true, useMargin);
  786. }
  787. //END jquery CSS API methods
  788. //START jquery Events API methods
  789. //http://docs.jquery.com/Events
  790. //ready() already defined above.
  791. //Event plumbing.
  792. var listeners = [];
  793. var listenId = 1;
  794. var eventAttr = dojo._scopeName + "eventid";
  795. var currentEvtData;
  796. function getNonNamespacedName(/*String*/evtName){
  797. //summary: gets name of the event before the first ".".
  798. //The $$ stuff is special ids used to create unique names
  799. //for bound functions that did not have a unique namespace name.
  800. evtName = evtName.split("$$")[0];
  801. var dotIndex = evtName.indexOf(".");
  802. if(dotIndex != -1){
  803. evtName = evtName.substring(0, dotIndex);
  804. }
  805. return evtName;
  806. }
  807. function domConnect(/*DOMNode*/node, /*String*/evtName){
  808. //summary: handles creating the connection with a real DOM event.
  809. //This work should only be done one time per evName type.
  810. //If the event if an ajax event, use dojo.subscribe instead.
  811. if(evtName.indexOf("ajax") == 0){
  812. return dojo.subscribe(topics[evtName], function(dfd, res){
  813. var fakeEvt = new $.Event(evtName);
  814. if("ajaxComplete|ajaxSend|ajaxSuccess".indexOf(evtName) != -1){
  815. triggerHandlers(node, [fakeEvt, dfd.ioArgs.xhr, dfd.ioArgs.args]);
  816. }else if(evtName == "ajaxError"){
  817. triggerHandlers(node, [fakeEvt, dfd.ioArgs.xhr, dfd.ioArgs.args, res]);
  818. }else{
  819. //ajaxStart|ajaxStop
  820. triggerHandlers(node, [fakeEvt]);
  821. }
  822. });
  823. }else{
  824. return dojo.connect(node, "on" + evtName, function(e){
  825. triggerHandlers(node, arguments);
  826. }); //Object
  827. }
  828. }
  829. //Event object for compatibility for some tests.
  830. $.Event = function(/*String*/type){
  831. //Allow for calling function without "new"
  832. if(this == $){
  833. return new $.Event(type);
  834. }
  835. if(typeof type == "string"){
  836. this.type = type.replace(/!/, "");
  837. }else{
  838. dojo.mixin(this, type);
  839. }
  840. this.timeStamp = (new Date()).getTime();
  841. this._isFake = true;
  842. this._isStrict = (this.type.indexOf("!") != -1);
  843. }
  844. var ep = $.Event.prototype = {
  845. preventDefault: function(){
  846. this.isDefaultPrevented = this._true;
  847. },
  848. stopPropagation: function(){
  849. this.isPropagationStopped = this._true;
  850. },
  851. stopImmediatePropagation: function(){
  852. this.isPropagationStopped = this._true;
  853. this.isImmediatePropagationStopped = this._true;
  854. },
  855. _true: function(){ return true; },
  856. _false: function(){ return false; }
  857. }
  858. dojo.mixin(ep, {
  859. isPropagationStopped: ep._false,
  860. isImmediatePropagationStopped: ep._false,
  861. isDefaultPrevented: ep._false
  862. });
  863. function makeTriggerData(data, type){
  864. //summary: makes sure that the data array is copied
  865. //and has an event as the first arg. If this function generates
  866. //a fake event (known by the data[0]._isFake property being true)
  867. //then the data[0].target needs to be set by the consumer of this function.
  868. data = data || [];
  869. data = [].concat(data);
  870. //If first data item is not an event, make one up.
  871. //Need to set up target: prop in the consumers of this
  872. //function.
  873. var evt = data[0];
  874. if(!evt || !evt.preventDefault){
  875. evt = type && type.preventDefault ? type : new $.Event(type);
  876. data.unshift(evt);
  877. }
  878. return data;
  879. }
  880. var triggerHandlersCalled = false;
  881. function triggerHandlers(/*DOMNode*/node, /*Array*/data, /*Function?*/extraFunc){
  882. //summary: handles the actual callbacks to the handlers.
  883. //Indicate triggerHandlers was called.
  884. triggerHandlersCalled = true;
  885. //Uses currentEvtData if this is a simulated event.
  886. data = data || currentEvtData;
  887. extraFunc = extraFunc;
  888. //Normalize on a real element if dealing with a document.
  889. if(node.nodeType == 9){
  890. node = node.documentElement;
  891. }
  892. var nodeId = node.getAttribute(eventAttr);
  893. if(!nodeId){
  894. return;
  895. }
  896. var evt = data[0];
  897. var evtFullName = evt.type;
  898. var evtName = getNonNamespacedName(evtFullName);
  899. var cbs = listeners[nodeId][evtName];
  900. var result;
  901. //Apply the extra function. What is that about? Not mentioned in the
  902. //public APIs?
  903. if(extraFunc){
  904. result = extraFunc.apply(node, data);
  905. }
  906. if (result !== false){
  907. for(var param in cbs){
  908. if(param != "_connectId" && (!evt._isStrict && (param.indexOf(evtFullName) == 0) || (evt._isStrict && param == evtFullName))){
  909. //Store the callback ID in case unbind is called with this event
  910. //so we can only unbind that one callback.
  911. evt[dojo._scopeName + "callbackId"] = param;
  912. var cb = cbs[param];
  913. if(typeof cb.data != "undefined"){
  914. evt.data = cb.data;
  915. }else{
  916. evt.data = null;
  917. }
  918. //Do the actual callback.
  919. if ((result = cb.fn.apply(evt.target, data)) === false && !evt._isFake){
  920. dojo.stopEvent(evt);
  921. }
  922. evt.result = result;
  923. }
  924. }
  925. }
  926. return result;
  927. }
  928. f.triggerHandler = function(/*String*/type, /*Array?*/data, /*Function?*/extraFunc){
  929. //Only triggers handlers on the first node. Huh.
  930. var node = this[0];
  931. if(node && node.nodeType != 3 && node.nodeType != 8){
  932. data = makeTriggerData(data, type);
  933. return triggerHandlers(node, data, extraFunc);
  934. }else{
  935. return undefined;
  936. }
  937. }
  938. f.trigger = function(/*String*/type, /*Array?*/data, /*Function?*/extraFunc){
  939. //Copy data since we may need to modify by adding a
  940. data = makeTriggerData(data, type);
  941. var evt = data[0];
  942. var type = getNonNamespacedName(evt.type);
  943. //Store the current event data in case handlers need
  944. //to reference it because of a simulated event.
  945. currentEvtData = data;
  946. currentExtraFunc = extraFunc;
  947. var result = null;
  948. var needTarget = !evt.target;
  949. this.forEach(function(node){
  950. //Only handle non text/comment nodes.
  951. if(node.nodeType != 3 && node.nodeType != 8){
  952. //Normalize on a real element if dealing with a document.
  953. if(node.nodeType == 9){
  954. node = node.documentElement;
  955. }
  956. //Set the node target appropriately for fake events.
  957. if(evt._isFake){
  958. evt.currentTarget = node;
  959. if(needTarget){
  960. evt.target = node;
  961. }
  962. }
  963. //Bizarre extra function thing. Not really demonstrated in public
  964. //API docs.
  965. if(extraFunc){
  966. var funcData = data.slice(1);
  967. result = extraFunc.apply(node, (result = null ? funcData : funcData.concat(result)));
  968. }
  969. if(result !== false){
  970. //Trigger DOM event. onclick is handled differently than
  971. //others.
  972. /*
  973. if(type == 'click' && node.onclick && node.nodeName.toUpperCase() == "A"){
  974. result = node.onclick.apply(node, data);
  975. }
  976. */
  977. //Set the "global" flag that indicates if triggerHandlers was called.
  978. //If the direct node.event/onevent does not trigger the handlers, do so
  979. //manually at the end.
  980. triggerHandlersCalled = false;
  981. //Trigger functions registered directly on the DOM node.
  982. if(node[type]){
  983. try{
  984. result = node[type]();
  985. }catch(e){
  986. //Apparently IE throws on some hidden elements. Just eat it.
  987. }
  988. }else if(node["on" + type]){
  989. try{
  990. result = node["on" + type]();
  991. }catch(e){
  992. //Apparently IE throws on some hidden elements. Just eat it.
  993. }
  994. }
  995. if(!triggerHandlersCalled){
  996. //Finally triggerHandlers directly if the above code did not trigger it yet.
  997. result = triggerHandlers(node, data);
  998. }
  999. //Bubble the event up.
  1000. //TODO: optimize this path so we don't have to do forEach and NodeList work.
  1001. var parentNode = node.parentNode;
  1002. if(result !== false && !evt.isImmediatePropagationStopped() && !evt.isPropagationStopped() && parentNode && parentNode.nodeType == 1){
  1003. $(parentNode).trigger(type, data, extraFunc);
  1004. }
  1005. }
  1006. }
  1007. });
  1008. //Clear current event data.
  1009. currentEvtData = null;
  1010. currentExtraFunc = null;
  1011. return this;
  1012. }
  1013. var bindIdCounter = 0;
  1014. f.bind = function(/*String*/type, /*Array||Function?*/data, /*Function*/fn){
  1015. //Type can be space separated values.
  1016. type = type.split(" ");
  1017. //May not have data argument.
  1018. if(!fn){
  1019. fn = data;
  1020. data = null;
  1021. }
  1022. this.forEach(function(node){
  1023. //Only handle non text/comment nodes.
  1024. if(node.nodeType != 3 && node.nodeType != 8){
  1025. //If document, bind to documentElement
  1026. if(node.nodeType == 9){
  1027. node = node.documentElement;
  1028. }
  1029. //If no nodeId, then create one and attach it to the DOM node.
  1030. var nodeId = node.getAttribute(eventAttr);
  1031. if(!nodeId){
  1032. nodeId = listenId++;
  1033. node.setAttribute(eventAttr, nodeId);
  1034. listeners[nodeId] = {};
  1035. }
  1036. //Process each event type.
  1037. for(var i = 0; i < type.length; i++){
  1038. //Get event name, if have a dot on it, it is namespaced,
  1039. //be sure to get the core event name.
  1040. var evtFullName = type[i];
  1041. var evtName = getNonNamespacedName(evtFullName);
  1042. if(evtName == evtFullName){
  1043. //Generate a unique ID for this function binding
  1044. evtFullName = evtName + "$$" + (bindIdCounter++);
  1045. }
  1046. //Get the event listeners for the event name, the complete name.
  1047. var lls = listeners[nodeId];
  1048. if(!lls[evtName]){
  1049. lls[evtName] = {
  1050. _connectId: domConnect(node, evtName)
  1051. };
  1052. }
  1053. //Add the callback to the list of listeners.
  1054. lls[evtName][evtFullName] = {
  1055. fn: fn,
  1056. data: data
  1057. };
  1058. }
  1059. }
  1060. });
  1061. return this;
  1062. }
  1063. function copyEventHandlers(/*DOMNode*/ src, /*DOMNode*/ target){
  1064. // summary:
  1065. // copies the event handlers from onne src *element* node to
  1066. // another target *element* node. Assumes that target had
  1067. // no previous events on it, and is a clone of the src node.
  1068. //Get src listeners.
  1069. var srcNodeId = target.getAttribute(eventAttr);
  1070. var sls = listeners[srcNodeId];
  1071. if(!sls){
  1072. return;
  1073. }
  1074. //Generate listeners area for target.
  1075. var nodeId = nodeId = listenId++;
  1076. target.setAttribute(eventAttr, nodeId);
  1077. var tls = listeners[nodeId] = {};
  1078. //Loope through events in source. Protect against bad
  1079. //code modifying Object.prototype.
  1080. var empty = {};
  1081. for (var evtName in sls){
  1082. var tEvtData = tls[evtName] = {
  1083. _connectId: domConnect(target, evtName)
  1084. };
  1085. var sEvtData = sls[evtName];
  1086. for (var evtFullName in sEvtData){
  1087. tEvtData[evtFullName] = {
  1088. fn: sEvtData[evtFullName].fn,
  1089. data: sEvtData[evtFullName].data
  1090. };
  1091. }
  1092. }
  1093. }
  1094. function listenerUnbind(lls, evtName, evtFullName, callbackId, fn){
  1095. //Handles the real remove of an event and dojo.disconnects DOM handler if necessary.
  1096. //This has to be broken out of the main unbind function because we have to support
  1097. //things like unbind(".test") that go across major event names. Yuck.
  1098. var handles = lls[evtName];
  1099. if(handles){
  1100. var hasDot = evtFullName.indexOf(".") != -1;
  1101. var forceDelete = false;
  1102. if(callbackId){
  1103. //Only need to unbind that one callback
  1104. delete handles[callbackId];
  1105. }else if(!hasDot && !fn){
  1106. forceDelete = true;
  1107. }else if(hasDot){
  1108. //A namespaced event.
  1109. //Problem is the namespaced event could be something like
  1110. //".test" which means remove all that end in .test. Yuck.
  1111. if(evtFullName.charAt(0) == "."){
  1112. for(var param in handles){
  1113. if(param.indexOf(evtFullName) == param.length - evtFullName.length){
  1114. delete handles[param];
  1115. }
  1116. }
  1117. }else{
  1118. delete handles[evtFullName];
  1119. }
  1120. }else{
  1121. //Not a namespaced event. Cycle through the $$ names
  1122. //to find a function match.
  1123. for(var param in handles){
  1124. if(param.indexOf("$$") != -1 && handles[param].fn == fn){
  1125. delete handles[param];
  1126. break;
  1127. }
  1128. }
  1129. }
  1130. //Remove handles/disconnect dom if no other params.
  1131. var allDone = true;
  1132. for(var param in handles){
  1133. if(param != "_connectId"){
  1134. allDone = false;
  1135. break;
  1136. }
  1137. }
  1138. if(forceDelete || allDone){
  1139. if(evtName.indexOf("ajax") != -1){
  1140. dojo.unsubscribe(handles._connectId);
  1141. }else{
  1142. dojo.disconnect(handles._connectId);
  1143. }
  1144. delete lls[evtName];
  1145. }
  1146. }
  1147. }
  1148. f.unbind = function(/*String*/type, /*Function*/fn){
  1149. //See if event has a callbackId, if so, then we only unbind
  1150. //that one callback.
  1151. var callbackId = type ? type[dojo._scopeName + "callbackId"] : null;
  1152. //Type can be space separated values.
  1153. type = type && type.type ? type.type : type;
  1154. type = type ? type.split(" ") : type;
  1155. this.forEach(function(node){
  1156. //Only handle non text/comment nodes.
  1157. if(node.nodeType != 3 && node.nodeType != 8){
  1158. //If document, bind to documentElement
  1159. if(node.nodeType == 9){
  1160. node = node.documentElement;
  1161. }
  1162. //If no nodeId, then create one and attach it to the DOM node.
  1163. var nodeId = node.getAttribute(eventAttr);
  1164. if(nodeId){
  1165. //Get the event listeners for the event name, the complete name.
  1166. var lls = listeners[nodeId];
  1167. if(lls){
  1168. //If no type, then it means do all bound types. Make a list of them.
  1169. var etypes = type;
  1170. if(!etypes){
  1171. etypes = [];
  1172. for(var param in lls){
  1173. etypes.push(param);
  1174. }
  1175. }
  1176. //Process each event type.
  1177. for(var i = 0; i < etypes.length; i++){
  1178. //Get event name, if have a dot on it, it is namespaced,
  1179. //be sure to get the core event name.
  1180. var evtFullName = etypes[i];
  1181. var evtName = getNonNamespacedName(evtFullName);
  1182. //Problem is the namespaced event could be something like
  1183. //".test" which means remove all that end in .test. Yuck.
  1184. if(evtFullName.charAt(0) == "."){
  1185. for(var param in lls) {
  1186. listenerUnbind(lls, param, evtFullName, callbackId, fn);
  1187. }
  1188. }else{
  1189. listenerUnbind(lls, evtName, evtFullName, callbackId, fn);
  1190. }
  1191. }
  1192. }
  1193. }
  1194. }
  1195. });
  1196. return this;
  1197. }
  1198. f.one = function(/*String*/evtName, /*Function*/func){
  1199. var oneFunc = function(){
  1200. $(this).unbind(evtName, arguments.callee);
  1201. return func.apply(this, arguments);
  1202. }
  1203. return this.bind(evtName, oneFunc);
  1204. };
  1205. f._cloneNode = function(/*DOMNode*/ src){
  1206. // summary:
  1207. // private utiltity to clone a node. Copies event handlers too.
  1208. var target = src.cloneNode(true);
  1209. if(src.nodeType == 1){
  1210. //Look for event handlers in target.
  1211. var evNodes = dojo.query("[" + eventAttr + "]", target);
  1212. for(var i = 0, newNode; newNode = evNodes[i]; i++){
  1213. var oldNode = dojo.query('[' + eventAttr + '="' + newNode.getAttribute(eventAttr) + '"]', src)[0];
  1214. if(oldNode){
  1215. copyEventHandlers(oldNode, newNode);
  1216. }
  1217. }
  1218. }
  1219. return target;
  1220. };
  1221. //Temporary testing shim to get past jquery test setup errors.
  1222. dojo.getObject("$.event.global", true);
  1223. //Set up event handlers
  1224. dojo.forEach([
  1225. "blur", "focus", "dblclick", "click", "error", "keydown", "keypress", "keyup", "load", "mousedown",
  1226. "mouseenter", "mouseleave", "mousemove", "mouseout", "mouseover", "mouseup", "submit",
  1227. "ajaxStart", "ajaxSend", "ajaxSuccess", "ajaxError", "ajaxComplete", "ajaxStop"
  1228. ], function(evt){
  1229. f[evt] = function(callback){
  1230. if(callback){
  1231. this.bind(evt, callback);
  1232. }else{
  1233. this.trigger(evt);
  1234. }
  1235. return this;
  1236. }
  1237. }
  1238. );
  1239. //END jquery Events API methods
  1240. //START jquery Effects API methods
  1241. //http://docs.jquery.com/Effects
  1242. function speedInt(speed){
  1243. //Fix speed setting, translate string values to numbers.
  1244. if(dojo.isString(speed)){
  1245. if(speed == "slow"){
  1246. speed = 700;
  1247. }else if(speed = "fast"){
  1248. speed = 300;
  1249. }else{
  1250. //Everything else is considered normal speed.
  1251. speed = 500;
  1252. }
  1253. }
  1254. return speed;
  1255. }
  1256. f.hide = function(/*String||Number*/speed, /*Function?*/callback){
  1257. //Fix speed setting, translate string values to numbers.
  1258. speed = speedInt(speed);
  1259. this.forEach(function(node){
  1260. var style = node.style;
  1261. //Skip if already hidden
  1262. var cs = dojo.getComputedStyle(node);
  1263. if(cs.display == "none"){
  1264. return;
  1265. }
  1266. style.overflow = "hidden";
  1267. style.display = "block";
  1268. if(speed){
  1269. //It is alive!
  1270. dojo.anim(
  1271. node,
  1272. {
  1273. width: 0,
  1274. height: 0,
  1275. opacity: 0
  1276. },
  1277. speed,
  1278. null,
  1279. function(){
  1280. style.width = "";
  1281. style.height = "";
  1282. style.display = "none";
  1283. return callback && callback.call(node);
  1284. }
  1285. );
  1286. }else{
  1287. //No need for animation, fast path it.
  1288. dojo.style(node, "display", "none");
  1289. if(callback){
  1290. callback.call(node);
  1291. }
  1292. }
  1293. });
  1294. return this;
  1295. }
  1296. f.show = function(/*String||Number*/speed, /*Function?*/callback){
  1297. //Fix speed setting, translate string values to numbers.
  1298. speed = speedInt(speed);
  1299. this.forEach(function(node){
  1300. var style = node.style;
  1301. //Skip if the node is already showing.
  1302. var cs = dojo.getComputedStyle(node);
  1303. if(cs.display != "none"){
  1304. return;
  1305. }
  1306. if(speed){
  1307. //Figure out size of element
  1308. //so we know when to stop animation.
  1309. //Try the easy path first.
  1310. var width = parseFloat(style.width);
  1311. var height = parseFloat(style.height);
  1312. if(!width || !height){
  1313. //temporarily show the element to get
  1314. //dimensions
  1315. style.display = "block";
  1316. var box = dojo.marginBox(node);
  1317. width = box.w;
  1318. height = box.h;
  1319. }
  1320. //Make sure values are set to hidden state.
  1321. style.width = 0;
  1322. style.height = 0;
  1323. style.overflow = "hidden";
  1324. dojo.attr(node, "opacity", 0);
  1325. style.display = "block";
  1326. //It is alive!
  1327. dojo.anim(
  1328. node,
  1329. {
  1330. width: width,
  1331. height: height,
  1332. opacity: 1
  1333. },
  1334. speed,
  1335. null,
  1336. callback ? dojo.hitch(node, callback) : undefined
  1337. );
  1338. }else{
  1339. dojo.style(node, "display", "block");
  1340. if(callback){
  1341. callback.call(node);
  1342. }
  1343. }
  1344. });
  1345. return this;
  1346. }
  1347. //END jquery Effects API methods
  1348. //START jquery Ajax API methods
  1349. //http://docs.jquery.com/Ajax
  1350. $.ajaxSettings = {
  1351. };
  1352. $.ajaxSetup = function(/*Object*/args){
  1353. dojo.mixin($.ajaxSettings, args);
  1354. }
  1355. var topics = {
  1356. "ajaxStart": "/dojo/io/start",
  1357. "ajaxSend": "/dojo/io/send",
  1358. "ajaxSuccess": "/dojo/io/load",
  1359. "ajaxError": "/dojo/io/error",
  1360. "ajaxComplete": "/dojo/io/done",
  1361. "ajaxStop": "/dojo/io/stop"
  1362. };
  1363. for(var fnName in topics){
  1364. //Make sure we are dealing with properties
  1365. //we care about and not something another toolkit added.
  1366. if(fnName.indexOf("ajax") == 0){
  1367. ;(function(fnName){
  1368. f[fnName] = function(callback){
  1369. this.forEach(function(node){
  1370. dojo.subscribe(topics[fnName], function(){
  1371. var fakeEvt = new $.Event(fnName);
  1372. var ioArgs = arguments[0] && arguments[0].ioArgs;
  1373. var xhr = ioArgs && ioArgs.xhr;
  1374. var args = ioArgs && ioArgs.args;
  1375. var res = arguments[1];
  1376. if("ajaxComplete|ajaxSend|ajaxSuccess".indexOf(fnName) != -1){
  1377. return callback.call(node, fakeEvt, xhr, args);
  1378. }else if(fnName == "ajaxError"){
  1379. return callback.call(node, fakeEvt, xhr, args, res);
  1380. }else{
  1381. //ajaxStart|ajaxStop
  1382. return callback.call(node, fakeEvt);
  1383. }
  1384. });
  1385. });
  1386. return this;
  1387. }
  1388. })(fnName);
  1389. }
  1390. };
  1391. //Override dojo._xhrObj(dfd.ioArgs.args) to support beforeSend
  1392. //Do not understand the reason for beforeSend, particularly
  1393. //returning false stops the request.
  1394. //WARNING: even with this code, the error and complete callbacks
  1395. //will be fired because the deferred is cancelled. I feel this is
  1396. //correct behavior for dojo, and not sure why beforeSend is needed.
  1397. var _oldXhrObj = dojo._xhrObj;
  1398. dojo._xhrObj = function(args){
  1399. var xhr = _oldXhrObj.apply(dojo, arguments);
  1400. if(args && args.beforeSend){
  1401. if(args.beforeSend(xhr) === false){
  1402. return false;
  1403. }
  1404. }
  1405. return xhr;
  1406. }
  1407. $.ajax = function(/*Object*/args){
  1408. //Not sure if the args are considered mutable.
  1409. //Copy them to be safe.
  1410. var temp = dojo.delegate($.ajaxSettings);
  1411. for(var param in args){
  1412. //For data objects merge the data do not overwrite.
  1413. if(param == "data" && dojo.isObject(args[param]) && dojo.isObject(temp.data)){
  1414. for(var prop in args[param]){
  1415. temp.data[prop] = args[param][prop];
  1416. }
  1417. }else{
  1418. temp[param] = args[param];
  1419. }
  1420. }
  1421. args = temp;
  1422. var url = args.url;
  1423. if("async" in args){
  1424. args.sync = !args.async;
  1425. }
  1426. //Turn off topic publications
  1427. if(args.global === false){
  1428. args.ioPublish = false;
  1429. }
  1430. if(args.data){
  1431. var data = args.data;
  1432. if(dojo.isString(data)){
  1433. //convert to an object.
  1434. args.content = dojo.queryToObject(data);
  1435. }else{
  1436. //data property values could be a function, be sure to call them if so.
  1437. //Hmm, this seems to be of dubious value.
  1438. for(var param in data){
  1439. if(dojo.isFunction(data[param])){
  1440. data[param] = data[param]();
  1441. }
  1442. }
  1443. args.content = data;
  1444. }
  1445. }
  1446. //dataType
  1447. var dataType = args.dataType;
  1448. if("dataType" in args){
  1449. if(dataType == "script"){
  1450. dataType = "javascript";
  1451. }else if(dataType == "html"){
  1452. dataType = "text";
  1453. }
  1454. args.handleAs = dataType;
  1455. }else{
  1456. //Make a guess based on the URL.
  1457. dataType = args.handleAs = "text";
  1458. args.guessedType = true;
  1459. }
  1460. //cache:
  1461. if("cache" in args){
  1462. args.preventCache = !args.cache;
  1463. }else{
  1464. if(args.dataType == "script" || args.dataType == "jsonp"){
  1465. args.preventCache = true;
  1466. }
  1467. }
  1468. //Hide error since dojo treats it different.
  1469. if(args.error){
  1470. args._jqueryError = args.error;
  1471. delete args.error;
  1472. }
  1473. //TODO: dataFilter
  1474. //Set up callbacks.
  1475. args.handle = function(result, ioArgs){
  1476. var textStatus = "success";
  1477. if(result instanceof Error){
  1478. textStatus = (result.dojoType == "timeout" ? "timeout" : "error");
  1479. if(args._jqueryError){
  1480. args._jqueryError(ioArgs.xhr, textStatus, result);
  1481. }
  1482. }else{
  1483. //If we guessed the type, see if it should be XML.
  1484. var xml = (ioArgs.args.guessedType && ioArgs.xhr && ioArgs.xhr.responseXML);
  1485. if(xml){
  1486. result = xml;
  1487. }
  1488. if(args.success){
  1489. args.success(result, textStatus, ioArgs.xhr);
  1490. }
  1491. }
  1492. if(args.complete){
  1493. args.complete(result, textStatus, ioArgs.xhr);
  1494. }
  1495. return result;
  1496. };
  1497. //Use a script tag if the request is xdomain or a jsonp thing.
  1498. var useScript = (dataType == "jsonp");
  1499. if(dataType == "javascript"){
  1500. //Get protocol and domain.
  1501. var colonIndex = url.indexOf(":");
  1502. var slashIndex = url.indexOf("/");
  1503. if(colonIndex > 0 && colonIndex < slashIndex){
  1504. //Possibly xdomain. Peel off protocol and hostname to find out.
  1505. var lastSlash = url.indexOf("/", slashIndex + 2);
  1506. if(lastSlash == -1){
  1507. lastSlash = url.length;
  1508. }
  1509. if(location.protocol != url.substring(0, colonIndex + 1) ||
  1510. location.hostname != url.substring(slashIndex + 2, lastSlash)){
  1511. useScript = true;
  1512. }
  1513. }
  1514. }
  1515. if(useScript){
  1516. if(dataType == "jsonp"){
  1517. //Look for callback param
  1518. var cb = args.jsonp;
  1519. if(!cb){
  1520. //Look in the URL
  1521. var params = args.url.split("?")[1];
  1522. if(params && (params = dojo.queryToObject(params))){
  1523. cb = findJsonpCallback(params);
  1524. if(cb){
  1525. //Remove the cb from the url.
  1526. var regex = new RegExp("([&\\?])?" + cb + "=?");
  1527. args.url = args.url.replace(regex + "=?");
  1528. }
  1529. }
  1530. //Look in the content.
  1531. if(!cb){
  1532. cb = findJsonpCallback(args.content);
  1533. if(cb){
  1534. delete args.content[cb];
  1535. }
  1536. }
  1537. }
  1538. args.jsonp = cb || "callback";
  1539. }
  1540. var dfd = dojo.io.script.get(args);
  1541. return dfd;
  1542. }else{
  1543. var dfd = dojo.xhr(args.type || "GET", args);
  1544. //If the XHR object is false, it means beforeSend canceled the request.
  1545. return dfd.ioArgs.xhr === false ? false : dfd.ioArgs.xhr;
  1546. }
  1547. }
  1548. function findJsonpCallback(obj){
  1549. for(var prop in obj){
  1550. if(prop.indexOf("callback") == prop.length - 8){
  1551. return prop;
  1552. }
  1553. }
  1554. return null;
  1555. }
  1556. $.getpost = function(httpType, url, data, callback, dataType){
  1557. var args = {
  1558. url: url,
  1559. type: httpType
  1560. };
  1561. //Normalize data, considering it may be the real
  1562. //callback.
  1563. if(data){
  1564. if(dojo.isFunction(data) && !callback){
  1565. args.complete = data;
  1566. }else{
  1567. args.data = data;
  1568. }
  1569. }
  1570. //Normalize callback, considering it may be
  1571. //the datatype.
  1572. if(callback){
  1573. if(dojo.isString(callback) && !dataType){
  1574. dataType = callback;
  1575. }else{
  1576. args.complete = callback;
  1577. }
  1578. }
  1579. if(dataType){
  1580. args.dataType = dataType;
  1581. }
  1582. return $.ajax(args);
  1583. };
  1584. $.get = dojo.hitch($, "getpost", "GET");
  1585. $.post = dojo.hitch($, "getpost", "POST");
  1586. $.getJSON = function(url, data, callback){
  1587. return $.getpost("GET", url, data, callback, "json");
  1588. }
  1589. $.getScript = function(url, callback){
  1590. return $.ajax({
  1591. url: url,
  1592. success: callback,
  1593. dataType: "script"
  1594. });
  1595. }
  1596. f.load = function(url, data, callback){
  1597. //See if this is a window or document. If so, then want to
  1598. //register onload handler.
  1599. var node = this[0];
  1600. if(!node || !node.nodeType || node.nodeType == 9){
  1601. dojo.addOnLoad(url);
  1602. return this;
  1603. }
  1604. //The rest of this function is the ajax HTML load case.
  1605. //Pull off selector if it is on the url.
  1606. var parts = url.split(/\s+/);
  1607. url = parts[0];
  1608. var query = parts[1];
  1609. var finalCb = callback || data;
  1610. var cb = dojo.hitch(this, function(result, textStatus, xhr){
  1611. //Try to find all the body content.
  1612. var match = result.match(/\<\s*body[^>]+>.*<\/body\s*>/i);
  1613. if(match){
  1614. result = match;
  1615. }
  1616. //Convert to DOM nodes.
  1617. var nodes = dojo._toDom(result);
  1618. //Apply query, using a temp div to do the filtering.
  1619. if(query){
  1620. var temp = $(dojo.create("div"));
  1621. temp.append(nodes);
  1622. nodes = temp.find(query);
  1623. }else{
  1624. nodes = $(nodes.nodeType == 11 ? nodes.childNodes : nodes);
  1625. }
  1626. //Add the HTML to all nodes in this node list.
  1627. this.html(nodes);
  1628. //Call the user's callback.
  1629. //Use a timeout to allow any embedded scripts that
  1630. //were just inserted to run.
  1631. if(finalCb){
  1632. setTimeout(dojo.hitch(this, function(){
  1633. this.forEach(function(node){
  1634. finalCb.call(node, result, textStatus, xhr);
  1635. });
  1636. }), 10);
  1637. }
  1638. });
  1639. //Adjust parameters since they are variable.
  1640. if(!callback){
  1641. data = cb;
  1642. }else{
  1643. callback = cb;
  1644. }
  1645. //Set HTTP method. If the data is a string, use get, if it is an object,
  1646. //use post.
  1647. var method = "GET";
  1648. if(data && dojo.isObject(data)){
  1649. method = "POST";
  1650. }
  1651. $.getpost(method, url, data, callback, "html");
  1652. return this;
  1653. }
  1654. var serializeExclude = "file|submit|image|reset|button|";
  1655. f.serialize = function(){
  1656. var ret = "";
  1657. var strs = this.map(function(node){
  1658. if(node.nodeName.toUpperCase() == "FORM"){
  1659. return dojo.formToQuery(node);
  1660. }else{
  1661. var type = (node.type||"").toLowerCase();
  1662. if(serializeExclude.indexOf(type) == -1){
  1663. var val = dojo.fieldToObject(node);
  1664. if(node.name && val != null){
  1665. var q = {};
  1666. q[node.name] = val;
  1667. return dojo.objectToQuery(q);
  1668. }
  1669. }
  1670. }
  1671. });
  1672. return ret + strs.join("&");
  1673. }
  1674. $.param = function(obj){
  1675. if(obj._is$ && obj.serialize){
  1676. return obj.serialize();
  1677. }else if(dojo.isArray(obj)){
  1678. return dojo.map(obj, function(item){
  1679. return $.param(item);
  1680. }).join("&");
  1681. }else{
  1682. return dojo.objectToQuery(obj);
  1683. }
  1684. }
  1685. //END jquery Ajax API methods
  1686. //START jquery Utilities API methods
  1687. //http://docs.jquery.com/Utilities
  1688. //TODO:
  1689. $.isFunction = function(){
  1690. var result = dojo.isFunction.apply(dojo, arguments);
  1691. //Make sure Object does not return true
  1692. if(result){
  1693. result = (typeof(arguments[0]) != "object");
  1694. }
  1695. return result;
  1696. }
  1697. //END jquery Utilities API methods
  1698. })();
  1699. }