Base.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /***
  2. MochiKit.Base 1.4
  3. See <http://mochikit.com/> for documentation, downloads, license, etc.
  4. (c) 2005 Bob Ippolito. All rights Reserved.
  5. ***/
  6. if (typeof(dojo) != 'undefined') {
  7. dojo.provide("MochiKit.Base");
  8. }
  9. if (typeof(MochiKit) == 'undefined') {
  10. MochiKit = {};
  11. }
  12. if (typeof(MochiKit.Base) == 'undefined') {
  13. MochiKit.Base = {};
  14. }
  15. if (typeof(MochiKit.__export__) == "undefined") {
  16. MochiKit.__export__ = (MochiKit.__compat__ ||
  17. (typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined')
  18. );
  19. }
  20. MochiKit.Base.VERSION = "1.4";
  21. MochiKit.Base.NAME = "MochiKit.Base";
  22. /** @id MochiKit.Base.update */
  23. MochiKit.Base.update = function (self, obj/*, ... */) {
  24. if (self === null) {
  25. self = {};
  26. }
  27. for (var i = 1; i < arguments.length; i++) {
  28. var o = arguments[i];
  29. if (typeof(o) != 'undefined' && o !== null) {
  30. for (var k in o) {
  31. self[k] = o[k];
  32. }
  33. }
  34. }
  35. return self;
  36. };
  37. MochiKit.Base.update(MochiKit.Base, {
  38. __repr__: function () {
  39. return "[" + this.NAME + " " + this.VERSION + "]";
  40. },
  41. toString: function () {
  42. return this.__repr__();
  43. },
  44. /** @id MochiKit.Base.camelize */
  45. camelize: function (selector) {
  46. /* from dojo.style.toCamelCase */
  47. var arr = selector.split('-');
  48. var cc = arr[0];
  49. for (var i = 1; i < arr.length; i++) {
  50. cc += arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
  51. }
  52. return cc;
  53. },
  54. /** @id MochiKit.Base.counter */
  55. counter: function (n/* = 1 */) {
  56. if (arguments.length === 0) {
  57. n = 1;
  58. }
  59. return function () {
  60. return n++;
  61. };
  62. },
  63. /** @id MochiKit.Base.clone */
  64. clone: function (obj) {
  65. var me = arguments.callee;
  66. if (arguments.length == 1) {
  67. me.prototype = obj;
  68. return new me();
  69. }
  70. },
  71. _flattenArray: function (res, lst) {
  72. for (var i = 0; i < lst.length; i++) {
  73. var o = lst[i];
  74. if (o instanceof Array) {
  75. arguments.callee(res, o);
  76. } else {
  77. res.push(o);
  78. }
  79. }
  80. return res;
  81. },
  82. /** @id MochiKit.Base.flattenArray */
  83. flattenArray: function (lst) {
  84. return MochiKit.Base._flattenArray([], lst);
  85. },
  86. /** @id MochiKit.Base.flattenArguments */
  87. flattenArguments: function (lst/* ...*/) {
  88. var res = [];
  89. var m = MochiKit.Base;
  90. var args = m.extend(null, arguments);
  91. while (args.length) {
  92. var o = args.shift();
  93. if (o && typeof(o) == "object" && typeof(o.length) == "number") {
  94. for (var i = o.length - 1; i >= 0; i--) {
  95. args.unshift(o[i]);
  96. }
  97. } else {
  98. res.push(o);
  99. }
  100. }
  101. return res;
  102. },
  103. /** @id MochiKit.Base.extend */
  104. extend: function (self, obj, /* optional */skip) {
  105. // Extend an array with an array-like object starting
  106. // from the skip index
  107. if (!skip) {
  108. skip = 0;
  109. }
  110. if (obj) {
  111. // allow iterable fall-through, but skip the full isArrayLike
  112. // check for speed, this is called often.
  113. var l = obj.length;
  114. if (typeof(l) != 'number' /* !isArrayLike(obj) */) {
  115. if (typeof(MochiKit.Iter) != "undefined") {
  116. obj = MochiKit.Iter.list(obj);
  117. l = obj.length;
  118. } else {
  119. throw new TypeError("Argument not an array-like and MochiKit.Iter not present");
  120. }
  121. }
  122. if (!self) {
  123. self = [];
  124. }
  125. for (var i = skip; i < l; i++) {
  126. self.push(obj[i]);
  127. }
  128. }
  129. // This mutates, but it's convenient to return because
  130. // it's often used like a constructor when turning some
  131. // ghetto array-like to a real array
  132. return self;
  133. },
  134. /** @id MochiKit.Base.updatetree */
  135. updatetree: function (self, obj/*, ...*/) {
  136. if (self === null) {
  137. self = {};
  138. }
  139. for (var i = 1; i < arguments.length; i++) {
  140. var o = arguments[i];
  141. if (typeof(o) != 'undefined' && o !== null) {
  142. for (var k in o) {
  143. var v = o[k];
  144. if (typeof(self[k]) == 'object' && typeof(v) == 'object') {
  145. arguments.callee(self[k], v);
  146. } else {
  147. self[k] = v;
  148. }
  149. }
  150. }
  151. }
  152. return self;
  153. },
  154. /** @id MochiKit.Base.setdefault */
  155. setdefault: function (self, obj/*, ...*/) {
  156. if (self === null) {
  157. self = {};
  158. }
  159. for (var i = 1; i < arguments.length; i++) {
  160. var o = arguments[i];
  161. for (var k in o) {
  162. if (!(k in self)) {
  163. self[k] = o[k];
  164. }
  165. }
  166. }
  167. return self;
  168. },
  169. /** @id MochiKit.Base.keys */
  170. keys: function (obj) {
  171. var rval = [];
  172. for (var prop in obj) {
  173. rval.push(prop);
  174. }
  175. return rval;
  176. },
  177. /** @id MochiKit.Base.values */
  178. values: function (obj) {
  179. var rval = [];
  180. for (var prop in obj) {
  181. rval.push(obj[prop]);
  182. }
  183. return rval;
  184. },
  185. /** @id MochiKit.Base.items */
  186. items: function (obj) {
  187. var rval = [];
  188. var e;
  189. for (var prop in obj) {
  190. var v;
  191. try {
  192. v = obj[prop];
  193. } catch (e) {
  194. continue;
  195. }
  196. rval.push([prop, v]);
  197. }
  198. return rval;
  199. },
  200. _newNamedError: function (module, name, func) {
  201. func.prototype = new MochiKit.Base.NamedError(module.NAME + "." + name);
  202. module[name] = func;
  203. },
  204. /** @id MochiKit.Base.operator */
  205. operator: {
  206. // unary logic operators
  207. /** @id MochiKit.Base.truth */
  208. truth: function (a) { return !!a; },
  209. /** @id MochiKit.Base.lognot */
  210. lognot: function (a) { return !a; },
  211. /** @id MochiKit.Base.identity */
  212. identity: function (a) { return a; },
  213. // bitwise unary operators
  214. /** @id MochiKit.Base.not */
  215. not: function (a) { return ~a; },
  216. /** @id MochiKit.Base.neg */
  217. neg: function (a) { return -a; },
  218. // binary operators
  219. /** @id MochiKit.Base.add */
  220. add: function (a, b) { return a + b; },
  221. /** @id MochiKit.Base.sub */
  222. sub: function (a, b) { return a - b; },
  223. /** @id MochiKit.Base.div */
  224. div: function (a, b) { return a / b; },
  225. /** @id MochiKit.Base.mod */
  226. mod: function (a, b) { return a % b; },
  227. /** @id MochiKit.Base.mul */
  228. mul: function (a, b) { return a * b; },
  229. // bitwise binary operators
  230. /** @id MochiKit.Base.and */
  231. and: function (a, b) { return a & b; },
  232. /** @id MochiKit.Base.or */
  233. or: function (a, b) { return a | b; },
  234. /** @id MochiKit.Base.xor */
  235. xor: function (a, b) { return a ^ b; },
  236. /** @id MochiKit.Base.lshift */
  237. lshift: function (a, b) { return a << b; },
  238. /** @id MochiKit.Base.rshift */
  239. rshift: function (a, b) { return a >> b; },
  240. /** @id MochiKit.Base.zrshift */
  241. zrshift: function (a, b) { return a >>> b; },
  242. // near-worthless built-in comparators
  243. /** @id MochiKit.Base.eq */
  244. eq: function (a, b) { return a == b; },
  245. /** @id MochiKit.Base.ne */
  246. ne: function (a, b) { return a != b; },
  247. /** @id MochiKit.Base.gt */
  248. gt: function (a, b) { return a > b; },
  249. /** @id MochiKit.Base.ge */
  250. ge: function (a, b) { return a >= b; },
  251. /** @id MochiKit.Base.lt */
  252. lt: function (a, b) { return a < b; },
  253. /** @id MochiKit.Base.le */
  254. le: function (a, b) { return a <= b; },
  255. // strict built-in comparators
  256. seq: function (a, b) { return a === b; },
  257. sne: function (a, b) { return a !== b; },
  258. // compare comparators
  259. /** @id MochiKit.Base.ceq */
  260. ceq: function (a, b) { return MochiKit.Base.compare(a, b) === 0; },
  261. /** @id MochiKit.Base.cne */
  262. cne: function (a, b) { return MochiKit.Base.compare(a, b) !== 0; },
  263. /** @id MochiKit.Base.cgt */
  264. cgt: function (a, b) { return MochiKit.Base.compare(a, b) == 1; },
  265. /** @id MochiKit.Base.cge */
  266. cge: function (a, b) { return MochiKit.Base.compare(a, b) != -1; },
  267. /** @id MochiKit.Base.clt */
  268. clt: function (a, b) { return MochiKit.Base.compare(a, b) == -1; },
  269. /** @id MochiKit.Base.cle */
  270. cle: function (a, b) { return MochiKit.Base.compare(a, b) != 1; },
  271. // binary logical operators
  272. /** @id MochiKit.Base.logand */
  273. logand: function (a, b) { return a && b; },
  274. /** @id MochiKit.Base.logor */
  275. logor: function (a, b) { return a || b; },
  276. /** @id MochiKit.Base.contains */
  277. contains: function (a, b) { return b in a; }
  278. },
  279. /** @id MochiKit.Base.forwardCall */
  280. forwardCall: function (func) {
  281. return function () {
  282. return this[func].apply(this, arguments);
  283. };
  284. },
  285. /** @id MochiKit.Base.itemgetter */
  286. itemgetter: function (func) {
  287. return function (arg) {
  288. return arg[func];
  289. };
  290. },
  291. /** @id MochiKit.Base.typeMatcher */
  292. typeMatcher: function (/* typ */) {
  293. var types = {};
  294. for (var i = 0; i < arguments.length; i++) {
  295. var typ = arguments[i];
  296. types[typ] = typ;
  297. }
  298. return function () {
  299. for (var i = 0; i < arguments.length; i++) {
  300. if (!(typeof(arguments[i]) in types)) {
  301. return false;
  302. }
  303. }
  304. return true;
  305. };
  306. },
  307. /** @id MochiKit.Base.isNull */
  308. isNull: function (/* ... */) {
  309. for (var i = 0; i < arguments.length; i++) {
  310. if (arguments[i] !== null) {
  311. return false;
  312. }
  313. }
  314. return true;
  315. },
  316. /** @id MochiKit.Base.isUndefinedOrNull */
  317. isUndefinedOrNull: function (/* ... */) {
  318. for (var i = 0; i < arguments.length; i++) {
  319. var o = arguments[i];
  320. if (!(typeof(o) == 'undefined' || o === null)) {
  321. return false;
  322. }
  323. }
  324. return true;
  325. },
  326. /** @id MochiKit.Base.isEmpty */
  327. isEmpty: function (obj) {
  328. return !MochiKit.Base.isNotEmpty.apply(this, arguments);
  329. },
  330. /** @id MochiKit.Base.isNotEmpty */
  331. isNotEmpty: function (obj) {
  332. for (var i = 0; i < arguments.length; i++) {
  333. var o = arguments[i];
  334. if (!(o && o.length)) {
  335. return false;
  336. }
  337. }
  338. return true;
  339. },
  340. /** @id MochiKit.Base.isArrayLike */
  341. isArrayLike: function () {
  342. for (var i = 0; i < arguments.length; i++) {
  343. var o = arguments[i];
  344. var typ = typeof(o);
  345. if (
  346. (typ != 'object' && !(typ == 'function' && typeof(o.item) == 'function')) ||
  347. o === null ||
  348. typeof(o.length) != 'number' ||
  349. o.nodeType === 3
  350. ) {
  351. return false;
  352. }
  353. }
  354. return true;
  355. },
  356. /** @id MochiKit.Base.isDateLike */
  357. isDateLike: function () {
  358. for (var i = 0; i < arguments.length; i++) {
  359. var o = arguments[i];
  360. if (typeof(o) != "object" || o === null
  361. || typeof(o.getTime) != 'function') {
  362. return false;
  363. }
  364. }
  365. return true;
  366. },
  367. /** @id MochiKit.Base.xmap */
  368. xmap: function (fn/*, obj... */) {
  369. if (fn === null) {
  370. return MochiKit.Base.extend(null, arguments, 1);
  371. }
  372. var rval = [];
  373. for (var i = 1; i < arguments.length; i++) {
  374. rval.push(fn(arguments[i]));
  375. }
  376. return rval;
  377. },
  378. /** @id MochiKit.Base.map */
  379. map: function (fn, lst/*, lst... */) {
  380. var m = MochiKit.Base;
  381. var itr = MochiKit.Iter;
  382. var isArrayLike = m.isArrayLike;
  383. if (arguments.length <= 2) {
  384. // allow an iterable to be passed
  385. if (!isArrayLike(lst)) {
  386. if (itr) {
  387. // fast path for map(null, iterable)
  388. lst = itr.list(lst);
  389. if (fn === null) {
  390. return lst;
  391. }
  392. } else {
  393. throw new TypeError("Argument not an array-like and MochiKit.Iter not present");
  394. }
  395. }
  396. // fast path for map(null, lst)
  397. if (fn === null) {
  398. return m.extend(null, lst);
  399. }
  400. // disabled fast path for map(fn, lst)
  401. /*
  402. if (false && typeof(Array.prototype.map) == 'function') {
  403. // Mozilla fast-path
  404. return Array.prototype.map.call(lst, fn);
  405. }
  406. */
  407. var rval = [];
  408. for (var i = 0; i < lst.length; i++) {
  409. rval.push(fn(lst[i]));
  410. }
  411. return rval;
  412. } else {
  413. // default for map(null, ...) is zip(...)
  414. if (fn === null) {
  415. fn = Array;
  416. }
  417. var length = null;
  418. for (i = 1; i < arguments.length; i++) {
  419. // allow iterables to be passed
  420. if (!isArrayLike(arguments[i])) {
  421. if (itr) {
  422. return itr.list(itr.imap.apply(null, arguments));
  423. } else {
  424. throw new TypeError("Argument not an array-like and MochiKit.Iter not present");
  425. }
  426. }
  427. // find the minimum length
  428. var l = arguments[i].length;
  429. if (length === null || length > l) {
  430. length = l;
  431. }
  432. }
  433. rval = [];
  434. for (i = 0; i < length; i++) {
  435. var args = [];
  436. for (var j = 1; j < arguments.length; j++) {
  437. args.push(arguments[j][i]);
  438. }
  439. rval.push(fn.apply(this, args));
  440. }
  441. return rval;
  442. }
  443. },
  444. /** @id MochiKit.Base.xfilter */
  445. xfilter: function (fn/*, obj... */) {
  446. var rval = [];
  447. if (fn === null) {
  448. fn = MochiKit.Base.operator.truth;
  449. }
  450. for (var i = 1; i < arguments.length; i++) {
  451. var o = arguments[i];
  452. if (fn(o)) {
  453. rval.push(o);
  454. }
  455. }
  456. return rval;
  457. },
  458. /** @id MochiKit.Base.filter */
  459. filter: function (fn, lst, self) {
  460. var rval = [];
  461. // allow an iterable to be passed
  462. var m = MochiKit.Base;
  463. if (!m.isArrayLike(lst)) {
  464. if (MochiKit.Iter) {
  465. lst = MochiKit.Iter.list(lst);
  466. } else {
  467. throw new TypeError("Argument not an array-like and MochiKit.Iter not present");
  468. }
  469. }
  470. if (fn === null) {
  471. fn = m.operator.truth;
  472. }
  473. if (typeof(Array.prototype.filter) == 'function') {
  474. // Mozilla fast-path
  475. return Array.prototype.filter.call(lst, fn, self);
  476. } else if (typeof(self) == 'undefined' || self === null) {
  477. for (var i = 0; i < lst.length; i++) {
  478. var o = lst[i];
  479. if (fn(o)) {
  480. rval.push(o);
  481. }
  482. }
  483. } else {
  484. for (i = 0; i < lst.length; i++) {
  485. o = lst[i];
  486. if (fn.call(self, o)) {
  487. rval.push(o);
  488. }
  489. }
  490. }
  491. return rval;
  492. },
  493. _wrapDumbFunction: function (func) {
  494. return function () {
  495. // fast path!
  496. switch (arguments.length) {
  497. case 0: return func();
  498. case 1: return func(arguments[0]);
  499. case 2: return func(arguments[0], arguments[1]);
  500. case 3: return func(arguments[0], arguments[1], arguments[2]);
  501. }
  502. var args = [];
  503. for (var i = 0; i < arguments.length; i++) {
  504. args.push("arguments[" + i + "]");
  505. }
  506. return eval("(func(" + args.join(",") + "))");
  507. };
  508. },
  509. /** @id MochiKit.Base.methodcaller */
  510. methodcaller: function (func/*, args... */) {
  511. var args = MochiKit.Base.extend(null, arguments, 1);
  512. if (typeof(func) == "function") {
  513. return function (obj) {
  514. return func.apply(obj, args);
  515. };
  516. } else {
  517. return function (obj) {
  518. return obj[func].apply(obj, args);
  519. };
  520. }
  521. },
  522. /** @id MochiKit.Base.method */
  523. method: function (self, func) {
  524. var m = MochiKit.Base;
  525. return m.bind.apply(this, m.extend([func, self], arguments, 2));
  526. },
  527. /** @id MochiKit.Base.compose */
  528. compose: function (f1, f2/*, f3, ... fN */) {
  529. var fnlist = [];
  530. var m = MochiKit.Base;
  531. if (arguments.length === 0) {
  532. throw new TypeError("compose() requires at least one argument");
  533. }
  534. for (var i = 0; i < arguments.length; i++) {
  535. var fn = arguments[i];
  536. if (typeof(fn) != "function") {
  537. throw new TypeError(m.repr(fn) + " is not a function");
  538. }
  539. fnlist.push(fn);
  540. }
  541. return function () {
  542. var args = arguments;
  543. for (var i = fnlist.length - 1; i >= 0; i--) {
  544. args = [fnlist[i].apply(this, args)];
  545. }
  546. return args[0];
  547. };
  548. },
  549. /** @id MochiKit.Base.bind */
  550. bind: function (func, self/* args... */) {
  551. if (typeof(func) == "string") {
  552. func = self[func];
  553. }
  554. var im_func = func.im_func;
  555. var im_preargs = func.im_preargs;
  556. var im_self = func.im_self;
  557. var m = MochiKit.Base;
  558. if (typeof(func) == "function" && typeof(func.apply) == "undefined") {
  559. // this is for cases where JavaScript sucks ass and gives you a
  560. // really dumb built-in function like alert() that doesn't have
  561. // an apply
  562. func = m._wrapDumbFunction(func);
  563. }
  564. if (typeof(im_func) != 'function') {
  565. im_func = func;
  566. }
  567. if (typeof(self) != 'undefined') {
  568. im_self = self;
  569. }
  570. if (typeof(im_preargs) == 'undefined') {
  571. im_preargs = [];
  572. } else {
  573. im_preargs = im_preargs.slice();
  574. }
  575. m.extend(im_preargs, arguments, 2);
  576. var newfunc = function () {
  577. var args = arguments;
  578. var me = arguments.callee;
  579. if (me.im_preargs.length > 0) {
  580. args = m.concat(me.im_preargs, args);
  581. }
  582. var self = me.im_self;
  583. if (!self) {
  584. self = this;
  585. }
  586. return me.im_func.apply(self, args);
  587. };
  588. newfunc.im_self = im_self;
  589. newfunc.im_func = im_func;
  590. newfunc.im_preargs = im_preargs;
  591. return newfunc;
  592. },
  593. /** @id MochiKit.Base.bindMethods */
  594. bindMethods: function (self) {
  595. var bind = MochiKit.Base.bind;
  596. for (var k in self) {
  597. var func = self[k];
  598. if (typeof(func) == 'function') {
  599. self[k] = bind(func, self);
  600. }
  601. }
  602. },
  603. /** @id MochiKit.Base.registerComparator */
  604. registerComparator: function (name, check, comparator, /* optional */ override) {
  605. MochiKit.Base.comparatorRegistry.register(name, check, comparator, override);
  606. },
  607. _primitives: {'boolean': true, 'string': true, 'number': true},
  608. /** @id MochiKit.Base.compare */
  609. compare: function (a, b) {
  610. if (a == b) {
  611. return 0;
  612. }
  613. var aIsNull = (typeof(a) == 'undefined' || a === null);
  614. var bIsNull = (typeof(b) == 'undefined' || b === null);
  615. if (aIsNull && bIsNull) {
  616. return 0;
  617. } else if (aIsNull) {
  618. return -1;
  619. } else if (bIsNull) {
  620. return 1;
  621. }
  622. var m = MochiKit.Base;
  623. // bool, number, string have meaningful comparisons
  624. var prim = m._primitives;
  625. if (!(typeof(a) in prim && typeof(b) in prim)) {
  626. try {
  627. return m.comparatorRegistry.match(a, b);
  628. } catch (e) {
  629. if (e != m.NotFound) {
  630. throw e;
  631. }
  632. }
  633. }
  634. if (a < b) {
  635. return -1;
  636. } else if (a > b) {
  637. return 1;
  638. }
  639. // These types can't be compared
  640. var repr = m.repr;
  641. throw new TypeError(repr(a) + " and " + repr(b) + " can not be compared");
  642. },
  643. /** @id MochiKit.Base.compareDateLike */
  644. compareDateLike: function (a, b) {
  645. return MochiKit.Base.compare(a.getTime(), b.getTime());
  646. },
  647. /** @id MochiKit.Base.compareArrayLike */
  648. compareArrayLike: function (a, b) {
  649. var compare = MochiKit.Base.compare;
  650. var count = a.length;
  651. var rval = 0;
  652. if (count > b.length) {
  653. rval = 1;
  654. count = b.length;
  655. } else if (count < b.length) {
  656. rval = -1;
  657. }
  658. for (var i = 0; i < count; i++) {
  659. var cmp = compare(a[i], b[i]);
  660. if (cmp) {
  661. return cmp;
  662. }
  663. }
  664. return rval;
  665. },
  666. /** @id MochiKit.Base.registerRepr */
  667. registerRepr: function (name, check, wrap, /* optional */override) {
  668. MochiKit.Base.reprRegistry.register(name, check, wrap, override);
  669. },
  670. /** @id MochiKit.Base.repr */
  671. repr: function (o) {
  672. if (typeof(o) == "undefined") {
  673. return "undefined";
  674. } else if (o === null) {
  675. return "null";
  676. }
  677. try {
  678. if (typeof(o.__repr__) == 'function') {
  679. return o.__repr__();
  680. } else if (typeof(o.repr) == 'function' && o.repr != arguments.callee) {
  681. return o.repr();
  682. }
  683. return MochiKit.Base.reprRegistry.match(o);
  684. } catch (e) {
  685. try {
  686. if (typeof(o.NAME) == 'string' && (
  687. o.toString == Function.prototype.toString ||
  688. o.toString == Object.prototype.toString
  689. )) {
  690. return o.NAME;
  691. }
  692. } catch (e) {
  693. }
  694. }
  695. try {
  696. var ostring = (o + "");
  697. } catch (e) {
  698. return "[" + typeof(o) + "]";
  699. }
  700. if (typeof(o) == "function") {
  701. o = ostring.replace(/^\s+/, "");
  702. var idx = o.indexOf("{");
  703. if (idx != -1) {
  704. o = o.substr(0, idx) + "{...}";
  705. }
  706. }
  707. return ostring;
  708. },
  709. /** @id MochiKit.Base.reprArrayLike */
  710. reprArrayLike: function (o) {
  711. var m = MochiKit.Base;
  712. return "[" + m.map(m.repr, o).join(", ") + "]";
  713. },
  714. /** @id MochiKit.Base.reprString */
  715. reprString: function (o) {
  716. return ('"' + o.replace(/(["\\])/g, '\\$1') + '"'
  717. ).replace(/[\f]/g, "\\f"
  718. ).replace(/[\b]/g, "\\b"
  719. ).replace(/[\n]/g, "\\n"
  720. ).replace(/[\t]/g, "\\t"
  721. ).replace(/[\r]/g, "\\r");
  722. },
  723. /** @id MochiKit.Base.reprNumber */
  724. reprNumber: function (o) {
  725. return o + "";
  726. },
  727. /** @id MochiKit.Base.registerJSON */
  728. registerJSON: function (name, check, wrap, /* optional */override) {
  729. MochiKit.Base.jsonRegistry.register(name, check, wrap, override);
  730. },
  731. /** @id MochiKit.Base.evalJSON */
  732. evalJSON: function () {
  733. return eval("(" + arguments[0] + ")");
  734. },
  735. /** @id MochiKit.Base.serializeJSON */
  736. serializeJSON: function (o) {
  737. var objtype = typeof(o);
  738. if (objtype == "number" || objtype == "boolean") {
  739. return o + "";
  740. } else if (o === null) {
  741. return "null";
  742. }
  743. var m = MochiKit.Base;
  744. var reprString = m.reprString;
  745. if (objtype == "string") {
  746. return reprString(o);
  747. }
  748. // recurse
  749. var me = arguments.callee;
  750. // short-circuit for objects that support "json" serialization
  751. // if they return "self" then just pass-through...
  752. var newObj;
  753. if (typeof(o.__json__) == "function") {
  754. newObj = o.__json__();
  755. if (o !== newObj) {
  756. return me(newObj);
  757. }
  758. }
  759. if (typeof(o.json) == "function") {
  760. newObj = o.json();
  761. if (o !== newObj) {
  762. return me(newObj);
  763. }
  764. }
  765. // array
  766. if (objtype != "function" && typeof(o.length) == "number") {
  767. var res = [];
  768. for (var i = 0; i < o.length; i++) {
  769. var val = me(o[i]);
  770. if (typeof(val) != "string") {
  771. val = "undefined";
  772. }
  773. res.push(val);
  774. }
  775. return "[" + res.join(", ") + "]";
  776. }
  777. // look in the registry
  778. try {
  779. newObj = m.jsonRegistry.match(o);
  780. if (o !== newObj) {
  781. return me(newObj);
  782. }
  783. } catch (e) {
  784. if (e != m.NotFound) {
  785. // something really bad happened
  786. throw e;
  787. }
  788. }
  789. // undefined is outside of the spec
  790. if (objtype == "undefined") {
  791. throw new TypeError("undefined can not be serialized as JSON");
  792. }
  793. // it's a function with no adapter, bad
  794. if (objtype == "function") {
  795. return null;
  796. }
  797. // generic object code path
  798. res = [];
  799. for (var k in o) {
  800. var useKey;
  801. if (typeof(k) == "number") {
  802. useKey = '"' + k + '"';
  803. } else if (typeof(k) == "string") {
  804. useKey = reprString(k);
  805. } else {
  806. // skip non-string or number keys
  807. continue;
  808. }
  809. val = me(o[k]);
  810. if (typeof(val) != "string") {
  811. // skip non-serializable values
  812. continue;
  813. }
  814. res.push(useKey + ":" + val);
  815. }
  816. return "{" + res.join(", ") + "}";
  817. },
  818. /** @id MochiKit.Base.objEqual */
  819. objEqual: function (a, b) {
  820. return (MochiKit.Base.compare(a, b) === 0);
  821. },
  822. /** @id MochiKit.Base.arrayEqual */
  823. arrayEqual: function (self, arr) {
  824. if (self.length != arr.length) {
  825. return false;
  826. }
  827. return (MochiKit.Base.compare(self, arr) === 0);
  828. },
  829. /** @id MochiKit.Base.concat */
  830. concat: function (/* lst... */) {
  831. var rval = [];
  832. var extend = MochiKit.Base.extend;
  833. for (var i = 0; i < arguments.length; i++) {
  834. extend(rval, arguments[i]);
  835. }
  836. return rval;
  837. },
  838. /** @id MochiKit.Base.keyComparator */
  839. keyComparator: function (key/* ... */) {
  840. // fast-path for single key comparisons
  841. var m = MochiKit.Base;
  842. var compare = m.compare;
  843. if (arguments.length == 1) {
  844. return function (a, b) {
  845. return compare(a[key], b[key]);
  846. };
  847. }
  848. var compareKeys = m.extend(null, arguments);
  849. return function (a, b) {
  850. var rval = 0;
  851. // keep comparing until something is inequal or we run out of
  852. // keys to compare
  853. for (var i = 0; (rval === 0) && (i < compareKeys.length); i++) {
  854. var key = compareKeys[i];
  855. rval = compare(a[key], b[key]);
  856. }
  857. return rval;
  858. };
  859. },
  860. /** @id MochiKit.Base.reverseKeyComparator */
  861. reverseKeyComparator: function (key) {
  862. var comparator = MochiKit.Base.keyComparator.apply(this, arguments);
  863. return function (a, b) {
  864. return comparator(b, a);
  865. };
  866. },
  867. /** @id MochiKit.Base.partial */
  868. partial: function (func) {
  869. var m = MochiKit.Base;
  870. return m.bind.apply(this, m.extend([func, undefined], arguments, 1));
  871. },
  872. /** @id MochiKit.Base.listMinMax */
  873. listMinMax: function (which, lst) {
  874. if (lst.length === 0) {
  875. return null;
  876. }
  877. var cur = lst[0];
  878. var compare = MochiKit.Base.compare;
  879. for (var i = 1; i < lst.length; i++) {
  880. var o = lst[i];
  881. if (compare(o, cur) == which) {
  882. cur = o;
  883. }
  884. }
  885. return cur;
  886. },
  887. /** @id MochiKit.Base.objMax */
  888. objMax: function (/* obj... */) {
  889. return MochiKit.Base.listMinMax(1, arguments);
  890. },
  891. /** @id MochiKit.Base.objMin */
  892. objMin: function (/* obj... */) {
  893. return MochiKit.Base.listMinMax(-1, arguments);
  894. },
  895. /** @id MochiKit.Base.findIdentical */
  896. findIdentical: function (lst, value, start/* = 0 */, /* optional */end) {
  897. if (typeof(end) == "undefined" || end === null) {
  898. end = lst.length;
  899. }
  900. if (typeof(start) == "undefined" || start === null) {
  901. start = 0;
  902. }
  903. for (var i = start; i < end; i++) {
  904. if (lst[i] === value) {
  905. return i;
  906. }
  907. }
  908. return -1;
  909. },
  910. /** @id MochiKit.Base.mean */
  911. mean: function(/* lst... */) {
  912. /* http://www.nist.gov/dads/HTML/mean.html */
  913. var sum = 0;
  914. var m = MochiKit.Base;
  915. var args = m.extend(null, arguments);
  916. var count = args.length;
  917. while (args.length) {
  918. var o = args.shift();
  919. if (o && typeof(o) == "object" && typeof(o.length) == "number") {
  920. count += o.length - 1;
  921. for (var i = o.length - 1; i >= 0; i--) {
  922. sum += o[i];
  923. }
  924. } else {
  925. sum += o;
  926. }
  927. }
  928. if (count <= 0) {
  929. throw new TypeError('mean() requires at least one argument');
  930. }
  931. return sum/count;
  932. },
  933. /** @id MochiKit.Base.median */
  934. median: function(/* lst... */) {
  935. /* http://www.nist.gov/dads/HTML/median.html */
  936. var data = MochiKit.Base.flattenArguments(arguments);
  937. if (data.length === 0) {
  938. throw new TypeError('median() requires at least one argument');
  939. }
  940. data.sort(compare);
  941. if (data.length % 2 == 0) {
  942. var upper = data.length / 2;
  943. return (data[upper] + data[upper - 1]) / 2;
  944. } else {
  945. return data[(data.length - 1) / 2];
  946. }
  947. },
  948. /** @id MochiKit.Base.findValue */
  949. findValue: function (lst, value, start/* = 0 */, /* optional */end) {
  950. if (typeof(end) == "undefined" || end === null) {
  951. end = lst.length;
  952. }
  953. if (typeof(start) == "undefined" || start === null) {
  954. start = 0;
  955. }
  956. var cmp = MochiKit.Base.compare;
  957. for (var i = start; i < end; i++) {
  958. if (cmp(lst[i], value) === 0) {
  959. return i;
  960. }
  961. }
  962. return -1;
  963. },
  964. /** @id MochiKit.Base.nodeWalk */
  965. nodeWalk: function (node, visitor) {
  966. var nodes = [node];
  967. var extend = MochiKit.Base.extend;
  968. while (nodes.length) {
  969. var res = visitor(nodes.shift());
  970. if (res) {
  971. extend(nodes, res);
  972. }
  973. }
  974. },
  975. /** @id MochiKit.Base.nameFunctions */
  976. nameFunctions: function (namespace) {
  977. var base = namespace.NAME;
  978. if (typeof(base) == 'undefined') {
  979. base = '';
  980. } else {
  981. base = base + '.';
  982. }
  983. for (var name in namespace) {
  984. var o = namespace[name];
  985. if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') {
  986. try {
  987. o.NAME = base + name;
  988. } catch (e) {
  989. // pass
  990. }
  991. }
  992. }
  993. },
  994. /** @id MochiKit.Base.queryString */
  995. queryString: function (names, values) {
  996. // check to see if names is a string or a DOM element, and if
  997. // MochiKit.DOM is available. If so, drop it like it's a form
  998. // Ugliest conditional in MochiKit? Probably!
  999. if (typeof(MochiKit.DOM) != "undefined" && arguments.length == 1
  1000. && (typeof(names) == "string" || (
  1001. typeof(names.nodeType) != "undefined" && names.nodeType > 0
  1002. ))
  1003. ) {
  1004. var kv = MochiKit.DOM.formContents(names);
  1005. names = kv[0];
  1006. values = kv[1];
  1007. } else if (arguments.length == 1) {
  1008. var o = names;
  1009. names = [];
  1010. values = [];
  1011. for (var k in o) {
  1012. var v = o[k];
  1013. if (typeof(v) == "function") {
  1014. continue;
  1015. } else if (typeof(v) != "string" &&
  1016. typeof(v.length) == "number") {
  1017. for (var i = 0; i < v.length; i++) {
  1018. names.push(k);
  1019. values.push(v[i]);
  1020. }
  1021. } else {
  1022. names.push(k);
  1023. values.push(v);
  1024. }
  1025. }
  1026. }
  1027. var rval = [];
  1028. var len = Math.min(names.length, values.length);
  1029. var urlEncode = MochiKit.Base.urlEncode;
  1030. for (var i = 0; i < len; i++) {
  1031. v = values[i];
  1032. if (typeof(v) != 'undefined' && v !== null) {
  1033. rval.push(urlEncode(names[i]) + "=" + urlEncode(v));
  1034. }
  1035. }
  1036. return rval.join("&");
  1037. },
  1038. /** @id MochiKit.Base.parseQueryString */
  1039. parseQueryString: function (encodedString, useArrays) {
  1040. // strip a leading '?' from the encoded string
  1041. var qstr = (encodedString[0] == "?") ? encodedString.substring(1) :
  1042. encodedString;
  1043. var pairs = qstr.replace(/\+/g, "%20").split(/(\&amp\;|\&\#38\;|\&#x26;|\&)/);
  1044. var o = {};
  1045. var decode;
  1046. if (typeof(decodeURIComponent) != "undefined") {
  1047. decode = decodeURIComponent;
  1048. } else {
  1049. decode = unescape;
  1050. }
  1051. if (useArrays) {
  1052. for (var i = 0; i < pairs.length; i++) {
  1053. var pair = pairs[i].split("=");
  1054. if (pair.length !== 2) {
  1055. continue;
  1056. }
  1057. var name = decode(pair[0]);
  1058. var arr = o[name];
  1059. if (!(arr instanceof Array)) {
  1060. arr = [];
  1061. o[name] = arr;
  1062. }
  1063. arr.push(decode(pair[1]));
  1064. }
  1065. } else {
  1066. for (i = 0; i < pairs.length; i++) {
  1067. pair = pairs[i].split("=");
  1068. if (pair.length !== 2) {
  1069. continue;
  1070. }
  1071. o[decode(pair[0])] = decode(pair[1]);
  1072. }
  1073. }
  1074. return o;
  1075. }
  1076. });
  1077. /** @id MochiKit.Base.AdapterRegistry */
  1078. MochiKit.Base.AdapterRegistry = function () {
  1079. this.pairs = [];
  1080. };
  1081. MochiKit.Base.AdapterRegistry.prototype = {
  1082. /** @id MochiKit.Base.AdapterRegistry.prototype.register */
  1083. register: function (name, check, wrap, /* optional */ override) {
  1084. if (override) {
  1085. this.pairs.unshift([name, check, wrap]);
  1086. } else {
  1087. this.pairs.push([name, check, wrap]);
  1088. }
  1089. },
  1090. /** @id MochiKit.Base.AdapterRegistry.prototype.match */
  1091. match: function (/* ... */) {
  1092. for (var i = 0; i < this.pairs.length; i++) {
  1093. var pair = this.pairs[i];
  1094. if (pair[1].apply(this, arguments)) {
  1095. return pair[2].apply(this, arguments);
  1096. }
  1097. }
  1098. throw MochiKit.Base.NotFound;
  1099. },
  1100. /** @id MochiKit.Base.AdapterRegistry.prototype.unregister */
  1101. unregister: function (name) {
  1102. for (var i = 0; i < this.pairs.length; i++) {
  1103. var pair = this.pairs[i];
  1104. if (pair[0] == name) {
  1105. this.pairs.splice(i, 1);
  1106. return true;
  1107. }
  1108. }
  1109. return false;
  1110. }
  1111. };
  1112. MochiKit.Base.EXPORT = [
  1113. "flattenArray",
  1114. "noop",
  1115. "camelize",
  1116. "counter",
  1117. "clone",
  1118. "extend",
  1119. "update",
  1120. "updatetree",
  1121. "setdefault",
  1122. "keys",
  1123. "values",
  1124. "items",
  1125. "NamedError",
  1126. "operator",
  1127. "forwardCall",
  1128. "itemgetter",
  1129. "typeMatcher",
  1130. "isCallable",
  1131. "isUndefined",
  1132. "isUndefinedOrNull",
  1133. "isNull",
  1134. "isEmpty",
  1135. "isNotEmpty",
  1136. "isArrayLike",
  1137. "isDateLike",
  1138. "xmap",
  1139. "map",
  1140. "xfilter",
  1141. "filter",
  1142. "methodcaller",
  1143. "compose",
  1144. "bind",
  1145. "bindMethods",
  1146. "NotFound",
  1147. "AdapterRegistry",
  1148. "registerComparator",
  1149. "compare",
  1150. "registerRepr",
  1151. "repr",
  1152. "objEqual",
  1153. "arrayEqual",
  1154. "concat",
  1155. "keyComparator",
  1156. "reverseKeyComparator",
  1157. "partial",
  1158. "merge",
  1159. "listMinMax",
  1160. "listMax",
  1161. "listMin",
  1162. "objMax",
  1163. "objMin",
  1164. "nodeWalk",
  1165. "zip",
  1166. "urlEncode",
  1167. "queryString",
  1168. "serializeJSON",
  1169. "registerJSON",
  1170. "evalJSON",
  1171. "parseQueryString",
  1172. "findValue",
  1173. "findIdentical",
  1174. "flattenArguments",
  1175. "method",
  1176. "average",
  1177. "mean",
  1178. "median"
  1179. ];
  1180. MochiKit.Base.EXPORT_OK = [
  1181. "nameFunctions",
  1182. "comparatorRegistry",
  1183. "reprRegistry",
  1184. "jsonRegistry",
  1185. "compareDateLike",
  1186. "compareArrayLike",
  1187. "reprArrayLike",
  1188. "reprString",
  1189. "reprNumber"
  1190. ];
  1191. MochiKit.Base._exportSymbols = function (globals, module) {
  1192. if (!MochiKit.__export__) {
  1193. return;
  1194. }
  1195. var all = module.EXPORT_TAGS[":all"];
  1196. for (var i = 0; i < all.length; i++) {
  1197. globals[all[i]] = module[all[i]];
  1198. }
  1199. };
  1200. MochiKit.Base.__new__ = function () {
  1201. // A singleton raised when no suitable adapter is found
  1202. var m = this;
  1203. // convenience
  1204. /** @id MochiKit.Base.noop */
  1205. m.noop = m.operator.identity;
  1206. // Backwards compat
  1207. m.forward = m.forwardCall;
  1208. m.find = m.findValue;
  1209. if (typeof(encodeURIComponent) != "undefined") {
  1210. /** @id MochiKit.Base.urlEncode */
  1211. m.urlEncode = function (unencoded) {
  1212. return encodeURIComponent(unencoded).replace(/\'/g, '%27');
  1213. };
  1214. } else {
  1215. m.urlEncode = function (unencoded) {
  1216. return escape(unencoded
  1217. ).replace(/\+/g, '%2B'
  1218. ).replace(/\"/g,'%22'
  1219. ).rval.replace(/\'/g, '%27');
  1220. };
  1221. }
  1222. /** @id MochiKit.Base.NamedError */
  1223. m.NamedError = function (name) {
  1224. this.message = name;
  1225. this.name = name;
  1226. };
  1227. m.NamedError.prototype = new Error();
  1228. m.update(m.NamedError.prototype, {
  1229. repr: function () {
  1230. if (this.message && this.message != this.name) {
  1231. return this.name + "(" + m.repr(this.message) + ")";
  1232. } else {
  1233. return this.name + "()";
  1234. }
  1235. },
  1236. toString: m.forwardCall("repr")
  1237. });
  1238. /** @id MochiKit.Base.NotFound */
  1239. m.NotFound = new m.NamedError("MochiKit.Base.NotFound");
  1240. /** @id MochiKit.Base.listMax */
  1241. m.listMax = m.partial(m.listMinMax, 1);
  1242. /** @id MochiKit.Base.listMin */
  1243. m.listMin = m.partial(m.listMinMax, -1);
  1244. /** @id MochiKit.Base.isCallable */
  1245. m.isCallable = m.typeMatcher('function');
  1246. /** @id MochiKit.Base.isUndefined */
  1247. m.isUndefined = m.typeMatcher('undefined');
  1248. /** @id MochiKit.Base.merge */
  1249. m.merge = m.partial(m.update, null);
  1250. /** @id MochiKit.Base.zip */
  1251. m.zip = m.partial(m.map, null);
  1252. /** @id MochiKit.Base.average */
  1253. m.average = m.mean;
  1254. /** @id MochiKit.Base.comparatorRegistry */
  1255. m.comparatorRegistry = new m.AdapterRegistry();
  1256. m.registerComparator("dateLike", m.isDateLike, m.compareDateLike);
  1257. m.registerComparator("arrayLike", m.isArrayLike, m.compareArrayLike);
  1258. /** @id MochiKit.Base.reprRegistry */
  1259. m.reprRegistry = new m.AdapterRegistry();
  1260. m.registerRepr("arrayLike", m.isArrayLike, m.reprArrayLike);
  1261. m.registerRepr("string", m.typeMatcher("string"), m.reprString);
  1262. m.registerRepr("numbers", m.typeMatcher("number", "boolean"), m.reprNumber);
  1263. /** @id MochiKit.Base.jsonRegistry */
  1264. m.jsonRegistry = new m.AdapterRegistry();
  1265. var all = m.concat(m.EXPORT, m.EXPORT_OK);
  1266. m.EXPORT_TAGS = {
  1267. ":common": m.concat(m.EXPORT_OK),
  1268. ":all": all
  1269. };
  1270. m.nameFunctions(this);
  1271. };
  1272. MochiKit.Base.__new__();
  1273. //
  1274. // XXX: Internet Explorer blows
  1275. //
  1276. if (MochiKit.__export__) {
  1277. compare = MochiKit.Base.compare;
  1278. compose = MochiKit.Base.compose;
  1279. serializeJSON = MochiKit.Base.serializeJSON;
  1280. }
  1281. MochiKit.Base._exportSymbols(this, MochiKit.Base);