appframework.ui.js 104 KB

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