zepto.js 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  1. /* Zepto v1.1.2-5-g4c456f6 - zepto ajax event fx fx_methods selector touch - zeptojs.com/license */
  2. var Zepto = (function() {
  3. var undefined, key, $, classList, emptyArray = [], slice = emptyArray.slice, filter = emptyArray.filter,
  4. document = window.document,
  5. elementDisplay = {}, classCache = {},
  6. cssNumber = { 'column-count': 1, 'columns': 1, 'font-weight': 1, 'line-height': 1,'opacity': 1, 'z-index': 1, 'zoom': 1 },
  7. fragmentRE = /^\s*<(\w+|!)[^>]*>/,
  8. singleTagRE = /^<(\w+)\s*\/?>(?:<\/\1>|)$/,
  9. tagExpanderRE = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig,
  10. rootNodeRE = /^(?:body|html)$/i,
  11. capitalRE = /([A-Z])/g,
  12. // special attributes that should be get/set via method calls
  13. methodAttributes = ['val', 'css', 'html', 'text', 'data', 'width', 'height', 'offset'],
  14. adjacencyOperators = [ 'after', 'prepend', 'before', 'append' ],
  15. table = document.createElement('table'),
  16. tableRow = document.createElement('tr'),
  17. containers = {
  18. 'tr': document.createElement('tbody'),
  19. 'tbody': table, 'thead': table, 'tfoot': table,
  20. 'td': tableRow, 'th': tableRow,
  21. '*': document.createElement('div')
  22. },
  23. readyRE = /complete|loaded|interactive/,
  24. classSelectorRE = /^\.([\w-]+)$/,
  25. idSelectorRE = /^#([\w-]*)$/,
  26. simpleSelectorRE = /^[\w-]*$/,
  27. class2type = {},
  28. toString = class2type.toString,
  29. zepto = {},
  30. camelize, uniq,
  31. tempParent = document.createElement('div'),
  32. propMap = {
  33. 'tabindex': 'tabIndex',
  34. 'readonly': 'readOnly',
  35. 'for': 'htmlFor',
  36. 'class': 'className',
  37. 'maxlength': 'maxLength',
  38. 'cellspacing': 'cellSpacing',
  39. 'cellpadding': 'cellPadding',
  40. 'rowspan': 'rowSpan',
  41. 'colspan': 'colSpan',
  42. 'usemap': 'useMap',
  43. 'frameborder': 'frameBorder',
  44. 'contenteditable': 'contentEditable'
  45. }
  46. zepto.matches = function(element, selector) {
  47. if (!selector || !element || element.nodeType !== 1) return false
  48. var matchesSelector = element.webkitMatchesSelector || element.mozMatchesSelector ||
  49. element.oMatchesSelector || element.matchesSelector
  50. if (matchesSelector) return matchesSelector.call(element, selector)
  51. // fall back to performing a selector:
  52. var match, parent = element.parentNode, temp = !parent
  53. if (temp) (parent = tempParent).appendChild(element)
  54. match = ~zepto.qsa(parent, selector).indexOf(element)
  55. temp && tempParent.removeChild(element)
  56. return match
  57. }
  58. function type(obj) {
  59. return obj == null ? String(obj) :
  60. class2type[toString.call(obj)] || "object"
  61. }
  62. function isFunction(value) { return type(value) == "function" }
  63. function isWindow(obj) { return obj != null && obj == obj.window }
  64. function isDocument(obj) { return obj != null && obj.nodeType == obj.DOCUMENT_NODE }
  65. function isObject(obj) { return type(obj) == "object" }
  66. function isPlainObject(obj) {
  67. return isObject(obj) && !isWindow(obj) && Object.getPrototypeOf(obj) == Object.prototype
  68. }
  69. function isArray(value) { return value instanceof Array }
  70. function likeArray(obj) { return typeof obj.length == 'number' }
  71. function compact(array) { return filter.call(array, function(item){ return item != null }) }
  72. function flatten(array) { return array.length > 0 ? $.fn.concat.apply([], array) : array }
  73. camelize = function(str){ return str.replace(/-+(.)?/g, function(match, chr){ return chr ? chr.toUpperCase() : '' }) }
  74. function dasherize(str) {
  75. return str.replace(/::/g, '/')
  76. .replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2')
  77. .replace(/([a-z\d])([A-Z])/g, '$1_$2')
  78. .replace(/_/g, '-')
  79. .toLowerCase()
  80. }
  81. uniq = function(array){ return filter.call(array, function(item, idx){ return array.indexOf(item) == idx }) }
  82. function classRE(name) {
  83. return name in classCache ?
  84. classCache[name] : (classCache[name] = new RegExp('(^|\\s)' + name + '(\\s|$)'))
  85. }
  86. function maybeAddPx(name, value) {
  87. return (typeof value == "number" && !cssNumber[dasherize(name)]) ? value + "px" : value
  88. }
  89. function defaultDisplay(nodeName) {
  90. var element, display
  91. if (!elementDisplay[nodeName]) {
  92. element = document.createElement(nodeName)
  93. document.body.appendChild(element)
  94. display = getComputedStyle(element, '').getPropertyValue("display")
  95. element.parentNode.removeChild(element)
  96. display == "none" && (display = "block")
  97. elementDisplay[nodeName] = display
  98. }
  99. return elementDisplay[nodeName]
  100. }
  101. function children(element) {
  102. return 'children' in element ?
  103. slice.call(element.children) :
  104. $.map(element.childNodes, function(node){ if (node.nodeType == 1) return node })
  105. }
  106. // `$.zepto.fragment` takes a html string and an optional tag name
  107. // to generate DOM nodes nodes from the given html string.
  108. // The generated DOM nodes are returned as an array.
  109. // This function can be overriden in plugins for example to make
  110. // it compatible with browsers that don't support the DOM fully.
  111. zepto.fragment = function(html, name, properties) {
  112. var dom, nodes, container
  113. // A special case optimization for a single tag
  114. if (singleTagRE.test(html)) dom = $(document.createElement(RegExp.$1))
  115. if (!dom) {
  116. if (html.replace) html = html.replace(tagExpanderRE, "<$1></$2>")
  117. if (name === undefined) name = fragmentRE.test(html) && RegExp.$1
  118. if (!(name in containers)) name = '*'
  119. container = containers[name]
  120. container.innerHTML = '' + html
  121. dom = $.each(slice.call(container.childNodes), function(){
  122. container.removeChild(this)
  123. })
  124. }
  125. if (isPlainObject(properties)) {
  126. nodes = $(dom)
  127. $.each(properties, function(key, value) {
  128. if (methodAttributes.indexOf(key) > -1) nodes[key](value)
  129. else nodes.attr(key, value)
  130. })
  131. }
  132. return dom
  133. }
  134. // `$.zepto.Z` swaps out the prototype of the given `dom` array
  135. // of nodes with `$.fn` and thus supplying all the Zepto functions
  136. // to the array. Note that `__proto__` is not supported on Internet
  137. // Explorer. This method can be overriden in plugins.
  138. zepto.Z = function(dom, selector) {
  139. dom = dom || []
  140. dom.__proto__ = $.fn
  141. dom.selector = selector || ''
  142. return dom
  143. }
  144. // `$.zepto.isZ` should return `true` if the given object is a Zepto
  145. // collection. This method can be overriden in plugins.
  146. zepto.isZ = function(object) {
  147. return object instanceof zepto.Z
  148. }
  149. // `$.zepto.init` is Zepto's counterpart to jQuery's `$.fn.init` and
  150. // takes a CSS selector and an optional context (and handles various
  151. // special cases).
  152. // This method can be overriden in plugins.
  153. zepto.init = function(selector, context) {
  154. var dom
  155. // If nothing given, return an empty Zepto collection
  156. if (!selector) return zepto.Z()
  157. // Optimize for string selectors
  158. else if (typeof selector == 'string') {
  159. selector = selector.trim()
  160. // If it's a html fragment, create nodes from it
  161. // Note: In both Chrome 21 and Firefox 15, DOM error 12
  162. // is thrown if the fragment doesn't begin with <
  163. if (selector[0] == '<' && fragmentRE.test(selector))
  164. dom = zepto.fragment(selector, RegExp.$1, context), selector = null
  165. // If there's a context, create a collection on that context first, and select
  166. // nodes from there
  167. else if (context !== undefined) return $(context).find(selector)
  168. // If it's a CSS selector, use it to select nodes.
  169. else dom = zepto.qsa(document, selector)
  170. }
  171. // If a function is given, call it when the DOM is ready
  172. else if (isFunction(selector)) return $(document).ready(selector)
  173. // If a Zepto collection is given, just return it
  174. else if (zepto.isZ(selector)) return selector
  175. else {
  176. // normalize array if an array of nodes is given
  177. if (isArray(selector)) dom = compact(selector)
  178. // Wrap DOM nodes.
  179. else if (isObject(selector))
  180. dom = [selector], selector = null
  181. // If it's a html fragment, create nodes from it
  182. else if (fragmentRE.test(selector))
  183. dom = zepto.fragment(selector.trim(), RegExp.$1, context), selector = null
  184. // If there's a context, create a collection on that context first, and select
  185. // nodes from there
  186. else if (context !== undefined) return $(context).find(selector)
  187. // And last but no least, if it's a CSS selector, use it to select nodes.
  188. else dom = zepto.qsa(document, selector)
  189. }
  190. // create a new Zepto collection from the nodes found
  191. return zepto.Z(dom, selector)
  192. }
  193. // `$` will be the base `Zepto` object. When calling this
  194. // function just call `$.zepto.init, which makes the implementation
  195. // details of selecting nodes and creating Zepto collections
  196. // patchable in plugins.
  197. $ = function(selector, context){
  198. return zepto.init(selector, context)
  199. }
  200. function extend(target, source, deep) {
  201. for (key in source)
  202. if (deep && (isPlainObject(source[key]) || isArray(source[key]))) {
  203. if (isPlainObject(source[key]) && !isPlainObject(target[key]))
  204. target[key] = {}
  205. if (isArray(source[key]) && !isArray(target[key]))
  206. target[key] = []
  207. extend(target[key], source[key], deep)
  208. }
  209. else if (source[key] !== undefined) target[key] = source[key]
  210. }
  211. // Copy all but undefined properties from one or more
  212. // objects to the `target` object.
  213. $.extend = function(target){
  214. var deep, args = slice.call(arguments, 1)
  215. if (typeof target == 'boolean') {
  216. deep = target
  217. target = args.shift()
  218. }
  219. args.forEach(function(arg){ extend(target, arg, deep) })
  220. return target
  221. }
  222. // `$.zepto.qsa` is Zepto's CSS selector implementation which
  223. // uses `document.querySelectorAll` and optimizes for some special cases, like `#id`.
  224. // This method can be overriden in plugins.
  225. zepto.qsa = function(element, selector){
  226. var found,
  227. maybeID = selector[0] == '#',
  228. maybeClass = !maybeID && selector[0] == '.',
  229. nameOnly = maybeID || maybeClass ? selector.slice(1) : selector, // Ensure that a 1 char tag name still gets checked
  230. isSimple = simpleSelectorRE.test(nameOnly)
  231. return (isDocument(element) && isSimple && maybeID) ?
  232. ( (found = element.getElementById(nameOnly)) ? [found] : [] ) :
  233. (element.nodeType !== 1 && element.nodeType !== 9) ? [] :
  234. slice.call(
  235. isSimple && !maybeID ?
  236. maybeClass ? element.getElementsByClassName(nameOnly) : // If it's simple, it could be a class
  237. element.getElementsByTagName(selector) : // Or a tag
  238. element.querySelectorAll(selector) // Or it's not simple, and we need to query all
  239. )
  240. }
  241. function filtered(nodes, selector) {
  242. return selector == null ? $(nodes) : $(nodes).filter(selector)
  243. }
  244. $.contains = function(parent, node) {
  245. return parent !== node && parent.contains(node)
  246. }
  247. function funcArg(context, arg, idx, payload) {
  248. return isFunction(arg) ? arg.call(context, idx, payload) : arg
  249. }
  250. function setAttribute(node, name, value) {
  251. value == null ? node.removeAttribute(name) : node.setAttribute(name, value)
  252. }
  253. // access className property while respecting SVGAnimatedString
  254. function className(node, value){
  255. var klass = node.className,
  256. svg = klass && klass.baseVal !== undefined
  257. if (value === undefined) return svg ? klass.baseVal : klass
  258. svg ? (klass.baseVal = value) : (node.className = value)
  259. }
  260. // "true" => true
  261. // "false" => false
  262. // "null" => null
  263. // "42" => 42
  264. // "42.5" => 42.5
  265. // "08" => "08"
  266. // JSON => parse if valid
  267. // String => self
  268. function deserializeValue(value) {
  269. var num
  270. try {
  271. return value ?
  272. value == "true" ||
  273. ( value == "false" ? false :
  274. value == "null" ? null :
  275. !/^0/.test(value) && !isNaN(num = Number(value)) ? num :
  276. /^[\[\{]/.test(value) ? $.parseJSON(value) :
  277. value )
  278. : value
  279. } catch(e) {
  280. return value
  281. }
  282. }
  283. $.type = type
  284. $.isFunction = isFunction
  285. $.isWindow = isWindow
  286. $.isArray = isArray
  287. $.isPlainObject = isPlainObject
  288. $.isEmptyObject = function(obj) {
  289. var name
  290. for (name in obj) return false
  291. return true
  292. }
  293. $.inArray = function(elem, array, i){
  294. return emptyArray.indexOf.call(array, elem, i)
  295. }
  296. $.camelCase = camelize
  297. $.trim = function(str) {
  298. return str == null ? "" : String.prototype.trim.call(str)
  299. }
  300. // plugin compatibility
  301. $.uuid = 0
  302. $.support = { }
  303. $.expr = { }
  304. $.map = function(elements, callback){
  305. var value, values = [], i, key
  306. if (likeArray(elements))
  307. for (i = 0; i < elements.length; i++) {
  308. value = callback(elements[i], i)
  309. if (value != null) values.push(value)
  310. }
  311. else
  312. for (key in elements) {
  313. value = callback(elements[key], key)
  314. if (value != null) values.push(value)
  315. }
  316. return flatten(values)
  317. }
  318. $.each = function(elements, callback){
  319. var i, key
  320. if (likeArray(elements)) {
  321. for (i = 0; i < elements.length; i++)
  322. if (callback.call(elements[i], i, elements[i]) === false) return elements
  323. } else {
  324. for (key in elements)
  325. if (callback.call(elements[key], key, elements[key]) === false) return elements
  326. }
  327. return elements
  328. }
  329. $.grep = function(elements, callback){
  330. return filter.call(elements, callback)
  331. }
  332. if (window.JSON) $.parseJSON = JSON.parse
  333. // Populate the class2type map
  334. $.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) {
  335. class2type[ "[object " + name + "]" ] = name.toLowerCase()
  336. })
  337. // Define methods that will be available on all
  338. // Zepto collections
  339. $.fn = {
  340. // Because a collection acts like an array
  341. // copy over these useful array functions.
  342. forEach: emptyArray.forEach,
  343. reduce: emptyArray.reduce,
  344. push: emptyArray.push,
  345. sort: emptyArray.sort,
  346. indexOf: emptyArray.indexOf,
  347. concat: emptyArray.concat,
  348. // `map` and `slice` in the jQuery API work differently
  349. // from their array counterparts
  350. map: function(fn){
  351. return $($.map(this, function(el, i){ return fn.call(el, i, el) }))
  352. },
  353. slice: function(){
  354. return $(slice.apply(this, arguments))
  355. },
  356. ready: function(callback){
  357. // need to check if document.body exists for IE as that browser reports
  358. // document ready when it hasn't yet created the body element
  359. if (readyRE.test(document.readyState) && document.body) callback($)
  360. else document.addEventListener('DOMContentLoaded', function(){ callback($) }, false)
  361. return this
  362. },
  363. get: function(idx){
  364. return idx === undefined ? slice.call(this) : this[idx >= 0 ? idx : idx + this.length]
  365. },
  366. toArray: function(){ return this.get() },
  367. size: function(){
  368. return this.length
  369. },
  370. remove: function(){
  371. return this.each(function(){
  372. if (this.parentNode != null)
  373. this.parentNode.removeChild(this)
  374. })
  375. },
  376. each: function(callback){
  377. emptyArray.every.call(this, function(el, idx){
  378. return callback.call(el, idx, el) !== false
  379. })
  380. return this
  381. },
  382. filter: function(selector){
  383. if (isFunction(selector)) return this.not(this.not(selector))
  384. return $(filter.call(this, function(element){
  385. return zepto.matches(element, selector)
  386. }))
  387. },
  388. add: function(selector,context){
  389. return $(uniq(this.concat($(selector,context))))
  390. },
  391. is: function(selector){
  392. return this.length > 0 && zepto.matches(this[0], selector)
  393. },
  394. not: function(selector){
  395. var nodes=[]
  396. if (isFunction(selector) && selector.call !== undefined)
  397. this.each(function(idx){
  398. if (!selector.call(this,idx)) nodes.push(this)
  399. })
  400. else {
  401. var excludes = typeof selector == 'string' ? this.filter(selector) :
  402. (likeArray(selector) && isFunction(selector.item)) ? slice.call(selector) : $(selector)
  403. this.forEach(function(el){
  404. if (excludes.indexOf(el) < 0) nodes.push(el)
  405. })
  406. }
  407. return $(nodes)
  408. },
  409. has: function(selector){
  410. return this.filter(function(){
  411. return isObject(selector) ?
  412. $.contains(this, selector) :
  413. $(this).find(selector).size()
  414. })
  415. },
  416. eq: function(idx){
  417. return idx === -1 ? this.slice(idx) : this.slice(idx, + idx + 1)
  418. },
  419. first: function(){
  420. var el = this[0]
  421. return el && !isObject(el) ? el : $(el)
  422. },
  423. last: function(){
  424. var el = this[this.length - 1]
  425. return el && !isObject(el) ? el : $(el)
  426. },
  427. find: function(selector){
  428. var result, $this = this
  429. if (typeof selector == 'object')
  430. result = $(selector).filter(function(){
  431. var node = this
  432. return emptyArray.some.call($this, function(parent){
  433. return $.contains(parent, node)
  434. })
  435. })
  436. else if (this.length == 1) result = $(zepto.qsa(this[0], selector))
  437. else result = this.map(function(){ return zepto.qsa(this, selector) })
  438. return result
  439. },
  440. closest: function(selector, context){
  441. var node = this[0], collection = false
  442. if (typeof selector == 'object') collection = $(selector)
  443. while (node && !(collection ? collection.indexOf(node) >= 0 : zepto.matches(node, selector)))
  444. node = node !== context && !isDocument(node) && node.parentNode
  445. return $(node)
  446. },
  447. parents: function(selector){
  448. var ancestors = [], nodes = this
  449. while (nodes.length > 0)
  450. nodes = $.map(nodes, function(node){
  451. if ((node = node.parentNode) && !isDocument(node) && ancestors.indexOf(node) < 0) {
  452. ancestors.push(node)
  453. return node
  454. }
  455. })
  456. return filtered(ancestors, selector)
  457. },
  458. parent: function(selector){
  459. return filtered(uniq(this.pluck('parentNode')), selector)
  460. },
  461. children: function(selector){
  462. return filtered(this.map(function(){ return children(this) }), selector)
  463. },
  464. contents: function() {
  465. return this.map(function() { return slice.call(this.childNodes) })
  466. },
  467. siblings: function(selector){
  468. return filtered(this.map(function(i, el){
  469. return filter.call(children(el.parentNode), function(child){ return child!==el })
  470. }), selector)
  471. },
  472. empty: function(){
  473. return this.each(function(){ this.innerHTML = '' })
  474. },
  475. // `pluck` is borrowed from Prototype.js
  476. pluck: function(property){
  477. return $.map(this, function(el){ return el[property] })
  478. },
  479. show: function(){
  480. return this.each(function(){
  481. this.style.display == "none" && (this.style.display = '')
  482. if (getComputedStyle(this, '').getPropertyValue("display") == "none")
  483. this.style.display = defaultDisplay(this.nodeName)
  484. })
  485. },
  486. replaceWith: function(newContent){
  487. return this.before(newContent).remove()
  488. },
  489. wrap: function(structure){
  490. var func = isFunction(structure)
  491. if (this[0] && !func)
  492. var dom = $(structure).get(0),
  493. clone = dom.parentNode || this.length > 1
  494. return this.each(function(index){
  495. $(this).wrapAll(
  496. func ? structure.call(this, index) :
  497. clone ? dom.cloneNode(true) : dom
  498. )
  499. })
  500. },
  501. wrapAll: function(structure){
  502. if (this[0]) {
  503. $(this[0]).before(structure = $(structure))
  504. var children
  505. // drill down to the inmost element
  506. while ((children = structure.children()).length) structure = children.first()
  507. $(structure).append(this)
  508. }
  509. return this
  510. },
  511. wrapInner: function(structure){
  512. var func = isFunction(structure)
  513. return this.each(function(index){
  514. var self = $(this), contents = self.contents(),
  515. dom = func ? structure.call(this, index) : structure
  516. contents.length ? contents.wrapAll(dom) : self.append(dom)
  517. })
  518. },
  519. unwrap: function(){
  520. this.parent().each(function(){
  521. $(this).replaceWith($(this).children())
  522. })
  523. return this
  524. },
  525. clone: function(){
  526. return this.map(function(){ return this.cloneNode(true) })
  527. },
  528. hide: function(){
  529. return this.css("display", "none")
  530. },
  531. toggle: function(setting){
  532. return this.each(function(){
  533. var el = $(this)
  534. ;(setting === undefined ? el.css("display") == "none" : setting) ? el.show() : el.hide()
  535. })
  536. },
  537. prev: function(selector){ return $(this.pluck('previousElementSibling')).filter(selector || '*') },
  538. next: function(selector){ return $(this.pluck('nextElementSibling')).filter(selector || '*') },
  539. html: function(html){
  540. return arguments.length === 0 ?
  541. (this.length > 0 ? this[0].innerHTML : null) :
  542. this.each(function(idx){
  543. var originHtml = this.innerHTML
  544. $(this).empty().append( funcArg(this, html, idx, originHtml) )
  545. })
  546. },
  547. text: function(text){
  548. return arguments.length === 0 ?
  549. (this.length > 0 ? this[0].textContent : null) :
  550. this.each(function(){ this.textContent = (text === undefined) ? '' : ''+text })
  551. },
  552. attr: function(name, value){
  553. var result
  554. return (typeof name == 'string' && value === undefined) ?
  555. (this.length == 0 || this[0].nodeType !== 1 ? undefined :
  556. (name == 'value' && this[0].nodeName == 'INPUT') ? this.val() :
  557. (!(result = this[0].getAttribute(name)) && name in this[0]) ? this[0][name] : result
  558. ) :
  559. this.each(function(idx){
  560. if (this.nodeType !== 1) return
  561. if (isObject(name)) for (key in name) setAttribute(this, key, name[key])
  562. else setAttribute(this, name, funcArg(this, value, idx, this.getAttribute(name)))
  563. })
  564. },
  565. removeAttr: function(name){
  566. return this.each(function(){ this.nodeType === 1 && setAttribute(this, name) })
  567. },
  568. prop: function(name, value){
  569. name = propMap[name] || name
  570. return (value === undefined) ?
  571. (this[0] && this[0][name]) :
  572. this.each(function(idx){
  573. this[name] = funcArg(this, value, idx, this[name])
  574. })
  575. },
  576. data: function(name, value){
  577. var data = this.attr('data-' + name.replace(capitalRE, '-$1').toLowerCase(), value)
  578. return data !== null ? deserializeValue(data) : undefined
  579. },
  580. val: function(value){
  581. return arguments.length === 0 ?
  582. (this[0] && (this[0].multiple ?
  583. $(this[0]).find('option').filter(function(){ return this.selected }).pluck('value') :
  584. this[0].value)
  585. ) :
  586. this.each(function(idx){
  587. this.value = funcArg(this, value, idx, this.value)
  588. })
  589. },
  590. offset: function(coordinates){
  591. if (coordinates) return this.each(function(index){
  592. var $this = $(this),
  593. coords = funcArg(this, coordinates, index, $this.offset()),
  594. parentOffset = $this.offsetParent().offset(),
  595. props = {
  596. top: coords.top - parentOffset.top,
  597. left: coords.left - parentOffset.left
  598. }
  599. if ($this.css('position') == 'static') props['position'] = 'relative'
  600. $this.css(props)
  601. })
  602. if (this.length==0) return null
  603. var obj = this[0].getBoundingClientRect()
  604. return {
  605. left: obj.left + window.pageXOffset,
  606. top: obj.top + window.pageYOffset,
  607. width: Math.round(obj.width),
  608. height: Math.round(obj.height)
  609. }
  610. },
  611. css: function(property, value){
  612. if (arguments.length < 2) {
  613. var element = this[0], computedStyle = getComputedStyle(element, '')
  614. if(!element) return
  615. if (typeof property == 'string')
  616. return element.style[camelize(property)] || computedStyle.getPropertyValue(property)
  617. else if (isArray(property)) {
  618. var props = {}
  619. $.each(isArray(property) ? property: [property], function(_, prop){
  620. props[prop] = (element.style[camelize(prop)] || computedStyle.getPropertyValue(prop))
  621. })
  622. return props
  623. }
  624. }
  625. var css = ''
  626. if (type(property) == 'string') {
  627. if (!value && value !== 0)
  628. this.each(function(){ this.style.removeProperty(dasherize(property)) })
  629. else
  630. css = dasherize(property) + ":" + maybeAddPx(property, value)
  631. } else {
  632. for (key in property)
  633. if (!property[key] && property[key] !== 0)
  634. this.each(function(){ this.style.removeProperty(dasherize(key)) })
  635. else
  636. css += dasherize(key) + ':' + maybeAddPx(key, property[key]) + ';'
  637. }
  638. return this.each(function(){ this.style.cssText += ';' + css })
  639. },
  640. index: function(element){
  641. return element ? this.indexOf($(element)[0]) : this.parent().children().indexOf(this[0])
  642. },
  643. hasClass: function(name){
  644. if (!name) return false
  645. return emptyArray.some.call(this, function(el){
  646. return this.test(className(el))
  647. }, classRE(name))
  648. },
  649. addClass: function(name){
  650. if (!name) return this
  651. return this.each(function(idx){
  652. classList = []
  653. var cls = className(this), newName = funcArg(this, name, idx, cls)
  654. newName.split(/\s+/g).forEach(function(klass){
  655. if (!$(this).hasClass(klass)) classList.push(klass)
  656. }, this)
  657. classList.length && className(this, cls + (cls ? " " : "") + classList.join(" "))
  658. })
  659. },
  660. removeClass: function(name){
  661. return this.each(function(idx){
  662. if (name === undefined) return className(this, '')
  663. classList = className(this)
  664. funcArg(this, name, idx, classList).split(/\s+/g).forEach(function(klass){
  665. classList = classList.replace(classRE(klass), " ")
  666. })
  667. className(this, classList.trim())
  668. })
  669. },
  670. toggleClass: function(name, when){
  671. if (!name) return this
  672. return this.each(function(idx){
  673. var $this = $(this), names = funcArg(this, name, idx, className(this))
  674. names.split(/\s+/g).forEach(function(klass){
  675. (when === undefined ? !$this.hasClass(klass) : when) ?
  676. $this.addClass(klass) : $this.removeClass(klass)
  677. })
  678. })
  679. },
  680. scrollTop: function(value){
  681. if (!this.length) return
  682. var hasScrollTop = 'scrollTop' in this[0]
  683. if (value === undefined) return hasScrollTop ? this[0].scrollTop : this[0].pageYOffset
  684. return this.each(hasScrollTop ?
  685. function(){ this.scrollTop = value } :
  686. function(){ this.scrollTo(this.scrollX, value) })
  687. },
  688. scrollLeft: function(value){
  689. if (!this.length) return
  690. var hasScrollLeft = 'scrollLeft' in this[0]
  691. if (value === undefined) return hasScrollLeft ? this[0].scrollLeft : this[0].pageXOffset
  692. return this.each(hasScrollLeft ?
  693. function(){ this.scrollLeft = value } :
  694. function(){ this.scrollTo(value, this.scrollY) })
  695. },
  696. position: function() {
  697. if (!this.length) return
  698. var elem = this[0],
  699. // Get *real* offsetParent
  700. offsetParent = this.offsetParent(),
  701. // Get correct offsets
  702. offset = this.offset(),
  703. parentOffset = rootNodeRE.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset()
  704. // Subtract element margins
  705. // note: when an element has margin: auto the offsetLeft and marginLeft
  706. // are the same in Safari causing offset.left to incorrectly be 0
  707. offset.top -= parseFloat( $(elem).css('margin-top') ) || 0
  708. offset.left -= parseFloat( $(elem).css('margin-left') ) || 0
  709. // Add offsetParent borders
  710. parentOffset.top += parseFloat( $(offsetParent[0]).css('border-top-width') ) || 0
  711. parentOffset.left += parseFloat( $(offsetParent[0]).css('border-left-width') ) || 0
  712. // Subtract the two offsets
  713. return {
  714. top: offset.top - parentOffset.top,
  715. left: offset.left - parentOffset.left
  716. }
  717. },
  718. offsetParent: function() {
  719. return this.map(function(){
  720. var parent = this.offsetParent || document.body
  721. while (parent && !rootNodeRE.test(parent.nodeName) && $(parent).css("position") == "static")
  722. parent = parent.offsetParent
  723. return parent
  724. })
  725. }
  726. }
  727. // for now
  728. $.fn.detach = $.fn.remove
  729. // Generate the `width` and `height` functions
  730. ;['width', 'height'].forEach(function(dimension){
  731. var dimensionProperty =
  732. dimension.replace(/./, function(m){ return m[0].toUpperCase() })
  733. $.fn[dimension] = function(value){
  734. var offset, el = this[0]
  735. if (value === undefined) return isWindow(el) ? el['inner' + dimensionProperty] :
  736. isDocument(el) ? el.documentElement['scroll' + dimensionProperty] :
  737. (offset = this.offset()) && offset[dimension]
  738. else return this.each(function(idx){
  739. el = $(this)
  740. el.css(dimension, funcArg(this, value, idx, el[dimension]()))
  741. })
  742. }
  743. })
  744. function traverseNode(node, fun) {
  745. fun(node)
  746. for (var key in node.childNodes) traverseNode(node.childNodes[key], fun)
  747. }
  748. // Generate the `after`, `prepend`, `before`, `append`,
  749. // `insertAfter`, `insertBefore`, `appendTo`, and `prependTo` methods.
  750. adjacencyOperators.forEach(function(operator, operatorIndex) {
  751. var inside = operatorIndex % 2 //=> prepend, append
  752. $.fn[operator] = function(){
  753. // arguments can be nodes, arrays of nodes, Zepto objects and HTML strings
  754. var argType, nodes = $.map(arguments, function(arg) {
  755. argType = type(arg)
  756. return argType == "object" || argType == "array" || arg == null ?
  757. arg : zepto.fragment(arg)
  758. }),
  759. parent, copyByClone = this.length > 1
  760. if (nodes.length < 1) return this
  761. return this.each(function(_, target){
  762. parent = inside ? target : target.parentNode
  763. // convert all methods to a "before" operation
  764. target = operatorIndex == 0 ? target.nextSibling :
  765. operatorIndex == 1 ? target.firstChild :
  766. operatorIndex == 2 ? target :
  767. null
  768. nodes.forEach(function(node){
  769. if (copyByClone) node = node.cloneNode(true)
  770. else if (!parent) return $(node).remove()
  771. traverseNode(parent.insertBefore(node, target), function(el){
  772. if (el.nodeName != null && el.nodeName.toUpperCase() === 'SCRIPT' &&
  773. (!el.type || el.type === 'text/javascript') && !el.src)
  774. window['eval'].call(window, el.innerHTML)
  775. })
  776. })
  777. })
  778. }
  779. // after => insertAfter
  780. // prepend => prependTo
  781. // before => insertBefore
  782. // append => appendTo
  783. $.fn[inside ? operator+'To' : 'insert'+(operatorIndex ? 'Before' : 'After')] = function(html){
  784. $(html)[operator](this)
  785. return this
  786. }
  787. })
  788. zepto.Z.prototype = $.fn
  789. // Export internal API functions in the `$.zepto` namespace
  790. zepto.uniq = uniq
  791. zepto.deserializeValue = deserializeValue
  792. $.zepto = zepto
  793. return $
  794. })()
  795. window.Zepto = Zepto
  796. window.$ === undefined && (window.$ = Zepto)
  797. ;(function($){
  798. var jsonpID = 0,
  799. document = window.document,
  800. key,
  801. name,
  802. rscript = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
  803. scriptTypeRE = /^(?:text|application)\/javascript/i,
  804. xmlTypeRE = /^(?:text|application)\/xml/i,
  805. jsonType = 'application/json',
  806. htmlType = 'text/html',
  807. blankRE = /^\s*$/
  808. // trigger a custom event and return false if it was cancelled
  809. function triggerAndReturn(context, eventName, data) {
  810. var event = $.Event(eventName)
  811. $(context).trigger(event, data)
  812. return !event.isDefaultPrevented()
  813. }
  814. // trigger an Ajax "global" event
  815. function triggerGlobal(settings, context, eventName, data) {
  816. if (settings.global) return triggerAndReturn(context || document, eventName, data)
  817. }
  818. // Number of active Ajax requests
  819. $.active = 0
  820. function ajaxStart(settings) {
  821. if (settings.global && $.active++ === 0) triggerGlobal(settings, null, 'ajaxStart')
  822. }
  823. function ajaxStop(settings) {
  824. if (settings.global && !(--$.active)) triggerGlobal(settings, null, 'ajaxStop')
  825. }
  826. // triggers an extra global event "ajaxBeforeSend" that's like "ajaxSend" but cancelable
  827. function ajaxBeforeSend(xhr, settings) {
  828. var context = settings.context
  829. if (settings.beforeSend.call(context, xhr, settings) === false ||
  830. triggerGlobal(settings, context, 'ajaxBeforeSend', [xhr, settings]) === false)
  831. return false
  832. triggerGlobal(settings, context, 'ajaxSend', [xhr, settings])
  833. }
  834. function ajaxSuccess(data, xhr, settings, deferred) {
  835. var context = settings.context, status = 'success'
  836. settings.success.call(context, data, status, xhr)
  837. if (deferred) deferred.resolveWith(context, [data, status, xhr])
  838. triggerGlobal(settings, context, 'ajaxSuccess', [xhr, settings, data])
  839. ajaxComplete(status, xhr, settings)
  840. }
  841. // type: "timeout", "error", "abort", "parsererror"
  842. function ajaxError(error, type, xhr, settings, deferred) {
  843. var context = settings.context
  844. settings.error.call(context, xhr, type, error)
  845. if (deferred) deferred.rejectWith(context, [xhr, type, error])
  846. triggerGlobal(settings, context, 'ajaxError', [xhr, settings, error || type])
  847. ajaxComplete(type, xhr, settings)
  848. }
  849. // status: "success", "notmodified", "error", "timeout", "abort", "parsererror"
  850. function ajaxComplete(status, xhr, settings) {
  851. var context = settings.context
  852. settings.complete.call(context, xhr, status)
  853. triggerGlobal(settings, context, 'ajaxComplete', [xhr, settings])
  854. ajaxStop(settings)
  855. }
  856. // Empty function, used as default callback
  857. function empty() {}
  858. $.ajaxJSONP = function(options, deferred){
  859. if (!('type' in options)) return $.ajax(options)
  860. var _callbackName = options.jsonpCallback,
  861. callbackName = ($.isFunction(_callbackName) ?
  862. _callbackName() : _callbackName) || ('jsonp' + (++jsonpID)),
  863. script = document.createElement('script'),
  864. originalCallback = window[callbackName],
  865. responseData,
  866. abort = function(errorType) {
  867. $(script).triggerHandler('error', errorType || 'abort')
  868. },
  869. xhr = { abort: abort }, abortTimeout
  870. if (deferred) deferred.promise(xhr)
  871. $(script).on('load error', function(e, errorType){
  872. clearTimeout(abortTimeout)
  873. $(script).off().remove()
  874. if (e.type == 'error' || !responseData) {
  875. ajaxError(null, errorType || 'error', xhr, options, deferred)
  876. } else {
  877. ajaxSuccess(responseData[0], xhr, options, deferred)
  878. }
  879. window[callbackName] = originalCallback
  880. if (responseData && $.isFunction(originalCallback))
  881. originalCallback(responseData[0])
  882. originalCallback = responseData = undefined
  883. })
  884. if (ajaxBeforeSend(xhr, options) === false) {
  885. abort('abort')
  886. return xhr
  887. }
  888. window[callbackName] = function(){
  889. responseData = arguments
  890. }
  891. script.src = options.url.replace(/=\?/, '=' + callbackName)
  892. document.head.appendChild(script)
  893. if (options.timeout > 0) abortTimeout = setTimeout(function(){
  894. abort('timeout')
  895. }, options.timeout)
  896. return xhr
  897. }
  898. $.ajaxSettings = {
  899. // Default type of request
  900. type: 'GET',
  901. // Callback that is executed before request
  902. beforeSend: empty,
  903. // Callback that is executed if the request succeeds
  904. success: empty,
  905. // Callback that is executed the the server drops error
  906. error: empty,
  907. // Callback that is executed on request complete (both: error and success)
  908. complete: empty,
  909. // The context for the callbacks
  910. context: null,
  911. // Whether to trigger "global" Ajax events
  912. global: true,
  913. // Transport
  914. xhr: function () {
  915. return new window.XMLHttpRequest()
  916. },
  917. // MIME types mapping
  918. // IIS returns Javascript as "application/x-javascript"
  919. accepts: {
  920. script: 'text/javascript, application/javascript, application/x-javascript',
  921. json: jsonType,
  922. xml: 'application/xml, text/xml',
  923. html: htmlType,
  924. text: 'text/plain'
  925. },
  926. // Whether the request is to another domain
  927. crossDomain: false,
  928. // Default timeout
  929. timeout: 0,
  930. // Whether data should be serialized to string
  931. processData: true,
  932. // Whether the browser should be allowed to cache GET responses
  933. cache: true
  934. }
  935. function mimeToDataType(mime) {
  936. if (mime) mime = mime.split(';', 2)[0]
  937. return mime && ( mime == htmlType ? 'html' :
  938. mime == jsonType ? 'json' :
  939. scriptTypeRE.test(mime) ? 'script' :
  940. xmlTypeRE.test(mime) && 'xml' ) || 'text'
  941. }
  942. function appendQuery(url, query) {
  943. if (query == '') return url
  944. return (url + '&' + query).replace(/[&?]{1,2}/, '?')
  945. }
  946. // serialize payload and append it to the URL for GET requests
  947. function serializeData(options) {
  948. if (options.processData && options.data && $.type(options.data) != "string")
  949. options.data = $.param(options.data, options.traditional)
  950. if (options.data && (!options.type || options.type.toUpperCase() == 'GET'))
  951. options.url = appendQuery(options.url, options.data), options.data = undefined
  952. }
  953. $.ajax = function(options){
  954. var settings = $.extend({}, options || {}),
  955. deferred = $.Deferred && $.Deferred()
  956. for (key in $.ajaxSettings) if (settings[key] === undefined) settings[key] = $.ajaxSettings[key]
  957. ajaxStart(settings)
  958. if (!settings.crossDomain) settings.crossDomain = /^([\w-]+:)?\/\/([^\/]+)/.test(settings.url) &&
  959. RegExp.$2 != window.location.host
  960. if (!settings.url) settings.url = window.location.toString()
  961. serializeData(settings)
  962. if (settings.cache === false) settings.url = appendQuery(settings.url, '_=' + Date.now())
  963. var dataType = settings.dataType, hasPlaceholder = /=\?/.test(settings.url)
  964. if (dataType == 'jsonp' || hasPlaceholder) {
  965. if (!hasPlaceholder)
  966. settings.url = appendQuery(settings.url,
  967. settings.jsonp ? (settings.jsonp + '=?') : settings.jsonp === false ? '' : 'callback=?')
  968. return $.ajaxJSONP(settings, deferred)
  969. }
  970. var mime = settings.accepts[dataType],
  971. headers = { },
  972. setHeader = function(name, value) { headers[name.toLowerCase()] = [name, value] },
  973. protocol = /^([\w-]+:)\/\//.test(settings.url) ? RegExp.$1 : window.location.protocol,
  974. xhr = settings.xhr(),
  975. nativeSetHeader = xhr.setRequestHeader,
  976. abortTimeout
  977. if (deferred) deferred.promise(xhr)
  978. if (!settings.crossDomain) setHeader('X-Requested-With', 'XMLHttpRequest')
  979. setHeader('Accept', mime || '*/*')
  980. if (mime = settings.mimeType || mime) {
  981. if (mime.indexOf(',') > -1) mime = mime.split(',', 2)[0]
  982. xhr.overrideMimeType && xhr.overrideMimeType(mime)
  983. }
  984. if (settings.contentType || (settings.contentType !== false && settings.data && settings.type.toUpperCase() != 'GET'))
  985. setHeader('Content-Type', settings.contentType || 'application/x-www-form-urlencoded')
  986. if (settings.headers) for (name in settings.headers) setHeader(name, settings.headers[name])
  987. xhr.setRequestHeader = setHeader
  988. xhr.onreadystatechange = function(){
  989. if (xhr.readyState == 4) {
  990. xhr.onreadystatechange = empty
  991. clearTimeout(abortTimeout)
  992. var result, error = false
  993. if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304 || (xhr.status == 0 && protocol == 'file:')) {
  994. dataType = dataType || mimeToDataType(settings.mimeType || xhr.getResponseHeader('content-type'))
  995. result = xhr.responseText
  996. try {
  997. // http://perfectionkills.com/global-eval-what-are-the-options/
  998. if (dataType == 'script') (1,eval)(result)
  999. else if (dataType == 'xml') result = xhr.responseXML
  1000. else if (dataType == 'json') result = blankRE.test(result) ? null : $.parseJSON(result)
  1001. } catch (e) { error = e }
  1002. if (error) ajaxError(error, 'parsererror', xhr, settings, deferred)
  1003. else ajaxSuccess(result, xhr, settings, deferred)
  1004. } else {
  1005. ajaxError(xhr.statusText || null, xhr.status ? 'error' : 'abort', xhr, settings, deferred)
  1006. }
  1007. }
  1008. }
  1009. if (ajaxBeforeSend(xhr, settings) === false) {
  1010. xhr.abort()
  1011. ajaxError(null, 'abort', xhr, settings, deferred)
  1012. return xhr
  1013. }
  1014. if (settings.xhrFields) for (name in settings.xhrFields) xhr[name] = settings.xhrFields[name]
  1015. var async = 'async' in settings ? settings.async : true
  1016. xhr.open(settings.type, settings.url, async, settings.username, settings.password)
  1017. for (name in headers) nativeSetHeader.apply(xhr, headers[name])
  1018. if (settings.timeout > 0) abortTimeout = setTimeout(function(){
  1019. xhr.onreadystatechange = empty
  1020. xhr.abort()
  1021. ajaxError(null, 'timeout', xhr, settings, deferred)
  1022. }, settings.timeout)
  1023. // avoid sending empty string (#319)
  1024. xhr.send(settings.data ? settings.data : null)
  1025. return xhr
  1026. }
  1027. // handle optional data/success arguments
  1028. function parseArguments(url, data, success, dataType) {
  1029. var hasData = !$.isFunction(data)
  1030. return {
  1031. url: url,
  1032. data: hasData ? data : undefined,
  1033. success: !hasData ? data : $.isFunction(success) ? success : undefined,
  1034. dataType: hasData ? dataType || success : success
  1035. }
  1036. }
  1037. $.get = function(url, data, success, dataType){
  1038. return $.ajax(parseArguments.apply(null, arguments))
  1039. }
  1040. $.post = function(url, data, success, dataType){
  1041. var options = parseArguments.apply(null, arguments)
  1042. options.type = 'POST'
  1043. return $.ajax(options)
  1044. }
  1045. $.getJSON = function(url, data, success){
  1046. var options = parseArguments.apply(null, arguments)
  1047. options.dataType = 'json'
  1048. return $.ajax(options)
  1049. }
  1050. $.fn.load = function(url, data, success){
  1051. if (!this.length) return this
  1052. var self = this, parts = url.split(/\s/), selector,
  1053. options = parseArguments(url, data, success),
  1054. callback = options.success
  1055. if (parts.length > 1) options.url = parts[0], selector = parts[1]
  1056. options.success = function(response){
  1057. self.html(selector ?
  1058. $('<div>').html(response.replace(rscript, "")).find(selector)
  1059. : response)
  1060. callback && callback.apply(self, arguments)
  1061. }
  1062. $.ajax(options)
  1063. return this
  1064. }
  1065. var escape = encodeURIComponent
  1066. function serialize(params, obj, traditional, scope){
  1067. var type, array = $.isArray(obj), hash = $.isPlainObject(obj)
  1068. $.each(obj, function(key, value) {
  1069. type = $.type(value)
  1070. if (scope) key = traditional ? scope :
  1071. scope + '[' + (hash || type == 'object' || type == 'array' ? key : '') + ']'
  1072. // handle data in serializeArray() format
  1073. if (!scope && array) params.add(value.name, value.value)
  1074. // recurse into nested objects
  1075. else if (type == "array" || (!traditional && type == "object"))
  1076. serialize(params, value, traditional, key)
  1077. else params.add(key, value)
  1078. })
  1079. }
  1080. $.param = function(obj, traditional){
  1081. var params = []
  1082. params.add = function(k, v){ this.push(escape(k) + '=' + escape(v)) }
  1083. serialize(params, obj, traditional)
  1084. return params.join('&').replace(/%20/g, '+')
  1085. }
  1086. })(Zepto)
  1087. ;(function($){
  1088. var $$ = $.zepto.qsa, _zid = 1, undefined,
  1089. slice = Array.prototype.slice,
  1090. isFunction = $.isFunction,
  1091. isString = function(obj){ return typeof obj == 'string' },
  1092. handlers = {},
  1093. specialEvents={},
  1094. focusinSupported = 'onfocusin' in window,
  1095. focus = { focus: 'focusin', blur: 'focusout' },
  1096. hover = { mouseenter: 'mouseover', mouseleave: 'mouseout' }
  1097. specialEvents.click = specialEvents.mousedown = specialEvents.mouseup = specialEvents.mousemove = 'MouseEvents'
  1098. function zid(element) {
  1099. return element._zid || (element._zid = _zid++)
  1100. }
  1101. function findHandlers(element, event, fn, selector) {
  1102. event = parse(event)
  1103. if (event.ns) var matcher = matcherFor(event.ns)
  1104. return (handlers[zid(element)] || []).filter(function(handler) {
  1105. return handler
  1106. && (!event.e || handler.e == event.e)
  1107. && (!event.ns || matcher.test(handler.ns))
  1108. && (!fn || zid(handler.fn) === zid(fn))
  1109. && (!selector || handler.sel == selector)
  1110. })
  1111. }
  1112. function parse(event) {
  1113. var parts = ('' + event).split('.')
  1114. return {e: parts[0], ns: parts.slice(1).sort().join(' ')}
  1115. }
  1116. function matcherFor(ns) {
  1117. return new RegExp('(?:^| )' + ns.replace(' ', ' .* ?') + '(?: |$)')
  1118. }
  1119. function eventCapture(handler, captureSetting) {
  1120. return handler.del &&
  1121. (!focusinSupported && (handler.e in focus)) ||
  1122. !!captureSetting
  1123. }
  1124. function realEvent(type) {
  1125. return hover[type] || (focusinSupported && focus[type]) || type
  1126. }
  1127. function add(element, events, fn, data, selector, delegator, capture){
  1128. var id = zid(element), set = (handlers[id] || (handlers[id] = []))
  1129. events.split(/\s/).forEach(function(event){
  1130. if (event == 'ready') return $(document).ready(fn)
  1131. var handler = parse(event)
  1132. handler.fn = fn
  1133. handler.sel = selector
  1134. // emulate mouseenter, mouseleave
  1135. if (handler.e in hover) fn = function(e){
  1136. var related = e.relatedTarget
  1137. if (!related || (related !== this && !$.contains(this, related)))
  1138. return handler.fn.apply(this, arguments)
  1139. }
  1140. handler.del = delegator
  1141. var callback = delegator || fn
  1142. handler.proxy = function(e){
  1143. e = compatible(e)
  1144. if (e.isImmediatePropagationStopped()) return
  1145. e.data = data
  1146. var result = callback.apply(element, e._args == undefined ? [e] : [e].concat(e._args))
  1147. if (result === false) e.preventDefault(), e.stopPropagation()
  1148. return result
  1149. }
  1150. handler.i = set.length
  1151. set.push(handler)
  1152. if ('addEventListener' in element)
  1153. element.addEventListener(realEvent(handler.e), handler.proxy, eventCapture(handler, capture))
  1154. })
  1155. }
  1156. function remove(element, events, fn, selector, capture){
  1157. var id = zid(element)
  1158. ;(events || '').split(/\s/).forEach(function(event){
  1159. findHandlers(element, event, fn, selector).forEach(function(handler){
  1160. delete handlers[id][handler.i]
  1161. if ('removeEventListener' in element)
  1162. element.removeEventListener(realEvent(handler.e), handler.proxy, eventCapture(handler, capture))
  1163. })
  1164. })
  1165. }
  1166. $.event = { add: add, remove: remove }
  1167. $.proxy = function(fn, context) {
  1168. if (isFunction(fn)) {
  1169. var proxyFn = function(){ return fn.apply(context, arguments) }
  1170. proxyFn._zid = zid(fn)
  1171. return proxyFn
  1172. } else if (isString(context)) {
  1173. return $.proxy(fn[context], fn)
  1174. } else {
  1175. throw new TypeError("expected function")
  1176. }
  1177. }
  1178. $.fn.bind = function(event, data, callback){
  1179. return this.on(event, data, callback)
  1180. }
  1181. $.fn.unbind = function(event, callback){
  1182. return this.off(event, callback)
  1183. }
  1184. $.fn.one = function(event, selector, data, callback){
  1185. return this.on(event, selector, data, callback, 1)
  1186. }
  1187. var returnTrue = function(){return true},
  1188. returnFalse = function(){return false},
  1189. ignoreProperties = /^([A-Z]|returnValue$|layer[XY]$)/,
  1190. eventMethods = {
  1191. preventDefault: 'isDefaultPrevented',
  1192. stopImmediatePropagation: 'isImmediatePropagationStopped',
  1193. stopPropagation: 'isPropagationStopped'
  1194. }
  1195. function compatible(event, source) {
  1196. if (source || !event.isDefaultPrevented) {
  1197. source || (source = event)
  1198. $.each(eventMethods, function(name, predicate) {
  1199. var sourceMethod = source[name]
  1200. event[name] = function(){
  1201. this[predicate] = returnTrue
  1202. return sourceMethod && sourceMethod.apply(source, arguments)
  1203. }
  1204. event[predicate] = returnFalse
  1205. })
  1206. if (source.defaultPrevented !== undefined ? source.defaultPrevented :
  1207. 'returnValue' in source ? source.returnValue === false :
  1208. source.getPreventDefault && source.getPreventDefault())
  1209. event.isDefaultPrevented = returnTrue
  1210. }
  1211. return event
  1212. }
  1213. function createProxy(event) {
  1214. var key, proxy = { originalEvent: event }
  1215. for (key in event)
  1216. if (!ignoreProperties.test(key) && event[key] !== undefined) proxy[key] = event[key]
  1217. return compatible(proxy, event)
  1218. }
  1219. $.fn.delegate = function(selector, event, callback){
  1220. return this.on(event, selector, callback)
  1221. }
  1222. $.fn.undelegate = function(selector, event, callback){
  1223. return this.off(event, selector, callback)
  1224. }
  1225. $.fn.live = function(event, callback){
  1226. $(document.body).delegate(this.selector, event, callback)
  1227. return this
  1228. }
  1229. $.fn.die = function(event, callback){
  1230. $(document.body).undelegate(this.selector, event, callback)
  1231. return this
  1232. }
  1233. $.fn.on = function(event, selector, data, callback, one){
  1234. var autoRemove, delegator, $this = this
  1235. if (event && !isString(event)) {
  1236. $.each(event, function(type, fn){
  1237. $this.on(type, selector, data, fn, one)
  1238. })
  1239. return $this
  1240. }
  1241. if (!isString(selector) && !isFunction(callback) && callback !== false)
  1242. callback = data, data = selector, selector = undefined
  1243. if (isFunction(data) || data === false)
  1244. callback = data, data = undefined
  1245. if (callback === false) callback = returnFalse
  1246. return $this.each(function(_, element){
  1247. if (one) autoRemove = function(e){
  1248. remove(element, e.type, callback)
  1249. return callback.apply(this, arguments)
  1250. }
  1251. if (selector) delegator = function(e){
  1252. var evt, match = $(e.target).closest(selector, element).get(0)
  1253. if (match && match !== element) {
  1254. evt = $.extend(createProxy(e), {currentTarget: match, liveFired: element})
  1255. return (autoRemove || callback).apply(match, [evt].concat(slice.call(arguments, 1)))
  1256. }
  1257. }
  1258. add(element, event, callback, data, selector, delegator || autoRemove)
  1259. })
  1260. }
  1261. $.fn.off = function(event, selector, callback){
  1262. var $this = this
  1263. if (event && !isString(event)) {
  1264. $.each(event, function(type, fn){
  1265. $this.off(type, selector, fn)
  1266. })
  1267. return $this
  1268. }
  1269. if (!isString(selector) && !isFunction(callback) && callback !== false)
  1270. callback = selector, selector = undefined
  1271. if (callback === false) callback = returnFalse
  1272. return $this.each(function(){
  1273. remove(this, event, callback, selector)
  1274. })
  1275. }
  1276. $.fn.trigger = function(event, args){
  1277. event = (isString(event) || $.isPlainObject(event)) ? $.Event(event) : compatible(event)
  1278. event._args = args
  1279. return this.each(function(){
  1280. // items in the collection might not be DOM elements
  1281. if('dispatchEvent' in this) this.dispatchEvent(event)
  1282. else $(this).triggerHandler(event, args)
  1283. })
  1284. }
  1285. // triggers event handlers on current element just as if an event occurred,
  1286. // doesn't trigger an actual event, doesn't bubble
  1287. $.fn.triggerHandler = function(event, args){
  1288. var e, result
  1289. this.each(function(i, element){
  1290. e = createProxy(isString(event) ? $.Event(event) : event)
  1291. e._args = args
  1292. e.target = element
  1293. $.each(findHandlers(element, event.type || event), function(i, handler){
  1294. result = handler.proxy(e)
  1295. if (e.isImmediatePropagationStopped()) return false
  1296. })
  1297. })
  1298. return result
  1299. }
  1300. // shortcut methods for `.bind(event, fn)` for each event type
  1301. ;('focusin focusout load resize scroll unload click dblclick '+
  1302. 'mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave '+
  1303. 'change select keydown keypress keyup error').split(' ').forEach(function(event) {
  1304. $.fn[event] = function(callback) {
  1305. return callback ?
  1306. this.bind(event, callback) :
  1307. this.trigger(event)
  1308. }
  1309. })
  1310. ;['focus', 'blur'].forEach(function(name) {
  1311. $.fn[name] = function(callback) {
  1312. if (callback) this.bind(name, callback)
  1313. else this.each(function(){
  1314. try { this[name]() }
  1315. catch(e) {}
  1316. })
  1317. return this
  1318. }
  1319. })
  1320. $.Event = function(type, props) {
  1321. if (!isString(type)) props = type, type = props.type
  1322. var event = document.createEvent(specialEvents[type] || 'Events'), bubbles = true
  1323. if (props) for (var name in props) (name == 'bubbles') ? (bubbles = !!props[name]) : (event[name] = props[name])
  1324. event.initEvent(type, bubbles, true)
  1325. return compatible(event)
  1326. }
  1327. })(Zepto)
  1328. ;(function($, undefined){
  1329. var prefix = '', eventPrefix, endEventName, endAnimationName,
  1330. vendors = { Webkit: 'webkit', Moz: '', O: 'o' },
  1331. document = window.document, testEl = document.createElement('div'),
  1332. supportedTransforms = /^((translate|rotate|scale)(X|Y|Z|3d)?|matrix(3d)?|perspective|skew(X|Y)?)$/i,
  1333. transform,
  1334. transitionProperty, transitionDuration, transitionTiming, transitionDelay,
  1335. animationName, animationDuration, animationTiming, animationDelay,
  1336. cssReset = {}
  1337. function dasherize(str) { return str.replace(/([a-z])([A-Z])/, '$1-$2').toLowerCase() }
  1338. function normalizeEvent(name) { return eventPrefix ? eventPrefix + name : name.toLowerCase() }
  1339. $.each(vendors, function(vendor, event){
  1340. if (testEl.style[vendor + 'TransitionProperty'] !== undefined) {
  1341. prefix = '-' + vendor.toLowerCase() + '-'
  1342. eventPrefix = event
  1343. return false
  1344. }
  1345. })
  1346. transform = prefix + 'transform'
  1347. cssReset[transitionProperty = prefix + 'transition-property'] =
  1348. cssReset[transitionDuration = prefix + 'transition-duration'] =
  1349. cssReset[transitionDelay = prefix + 'transition-delay'] =
  1350. cssReset[transitionTiming = prefix + 'transition-timing-function'] =
  1351. cssReset[animationName = prefix + 'animation-name'] =
  1352. cssReset[animationDuration = prefix + 'animation-duration'] =
  1353. cssReset[animationDelay = prefix + 'animation-delay'] =
  1354. cssReset[animationTiming = prefix + 'animation-timing-function'] = ''
  1355. $.fx = {
  1356. off: (eventPrefix === undefined && testEl.style.transitionProperty === undefined),
  1357. speeds: { _default: 400, fast: 200, slow: 600 },
  1358. cssPrefix: prefix,
  1359. transitionEnd: normalizeEvent('TransitionEnd'),
  1360. animationEnd: normalizeEvent('AnimationEnd')
  1361. }
  1362. $.fn.animate = function(properties, duration, ease, callback, delay){
  1363. if ($.isFunction(duration))
  1364. callback = duration, ease = undefined, duration = undefined
  1365. if ($.isFunction(ease))
  1366. callback = ease, ease = undefined
  1367. if ($.isPlainObject(duration))
  1368. ease = duration.easing, callback = duration.complete, delay = duration.delay, duration = duration.duration
  1369. if (duration) duration = (typeof duration == 'number' ? duration :
  1370. ($.fx.speeds[duration] || $.fx.speeds._default)) / 1000
  1371. if (delay) delay = parseFloat(delay) / 1000
  1372. return this.anim(properties, duration, ease, callback, delay)
  1373. }
  1374. $.fn.anim = function(properties, duration, ease, callback, delay){
  1375. var key, cssValues = {}, cssProperties, transforms = '',
  1376. that = this, wrappedCallback, endEvent = $.fx.transitionEnd,
  1377. fired = false
  1378. if (duration === undefined) duration = $.fx.speeds._default / 1000
  1379. if (delay === undefined) delay = 0
  1380. if ($.fx.off) duration = 0
  1381. if (typeof properties == 'string') {
  1382. // keyframe animation
  1383. cssValues[animationName] = properties
  1384. cssValues[animationDuration] = duration + 's'
  1385. cssValues[animationDelay] = delay + 's'
  1386. cssValues[animationTiming] = (ease || 'linear')
  1387. endEvent = $.fx.animationEnd
  1388. } else {
  1389. cssProperties = []
  1390. // CSS transitions
  1391. for (key in properties)
  1392. if (supportedTransforms.test(key)) transforms += key + '(' + properties[key] + ') '
  1393. else cssValues[key] = properties[key], cssProperties.push(dasherize(key))
  1394. if (transforms) cssValues[transform] = transforms, cssProperties.push(transform)
  1395. if (duration > 0 && typeof properties === 'object') {
  1396. cssValues[transitionProperty] = cssProperties.join(', ')
  1397. cssValues[transitionDuration] = duration + 's'
  1398. cssValues[transitionDelay] = delay + 's'
  1399. cssValues[transitionTiming] = (ease || 'linear')
  1400. }
  1401. }
  1402. wrappedCallback = function(event){
  1403. if (typeof event !== 'undefined') {
  1404. if (event.target !== event.currentTarget) return // makes sure the event didn't bubble from "below"
  1405. $(event.target).unbind(endEvent, wrappedCallback)
  1406. } else
  1407. $(this).unbind(endEvent, wrappedCallback) // triggered by setTimeout
  1408. fired = true
  1409. $(this).css(cssReset)
  1410. callback && callback.call(this)
  1411. }
  1412. if (duration > 0){
  1413. this.bind(endEvent, wrappedCallback)
  1414. // transitionEnd is not always firing on older Android phones
  1415. // so make sure it gets fired
  1416. setTimeout(function(){
  1417. if (fired) return
  1418. wrappedCallback.call(that)
  1419. }, (duration * 1000) + 25)
  1420. }
  1421. // trigger page reflow so new elements can animate
  1422. this.size() && this.get(0).clientLeft
  1423. this.css(cssValues)
  1424. if (duration <= 0) setTimeout(function() {
  1425. that.each(function(){ wrappedCallback.call(this) })
  1426. }, 0)
  1427. return this
  1428. }
  1429. testEl = null
  1430. })(Zepto)
  1431. ;(function($, undefined){
  1432. var document = window.document, docElem = document.documentElement,
  1433. origShow = $.fn.show, origHide = $.fn.hide, origToggle = $.fn.toggle
  1434. function anim(el, speed, opacity, scale, callback) {
  1435. if (typeof speed == 'function' && !callback) callback = speed, speed = undefined
  1436. var props = { opacity: opacity }
  1437. if (scale) {
  1438. props.scale = scale
  1439. el.css($.fx.cssPrefix + 'transform-origin', '0 0')
  1440. }
  1441. return el.animate(props, speed, null, callback)
  1442. }
  1443. function hide(el, speed, scale, callback) {
  1444. return anim(el, speed, 0, scale, function(){
  1445. origHide.call($(this))
  1446. callback && callback.call(this)
  1447. })
  1448. }
  1449. $.fn.show = function(speed, callback) {
  1450. origShow.call(this)
  1451. if (speed === undefined) speed = 0
  1452. else this.css('opacity', 0)
  1453. return anim(this, speed, 1, '1,1', callback)
  1454. }
  1455. $.fn.hide = function(speed, callback) {
  1456. if (speed === undefined) return origHide.call(this)
  1457. else return hide(this, speed, '0,0', callback)
  1458. }
  1459. $.fn.toggle = function(speed, callback) {
  1460. if (speed === undefined || typeof speed == 'boolean')
  1461. return origToggle.call(this, speed)
  1462. else return this.each(function(){
  1463. var el = $(this)
  1464. el[el.css('display') == 'none' ? 'show' : 'hide'](speed, callback)
  1465. })
  1466. }
  1467. $.fn.fadeTo = function(speed, opacity, callback) {
  1468. return anim(this, speed, opacity, null, callback)
  1469. }
  1470. $.fn.fadeIn = function(speed, callback) {
  1471. var target = this.css('opacity')
  1472. if (target > 0) this.css('opacity', 0)
  1473. else target = 1
  1474. return origShow.call(this).fadeTo(speed, target, callback)
  1475. }
  1476. $.fn.fadeOut = function(speed, callback) {
  1477. return hide(this, speed, null, callback)
  1478. }
  1479. $.fn.fadeToggle = function(speed, callback) {
  1480. return this.each(function(){
  1481. var el = $(this)
  1482. el[
  1483. (el.css('opacity') == 0 || el.css('display') == 'none') ? 'fadeIn' : 'fadeOut'
  1484. ](speed, callback)
  1485. })
  1486. }
  1487. })(Zepto)
  1488. ;(function($){
  1489. var zepto = $.zepto, oldQsa = zepto.qsa, oldMatches = zepto.matches
  1490. function visible(elem){
  1491. elem = $(elem)
  1492. return !!(elem.width() || elem.height()) && elem.css("display") !== "none"
  1493. }
  1494. // Implements a subset from:
  1495. // http://api.jquery.com/category/selectors/jquery-selector-extensions/
  1496. //
  1497. // Each filter function receives the current index, all nodes in the
  1498. // considered set, and a value if there were parentheses. The value
  1499. // of `this` is the node currently being considered. The function returns the
  1500. // resulting node(s), null, or undefined.
  1501. //
  1502. // Complex selectors are not supported:
  1503. // li:has(label:contains("foo")) + li:has(label:contains("bar"))
  1504. // ul.inner:first > li
  1505. var filters = $.expr[':'] = {
  1506. visible: function(){ if (visible(this)) return this },
  1507. hidden: function(){ if (!visible(this)) return this },
  1508. selected: function(){ if (this.selected) return this },
  1509. checked: function(){ if (this.checked) return this },
  1510. parent: function(){ return this.parentNode },
  1511. first: function(idx){ if (idx === 0) return this },
  1512. last: function(idx, nodes){ if (idx === nodes.length - 1) return this },
  1513. eq: function(idx, _, value){ if (idx === value) return this },
  1514. contains: function(idx, _, text){ if ($(this).text().indexOf(text) > -1) return this },
  1515. has: function(idx, _, sel){ if (zepto.qsa(this, sel).length) return this }
  1516. }
  1517. var filterRe = new RegExp('(.*):(\\w+)(?:\\(([^)]+)\\))?$\\s*'),
  1518. childRe = /^\s*>/,
  1519. classTag = 'Zepto' + (+new Date())
  1520. function process(sel, fn) {
  1521. // quote the hash in `a[href^=#]` expression
  1522. sel = sel.replace(/=#\]/g, '="#"]')
  1523. var filter, arg, match = filterRe.exec(sel)
  1524. if (match && match[2] in filters) {
  1525. filter = filters[match[2]], arg = match[3]
  1526. sel = match[1]
  1527. if (arg) {
  1528. var num = Number(arg)
  1529. if (isNaN(num)) arg = arg.replace(/^["']|["']$/g, '')
  1530. else arg = num
  1531. }
  1532. }
  1533. return fn(sel, filter, arg)
  1534. }
  1535. zepto.qsa = function(node, selector) {
  1536. return process(selector, function(sel, filter, arg){
  1537. try {
  1538. var taggedParent
  1539. if (!sel && filter) sel = '*'
  1540. else if (childRe.test(sel))
  1541. // support "> *" child queries by tagging the parent node with a
  1542. // unique class and prepending that classname onto the selector
  1543. taggedParent = $(node).addClass(classTag), sel = '.'+classTag+' '+sel
  1544. var nodes = oldQsa(node, sel)
  1545. } catch(e) {
  1546. console.error('error performing selector: %o', selector)
  1547. throw e
  1548. } finally {
  1549. if (taggedParent) taggedParent.removeClass(classTag)
  1550. }
  1551. return !filter ? nodes :
  1552. zepto.uniq($.map(nodes, function(n, i){ return filter.call(n, i, nodes, arg) }))
  1553. })
  1554. }
  1555. zepto.matches = function(node, selector){
  1556. return process(selector, function(sel, filter, arg){
  1557. return (!sel || oldMatches(node, sel)) &&
  1558. (!filter || filter.call(node, null, arg) === node)
  1559. })
  1560. }
  1561. })(Zepto)
  1562. ;(function($){
  1563. var touch = {},
  1564. touchTimeout, tapTimeout, swipeTimeout, longTapTimeout,
  1565. longTapDelay = 750,
  1566. gesture
  1567. function swipeDirection(x1, x2, y1, y2) {
  1568. return Math.abs(x1 - x2) >=
  1569. Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down')
  1570. }
  1571. function longTap() {
  1572. longTapTimeout = null
  1573. if (touch.last) {
  1574. touch.el.trigger('longTap')
  1575. touch = {}
  1576. }
  1577. }
  1578. function cancelLongTap() {
  1579. if (longTapTimeout) clearTimeout(longTapTimeout)
  1580. longTapTimeout = null
  1581. }
  1582. function cancelAll() {
  1583. if (touchTimeout) clearTimeout(touchTimeout)
  1584. if (tapTimeout) clearTimeout(tapTimeout)
  1585. if (swipeTimeout) clearTimeout(swipeTimeout)
  1586. if (longTapTimeout) clearTimeout(longTapTimeout)
  1587. touchTimeout = tapTimeout = swipeTimeout = longTapTimeout = null
  1588. touch = {}
  1589. }
  1590. function isPrimaryTouch(event){
  1591. return (event.pointerType == 'touch' ||
  1592. event.pointerType == event.MSPOINTER_TYPE_TOUCH)
  1593. && event.isPrimary
  1594. }
  1595. function isPointerEventType(e, type){
  1596. return (e.type == 'pointer'+type ||
  1597. e.type.toLowerCase() == 'mspointer'+type)
  1598. }
  1599. $(document).ready(function(){
  1600. var now, delta, deltaX = 0, deltaY = 0, firstTouch, _isPointerType
  1601. if ('MSGesture' in window) {
  1602. gesture = new MSGesture()
  1603. gesture.target = document.body
  1604. }
  1605. $(document)
  1606. .bind('MSGestureEnd', function(e){
  1607. var swipeDirectionFromVelocity =
  1608. e.velocityX > 1 ? 'Right' : e.velocityX < -1 ? 'Left' : e.velocityY > 1 ? 'Down' : e.velocityY < -1 ? 'Up' : null;
  1609. if (swipeDirectionFromVelocity) {
  1610. touch.el.trigger('swipe')
  1611. touch.el.trigger('swipe'+ swipeDirectionFromVelocity)
  1612. }
  1613. })
  1614. .on('touchstart MSPointerDown pointerdown', function(e){
  1615. if((_isPointerType = isPointerEventType(e, 'down')) &&
  1616. !isPrimaryTouch(e)) return
  1617. firstTouch = _isPointerType ? e : e.touches[0]
  1618. if (e.touches && e.touches.length === 1 && touch.x2) {
  1619. // Clear out touch movement data if we have it sticking around
  1620. // This can occur if touchcancel doesn't fire due to preventDefault, etc.
  1621. touch.x2 = undefined
  1622. touch.y2 = undefined
  1623. }
  1624. now = Date.now()
  1625. delta = now - (touch.last || now)
  1626. touch.el = $('tagName' in firstTouch.target ?
  1627. firstTouch.target : firstTouch.target.parentNode)
  1628. touchTimeout && clearTimeout(touchTimeout)
  1629. touch.x1 = firstTouch.pageX
  1630. touch.y1 = firstTouch.pageY
  1631. if (delta > 0 && delta <= 250) touch.isDoubleTap = true
  1632. touch.last = now
  1633. longTapTimeout = setTimeout(longTap, longTapDelay)
  1634. // adds the current touch contact for IE gesture recognition
  1635. if (gesture && _isPointerType) gesture.addPointer(e.pointerId);
  1636. })
  1637. .on('touchmove MSPointerMove pointermove', function(e){
  1638. if((_isPointerType = isPointerEventType(e, 'move')) &&
  1639. !isPrimaryTouch(e)) return
  1640. firstTouch = _isPointerType ? e : e.touches[0]
  1641. cancelLongTap()
  1642. touch.x2 = firstTouch.pageX
  1643. touch.y2 = firstTouch.pageY
  1644. deltaX += Math.abs(touch.x1 - touch.x2)
  1645. deltaY += Math.abs(touch.y1 - touch.y2)
  1646. })
  1647. .on('touchend MSPointerUp pointerup', function(e){
  1648. if((_isPointerType = isPointerEventType(e, 'up')) &&
  1649. !isPrimaryTouch(e)) return
  1650. cancelLongTap()
  1651. // swipe
  1652. if ((touch.x2 && Math.abs(touch.x1 - touch.x2) > 30) ||
  1653. (touch.y2 && Math.abs(touch.y1 - touch.y2) > 30))
  1654. swipeTimeout = setTimeout(function() {
  1655. touch.el.trigger('swipe')
  1656. touch.el.trigger('swipe' + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)))
  1657. touch = {}
  1658. }, 0)
  1659. // normal tap
  1660. else if ('last' in touch)
  1661. // don't fire tap when delta position changed by more than 30 pixels,
  1662. // for instance when moving to a point and back to origin
  1663. if (deltaX < 30 && deltaY < 30) {
  1664. // delay by one tick so we can cancel the 'tap' event if 'scroll' fires
  1665. // ('tap' fires before 'scroll')
  1666. tapTimeout = setTimeout(function() {
  1667. // trigger universal 'tap' with the option to cancelTouch()
  1668. // (cancelTouch cancels processing of single vs double taps for faster 'tap' response)
  1669. var event = $.Event('tap')
  1670. event.cancelTouch = cancelAll
  1671. touch.el.trigger(event)
  1672. // trigger double tap immediately
  1673. if (touch.isDoubleTap) {
  1674. if (touch.el) touch.el.trigger('doubleTap')
  1675. touch = {}
  1676. }
  1677. // trigger single tap after 250ms of inactivity
  1678. else {
  1679. touchTimeout = setTimeout(function(){
  1680. touchTimeout = null
  1681. if (touch.el) touch.el.trigger('singleTap')
  1682. touch = {}
  1683. }, 250)
  1684. }
  1685. }, 0)
  1686. } else {
  1687. touch = {}
  1688. }
  1689. deltaX = deltaY = 0
  1690. })
  1691. // when the browser window loses focus,
  1692. // for example when a modal dialog is shown,
  1693. // cancel all ongoing events
  1694. .on('touchcancel MSPointerCancel pointercancel', cancelAll)
  1695. // scrolling the window indicates intention of the user
  1696. // to scroll, not tap or swipe, so cancel all ongoing events
  1697. $(window).on('scroll', cancelAll)
  1698. })
  1699. ;['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown',
  1700. 'doubleTap', 'tap', 'singleTap', 'longTap'].forEach(function(eventName){
  1701. $.fn[eventName] = function(callback){ return this.on(eventName, callback) }
  1702. })
  1703. })(Zepto)