zepto.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. // Zepto.js
  2. // (c) 2010, 2011 Thomas Fuchs
  3. // Zepto.js may be freely distributed under the MIT license.
  4. (function(undefined){
  5. if (String.prototype.trim === undefined) // fix for iOS 3.2
  6. String.prototype.trim = function(){ return this.replace(/^\s+/, '').replace(/\s+$/, '') };
  7. // For iOS 3.x
  8. // from https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduce
  9. if (Array.prototype.reduce === undefined)
  10. Array.prototype.reduce = function(fun){
  11. if(this === void 0 || this === null) throw new TypeError();
  12. var t = Object(this), len = t.length >>> 0, k = 0, accumulator;
  13. if(typeof fun != 'function') throw new TypeError();
  14. if(len == 0 && arguments.length == 1) throw new TypeError();
  15. if(arguments.length >= 2)
  16. accumulator = arguments[1];
  17. else
  18. do{
  19. if(k in t){
  20. accumulator = t[k++];
  21. break;
  22. }
  23. if(++k >= len) throw new TypeError();
  24. } while (true);
  25. while (k < len){
  26. if(k in t) accumulator = fun.call(undefined, accumulator, t[k], k, t);
  27. k++;
  28. }
  29. return accumulator;
  30. };
  31. })();
  32. // Zepto.js
  33. // (c) 2010, 2011 Thomas Fuchs
  34. // Zepto.js may be freely distributed under the MIT license.
  35. var Zepto = (function() {
  36. var undefined, key, $$, classList, emptyArray = [], slice = emptyArray.slice,
  37. document = window.document,
  38. elementDisplay = {}, classCache = {},
  39. getComputedStyle = document.defaultView.getComputedStyle,
  40. cssNumber = { 'column-count': 1, 'columns': 1, 'font-weight': 1, 'line-height': 1,'opacity': 1, 'z-index': 1, 'zoom': 1 },
  41. fragmentRE = /^\s*<(\w+)[^>]*>/,
  42. elementTypes = [1, 9, 11],
  43. adjacencyOperators = ['prepend', 'after', 'before', 'append'],
  44. reverseAdjacencyOperators = ['append', 'prepend'],
  45. table = document.createElement('table'),
  46. tableRow = document.createElement('tr'),
  47. containers = {
  48. 'tr': document.createElement('tbody'),
  49. 'tbody': table, 'thead': table, 'tfoot': table,
  50. 'td': tableRow, 'th': tableRow,
  51. '*': document.createElement('div')
  52. };
  53. function isF(value) { return ({}).toString.call(value) == "[object Function]" }
  54. function isO(value) { return value instanceof Object }
  55. function isA(value) { return value instanceof Array }
  56. function likeArray(obj) { return typeof obj.length == 'number' }
  57. function compact(array) { return array.filter(function(item){ return item !== undefined && item !== null }) }
  58. function flatten(array) { return array.length > 0 ? [].concat.apply([], array) : array }
  59. function camelize(str) { return str.replace(/-+(.)?/g, function(match, chr){ return chr ? chr.toUpperCase() : '' }) }
  60. function dasherize(str){
  61. return str.replace(/::/g, '/')
  62. .replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2')
  63. .replace(/([a-z\d])([A-Z])/g, '$1_$2')
  64. .replace(/_/g, '-')
  65. .toLowerCase();
  66. }
  67. function uniq(array) { return array.filter(function(item,index,array){ return array.indexOf(item) == index }) }
  68. function classRE(name){
  69. return name in classCache ?
  70. classCache[name] : (classCache[name] = new RegExp('(^|\\s)' + name + '(\\s|$)'));
  71. }
  72. function maybeAddPx(name, value) { return (typeof value == "number" && !cssNumber[dasherize(name)]) ? value + "px" : value; }
  73. function defaultDisplay(nodeName) {
  74. var element, display;
  75. if (!elementDisplay[nodeName]) {
  76. element = document.createElement(nodeName);
  77. document.body.appendChild(element);
  78. display = getComputedStyle(element, '').getPropertyValue("display");
  79. element.parentNode.removeChild(element);
  80. display == "none" && (display = "block");
  81. elementDisplay[nodeName] = display;
  82. }
  83. return elementDisplay[nodeName];
  84. }
  85. function fragment(html, name) {
  86. if (name === undefined) fragmentRE.test(html) && RegExp.$1;
  87. if (!(name in containers)) name = '*';
  88. var container = containers[name];
  89. container.innerHTML = '' + html;
  90. return slice.call(container.childNodes);
  91. }
  92. function Z(dom, selector){
  93. dom = dom || emptyArray;
  94. dom.__proto__ = Z.prototype;
  95. dom.selector = selector || '';
  96. return dom;
  97. }
  98. function $(selector, context){
  99. if (!selector) return Z();
  100. if (context !== undefined) return $(context).find(selector);
  101. else if (isF(selector)) return $(document).ready(selector);
  102. else if (selector instanceof Z) return selector;
  103. else {
  104. var dom;
  105. if (isA(selector)) dom = compact(selector);
  106. else if (elementTypes.indexOf(selector.nodeType) >= 0 || selector === window)
  107. dom = [selector], selector = null;
  108. else if (fragmentRE.test(selector))
  109. dom = fragment(selector, RegExp.$1), selector = null;
  110. else if (selector.nodeType && selector.nodeType == 3) dom = [selector];
  111. else dom = $$(document, selector);
  112. return Z(dom, selector);
  113. }
  114. }
  115. $.extend = function(target){
  116. slice.call(arguments, 1).forEach(function(source) {
  117. for (key in source) target[key] = source[key];
  118. })
  119. return target;
  120. }
  121. $.qsa = $$ = function(element, selector){ return slice.call(element.querySelectorAll(selector)) }
  122. function filtered(nodes, selector){
  123. return selector === undefined ? $(nodes) : $(nodes).filter(selector);
  124. }
  125. function funcArg(context, arg, idx, payload){
  126. return isF(arg) ? arg.call(context, idx, payload) : arg;
  127. }
  128. $.isFunction = isF;
  129. $.isObject = isO;
  130. $.isArray = isA;
  131. $.map = function(elements, callback) {
  132. var value, values = [], i, key;
  133. if (likeArray(elements))
  134. for (i = 0; i < elements.length; i++) {
  135. value = callback(elements[i], i);
  136. if (value != null) values.push(value);
  137. }
  138. else
  139. for (key in elements) {
  140. value = callback(elements[key], key);
  141. if (value != null) values.push(value);
  142. }
  143. return flatten(values);
  144. }
  145. $.each = function(elements, callback) {
  146. var i, key;
  147. if (likeArray(elements))
  148. for(i = 0; i < elements.length; i++) {
  149. if(callback(i, elements[i]) === false) return elements;
  150. }
  151. else
  152. for(key in elements) {
  153. if(callback(key, elements[key]) === false) return elements;
  154. }
  155. return elements;
  156. }
  157. $.fn = {
  158. forEach: emptyArray.forEach,
  159. reduce: emptyArray.reduce,
  160. push: emptyArray.push,
  161. indexOf: emptyArray.indexOf,
  162. concat: emptyArray.concat,
  163. map: function(fn){
  164. return $.map(this, function(el, i){ return fn.call(el, i, el) });
  165. },
  166. slice: function(){
  167. return $(slice.apply(this, arguments));
  168. },
  169. ready: function(callback){
  170. if (document.readyState == 'complete' || document.readyState == 'loaded') callback();
  171. document.addEventListener('DOMContentLoaded', callback, false);
  172. return this;
  173. },
  174. get: function(idx){ return idx === undefined ? this : this[idx] },
  175. size: function(){ return this.length },
  176. remove: function () {
  177. return this.each(function () {
  178. if (this.parentNode != null) {
  179. this.parentNode.removeChild(this);
  180. }
  181. });
  182. },
  183. each: function(callback){
  184. this.forEach(function(el, idx){ callback.call(el, idx, el) });
  185. return this;
  186. },
  187. filter: function(selector){
  188. return $([].filter.call(this, function(element){
  189. return $$(element.parentNode, selector).indexOf(element) >= 0;
  190. }));
  191. },
  192. end: function(){
  193. return this.prevObject || $();
  194. },
  195. add:function(selector,context){
  196. return $(uniq(this.concat($(selector,context))));
  197. },
  198. is: function(selector){
  199. return this.length > 0 && $(this[0]).filter(selector).length > 0;
  200. },
  201. not: function(selector){
  202. var nodes=[];
  203. if (isF(selector) && selector.call !== undefined)
  204. this.each(function(idx){
  205. if (!selector.call(this,idx)) nodes.push(this);
  206. });
  207. else {
  208. var excludes = typeof selector == 'string' ? this.filter(selector) :
  209. (likeArray(selector) && isF(selector.item)) ? slice.call(selector) : $(selector);
  210. this.forEach(function(el){
  211. if (excludes.indexOf(el) < 0) nodes.push(el);
  212. });
  213. }
  214. return $(nodes);
  215. },
  216. eq: function(idx){
  217. return idx === -1 ? this.slice(idx) : this.slice(idx, + idx + 1);
  218. },
  219. first: function(){ return $(this[0]) },
  220. last: function(){ return $(this[this.length - 1]) },
  221. find: function(selector){
  222. var result;
  223. if (this.length == 1) result = $$(this[0], selector);
  224. else result = this.map(function(){ return $$(this, selector) });
  225. return $(result);
  226. },
  227. closest: function(selector, context){
  228. var node = this[0], nodes = $$(context !== undefined ? context : document, selector);
  229. if (nodes.length === 0) node = null;
  230. while(node && node !== document && nodes.indexOf(node) < 0) node = node.parentNode;
  231. return $(node !== document && node);
  232. },
  233. parents: function(selector){
  234. var ancestors = [], nodes = this;
  235. while (nodes.length > 0)
  236. nodes = $.map(nodes, function(node){
  237. if ((node = node.parentNode) && node !== document && ancestors.indexOf(node) < 0) {
  238. ancestors.push(node);
  239. return node;
  240. }
  241. });
  242. return filtered(ancestors, selector);
  243. },
  244. parent: function(selector){
  245. return filtered(uniq(this.pluck('parentNode')), selector);
  246. },
  247. children: function(selector){
  248. return filtered(this.map(function(){ return slice.call(this.children) }), selector);
  249. },
  250. siblings: function(selector){
  251. return filtered(this.map(function(i, el){
  252. return slice.call(el.parentNode.children).filter(function(child){ return child!==el });
  253. }), selector);
  254. },
  255. empty: function(){ return this.each(function(){ this.innerHTML = '' }) },
  256. pluck: function(property){ return this.map(function(){ return this[property] }) },
  257. show: function(){
  258. return this.each(function() {
  259. this.style.display == "none" && (this.style.display = null);
  260. if (getComputedStyle(this, '').getPropertyValue("display") == "none") {
  261. this.style.display = defaultDisplay(this.nodeName)
  262. }
  263. })
  264. },
  265. replaceWith: function(newContent) {
  266. return this.each(function() {
  267. var par=this.parentNode,next=this.nextSibling;
  268. $(this).remove();
  269. next ? $(next).before(newContent) : $(par).append(newContent);
  270. });
  271. },
  272. wrap: function(newContent) {
  273. return this.each(function() {
  274. $(this).wrapAll($(newContent)[0].cloneNode(false));
  275. });
  276. },
  277. wrapAll: function(newContent) {
  278. if (this[0]) {
  279. $(this[0]).before(newContent = $(newContent));
  280. newContent.append(this);
  281. }
  282. return this;
  283. },
  284. unwrap: function(){
  285. this.parent().each(function(){
  286. $(this).replaceWith($(this).children());
  287. });
  288. return this;
  289. },
  290. hide: function(){
  291. return this.css("display", "none")
  292. },
  293. toggle: function(setting){
  294. return (setting === undefined ? this.css("display") == "none" : setting) ? this.show() : this.hide();
  295. },
  296. prev: function(){ return $(this.pluck('previousElementSibling')) },
  297. next: function(){ return $(this.pluck('nextElementSibling')) },
  298. html: function(html){
  299. return html === undefined ?
  300. (this.length > 0 ? this[0].innerHTML : null) :
  301. this.each(function (idx) {
  302. var originHtml = this.innerHTML;
  303. $(this).empty().append( funcArg(this, html, idx, originHtml) );
  304. });
  305. },
  306. text: function(text){
  307. return text === undefined ?
  308. (this.length > 0 ? this[0].textContent : null) :
  309. this.each(function(){ this.textContent = text });
  310. },
  311. attr: function(name, value){
  312. return (typeof name == 'string' && value === undefined) ?
  313. (this.length > 0 && this[0].nodeName == 'INPUT' && this[0].type == 'text' && name == 'value') ? (this.val()) :
  314. (this.length > 0 ? this[0].getAttribute(name) || (name in this[0] ? this[0][name] : undefined) : undefined) :
  315. this.each(function(idx){
  316. if (isO(name)) for (key in name) this.setAttribute(key, name[key])
  317. else this.setAttribute(name, funcArg(this, value, idx, this.getAttribute(name)));
  318. });
  319. },
  320. removeAttr: function(name) {
  321. return this.each(function() { this.removeAttribute(name); });
  322. },
  323. data: function(name, value){
  324. return this.attr('data-' + name, value);
  325. },
  326. val: function(value){
  327. return (value === undefined) ?
  328. (this.length > 0 ? this[0].value : null) :
  329. this.each(function(){
  330. this.value = value;
  331. });
  332. },
  333. offset: function(){
  334. if(this.length==0) return null;
  335. var obj = this[0].getBoundingClientRect();
  336. return {
  337. left: obj.left + document.body.scrollLeft,
  338. top: obj.top + document.body.scrollTop,
  339. width: obj.width,
  340. height: obj.height
  341. };
  342. },
  343. css: function(property, value){
  344. if (value === undefined && typeof property == 'string')
  345. return this[0].style[camelize(property)] || getComputedStyle(this[0], '').getPropertyValue(property);
  346. var css = '';
  347. for (key in property) css += dasherize(key) + ':' + maybeAddPx(key, property[key]) + ';';
  348. if (typeof property == 'string') css = dasherize(property) + ":" + maybeAddPx(property, value);
  349. return this.each(function() { this.style.cssText += ';' + css });
  350. },
  351. index: function(element){
  352. return element ? this.indexOf($(element)[0]) : this.parent().children().indexOf(this[0]);
  353. },
  354. hasClass: function(name){
  355. if (this.length < 1) return false;
  356. else return classRE(name).test(this[0].className);
  357. },
  358. addClass: function(name){
  359. return this.each(function(idx) {
  360. classList = [];
  361. var cls = this.className, newName = funcArg(this, name, idx, cls);
  362. newName.split(/\s+/g).forEach(function(klass) {
  363. if (!$(this).hasClass(klass)) {
  364. classList.push(klass)
  365. }
  366. }, this);
  367. classList.length && (this.className += (cls ? " " : "") + classList.join(" "))
  368. });
  369. },
  370. removeClass: function(name){
  371. return this.each(function(idx) {
  372. if(name === undefined)
  373. return this.className = '';
  374. classList = this.className;
  375. funcArg(this, name, idx, classList).split(/\s+/g).forEach(function(klass) {
  376. classList = classList.replace(classRE(klass), " ")
  377. });
  378. this.className = classList.trim()
  379. });
  380. },
  381. toggleClass: function(name, when){
  382. return this.each(function(idx){
  383. var cls = this.className, newName = funcArg(this, name, idx, cls);
  384. ((when !== undefined && !when) || $(this).hasClass(newName)) ?
  385. $(this).removeClass(newName) : $(this).addClass(newName)
  386. });
  387. }
  388. };
  389. 'filter,add,not,eq,first,last,find,closest,parents,parent,children,siblings'.split(',').forEach(function(property){
  390. var fn = $.fn[property];
  391. $.fn[property] = function() {
  392. var ret = fn.apply(this, arguments);
  393. ret.prevObject = this;
  394. return ret;
  395. }
  396. });
  397. ['width', 'height'].forEach(function(property){
  398. $.fn[property] = function(value) {
  399. var offset;
  400. if (value === undefined) { return (offset = this.offset()) && offset[property] }
  401. else return this.css(property, value);
  402. }
  403. });
  404. function insert(operator, target, node) {
  405. var parent = (!operator || operator == 3) ? target : target.parentNode;
  406. parent.insertBefore(node,
  407. !operator ? parent.firstChild : // prepend
  408. operator == 1 ? target.nextSibling : // after
  409. operator == 2 ? target : // before
  410. null); // append
  411. }
  412. function traverseNode (node, fun) {
  413. fun(node);
  414. for (key in node.childNodes) {
  415. traverseNode(node.childNodes[key], fun);
  416. }
  417. }
  418. adjacencyOperators.forEach(function(key, operator) {
  419. $.fn[key] = function(html){
  420. var nodes = typeof(html) == 'object' ? html : fragment(html);
  421. if (!('length' in nodes)) nodes = [nodes];
  422. if (nodes.length < 1) return this;
  423. var size = this.length, copyByClone = size > 1, inReverse = operator < 2;
  424. return this.each(function(index, target){
  425. for (var i = 0; i < nodes.length; i++) {
  426. var node = nodes[inReverse ? nodes.length-i-1 : i];
  427. traverseNode(node, function (node) {
  428. if (node.nodeName != null && node.nodeName.toUpperCase() === 'SCRIPT') {
  429. window['eval'].call(window, node.innerHTML);
  430. }
  431. });
  432. if (copyByClone && index < size - 1) node = node.cloneNode(true);
  433. insert(operator, target, node);
  434. }
  435. });
  436. };
  437. });
  438. reverseAdjacencyOperators.forEach(function(key) {
  439. $.fn[key+'To'] = function(html){
  440. if (typeof(html) != 'object') html = $(html);
  441. html[key](this);
  442. return this;
  443. };
  444. });
  445. Z.prototype = $.fn;
  446. return $;
  447. })();
  448. '$' in window || (window.$ = Zepto);
  449. // Zepto.js
  450. // (c) 2010, 2011 Thomas Fuchs
  451. // Zepto.js may be freely distributed under the MIT license.
  452. (function($){
  453. var $$ = $.qsa, handlers = {}, _zid = 1;
  454. function zid(element) {
  455. return element._zid || (element._zid = _zid++);
  456. }
  457. function findHandlers(element, event, fn, selector) {
  458. event = parse(event);
  459. if (event.ns) var matcher = matcherFor(event.ns);
  460. return (handlers[zid(element)] || []).filter(function(handler) {
  461. return handler
  462. && (!event.e || handler.e == event.e)
  463. && (!event.ns || matcher.test(handler.ns))
  464. && (!fn || handler.fn == fn)
  465. && (!selector || handler.sel == selector);
  466. });
  467. }
  468. function parse(event) {
  469. var parts = ('' + event).split('.');
  470. return {e: parts[0], ns: parts.slice(1).sort().join(' ')};
  471. }
  472. function matcherFor(ns) {
  473. return new RegExp('(?:^| )' + ns.replace(' ', ' .* ?') + '(?: |$)');
  474. }
  475. function add(element, events, fn, selector, delegate){
  476. var id = zid(element), set = (handlers[id] || (handlers[id] = []));
  477. events.split(/\s/).forEach(function(event){
  478. var callback = delegate || fn;
  479. var proxyfn = function (event) {
  480. var result = callback.apply(element, [event].concat(event.data));
  481. if (result === false) {
  482. event.preventDefault();
  483. }
  484. return result;
  485. };
  486. var handler = $.extend(parse(event), {fn: fn, proxy: proxyfn, sel: selector, del: delegate, i: set.length});
  487. set.push(handler);
  488. element.addEventListener(handler.e, proxyfn, false);
  489. });
  490. }
  491. function remove(element, events, fn, selector){
  492. var id = zid(element);
  493. (events || '').split(/\s/).forEach(function(event){
  494. findHandlers(element, event, fn, selector).forEach(function(handler){
  495. delete handlers[id][handler.i];
  496. element.removeEventListener(handler.e, handler.proxy, false);
  497. });
  498. });
  499. }
  500. $.event = { add: add, remove: remove }
  501. $.fn.bind = function(event, callback){
  502. return this.each(function(){
  503. add(this, event, callback);
  504. });
  505. };
  506. $.fn.unbind = function(event, callback){
  507. return this.each(function(){
  508. remove(this, event, callback);
  509. });
  510. };
  511. $.fn.one = function(event, callback){
  512. return this.each(function(){
  513. var self = this;
  514. add(this, event, function wrapper(evt){
  515. callback.call(self, evt);
  516. remove(self, event, arguments.callee);
  517. });
  518. });
  519. };
  520. var returnTrue = function(){return true},
  521. returnFalse = function(){return false},
  522. eventMethods = {
  523. preventDefault: 'isDefaultPrevented',
  524. stopImmediatePropagation: 'isImmediatePropagationStopped',
  525. stopPropagation: 'isPropagationStopped'
  526. };
  527. function createProxy(event) {
  528. var proxy = $.extend({originalEvent: event}, event);
  529. $.each(eventMethods, function(name, predicate) {
  530. proxy[name] = function(){
  531. this[predicate] = returnTrue;
  532. return event[name].apply(event, arguments);
  533. };
  534. proxy[predicate] = returnFalse;
  535. })
  536. return proxy;
  537. }
  538. $.fn.delegate = function(selector, event, callback){
  539. return this.each(function(i, element){
  540. add(element, event, callback, selector, function(e, data){
  541. var target = e.target, nodes = $$(element, selector);
  542. while (target && nodes.indexOf(target) < 0) target = target.parentNode;
  543. if (target && !(target === element) && !(target === document)) {
  544. callback.call(target, $.extend(createProxy(e), {
  545. currentTarget: target, liveFired: element
  546. }), data);
  547. }
  548. });
  549. });
  550. };
  551. $.fn.undelegate = function(selector, event, callback){
  552. return this.each(function(){
  553. remove(this, event, callback, selector);
  554. });
  555. }
  556. $.fn.live = function(event, callback){
  557. $(document.body).delegate(this.selector, event, callback);
  558. return this;
  559. };
  560. $.fn.die = function(event, callback){
  561. $(document.body).undelegate(this.selector, event, callback);
  562. return this;
  563. };
  564. $.fn.trigger = function(event, data){
  565. if (typeof event == 'string') event = $.Event(event);
  566. event.data = data;
  567. return this.each(function(){ this.dispatchEvent(event) });
  568. };
  569. // triggers event handlers on current element just as if an event occurred,
  570. // doesn't trigger an actual event, doesn't bubble
  571. $.fn.triggerHandler = function(event, data){
  572. var e, result;
  573. this.each(function(i, element){
  574. e = createProxy(typeof event == 'string' ? $.Event(event) : event);
  575. e.data = data; e.target = element;
  576. $.each(findHandlers(element, event.type || event), function(i, handler){
  577. result = handler.proxy(e);
  578. if (e.isImmediatePropagationStopped()) return false;
  579. });
  580. });
  581. return result;
  582. };
  583. // shortcut methods for `.bind(event, fn)` for each event type
  584. ('focusin focusout load resize scroll unload click dblclick '+
  585. 'mousedown mouseup mousemove mouseover mouseout '+
  586. 'change select keydown keypress keyup error').split(' ').forEach(function(event) {
  587. $.fn[event] = function(callback){ return this.bind(event, callback) };
  588. });
  589. ['focus', 'blur'].forEach(function(name) {
  590. $.fn[name] = function(callback) {
  591. if (callback) this.bind(name, callback);
  592. else if (this.length) try { this.get(0)[name]() } catch(e){};
  593. return this;
  594. };
  595. });
  596. $.Event = function(type, props) {
  597. var event = document.createEvent('Events');
  598. if (props) $.extend(event, props);
  599. event.initEvent(type, !(props && props.bubbles === false), true);
  600. return event;
  601. };
  602. })(Zepto);
  603. // Zepto.js
  604. // (c) 2010, 2011 Thomas Fuchs
  605. // Zepto.js may be freely distributed under the MIT license.
  606. (function($){
  607. function detect(ua){
  608. var ua = ua, os = {},
  609. android = ua.match(/(Android)\s+([\d.]+)/),
  610. ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
  611. iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
  612. webos = ua.match(/(webOS|hpwOS)[\s\/]([\d.]+)/),
  613. touchpad = webos && ua.match(/TouchPad/),
  614. blackberry = ua.match(/(BlackBerry).*Version\/([\d.]+)/);
  615. if (android) os.android = true, os.version = android[2];
  616. if (iphone) os.ios = true, os.version = iphone[2].replace(/_/g, '.'), os.iphone = true;
  617. if (ipad) os.ios = true, os.version = ipad[2].replace(/_/g, '.'), os.ipad = true;
  618. if (webos) os.webos = true, os.version = webos[2];
  619. if (touchpad) os.touchpad = true;
  620. if (blackberry) os.blackberry = true, os.version = blackberry[2];
  621. return os;
  622. }
  623. // ### $.os
  624. //
  625. // Object contains information about running environmental
  626. //
  627. // *Example:*
  628. //
  629. // $.os.ios // => true if running on Apple iOS
  630. // $.os.android // => true if running on Android
  631. // $.os.webos // => true if running on HP/Palm WebOS
  632. // $.os.touchpad // => true if running on a HP TouchPad
  633. // $.os.version // => string with version number,
  634. // "4.0", "3.1.1", "2.1", etc.
  635. // $.os.iphone // => true if running on iPhone
  636. // $.os.ipad // => true if running on iPad
  637. // $.os.blackberry // => true if running on BlackBerry
  638. //
  639. $.os = detect(navigator.userAgent);
  640. $.__detect = detect;
  641. var v = navigator.userAgent.match(/WebKit\/([\d.]+)/);
  642. $.browser = v ? { webkit: true, version: v[1] } : { webkit: false };
  643. })(Zepto);
  644. // Zepto.js
  645. // (c) 2010, 2011 Thomas Fuchs
  646. // Zepto.js may be freely distributed under the MIT license.
  647. (function($, undefined){
  648. var supportedTransforms = [
  649. 'scale', 'scaleX', 'scaleY',
  650. 'translate', 'translateX', 'translateY', 'translate3d',
  651. 'skew', 'skewX', 'skewY',
  652. 'rotate', 'rotateX', 'rotateY', 'rotateZ', 'rotate3d',
  653. 'matrix'
  654. ];
  655. $.fn.anim = function(properties, duration, ease, callback){
  656. var transforms = [], cssProperties = {}, key, that = this, wrappedCallback;
  657. for (key in properties)
  658. if (supportedTransforms.indexOf(key)>=0)
  659. transforms.push(key + '(' + properties[key] + ')');
  660. else
  661. cssProperties[key] = properties[key];
  662. wrappedCallback = function(){
  663. that.css({'-webkit-transition':'none'});
  664. callback && callback();
  665. }
  666. if (duration > 0)
  667. this.one('webkitTransitionEnd', wrappedCallback);
  668. else
  669. setTimeout(wrappedCallback, 0);
  670. if (transforms.length > 0) {
  671. cssProperties['-webkit-transform'] = transforms.join(' ')
  672. }
  673. cssProperties['-webkit-transition'] = 'all ' + (duration !== undefined ? duration : 0.5) + 's ' + (ease || '');
  674. setTimeout(function () {
  675. that.css(cssProperties);
  676. }, 0);
  677. return this;
  678. }
  679. })(Zepto);
  680. // Zepto.js
  681. // (c) 2010, 2011 Thomas Fuchs
  682. // Zepto.js may be freely distributed under the MIT license.
  683. (function($){
  684. var jsonpID = 0,
  685. isObject = $.isObject,
  686. key;
  687. // Empty function, used as default callback
  688. function empty() {}
  689. // ### $.ajaxJSONP
  690. //
  691. // Load JSON from a server in a different domain (JSONP)
  692. //
  693. // *Arguments:*
  694. //
  695. // options — object that configure the request,
  696. // see avaliable options below
  697. //
  698. // *Avaliable options:*
  699. //
  700. // url — url to which the request is sent
  701. // success — callback that is executed if the request succeeds
  702. //
  703. // *Example:*
  704. //
  705. // $.ajaxJSONP({
  706. // url: 'http://example.com/projects?callback=?',
  707. // success: function (data) {
  708. // projects.push(json);
  709. // }
  710. // });
  711. //
  712. $.ajaxJSONP = function(options){
  713. var jsonpString = 'jsonp' + ++jsonpID,
  714. script = document.createElement('script');
  715. window[jsonpString] = function(data){
  716. options.success(data);
  717. delete window[jsonpString];
  718. };
  719. script.src = options.url.replace(/=\?/, '=' + jsonpString);
  720. $('head').append(script);
  721. };
  722. // ### $.ajaxSettings
  723. //
  724. // AJAX settings
  725. //
  726. $.ajaxSettings = {
  727. // Default type of request
  728. type: 'GET',
  729. // Callback that is executed before request
  730. beforeSend: empty,
  731. // Callback that is executed if the request succeeds
  732. success: empty,
  733. // Callback that is executed the the server drops error
  734. error: empty,
  735. // Callback that is executed on request complete (both: error and success)
  736. complete: empty,
  737. // MIME types mapping
  738. accepts: {
  739. script: 'text/javascript, application/javascript',
  740. json: 'application/json',
  741. xml: 'application/xml, text/xml',
  742. html: 'text/html',
  743. text: 'text/plain'
  744. }
  745. };
  746. // ### $.ajax
  747. //
  748. // Perform AJAX request
  749. //
  750. // *Arguments:*
  751. //
  752. // options — object that configure the request,
  753. // see avaliable options below
  754. //
  755. // *Avaliable options:*
  756. //
  757. // type ('GET') — type of request GET / POST
  758. // url (window.location) — url to which the request is sent
  759. // data — data to send to server,
  760. // can be string or object
  761. // dataType ('json') — what response type you accept from
  762. // the server:
  763. // 'json', 'xml', 'html', or 'text'
  764. // success — callback that is executed if
  765. // the request succeeds
  766. // error — callback that is executed if
  767. // the server drops error
  768. //
  769. // *Example:*
  770. //
  771. // $.ajax({
  772. // type: 'POST',
  773. // url: '/projects',
  774. // data: { name: 'Zepto.js' },
  775. // dataType: 'html',
  776. // success: function (data) {
  777. // $('body').append(data);
  778. // },
  779. // error: function (xhr, type) {
  780. // alert('Error!');
  781. // }
  782. // });
  783. //
  784. $.ajax = function(options){
  785. options = options || {};
  786. var settings = $.extend({}, options);
  787. for (key in $.ajaxSettings) if (!settings[key]) settings[key] = $.ajaxSettings[key];
  788. if (/=\?/.test(settings.url)) return $.ajaxJSONP(settings);
  789. if (!settings.url) settings.url = window.location.toString();
  790. if (settings.data && !settings.contentType) settings.contentType = 'application/x-www-form-urlencoded';
  791. if (isObject(settings.data)) settings.data = $.param(settings.data);
  792. if (settings.type.match(/get/i) && settings.data) {
  793. var queryString = settings.data;
  794. if (settings.url.match(/\?.*=/)) {
  795. queryString = '&' + queryString;
  796. } else if (queryString[0] != '?') {
  797. queryString = '?' + queryString;
  798. }
  799. settings.url += queryString;
  800. }
  801. var mime = settings.accepts[settings.dataType],
  802. xhr = new XMLHttpRequest();
  803. settings.headers = $.extend({'X-Requested-With': 'XMLHttpRequest'}, settings.headers || {});
  804. if (mime) settings.headers['Accept'] = mime;
  805. xhr.onreadystatechange = function(){
  806. if (xhr.readyState == 4) {
  807. var result, error = false;
  808. if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 0) {
  809. if (mime == 'application/json' && !(xhr.responseText == '')) {
  810. try { result = JSON.parse(xhr.responseText); }
  811. catch (e) { error = e; }
  812. }
  813. else result = xhr.responseText;
  814. if (error) settings.error(xhr, 'parsererror', error);
  815. else settings.success(result, 'success', xhr);
  816. } else {
  817. error = true;
  818. settings.error(xhr, 'error');
  819. }
  820. settings.complete(xhr, error ? 'error' : 'success');
  821. }
  822. };
  823. xhr.open(settings.type, settings.url, true);
  824. if (settings.beforeSend(xhr, settings) === false) {
  825. xhr.abort();
  826. return false;
  827. }
  828. if (settings.contentType) settings.headers['Content-Type'] = settings.contentType;
  829. for (name in settings.headers) xhr.setRequestHeader(name, settings.headers[name]);
  830. xhr.send(settings.data);
  831. return xhr;
  832. };
  833. // ### $.get
  834. //
  835. // Load data from the server using a GET request
  836. //
  837. // *Arguments:*
  838. //
  839. // url — url to which the request is sent
  840. // success — callback that is executed if the request succeeds
  841. //
  842. // *Example:*
  843. //
  844. // $.get(
  845. // '/projects/42',
  846. // function (data) {
  847. // $('body').append(data);
  848. // }
  849. // );
  850. //
  851. $.get = function(url, success){ $.ajax({ url: url, success: success }) };
  852. // ### $.post
  853. //
  854. // Load data from the server using POST request
  855. //
  856. // *Arguments:*
  857. //
  858. // url — url to which the request is sent
  859. // [data] — data to send to server, can be string or object
  860. // [success] — callback that is executed if the request succeeds
  861. // [dataType] — type of expected response
  862. // 'json', 'xml', 'html', or 'text'
  863. //
  864. // *Example:*
  865. //
  866. // $.post(
  867. // '/projects',
  868. // { name: 'Zepto.js' },
  869. // function (data) {
  870. // $('body').append(data);
  871. // },
  872. // 'html'
  873. // );
  874. //
  875. $.post = function(url, data, success, dataType){
  876. if ($.isFunction(data)) dataType = dataType || success, success = data, data = null;
  877. $.ajax({ type: 'POST', url: url, data: data, success: success, dataType: dataType });
  878. };
  879. // ### $.getJSON
  880. //
  881. // Load JSON from the server using GET request
  882. //
  883. // *Arguments:*
  884. //
  885. // url — url to which the request is sent
  886. // success — callback that is executed if the request succeeds
  887. //
  888. // *Example:*
  889. //
  890. // $.getJSON(
  891. // '/projects/42',
  892. // function (json) {
  893. // projects.push(json);
  894. // }
  895. // );
  896. //
  897. $.getJSON = function(url, success){ $.ajax({ url: url, success: success, dataType: 'json' }) };
  898. // ### $.fn.load
  899. //
  900. // Load data from the server into an element
  901. //
  902. // *Arguments:*
  903. //
  904. // url — url to which the request is sent
  905. // [success] — callback that is executed if the request succeeds
  906. //
  907. // *Examples:*
  908. //
  909. // $('#project_container').get(
  910. // '/projects/42',
  911. // function () {
  912. // alert('Project was successfully loaded');
  913. // }
  914. // );
  915. //
  916. // $('#project_comments').get(
  917. // '/projects/42 #comments',
  918. // function () {
  919. // alert('Comments was successfully loaded');
  920. // }
  921. // );
  922. //
  923. $.fn.load = function(url, success){
  924. if (!this.length) return this;
  925. var self = this, parts = url.split(/\s/), selector;
  926. if (parts.length > 1) url = parts[0], selector = parts[1];
  927. $.get(url, function(response){
  928. self.html(selector ?
  929. $(document.createElement('div')).html(response).find(selector).html()
  930. : response);
  931. success && success();
  932. });
  933. return this;
  934. };
  935. // ### $.param
  936. //
  937. // Encode object as a string for submission
  938. //
  939. // *Arguments:*
  940. //
  941. // obj — object to serialize
  942. // [v] — root node
  943. //
  944. // *Example:*
  945. //
  946. // $.param( { name: 'Zepto.js', version: '0.6' } );
  947. //
  948. $.param = function(obj, v){
  949. var result = [], add = function(key, value){
  950. result.push(encodeURIComponent(v ? v + '[' + key + ']' : key)
  951. + '=' + encodeURIComponent(value));
  952. },
  953. isObjArray = $.isArray(obj);
  954. for(key in obj)
  955. if(isObject(obj[key]))
  956. result.push($.param(obj[key], (v ? v + '[' + key + ']' : key)));
  957. else
  958. add(isObjArray ? '' : key, obj[key]);
  959. return result.join('&').replace('%20', '+');
  960. };
  961. })(Zepto);
  962. // Zepto.js
  963. // (c) 2010, 2011 Thomas Fuchs
  964. // Zepto.js may be freely distributed under the MIT license.
  965. (function ($) {
  966. // ### $.fn.serializeArray
  967. //
  968. // Encode a set of form elements as an array of names and values
  969. //
  970. // *Example:*
  971. //
  972. // $('#login_form').serializeArray();
  973. //
  974. // returns
  975. //
  976. // [
  977. // {
  978. // name: 'email',
  979. // value: 'koss@nocorp.me'
  980. // },
  981. // {
  982. // name: 'password',
  983. // value: '123456'
  984. // }
  985. // ]
  986. //
  987. $.fn.serializeArray = function () {
  988. var result = [], el;
  989. $( Array.prototype.slice.call(this.get(0).elements) ).each(function () {
  990. el = $(this);
  991. if ( (el.attr('type') !== 'radio' || el.is(':checked')) && !(el.attr('type') === 'checkbox' && !el.is(':checked'))) {
  992. result.push({
  993. name: el.attr('name'),
  994. value: el.val()
  995. });
  996. }
  997. });
  998. return result;
  999. };
  1000. // ### $.fn.serialize
  1001. //
  1002. //
  1003. // Encode a set of form elements as a string for submission
  1004. //
  1005. // *Example:*
  1006. //
  1007. // $('#login_form').serialize();
  1008. //
  1009. // returns
  1010. //
  1011. // "email=koss%40nocorp.me&password=123456"
  1012. //
  1013. $.fn.serialize = function () {
  1014. var result = [];
  1015. this.serializeArray().forEach(function (elm) {
  1016. result.push( encodeURIComponent(elm.name) + '=' + encodeURIComponent(elm.value) );
  1017. });
  1018. return result.join('&');
  1019. };
  1020. // ### $.fn.submit
  1021. //
  1022. // Bind or trigger the submit event for a form
  1023. //
  1024. // *Examples:*
  1025. //
  1026. // To bind a handler for the submit event:
  1027. //
  1028. // $('#login_form').submit(function (e) {
  1029. // alert('Form was submitted!');
  1030. // e.preventDefault();
  1031. // });
  1032. //
  1033. // To trigger form submit:
  1034. //
  1035. // $('#login_form').submit();
  1036. //
  1037. $.fn.submit = function (callback) {
  1038. if (callback) this.bind('submit', callback)
  1039. else if (this.length) {
  1040. var event = $.Event('submit');
  1041. this.eq(0).trigger(event);
  1042. if (!event.defaultPrevented) this.get(0).submit()
  1043. }
  1044. return this;
  1045. }
  1046. })(Zepto);
  1047. // Zepto.js
  1048. // (c) 2010, 2011 Thomas Fuchs
  1049. // Zepto.js may be freely distributed under the MIT license.
  1050. (function($){
  1051. var touch = {}, touchTimeout;
  1052. function parentIfText(node){
  1053. return 'tagName' in node ? node : node.parentNode;
  1054. }
  1055. function swipeDirection(x1, x2, y1, y2){
  1056. var xDelta = Math.abs(x1 - x2), yDelta = Math.abs(y1 - y2);
  1057. if (xDelta >= yDelta) {
  1058. return (x1 - x2 > 0 ? 'Left' : 'Right');
  1059. } else {
  1060. return (y1 - y2 > 0 ? 'Up' : 'Down');
  1061. }
  1062. }
  1063. var longTapDelay = 750;
  1064. function longTap(){
  1065. if (touch.last && (Date.now() - touch.last >= longTapDelay)) {
  1066. $(touch.target).trigger('longTap');
  1067. touch = {};
  1068. }
  1069. }
  1070. $(document).ready(function(){
  1071. $(document.body).bind('touchstart', function(e){
  1072. var now = Date.now(), delta = now - (touch.last || now);
  1073. touch.target = parentIfText(e.touches[0].target);
  1074. touchTimeout && clearTimeout(touchTimeout);
  1075. touch.x1 = e.touches[0].pageX;
  1076. touch.y1 = e.touches[0].pageY;
  1077. if (delta > 0 && delta <= 250) touch.isDoubleTap = true;
  1078. touch.last = now;
  1079. setTimeout(longTap, longTapDelay);
  1080. }).bind('touchmove', function(e){
  1081. touch.x2 = e.touches[0].pageX;
  1082. touch.y2 = e.touches[0].pageY;
  1083. }).bind('touchend', function(e){
  1084. if (touch.isDoubleTap) {
  1085. $(touch.target).trigger('doubleTap');
  1086. touch = {};
  1087. } else if (touch.x2 > 0 || touch.y2 > 0) {
  1088. (Math.abs(touch.x1 - touch.x2) > 30 || Math.abs(touch.y1 - touch.y2) > 30) &&
  1089. $(touch.target).trigger('swipe') &&
  1090. $(touch.target).trigger('swipe' + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)));
  1091. touch.x1 = touch.x2 = touch.y1 = touch.y2 = touch.last = 0;
  1092. } else if ('last' in touch) {
  1093. touchTimeout = setTimeout(function(){
  1094. touchTimeout = null;
  1095. $(touch.target).trigger('tap')
  1096. touch = {};
  1097. }, 250);
  1098. }
  1099. }).bind('touchcancel', function(){ touch = {} });
  1100. });
  1101. ['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown', 'doubleTap', 'tap', 'longTap'].forEach(function(m){
  1102. $.fn[m] = function(callback){ return this.bind(m, callback) }
  1103. });
  1104. })(Zepto);