PageRenderTime 62ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/50-HatenaStar.js

https://github.com/Cside/hatena-bookmark-googlechrome-extension
JavaScript | 3752 lines | 3696 code | 40 blank | 16 comment | 112 complexity | 9131b78df36628980be37af16fa1f1eb MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /* Ten */
  2. if (typeof(Ten) == 'undefined') {
  3. Ten = {};
  4. Ten.NAME = 'Ten';
  5. Ten.VERSION = 0.28;
  6. /* Ten.Class */
  7. Ten.Class = function(klass, prototype) {
  8. if (klass && klass.initialize) {
  9. var c = klass.initialize;
  10. } else if(klass && klass.base) {
  11. var c = function() { return klass.base[0].apply(this, arguments) };
  12. } else {
  13. var c = function() {};
  14. }
  15. c.prototype = prototype || {};
  16. c.prototype.constructor = c;
  17. Ten.Class.inherit(c, klass);
  18. if (klass && klass.base) {
  19. for (var i = 0; i < klass.base.length; i++) {
  20. var parent = klass.base[i];
  21. if (i == 0) {
  22. c.SUPER = parent;
  23. c.prototype.SUPER = parent.prototype;
  24. }
  25. Ten.Class.inherit(c, parent);
  26. Ten.Class.inherit(c.prototype, parent.prototype);
  27. }
  28. }
  29. return c;
  30. }
  31. Ten.Class.inherit = function(child,parent) {
  32. for (var prop in parent) {
  33. if (typeof(child[prop]) != 'undefined' || prop == 'initialize') continue;
  34. child[prop] = parent[prop];
  35. }
  36. }
  37. /*
  38. // Basic Ten Classes
  39. */
  40. /* Ten.Function */
  41. Ten.Function = {
  42. bind: function(f,o) {
  43. return function() {
  44. return f.apply(o, arguments);
  45. }
  46. },
  47. method: function(obj, method) {
  48. return Ten.Function.bind(obj[method], obj);
  49. }
  50. };
  51. /* Ten.Array */
  52. Ten.Array = {
  53. flatten: function(arr) {
  54. var ret = [];
  55. (function(arr) {
  56. for (var i = 0; i < arr.length; i++) {
  57. var o = arr[i];
  58. if (Ten.Array.isArray(o)) {
  59. arguments.callee(o);
  60. } else {
  61. ret.push(o);
  62. }
  63. }
  64. })(arr);
  65. return ret;
  66. },
  67. dup: function(arr) {
  68. var res = [];
  69. for (var i = 0; i < arr.length; i++) {
  70. res[i] = arr[i];
  71. }
  72. return res;
  73. },
  74. indexOf: function(arr,e) {
  75. for (var i = 0; i < arr.length; i++) {
  76. if (arr[i] == e) return i;
  77. }
  78. return -1;
  79. },
  80. isArray: function(o) {
  81. return (o instanceof Array ||
  82. (o && typeof(o.length) === 'number' && typeof(o) != 'string' && !o.nodeType));
  83. }
  84. };
  85. /* Ten.JSONP */
  86. Ten.JSONP = new Ten.Class({
  87. initialize: function(uri,obj,method) {
  88. if (Ten.JSONP.Callbacks.length) {
  89. setTimeout(function() {new Ten.JSONP(uri,obj,method)}, 500);
  90. return;
  91. }
  92. var del = uri.match(/\?/) ? '&' : '?';
  93. uri += del + 'callback=Ten.JSONP.callback';
  94. if (!uri.match(/timestamp=/)) {
  95. uri += '&' + encodeURI(new Date());
  96. }
  97. if (typeof(obj) == 'function' && typeof(method) == 'undefined') {
  98. obj = {callback: obj};
  99. method = 'callback';
  100. }
  101. if (obj && method) Ten.JSONP.addCallback(obj,method);
  102. this.script = document.createElement('script');
  103. this.script.src = uri;
  104. this.script.type = 'text/javascript';
  105. document.getElementsByTagName('head')[0].appendChild(this.script);
  106. },
  107. addCallback: function(obj,method) {
  108. Ten.JSONP.Callbacks.push({object: obj, method: method});
  109. },
  110. callback: function(args) {
  111. // alert('callback called');
  112. var cbs = Ten.JSONP.Callbacks;
  113. for (var i = 0; i < cbs.length; i++) {
  114. var cb = cbs[i];
  115. cb.object[cb.method].call(cb.object, args);
  116. }
  117. Ten.JSONP.Callbacks = [];
  118. },
  119. MaxBytes: 1800,
  120. Callbacks: []
  121. });
  122. /* Ten.XHR */
  123. Ten.XHR = new Ten.Class({
  124. initialize: function(uri,opts,obj,callPropertyName) {
  125. Ten.EventDispatcher.implementEventDispatcher(this);
  126. this.method = 'GET';
  127. if (!uri) return;
  128. if (!opts) opts = {};
  129. if (opts.method)
  130. this.method = opts.method;
  131. var self = this;
  132. this.addEventListener('complete', function() {
  133. if (!obj) return;
  134. if (typeof(obj) == 'function' && typeof(callPropertyName) == 'undefined') {
  135. obj.call(obj, self.request);
  136. } else {
  137. obj[callPropertyName].call(obj, self.request);
  138. }
  139. });
  140. this.load(uri, opts.data);
  141. },
  142. getXMLHttpRequest: function() {
  143. var xhr;
  144. var tryThese = [
  145. function () { return new XMLHttpRequest(); },
  146. function () { return new ActiveXObject('Msxml2.XMLHTTP'); },
  147. function () { return new ActiveXObject('Microsoft.XMLHTTP'); },
  148. function () { return new ActiveXObject('Msxml2.XMLHTTP.4.0'); },
  149. ];
  150. for (var i = 0; i < tryThese.length; i++) {
  151. var func = tryThese[i];
  152. try {
  153. xhr = func;
  154. return func();
  155. } catch (e) {
  156. //alert(e);
  157. }
  158. }
  159. return xhr;
  160. },
  161. makePostData: function(data) {
  162. var pairs = [];
  163. var regexp = /%20/g;
  164. for (var k in data) {
  165. if (!data[k]) continue;
  166. var v = data[k].toString();
  167. var pair = encodeURIComponent(k).replace(regexp,'+') + '=' +
  168. encodeURIComponent(v).replace(regexp,'+');
  169. pairs.push(pair);
  170. }
  171. return pairs.join('&');
  172. }
  173. },{
  174. load: function(url, params) {
  175. var req = Ten.XHR.getXMLHttpRequest();
  176. this.request = req;
  177. var self = this;
  178. req.onreadystatechange = function() {
  179. self.stateChangeHandler.call(self, req);
  180. };
  181. params = params ? Ten.XHR.makePostData(params) : null;
  182. req.open(this.method, url, true);
  183. if (this.method == 'POST')
  184. req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  185. req.send(params);
  186. },
  187. stateChangeHandler: function(req) {
  188. this.dispatchEvent('state_change');
  189. if (req.readyState == 4) {
  190. this.dispatchEvent('ready', req.status.toString());
  191. if (req.status >= 200 && req.status < 300) {
  192. this.dispatchEvent('complete', req);
  193. } else {
  194. this.dispatchEvent('error', req);
  195. }
  196. }
  197. }
  198. });
  199. /* Ten.Observer */
  200. Ten.Observer = new Ten.Class({
  201. initialize: function(element,event,obj,method) {
  202. var func = obj;
  203. if (typeof(method) == 'string') {
  204. func = obj[method];
  205. }
  206. this.element = element;
  207. this.event = event;
  208. this.listener = function(event) {
  209. return func.call(obj, new Ten.Event(event || window.event));
  210. }
  211. this.start();
  212. }
  213. },{
  214. stop: function() {
  215. if (this.element.removeEventListener) {
  216. this.element.removeEventListener(this.event,this.listener,false);
  217. } else if (this.element.detachEvent) {
  218. this.element.detachEvent(this.event,this.listener);
  219. }
  220. },
  221. start: function() {
  222. if (this.element.addEventListener) {
  223. if (this.event.indexOf('on') == 0) {
  224. this.event = this.event.substr(2);
  225. }
  226. this.element.addEventListener(this.event, this.listener, false);
  227. } else if (this.element.attachEvent) {
  228. this.element.attachEvent(this.event, this.listener);
  229. }
  230. }
  231. });
  232. /* Ten.Event */
  233. Ten.Event = new Ten.Class({
  234. initialize: function(e) {
  235. this.event = e;
  236. if (e) {
  237. this.target = e.target || e.srcElement;
  238. this.shiftKey = e.shiftKey;
  239. this.ctrlKey = e.ctrlKey;
  240. this.altKey = e.altKey;
  241. }
  242. },
  243. KeyMap: {
  244. 8:"backspace", 9:"tab", 13:"enter", 19:"pause", 27:"escape", 32:"space",
  245. 33:"pageup", 34:"pagedown", 35:"end", 36:"home", 37:"left", 38:"up",
  246. 39:"right", 40:"down", 44:"printscreen", 45:"insert", 46:"delete",
  247. 112:"f1", 113:"f2", 114:"f3", 115:"f4", 116:"f5", 117:"f6", 118:"f7",
  248. 119:"f8", 120:"f9", 121:"f10", 122:"f11", 123:"f12",
  249. 144:"numlock", 145:"scrolllock"
  250. }
  251. },{
  252. mousePosition: function() {
  253. if (!this.event.clientX) return null;
  254. return Ten.Geometry.getMousePosition(this.event);
  255. },
  256. isKey: function(name) {
  257. var ecode = this.event.keyCode;
  258. if (!ecode) return false;
  259. var ename = Ten.Event.KeyMap[ecode];
  260. if (!ename) return false;
  261. return (ename == name);
  262. },
  263. targetIsFormElements: function() {
  264. if (!this.target) return false;
  265. var T = (this.target.tagName || '').toUpperCase();
  266. return (T == 'INPUT' || T == 'SELECT' || T == 'OPTION' ||
  267. T == 'BUTTON' || T == 'TEXTAREA');
  268. },
  269. stop: function() {
  270. var e = this.event;
  271. if (e.stopPropagation) {
  272. e.stopPropagation();
  273. e.preventDefault();
  274. } else {
  275. e.cancelBubble = true;
  276. e.returnValue = false;
  277. }
  278. }
  279. });
  280. /* Ten.EventDispatcher */
  281. Ten.EventDispatcher = new Ten.Class({
  282. initialize: function() {
  283. this._eventListeners = {};
  284. },
  285. implementEventDispatcher: function(obj) {
  286. Ten.Class.inherit(obj, Ten.EventDispatcher.prototype);
  287. obj._eventListeners = {};
  288. }
  289. }, {
  290. hasEventListener: function(type) {
  291. return (this._eventListeners[type] instanceof Array && this._eventListeners[type].length > 0);
  292. },
  293. addEventListener: function(type, listener) {
  294. if (!this.hasEventListener(type)) {
  295. this._eventListeners[type] = [];
  296. }
  297. var listeners = this._eventListeners[type];
  298. for (var i = 0; i < listeners.length; i++) {
  299. if (listeners[i] == listener) {
  300. return;
  301. }
  302. }
  303. listeners.push(listener);
  304. },
  305. removeEventListener: function(type, listener) {
  306. if (this.hasEventListener(type)) {
  307. var listeners = this._eventListeners[type];
  308. for (var i = 0; i < listeners.length; i++) {
  309. if (listeners[i] == listener) {
  310. listeners.splice(i, 1);
  311. return;
  312. }
  313. }
  314. }
  315. },
  316. dispatchEvent: function(type, opt) {
  317. if (!this.hasEventListener(type)) return false;
  318. var listeners = this._eventListeners[type];
  319. for (var i = 0; i < listeners.length; i++) {
  320. listeners[i].call(this, opt);
  321. }
  322. return true; // preventDefault is not implemented
  323. }
  324. });
  325. /* Ten.DOM */
  326. Ten.DOM = new Ten.Class({
  327. createElementFromString : function (str, opts) {
  328. if (!opts) opts = { data: {} };
  329. if (!opts.data) opts.data = { };
  330. var t, cur = opts.parent || document.createDocumentFragment(), root, stack = [cur];
  331. while (str.length) {
  332. if (str.indexOf("<") == 0) {
  333. if ((t = str.match(/^\s*<(\/?[^\s>\/]+)([^>]+?)?(\/)?>/))) {
  334. var tag = t[1], attrs = t[2], isempty = !!t[3];
  335. if (tag.indexOf("/") == -1) {
  336. child = document.createElement(tag);
  337. if (attrs) attrs.replace(/([a-z]+)=(?:'([^']+)'|"([^"]+)")/gi,
  338. function (m, name, v1, v2) {
  339. var v = text(v1 || v2);
  340. if (name == "class") root && (root[v] = child), child.className = v;
  341. child.setAttribute(name, v);
  342. }
  343. );
  344. cur.appendChild(root ? child : (root = child));
  345. if (!isempty) {
  346. stack.push(cur);
  347. cur = child;
  348. }
  349. } else cur = stack.pop();
  350. } else throw("Parse Error: " + str);
  351. } else {
  352. if ((t = str.match(/^([^<]+)/))) cur.appendChild(document.createTextNode(text(t[0])));
  353. }
  354. str = str.substring(t[0].length);
  355. }
  356. function text (str) {
  357. return str
  358. .replace(/&(#(x)?)?([^;]+);/g, function (_, isNumRef, isHex, ref) {
  359. return isNumRef ? String.fromCharCode(parseInt(ref, isHex ? 16 : 10)):
  360. {"lt":"<","gt":"<","amp":"&"}[ref];
  361. })
  362. .replace(/#\{([^}]+)\}/g, function (_, name) {
  363. return (typeof(opts.data[name]) == "undefined") ? _ : opts.data[name];
  364. });
  365. }
  366. return root;
  367. },
  368. getElementsByTagAndClassName: function(tagName, className, parent) {
  369. if (typeof(parent) == 'undefined') parent = document;
  370. if (!tagName) return Ten.DOM.getElementsByClassName(className, parent);
  371. var children = parent.getElementsByTagName(tagName);
  372. if (className) {
  373. var elements = [];
  374. for (var i = 0; i < children.length; i++) {
  375. var child = children[i];
  376. if (Ten.DOM.hasClassName(child, className)) {
  377. elements.push(child);
  378. }
  379. }
  380. return elements;
  381. } else {
  382. return children;
  383. }
  384. },
  385. getElementsByClassName: function(className, parent) {
  386. if (typeof(parent) == 'undefined') parent = document;
  387. var ret = [];
  388. if (parent.getElementsByClassName) {
  389. var nodes = parent.getElementsByClassName(className);
  390. for (var i = 0 , len = nodes.length ; i < len ; i++ ) ret.push(nodes.item(i));
  391. return ret;
  392. } else {
  393. if (!className) return ret;
  394. (function(parent) {
  395. var elems = parent.childNodes;
  396. for (var i = 0; i < elems.length; i++) {
  397. var e = elems[i];
  398. if (Ten.DOM.hasClassName(e, className)) {
  399. ret.push(e);
  400. }
  401. arguments.callee(e);
  402. }
  403. })(parent);
  404. ret = Ten.Array.flatten(ret);
  405. return ret;
  406. }
  407. },
  408. hasClassName: function(element, className) {
  409. if (!element || !className) return false;
  410. var cname = element.className;
  411. if (!cname) return false;
  412. cname = ' ' + cname.toLowerCase() + ' ';
  413. className = ' ' + className.toLowerCase() + ' ';
  414. return (cname.indexOf(className) != -1);
  415. },
  416. addClassName: function(element, className) {
  417. if (Ten.DOM.hasClassName(element, className)) return;
  418. var c = element.className || '';
  419. c = c.length ? c + " " + className : className;
  420. element.className = c;
  421. },
  422. removeClassName: function(element, className) {
  423. if (!Ten.DOM.hasClassName(element, className)) return;
  424. var c = element.className;
  425. var classes = c.split(/\s+/);
  426. for (var i = 0; i < classes.length; i++) {
  427. if (classes[i] == className) {
  428. classes.splice(i,1);
  429. break;
  430. }
  431. }
  432. element.className = classes.join(' ');
  433. },
  434. removeEmptyTextNodes: function(element) {
  435. var nodes = element.childNodes;
  436. for (var i = 0; i < nodes.length; i++) {
  437. var node = nodes[i];
  438. if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) {
  439. node.parentNode.removeChild(node);
  440. }
  441. }
  442. },
  443. nextElement: function(elem) {
  444. do {
  445. elem = elem.nextSibling;
  446. } while (elem && elem.nodeType != 1);
  447. return elem;
  448. },
  449. prevElement: function(elem) {
  450. do {
  451. elem = elem.previousSibling;
  452. } while (elem && elem.nodeType != 1);
  453. return elem;
  454. },
  455. nextSiblingInSource: function(elem) {
  456. if (elem.childNodes && elem.childNodes.length) {
  457. return elem.childNodes[0];
  458. } else if (elem.nextSibling) {
  459. return elem.nextSibling;
  460. } else if (elem.parentNode && elem.parentNode.nextSibling) {
  461. return elem.parentNode.nextSibling;
  462. }
  463. return null;
  464. },
  465. insertBefore: function(node, ref) {
  466. ref.parentNode.insertBefore(node, ref);
  467. },
  468. insertAfter: function(node, ref) {
  469. if (ref.nextSibling) {
  470. ref.parentNode.insertBefore(node, ref.nextSibling);
  471. } else {
  472. ref.parentNode.appendChild(node);
  473. }
  474. },
  475. unshiftChild: function(elem, child) {
  476. if (elem.firstChild) {
  477. elem.insertBefore(child, elem.firstChild);
  478. } else {
  479. elem.appendChild(child);
  480. }
  481. },
  482. replaceNode: function(newNode, oldNode) {
  483. var parent = oldNode.parentNode;
  484. if (newNode && parent && parent.nodeType == 1) {
  485. parent.insertBefore(newNode, oldNode);
  486. parent.removeChild(oldNode);
  487. }
  488. },
  489. removeElement: function(elem) {
  490. if (!elem.parentNode) return;
  491. elem.parentNode.removeChild(elem);
  492. },
  493. removeAllChildren: function(node) {
  494. while (node.firstChild)
  495. node.removeChild(node.firstChild);
  496. },
  497. scrapeText: function(node) {
  498. if (typeof node.textContent == 'string') return node.textContent;
  499. if (typeof node.innerText == 'string') return node.innerText;
  500. var rval = [];
  501. (function (node) {
  502. var cn = node.childNodes;
  503. if (cn) {
  504. for (var i = 0; i < cn.length; i++) {
  505. arguments.callee.call(this, cn[i]);
  506. }
  507. }
  508. var nodeValue = node.nodeValue;
  509. if (typeof(nodeValue) == 'string') {
  510. rval.push(nodeValue);
  511. }
  512. })(node);
  513. return rval.join('');
  514. },
  515. getSelectedText: function() {
  516. if (window.getSelection)
  517. return '' + (window.getSelection() || '');
  518. else if (document.getSelection)
  519. return document.getSelection();
  520. else if (document.selection)
  521. return document.selection.createRange().text;
  522. else
  523. return '';
  524. },
  525. show: function(elem) {
  526. elem.style.display = 'block';
  527. },
  528. hide: function(elem) {
  529. elem.style.display = 'none';
  530. },
  531. addObserver: function() {
  532. var c = Ten.DOM;
  533. if (c.observer || c.loaded) return;
  534. c.observer = new Ten.Observer(window,'onload',c,'finishLoad');
  535. var ua = navigator.userAgent.toUpperCase();
  536. if (window.opera || ua.indexOf('FIREFOX') >= 0) {
  537. new Ten.Observer(window,'DOMContentLoaded',c,'finishLoad');
  538. } else if ((ua.indexOf('MSIE') >= 0 || ua.toLowerCase().indexOf('webkit') >= 0) && window == top) {
  539. var i = 0;
  540. (function() {
  541. if (i++ > 10000) return null;
  542. try {
  543. if (document.readyState != 'loaded' &&
  544. document.readyState != 'complete') {
  545. document.documentElement.doScroll('left');
  546. }
  547. } catch(error) {
  548. return setTimeout(arguments.callee, 13);
  549. }
  550. return c.finishLoad();
  551. })();
  552. }
  553. },
  554. finishLoad: function() {
  555. var c = Ten.DOM;
  556. if (!c.loaded) {
  557. c.dispatchEvent('DOMContentLoaded');
  558. c.dispatchEvent('onload'); // for backward compatibility
  559. c.loaded = true;
  560. c.observer.stop();
  561. c.observer = null;
  562. }
  563. },
  564. observer: null,
  565. loaded: false
  566. });
  567. Ten.EventDispatcher.implementEventDispatcher(Ten.DOM);
  568. Ten.DOM.addObserver();
  569. /* Ten.Element */
  570. Ten.Element = new Ten.Class({
  571. initialize: function(tagName, attributes) {
  572. var elem = document.createElement(tagName);
  573. for (var a in attributes) {
  574. if (a == 'style') {
  575. Ten.Style.applyStyle(elem, attributes[a])
  576. } else if (a == 'value' && tagName.toLowerCase() == 'input') {
  577. elem.setAttribute('value', attributes[a]);
  578. } else if (a.indexOf('on') == 0) {
  579. new Ten.Observer(elem, a, attributes[a]);
  580. } else {
  581. elem[a] = attributes[a];
  582. }
  583. }
  584. var children = Array.prototype.slice.call(arguments, 2);
  585. for (var i = 0; i < children.length; i++) {
  586. var child = children[i];
  587. if (typeof child == 'string')
  588. child = document.createTextNode(child);
  589. if (!child)
  590. continue;
  591. elem.appendChild(child);
  592. }
  593. Ten.Element.dispatchEvent('create',elem);
  594. return elem;
  595. }
  596. });
  597. Ten.EventDispatcher.implementEventDispatcher(Ten.Element);
  598. /* Ten.Cookie */
  599. Ten.Cookie = new Ten.Class({
  600. initialize: function(string) {
  601. this.cookies = this.constructor.parse(string);
  602. },
  603. parse: function(string) {
  604. var cookies = { };
  605. var segments = (string || document.cookie).split(/;\s*/);
  606. while (segments.length) {
  607. try {
  608. var segment = segments.shift().replace(/^\s*|\s*$/g, '');
  609. if (!segment.match(/^([^=]*)=(.*)$/))
  610. continue;
  611. var key = RegExp.$1, value = RegExp.$2;
  612. if (value.indexOf('&') != -1) {
  613. value = value.split(/&/);
  614. for (var i = 0; i < value.length; i++)
  615. value[i] = decodeURIComponent(value[i]);
  616. } else {
  617. value = decodeURIComponent(value);
  618. }
  619. key = decodeURIComponent(key);
  620. cookies[key] = value;
  621. } catch (e) {
  622. }
  623. }
  624. return cookies;
  625. }
  626. }, {
  627. set: function(key, value, option) {
  628. this.cookies[key] = value;
  629. if (value instanceof Array) {
  630. for (var i = 0; i < value.length; i++)
  631. value[i] = encodeURIComponent(value[i]);
  632. value = value.join('&');
  633. } else {
  634. value = encodeURIComponent(value);
  635. }
  636. var cookie = encodeURIComponent(key) + '=' + value;
  637. option = option || { };
  638. if (typeof option == 'string' || option instanceof Date) {
  639. // deprecated
  640. option = {
  641. expires: option
  642. };
  643. }
  644. if (!option.expires) {
  645. option.expires = this.defaultExpires;
  646. }
  647. if (/^\+?(\d+)([ymdh])$/.exec(option.expires)) {
  648. var count = parseInt(RegExp.$1);
  649. var field = ({ y: 'FullYear', m: 'Month', d: 'Date', h: 'Hours' })[RegExp.$2];
  650. var date = new Date;
  651. date['set' + field](date['get' + field]() + count);
  652. option.expires = date;
  653. }
  654. if (option.expires) {
  655. if (option.expires.toUTCString)
  656. option.expires = option.expires.toUTCString();
  657. cookie += '; expires=' + option.expires;
  658. }
  659. if (option.domain) {
  660. cookie += '; domain=' + option.domain;
  661. }
  662. if (option.path) {
  663. cookie += '; path=' + option.path;
  664. } else {
  665. cookie += '; path=/';
  666. }
  667. return document.cookie = cookie;
  668. },
  669. get: function(key) {
  670. return this.cookies[key];
  671. },
  672. has: function(key) {
  673. return (key in this.cookies) && !(key in Object.prototype);
  674. },
  675. clear: function(key) {
  676. this.set(key, '', new Date(0));
  677. delete this.cookies[key];
  678. }
  679. });
  680. /* Ten.Selector */
  681. Ten.Selector = new Ten.Class({
  682. initialize: function(selector) {
  683. this.selectorText = selector;
  684. var sels = selector.split(/\s+/);
  685. var child = null;
  686. var separator = null;
  687. for (var i = sels.length - 1; i >= 0; i--) {
  688. if (sels[i] == '>') {
  689. continue;
  690. } else if ((i > 0) && sels[i-1] == '>') {
  691. separator = sels[i-1];
  692. }
  693. var opt = separator ? {separator: separator} : null;
  694. separator = null;
  695. var node = new Ten.SelectorNode(sels[i],child,opt);
  696. child = node;
  697. }
  698. this.childNode = child;
  699. },
  700. getElementsBySelector: function(selector, parent) {
  701. sels = selector.split(/\s*,\s*/);
  702. var ret = [];
  703. for (var i = 0; i < sels.length; i++) {
  704. var sel = new Ten.Selector(sels[i]);
  705. ret = ret.concat(sel.getElements(parent));
  706. }
  707. ret = Ten.Array.flatten(ret);
  708. return ret;
  709. }
  710. },{
  711. getElements: function(parent) {
  712. if (typeof(parent) == 'undefined') {
  713. parent = document;
  714. }
  715. return this.childNode.getElements(parent);
  716. }
  717. });
  718. /* Ten.SelectorNode */
  719. Ten.SelectorNode = new Ten.Class({
  720. initialize: function(selector, child, opt) {
  721. if (selector) {
  722. selector = selector.replace(/\s/g,'');
  723. }
  724. this.option = opt;
  725. this.selectorText = selector;
  726. this.childNode = child;
  727. this.parseSelector();
  728. }
  729. },{
  730. getElementsBySelector: null, // will be overridden by parser
  731. parseSelector: function() {
  732. var f = 'getElementsBySelector';
  733. var t = this.selectorText;
  734. var match;
  735. if ((match = t.match(/^(.+)\:([\w-+()]+)$/))) {
  736. t = match[1];
  737. this.pseudoClass = match[2];
  738. }
  739. if (t.match(/^[\w-]+$/)) {
  740. this[f] = function(parent) {
  741. return parent.getElementsByTagName(t);
  742. };
  743. } else if ((match = t.match(/^([\w-]+)?#([\w-]+)$/))) {
  744. var tname = match[1];
  745. var idname = match[2];
  746. this[f] = function(parent) {
  747. var e = document.getElementById(idname);
  748. if (!tname ||
  749. e.tagName.toLowerCase() == tname.toLowerCase()) {
  750. return [e];
  751. } else {
  752. return [];
  753. }
  754. };
  755. } else if ((match = t.match(/^([\w-]+)?\.([\w-]+)/))) {
  756. var tname = match[1];
  757. var cname = match[2];
  758. this[f] = function(parent) {
  759. return Ten.DOM.getElementsByTagAndClassName(tname,cname,parent);
  760. };
  761. }
  762. if (this.option && this.option.separator) this.parseSeparator();
  763. if (this.pseudoClass) this.parsePseudoClass();
  764. },
  765. parsePseudoClass: function() {
  766. if (!this.pseudoClass) return;
  767. var pseudo = this.pseudoClass;
  768. var f = 'getElementsBySelector';
  769. var func = this[f];
  770. var match;
  771. if ((match = pseudo.match(/^(.+)-child(\((\d+)\))?$/))) {
  772. var type = match[1];
  773. var n = match[3];
  774. var index;
  775. if (type == 'first') {
  776. index = 0;
  777. } else if (type == 'last') {
  778. index = -1;
  779. } else if (type == 'nth' && n) {
  780. index = n - 1;
  781. }
  782. if (typeof index == 'number') {
  783. this[f] = function(parent) {
  784. var elems = func(parent);
  785. if (index < 0) index = elems.length + index;
  786. if (elems[index]) {
  787. return [elems[index]];
  788. } else {
  789. return [];
  790. }
  791. }
  792. }
  793. } else if ((match = pseudo.match(/^nth-child\((\d+)n\+(\d+)\)$/))) {
  794. var a = new Number(match[1]);
  795. var b = new Number(match[2]);
  796. this[f] = function(parent) {
  797. var elems = func(parent);
  798. var ret = [];
  799. for (var n = 0; n < 1000; n++) {
  800. var i = a * n + b - 1;
  801. if (i < 0) continue;
  802. if (typeof elems[i] == 'undefined') break;
  803. ret.push(elems[i]);
  804. }
  805. return ret;
  806. };
  807. }
  808. },
  809. parseSeparator: function() {
  810. if (!this.option) return;
  811. var sep = this.option.separator;
  812. if (!sep) return;
  813. var f = 'getElementsBySelector';
  814. var func = this[f];
  815. if (sep == '>') {
  816. this[f] = function(parent) {
  817. var elems = func(parent);
  818. var ret = [];
  819. for (var i = 0; i < elems.length; i++) {
  820. if (elems[i].parentNode == parent) ret.push(elems[i]);
  821. }
  822. return ret;
  823. }
  824. }
  825. },
  826. getElements: function(parent) {
  827. if (typeof this.getElementsBySelector != 'function') return null;
  828. var ret = [];
  829. var elems = this.getElementsBySelector(parent);
  830. if (elems && this.childNode) {
  831. for (var i = 0; i < elems.length; i++) {
  832. ret.push(this.childNode.getElements(elems[i]));
  833. }
  834. return ret;
  835. } else {
  836. return elems;
  837. }
  838. }
  839. });
  840. /* Ten._Selector */
  841. Ten._Selector = new Ten.Class({
  842. base : [Ten.Selector],
  843. initialize: function(selector) {
  844. this.selectorText = selector;
  845. var sels = selector.split(/\s+/);
  846. var child = null;
  847. var separator = null;
  848. for (var i = sels.length - 1; i >= 0; i--) {
  849. if (sels[i] == '>') {
  850. continue;
  851. } else if ((i > 0) && sels[i-1] == '>') {
  852. separator = sels[i-1];
  853. }
  854. var opt = separator ? {separator: separator} : null;
  855. separator = null;
  856. var node = new Ten._SelectorNode(sels[i],child,opt);
  857. child = node;
  858. }
  859. this.childNode = child;
  860. },
  861. getElementsBySelector: function(selector, parent) {
  862. sels = selector.split(/\s*,\s*/);
  863. var ret = [];
  864. for (var i = 0; i < sels.length; i++) {
  865. var sel = new Ten._Selector(sels[i]);
  866. ret = ret.concat(sel.getElements(parent));
  867. }
  868. ret = Ten._Selector._elementArrayFlatten(ret);
  869. if (selector.indexOf(',') >= 0) ret.sort(Ten._Selector._sortByElementOrder);
  870. return ret;
  871. },
  872. _sortByElementOrder: function(a, b) {
  873. var depthA = Ten._Selector._getNodeDepth(a);
  874. var depthB = Ten._Selector._getNodeDepth(b);
  875. if (depthA > depthB) for (var i = 0; i < (depthA - depthB) ; i++ ) a = a.parentNode;
  876. else if (depthA < depthB) for (var i = 0; i < (depthB - depthA) ; i++ ) b = b.parentNode;
  877. return Ten._Selector._getSiblingDepth(b) - Ten._Selector._getSiblingDepth(a);
  878. },
  879. _getNodeDepth: function(elem) {
  880. var i = 0;
  881. for (var n = elem ; n ; n = n.parentNode, i++){}
  882. return i;
  883. },
  884. _getSiblingDepth: function(elem) {
  885. var i = 0;
  886. for (var n = elem; n ; n = n.nextSibling, i++){}
  887. return i;
  888. },
  889. _elementArrayFlatten : function(arr) {
  890. var ret = [];
  891. (function(arr) {
  892. for (var i = 0; i < arr.length; i++) {
  893. var o = arr[i];
  894. if (o instanceof Array ||
  895. (o && typeof(o.length) === 'number'
  896. && typeof(o) != 'string'
  897. && !o.tagName)){
  898. arguments.callee(o);
  899. } else {
  900. ret.push(o);
  901. }
  902. }
  903. })(arr);
  904. return ret;
  905. }
  906. },{
  907. });
  908. /* Ten._SelectorNode */
  909. Ten._SelectorNode = new Ten.Class({
  910. base : [Ten.SelectorNode]
  911. },{
  912. parsePseudoClass: function() {
  913. if (!this.pseudoClass) return;
  914. var pseudo = this.pseudoClass;
  915. var f = 'getElementsBySelector';
  916. var func = this[f];
  917. var match;
  918. if ((match = pseudo.match(/^(.+)-child(\((\d+)\))?$/))) {
  919. var type = match[1];
  920. var n = match[3];
  921. var index;
  922. if (type == 'first') {
  923. index = 0;
  924. } else if (type == 'last') {
  925. index = -1;
  926. } else if (type == 'nth' && n) {
  927. index = n - 1;
  928. }
  929. if (typeof index == 'number') {
  930. this[f] = function(parent) {
  931. var elems = func(parent);
  932. var ret = [];
  933. for (var i = 0, len = elems.length ; i < len ; i++ ) {
  934. var children = elems[i].parentNode.childNodes;
  935. if((index >= 0 && children[index] == elems[i])
  936. || (index < 0 && children[children.length - 1] == elems[i]))
  937. ret.push(elems[i]);
  938. }
  939. return ret;
  940. }
  941. }
  942. } else if ((match = pseudo.match(/^nth-child\((\d+)n\+(\d+)\)$/))) {
  943. var a = new Number(match[1]);
  944. var b = new Number(match[2]);
  945. this[f] = function(parent) {
  946. var elems = func(parent);
  947. var tagName = elems[0].tagName;
  948. var parents = [];
  949. var checkArray = function (array , e) {
  950. for (var i = 0 , len = array.length; i < len ; i++) {
  951. if (array[i] == e) return;
  952. }
  953. array.push(e);
  954. }
  955. for (var i = 0, len = elems.length ; i < len ; i++ ){
  956. checkArray(parents, elems[i].parentNode);
  957. }
  958. var ret = [];
  959. for (var j = 0, len = parents.length ; j < len ; j++) {
  960. var children = parents[j].childNodes;
  961. for (var n = 0; n < children.length; n++) {
  962. var i = a * n + b - 1;
  963. if (i < 0) continue;
  964. if (children[i] && children[i].tagName == tagName) ret.push(children[i]);
  965. }
  966. }
  967. return ret;
  968. };
  969. }
  970. }
  971. });
  972. /* Ten.querySelector */
  973. Ten.querySelector;
  974. if (document.querySelector) {
  975. Ten.querySelector = function (selector, elem) {
  976. if (elem) return (elem.querySelector) ? elem.querySelector(selector) : null;
  977. return document.querySelector(selector);
  978. }
  979. } else {
  980. Ten.querySelector = function (selector, elem) {
  981. return Ten._Selector.getElementsBySelector(selector, elem)[0] || null;
  982. }
  983. }
  984. Ten.querySelectorAll;
  985. if (document.querySelectorAll) {
  986. Ten.querySelectorAll = function (selector, elem) {
  987. var elems ;
  988. try {
  989. if (elem) elems = (elem.querySelectorAll) ? elem.querySelectorAll(selector) : [];
  990. else elems = document.querySelectorAll(selector);
  991. } catch (e) {
  992. return (elem) ? Ten._Selector.getElementsBySelector(selector, elem) : Ten._Selector.getElementsBySelector(selector);
  993. }
  994. // return Array.prototype.slice.apply(elems);
  995. var ret = [];
  996. for (var i = 0 , len = elems.length ; i < len ; i++ ) ret.push(elems[i]);
  997. return ret;
  998. }
  999. Ten.DOM.orig_getElementsByTagAndClassName = Ten.DOM.getElementsByTagAndClassName;
  1000. Ten.DOM.getElementsByTagAndClassName = function(tag,klass,parent) {
  1001. var selector = tag || '';
  1002. if (klass) selector += '.' + klass;
  1003. if (!tag && !klass) return [];
  1004. try {
  1005. return Ten.querySelectorAll(selector, parent);
  1006. } catch(e) {
  1007. return Ten.DOM.orig_getElementsByTagAndClassName(tag, klass, parent);
  1008. }
  1009. }
  1010. } else {
  1011. Ten.querySelectorAll = Ten._Selector.getElementsBySelector;
  1012. }
  1013. /* Ten.Color */
  1014. Ten.Color = new Ten.Class({
  1015. initialize: function(r,g,b,a) {
  1016. if (typeof(a) == 'undefined' || a === null) a = 1;
  1017. this.r = r;
  1018. this.g = g;
  1019. this.b = b;
  1020. this.a = a;
  1021. },
  1022. parseFromString: function(str) {
  1023. var match;
  1024. if ((match = str.match(/^#([0-9a-f]{6}|[0-9a-f]{3})$/i))) {
  1025. var hexstr = match[1];
  1026. var w = hexstr.length / 3;
  1027. var rgb = [];
  1028. for (var i = 0; i < 3; i++) {
  1029. var hex = hexstr.substr(w * i, w);
  1030. if (hex.length == 1) hex += hex;
  1031. rgb.push(parseInt(hex,16));
  1032. }
  1033. return new Ten.Color(rgb[0],rgb[1],rgb[2]);
  1034. } else if ((match = str.match(/^rgb\(([\d.,\s]+)\)/))) {
  1035. var rdba = match[1].split(/[\s,]+/);
  1036. return new Ten.Color(rdba[0],rdba[1],rdba[2],rdba[3]);
  1037. }
  1038. return null;
  1039. },
  1040. parseFromElementColor: function(elem,prop) {
  1041. var ret;
  1042. for (var color; elem; elem = elem.parentNode) {
  1043. color = Ten.Style.getElementStyle(elem, prop);
  1044. if (typeof(color) != 'undefined' && color != 'transparent') {
  1045. ret = color;
  1046. break;
  1047. }
  1048. }
  1049. return ret ? Ten.Color.parseFromString(ret) : null;
  1050. }
  1051. },{
  1052. asRGBString: function() {
  1053. if (this.a < 1) {
  1054. return 'rgba(' + this.r + ',' + this.g + ',' + this.b +
  1055. ',' + this.a + ')';
  1056. } else {
  1057. return 'rgb(' + this.r + ',' + this.g + ',' + this.b + ')';
  1058. }
  1059. },
  1060. asHexString: function() {
  1061. var str = '#';
  1062. var cls = ['r','g','b'];
  1063. for (var i = 0; i < 3; i ++) {
  1064. var c = Math.round(this[cls[i]]);
  1065. var s = c.toString(16);
  1066. if (c < 16) s = '0' + s;
  1067. str += s;
  1068. }
  1069. return str;
  1070. },
  1071. overlay: function(color) {
  1072. if (color.a == 1) return color;
  1073. r = Math.round(color.r * color.a + this.r * this.a * (1 - color.a));
  1074. g = Math.round(color.g * color.a + this.g * this.a * (1 - color.a));
  1075. b = Math.round(color.b * color.a + this.b * this.a * (1 - color.a));
  1076. return new Ten.Color(r,g,b);
  1077. }
  1078. });
  1079. /* Ten.Style */
  1080. Ten.Style = new Ten.Class({
  1081. applyStyle: function(elem, style) {
  1082. var cssText = elem.style.cssText;
  1083. var estyle = elem.style;
  1084. for (var prop in style) {
  1085. estyle[prop] = style[prop];
  1086. }
  1087. return function() {
  1088. elem.style.cssText = cssText;
  1089. };
  1090. },
  1091. getGlobalRule: function(selector) {
  1092. selector = selector.toLowerCase();
  1093. if (Ten.Style._cache[selector]) {
  1094. return Ten.Style._cache[selector];
  1095. } else if (Ten.Style._cache[selector] === null) {
  1096. return null;
  1097. } else {
  1098. for (var i = document.styleSheets.length - 1; i >= 0; i--) {
  1099. var ss = document.styleSheets[i];
  1100. try {
  1101. var cssRules = ss.cssRules || ss.rules;
  1102. } catch(e) {
  1103. continue;
  1104. }
  1105. for (var j = cssRules.length - 1; j >= 0; j--) {
  1106. var rule = cssRules[j];
  1107. if (rule.selectorText &&
  1108. rule.selectorText.toLowerCase() == selector) {
  1109. Ten.Style._cache[selector] = rule;
  1110. return rule;
  1111. }
  1112. }
  1113. }
  1114. }
  1115. Ten.Style._cache[selector] = null;
  1116. return null;
  1117. },
  1118. getGlobalStyle: function(selector, prop) {
  1119. var rule = Ten.Style.getGlobalRule(selector);
  1120. if (rule && rule.style[prop]) {
  1121. return rule.style[prop];
  1122. } else {
  1123. return null;
  1124. }
  1125. },
  1126. getElementStyle: function(elem, prop) {
  1127. var style = elem.style ? elem.style[prop] : null;
  1128. if (!style) {
  1129. var dv = document.defaultView;
  1130. if (dv && dv.getComputedStyle) {
  1131. try {
  1132. var styles = dv.getComputedStyle(elem, null);
  1133. } catch(e) {
  1134. return null;
  1135. }
  1136. prop = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
  1137. style = styles ? styles.getPropertyValue(prop) : null;
  1138. } else if (elem.currentStyle) {
  1139. style = elem.currentStyle[prop];
  1140. }
  1141. }
  1142. return style;
  1143. },
  1144. scrapeURL: function(url) {
  1145. if (url.match(/url\((.+)\)/)) {
  1146. url = RegExp.$1;
  1147. url = url.replace(/[\'\"<>]/g, '');
  1148. return url;
  1149. }
  1150. return null;
  1151. },
  1152. _cache: {}
  1153. });
  1154. /* Ten.Geometry */
  1155. Ten.Geometry = new Ten.Class({
  1156. initialize: function() {
  1157. if (Ten.Geometry._initialized) return;
  1158. var func = Ten.Geometry._functions;
  1159. var de = document.documentElement;
  1160. if (window.innerWidth) {
  1161. func.getWindowWidth = function() { return window.innerWidth; }
  1162. func.getWindowHeight = function() { return window.innerHeight; }
  1163. func.getXScroll = function() { return window.pageXOffset; }
  1164. func.getYScroll = function() { return window.pageYOffset; }
  1165. } else if (de && de.clientWidth) {
  1166. func.getWindowWidth = function() { return de.clientWidth; }
  1167. func.getWindowHeight = function() { return de.clientHeight; }
  1168. func.getXScroll = function() { return de.scrollLeft; }
  1169. func.getYScroll = function() { return de.scrollTop; }
  1170. } else if (document.body.clientWidth) {
  1171. func.getWindowWidth = function() { return document.body.clientWidth; }
  1172. func.getWindowHeight = function() { return document.body.clientHeight; }
  1173. func.getXScroll = function() { return document.body.scrollLeft; }
  1174. func.getYScroll = function() { return document.body.scrollTop; }
  1175. }
  1176. if (window.opera) {
  1177. func.getDocumentHeight = function(w) { return parseInt(Ten.Style.getElementStyle(w.document.body, 'height')); }
  1178. func.getDocumentWidth = function(w) { return parseInt(Ten.Style.getElementStyle(w.document.body, 'width')); }
  1179. } else if (document.all) {
  1180. func.getDocumentHeight = function(w) { return w.document.body.scrollHeight; }
  1181. func.getDocumentWidth = function(w) { return w.document.body.scrollWidth; }
  1182. } else {
  1183. func.getDocumentHeight = function(w) { return w.document.body.offsetHeight || w.document.documentElement.scrollHeight; }
  1184. func.getDocumentWidth = function(w) { return w.document.body.offsetWidth || w.document.documentElement.scrollWidth; }
  1185. }
  1186. Ten.Geometry._initialized = true;
  1187. },
  1188. _initialized: false,
  1189. _functions: {},
  1190. getScroll: function() {
  1191. if (!Ten.Geometry._initialized) new Ten.Geometry;
  1192. return {
  1193. x: Ten.Geometry._functions.getXScroll(),
  1194. y: Ten.Geometry._functions.getYScroll()
  1195. };
  1196. },
  1197. getMousePosition: function(pos) {
  1198. // pos should have clientX, clientY same as mouse event
  1199. if (!Ten.Browser.isChrome && (navigator.userAgent.indexOf('Safari') > -1) &&
  1200. (navigator.userAgent.indexOf('Version/') < 0)) {
  1201. return {
  1202. x: pos.clientX,
  1203. y: pos.clientY
  1204. };
  1205. } else {
  1206. var scroll = Ten.Geometry.getScroll();
  1207. return {
  1208. x: pos.clientX + scroll.x,
  1209. y: pos.clientY + scroll.y
  1210. };
  1211. }
  1212. },
  1213. getElementPosition: function(e) {
  1214. var pos = {x:0, y:0};
  1215. if (document.documentElement.getBoundingClientRect) { // IE
  1216. var box = e.getBoundingClientRect();
  1217. var owner = e.ownerDocument;
  1218. pos.x = box.left + Math.max(owner.documentElement.scrollLeft, owner.body.scrollLeft) - 2;
  1219. pos.y = box.top + Math.max(owner.documentElement.scrollTop, owner.body.scrollTop) - 2
  1220. } else if(document.getBoxObjectFor) { //Firefox
  1221. pos.x = document.getBoxObjectFor(e).x;
  1222. pos.y = document.getBoxObjectFor(e).y;
  1223. } else {
  1224. do {
  1225. pos.x += e.offsetLeft;
  1226. pos.y += e.offsetTop;
  1227. } while ((e = e.offsetParent));
  1228. }
  1229. return pos;
  1230. },
  1231. getWindowSize: function() {
  1232. if (!Ten.Geometry._initialized) new Ten.Geometry;
  1233. return {
  1234. w: Ten.Geometry._functions.getWindowWidth(),
  1235. h: Ten.Geometry._functions.getWindowHeight()
  1236. };
  1237. },
  1238. getDocumentSize: function(w) {
  1239. if (!Ten.Geometry._initialized) new Ten.Geometry;
  1240. w = w || window;
  1241. return {
  1242. w: Ten.Geometry._functions.getDocumentWidth(w),
  1243. h: Ten.Geometry._functions.getDocumentHeight(w)
  1244. };
  1245. }
  1246. });
  1247. /* Ten.Position */
  1248. Ten.Position = new Ten.Class({
  1249. initialize: function(x,y) {
  1250. this.x = x;
  1251. this.y = y;
  1252. },
  1253. add: function(a,b) {
  1254. return new Ten.Position(a.x + b.x, a.y + b.y);
  1255. },
  1256. subtract: function(a,b) {
  1257. return new Ten.Position(a.x - b.x, a.y - b.y);
  1258. }
  1259. });
  1260. /* Ten.Logger */
  1261. Ten.Logger = new Ten.Class({
  1262. initialize: function(level, fallbackElement) {
  1263. this.level = level || 'info';
  1264. this.fallbackElement = fallbackElement;
  1265. this.logFunction = this.constructor.logFunction;
  1266. this.logs = [];
  1267. },
  1268. LEVEL: {
  1269. error: 0,
  1270. warn: 1,
  1271. info: 2,
  1272. debug: 3
  1273. },
  1274. logFunction: function(level, args) {
  1275. if (typeof console == 'undefined') {
  1276. try {
  1277. if (window.opera) {
  1278. // Opera
  1279. opera.postError(args.join(', '));
  1280. } else {
  1281. // fub
  1282. external.consoleLog(args.join(', '));
  1283. }
  1284. } catch (e) {
  1285. if (this.fallbackElement && this.fallbackElement.appendChild) {
  1286. this.fallbackElement.appendChild(document.createTextNode(level + ': ' + args.join(', ')));
  1287. this.fallbackElement.appendChild(document.createElement('br'));
  1288. }
  1289. }
  1290. } else if (typeof console[level] == 'function') {
  1291. if (navigator.userAgent.indexOf('Safari') >= 0) {
  1292. // Safari
  1293. console[level](args.join(', '));
  1294. } else {
  1295. // Firefox (with Firebug)
  1296. console[level].apply(console, args);
  1297. }
  1298. } else if (typeof console.log == 'function') {
  1299. console.log(args.join(', '));
  1300. }
  1301. }
  1302. }, {
  1303. logs: null,
  1304. log: function(level) {
  1305. var LEVEL = this.constructor.LEVEL;
  1306. if (!(level in LEVEL) || LEVEL[level] > LEVEL[this.level])
  1307. return;
  1308. var args = [];
  1309. for (var i = 1; i < arguments.length; i++) {
  1310. args.push(arguments[i]);
  1311. }
  1312. this.logs.push([level, args]);
  1313. this.logFunction(level, args);
  1314. },
  1315. error: function() {
  1316. return this._log('error', arguments);
  1317. },
  1318. warn: function() {
  1319. return this._log('warn', arguments);
  1320. },
  1321. info: function() {
  1322. return this._log('info', arguments);
  1323. },
  1324. debug: function() {
  1325. return this._log('debug', arguments);
  1326. },
  1327. _log: function(level, _arguments) {
  1328. var args = [level];
  1329. for (var i = 0; i < _arguments.length; i++)
  1330. args.push(_arguments[i]);
  1331. return this.log.apply(this, args);
  1332. }
  1333. });
  1334. /* Ten.Browser */
  1335. Ten.Browser = {
  1336. isIE: navigator.userAgent.indexOf('MSIE') != -1,
  1337. isMozilla: navigator.userAgent.indexOf('Mozilla') != -1 && !/compatible|WebKit/.test(navigator.userAgent),
  1338. isOpera: !!window.opera,
  1339. isSafari: navigator.userAgent.indexOf('WebKit') != -1,
  1340. isChrome : navigator.userAgent.indexOf('Chrome/') != -1,
  1341. isFirefox : navigator.userAgent.indexOf('Firefox/') != -1,
  1342. isSupportsXPath : !!document.evaluate,
  1343. version: {
  1344. string: (/(?:Firefox\/|MSIE |Opera\/|Chrome\/|Version\/)([\d.]+)/.exec(navigator.userAgent) || []).pop(),
  1345. valueOf: function() { return parseFloat(this.string) },
  1346. toString: function() { return this.string }
  1347. }
  1348. };
  1349. Ten.Deferred = (function () {
  1350. function Deferred () { return (this instanceof Deferred) ? this.init() : new Deferred() }
  1351. Deferred.ok = function (x) { return x };
  1352. Deferred.ng = function (x) { throw x };
  1353. Deferred.prototype = {
  1354. init : function () {
  1355. this._next = null;
  1356. this.callback = {
  1357. ok: Deferred.ok,
  1358. ng: Deferred.ng
  1359. };
  1360. return this;
  1361. },
  1362. next : function (fun) { return this._post("ok", fun) },
  1363. error : function (fun) { return this._post("ng", fun) },
  1364. call : function (val) { return this._fire("ok", val) },
  1365. fail : function (err) { return this._fire("ng", err) },
  1366. cancel : function () {
  1367. (this.canceller || function () {})();
  1368. return this.init();
  1369. },
  1370. _post : function (okng, fun) {
  1371. this._next = new Deferred();
  1372. this._next.callback[okng] = fun;
  1373. return this._next;
  1374. },
  1375. _fire : function (okng, value) {
  1376. var next = "ok";
  1377. try {
  1378. value = this.callback[okng].call(this, value);
  1379. } catch (e) {
  1380. next = "ng";
  1381. value = e;
  1382. if (Deferred.onerror) Deferred.onerror(e);
  1383. }
  1384. if (value instanceof Deferred) {
  1385. value._next = this._next;
  1386. } else {
  1387. if (this._next) this._next._fire(next, value);
  1388. }
  1389. return this;
  1390. }
  1391. };
  1392. Deferred.next_default = function (fun) {
  1393. var d = new Deferred();
  1394. var id = setTimeout(function () { d.call() }, 0);
  1395. d.canceller = function () { clearTimeout(id) };
  1396. if (fun) d.callback.ok = fun;
  1397. return d;
  1398. };
  1399. Deferred.next_faster_way_readystatechange = ((location.protocol == "http:") && !window.opera && /\bMSIE\b/.test(navigator.userAgent)) && function (fun) {
  1400. var d = new Deferred();
  1401. var t = new Date().getTime();
  1402. if (t - arguments.callee._prev_timeout_called < 150) {
  1403. var cancel = false;
  1404. var scrip

Large files files are truncated, but you can click here to view the full file