jq.js 51 KB

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