zepto.js 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. /* Zepto v1.1.2 - zepto event ajax form ie - 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 $$ = $.zepto.qsa, _zid = 1, undefined,
  799. slice = Array.prototype.slice,
  800. isFunction = $.isFunction,
  801. isString = function(obj){ return typeof obj == 'string' },
  802. handlers = {},
  803. specialEvents={},
  804. focusinSupported = 'onfocusin' in window,
  805. focus = { focus: 'focusin', blur: 'focusout' },
  806. hover = { mouseenter: 'mouseover', mouseleave: 'mouseout' }
  807. specialEvents.click = specialEvents.mousedown = specialEvents.mouseup = specialEvents.mousemove = 'MouseEvents'
  808. function zid(element) {
  809. return element._zid || (element._zid = _zid++)
  810. }
  811. function findHandlers(element, event, fn, selector) {
  812. event = parse(event)
  813. if (event.ns) var matcher = matcherFor(event.ns)
  814. return (handlers[zid(element)] || []).filter(function(handler) {
  815. return handler
  816. && (!event.e || handler.e == event.e)
  817. && (!event.ns || matcher.test(handler.ns))
  818. && (!fn || zid(handler.fn) === zid(fn))
  819. && (!selector || handler.sel == selector)
  820. })
  821. }
  822. function parse(event) {
  823. var parts = ('' + event).split('.')
  824. return {e: parts[0], ns: parts.slice(1).sort().join(' ')}
  825. }
  826. function matcherFor(ns) {
  827. return new RegExp('(?:^| )' + ns.replace(' ', ' .* ?') + '(?: |$)')
  828. }
  829. function eventCapture(handler, captureSetting) {
  830. return handler.del &&
  831. (!focusinSupported && (handler.e in focus)) ||
  832. !!captureSetting
  833. }
  834. function realEvent(type) {
  835. return hover[type] || (focusinSupported && focus[type]) || type
  836. }
  837. function add(element, events, fn, data, selector, delegator, capture){
  838. var id = zid(element), set = (handlers[id] || (handlers[id] = []))
  839. events.split(/\s/).forEach(function(event){
  840. if (event == 'ready') return $(document).ready(fn)
  841. var handler = parse(event)
  842. handler.fn = fn
  843. handler.sel = selector
  844. // emulate mouseenter, mouseleave
  845. if (handler.e in hover) fn = function(e){
  846. var related = e.relatedTarget
  847. if (!related || (related !== this && !$.contains(this, related)))
  848. return handler.fn.apply(this, arguments)
  849. }
  850. handler.del = delegator
  851. var callback = delegator || fn
  852. handler.proxy = function(e){
  853. e = compatible(e)
  854. if (e.isImmediatePropagationStopped()) return
  855. e.data = data
  856. var result = callback.apply(element, e._args == undefined ? [e] : [e].concat(e._args))
  857. if (result === false) e.preventDefault(), e.stopPropagation()
  858. return result
  859. }
  860. handler.i = set.length
  861. set.push(handler)
  862. if ('addEventListener' in element)
  863. element.addEventListener(realEvent(handler.e), handler.proxy, eventCapture(handler, capture))
  864. })
  865. }
  866. function remove(element, events, fn, selector, capture){
  867. var id = zid(element)
  868. ;(events || '').split(/\s/).forEach(function(event){
  869. findHandlers(element, event, fn, selector).forEach(function(handler){
  870. delete handlers[id][handler.i]
  871. if ('removeEventListener' in element)
  872. element.removeEventListener(realEvent(handler.e), handler.proxy, eventCapture(handler, capture))
  873. })
  874. })
  875. }
  876. $.event = { add: add, remove: remove }
  877. $.proxy = function(fn, context) {
  878. if (isFunction(fn)) {
  879. var proxyFn = function(){ return fn.apply(context, arguments) }
  880. proxyFn._zid = zid(fn)
  881. return proxyFn
  882. } else if (isString(context)) {
  883. return $.proxy(fn[context], fn)
  884. } else {
  885. throw new TypeError("expected function")
  886. }
  887. }
  888. $.fn.bind = function(event, data, callback){
  889. return this.on(event, data, callback)
  890. }
  891. $.fn.unbind = function(event, callback){
  892. return this.off(event, callback)
  893. }
  894. $.fn.one = function(event, selector, data, callback){
  895. return this.on(event, selector, data, callback, 1)
  896. }
  897. var returnTrue = function(){return true},
  898. returnFalse = function(){return false},
  899. ignoreProperties = /^([A-Z]|returnValue$|layer[XY]$)/,
  900. eventMethods = {
  901. preventDefault: 'isDefaultPrevented',
  902. stopImmediatePropagation: 'isImmediatePropagationStopped',
  903. stopPropagation: 'isPropagationStopped'
  904. }
  905. function compatible(event, source) {
  906. if (source || !event.isDefaultPrevented) {
  907. source || (source = event)
  908. $.each(eventMethods, function(name, predicate) {
  909. var sourceMethod = source[name]
  910. event[name] = function(){
  911. this[predicate] = returnTrue
  912. return sourceMethod && sourceMethod.apply(source, arguments)
  913. }
  914. event[predicate] = returnFalse
  915. })
  916. if (source.defaultPrevented !== undefined ? source.defaultPrevented :
  917. 'returnValue' in source ? source.returnValue === false :
  918. source.getPreventDefault && source.getPreventDefault())
  919. event.isDefaultPrevented = returnTrue
  920. }
  921. return event
  922. }
  923. function createProxy(event) {
  924. var key, proxy = { originalEvent: event }
  925. for (key in event)
  926. if (!ignoreProperties.test(key) && event[key] !== undefined) proxy[key] = event[key]
  927. return compatible(proxy, event)
  928. }
  929. $.fn.delegate = function(selector, event, callback){
  930. return this.on(event, selector, callback)
  931. }
  932. $.fn.undelegate = function(selector, event, callback){
  933. return this.off(event, selector, callback)
  934. }
  935. $.fn.live = function(event, callback){
  936. $(document.body).delegate(this.selector, event, callback)
  937. return this
  938. }
  939. $.fn.die = function(event, callback){
  940. $(document.body).undelegate(this.selector, event, callback)
  941. return this
  942. }
  943. $.fn.on = function(event, selector, data, callback, one){
  944. var autoRemove, delegator, $this = this
  945. if (event && !isString(event)) {
  946. $.each(event, function(type, fn){
  947. $this.on(type, selector, data, fn, one)
  948. })
  949. return $this
  950. }
  951. if (!isString(selector) && !isFunction(callback) && callback !== false)
  952. callback = data, data = selector, selector = undefined
  953. if (isFunction(data) || data === false)
  954. callback = data, data = undefined
  955. if (callback === false) callback = returnFalse
  956. return $this.each(function(_, element){
  957. if (one) autoRemove = function(e){
  958. remove(element, e.type, callback)
  959. return callback.apply(this, arguments)
  960. }
  961. if (selector) delegator = function(e){
  962. var evt, match = $(e.target).closest(selector, element).get(0)
  963. if (match && match !== element) {
  964. evt = $.extend(createProxy(e), {currentTarget: match, liveFired: element})
  965. return (autoRemove || callback).apply(match, [evt].concat(slice.call(arguments, 1)))
  966. }
  967. }
  968. add(element, event, callback, data, selector, delegator || autoRemove)
  969. })
  970. }
  971. $.fn.off = function(event, selector, callback){
  972. var $this = this
  973. if (event && !isString(event)) {
  974. $.each(event, function(type, fn){
  975. $this.off(type, selector, fn)
  976. })
  977. return $this
  978. }
  979. if (!isString(selector) && !isFunction(callback) && callback !== false)
  980. callback = selector, selector = undefined
  981. if (callback === false) callback = returnFalse
  982. return $this.each(function(){
  983. remove(this, event, callback, selector)
  984. })
  985. }
  986. $.fn.trigger = function(event, args){
  987. event = (isString(event) || $.isPlainObject(event)) ? $.Event(event) : compatible(event)
  988. event._args = args
  989. return this.each(function(){
  990. // items in the collection might not be DOM elements
  991. if('dispatchEvent' in this) this.dispatchEvent(event)
  992. else $(this).triggerHandler(event, args)
  993. })
  994. }
  995. // triggers event handlers on current element just as if an event occurred,
  996. // doesn't trigger an actual event, doesn't bubble
  997. $.fn.triggerHandler = function(event, args){
  998. var e, result
  999. this.each(function(i, element){
  1000. e = createProxy(isString(event) ? $.Event(event) : event)
  1001. e._args = args
  1002. e.target = element
  1003. $.each(findHandlers(element, event.type || event), function(i, handler){
  1004. result = handler.proxy(e)
  1005. if (e.isImmediatePropagationStopped()) return false
  1006. })
  1007. })
  1008. return result
  1009. }
  1010. // shortcut methods for `.bind(event, fn)` for each event type
  1011. ;('focusin focusout load resize scroll unload click dblclick '+
  1012. 'mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave '+
  1013. 'change select keydown keypress keyup error').split(' ').forEach(function(event) {
  1014. $.fn[event] = function(callback) {
  1015. return callback ?
  1016. this.bind(event, callback) :
  1017. this.trigger(event)
  1018. }
  1019. })
  1020. ;['focus', 'blur'].forEach(function(name) {
  1021. $.fn[name] = function(callback) {
  1022. if (callback) this.bind(name, callback)
  1023. else this.each(function(){
  1024. try { this[name]() }
  1025. catch(e) {}
  1026. })
  1027. return this
  1028. }
  1029. })
  1030. $.Event = function(type, props) {
  1031. if (!isString(type)) props = type, type = props.type
  1032. var event = document.createEvent(specialEvents[type] || 'Events'), bubbles = true
  1033. if (props) for (var name in props) (name == 'bubbles') ? (bubbles = !!props[name]) : (event[name] = props[name])
  1034. event.initEvent(type, bubbles, true)
  1035. return compatible(event)
  1036. }
  1037. })(Zepto)
  1038. ;(function($){
  1039. var jsonpID = 0,
  1040. document = window.document,
  1041. key,
  1042. name,
  1043. rscript = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
  1044. scriptTypeRE = /^(?:text|application)\/javascript/i,
  1045. xmlTypeRE = /^(?:text|application)\/xml/i,
  1046. jsonType = 'application/json',
  1047. htmlType = 'text/html',
  1048. blankRE = /^\s*$/
  1049. // trigger a custom event and return false if it was cancelled
  1050. function triggerAndReturn(context, eventName, data) {
  1051. var event = $.Event(eventName)
  1052. $(context).trigger(event, data)
  1053. return !event.isDefaultPrevented()
  1054. }
  1055. // trigger an Ajax "global" event
  1056. function triggerGlobal(settings, context, eventName, data) {
  1057. if (settings.global) return triggerAndReturn(context || document, eventName, data)
  1058. }
  1059. // Number of active Ajax requests
  1060. $.active = 0
  1061. function ajaxStart(settings) {
  1062. if (settings.global && $.active++ === 0) triggerGlobal(settings, null, 'ajaxStart')
  1063. }
  1064. function ajaxStop(settings) {
  1065. if (settings.global && !(--$.active)) triggerGlobal(settings, null, 'ajaxStop')
  1066. }
  1067. // triggers an extra global event "ajaxBeforeSend" that's like "ajaxSend" but cancelable
  1068. function ajaxBeforeSend(xhr, settings) {
  1069. var context = settings.context
  1070. if (settings.beforeSend.call(context, xhr, settings) === false ||
  1071. triggerGlobal(settings, context, 'ajaxBeforeSend', [xhr, settings]) === false)
  1072. return false
  1073. triggerGlobal(settings, context, 'ajaxSend', [xhr, settings])
  1074. }
  1075. function ajaxSuccess(data, xhr, settings, deferred) {
  1076. var context = settings.context, status = 'success'
  1077. settings.success.call(context, data, status, xhr)
  1078. if (deferred) deferred.resolveWith(context, [data, status, xhr])
  1079. triggerGlobal(settings, context, 'ajaxSuccess', [xhr, settings, data])
  1080. ajaxComplete(status, xhr, settings)
  1081. }
  1082. // type: "timeout", "error", "abort", "parsererror"
  1083. function ajaxError(error, type, xhr, settings, deferred) {
  1084. var context = settings.context
  1085. settings.error.call(context, xhr, type, error)
  1086. if (deferred) deferred.rejectWith(context, [xhr, type, error])
  1087. triggerGlobal(settings, context, 'ajaxError', [xhr, settings, error || type])
  1088. ajaxComplete(type, xhr, settings)
  1089. }
  1090. // status: "success", "notmodified", "error", "timeout", "abort", "parsererror"
  1091. function ajaxComplete(status, xhr, settings) {
  1092. var context = settings.context
  1093. settings.complete.call(context, xhr, status)
  1094. triggerGlobal(settings, context, 'ajaxComplete', [xhr, settings])
  1095. ajaxStop(settings)
  1096. }
  1097. // Empty function, used as default callback
  1098. function empty() {}
  1099. $.ajaxJSONP = function(options, deferred){
  1100. if (!('type' in options)) return $.ajax(options)
  1101. var _callbackName = options.jsonpCallback,
  1102. callbackName = ($.isFunction(_callbackName) ?
  1103. _callbackName() : _callbackName) || ('jsonp' + (++jsonpID)),
  1104. script = document.createElement('script'),
  1105. originalCallback = window[callbackName],
  1106. responseData,
  1107. abort = function(errorType) {
  1108. $(script).triggerHandler('error', errorType || 'abort')
  1109. },
  1110. xhr = { abort: abort }, abortTimeout
  1111. if (deferred) deferred.promise(xhr)
  1112. $(script).on('load error', function(e, errorType){
  1113. clearTimeout(abortTimeout)
  1114. $(script).off().remove()
  1115. if (e.type == 'error' || !responseData) {
  1116. ajaxError(null, errorType || 'error', xhr, options, deferred)
  1117. } else {
  1118. ajaxSuccess(responseData[0], xhr, options, deferred)
  1119. }
  1120. window[callbackName] = originalCallback
  1121. if (responseData && $.isFunction(originalCallback))
  1122. originalCallback(responseData[0])
  1123. originalCallback = responseData = undefined
  1124. })
  1125. if (ajaxBeforeSend(xhr, options) === false) {
  1126. abort('abort')
  1127. return xhr
  1128. }
  1129. window[callbackName] = function(){
  1130. responseData = arguments
  1131. }
  1132. script.src = options.url.replace(/=\?/, '=' + callbackName)
  1133. document.head.appendChild(script)
  1134. if (options.timeout > 0) abortTimeout = setTimeout(function(){
  1135. abort('timeout')
  1136. }, options.timeout)
  1137. return xhr
  1138. }
  1139. $.ajaxSettings = {
  1140. // Default type of request
  1141. type: 'GET',
  1142. // Callback that is executed before request
  1143. beforeSend: empty,
  1144. // Callback that is executed if the request succeeds
  1145. success: empty,
  1146. // Callback that is executed the the server drops error
  1147. error: empty,
  1148. // Callback that is executed on request complete (both: error and success)
  1149. complete: empty,
  1150. // The context for the callbacks
  1151. context: null,
  1152. // Whether to trigger "global" Ajax events
  1153. global: true,
  1154. // Transport
  1155. xhr: function () {
  1156. return new window.XMLHttpRequest()
  1157. },
  1158. // MIME types mapping
  1159. // IIS returns Javascript as "application/x-javascript"
  1160. accepts: {
  1161. script: 'text/javascript, application/javascript, application/x-javascript',
  1162. json: jsonType,
  1163. xml: 'application/xml, text/xml',
  1164. html: htmlType,
  1165. text: 'text/plain'
  1166. },
  1167. // Whether the request is to another domain
  1168. crossDomain: false,
  1169. // Default timeout
  1170. timeout: 0,
  1171. // Whether data should be serialized to string
  1172. processData: true,
  1173. // Whether the browser should be allowed to cache GET responses
  1174. cache: true
  1175. }
  1176. function mimeToDataType(mime) {
  1177. if (mime) mime = mime.split(';', 2)[0]
  1178. return mime && ( mime == htmlType ? 'html' :
  1179. mime == jsonType ? 'json' :
  1180. scriptTypeRE.test(mime) ? 'script' :
  1181. xmlTypeRE.test(mime) && 'xml' ) || 'text'
  1182. }
  1183. function appendQuery(url, query) {
  1184. if (query == '') return url
  1185. return (url + '&' + query).replace(/[&?]{1,2}/, '?')
  1186. }
  1187. // serialize payload and append it to the URL for GET requests
  1188. function serializeData(options) {
  1189. if (options.processData && options.data && $.type(options.data) != "string")
  1190. options.data = $.param(options.data, options.traditional)
  1191. if (options.data && (!options.type || options.type.toUpperCase() == 'GET'))
  1192. options.url = appendQuery(options.url, options.data), options.data = undefined
  1193. }
  1194. $.ajax = function(options){
  1195. var settings = $.extend({}, options || {}),
  1196. deferred = $.Deferred && $.Deferred()
  1197. for (key in $.ajaxSettings) if (settings[key] === undefined) settings[key] = $.ajaxSettings[key]
  1198. ajaxStart(settings)
  1199. if (!settings.crossDomain) settings.crossDomain = /^([\w-]+:)?\/\/([^\/]+)/.test(settings.url) &&
  1200. RegExp.$2 != window.location.host
  1201. if (!settings.url) settings.url = window.location.toString()
  1202. serializeData(settings)
  1203. if (settings.cache === false) settings.url = appendQuery(settings.url, '_=' + Date.now())
  1204. var dataType = settings.dataType, hasPlaceholder = /=\?/.test(settings.url)
  1205. if (dataType == 'jsonp' || hasPlaceholder) {
  1206. if (!hasPlaceholder)
  1207. settings.url = appendQuery(settings.url,
  1208. settings.jsonp ? (settings.jsonp + '=?') : settings.jsonp === false ? '' : 'callback=?')
  1209. return $.ajaxJSONP(settings, deferred)
  1210. }
  1211. var mime = settings.accepts[dataType],
  1212. headers = { },
  1213. setHeader = function(name, value) { headers[name.toLowerCase()] = [name, value] },
  1214. protocol = /^([\w-]+:)\/\//.test(settings.url) ? RegExp.$1 : window.location.protocol,
  1215. xhr = settings.xhr(),
  1216. nativeSetHeader = xhr.setRequestHeader,
  1217. abortTimeout
  1218. if (deferred) deferred.promise(xhr)
  1219. if (!settings.crossDomain) setHeader('X-Requested-With', 'XMLHttpRequest')
  1220. setHeader('Accept', mime || '*/*')
  1221. if (mime = settings.mimeType || mime) {
  1222. if (mime.indexOf(',') > -1) mime = mime.split(',', 2)[0]
  1223. xhr.overrideMimeType && xhr.overrideMimeType(mime)
  1224. }
  1225. if (settings.contentType || (settings.contentType !== false && settings.data && settings.type.toUpperCase() != 'GET'))
  1226. setHeader('Content-Type', settings.contentType || 'application/x-www-form-urlencoded')
  1227. if (settings.headers) for (name in settings.headers) setHeader(name, settings.headers[name])
  1228. xhr.setRequestHeader = setHeader
  1229. xhr.onreadystatechange = function(){
  1230. if (xhr.readyState == 4) {
  1231. xhr.onreadystatechange = empty
  1232. clearTimeout(abortTimeout)
  1233. var result, error = false
  1234. if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304 || (xhr.status == 0 && protocol == 'file:')) {
  1235. dataType = dataType || mimeToDataType(settings.mimeType || xhr.getResponseHeader('content-type'))
  1236. result = xhr.responseText
  1237. try {
  1238. // http://perfectionkills.com/global-eval-what-are-the-options/
  1239. if (dataType == 'script') (1,eval)(result)
  1240. else if (dataType == 'xml') result = xhr.responseXML
  1241. else if (dataType == 'json') result = blankRE.test(result) ? null : $.parseJSON(result)
  1242. } catch (e) { error = e }
  1243. if (error) ajaxError(error, 'parsererror', xhr, settings, deferred)
  1244. else ajaxSuccess(result, xhr, settings, deferred)
  1245. } else {
  1246. ajaxError(xhr.statusText || null, xhr.status ? 'error' : 'abort', xhr, settings, deferred)
  1247. }
  1248. }
  1249. }
  1250. if (ajaxBeforeSend(xhr, settings) === false) {
  1251. xhr.abort()
  1252. ajaxError(null, 'abort', xhr, settings, deferred)
  1253. return xhr
  1254. }
  1255. if (settings.xhrFields) for (name in settings.xhrFields) xhr[name] = settings.xhrFields[name]
  1256. var async = 'async' in settings ? settings.async : true
  1257. xhr.open(settings.type, settings.url, async, settings.username, settings.password)
  1258. for (name in headers) nativeSetHeader.apply(xhr, headers[name])
  1259. if (settings.timeout > 0) abortTimeout = setTimeout(function(){
  1260. xhr.onreadystatechange = empty
  1261. xhr.abort()
  1262. ajaxError(null, 'timeout', xhr, settings, deferred)
  1263. }, settings.timeout)
  1264. // avoid sending empty string (#319)
  1265. xhr.send(settings.data ? settings.data : null)
  1266. return xhr
  1267. }
  1268. // handle optional data/success arguments
  1269. function parseArguments(url, data, success, dataType) {
  1270. var hasData = !$.isFunction(data)
  1271. return {
  1272. url: url,
  1273. data: hasData ? data : undefined,
  1274. success: !hasData ? data : $.isFunction(success) ? success : undefined,
  1275. dataType: hasData ? dataType || success : success
  1276. }
  1277. }
  1278. $.get = function(url, data, success, dataType){
  1279. return $.ajax(parseArguments.apply(null, arguments))
  1280. }
  1281. $.post = function(url, data, success, dataType){
  1282. var options = parseArguments.apply(null, arguments)
  1283. options.type = 'POST'
  1284. return $.ajax(options)
  1285. }
  1286. $.getJSON = function(url, data, success){
  1287. var options = parseArguments.apply(null, arguments)
  1288. options.dataType = 'json'
  1289. return $.ajax(options)
  1290. }
  1291. $.fn.load = function(url, data, success){
  1292. if (!this.length) return this
  1293. var self = this, parts = url.split(/\s/), selector,
  1294. options = parseArguments(url, data, success),
  1295. callback = options.success
  1296. if (parts.length > 1) options.url = parts[0], selector = parts[1]
  1297. options.success = function(response){
  1298. self.html(selector ?
  1299. $('<div>').html(response.replace(rscript, "")).find(selector)
  1300. : response)
  1301. callback && callback.apply(self, arguments)
  1302. }
  1303. $.ajax(options)
  1304. return this
  1305. }
  1306. var escape = encodeURIComponent
  1307. function serialize(params, obj, traditional, scope){
  1308. var type, array = $.isArray(obj), hash = $.isPlainObject(obj)
  1309. $.each(obj, function(key, value) {
  1310. type = $.type(value)
  1311. if (scope) key = traditional ? scope :
  1312. scope + '[' + (hash || type == 'object' || type == 'array' ? key : '') + ']'
  1313. // handle data in serializeArray() format
  1314. if (!scope && array) params.add(value.name, value.value)
  1315. // recurse into nested objects
  1316. else if (type == "array" || (!traditional && type == "object"))
  1317. serialize(params, value, traditional, key)
  1318. else params.add(key, value)
  1319. })
  1320. }
  1321. $.param = function(obj, traditional){
  1322. var params = []
  1323. params.add = function(k, v){ this.push(escape(k) + '=' + escape(v)) }
  1324. serialize(params, obj, traditional)
  1325. return params.join('&').replace(/%20/g, '+')
  1326. }
  1327. })(Zepto)
  1328. ;(function($){
  1329. $.fn.serializeArray = function() {
  1330. var result = [], el
  1331. $([].slice.call(this.get(0).elements)).each(function(){
  1332. el = $(this)
  1333. var type = el.attr('type')
  1334. if (this.nodeName.toLowerCase() != 'fieldset' &&
  1335. !this.disabled && type != 'submit' && type != 'reset' && type != 'button' &&
  1336. ((type != 'radio' && type != 'checkbox') || this.checked))
  1337. result.push({
  1338. name: el.attr('name'),
  1339. value: el.val()
  1340. })
  1341. })
  1342. return result
  1343. }
  1344. $.fn.serialize = function(){
  1345. var result = []
  1346. this.serializeArray().forEach(function(elm){
  1347. result.push(encodeURIComponent(elm.name) + '=' + encodeURIComponent(elm.value))
  1348. })
  1349. return result.join('&')
  1350. }
  1351. $.fn.submit = function(callback) {
  1352. if (callback) this.bind('submit', callback)
  1353. else if (this.length) {
  1354. var event = $.Event('submit')
  1355. this.eq(0).trigger(event)
  1356. if (!event.isDefaultPrevented()) this.get(0).submit()
  1357. }
  1358. return this
  1359. }
  1360. })(Zepto)
  1361. ;(function($){
  1362. // __proto__ doesn't exist on IE<11, so redefine
  1363. // the Z function to use object extension instead
  1364. if (!('__proto__' in {})) {
  1365. $.extend($.zepto, {
  1366. Z: function(dom, selector){
  1367. dom = dom || []
  1368. $.extend(dom, $.fn)
  1369. dom.selector = selector || ''
  1370. dom.__Z = true
  1371. return dom
  1372. },
  1373. // this is a kludge but works
  1374. isZ: function(object){
  1375. return $.type(object) === 'array' && '__Z' in object
  1376. }
  1377. })
  1378. }
  1379. // getComputedStyle shouldn't freak out when called
  1380. // without a valid element as argument
  1381. try {
  1382. getComputedStyle(undefined)
  1383. } catch(e) {
  1384. var nativeGetComputedStyle = getComputedStyle;
  1385. window.getComputedStyle = function(element){
  1386. try {
  1387. return nativeGetComputedStyle(element)
  1388. } catch(e) {
  1389. return null
  1390. }
  1391. }
  1392. }
  1393. })(Zepto)