appframework.ui.js 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807
  1. /*! intel-appframework - v3.0.0 - 2014-10-16 */
  2. /**
  3. * af.shim.js
  4. * @copyright Intel 2014
  5. * @author Ian Maffett
  6. * @description jQuery helper functions for App Framework
  7. */
  8. /* jshint eqeqeq:false */
  9. define(['jquery'],function(jQuery){
  10. (function($,window){
  11. "use strict";
  12. jQuery.event.props.push("touches");
  13. jQuery.event.props.push("originalTouches");
  14. jQuery.event.props.push("changedTouches");
  15. var nundefined, document = window.document,classCache = {},isWin8=(typeof(MSApp)==="object");
  16. function classRE(name) {
  17. return name in classCache ? classCache[name] : (classCache[name] = new RegExp("(^|\\s)" + name + "(\\s|$)"));
  18. }
  19. function _shimNodes(nodes, obj) {
  20. if (!nodes)
  21. return;
  22. if (nodes.nodeType) {
  23. obj[obj.length++] = nodes;
  24. return;
  25. }
  26. for (var i = 0, iz = nodes.length; i < iz; i++)
  27. obj[obj.length++] = nodes[i];
  28. }
  29. $.extend($.fn,{
  30. /**
  31. * Get/Set vendor specific CSS
  32. * Also set the vendor neutral version
  33. * @param {String} attribute to get
  34. * @param {String} value to set as
  35. * @return {Object} an appframework object
  36. * @title $().css(attribute,[value])
  37. */
  38. vendorCss: function (attribute, value, obj) {
  39. this.css(attribute.toLowerCase(),value,obj);
  40. return this.css($.feat.cssPrefix + attribute, value, obj);
  41. },
  42. /**
  43. * Performs a css vendor specific transform:translate operation on the collection.
  44. *
  45. ```
  46. $("#main").cssTranslate('200px,0,0');
  47. ```
  48. * @param {String} Transform values
  49. * @return {Object} an appframework object
  50. * @title $().vendorCss(value)
  51. */
  52. cssTranslate: function (val) {
  53. this.vendorCss("Transform", "translate" + $.feat.cssTransformStart + val + $.feat.cssTransformEnd);
  54. },
  55. /**
  56. * Gets the computed style of CSS values
  57. *
  58. ```
  59. $("#main").computedStyle('display');
  60. ```
  61. * @param {String} css property
  62. * @return {Int|String|Float|} css vlaue
  63. * @title $().computedStyle()
  64. */
  65. computedStyle:function(val){
  66. if(this.length===0||val==nundefined) return;
  67. return window.getComputedStyle(this[0],"")[val];
  68. },
  69. replaceClass: function(name, newName) {
  70. if (name == nundefined || newName == nundefined) return this;
  71. var replaceClassFn=function(cname) {
  72. classList = classList.replace(classRE(cname), " ");
  73. };
  74. for (var i = 0; i < this.length; i++) {
  75. var classList = this[i].className;
  76. name.split(/\s+/g).concat(newName.split(/\s+/g)).forEach(replaceClassFn);
  77. classList = classList.trim();
  78. if (classList.length > 0) {
  79. this[i].className = (classList + " " + newName).trim();
  80. } else
  81. this[i].className = newName;
  82. }
  83. return this;
  84. }
  85. });
  86. function detectUA($, userAgent) {
  87. $.os = {};
  88. $.os.webkit = userAgent.match(/WebKit\/([\d.]+)/) ? true : false;
  89. $.os.android = userAgent.match(/(Android)\s+([\d.]+)/) || userAgent.match(/Silk-Accelerated/) ? true : false;
  90. $.os.androidICS = $.os.android && userAgent.match(/(Android)\s4/) ? true : false;
  91. $.os.ipad = userAgent.match(/(iPad).*OS\s([\d_]+)/) ? true : false;
  92. $.os.iphone = !$.os.ipad && userAgent.match(/(iPhone\sOS)\s([\d_]+)/) ? true : false;
  93. $.os.ios7 = ($.os.ipad||$.os.iphone)&&(userAgent.match(/7_/)||userAgent.match(/8_/)) ? true : false;
  94. $.os.webos = userAgent.match(/(webOS|hpwOS)[\s\/]([\d.]+)/) ? true : false;
  95. $.os.touchpad = $.os.webos && userAgent.match(/TouchPad/) ? true : false;
  96. $.os.ios = $.os.ipad || $.os.iphone;
  97. $.os.playbook = userAgent.match(/PlayBook/) ? true : false;
  98. $.os.blackberry10 = userAgent.match(/BB10/) ? true : false;
  99. $.os.blackberry = $.os.playbook || $.os.blackberry10|| userAgent.match(/BlackBerry/) ? true : false;
  100. $.os.chrome = userAgent.match(/Chrome/) ? true : false;
  101. $.os.opera = userAgent.match(/Opera/) ? true : false;
  102. $.os.fennec = userAgent.match(/fennec/i) ? true : userAgent.match(/Firefox/) ? true : false;
  103. $.os.ie = userAgent.match(/MSIE 10.0/i)||userAgent.match(/Trident\/7/i) ? true : false;
  104. $.os.ieTouch = $.os.ie && userAgent.toLowerCase().match(/touch/i) ? true : false;
  105. $.os.tizen = userAgent.match(/Tizen/i)?true:false;
  106. $.os.supportsTouch = ((window.DocumentTouch && document instanceof window.DocumentTouch) || "ontouchstart" in window);
  107. $.os.kindle=userAgent.match(/Silk-Accelerated/)?true:false;
  108. //features
  109. $.feat = {};
  110. $.feat.cssPrefix = $.os.webkit ? "Webkit" : $.os.fennec ? "Moz" : $.os.ie ? "ms" : $.os.opera ? "O" : "";
  111. $.feat.cssTransformStart = !$.os.opera ? "3d(" : "(";
  112. $.feat.cssTransformEnd = !$.os.opera ? ",0)" : ")";
  113. if ($.os.android && !$.os.webkit)
  114. $.os.android = false;
  115. }
  116. detectUA($, navigator.userAgent);
  117. $.__detectUA = detectUA; //needed for unit tests
  118. /**
  119. * Utility function to create a psuedo GUID
  120. ```
  121. var id= $.uuid();
  122. ```
  123. * @title $.uuid
  124. */
  125. $.uuid = function () {
  126. var S4 = function () {
  127. return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  128. };
  129. return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
  130. };
  131. /**
  132. * Gets the css matrix, or creates a fake one
  133. ```
  134. $.getCssMatrix(domElement)
  135. ```
  136. @returns matrix with postion
  137. */
  138. $.getCssMatrix = function(ele) {
  139. if ($.is$(ele)) ele = ele.get(0);
  140. var MatrixFN = window.WebKitCSSMatrix || window.MSCSSMatrix;
  141. if (ele === nundefined) {
  142. if (MatrixFN) {
  143. return new MatrixFN();
  144. }
  145. else {
  146. return {
  147. a: 0,
  148. b: 0,
  149. c: 0,
  150. d: 0,
  151. e: 0,
  152. f: 0
  153. };
  154. }
  155. }
  156. var computedStyle = window.getComputedStyle(ele);
  157. var transform = computedStyle.webkitTransform ||
  158. computedStyle.transform ||
  159. computedStyle[$.feat.cssPrefix + "Transform"];
  160. if (MatrixFN)
  161. return new MatrixFN(transform);
  162. else if (transform) {
  163. //fake css matrix
  164. var mat = transform.replace(/[^0-9\-.,]/g, "").split(",");
  165. return {
  166. a: +mat[0],
  167. b: +mat[1],
  168. c: +mat[2],
  169. d: +mat[3],
  170. e: +mat[4],
  171. f: +mat[5]
  172. };
  173. }
  174. else {
  175. return {
  176. a: 0,
  177. b: 0,
  178. c: 0,
  179. d: 0,
  180. e: 0,
  181. f: 0
  182. };
  183. }
  184. };
  185. /**
  186. * $.create - a faster alertnative to $("<div id='main'>this is some text</div>");
  187. ```
  188. $.create("div",{id:'main',innerHTML:'this is some text'});
  189. $.create("<div id='main'>this is some text</div>");
  190. ```
  191. * @param {String} DOM Element type or html
  192. * @param [{Object}] properties to apply to the element
  193. * @return {Object} Returns an appframework object
  194. * @title $.create(type,[params])
  195. */
  196. $.create = function(type, props) {
  197. var elem;
  198. var f = new $();
  199. if (props || type[0] !== "<") {
  200. if (props.html){
  201. props.innerHTML = props.html;
  202. delete props.html;
  203. }
  204. elem = document.createElement(type);
  205. for (var j in props) {
  206. elem[j] = props[j];
  207. }
  208. f[f.length++] = elem;
  209. } else {
  210. elem = document.createElement("div");
  211. if (isWin8) {
  212. MSApp.execUnsafeLocalFunction(function() {
  213. elem.innerHTML = type.trim();
  214. });
  215. } else
  216. elem.innerHTML = type;
  217. _shimNodes(elem.childNodes, f);
  218. }
  219. return f;
  220. };
  221. /**
  222. * $.query - a no longer faster alertnative to $("div") (App Framework was faster)
  223. ```
  224. $.query(".panel");
  225. ```
  226. * @param {String} selector
  227. * @param {Object} [context]
  228. * @return {Object} Returns an appframework object
  229. * @title $.query(selector,[context])
  230. */
  231. $.query = function (sel, what) {
  232. try {
  233. return $(sel,what);
  234. }
  235. catch(e) {
  236. return $();
  237. }
  238. };
  239. $.isObject = function (obj) {
  240. return typeof obj === "object";
  241. };
  242. $.is$ = function (obj) {
  243. return obj instanceof $;
  244. };
  245. //Shim to put touch events on the jQuery special event
  246. window.$afm=$;
  247. $.feat.TouchList=function(){
  248. this.length=0;
  249. };
  250. $.feat.TouchList.prototype = {
  251. item:function(ind){
  252. return this[ind];
  253. },
  254. _add:function(touch){
  255. this[this.length]=touch;
  256. this.length++;
  257. }
  258. };
  259. var identifier=1000;
  260. $.feat.Touch = function() {
  261. this.identifier=identifier++;
  262. };
  263. $.feat.Touch.prototype = {
  264. "clientX":0,
  265. "clientY":0,
  266. "screenX":0,
  267. "screenY":0,
  268. "pageX":0,
  269. "pageY":0,
  270. "identifier":0
  271. };
  272. })(jQuery,window);
  273. window.af=window.jq=jQuery;
  274. /**
  275. * appframework.ui - A User Interface library for App Framework applications
  276. *
  277. * @copyright 2011-2014 Intel
  278. * @author Intel
  279. * @version 3.0
  280. */
  281. /* global FastClick*/
  282. /* jshint camelcase:false*/
  283. (function($) {
  284. "use strict";
  285. var startPath = window.location.pathname + window.location.search;
  286. var defaultHash = window.location.hash;
  287. var previousTarget = defaultHash;
  288. var AFUi = function() {
  289. // Init the page
  290. var that = this;
  291. if (typeof define === "function" && define.amd) {
  292. that.autoLaunch=false;
  293. define("appframeworkui", [], function() {
  294. return $.afui;
  295. });
  296. } else if (typeof module !== "undefined" && module.exports) {
  297. that.autoLaunch=false;
  298. $.afui = that;
  299. }
  300. var setupAFDom = function() {
  301. setupCustomTheme();
  302. if(window.FastClick)
  303. FastClick.attach(document.documentElement);
  304. };
  305. if (document.readyState === "complete" || document.readyState === "loaded") {
  306. setupAFDom();
  307. if(that.init)
  308. that.autoBoot();
  309. else{
  310. $(window).one("afui:init", function() {
  311. that.autoBoot();
  312. });
  313. }
  314. } else $(document).ready(
  315. function() {
  316. setupAFDom();
  317. if(that.init)
  318. that.autoBoot();
  319. else{
  320. $(window).one("afui:init", function() {
  321. that.autoBoot();
  322. });
  323. }
  324. },
  325. false);
  326. //click back event
  327. window.addEventListener("popstate", function() {
  328. if(!that.useInternalRouting) return;
  329. var id = that.getPanelId(document.location.hash);
  330. var hashChecker = document.location.href.replace(document.location.origin + "/", "");
  331. //make sure we allow hash changes outside afui
  332. if (hashChecker === "#") return;
  333. if (id === "" && that.history.length === 1) //Fix going back to first panel and an empty hash
  334. id = "#" + that.firstPanel.id;
  335. if (id === "") return;
  336. if (af(id).filter(".panel").length === 0) return;
  337. if (id !== "#" + that.activeDiv.id) that.goBack();
  338. }, false);
  339. window.addEventListener("orientationchange",function(){
  340. window.scrollTo(0, 0);
  341. });
  342. function setupCustomTheme() {
  343. if (that.useOSThemes) {
  344. var $el=$(document.body);
  345. $el.removeClass("ios ios7 win8 tizen bb android light dark firefox");
  346. if ($.os.android)
  347. $el.addClass("android");
  348. else if ($.os.ie) {
  349. $el.addClass("win8");
  350. } else if ($.os.blackberry||$.os.blackberry10||$.os.playbook) {
  351. $el.addClass("bb");
  352. that.backButtonText = "Back";
  353. } else if ($.os.ios7){
  354. $el.addClass("ios7");
  355. } else if ($.os.ios)
  356. $el.addClass("ios7");
  357. else if($.os.tizen)
  358. $el.addClass("tizen");
  359. else if($.os.fennec)
  360. $el.addClass("firefox");
  361. }
  362. if($.os.ios7&&that.overlayStatusbar){
  363. that.ready(function(){
  364. $(document.body).addClass("overlayStatusbar");
  365. });
  366. }
  367. }
  368. };
  369. var clickHandlers=[];
  370. AFUi.prototype = {
  371. init:false,
  372. showLoading: true,
  373. loadingText: "Loading Content",
  374. remotePages: {},
  375. history: [],
  376. views:{},
  377. _readyFunc: null,
  378. doingTransition: false,
  379. ajaxUrl: "",
  380. transitionType: "slide",
  381. firstPanel: "",
  382. hasLaunched: false,
  383. isLaunching:false,
  384. launchCompleted: false,
  385. activeDiv: "",
  386. customClickHandler: "",
  387. useOSThemes: true,
  388. overlayStatusbar: false,
  389. useAutoPressed: true,
  390. useInternalRouting:true,
  391. autoBoot: function() {
  392. this.hasLaunched = true;
  393. if (this.autoLaunch) {
  394. this.launch();
  395. }
  396. },
  397. /**
  398. * This blocks the page from bouncing on iOS
  399. * @api private
  400. */
  401. blockPageBounce:function(enable){
  402. if(enable===false){
  403. window.removeEventListener("touchmove",this.handlePageBounce,false);
  404. window.removeEventListener("touchstart",this.handlePageBounce,false);
  405. return;
  406. }
  407. window.addEventListener("touchmove",this.handlePageBounce,false);
  408. window.addEventListener("touchstart",this.handlePageBounce,false);
  409. },
  410. /**
  411. * Handle touch events for scrolling divs
  412. * @api private
  413. */
  414. handlePageBounce:function(evt){
  415. if(evt.type==="touchstart"){
  416. this._startTouchY=evt.touches[0].screenY;
  417. return;
  418. }
  419. var panel=$(evt.target).closest(".panel");
  420. if(panel.length===0) return evt.preventDefault();
  421. var el=panel.get(0);
  422. var canScroll=el.scrollHeight>el.clientHeight;
  423. var hasTouchOverflow=$(el).computedStyle("-webkit-overflow-scrolling")==="touch";
  424. var hasOverflow=$(el).computedStyle("overflowY")!=="hidden";
  425. var height=parseInt($(el).computedStyle("height"),10);
  426. if(canScroll&&hasTouchOverflow&&hasOverflow){
  427. var currY=evt.touches[0].screenY;
  428. var scrollAtTop=((this._startTouchY<=currY)&&(el.scrollTop===0));
  429. var scrollAtBottom=((this._startTouchY>=currY)&&((el.scrollHeight-el.scrollTop)===height));
  430. if(scrollAtTop||scrollAtBottom)
  431. evt.preventDefault();
  432. return;
  433. }
  434. },
  435. /**
  436. * Register a data directive for a click event. Plugins use this to allow
  437. * html based execution (see af.popup.js)
  438. ```
  439. $.afui.registerDataDirective("[data-foo]",function(){
  440. console.log("foo was clicked");
  441. })
  442. ```
  443. * @param {string} selector
  444. * @param {function} callback to execute
  445. * @title $.afui.registerDataDirective
  446. */
  447. registerDataDirective:function(selector,cb){
  448. clickHandlers.push({sel:selector,cb:cb});
  449. },
  450. /**
  451. * This enables the tab bar ability to keep pressed states on elements
  452. ```
  453. $.afui.enableTabBar();
  454. ```
  455. @title $.afui.enableTabBar
  456. */
  457. enableTabBar:function(){
  458. $(document).on("click",".button-grouped.tabbed",function(e){
  459. var $el=$(e.target);
  460. $el.closest(".tabbed").find(".button").data("ignore-pressed","true").removeClass("pressed");
  461. //this is a hack, but the touchEvents plugin will remove pressed
  462. $el.closest(".button").addClass("pressed");
  463. setTimeout(function(){
  464. $el.closest(".button").addClass("pressed");
  465. });
  466. });
  467. },
  468. /**
  469. * This disables the tab bar ability to keep pressed states on elements
  470. ```
  471. $.afui.disableTabBar();
  472. ```
  473. * @title $.afui.disableTabBar
  474. */
  475. disableTabBar:function(){
  476. $(document).off("click",".button-grouped.tabbed");
  477. $(".button-grouped.tabbed .button").removeAttr("data-ignore-pressed");
  478. },
  479. /**
  480. * This is a boolean property. When set to true, we manage history and update the hash
  481. ```
  482. $.afui.manageHistory=false;//Don't manage for apps using Backbone
  483. ```
  484. *@title $.afui.manageHistory
  485. */
  486. manageHistory: true,
  487. /**
  488. * This is a boolean property. When set to true (default) it will load that panel when the app is started
  489. ```
  490. $.afui.loadDefaultHash=false; //Never load the page from the hash when the app is started
  491. $.afui.loadDefaultHash=true; //Default
  492. ```
  493. *@title $.afui.loadDefaultHash
  494. */
  495. loadDefaultHash: true,
  496. /**
  497. * This is a shorthand call to the $.actionsheet plugin. We wire it to the afui div automatically
  498. ```
  499. $.afui.actionsheet("<a href='javascript:;' class='button'>Settings</a> <a href='javascript:;' class='button red'>Logout</a>")
  500. $.afui.actionsheet("[{
  501. text: 'back',
  502. cssClasses: 'red',
  503. handler: function () { $.afui.goBack(); ; }
  504. }, {
  505. text: 'show alert 5',
  506. cssClasses: 'blue',
  507. handler: function () { alert("hi"); }
  508. }, {
  509. text: 'show alert 6',
  510. cssClasses: '',
  511. handler: function () { alert("goodbye"); }
  512. }]");
  513. ```
  514. * @param {(string|Array.<string>)} links
  515. * @title $.afui.actionsheet()
  516. */
  517. actionsheet: function(opts) {
  518. return $.query(document.body).actionsheet(opts);
  519. },
  520. /**
  521. * This is a wrapper to $.popup.js plugin. If you pass in a text string, it acts like an alert box and just gives a message
  522. ```
  523. $.afui.popup(opts);
  524. $.afui.popup( {
  525. title:"Alert! Alert!",
  526. message:"This is a test of the emergency alert system!! Don't PANIC!",
  527. cancelText:"Cancel me",
  528. cancelCallback: function(){console.log("cancelled");},
  529. doneText:"I'm done!",
  530. doneCallback: function(){console.log("Done for!");},
  531. cancelOnly:false
  532. });
  533. $.afui.popup('Hi there');
  534. ```
  535. * @param {(object|string)} options
  536. * @title $.afui.popup(opts)
  537. */
  538. popup: function(opts) {
  539. return $.query(document.body).popup(opts);
  540. },
  541. /**
  542. * This is a reference to the drawer plugin.
  543. ```
  544. $.afui.drawer.show('#left','left','reveal')
  545. ```
  546. * @param {string} id of drawer
  547. * @param {string} position (left|right)
  548. * @param {string} transition (push, cover, reveal)
  549. * @title $.afui.drawer.show
  550. */
  551. /**
  552. * This is a reference to the drawer plugin.
  553. ```
  554. $.afui.drawer.hide('#left','left')
  555. ```
  556. * @param {string} id of drawer
  557. * @param {string} position (left|right)
  558. * @title $.afui.drawer.hide
  559. */
  560. /**
  561. *This will throw up a mask and block the UI
  562. ```
  563. $.afui.blockUI(.9)
  564. ````
  565. * @param {number} opacity
  566. * @title $.afui.blockUI(opacity)
  567. */
  568. blockUI: function(opacity) {
  569. $.blockUI(opacity);
  570. },
  571. /**
  572. *This will remove the UI mask
  573. ```
  574. $.afui.unblockUI()
  575. ````
  576. * @title $.afui.unblockUI()
  577. */
  578. unblockUI: function() {
  579. $.unblockUI();
  580. },
  581. /**
  582. * Boolean if you want to auto launch afui
  583. ```
  584. $.afui.autoLaunch = false; //
  585. ```
  586. * @title $.afui.autoLaunch
  587. */
  588. autoLaunch: true,
  589. /**
  590. * function to fire when afui is ready and completed launch
  591. ```
  592. $.afui.ready(function(){console.log('afui is ready');});
  593. ```
  594. * @param {function} param function to execute
  595. * @title $.afui.ready
  596. */
  597. ready: function(param) {
  598. if (this.launchCompleted)
  599. param();
  600. else {
  601. $(document).one("afui:ready", function() {
  602. param();
  603. });
  604. }
  605. },
  606. /**
  607. * Initiate a back transition
  608. ```
  609. $.afui.goBack()
  610. ```
  611. * @title $.afui.goBack()
  612. * @param {number=} delta relative position from the last element (> 0)
  613. */
  614. goBack: function(e) {
  615. //find the view
  616. var view=$(this.activeDiv).closest(".view");
  617. if(e&&e.target)
  618. view=$(e.target).closest(".view");
  619. if(view.length===0) return;
  620. //history entry
  621. if(!this.views[view.prop("id")]) return;
  622. var hist=this.views[view.prop("id")];
  623. if(hist.length===0) return;
  624. var item=hist.pop();
  625. if(item.length===0) return;
  626. if(hist.length>0){
  627. var toTarget=hist[hist.length-1].target;
  628. if(!toTarget||item.target===toTarget) return;
  629. this.runTransition(item.transition,item.target,toTarget,true);
  630. this.loadContentData(toTarget,view,true);
  631. this.updateHash(toTarget.id);
  632. }
  633. else {
  634. //try to dismiss the view
  635. try{
  636. this.dismissView(item.target,item.transition);
  637. }
  638. catch(ex){
  639. }
  640. }
  641. },
  642. /**
  643. * Clear the history queue for the current view based off the active div
  644. ```
  645. $.afui.clearHistory()
  646. ```
  647. * @title $.afui.clearHistory()
  648. */
  649. clearHistory: function() {
  650. //find the view
  651. var view=this.findViewTarget(this.activeDiv);
  652. this.views[view.prop("id")]=[];
  653. this.setBackButtonVisibility(false);
  654. },
  655. /**
  656. * PushHistory
  657. ```
  658. $.afui.pushHistory(previousPage, newPage, transition, hashExtras)
  659. ```
  660. * @api private
  661. * @title $.afui.pushHistory()
  662. */
  663. pushHistory: function(previousPage, newPage, transition, hashExtras) {
  664. //push into local history
  665. //push into the browser history
  666. try {
  667. if (!this.manageHistory) return;
  668. window.history.pushState(newPage, newPage, startPath + "#" + newPage + hashExtras);
  669. $(window).trigger("hashchange", null, {
  670. newUrl: startPath + "#" + newPage + hashExtras,
  671. oldUrl: startPath + previousPage
  672. });
  673. } catch (e) {}
  674. },
  675. /**
  676. * Updates the current window hash
  677. *
  678. * @param {string} newHash New Hash value
  679. * @title $.afui.updateHash(newHash)
  680. * @api private
  681. */
  682. updateHash: function(newHash) {
  683. if (!this.manageHistory) return;
  684. newHash = newHash.indexOf("#") === -1 ? "#" + newHash : newHash; //force having the # in the begginning as a standard
  685. previousTarget = newHash;
  686. var previousHash = window.location.hash;
  687. var panelName = this.getPanelId(newHash).substring(1); //remove the #
  688. try {
  689. window.history.replaceState(panelName, panelName, startPath + newHash);
  690. $(window).trigger("hashchange", null, {
  691. newUrl: startPath + newHash,
  692. oldUrl: startPath + previousHash
  693. });
  694. } catch (e) {}
  695. },
  696. /**
  697. * gets the panel name from an hash
  698. * @api private
  699. */
  700. getPanelId: function(hash) {
  701. var firstSlash = hash.indexOf("/");
  702. return firstSlash === -1 ? hash : hash.substring(0, firstSlash);
  703. },
  704. /**
  705. * set the back button text
  706. ```
  707. $.afui.setBackButtonText("about");
  708. ```
  709. * @param {string} text
  710. * @title $.afui.setBackButtonText(title)
  711. */
  712. setBackButtonText:function(text){
  713. $(this.activeDiv).parent().find("header .backButton").html(text);
  714. },
  715. /**
  716. * Set the title of the active header from
  717. ```
  718. $.afui.setTitle("New Title")
  719. ```
  720. * @param {string|object} String or DOM node to get the title from
  721. * @title $.afui.setTitle
  722. */
  723. setTitle:function(item){
  724. //find the header
  725. var title="";
  726. if(typeof(item)==="string"){
  727. title=item;
  728. item=$(this.activeDiv).closest(".view");
  729. }
  730. else if($(item).attr("data-title"))
  731. title=$(item).attr("data-title");
  732. else if($(item).attr("title"))
  733. title=$(item).attr("title");
  734. if(title)
  735. $(item).closest(".view").children("header").find("h1").html(title);
  736. },
  737. /**
  738. * Get the title of the active header
  739. ```
  740. $.afui.getTitle()
  741. ```
  742. * @title $.afui.getTitle
  743. */
  744. getTitle:function(){
  745. return $(this.activeDiv).closest(".view").children("header").find("h1").html();
  746. },
  747. /**
  748. * Set the visibility of the back button for the current header
  749. ```
  750. $.afui.setBackButtonVisbility(true)
  751. ```
  752. * @param {boolean} Boolean to set the visibility. true show, false hide
  753. * @title $.afui.setBackButtonVisbility
  754. */
  755. setBackButtonVisibility:function(what){
  756. var visibility=what?"visibile":"hidden";
  757. $(this.activeDiv).closest(".view").children("header").find(".backButton").css("visibility",visibility);
  758. },
  759. /**
  760. * Update a badge on the selected target. Position can be
  761. bl = bottom left
  762. tl = top left
  763. br = bottom right
  764. tr = top right (default)
  765. ```
  766. $.afui.updateBadge("#mydiv","3","bl","green");
  767. ```
  768. * @param {string} target
  769. * @param {string} value
  770. * @param {string=} position
  771. * @param {(string=|object)} color Color or CSS hash
  772. * @title $.afui.updateBadge(target,value,[position],[color])
  773. */
  774. updateBadge: function(target, value, position, color) {
  775. if (position === undefined) position = "";
  776. var $target = $(target);
  777. var badge = $target.find("span.af-badge");
  778. if (badge.length === 0) {
  779. if ($target.css("position") !== "absolute") $target.css("position", "relative");
  780. badge = $.create("span", {
  781. className: "af-badge " + position,
  782. html: value
  783. });
  784. $target.append(badge);
  785. } else badge.html(value);
  786. badge.removeClass("tl bl br tr");
  787. badge.addClass(position);
  788. if (color === undefined)
  789. color = "red";
  790. if ($.isObject(color)) {
  791. badge.css(color);
  792. } else if (color) {
  793. badge.css("background", color);
  794. }
  795. badge.data("ignore-pressed", "true");
  796. },
  797. /**
  798. * Removes a badge from the selected target.
  799. ```
  800. $.afui.removeBadge("#mydiv");
  801. ```
  802. * @param {string} target
  803. * @title $.afui.removeBadge(target)
  804. */
  805. removeBadge: function(target) {
  806. $(target).find("span.af-badge").remove();
  807. },
  808. /**
  809. * Show the loading mask with specificed text
  810. ```
  811. $.afui.showMask()
  812. $.afui.showMask('Fetching data...')
  813. ```
  814. * @param {string=} text
  815. * @title $.afui.showMask(text);
  816. */
  817. showMask: function(text) {
  818. if (!text) text = this.loadingText || "";
  819. $.query("#afui_mask>h1").html(text);
  820. $.query("#afui_mask").show();
  821. },
  822. /**
  823. * Hide the loading mask
  824. ```
  825. $.afui.hideMask();
  826. ```
  827. * @title $.afui.hideMask();
  828. */
  829. hideMask: function() {
  830. $.query("#afui_mask").hide();
  831. },
  832. /**
  833. * @api private
  834. */
  835. dismissView:function(target,transition){
  836. transition=transition.replace(":dismiss","");
  837. var theView=$(target).closest(".view");
  838. this.runTransition(transition,theView,null,true,$(target.hash).addClass("active").closest(".view"));
  839. //this.activeDiv=target;
  840. this.activeDiv=$(".view").not(theView).find(".panel.active").get(0);
  841. this.updateHash(this.activeDiv.id);
  842. },
  843. /**
  844. * This is called to initiate a transition or load content via ajax.
  845. * We can pass in a hash+id or URL.
  846. ```
  847. $.afui.loadContent("#main",false,false,"up");
  848. ```
  849. * @param {string} target
  850. * @param {boolean=} newtab (resets history)
  851. * @param {boolean=} go back (initiate the back click)
  852. * @param {string=} transition
  853. * @param {object=} anchor
  854. * @title $.afui.loadContent(target, newTab, goBack, transition, anchor);
  855. * @api public
  856. */
  857. loadContent: function(target, newView, back, transition, anchor) {
  858. if (this.doingTransition) {
  859. return;
  860. }
  861. if (target.length === 0) return;
  862. if(target.indexOf("#")!==-1){
  863. this.loadDiv(target, newView, back, transition,anchor);
  864. }
  865. else{
  866. this.loadAjax(target, newView, back, transition,anchor);
  867. }
  868. },
  869. /**
  870. * This is called internally by loadContent. Here we are loading a div instead of an Ajax link
  871. ```
  872. $.afui.loadDiv("#main",false,false,"up");
  873. ```
  874. * @param {string} target
  875. * @param {boolean=} newview (resets history)
  876. * @param {boolean=} back Go back (initiate the back click)
  877. * @param {string=} transition
  878. * @title $.afui.loadDiv(target,newTab,goBack,transition);
  879. * @api private
  880. */
  881. loadDiv: function(target, newView, back, transition,anchor) {
  882. // load a div
  883. var newDiv = target.replace("#", "");
  884. var slashIndex = newDiv.indexOf("/");
  885. var hashLink = "";
  886. if (slashIndex !== -1) {
  887. // Ignore everything after the slash for loading
  888. hashLink = newDiv.substr(slashIndex);
  889. newDiv = newDiv.substr(0, slashIndex);
  890. }
  891. newDiv = $.query("#" + newDiv).get(0);
  892. if (!newDiv) {
  893. $(document).trigger("missingpanel", null, {missingTarget: target});
  894. this.doingTransition=false;
  895. return;
  896. }
  897. if (newDiv === this.activeDiv && !back) {
  898. //toggle the menu if applicable
  899. this.doingTransition=false;
  900. return;
  901. }
  902. this.transitionType = transition;
  903. var view=this.findViewTarget(newDiv);
  904. var previous=this.findPreviousPanel(newDiv,view);
  905. //check current view
  906. var currentView;
  907. if(anchor){
  908. currentView=this.findViewTarget(anchor);
  909. }
  910. else
  911. currentView=this.findViewTarget(this.activeDiv);
  912. //Check if we are dealing with the same view
  913. var isSplitViewParent=(currentView&&currentView.get(0)!==view.get(0)&&currentView.closest(".splitview").get(0)===view.closest(".splitview").get(0)&&currentView.closest(".splitview").length!==0);
  914. if(isSplitViewParent){
  915. newView=false;
  916. }
  917. $(newDiv).trigger("panelbeforeload");
  918. $(previous).trigger("panelbeforeunload");
  919. var isNewView=false;
  920. //check nested views
  921. if(!isSplitViewParent)
  922. isSplitViewParent=currentView.parent().closest('.view').length===1;
  923. if(isSplitViewParent&&currentView&&currentView.get(0)!==view.get(0))
  924. $(currentView).trigger("nestedviewunload");
  925. if(!isSplitViewParent&&(newView||currentView&&currentView.get(0)!==view.get(0))){
  926. //Need to transition the view
  927. newView=currentView||newView;
  928. this.runViewTransition(transition,view,newView,newDiv,back);
  929. this.updateViewHistory(view,newDiv,transition,target);
  930. isNewView=true;
  931. }
  932. else{
  933. this.runTransition(transition,previous, newDiv, back);
  934. this.updateViewHistory(view,newDiv,transition,target);
  935. }
  936. //Let's check if it has a function to run to update the data
  937. //Need to call after parsePanelFunctions, since new headers can override
  938. this.loadContentData(newDiv,view,false,isNewView);
  939. },
  940. /**
  941. * @api private
  942. */
  943. findViewTarget:function(panel){
  944. var view=$(panel).closest(".view");
  945. if(!view) return false;
  946. if(!this.views[view.prop("id")])
  947. this.views[view.prop("id")]=[];
  948. return view;
  949. },
  950. /**
  951. * @api private
  952. */
  953. findPreviousPanel:function(div,view){
  954. var item=$(view).find(">.pages .panel.active").not(div);
  955. if(item.length===0)
  956. item=$(view).find(">.pages .panel:first-of-type");
  957. return item.get(0);
  958. },
  959. /**
  960. * This is called internally by loadDiv. This sets up the back button in the header and scroller for the panel
  961. ```
  962. $.afui.loadContentData("#main",false,false,"up");
  963. ```
  964. * @param {string} target
  965. * @param {boolean=} newtab (resets history)
  966. * @param {boolean=} go back (initiate the back click)
  967. * @title $.afui.loadContentData(target,newTab,goBack);
  968. * @api private
  969. */
  970. loadContentData: function(what,view,back,isNewView) {
  971. this.activeDiv = what;
  972. this.setTitle(what,view,back,isNewView);
  973. this.showBackButton(view,isNewView);
  974. this.setActiveTab(what,view);
  975. },
  976. /**
  977. * Set the active tab in the footer
  978. ```
  979. $.afui.setActiveTab("main",currView)
  980. ```
  981. @param {string|Node} id or DOM node for footer tab
  982. @param {Node} DOM node of the current view to set the footer
  983. @title $.afui.setActiveTab
  984. */
  985. setActiveTab:function(ele,view){
  986. var hash;
  987. if(typeof(ele)!=="string")
  988. hash=$(ele).prop("id");
  989. hash="#"+hash;
  990. view.find("footer").find("a").removeClass("pressed").attr("data-ignore-pressed","true").filter("[href='"+hash+"']").addClass("pressed");
  991. },
  992. /**
  993. * Show or hide the back button
  994. ```
  995. $.afui.showBackButton(view,isNewView)
  996. ```
  997. @param {Node} DOM node of current view
  998. @param {boolean} Are we loading a new view (hide the back button)
  999. @title $.afui.showBackButton
  1000. */
  1001. showBackButton:function(view,isNewView) {
  1002. var items=(this.views[view.prop("id")].length);
  1003. var hdr=view.children("header");
  1004. if(hdr.length===0) return;
  1005. if(items>=2&&isNewView!==true){
  1006. //Add the back button if it's not there
  1007. if(hdr.find(".backButton").length===1) return;
  1008. hdr.prepend("<a class='backButton back'>Back</a>");
  1009. }
  1010. else {
  1011. hdr.find(".backButton").remove();
  1012. }
  1013. },
  1014. /**
  1015. * This is called internally by loadContent. Here we are using Ajax to fetch the data
  1016. ```
  1017. $.afui.loadDiv("page.html",false,false,"up");
  1018. ```
  1019. * @param {string} target
  1020. * @param {boolean=} newtab (resets history)
  1021. * @param {boolean=} go back (initiate the back click)
  1022. * @param {string=} transition
  1023. * @param {object=} anchor
  1024. * @title $.afui.loadDiv(target,newTab,goBack,transition);
  1025. * @api private
  1026. */
  1027. loadAjax: function(target, newTab, back, transition, anchor) {
  1028. var that=this;
  1029. var crc=crc32(target);
  1030. var found=$(".panel[data-crc='"+crc+"']");
  1031. var forceRefresh=anchor.getAttribute("data-refresh");
  1032. if(found.length>0){
  1033. if(forceRefresh){
  1034. that.showLoading&&that.showMask("Loading Content");
  1035. $.ajax(target).then(function(res){
  1036. found.html(res);
  1037. that.showLoading&&that.hideMask();
  1038. return that.loadContent("#"+found.prop("id"),newTab,back,transition,anchor);
  1039. });
  1040. }
  1041. else
  1042. return that.loadContent("#"+found.prop("id"),newTab,back,transition,anchor);
  1043. }
  1044. that.showLoading&&that.showMask("Loading Content");
  1045. $.ajax(target).then(function(res){
  1046. var $res=$.create("div",{html:res});
  1047. if(!$res.hasClass(".panel")){
  1048. $res=$res.attr("data-title",(target));
  1049. $res.prop("id",crc);
  1050. $res.addClass("panel");
  1051. }
  1052. else {
  1053. $res=$res.find(".panel");
  1054. }
  1055. $(that.activeDiv).closest(".pages").append($res);
  1056. $res.attr("data-crc",crc);
  1057. that.showLoading&&that.hideMask();
  1058. that.loadContent("#"+$res.prop("id"),newTab,back,transition,anchor);
  1059. }).fail(function(res){
  1060. that.showLoading&&that.hideMask();
  1061. console.log("Error with ajax request",res);
  1062. });
  1063. },
  1064. /**
  1065. * This executes the transition for the panel
  1066. ```
  1067. $.afui.runTransition(transition,oldDiv,currDiv,back)
  1068. ```
  1069. * @api private
  1070. * @title $.afui.runTransition(transition,oldDiv,currDiv,back)
  1071. */
  1072. runTransition: function(transition,previous,target, back,noTrans) {
  1073. if(!transition)
  1074. transition="slide";
  1075. if(transition.indexOf(":back")!==-1){
  1076. transition=transition.replace(":back","");
  1077. back=true;
  1078. }
  1079. var that=this;
  1080. var show=back?previous:target;
  1081. var hide=back?target:previous;
  1082. var doPush=false;
  1083. if(transition.indexOf("-reveal")!==-1){
  1084. transition=transition.replace("-reveal","");
  1085. doPush=true;
  1086. }
  1087. $(show).css("zIndex","10");
  1088. $(hide).css("zIndex","1");
  1089. $(noTrans).css("zIndex","1").addClass("active");
  1090. var from=$(hide).animation().remove(transition+"-in");
  1091. if(!doPush&&from){
  1092. if(back)
  1093. from.reverse();
  1094. from.end(function(){
  1095. if(!back){
  1096. this.classList.remove("active");
  1097. $(this).trigger("panelunload");
  1098. }
  1099. else{
  1100. this.classList.add("active");
  1101. $(this).trigger("panelload");
  1102. }
  1103. that.doingTransition=false;
  1104. }).run(transition+"-out");
  1105. }
  1106. else {
  1107. if(!back){
  1108. $(hide).trigger("panelunload");
  1109. }
  1110. else{
  1111. $(hide).trigger("panelload");
  1112. $(hide).addClass("activeDiv");
  1113. }
  1114. }
  1115. var to=$(show).animation().remove(transition+"-out");
  1116. if(back)
  1117. to.reverse();
  1118. to.end(function(){
  1119. that.doingTransition=false;
  1120. if(!back){
  1121. this.classList.add("active");
  1122. $(this).trigger("panelload");
  1123. $(noTrans).trigger("panelload");
  1124. }
  1125. else{
  1126. if(noTrans){
  1127. $(noTrans).css("zIndex","10");
  1128. }
  1129. this.classList.remove("active");
  1130. $(this).trigger("panelunload");
  1131. }
  1132. }).run(transition+"-in");
  1133. },
  1134. /**
  1135. * This executes the transition of a view
  1136. ```
  1137. $.afui.runViewTransition(transition,view, active, newDiv,back)
  1138. ```
  1139. * @api private
  1140. * @title $.afui.runViewTransition(transition,view, active, newDiv,back)
  1141. */
  1142. runViewTransition:function(transition,view,active,newDiv,back){
  1143. //find the active
  1144. view.addClass("active");
  1145. //view.find(".panel").removeClass("active");
  1146. $(newDiv).addClass("active");
  1147. if(transition==="none"){
  1148. this.doingTransition=false;
  1149. setTimeout(function(){
  1150. active.removeClass("active");
  1151. },50);
  1152. return;
  1153. }
  1154. this.runTransition(transition,active,view,back,newDiv);
  1155. },
  1156. /**
  1157. * This updates the history of the view cache
  1158. ```
  1159. $.afui.updateViewHistory(view, div, transition, target)
  1160. ```
  1161. * @param {Node} DOM node for view
  1162. * @param {Node} DOM node for div adding to the history
  1163. * @param {string} string for the transition
  1164. * @param {string} string of the hash for the div
  1165. * @api private
  1166. * @title $.afui.updateViewHistory(view, div, transition, target)
  1167. */
  1168. updateViewHistory:function(view,div,transition,target){
  1169. var ref=this.views[view.prop("id")];
  1170. if(!ref)
  1171. ref=this.views[view.prop("id")]=[];
  1172. if(ref.length>=1&&ref[ref.length-1].target===div) return;
  1173. //pushHistory: function(previousPage, newPage, transition, hashExtras) {)
  1174. this.pushHistory(div.id,div.id,transition,target.replace(div.id,"").replace("#",""));
  1175. this.views[view.prop("id")].push({
  1176. target:div,
  1177. transition:transition
  1178. });
  1179. },
  1180. /**
  1181. * This is callled when you want to launch afui. If autoLaunch is set to true, it gets called on DOMContentLoaded.
  1182. * If autoLaunch is set to false, you can manually invoke it.
  1183. ```
  1184. $.afui.autoLaunch=false;
  1185. $.afui.launch();
  1186. ```
  1187. * @title $.afui.launch();
  1188. */
  1189. launch: function() {
  1190. if (this.hasLaunched === false || this.launchCompleted) {
  1191. this.hasLaunched = true;
  1192. return;
  1193. }
  1194. if(this.isLaunching)
  1195. return true;
  1196. this.isLaunching=true;
  1197. this.blockPageBounce();
  1198. var that = this;
  1199. var maskDiv = $.create("div", {
  1200. id: "afui_mask",
  1201. className: "ui-loader",
  1202. html: "<span class='ui-icon ui-icon-loading spin'></span><h1>Loading Content</h1>"
  1203. }).css({
  1204. "z-index": 20000,
  1205. display: "none"
  1206. });
  1207. document.body.appendChild(maskDiv.get(0));
  1208. //setup modalDiv
  1209. //get first div, defer
  1210. var first=$(".view[data-default='true']");
  1211. if(first.length===0)
  1212. first=$(".view").eq(0);
  1213. else
  1214. throw ("You need to create a view");
  1215. first.addClass("active");
  1216. //history
  1217. this.views[first.prop("id")]=[];
  1218. var firsthash=window.location.hash;
  1219. var firstPanel=first.find(".panel[data-selected='true']").length===0?first.find(".panel").eq(0):first.find(".panel[data-selected='true']");
  1220. firstPanel.addClass("active");
  1221. this.activeDiv=firstPanel.get(0);
  1222. this.views[first.prop("id")].push({
  1223. target:firstPanel.get(0),
  1224. transition:this.transitionType
  1225. });
  1226. this.defaultPanel=firstPanel.get(0);
  1227. this.loadContentData(firstPanel.get(0),first,false,true);
  1228. this.updateHash(firstPanel.get(0).id);
  1229. if(this.loadDefaultHash){
  1230. //delay launch so events can be registered/handled
  1231. setTimeout(function(){
  1232. this.loadContent(firsthash,false,0,"none");
  1233. }.bind(this));
  1234. }
  1235. this.enableTabBar();
  1236. $(document).on("click", "a", function(e) {
  1237. if(that.useInternalRouting)
  1238. checkAnchorClick(e, e.currentTarget);
  1239. });
  1240. /**
  1241. * This is our data directive processor
  1242. */
  1243. $(document).on("click",function(e){
  1244. var len=clickHandlers.length;
  1245. var $el=$(e.target);
  1246. for(var i=0;i<len;i++){
  1247. var handler=clickHandlers[i];
  1248. var checker=$el.closest(handler.sel);
  1249. if(checker.length>0)
  1250. handler.cb.call(that,checker.get(0),e);
  1251. }
  1252. });
  1253. $(document).on("click", ".backButton, [data-back]", that.goBack.bind(that));
  1254. //Check for includes
  1255. var items=$("[data-include]");
  1256. if(items.length===0){
  1257. this.launchCompleted=true;
  1258. $(document).trigger("afui:ready");
  1259. }
  1260. else{
  1261. var deferred=[];
  1262. items.each(function(){
  1263. var url=this.getAttribute("data-include");
  1264. var self=$(this);
  1265. deferred.push($.get(url,function(res){
  1266. self.append(res);
  1267. }));
  1268. });
  1269. $.when.apply($,deferred).then(function(){
  1270. this.launchCompleted=true;
  1271. $(document).trigger("afui:ready");
  1272. }).fail(function(){
  1273. this.launchCompleted=true;
  1274. $(document).trigger("afui:ready");
  1275. });
  1276. }
  1277. $(document).on("click","footer a:not(.button)",function(e){
  1278. var $item=$(e.target);
  1279. var footer=$item.closest("footer");
  1280. $item.parent().find("a:not(.button)").attr("data-ignore-pressed","true").removeClass("pressed");
  1281. if(footer.attr("data-ignore-pressed")==="true") return;
  1282. $item.addClass("pressed");
  1283. });
  1284. }
  1285. /**
  1286. * END
  1287. * @api private
  1288. */
  1289. };
  1290. //lookup for a clicked anchor recursively and fire UI own actions when applicable
  1291. var checkAnchorClick = function(e, theTarget) {
  1292. theTarget=theTarget||e.currentTarget;
  1293. if (theTarget === document) {
  1294. return;
  1295. }
  1296. //this technique fails when considerable content exists inside anchor, should be recursive ?
  1297. if (theTarget.tagName.toLowerCase() !== "a" && theTarget.parentNode) return checkAnchorClick(e, theTarget.parentNode); //let's try the parent (recursive)
  1298. //anchors
  1299. if (theTarget.tagName !== "undefined" && theTarget.tagName.toLowerCase() === "a") {
  1300. if (theTarget.href.toLowerCase().indexOf("javascript:") !== -1 || theTarget.getAttribute("data-ignore")) {
  1301. return;
  1302. }
  1303. /* IE 10 fixes*/
  1304. var href = theTarget.href,
  1305. prefix = location.protocol + "//" + location.hostname + ":" + location.port + location.pathname;
  1306. if (href.indexOf(prefix) === 0) {
  1307. href = href.substring(prefix.length);
  1308. }
  1309. //empty links
  1310. if (href === "#" || (href.indexOf("#") === href.length - 1) || (href.length === 0 && theTarget.hash.length === 0)) return e.preventDefault();
  1311. //internal links
  1312. //http urls
  1313. var urlRegex=/^((http|https|file):\/\/)/;
  1314. //only call prevent default on http/fileurls. If it's a protocol handler, do not call prevent default.
  1315. //It will fall through to the ajax call and fail
  1316. if(theTarget.href.indexOf(":") !== -1 &&urlRegex.test(theTarget.href))
  1317. e.preventDefault();
  1318. var mytransition = theTarget.getAttribute("data-transition");
  1319. if(!mytransition&&$(theTarget).closest("footer").length>0)
  1320. mytransition="none";
  1321. if(mytransition&&mytransition.indexOf(":dismiss")!==-1){
  1322. return $.afui.dismissView(theTarget,mytransition);
  1323. }
  1324. href = theTarget.hash.length > 0 ? theTarget.hash : href;
  1325. $.afui.loadContent(href, false, 0, mytransition, theTarget);
  1326. return;
  1327. }
  1328. };
  1329. var table = "00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D"; /* Number */
  1330. var crc32 = function( /* String */ str, /* Number */ crc) {
  1331. if (crc === undefined) crc = 0;
  1332. var n = 0; //a number between 0 and 255
  1333. var x = 0; //an hex number
  1334. crc = crc ^ (-1);
  1335. for (var i = 0, iTop = str.length; i < iTop; i++) {
  1336. n = (crc ^ str.charCodeAt(i)) & 0xFF;
  1337. x = "0x" + table.substr(n * 9, 8);
  1338. crc = (crc >>> 8) ^ x;
  1339. }
  1340. return (crc ^ (-1))>>>0;
  1341. };
  1342. $.afui = new AFUi();
  1343. $.afui.init=true;
  1344. $(window).trigger("afui:preinit");
  1345. $(window).trigger("afui:init");
  1346. })(jQuery);
  1347. /**
  1348. * af.actionsheet - an actionsheet for html5 mobile apps
  1349. * Copyright 2014 - Intel
  1350. */
  1351. /* EXAMPLE
  1352. You can pass in an HTML string that will get rendered
  1353. $(document.body).actionsheet('<a >Back</a><a onclick="alert(\'hi\');" >Show Alert 3</a><a onclick="alert(\'goodbye\');">Show Alert 4</a>');
  1354. You can also use an arra of objects to show each item. There are three propertyes
  1355. text - the text to display
  1356. cssClasses - extra css classes
  1357. handler - click handler function
  1358. $(document.body).actionsheet(
  1359. [{
  1360. text: 'back',
  1361. cssClasses: 'red',
  1362. handler: function () {
  1363. $.ui.goBack();
  1364. }
  1365. }, {
  1366. text: 'show alert 5',
  1367. cssClasses: 'blue',
  1368. handler: function () {
  1369. alert("hi");
  1370. }
  1371. }, {
  1372. text: 'show alert 6',
  1373. cssClasses: '',
  1374. handler: function () {
  1375. alert("goodbye");
  1376. }
  1377. }]
  1378. );
  1379. */
  1380. (function($) {
  1381. "use strict";
  1382. $.fn.actionsheet = function(opts) {
  1383. var tmp;
  1384. for (var i = 0; i < this.length; i++) {
  1385. tmp = new actionsheet(this[i], opts);
  1386. }
  1387. return this.length === 1 ? tmp : this;
  1388. };
  1389. var actionsheet = function(elID, opts) {
  1390. if (typeof elID === "string" || elID instanceof String) {
  1391. this.el = document.getElementById(elID);
  1392. } else {
  1393. this.el = elID;
  1394. }
  1395. if (!this.el) {
  1396. window.alert("Could not find element for actionsheet " + elID);
  1397. return;
  1398. }
  1399. if (this instanceof actionsheet) {
  1400. if (typeof(opts) === "object") {
  1401. for (var j in opts) {
  1402. this[j] = opts[j];
  1403. }
  1404. }
  1405. } else {
  1406. return new actionsheet(elID, opts);
  1407. }
  1408. var markStart = "<div id='af_actionsheet'><div style='width:100%'>";
  1409. var markEnd = "</div></div>";
  1410. var markup;
  1411. var noop=function(){};
  1412. if (typeof opts === "string") {
  1413. markup = $(markStart + opts + "<a href='javascript:;' class='cancel'>Cancel</a>" + markEnd);
  1414. } else if (typeof opts === "object") {
  1415. markup = $(markStart + markEnd);
  1416. var container = $(markup.children().get(0));
  1417. opts.push({
  1418. text: "Cancel",
  1419. cssClasses: "cancel"
  1420. });
  1421. for (var i = 0; i < opts.length; i++) {
  1422. var item = $("<a href='javascript:;'>" + (opts[i].text || "TEXT NOT ENTERED") + "</a>");
  1423. item[0].onclick = (opts[i].handler || noop);
  1424. if (opts[i].cssClasses && opts[i].cssClasses.length > 0)
  1425. item.addClass(opts[i].cssClasses);
  1426. container.append(item);
  1427. }
  1428. }
  1429. $(elID).find("#af_actionsheet").remove();
  1430. $(elID).find("#af_action_mask").remove();
  1431. $(elID).append(markup);
  1432. markup.vendorCss("Transition", "all 0ms");
  1433. this.el.style.overflow = "hidden";
  1434. markup.on("click", "a", this.sheetClickHandler.bind(this));
  1435. this.activeSheet = markup;
  1436. markup.cssTranslate("0," + ((markup.height())) + "px");
  1437. $(elID).append("<div id='af_action_mask' style='position:absolute;top:0px;left:0px;right:0px;bottom:0px;z-index:9998;background:rgba(0,0,0,.4)'/>");
  1438. setTimeout(function() {
  1439. markup.vendorCss("Transition", "all 300ms");
  1440. markup.cssTranslate("0,0");
  1441. }, 10);
  1442. $("#af_action_mask").bind("touchstart touchmove touchend click",function(e){
  1443. e.preventDefault();
  1444. e.stopPropagation();
  1445. });
  1446. };
  1447. actionsheet.prototype = {
  1448. activeSheet: null,
  1449. sheetClickHandler:function(){
  1450. this.hideSheet();
  1451. return false;
  1452. },
  1453. hideSheet: function() {
  1454. this.activeSheet.off("click", "a", this.sheetClickHandler);
  1455. $(this.el).find("#af_action_mask").unbind("click").remove();
  1456. this.activeSheet.vendorCss("Transition", "all 0ms");
  1457. var markup = this.activeSheet;
  1458. var theEl = this.el;
  1459. setTimeout(function() {
  1460. markup.vendorCss("Transition", "all 300ms");
  1461. markup.cssTranslate("0,"+(markup.height()+60)+"px");
  1462. setTimeout(function() {
  1463. markup.remove();
  1464. markup = null;
  1465. theEl.style.overflow = "none";
  1466. }, 350);
  1467. }, 10);
  1468. }
  1469. };
  1470. $.afui.actionsheet=function(opts){
  1471. return $(document.body).actionsheet(opts);
  1472. };
  1473. })(jQuery);
  1474. /**
  1475. * This plugin creates a "grow" transition using scaling and transforms
  1476. * @author Ian Maffett
  1477. * @copyright 2014 Intel
  1478. */
  1479. (function($){
  1480. "use strict";
  1481. $.afui.registerDataDirective("[data-grower]",function(item,e){
  1482. var $el=$(item).closest("[data-grower]");
  1483. var items=$el.offset();
  1484. var view=$el.closest(".view");
  1485. var toPanel=e.target.hash||$el.attr("data-grower");
  1486. view.css("zIndex","1");
  1487. var growView=$(toPanel).closest(".view");
  1488. var scaleX=($el.width()/window.innerWidth);
  1489. var scaleY=($el.height()/window.innerHeight);
  1490. var transProps={
  1491. left:items.left,
  1492. top:items.top,
  1493. x:scaleX,
  1494. y:scaleY
  1495. };
  1496. $(toPanel).trigger("panelgrowstart",[$el.get(0)]);
  1497. growView.addClass("active").css("zIndex","10");
  1498. growView.vendorCss("TransformOrigin","0 0");
  1499. growView.data("growTransProps",transProps);
  1500. growView.vendorCss("TransitionDuration","0");
  1501. growView.vendorCss("Transform","translate3d("+items.left+"px,"+items.top+"px,0) scale("+scaleX+","+scaleY+")");
  1502. growView.data("growTarget",$el.closest(".panel"));
  1503. growView.data("growFrom",$(toPanel).attr("id"));
  1504. $.afui.loadContent(toPanel,view,false,"stretch");
  1505. $(toPanel).one("panelload",function(){
  1506. growView.vendorCss("Transform","");
  1507. $(toPanel).trigger("panelgrowcomplete",[$el.get(0)]);
  1508. });
  1509. });
  1510. $.afui.registerDataDirective("[data-grower-back]",function(item){
  1511. var growView=$(item).closest(".view");
  1512. var fromPanel=$("#"+growView.data("growFrom"));
  1513. fromPanel.trigger("panelgrowendstart");
  1514. var trans=growView.data("growTransProps");
  1515. var toPanel="#"+growView.data("growTarget").prop("id");
  1516. $(toPanel).closest(".view").addClass("active");
  1517. growView.addClass("animation-active");
  1518. growView.transition().end(function(){
  1519. growView.removeClass("active");
  1520. fromPanel.trigger("panelgrowendstart");
  1521. $.afui.loadContent(toPanel,false,false,"none");
  1522. }).run("translate3d("+trans.left+"px,"+trans.top+"px,0) scale("+trans.x+","+trans.y+")","300ms");
  1523. });
  1524. })(jQuery);
  1525. //Touch events are based from zepto/touch.js
  1526. //Many modifications and enhancements made
  1527. /**
  1528. * Simply include this in your project to get access to the following touch events on an element
  1529. * tap
  1530. * doubleTap
  1531. * singleTap
  1532. * longPress
  1533. * swipe(Left,Right,Up,Down)
  1534. * swipeStart(Left,Right,Up,Down)
  1535. */
  1536. (function($) {
  1537. "use strict";
  1538. var touch = {}, touchTimeout;
  1539. function parentIfText(node) {
  1540. return "tagName" in node ? node : node.parentNode;
  1541. }
  1542. function swipeDirection(x1, x2, y1, y2) {
  1543. var xDelta = Math.abs(x1 - x2), yDelta = Math.abs(y1 - y2);
  1544. if (xDelta >= yDelta) {
  1545. return (x1 - x2 > 0 ? "Left" : "Right");
  1546. } else {
  1547. return (y1 - y2 > 0 ? "Up" : "Down");
  1548. }
  1549. }
  1550. var longTapDelay = 750;
  1551. function longTap() {
  1552. if (touch.last && (Date.now() - touch.last >= longTapDelay)) {
  1553. touch.el.trigger("longTap");
  1554. touch = {};
  1555. }
  1556. }
  1557. var swipeCounter;
  1558. var longTapTimer;
  1559. var parentChecker=false;
  1560. var didParentCheck=false;
  1561. $(document).ready(function() {
  1562. var prevEl;
  1563. $(document.body).bind("touchstart", function(e) {
  1564. swipeCounter=0;
  1565. if (e.originalEvent)
  1566. e = e.originalEvent;
  1567. if (!e.touches || e.touches.length === 0) return;
  1568. var now = Date.now(), delta = now - (touch.last || now);
  1569. if (!e.touches || e.touches.length === 0) return;
  1570. touch.el = $(parentIfText(e.touches[0].target));
  1571. touchTimeout && clearTimeout(touchTimeout);
  1572. touch.x1 = e.touches[0].pageX;
  1573. touch.y1 = e.touches[0].pageY;
  1574. touch.x2 = touch.y2 = 0;
  1575. if (delta > 0 && delta <= 250)
  1576. touch.isDoubleTap = true;
  1577. touch.last = now;
  1578. longTapTimer = setTimeout(longTap, longTapDelay);
  1579. if ($.afui.useAutoPressed && !touch.el.attr("data-ignore-pressed"))
  1580. touch.el.addClass("pressed");
  1581. if (prevEl && $.afui.useAutoPressed && !prevEl.attr("data-ignore-pressed") && prevEl[0] !== touch.el[0])
  1582. prevEl.removeClass("pressed");
  1583. prevEl = touch.el;
  1584. parentChecker=false;
  1585. didParentCheck=false;
  1586. }).bind("touchmove", function(e) {
  1587. if(e.originalEvent)
  1588. e = e.originalEvent;
  1589. touch.x2 = e.touches[0].pageX;
  1590. touch.y2 = e.touches[0].pageY;
  1591. if(!didParentCheck&&(Math.abs(touch.x2-touch.x1)>5||Math.abs(touch.y2-touch.y1)>5))
  1592. {
  1593. var moveX=Math.abs(touch.x2-touch.x1)>5;
  1594. var moveY=Math.abs(touch.y2-touch.y1)>5;
  1595. didParentCheck=true;
  1596. touch.el.trigger("swipeStart",[e]);
  1597. touch.el.trigger("swipeStart" + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)), [touch,e]);
  1598. var parentContainers=touch.el.closest(".swipe, .swipe-reveal, .swipe-x, .swipe-y");
  1599. var elScroller=touch.el.closest(".x-scroll, .y-scroll, .scroll");
  1600. parentChecker=touch.el.closest(".swipe, .swipe-reveal").length!==0;
  1601. if(elScroller.parent(parentContainers).length!==0)
  1602. {
  1603. parentChecker=false;
  1604. }
  1605. else if(moveX&&touch.el.closest(".swipe-x").length!==0)
  1606. parentChecker=true;
  1607. else if(moveY&&touch.el.closest(".swipe-y").length!==0)
  1608. parentChecker=true;
  1609. }
  1610. if($.os.android){
  1611. if(didParentCheck&&parentChecker)
  1612. e.preventDefault();
  1613. }
  1614. clearTimeout(longTapTimer);
  1615. }).bind("touchend", function(e) {
  1616. if(e.originalEvent)
  1617. e=e.originalEvent;
  1618. if (!touch.el)
  1619. return;
  1620. if ($.afui.useAutoPressed && !touch.el.attr("data-ignore-pressed"))
  1621. touch.el.removeClass("pressed");
  1622. if (touch.isDoubleTap) {
  1623. touch.el.trigger("doubleTap");
  1624. touch = {};
  1625. } else if (touch.x2 > 0 || touch.y2 > 0) {
  1626. (Math.abs(touch.x1 - touch.x2) > 30 || Math.abs(touch.y1 - touch.y2) > 30) &&
  1627. touch.el.trigger("swipe");
  1628. //touch.el.trigger("swipe" + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)), touch);
  1629. //@TODO - don't dispatch when you need to block it (scrolling areas)
  1630. var direction= (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2));
  1631. var scrollDir=".x-scroll";
  1632. var swipeDir=".swipe-x";
  1633. if(direction.toLowerCase()==="up"||direction.toLowerCase()==="down"){
  1634. scrollDir=".y-scroll";
  1635. swipeDir=".swipe-y";
  1636. }
  1637. var swipe=touch.el.closest(swipeDir);
  1638. var scroller=touch.el.closest(scrollDir);
  1639. if((swipe.length===0||scroller.length===0)||swipe.find(scroller).length==0)
  1640. {
  1641. touch.el.trigger("swipe"+direction);
  1642. }
  1643. touch.x1 = touch.x2 = touch.y1 = touch.y2 = touch.last = 0;
  1644. } else if ("last" in touch) {
  1645. touch.el.trigger("tap");
  1646. touchTimeout = setTimeout(function() {
  1647. touchTimeout = null;
  1648. if (touch.el)
  1649. touch.el.trigger("singleTap");
  1650. touch = {};
  1651. }, 250);
  1652. }
  1653. }).bind("touchcancel", function() {
  1654. if(touch.el && $.afui.useAutoPressed && !touch.el.attr("data-ignore-pressed"))
  1655. touch.el.removeClass("pressed");
  1656. touch = {};
  1657. clearTimeout(longTapTimer);
  1658. });
  1659. });
  1660. ["swipe", "swipeLeft", "swipeRight", "swipeUp", "swipeDown", "doubleTap", "tap", "singleTap", "longTap"].forEach(function(m) {
  1661. $.fn[m] = function(callback) {
  1662. return this.bind(m, callback);
  1663. };
  1664. });
  1665. })(jQuery);
  1666. /**
  1667. * af.animateheader
  1668. * @copyright Intel 2014
  1669. *
  1670. */
  1671. (function($){
  1672. "use strict";
  1673. //animate the headers on transtion
  1674. var oldTitle=$.afui.setTitle;
  1675. $.afui.animateHeader=function(what){
  1676. if(what!==false){
  1677. $.afui.setTitle=function(item,view,back,newView){
  1678. var title;
  1679. if(typeof(item)==="string"){
  1680. title=item;
  1681. }
  1682. else if($(item).attr("data-title"))
  1683. title=$(item).attr("data-title");
  1684. else if($(item).attr("title"))
  1685. title=$(item).attr("title");
  1686. if(!title||title.length===0) return;
  1687. var header=$(this.activeDiv).closest(".view").children("header").eq(0);
  1688. var cssClass=back?"header-unload":"header-load";
  1689. var old=header.find("h1").eq(0).html();
  1690. //update the current header h1 so we keep the DOM node for events, etc
  1691. header.find("h1").eq(0).html(title).removeClass("header-unload header-load");
  1692. if(newView) return;
  1693. header.find("h1").animation().run(cssClass+"-to");
  1694. //append a second for animation
  1695. var second=$("<h1>"+old+"</h1>");
  1696. header.append(second);
  1697. second.animation().end(function(){
  1698. $(this).remove();
  1699. }).run(cssClass);
  1700. };
  1701. }
  1702. else{
  1703. $.afui.setTitle=oldTitle;
  1704. }
  1705. };
  1706. })(jQuery);
  1707. /**
  1708. * af.popup - a popup/alert library for html5 mobile apps
  1709. * copyright Indiepath 2011 - Tim Fisher
  1710. * Modifications/enhancements by Intel for App Framework
  1711. *
  1712. */
  1713. /* EXAMPLE
  1714. $.query("body").popup({
  1715. title:"Alert! Alert!",
  1716. message:"This is a test of the emergency alert system!! Don't PANIC!",
  1717. cancelText:"Cancel me",
  1718. cancelCallback: function(){console.log("cancelled");},
  1719. doneText:"I'm done!",
  1720. doneCallback: function(){console.log("Done for!");},
  1721. cancelOnly:false,
  1722. doneClass:'button',
  1723. cancelClass:'button',
  1724. onShow:function(){console.log("showing popup");}
  1725. autoCloseDone:true, //default is true will close the popup when done is clicked.
  1726. suppressTitle:false //Do not show the title if set to true
  1727. });
  1728. You can programatically trigger a close by dispatching a "close" event to it.
  1729. $.query("body").popup({title:'Alert',id:'myTestPopup'});
  1730. $("#myTestPopup").trigger("close");
  1731. */
  1732. (function ($) {
  1733. "use strict";
  1734. $.fn.popup = function (opts) {
  1735. return new popup(this[0], opts);
  1736. };
  1737. var queue = [];
  1738. var popup = function (containerEl, opts) {
  1739. if (typeof containerEl === "string" || containerEl instanceof String) {
  1740. this.container = document.getElementById(containerEl);
  1741. } else {
  1742. this.container = containerEl;
  1743. }
  1744. if (!this.container) {
  1745. window.alert("Error finding container for popup " + containerEl);
  1746. return;
  1747. }
  1748. try {
  1749. if (typeof (opts) === "string" || typeof (opts) === "number")
  1750. opts = {
  1751. message: opts,
  1752. cancelOnly: "true",
  1753. cancelText: "OK"
  1754. };
  1755. this.id = opts.id = opts.id || $.uuid(); //opts is passed by reference
  1756. this.addCssClass = opts.addCssClass ? opts.addCssClass : "";
  1757. this.suppressTitle = opts.suppressTitle || this.suppressTitle;
  1758. this.title = opts.suppressTitle ? "" : (opts.title || "Alert");
  1759. this.message = opts.message || "";
  1760. this.cancelText = opts.cancelText || "Cancel";
  1761. this.cancelCallback = opts.cancelCallback || function () {};
  1762. this.cancelClass = opts.cancelClass || "button";
  1763. this.doneText = opts.doneText || "Done";
  1764. this.doneCallback = opts.doneCallback || function () {
  1765. // no action by default
  1766. };
  1767. this.doneClass = opts.doneClass || "button";
  1768. this.cancelOnly = opts.cancelOnly || false;
  1769. this.onShow = opts.onShow || function () {};
  1770. this.autoCloseDone = opts.autoCloseDone !== undefined ? opts.autoCloseDone : true;
  1771. queue.push(this);
  1772. if (queue.length === 1)
  1773. this.show();
  1774. } catch (e) {
  1775. console.log("error adding popup " + e);
  1776. }
  1777. };
  1778. popup.prototype = {
  1779. id: null,
  1780. addCssClass: null,
  1781. title: null,
  1782. message: null,
  1783. cancelText: null,
  1784. cancelCallback: null,
  1785. cancelClass: null,
  1786. doneText: null,
  1787. doneCallback: null,
  1788. doneClass: null,
  1789. cancelOnly: false,
  1790. onShow: null,
  1791. autoCloseDone: true,
  1792. suppressTitle: false,
  1793. show: function () {
  1794. var self = this;
  1795. var markup = "<div id='" + this.id + "' class='afPopup hidden "+ this.addCssClass + "'>"+
  1796. "<header>" + this.title + "</header>"+
  1797. "<div>" + this.message + "</div>"+
  1798. "<footer>"+
  1799. "<a href='javascript:;' class='" + this.cancelClass + "' id='cancel'>" + this.cancelText + "</a>"+
  1800. "<a href='javascript:;' class='" + this.doneClass + "' id='action'>" + this.doneText + "</a>"+
  1801. "<div style='clear:both'></div>"+
  1802. "</footer>"+
  1803. "</div>";
  1804. var $el=$(markup);
  1805. $(this.container).append($el);
  1806. $el.bind("close", function () {
  1807. self.hide();
  1808. });
  1809. if (this.cancelOnly) {
  1810. $el.find("A#action").hide();
  1811. $el.find("A#cancel").addClass("center");
  1812. }
  1813. $el.find("A").each(function () {
  1814. var button = $(this);
  1815. button.bind("click", function (e) {
  1816. if (button.attr("id") === "cancel") {
  1817. self.cancelCallback.call(self.cancelCallback, self);
  1818. self.hide();
  1819. } else {
  1820. self.doneCallback.call(self.doneCallback, self);
  1821. if (self.autoCloseDone)
  1822. self.hide();
  1823. }
  1824. e.preventDefault();
  1825. });
  1826. });
  1827. self.positionPopup();
  1828. $.blockUI(0.5);
  1829. $el.bind("orientationchange", function () {
  1830. self.positionPopup();
  1831. });
  1832. //force header/footer showing to fix CSS style bugs
  1833. $el.find("header").show();
  1834. $el.find("footer").show();
  1835. setTimeout(function(){
  1836. $el.removeClass("hidden").addClass("show");
  1837. self.onShow(self);
  1838. },50);
  1839. },
  1840. hide: function () {
  1841. var self = this;
  1842. $.query("#" + self.id).addClass("hidden");
  1843. $.unblockUI();
  1844. if(!$.os.ie&&!$.os.android){
  1845. setTimeout(function () {
  1846. self.remove();
  1847. }, 250);
  1848. }
  1849. else
  1850. self.remove();
  1851. },
  1852. remove: function () {
  1853. var self = this;
  1854. var $el = $.query("#" + self.id);
  1855. $el.unbind("close");
  1856. $el.find("BUTTON#action").unbind("click");
  1857. $el.find("BUTTON#cancel").unbind("click");
  1858. $el.unbind("orientationchange").remove();
  1859. queue.splice(0, 1);
  1860. if (queue.length > 0)
  1861. queue[0].show();
  1862. },
  1863. positionPopup: function () {
  1864. var popup = $.query("#" + this.id);
  1865. popup.css("top", ((window.innerHeight / 2.5) + window.pageYOffset) - (popup[0].clientHeight / 2) + "px");
  1866. popup.css("left", (window.innerWidth / 2) - (popup[0].clientWidth / 2) + "px");
  1867. }
  1868. };
  1869. var uiBlocked = false;
  1870. $.blockUI = function (opacity) {
  1871. if (uiBlocked)
  1872. return;
  1873. opacity = opacity ? " style='opacity:" + opacity + ";'" : "";
  1874. $.query("BODY").prepend($("<div id='mask'" + opacity + "></div>"));
  1875. $.query("BODY DIV#mask").bind("touchstart", function (e) {
  1876. e.preventDefault();
  1877. });
  1878. $.query("BODY DIV#mask").bind("touchmove", function (e) {
  1879. e.preventDefault();
  1880. });
  1881. uiBlocked = true;
  1882. };
  1883. $.unblockUI = function () {
  1884. uiBlocked = false;
  1885. $.query("BODY DIV#mask").unbind("touchstart");
  1886. $.query("BODY DIV#mask").unbind("touchmove");
  1887. $("BODY DIV#mask").remove();
  1888. };
  1889. $.afui.registerDataDirective("[data-alert]",function(item){
  1890. var $item=$(item);
  1891. var message=$item.attr("data-message");
  1892. if(message.length===0) return;
  1893. $(document.body).popup(message);
  1894. });
  1895. $.afui.popup=function(opts){
  1896. return $(document.body).popup(opts);
  1897. };
  1898. })(jQuery);
  1899. /**
  1900. * af.animation
  1901. * @copyright Intel 2014
  1902. *
  1903. */
  1904. /* jshint strict:false*/
  1905. (function ($) {
  1906. $.fn.animation = function () {
  1907. var item = this;
  1908. this.each(function () {
  1909. item = new Animator(this);
  1910. });
  1911. return item;
  1912. };
  1913. function Animator(element) {
  1914. this.element = element;
  1915. this.element.classList.remove("animation-reverse");
  1916. this.keepClass = false;
  1917. }
  1918. var animEnd = function (evt) {
  1919. this.element.removeEventListener("webkitAnimationEnd", this.endCBCache, false);
  1920. this.element.removeEventListener("animationend", this.endCBCache, false);
  1921. this.element.removeEventListener("MSAnimationEnd", this.endCBCache, false);
  1922. if (this.endcb)
  1923. this.endcb.call(this.element, evt);
  1924. this.element.classList.remove("animation-reverse");
  1925. this.element.classList.remove("animation-active");
  1926. if (!this.keepClass)
  1927. this.element.classList.remove(this.animClass);
  1928. };
  1929. Animator.prototype = {
  1930. element: null,
  1931. animClass: null,
  1932. runEnd: false,
  1933. keepClass: false,
  1934. keep: function () {
  1935. this.keepClass = true;
  1936. return this;
  1937. },
  1938. remove: function (item) {
  1939. this.element.classList.remove(item);
  1940. this.element.offsetWidth = this.element.offsetWidth;
  1941. return this;
  1942. },
  1943. endCBCache: null,
  1944. run: function (item, duration) {
  1945. this.runEnd = false;
  1946. this.element.classList.add("animation-active");
  1947. //Hack to trigger reflow
  1948. this.element.offsetWidth = this.element.offsetWidth;
  1949. this.element.classList.add(item);
  1950. this.animClass = item;
  1951. //check if it exists..if not trigger end
  1952. var computedStyle = window.getComputedStyle(this.element, null);
  1953. var time = computedStyle.animation - duration;
  1954. if (!time)
  1955. time = computedStyle.animationDuration;
  1956. if (!time)
  1957. time = computedStyle.MozAnimationDuration;
  1958. if (!time)
  1959. time = computedStyle.webkitAnimationDuration;
  1960. time = parseFloat(time);
  1961. if (time <= 0.01 || isNaN(time))
  1962. this.runEnd = true;
  1963. //Due to calling .bind, we need to cache a reference to the function to remove it
  1964. this.endCBCache = animEnd.bind(this);
  1965. if (this.runEnd) {
  1966. this.endCBCache();
  1967. return this;
  1968. }
  1969. this.element.addEventListener("webkitAnimationEnd", this.endCBCache, false);
  1970. this.element.addEventListener("animationend", this.endCBCache, false);
  1971. this.element.addEventListener("MSAnimationEnd", this.endCBCache, false);
  1972. return this;
  1973. },
  1974. reverse: function () {
  1975. this.element.classList.add("animation-reverse");
  1976. return this;
  1977. },
  1978. reRun: function (item) {
  1979. this.remove(item);
  1980. return this.run(item);
  1981. },
  1982. endcb: function () {},
  1983. end: function (cb) {
  1984. this.endcb = cb;
  1985. return this;
  1986. }
  1987. };
  1988. $.fn.transition = function () {
  1989. var item = this;
  1990. this.each(function () {
  1991. item = new Transition(this);
  1992. });
  1993. return item;
  1994. };
  1995. function Transition(element) {
  1996. this.element = element;
  1997. this.element;
  1998. }
  1999. var transitionEnd = function (evt) {
  2000. clearTimeout(this.timer);
  2001. this.element.removeEventListener("webkitTransitionEnd", this.endCBCache, false);
  2002. this.element.removeEventListener("transitionend", this.endCBCache, false);
  2003. this.element.removeEventListener("MSTransitionEnd", this.endCBCache, false);
  2004. if (this.endcb)
  2005. this.endcb.call(this.element, evt);
  2006. if (!this.keepEnd) {
  2007. $(this.element).vendorCss("TransitionDuration", "");
  2008. $(this.element).vendorCss("Transform", "");
  2009. }
  2010. };
  2011. Transition.prototype = {
  2012. element: null,
  2013. runEnd: false,
  2014. keepEnd: false,
  2015. keep: function () {
  2016. this.keepEnd = true;
  2017. return this;
  2018. },
  2019. endCBCache: null,
  2020. timer: null,
  2021. run: function (trans, duration) {
  2022. this.endCBCache = transitionEnd.bind(this);
  2023. this.element.addEventListener("webkitTransitionEnd", this.endCBCache, false);
  2024. this.element.addEventListener("transitionend", this.endCBCache, false);
  2025. this.element.addEventListener("MSTransitionEnd", this.endCBCache, false);
  2026. //$(this.element).vendorCss("TransitionProperty","all");
  2027. $(this.element).vendorCss("TransitionDuration", duration);
  2028. $(this.element).vendorCss("Transform", trans);
  2029. this.timer = setTimeout(function () {
  2030. this.endCBCache();
  2031. }.bind(this), parseInt(duration,10) + 50);
  2032. return this;
  2033. },
  2034. endcb: function () {},
  2035. end: function (cb) {
  2036. this.endcb = cb;
  2037. return this;
  2038. }
  2039. };
  2040. })(jQuery);
  2041. /**
  2042. * af.splashscreen
  2043. * @copyright Intel 2014
  2044. *
  2045. */
  2046. (function($){
  2047. "use strict";
  2048. $.afui.ready(function(){
  2049. //delay to hide the splashscreen
  2050. setTimeout(function(){
  2051. $("#splashscreen").remove();
  2052. },250);
  2053. });
  2054. })(jQuery);
  2055. /**
  2056. * af.drawer
  2057. * @copyright Intel 2014
  2058. *
  2059. */
  2060. (function($){
  2061. "use strict";
  2062. var activePosition=null;
  2063. var activeEl;
  2064. function Drawer(){
  2065. return this;
  2066. }
  2067. var isTransitioning=false;
  2068. var transitionTypes = {
  2069. push:function(elem,reverse,position){
  2070. var item=$(elem).closest(".view").children().filter(":not(nav):not(aside)");
  2071. position=position||activePosition;
  2072. for(var i=0;i<item.length;i++){
  2073. var anim=$(item[i]).show().animation();
  2074. if(reverse){
  2075. anim.remove("slide-"+position+"-out").reverse();
  2076. }
  2077. else
  2078. anim.keep();
  2079. anim.run("slide-"+position+"-out");
  2080. }
  2081. },
  2082. cover:function(){},
  2083. reveal:function(elem,reverse){
  2084. return this.push(elem,reverse);
  2085. }
  2086. };
  2087. Drawer.prototype= {
  2088. defaultTransition:"slide",
  2089. defaultAnimation:"cover",
  2090. autoHide:function(event){
  2091. event.preventDefault();
  2092. this.hide();
  2093. },
  2094. checkViewToClose:function(e){
  2095. this.autoHide(e);
  2096. },
  2097. autoHideCheck:null,
  2098. selfCheckViewToClose:null,
  2099. show:function(id,position,animation){
  2100. if(this.isTransitioning) return;
  2101. var self=this;
  2102. activePosition=position==="right"?"right":"left";
  2103. animation=transitionTypes[animation]?animation:this.defaultAnimation;
  2104. var currEl=document.getElementById(id.replace("#",""));
  2105. if(!currEl) return;
  2106. //Let"s find the view
  2107. activeEl=currEl;
  2108. if(currEl.classList.contains("active")) {
  2109. return;
  2110. }
  2111. this.isTransitioning=true;
  2112. this.autoHideCheck=this.autoHide.bind(this);
  2113. $(currEl).closest(".view").children().filter(":not(nav):not(aside)").off("touchstart",this.autoHideCheck);
  2114. currEl.classList.add(activePosition);
  2115. //Let"s remove the other animation types
  2116. var anim=$(currEl).show();
  2117. var other=activePosition==="right"?"left":"right";
  2118. if($(currEl).closest(".view").find(".slide-"+other+"-out").length>0){
  2119. $(currEl).closest(".view").find(".slide-"+other+"-out").removeClass("slide-"+other+"-out");
  2120. $(currEl).closest(".view").find("aside.active, nav.active").removeClass("left right active").hide();
  2121. }
  2122. if(animation!=="reveal"){
  2123. anim.css("zIndex","999").animation().run("slide-"+activePosition+"-in").end(function(){
  2124. this.classList.add("active");
  2125. self.isTransitioning=false;
  2126. });
  2127. }
  2128. else {
  2129. anim.animation().run("blank").end(function(){
  2130. $(this).css("zIndex","999");
  2131. this.classList.add("active");
  2132. self.isTransitioning=false;
  2133. });
  2134. }
  2135. currEl.activeAnimation=animation;
  2136. currEl.activePosition=activePosition;
  2137. currEl.toggled=true;
  2138. transitionTypes[animation](currEl,false);
  2139. //fix bug with synthetic events bubbling up
  2140. setTimeout(function(){
  2141. $(currEl).closest(".view").children().filter(":not(nav):not(aside)").on("touchstart",self.autoHideCheck);
  2142. });
  2143. this.selfCheckViewToClose=self.checkViewToClose.bind(this);
  2144. $(currEl).bind("nestedviewunload",this.selfCheckViewToClose);
  2145. },
  2146. hide:function(id,position){
  2147. if(this.isTransitioning) return;
  2148. var self=this;
  2149. var currEl=id&&document.getElementById(id.replace("#",""))||activeEl;
  2150. $(currEl).unbind("nestedviewunload",this.selfCheckViewToClose);
  2151. if(!currEl||!currEl.toggled) return;
  2152. $(currEl).closest(".view").children().filter(":not(nav):not(aside)").off("touchstart",this.autoHideCheck);
  2153. var activePosition=position||currEl.activePosition;
  2154. var anim=$(currEl).animation();
  2155. if(currEl.activeAnimation==="reveal")
  2156. $(currEl).css("zIndex","1");
  2157. var theTransition=currEl.activeAnimation==="reveal"?"blank":"slide-"+activePosition+"-in";
  2158. anim.reverse().reRun(theTransition).end(function(){
  2159. this.classList.remove("active");
  2160. this.classList.remove(activePosition);
  2161. this.style.display="none";
  2162. this.style.zIndex="";
  2163. self.isTransitioning=false;
  2164. });
  2165. if(transitionTypes[currEl.activeAnimation]){
  2166. transitionTypes[currEl.activeAnimation](currEl,true);
  2167. }
  2168. currEl.toggled=false;
  2169. activeEl=null;
  2170. }
  2171. };
  2172. $.afui.drawer =new Drawer();
  2173. //Register the handler for opening
  2174. $.afui.registerDataDirective("[data-left-menu]:not([data-menu-close]),[data-right-menu]:not([data-menu-close])",function(item){
  2175. var $item=$(item);
  2176. var position=$item.attr("data-left-menu")?"left":"right";
  2177. var obj=position==="left"?$item.attr("data-left-menu"):$item.attr("data-right-menu");
  2178. var transition=$item.attr("data-transition");
  2179. this.drawer.show(obj,position,transition);
  2180. });
  2181. //Register the handler for closing
  2182. $.afui.registerDataDirective("[data-menu-close]",function(item){
  2183. var $item=$(item);
  2184. var position=null;
  2185. if($item.attr("data-left-menu"))
  2186. position="left";
  2187. if($item.attr("data-right-menu"))
  2188. position="right";
  2189. var obj=position==="left"?$item.attr("data-left-menu"):$item.attr("data-right-menu");
  2190. if(!obj)
  2191. {
  2192. //Am i dismissing from a drawer?
  2193. obj=$(item).closest("nav").prop("id");
  2194. //let"s find the visible item in the container
  2195. if(!obj||obj.length===0)
  2196. obj=$(item).closest(".view").find("nav.active,aside.active").prop("id");
  2197. }
  2198. this.drawer.hide(obj,position);
  2199. });
  2200. })(jQuery);
  2201. /**
  2202. * af.swipereveal
  2203. * @copyright Intel 2014
  2204. *
  2205. */
  2206. (function($){
  2207. "use strict";
  2208. //Register the handler for opening
  2209. var target;
  2210. var pos=0;
  2211. var end;
  2212. var width;
  2213. $.afui.swipeThreshold=0.3;
  2214. $(document).on("swipeStartLeft",".swipe-reveal",function(e,touch,originalE){
  2215. originalE.preventDefault();
  2216. target=$(e.target).closest(".swipe-content");
  2217. pos=touch.x2;
  2218. width=target.closest(".swipe-reveal").find(".swipe-hidden").width();
  2219. if($.getCssMatrix(e.target).e!==0)
  2220. return ;
  2221. target.bind("touchmove",tracker);
  2222. target.one("touchend",function(){
  2223. target.unbind("touchmove",tracker);
  2224. if(Math.abs(end)/width<$.afui.swipeThreshold)
  2225. {
  2226. width=0;
  2227. }
  2228. target.transition().keep().end(function(){
  2229. width=null;
  2230. target=null;
  2231. }).run("translate3d("+(-width)+"px,0px,0)","100ms");
  2232. });
  2233. });
  2234. $(document).on("swipeStartRight",".swipe-reveal",function(e,touch,originalE){
  2235. originalE.preventDefault();
  2236. target=$(e.target).closest(".swipe-content");
  2237. width=target.closest(".swipe-reveal").find(".swipe-hidden").width();
  2238. if($.getCssMatrix(e.target).e===0)
  2239. return ;
  2240. pos=touch.x2+width;
  2241. target.bind("touchmove",tracker);
  2242. target.one("touchend",function(){
  2243. target.unbind("touchmove",tracker);
  2244. if((1-Math.abs(end)/width)>$.afui.swipeThreshold)
  2245. {
  2246. width=0;
  2247. }
  2248. target.transition().keep().end(function(){
  2249. width=null;
  2250. target=null;
  2251. }).run("translate3d("+(-width)+"px,0px,0)","100ms");
  2252. });
  2253. });
  2254. var tracker=function(e){
  2255. var to=-(pos-(e.touches[0].pageX));
  2256. if((to)<-width)
  2257. to="-"+width;
  2258. else if(to>0)
  2259. to=0;
  2260. end=to;
  2261. target.cssTranslate(to+"px,0");
  2262. };
  2263. })(jQuery);
  2264. /**
  2265. * desktopBrowsers contributed by Carlos Ouro @ Badoo
  2266. * translates desktop browsers events to touch events and prevents defaults
  2267. * It can be used independently in other apps but it is required for using the touchLayer in the desktop
  2268. *
  2269. * @param {Function} $ The appframework selector function
  2270. */
  2271. (function ($) {
  2272. "use strict";
  2273. var cancelClickMove = false;
  2274. //See if we can create a touch event
  2275. var tmp;
  2276. if($.os.supportsTouch) return;
  2277. try {
  2278. tmp = document.createEvent("TouchEvent");
  2279. return;
  2280. } catch (ex) {
  2281. }
  2282. $.os.supportsTouch=true;
  2283. var preventAll = function (e) {
  2284. e.preventDefault();
  2285. e.stopPropagation();
  2286. };
  2287. var ieThreshold=navigator.userAgent.match(/Phone/i)?2:7;
  2288. /**
  2289. * Stop propagation, and remove default behavior for everything but INPUT, TEXTAREA & SELECT fields
  2290. *
  2291. * @param {Event} event
  2292. * @param {HTMLElement} target
  2293. */
  2294. var preventAllButInputs = function(event, target) {
  2295. var tag = target.tagName.toUpperCase();
  2296. if (tag.indexOf("SELECT") > -1 || tag.indexOf("TEXTAREA") > -1 || tag.indexOf("INPUT") > -1) {
  2297. return;
  2298. }
  2299. preventAll(event);
  2300. };
  2301. var redirectMouseToTouch = function (type, originalEvent, newTarget,skipPrevent) {
  2302. var theTarget = newTarget ? newTarget : originalEvent.target;
  2303. if(!skipPrevent)
  2304. preventAllButInputs(originalEvent, theTarget);
  2305. var touchevt = document.createEvent("MouseEvent");
  2306. touchevt.initEvent(type, true, true);
  2307. touchevt.initMouseEvent(type, true, true, window, originalEvent.detail, originalEvent.screenX, originalEvent.screenY, originalEvent.clientX, originalEvent.clientY, originalEvent.ctrlKey, originalEvent.shiftKey, originalEvent.altKey, originalEvent.metaKey, originalEvent.button, originalEvent.relatedTarget);
  2308. touchevt.touches= new $.feat.TouchList();
  2309. touchevt.changedTouches = new $.feat.TouchList();
  2310. touchevt.targetTouches = new $.feat.TouchList();
  2311. var thetouch=new $.feat.Touch();
  2312. thetouch.pageX=originalEvent.pageX;
  2313. thetouch.pageY=originalEvent.pageY;
  2314. thetouch.target=originalEvent.target;
  2315. touchevt.changedTouches._add(thetouch);
  2316. if (type !== "touchend") {
  2317. touchevt.touches = touchevt.changedTouches;
  2318. touchevt.targetTouches = touchevt.changedTouches;
  2319. }
  2320. //target
  2321. touchevt.mouseToTouch = true;
  2322. if ($.os.ie) {
  2323. // handle inline event handlers for target and parents (for bubbling)
  2324. var elem = originalEvent.target;
  2325. while (elem !== null) {
  2326. if (elem.hasAttribute("on" + type)) {
  2327. eval(elem.getAttribute("on" + type));
  2328. }
  2329. elem = elem.parentElement;
  2330. }
  2331. }
  2332. theTarget.dispatchEvent(touchevt);
  2333. };
  2334. var mouseDown = false,
  2335. lastTarget = null,
  2336. prevX=0,
  2337. prevY=0;
  2338. if (!window.navigator.msPointerEnabled) {
  2339. document.addEventListener("mousedown", function (e) {
  2340. mouseDown = true;
  2341. lastTarget = e.target;
  2342. if (e.target.nodeName.toLowerCase() === "a" && e.target.href.toLowerCase() === "javascript:;")
  2343. e.target.href = "#";
  2344. redirectMouseToTouch("touchstart", e);
  2345. cancelClickMove = false;
  2346. prevX=e.clientX;
  2347. prevY=e.clientY;
  2348. }, true);
  2349. document.addEventListener("mouseup", function (e) {
  2350. if (!mouseDown) return;
  2351. redirectMouseToTouch("touchend", e, lastTarget); //bind it to initial mousedown target
  2352. lastTarget = null;
  2353. mouseDown = false;
  2354. }, true);
  2355. document.addEventListener("mousemove", function (e) {
  2356. if(e.clientX===prevX&&e.clientY===prevY) return;
  2357. if (!mouseDown) return;
  2358. redirectMouseToTouch("touchmove", e, lastTarget);
  2359. cancelClickMove = true;
  2360. }, true);
  2361. } else { //Win8
  2362. var skipMove=false;
  2363. document.addEventListener("MSPointerDown", function (e) {
  2364. mouseDown = true;
  2365. skipMove=true;
  2366. lastTarget = e.target;
  2367. if (e.target.nodeName.toLowerCase() === "a" && e.target.href.toLowerCase() === "javascript:;")
  2368. e.target.href = "";
  2369. redirectMouseToTouch("touchstart", e,null,true);
  2370. cancelClickMove = false;
  2371. prevX=e.clientX;
  2372. prevY=e.clientY;
  2373. return true;
  2374. }, true);
  2375. document.addEventListener("MSPointerUp", function (e) {
  2376. if (!mouseDown) return;
  2377. redirectMouseToTouch("touchend", e, lastTarget,true); // bind it to initial mousedown target
  2378. lastTarget = null;
  2379. mouseDown = false;
  2380. return true;
  2381. }, true);
  2382. document.addEventListener("MSPointerMove", function (e) {
  2383. //IE is very flakey...we need 7 pixel movement before we trigger it
  2384. if(Math.abs(e.clientX-prevX)<=ieThreshold||Math.abs(e.clientY-prevY)<=ieThreshold) return;
  2385. if (!mouseDown) return;
  2386. redirectMouseToTouch("touchmove", e, lastTarget,true);
  2387. cancelClickMove = true;
  2388. return true;
  2389. }, true);
  2390. }
  2391. // prevent all mouse events which don't exist on touch devices
  2392. document.addEventListener("drag", preventAll, true);
  2393. document.addEventListener("dragstart", preventAll, true);
  2394. document.addEventListener("dragenter", preventAll, true);
  2395. document.addEventListener("dragover", preventAll, true);
  2396. document.addEventListener("dragleave", preventAll, true);
  2397. document.addEventListener("dragend", preventAll, true);
  2398. document.addEventListener("drop", preventAll, true);
  2399. // Allow selection of input elements
  2400. document.addEventListener("selectstart", function(e){
  2401. preventAllButInputs(e, e.target);
  2402. }, true);
  2403. document.addEventListener("click", function (e) {
  2404. if (!e.mouseToTouch && e.target === lastTarget) {
  2405. preventAll(e);
  2406. }
  2407. if (cancelClickMove) {
  2408. preventAll(e);
  2409. cancelClickMove = false;
  2410. }
  2411. }, true);
  2412. })(jQuery,window);
  2413. (function ($) {
  2414. "use strict";
  2415. $.fn.toast = function (opts) {
  2416. return new Toast(this[0], opts);
  2417. };
  2418. var Toast = (function () {
  2419. var Toast = function (containerEl, opts) {
  2420. if (typeof containerEl === "string" || containerEl instanceof String) {
  2421. this.container = document.getElementById(containerEl);
  2422. } else {
  2423. this.container = containerEl;
  2424. }
  2425. if (!this.container) {
  2426. window.alert("Error finding container for toast " + containerEl);
  2427. return;
  2428. }
  2429. if (typeof (opts) === "string" || typeof (opts) === "number") {
  2430. opts = {
  2431. message: opts
  2432. };
  2433. }
  2434. this.addCssClass = opts.addCssClass ? opts.addCssClass : "";
  2435. this.message = opts.message || "";
  2436. this.delay=opts.delay||this.delay;
  2437. this.position=opts.position||"";
  2438. this.addCssClass+=" "+this.position;
  2439. this.type=opts.type||"";
  2440. //Check if the container exists
  2441. this.container=$(this.container);
  2442. if(this.container.find(".afToastContainer").length===0)
  2443. {
  2444. this.container.append("<div class='afToastContainer'></div>");
  2445. }
  2446. this.container=this.container.find(".afToastContainer");
  2447. this.container.removeClass("tr br tl bl tc bc").addClass(this.addCssClass);
  2448. if(opts.autoClose===false)
  2449. this.autoClose=false;
  2450. this.show();
  2451. };
  2452. Toast.prototype = {
  2453. addCssClass: null,
  2454. message: null,
  2455. delay:5000,
  2456. el:null,
  2457. container:null,
  2458. timer:null,
  2459. autoClose:true,
  2460. show: function () {
  2461. var self = this;
  2462. var markup = "<div class='afToast "+this.type+"'>"+
  2463. "<div>" + this.message + "</div>"+
  2464. "</div>";
  2465. this.el=$(markup).get(0);
  2466. this.container.append(this.el);
  2467. var $el=$(this.el);
  2468. var height=this.el.clientHeight;
  2469. $el.addClass("hidden");
  2470. setTimeout(function(){
  2471. $el.css("height",height);
  2472. $el.removeClass("hidden");
  2473. },20);
  2474. if(this.autoClose){
  2475. this.timer=setTimeout(function(){
  2476. self.hide();
  2477. },this.delay);
  2478. }
  2479. $el.bind("click",function(){
  2480. self.hide();
  2481. });
  2482. },
  2483. hide: function () {
  2484. var self = this;
  2485. clearTimeout(this.timer);
  2486. $(this.el).unbind("click").addClass("hidden");
  2487. $(this.el).css("height","0px");
  2488. if(!$.os.ie&&!$.os.android){
  2489. setTimeout(function () {
  2490. self.remove();
  2491. }, 300);
  2492. }
  2493. else
  2494. self.remove();
  2495. },
  2496. remove: function () {
  2497. var $el = $(this.el);
  2498. $el.remove();
  2499. }
  2500. };
  2501. return Toast;
  2502. })();
  2503. $.afui.toast=function(opts){
  2504. $(document.body).toast(opts);
  2505. };
  2506. $.afui.registerDataDirective("[data-toast]",function(item){
  2507. var $item=$(item);
  2508. var message=$item.attr("data-message")||"";
  2509. if(message.length===0) return;
  2510. var position=$item.attr("data-position")||"tr";
  2511. var type=$item.attr("data-type");
  2512. var autoClose=$item.attr("data-auto-close")==="false"?false:true;
  2513. var delay=$item.attr("data-delay")||0;
  2514. var opts={
  2515. message:message,
  2516. position:position,
  2517. delay:delay,
  2518. autoClose:autoClose,
  2519. type:type
  2520. };
  2521. $(document.body).toast(opts);
  2522. });
  2523. })(jQuery);
  2524. return $.afui;
  2525. });