appframework.ui.js 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117
  1. /**
  2. * appframework.ui - A User Interface library for App Framework applications
  3. *
  4. * @copyright 2011 Intel
  5. * @author Intel
  6. * @version 2.0
  7. */ (function ($) {
  8. "use strict";
  9. var hasLaunched = false;
  10. var startPath = window.location.pathname + window.location.search;
  11. var defaultHash = window.location.hash;
  12. var previousTarget = defaultHash;
  13. var ui = function () {
  14. // Init the page
  15. var that = this;
  16. /**
  17. * Helper function to setup the transition objects
  18. * Custom transitions can be added via $.ui.availableTransitions
  19. ```
  20. $.ui.availableTransitions['none']=function();
  21. ```
  22. */
  23. this.availableTransitions = {};
  24. this.availableTransitions['default'] = this.availableTransitions.none = this.noTransition;
  25. //setup the menu and boot touchLayer
  26. function checkNodeInserted(i) {
  27. if (i.target.id === "afui") {
  28. setupCustomTheme();
  29. $(document).unbind("DOMNodeInserted", checkNodeInserted);
  30. }
  31. }
  32. if ($("#afui").length === 1) {
  33. setupCustomTheme();
  34. } else {
  35. $(document).bind("DOMNodeInserted", checkNodeInserted);
  36. }
  37. var checkAFDom = function () {
  38. //boot touchLayer
  39. //create afui element if it still does not exist
  40. var afui = document.getElementById("afui");
  41. if (afui === null) {
  42. afui = document.createElement("div");
  43. afui.id = "afui";
  44. var body = document.body;
  45. while (body && body.firstChild) {
  46. afui.appendChild(body.firstChild);
  47. }
  48. $(document.body).prepend(afui);
  49. }
  50. if ($.os.supportsTouch) $.touchLayer(afui);
  51. setupCustomTheme();
  52. };
  53. if ("intel" in window) {
  54. document.addEventListener("intel.xdk.device.ready", function () {
  55. checkAFDom();
  56. that.autoBoot();
  57. }, true);
  58. }
  59. else if (document.readyState == "complete" || document.readyState == "loaded") {
  60. checkAFDom();
  61. if (that.init)
  62. that.autoBoot();
  63. else {
  64. $(window).one("afui:init", function () {
  65. that.autoBoot();
  66. });
  67. }
  68. } else $(document).ready(function () {
  69. checkAFDom();
  70. if (that.init)
  71. that.autoBoot();
  72. else {
  73. $(window).one("afui:init", function () {
  74. that.autoBoot();
  75. });
  76. }
  77. }, false);
  78. if (!("intel" in window)) window.intel = { xdk: {} }, window.intel.xdk.webRoot = "";
  79. //click back event
  80. window.addEventListener("popstate", function () {
  81. if (!that.useInteralRouting) return;
  82. var id = that.getPanelId(document.location.hash);
  83. var hashChecker = document.location.href.replace(document.location.origin + "/", "");
  84. //make sure we allow hash changes outside afui
  85. if (hashChecker == "#") return;
  86. if (id === "" && that.history.length === 1) //Fix going back to first panel and an empty hash
  87. id = "#" + that.firstDiv.id;
  88. if (id === "") return;
  89. if (af(id).filter(".panel").length === 0) return;
  90. if (id != "#" + that.activeDiv.id) that.goBack();
  91. }, false);
  92. function setupCustomTheme() {
  93. if (that.useOSThemes) {
  94. if ($.os.android) $("#afui").addClass("android");
  95. else if ($.os.ie) {
  96. $("#afui").addClass("win8");
  97. } else if ($.os.blackberry || $.os.blackberry10 || $.os.playbook) {
  98. $("#afui").addClass("bb");
  99. that.backButtonText = "Back";
  100. } else if ($.os.ios7)
  101. $("#afui").addClass("ios7");
  102. else if ($.os.ios)
  103. $("#afui").addClass("ios");
  104. }
  105. if ($.os.ios) {
  106. $("head").find("#iosBlurrHack").remove();
  107. $("head").append("<style id='iosBlurrHack'>#afui .panel > * {-webkit-backface-visibility: hidden;-webkit-perspective: 1000;}</style>");
  108. }
  109. }
  110. };
  111. ui.prototype = {
  112. init: false,
  113. transitionTime: "230ms",
  114. showLoading: true,
  115. loadingText: "Loading Content",
  116. loadContentQueue: [],
  117. isIntel: false,
  118. titlebar: "",
  119. navbar: "",
  120. header: "",
  121. viewportContainer: "",
  122. remotePages: {},
  123. history: [],
  124. homeDiv: "",
  125. screenWidth: "",
  126. content: "",
  127. modalWindow: "",
  128. customFooter: false,
  129. defaultFooter: "",
  130. defaultHeader: null,
  131. customMenu: false,
  132. defaultMenu: "",
  133. _readyFunc: null,
  134. doingTransition: false,
  135. passwordBox: $.passwordBox ? new $.passwordBox() : false,
  136. selectBox: $.selectBox ? $.selectBox : false,
  137. ajaxUrl: "",
  138. transitionType: "slide",
  139. scrollingDivs: {},
  140. firstDiv: "",
  141. hasLaunched: false,
  142. launchCompleted: false,
  143. activeDiv: "",
  144. customClickHandler: "",
  145. menuAnimation: null,
  146. togglingSideMenu: false,
  147. sideMenuWidth: "200px",
  148. handheldMinWidth: "768",
  149. trimBackButtonText: true,
  150. useOSThemes: true,
  151. lockPageBounce: false,
  152. animateHeaders: true,
  153. useAutoPressed: true,
  154. horizontalScroll: false,
  155. _currentHeaderID: "defaultHeader",
  156. useInteralRouting: true,
  157. autoBoot: function () {
  158. this.hasLaunched = true;
  159. if (this.autoLaunch) {
  160. this.launch();
  161. }
  162. },
  163. css3animate: function (el, opts) {
  164. el = $(el);
  165. return el.css3Animate(opts);
  166. },
  167. /**
  168. * This changes the side menu width
  169. * ```
  170. $.ui.setSideMenuWidth('300px');
  171. ```
  172. *@title $.ui.setSideMenuWidth
  173. */
  174. setSideMenuWidth: function (width) {
  175. this.sideMenuWidth = width;
  176. //override the css style
  177. width = width + "";
  178. width = width.replace("px", "") + "px";
  179. $("head").find("#styleWidth").remove();
  180. $("head").append("<style id='styleWidth'>#afui #menu {width:" + width + " !important}</style>");
  181. },
  182. /**
  183. * this will disable native scrolling on iOS
  184. *
  185. ```
  186. $.ui.disableNativeScrolling);
  187. ```
  188. *@title $.ui.disableNativeScrolling
  189. */
  190. disableNativeScrolling: function () {
  191. $.feat.nativeTouchScroll = false;
  192. },
  193. /**
  194. * This is a boolean property. When set to true, we manage history and update the hash
  195. ```
  196. $.ui.manageHistory=false;//Don't manage for apps using Backbone
  197. ```
  198. *@title $.ui.manageHistory
  199. */
  200. manageHistory: true,
  201. /**
  202. * This is a boolean property. When set to true (default) it will load that panel when the app is started
  203. ```
  204. $.ui.loadDefaultHash=false; //Never load the page from the hash when the app is started
  205. $.ui.loadDefaultHash=true; //Default
  206. ```
  207. *@title $.ui.loadDefaultHash
  208. */
  209. loadDefaultHash: true,
  210. /**
  211. * This is a boolean that when set to true will add "&cache=_rand_" to any ajax loaded link
  212. The default is false
  213. ```
  214. $.ui.useAjaxCacheBuster=true;
  215. ```
  216. *@title $.ui.useAjaxCacheBuster
  217. */
  218. useAjaxCacheBuster: false,
  219. /**
  220. * This is a shorthand call to the $.actionsheet plugin. We wire it to the afui div automatically
  221. ```
  222. $.ui.actionsheet("<a href='javascript:;' class='button'>Settings</a> <a href='javascript:;' class='button red'>Logout</a>")
  223. $.ui.actionsheet("[{
  224. text: 'back',
  225. cssClasses: 'red',
  226. handler: function () { $.ui.goBack(); ; }
  227. }, {
  228. text: 'show alert 5',
  229. cssClasses: 'blue',
  230. handler: function () { alert("hi"); }
  231. }, {
  232. text: 'show alert 6',
  233. cssClasses: '',
  234. handler: function () { alert("goodbye"); }
  235. }]");
  236. ```
  237. * @param {String,Array} links
  238. * @title $.ui.actionsheet()
  239. */
  240. actionsheet: function (opts) {
  241. return $.query("#afui").actionsheet(opts);
  242. },
  243. /**
  244. * 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
  245. ```
  246. $.ui.popup(opts);
  247. $.ui.popup( {
  248. title:"Alert! Alert!",
  249. message:"This is a test of the emergency alert system!! Don't PANIC!",
  250. cancelText:"Cancel me",
  251. cancelCallback: function(){console.log("cancelled");},
  252. doneText:"I'm done!",
  253. doneCallback: function(){console.log("Done for!");},
  254. cancelOnly:false
  255. });
  256. $.ui.popup('Hi there');
  257. ```
  258. * @param {Object|String} options
  259. * @title $.ui.popup(opts)
  260. */
  261. popup: function (opts) {
  262. return $.query("#afui").popup(opts);
  263. },
  264. /**
  265. *This will throw up a mask and block the UI
  266. ```
  267. $.ui.blockUI(.9)
  268. ````
  269. * @param {Float} opacity
  270. * @title $.ui.blockUI(opacity)
  271. */
  272. blockUI: function (opacity) {
  273. $.blockUI(opacity);
  274. },
  275. /**
  276. *This will remove the UI mask
  277. ```
  278. $.ui.unblockUI()
  279. ````
  280. * @title $.ui.unblockUI()
  281. */
  282. unblockUI: function () {
  283. $.unblockUI();
  284. },
  285. /**
  286. * Will remove the bottom nav bar menu from your application
  287. ```
  288. $.ui.removeFooterMenu();
  289. ```
  290. * @title $.ui.removeFooterMenu
  291. */
  292. removeFooterMenu: function () {
  293. $.query("#navbar").hide();
  294. $.query("#content").css("bottom", "0px");
  295. this.showNavMenu = false;
  296. },
  297. /**
  298. * Boolean if you want to show the bottom nav menu.
  299. ```
  300. $.ui.showNavMenu = false;
  301. ```
  302. * @api private
  303. * @title $.ui.showNavMenu
  304. */
  305. showNavMenu: true,
  306. /**
  307. * Boolean if you want to auto launch afui
  308. ```
  309. $.ui.autoLaunch = false; //
  310. * @title $.ui.autoLaunch
  311. */
  312. autoLaunch: true,
  313. /**
  314. * Boolean if you want to show the back button
  315. ```
  316. $.ui.showBackButton = false; //
  317. * @title $.ui.showBackButton
  318. */
  319. showBackbutton: true, // Kept for backward compatibility.
  320. showBackButton: true,
  321. /**
  322. * Override the back button text
  323. ```
  324. $.ui.backButtonText="Back"
  325. ```
  326. * @title $.ui.backButtonText
  327. */
  328. backButtonText: "",
  329. /**
  330. * Boolean if you want to reset the scroller position when navigating panels. Default is true
  331. ```
  332. $.ui.resetScrollers=false; //Do not reset the scrollers when switching panels
  333. ```
  334. * @title $.ui.resetScrollers
  335. */
  336. resetScrollers: true,
  337. /**
  338. * function to fire when afui is ready and completed launch
  339. ```
  340. $.ui.ready(function(){console.log('afui is ready');});
  341. ```
  342. * @param {Function} function to execute
  343. * @title $.ui.ready
  344. */
  345. ready: function (param) {
  346. if (this.launchCompleted) param();
  347. else {
  348. $(document).on("afui:ready", function (e) {
  349. param();
  350. });
  351. }
  352. },
  353. /**
  354. * Override the back button class name
  355. ```
  356. $.ui.setBackButtonStyle('newClass');
  357. ```
  358. * @param {String} new class name
  359. * @title $.ui.setBackButtonStyle(class)
  360. */
  361. setBackButtonStyle: function (className) {
  362. $.query("#header #backButton").get(0).className = className;
  363. },
  364. /**
  365. * Initiate a back transition
  366. ```
  367. $.ui.goBack()
  368. ```
  369. * @title $.ui.goBack()
  370. * @param {Number} [delta=1] relative position from the last element (> 0)
  371. */
  372. goBack: function (delta) {
  373. delta = Math.min(Math.abs(~~delta || 1), this.history.length);
  374. if (delta) {
  375. var tmpEl = this.history.splice(-delta).shift();
  376. this.loadContent(tmpEl.target + "", 0, 1, tmpEl.transition);
  377. this.transitionType = tmpEl.transition;
  378. this.updateHash(tmpEl.target);
  379. }
  380. },
  381. goBackHistory: function (delta) {
  382. window.history.back();
  383. },
  384. /**
  385. * Clear the history queue
  386. ```
  387. $.ui.clearHistory()
  388. ```
  389. * @title $.ui.clearHistory()
  390. */
  391. clearHistory: function () {
  392. this.history = [];
  393. this.setBackButtonVisibility(false);
  394. },
  395. /**
  396. * PushHistory
  397. ```
  398. $.ui.pushHistory(previousPage, newPage, transition, hashExtras)
  399. ```
  400. * @api private
  401. * @title $.ui.pushHistory()
  402. */
  403. pushHistory: function (previousPage, newPage, transition, hashExtras) {
  404. //push into local history
  405. this.history.push({
  406. target: previousPage,
  407. transition: transition
  408. });
  409. //push into the browser history
  410. try {
  411. if (!this.manageHistory) return;
  412. window.history.pushState(newPage, newPage, startPath + '#' + newPage + hashExtras);
  413. $(window).trigger("hashchange", null, {
  414. newUrl: startPath + '#' + newPage + hashExtras,
  415. oldURL: startPath + previousPage
  416. });
  417. } catch (e) { }
  418. },
  419. /**
  420. * Updates the current window hash
  421. *
  422. * @param {String} newHash New Hash value
  423. * @title $.ui.updateHash(newHash)
  424. * @api private
  425. */
  426. updateHash: function (newHash) {
  427. if (!this.manageHistory) return;
  428. newHash = newHash.indexOf('#') == -1 ? '#' + newHash : newHash; //force having the # in the begginning as a standard
  429. previousTarget = newHash;
  430. var previousHash = window.location.hash;
  431. var panelName = this.getPanelId(newHash).substring(1); //remove the #
  432. try {
  433. window.history.replaceState(panelName, panelName, startPath + newHash);
  434. $(window).trigger("hashchange", null, {
  435. newUrl: startPath + newHash,
  436. oldUrl: startPath + previousHash
  437. });
  438. } catch (e) { }
  439. },
  440. /*gets the panel name from an hash*/
  441. getPanelId: function (hash) {
  442. var firstSlash = hash.indexOf('/');
  443. return firstSlash == -1 ? hash : hash.substring(0, firstSlash);
  444. },
  445. /**
  446. * Update a badge on the selected target. Position can be
  447. bl = bottom left
  448. tl = top left
  449. br = bottom right
  450. tr = top right (default)
  451. ```
  452. $.ui.updateBadge('#mydiv','3','bl','green');
  453. ```
  454. * @param {String} target
  455. * @param {String} Value
  456. * @param {String} [position]
  457. * @param {String|Object} [color or CSS hash]
  458. * @title $.ui.updateBadge(target,value,[position],[color])
  459. */
  460. updateBadge: function (target, value, position, color) {
  461. if (position === undefined) position = "";
  462. var $target = $(target);
  463. var badge = $target.find("span.af-badge");
  464. if (badge.length === 0) {
  465. if ($target.css("position") != "absolute") $target.css("position", "relative");
  466. badge = $.create("span", {
  467. className: "af-badge " + position,
  468. html: value
  469. });
  470. $target.append(badge);
  471. } else badge.html(value);
  472. badge.removeClass("tl bl br tr");
  473. badge.addClass(position);
  474. if (color === undefined)
  475. color = "red";
  476. if ($.isObject(color)) {
  477. badge.css(color);
  478. } else if (color) {
  479. badge.css("background", color);
  480. }
  481. badge.data("ignore-pressed", "true");
  482. },
  483. /**
  484. * Removes a badge from the selected target.
  485. ```
  486. $.ui.removeBadge('#mydiv');
  487. ```
  488. * @param {String} target
  489. * @title $.ui.removeBadge(target)
  490. */
  491. removeBadge: function (target) {
  492. $(target).find("span.af-badge").remove();
  493. },
  494. /**
  495. * Toggles the bottom nav menu. Force is a boolean to force show or hide.
  496. ```
  497. $.ui.toggleNavMenu();//toggle it
  498. $.ui.toggleNavMenu(true); //force show it
  499. ```
  500. * @param {Boolean} [force]
  501. * @title $.ui.toggleNavMenu([force])
  502. */
  503. toggleNavMenu: function (force) {
  504. if (!this.showNavMenu) return;
  505. if ($.query("#navbar").css("display") != "none" && ((force !== undefined && force !== true) || force === undefined)) {
  506. $.query("#content").css("bottom", "0px");
  507. $.query("#navbar").hide();
  508. } else if (force === undefined || (force !== undefined && force === true)) {
  509. $.query("#navbar").show();
  510. $.query("#content").css("bottom", $.query("#navbar").css("height"));
  511. }
  512. },
  513. /**
  514. * Toggles the top header menu. Force is a boolean to force show or hide.
  515. ```
  516. $.ui.toggleHeaderMenu();//toggle it
  517. ```
  518. * @param {Boolean} [force]
  519. * @title $.ui.toggleHeaderMenu([force])
  520. */
  521. toggleHeaderMenu: function (force) {
  522. if ($.query("#header").css("display") != "none" && ((force !== undefined && force !== true) || force === undefined)) {
  523. $.query("#content").css("top", "0px");
  524. $.query("#header").hide();
  525. } else if (force === undefined || (force !== undefined && force === true)) {
  526. $.query("#header").show();
  527. $.query("#content").css("top", $.query("#header").css("height"));
  528. }
  529. },
  530. /**
  531. * Toggles the side menu. Force is a boolean to force show or hide.
  532. ```
  533. $.ui.toggleSideMenu();//toggle it
  534. ```
  535. * @param {Boolean} [force]
  536. * @param {Function} [callback] Callback function to execute after menu toggle is finished
  537. * @param {int} [time] Time to run the transition
  538. * @title $.ui.toggleSideMenu([force],[callback],[time])
  539. */
  540. toggleSideMenu: function (force, callback, time) {
  541. if (!this.isSideMenuEnabled() || this.togglingSideMenu) return;
  542. var that = this;
  543. var menu = $.query("#menu");
  544. var els = $.query("#content, #header, #navbar");
  545. var panelMask = $.query(".afui_panel_mask");
  546. time = time || this.transitionTime;
  547. var open = this.isSideMenuOn();
  548. if (panelMask.length === 0 && window.innerWidth < $.ui.handheldMinWidth) {
  549. els.append('<div class="afui_panel_mask"></div>');
  550. panelMask = $.query(".afui_panel_mask");
  551. $(".afui_panel_mask").bind("click", function () {
  552. $.ui.toggleSideMenu(false);
  553. });
  554. }
  555. if (force === 2 || (!open && ((force !== undefined && force !== false) || force === undefined))) {
  556. this.togglingSideMenu = true;
  557. menu.show();
  558. that.css3animate(els, {
  559. x: that.sideMenuWidth,
  560. time: time,
  561. complete: function (canceled) {
  562. that.togglingSideMenu = false;
  563. els.vendorCss("Transition", "");
  564. if (callback) callback(canceled);
  565. if (panelMask.length !== 0 && window.innerWidth < $.ui.handheldMinWidth) {
  566. panelMask.show();
  567. }
  568. }
  569. });
  570. } else if (force === undefined || (force !== undefined && force === false)) {
  571. this.togglingSideMenu = true;
  572. that.css3animate(els, {
  573. x: "0px",
  574. time: time,
  575. complete: function (canceled) {
  576. // els.removeClass("on");
  577. els.vendorCss("Transition", "");
  578. els.vendorCss("Transform", "");
  579. that.togglingSideMenu = false;
  580. if (callback) callback(canceled);
  581. menu.hide();
  582. if (panelMask.length !== 0 && window.innerWidth < $.ui.handheldMinWidth) {
  583. panelMask.hide();
  584. }
  585. }
  586. });
  587. }
  588. },
  589. /**
  590. * Disables the side menu
  591. ```
  592. $.ui.disableSideMenu();
  593. ```
  594. * @title $.ui.disableSideMenu();
  595. */
  596. disableSideMenu: function () {
  597. var that = this;
  598. var els = $.query("#content, #header, #navbar");
  599. if (this.isSideMenuOn()) {
  600. this.toggleSideMenu(false, function (canceled) {
  601. if (!canceled) els.removeClass("hasMenu");
  602. });
  603. } else els.removeClass("hasMenu");
  604. $.query("#menu").removeClass("tabletMenu");
  605. },
  606. /**
  607. * Enables the side menu if it has been disabled
  608. ```
  609. $.ui.enableSideMenu();
  610. ```
  611. * @title $.ui.enableSideMenu();
  612. */
  613. enableSideMenu: function () {
  614. $.query("#content, #header, #navbar").addClass("hasMenu");
  615. $.query("#menu").addClass("tabletMenu");
  616. },
  617. /**
  618. *
  619. * @title $.ui.enableSideMenu();
  620. * @api private
  621. */
  622. isSideMenuEnabled: function () {
  623. return $.query("#content").hasClass("hasMenu");
  624. },
  625. /**
  626. *
  627. * @title $.ui.enableSideMenu();
  628. * @api private
  629. */
  630. isSideMenuOn: function () {
  631. var menu = parseInt($.getCssMatrix($("#content")).e) > 1 ? true : false;
  632. return this.isSideMenuEnabled() && menu;
  633. },
  634. /**
  635. * Reference to the default footer
  636. * @api private
  637. */
  638. prevFooter: null,
  639. /**
  640. * Updates the elements in the navbar
  641. ```
  642. $.ui.updateNavbarElements(elements);
  643. ```
  644. * @param {String|Object} Elements
  645. * @title $.ui.updateNavbarElements(Elements)
  646. */
  647. updateNavbarElements: function (elems) {
  648. if (this.prevFooter) {
  649. if (this.prevFooter.data("parent")) this.prevFooter.appendTo("#" + this.prevFooter.data("parent"));
  650. else this.prevFooter.appendTo("#afui");
  651. }
  652. if (!$.is$(elems)) //inline footer
  653. {
  654. elems = $.query("#" + elems);
  655. }
  656. $.query("#navbar").append(elems);
  657. this.prevFooter = elems;
  658. var tmpAnchors = $.query("#navbar >*");
  659. if (tmpAnchors.length > 0) {
  660. tmpAnchors.data("ignore-pressed", "true").data("resetHistory", "true");
  661. var width = parseFloat(100 / tmpAnchors.length);
  662. tmpAnchors.css("width", width + "%");
  663. }
  664. var nodes = $.query("#navbar footer");
  665. if (nodes.length === 0) return;
  666. nodes = nodes.get(0).childNodes;
  667. for (var i = 0; i < nodes.length; i++) {
  668. if (nodes[i].nodeType === 3) {
  669. nodes[i].parentNode.removeChild(nodes[i]);
  670. }
  671. }
  672. },
  673. /**
  674. * Reference to the previous header
  675. * @api private
  676. */
  677. prevHeader: null,
  678. /**
  679. * Updates the elements in the header
  680. ```
  681. $.ui.updateHeaderElements(elements);
  682. ```
  683. * @param {String|Object} Elements
  684. * @title $.ui.updateHeaderElements(Elements)
  685. */
  686. updateHeaderElements: function (elems, goBack) {
  687. var that = this;
  688. if (!$.is$(elems)) //inline footer
  689. {
  690. elems = $.query("#" + elems);
  691. }
  692. if (elems == this.prevHeader) return;
  693. this._currentHeaderID = elems.prop("id");
  694. if (this.prevHeader) {
  695. //Let's slide them out
  696. $.query("#header").append(elems);
  697. //Do not animate - sometimes they act funky
  698. if (!$.ui.animateHeaders) {
  699. if (that.prevHeader.data("parent")) that.prevHeader.appendTo("#" + that.prevHeader.data("parent"));
  700. else that.prevHeader.appendTo("#afui");
  701. that.prevHeader = elems;
  702. return;
  703. }
  704. var from = goBack ? "100px" : "-100px";
  705. var to = goBack ? "-100px" : "100px";
  706. that.prevHeader.addClass("ignore");
  707. that.css3animate(elems, {
  708. x: to,
  709. opacity: 0.3,
  710. time: "1ms"
  711. });
  712. that.css3animate(that.prevHeader, {
  713. x: from,
  714. y: 0,
  715. opacity: 0.3,
  716. time: that.transitionTime,
  717. delay: numOnly(that.transitionTime) / 5 + "ms",
  718. complete: function () {
  719. if (that.prevHeader.data("parent")) that.prevHeader.appendTo("#" + that.prevHeader.data("parent"));
  720. else that.prevHeader.appendTo("#afui");
  721. that.prevHeader.removeClass("ignore");
  722. that.css3animate(that.prevHeader, {
  723. x: to,
  724. opacity: 1,
  725. time: "1ms"
  726. });
  727. that.prevHeader = elems;
  728. }
  729. });
  730. that.css3animate(elems, {
  731. x: "0px",
  732. opacity: 1,
  733. time: that.transitionTime
  734. });
  735. } else {
  736. $.query("#header").append(elems);
  737. this.prevHeader = elems;
  738. }
  739. },
  740. /**
  741. * @api private
  742. * Kept for backwards compatibility
  743. */
  744. updateSideMenu: function (elems) {
  745. return this.updateSideMenuElements(elems);
  746. },
  747. /**
  748. * Updates the elements in the side menu
  749. ```
  750. $.ui.updateSideMenuElements(elements);
  751. ```
  752. * @param {String|Object} Elements
  753. * @title $.ui.updateSideMenuElements(Elements)
  754. */
  755. updateSideMenuElements: function (elems) {
  756. var that = this;
  757. if (elems === undefined || elems === null) return;
  758. var nb = $.query("#menu_scroller");
  759. if (this.prevMenu) {
  760. this.prevMenu.insertBefore("#afui #menu");
  761. this.prevMenu = null;
  762. }
  763. if (!$.is$(elems)) elems = $.query("#" + elems);
  764. nb.html('');
  765. nb.append(elems);
  766. this.prevMenu = elems;
  767. //Move the scroller to the top and hide it
  768. this.scrollingDivs.menu_scroller.hideScrollbars();
  769. this.scrollingDivs.menu_scroller.scrollToTop();
  770. },
  771. /**
  772. * Set the title of the current panel
  773. ```
  774. $.ui.setTitle("new title");
  775. ```
  776. * @param {String} value
  777. * @title $.ui.setTitle(value)
  778. */
  779. setTitle: function (val) {
  780. if (this._currentHeaderID !== "defaultHeader") return;
  781. $.query("#header header:not(.ignore) #pageTitle").html(val);
  782. },
  783. /**
  784. * Override the text for the back button
  785. ```
  786. $.ui.setBackButtonText("GO...");
  787. ```
  788. * @param {String} value
  789. * @title $.ui.setBackButtonText(value)
  790. */
  791. setBackButtonText: function (text) {
  792. if (this._currentHeaderID !== "defaultHeader") return;
  793. if (this.trimBackButtonText && text.length >= 7)
  794. text = text.substring(0, 5) + "...";
  795. if (this.backButtonText.length > 0) $.query("#header header:not(.ignore) #backButton").html(this.backButtonText);
  796. else $.query("#header header:not(.ignore) #backButton").html(text);
  797. },
  798. /**
  799. * Toggle visibility of the back button
  800. */
  801. setBackButtonVisibility: function (show) {
  802. //if (!show) $.query("#header #backButton").css("visibility", "hidden");
  803. // else $.query("#header #backButton").css("visibility", "visible");
  804. },
  805. /**
  806. * Show the loading mask
  807. ```
  808. $.ui.showMask()
  809. $.ui.showMask('Doing work')
  810. ```
  811. * @param {String} [text]
  812. * @title $.ui.showMask(text);
  813. */
  814. showMask: function (text) {
  815. if (!text) text = this.loadingText || "";
  816. $.query("#afui_mask>h1").html(text);
  817. $.query("#afui_mask").show();
  818. },
  819. /**
  820. * Hide the loading mask
  821. * @title $.ui.hideMask();
  822. */
  823. hideMask: function () {
  824. $.query("#afui_mask").hide();
  825. },
  826. /**
  827. * @api private
  828. */
  829. modalReference_: null,
  830. /**
  831. * Load a content panel in a modal window. We set the innerHTML so event binding will not work. Please use the data-load or panelloaded events to setup any event binding
  832. ```
  833. $.ui.showModal("#myDiv","fade");
  834. ```
  835. * @param {String|Object} panel to show
  836. * @param {String} [transition]
  837. * @title $.ui.showModal();
  838. */
  839. showModal: function (id, trans) {
  840. var that = this;
  841. this.modalTransition = trans || "up";
  842. var modalDiv = $.query("#modalContainer");
  843. if (typeof (id) === "string")
  844. id = "#" + id.replace("#", "");
  845. var $panel = $.query(id);
  846. this.modalReference_ = $panel;
  847. if ($panel.length) {
  848. var useScroller = this.scrollingDivs.hasOwnProperty($panel.attr("id"));
  849. var useScroller = this.scrollingDivs.hasOwnProperty($panel.attr("id"));
  850. //modalDiv.html($.feat.nativeTouchScroll || !useScroller ? $.query(id).html() : $.query(id).get(0).childNodes[0].innerHTML + '', true);
  851. modalDiv.empty();
  852. var elemsToCopy;
  853. if ($.feat.nativeTouchScroll || !useScroller) {
  854. elemsToCopy = $panel.contents();
  855. }
  856. else {
  857. elemsToCopy = $($panel.get(0).childNodes[0]).contents();
  858. }
  859. modalDiv.append(elemsToCopy);
  860. modalDiv.append("<a onclick='$.ui.hideModal();' class='closebutton modalbutton'></a>");
  861. that.modalWindow.style.display = "block";
  862. this.runTransition(this.modalTransition, that.modalTransContainer, that.modalWindow, false);
  863. if (useScroller) {
  864. this.scrollingDivs.modal_container.enable(that.resetScrollers);
  865. }
  866. else {
  867. this.scrollingDivs.modal_container.disable();
  868. }
  869. this.scrollToTop('modal');
  870. modalDiv.data("panel", id);
  871. var myPanel = $panel.get(0);
  872. var fnc = myPanel.getAttribute("data-load");
  873. if (typeof fnc == "string" && window[fnc]) {
  874. window[fnc](myPanel);
  875. }
  876. $panel.trigger("loadpanel");
  877. }
  878. },
  879. /**
  880. * Hide the modal window and remove the content. We remove any event listeners on the contents.
  881. ```
  882. $.ui.hideModal("");
  883. ```
  884. * @title $.ui.hideModal();
  885. */
  886. hideModal: function () {
  887. var self = this;
  888. //$.query("#modalContainer").html("", true);
  889. var $cnt = $.query("#modalContainer");
  890. $cnt.find(".closebutton.modalbutton").remove();
  891. var useScroller = this.scrollingDivs.hasOwnProperty(this.modalReference_.attr("id"));
  892. this.runTransition(self.modalTransition, self.modalWindow, self.modalTransContainer, true);
  893. this.scrollingDivs.modal_container.disable();
  894. //var tmp = $.query($.query("#modalContainer").data("panel"));
  895. var tmp = $.query($cnt.data("panel"));
  896. var fnc = tmp.data("unload");
  897. if (typeof fnc == "string" && window[fnc]) {
  898. window[fnc](tmp.get(0));
  899. }
  900. tmp.trigger("unloadpanel");
  901. setTimeout(function () {
  902. if ($.feat.nativeTouchScroll || !useScroller) {
  903. self.modalReference_.append($cnt.contents());
  904. }
  905. else {
  906. $(self.modalReference_.get(0).childNodes[0]).append($cnt.contents());
  907. }
  908. $cnt.html("", true);
  909. }, this.transitionTime);
  910. },
  911. /**
  912. * Update the HTML in a content panel
  913. ```
  914. $.ui.updatePanel("#myDiv","This is the new content");
  915. ```
  916. * @param {String,Object} panel
  917. * @param {String} html to update with
  918. * @title $.ui.updatePanel(id,content);
  919. */
  920. updatePanel: function (id, content) {
  921. id = "#" + id.replace("#", "");
  922. var el = $.query(id).get(0);
  923. if (!el) return;
  924. var newDiv = $.create("div", {
  925. html: content
  926. });
  927. if (newDiv.children('.panel') && newDiv.children('.panel').length > 0) newDiv = newDiv.children('.panel').get(0);
  928. else newDiv = newDiv.get(0);
  929. if (el.getAttribute("js-scrolling") && (el.getAttribute("js-scrolling").toLowerCase() == "yes" || el.getAttribute("js-scrolling").toLowerCase() == "true")) {
  930. $.cleanUpContent(el.childNodes[0], false, true);
  931. $(el.childNodes[0]).html(content);
  932. } else {
  933. $.cleanUpContent(el, false, true);
  934. $(el).html(content);
  935. }
  936. if (newDiv.getAttribute("data-title")) el.setAttribute("data-title", newDiv.getAttribute("data-title"));
  937. },
  938. /**
  939. * Same as $.ui.updatePanel. kept for backwards compatibility
  940. ```
  941. $.ui.updateContentDiv("#myDiv","This is the new content");
  942. ```
  943. * @param {String,Object} panel
  944. * @param {String} html to update with
  945. * @title $.ui.updateContentDiv(id,content);
  946. */
  947. updateContentDiv: function (id, content) {
  948. return this.updatePanel(id, content);
  949. },
  950. /**
  951. * Dynamically creates a new panel. It wires events, creates the scroller, applies Android fixes, etc.
  952. ```
  953. $.ui.addContentDiv("myDiv","This is the new content","Title");
  954. ```
  955. * @param {String|Object} Element to add
  956. * @param {String} Content
  957. * @param {String} title
  958. * @title $.ui.addContentDiv(id,content,title);
  959. */
  960. addContentDiv: function (el, content, title, refresh, refreshFunc) {
  961. el = typeof (el) !== "string" ? el : el.indexOf("#") == -1 ? "#" + el : el;
  962. var myEl = $.query(el).get(0);
  963. var newDiv, newId;
  964. if (!myEl) {
  965. newDiv = $.create("div", {
  966. html: content
  967. });
  968. if (newDiv.children('.panel') && newDiv.children('.panel').length > 0) newDiv = newDiv.children('.panel').get(0);
  969. else newDiv = newDiv.get(0);
  970. if (!newDiv.getAttribute("data-title") && title) newDiv.setAttribute("data-title", title);
  971. newId = (newDiv.id) ? newDiv.id : el.replace("#", ""); //figure out the new id - either the id from the loaded div.panel or the crc32 hash
  972. newDiv.id = newId;
  973. if (newDiv.id != el) newDiv.setAttribute("data-crc", el.replace("#", ""));
  974. } else {
  975. newDiv = myEl;
  976. }
  977. newDiv.className = "panel";
  978. newId = newDiv.id;
  979. this.addDivAndScroll(newDiv, refresh, refreshFunc);
  980. myEl = null;
  981. newDiv = null;
  982. return newId;
  983. },
  984. /**
  985. * Takes a div and sets up scrolling for it..
  986. ```
  987. $.ui.addDivAndScroll(object);
  988. ```
  989. * @param {Object} Element
  990. * @title $.ui.addDivAndScroll(element);
  991. * @api private
  992. */
  993. addDivAndScroll: function (tmp, refreshPull, refreshFunc, container) {
  994. var self = this;
  995. var jsScroll = false,
  996. scrollEl;
  997. var overflowStyle = tmp.style.overflow;
  998. var hasScroll = overflowStyle != 'hidden' && overflowStyle != 'visible';
  999. container = container || this.content;
  1000. //sets up scroll when required and not supported
  1001. if (!$.feat.nativeTouchScroll && hasScroll) tmp.setAttribute("js-scrolling", "true");
  1002. if (tmp.getAttribute("js-scrolling") && (tmp.getAttribute("js-scrolling").toLowerCase() == "yes" || tmp.getAttribute("js-scrolling").toLowerCase() == "true")) {
  1003. jsScroll = true;
  1004. hasScroll = true;
  1005. }
  1006. var title = tmp.getAttribute("data-title") || tmp.title;
  1007. tmp.title = "";
  1008. tmp.setAttribute("data-title", title);
  1009. if (tmp.getAttribute("scrolling") && tmp.getAttribute("scrolling") == "no") {
  1010. hasScroll = false;
  1011. jsScroll = false;
  1012. tmp.removeAttribute("js-scrolling");
  1013. }
  1014. if (!jsScroll || $.os.desktop) {
  1015. container.appendChild(tmp);
  1016. scrollEl = tmp;
  1017. tmp.style['-webkit-overflow-scrolling'] = "none";
  1018. } else {
  1019. //WE need to clone the div so we keep events
  1020. scrollEl = tmp.cloneNode(false);
  1021. tmp.title = null;
  1022. tmp.id = null;
  1023. var $tmp = $(tmp);
  1024. $tmp.removeAttr("data-footer data-aside data-nav data-header selected data-load data-unload data-tab data-crc title data-title");
  1025. $tmp.replaceClass("panel", "afScrollPanel");
  1026. scrollEl.appendChild(tmp);
  1027. container.appendChild(scrollEl);
  1028. if (this.selectBox !== false) this.selectBox.getOldSelects(scrollEl.id);
  1029. if (this.passwordBox !== false) this.passwordBox.getOldPasswords(scrollEl.id);
  1030. }
  1031. if (hasScroll) {
  1032. this.scrollingDivs[scrollEl.id] = ($(tmp).scroller({
  1033. scrollBars: true,
  1034. verticalScroll: true,
  1035. horizontalScroll: self.horizontalScroll,
  1036. vScrollCSS: "afScrollbar",
  1037. refresh: refreshPull,
  1038. useJsScroll: jsScroll,
  1039. noParent: !jsScroll,
  1040. lockBounce: this.lockPageBounce,
  1041. autoEnable: false //dont enable the events unnecessarilly
  1042. }));
  1043. //无限加载
  1044. var infinite = $("#" + scrollEl.id + " .infiniteScroll");
  1045. if (infinite.length > 0) {
  1046. var myScroller = $.ui.scrollingDivs[scrollEl.id];
  1047. myScroller.addInfinite();
  1048. $.bind(myScroller, "infinite-scroll", function () {
  1049. var self = this;
  1050. //ax
  1051. var arr = eval($("#" + $.ui.activeDiv.id + " .pvernextarr>*").text());
  1052. if (arr.next.url) { $.ui.showMask("加载中....");};
  1053. // console.log("infinite triggered");
  1054. $.bind(myScroller, "infinite-scroll-end", function () {
  1055. myScroller.scrollToBottom();
  1056. if (arr.next.url) {
  1057. $.ajax({
  1058. beforeSend: function () { },
  1059. dataType: "text",
  1060. url: arr.next.url,
  1061. success: function (data) {
  1062. infinite.append($(data).find(".infiniteScroll>*"));
  1063. $("#" + $.ui.activeDiv.id + " .pvernextarr").html("").append($(data).find(".pvernextarr>*"));
  1064. $.ui.hideMask();
  1065. }
  1066. })
  1067. }
  1068. $.unbind(myScroller, "infinite-scroll-end");
  1069. setTimeout(function () {
  1070. $.ui.hideMask();
  1071. self.clearInfinite();
  1072. }, 3000);
  1073. });
  1074. });
  1075. };
  1076. //backwards compatibility
  1077. if (refreshFunc) $.bind(this.scrollingDivs[scrollEl.id], 'refresh-release', function (trigger) {
  1078. if (trigger) refreshFunc();
  1079. });
  1080. }
  1081. tmp = null;
  1082. scrollEl = null;
  1083. },
  1084. /**
  1085. * Scrolls a panel to the top
  1086. ```
  1087. $.ui.scrollToTop(id);
  1088. ```
  1089. * @param {String} id
  1090. * @param {string} Time to scroll
  1091. * @title $.ui.scrollToTop(id);
  1092. */
  1093. scrollToTop: function (id, time) {
  1094. time = time || "300ms";
  1095. id = id.replace("#", "");
  1096. if (this.scrollingDivs[id]) {
  1097. this.scrollingDivs[id].scrollToTop(time);
  1098. }
  1099. },
  1100. /**
  1101. * Scrolls a panel to the bottom
  1102. ```
  1103. $.ui.scrollToBottom(id,time);
  1104. ```
  1105. * @param {String} id
  1106. * @param {string} Time to scroll
  1107. * @title $.ui.scrollToBottom(id);
  1108. */
  1109. scrollToBottom: function (id, time) {
  1110. id = id.replace("#", "");
  1111. if (this.scrollingDivs[id]) {
  1112. this.scrollingDivs[id].scrollToBottom(time);
  1113. }
  1114. },
  1115. /**
  1116. * This is used when a transition fires to do helper events. We check to see if we need to change the nav menus, footer, and fire
  1117. * the load/onload functions for panels
  1118. ```
  1119. $.ui.parsePanelFunctions(currentDiv,oldDiv);
  1120. ```
  1121. * @param {Object} current div
  1122. * @param {Object} old div
  1123. * @title $.ui.parsePanelFunctions(currentDiv,oldDiv);
  1124. * @api private
  1125. */
  1126. parsePanelFunctions: function (what, oldDiv, goBack) {
  1127. //check for custom footer
  1128. var that = this;
  1129. var hasFooter = what.getAttribute("data-footer");
  1130. var hasHeader = what.getAttribute("data-header");
  1131. //$asap removed since animations are fixed in css3animate
  1132. if (hasFooter && hasFooter.toLowerCase() == "none") {
  1133. that.toggleNavMenu(false);
  1134. hasFooter = false;
  1135. } else {
  1136. that.toggleNavMenu(true);
  1137. }
  1138. if (hasFooter && that.customFooter != hasFooter) {
  1139. that.customFooter = hasFooter;
  1140. that.updateNavbarElements(hasFooter);
  1141. } else if (hasFooter != that.customFooter) {
  1142. if (that.customFooter) that.updateNavbarElements(that.defaultFooter);
  1143. that.customFooter = false;
  1144. }
  1145. if (hasHeader && hasHeader.toLowerCase() == "none") {
  1146. that.toggleHeaderMenu(false);
  1147. hasHeader = false;
  1148. } else {
  1149. that.toggleHeaderMenu(true);
  1150. }
  1151. if (hasHeader && that.customHeader != hasHeader) {
  1152. that.customHeader = hasHeader;
  1153. that.updateHeaderElements(hasHeader, goBack);
  1154. } else if (hasHeader != that.customHeader) {
  1155. if (that.customHeader) {
  1156. that.updateHeaderElements(that.defaultHeader, goBack);
  1157. //that.setTitle(that.activeDiv.title);
  1158. }
  1159. that.customHeader = false;
  1160. }
  1161. //Load inline footers
  1162. var inlineFooters = $(what).find("footer");
  1163. if (inlineFooters.length > 0) {
  1164. that.customFooter = what.id;
  1165. inlineFooters.data("parent", what.id);
  1166. that.updateNavbarElements(inlineFooters);
  1167. }
  1168. //load inline headers
  1169. var inlineHeader = $(what).find("header");
  1170. if (inlineHeader.length > 0) {
  1171. that.customHeader = what.id;
  1172. inlineHeader.data("parent", what.id);
  1173. that.updateHeaderElements(inlineHeader, goBack);
  1174. }
  1175. //check if the panel has a footer
  1176. if (what.getAttribute("data-tab")) { //Allow the dev to force the footer menu
  1177. $.query("#navbar a").removeClass("pressed");
  1178. $.query("#navbar #" + what.getAttribute("data-tab")).addClass("pressed");
  1179. }
  1180. var hasMenu = what.getAttribute("data-nav");
  1181. if (hasMenu && this.customMenu != hasMenu) {
  1182. this.customMenu = hasMenu;
  1183. this.updateSideMenuElements(hasMenu);
  1184. } else if (hasMenu != this.customMenu) {
  1185. if (this.customMenu) {
  1186. this.updateSideMenuElements(this.defaultMenu);
  1187. }
  1188. this.customMenu = false;
  1189. }
  1190. if (oldDiv) {
  1191. fnc = oldDiv.getAttribute("data-unload");
  1192. if (typeof fnc == "string" && window[fnc]) {
  1193. window[fnc](oldDiv);
  1194. }
  1195. $(oldDiv).trigger("unloadpanel");
  1196. }
  1197. var fnc = what.getAttribute("data-load");
  1198. if (typeof fnc == "string" && window[fnc]) {
  1199. window[fnc](what);
  1200. }
  1201. $(what).trigger("loadpanel");
  1202. if (this.isSideMenuOn()) {
  1203. var that = this;
  1204. that.toggleSideMenu(false);
  1205. }
  1206. },
  1207. /**
  1208. * Helper function that parses a contents html for any script tags and either adds them or executes the code
  1209. * @api private
  1210. */
  1211. parseScriptTags: function (div) {
  1212. if (!div) return;
  1213. $.parseJS(div);
  1214. },
  1215. /**
  1216. * This is called to initiate a transition or load content via ajax.
  1217. * We can pass in a hash+id or URL and then we parse the panel for additional functions
  1218. ```
  1219. $.ui.loadContent("#main",false,false,"up");
  1220. ```
  1221. * @param {String} target
  1222. * @param {Boolean} newtab (resets history)
  1223. * @param {Boolean} go back (initiate the back click)
  1224. * @param {String} transition
  1225. * @title $.ui.loadContent(target,newTab,goBack,transition);
  1226. * @api public
  1227. */
  1228. loadContent: function (target, newTab, back, transition, anchor) {
  1229. if (this.doingTransition) {
  1230. var that = this;
  1231. this.loadContentQueue.push([target, newTab, back, transition, anchor]);
  1232. return;
  1233. }
  1234. if (target.length === 0) return;
  1235. var what = null;
  1236. var loadAjax = true;
  1237. anchor = anchor || document.createElement("a"); //Hack to allow passing in no anchor
  1238. if (target.indexOf("#") == -1) {
  1239. var urlHash = "url" + crc32(target); //Ajax urls
  1240. var crcCheck = $.query("div.panel[data-crc='" + urlHash + "']");
  1241. if ($.query("#" + target).length > 0) {
  1242. loadAjax = false;
  1243. } else if (crcCheck.length > 0) {
  1244. loadAjax = false;
  1245. if (anchor.getAttribute("data-refresh-ajax") === 'true' || (anchor.refresh && anchor.refresh === true || this.isAjaxApp)) {
  1246. loadAjax = true;
  1247. } else {
  1248. target = "#" + crcCheck.get(0).id;
  1249. }
  1250. } else if ($.query("#" + urlHash).length > 0) {
  1251. //ajax div already exists. Let's see if we should be refreshing it.
  1252. loadAjax = false;
  1253. if (anchor.getAttribute("data-refresh-ajax") === 'true' || (anchor.refresh && anchor.refresh === true || this.isAjaxApp)) {
  1254. loadAjax = true;
  1255. } else target = "#" + urlHash;
  1256. }
  1257. }
  1258. if (target.indexOf("#") == -1 && loadAjax) {
  1259. this.loadAjax(target, newTab, back, transition, anchor);
  1260. } else {
  1261. this.loadDiv(target, newTab, back, transition);
  1262. }
  1263. },
  1264. /**
  1265. * This is called internally by loadContent. Here we are loading a div instead of an Ajax link
  1266. ```
  1267. $.ui.loadDiv("#main",false,false,"up");
  1268. ```
  1269. * @param {String} target
  1270. * @param {Boolean} newtab (resets history)
  1271. * @param {Boolean} go back (initiate the back click)
  1272. * @param {String} transition
  1273. * @title $.ui.loadDiv(target,newTab,goBack,transition);
  1274. * @api private
  1275. */
  1276. loadDiv: function (target, newTab, back, transition) {
  1277. // load a div
  1278. var that = this;
  1279. var what = target.replace("#", "");
  1280. var slashIndex = what.indexOf('/');
  1281. var hashLink = "";
  1282. if (slashIndex != -1) {
  1283. // Ignore everything after the slash for loading
  1284. hashLink = what.substr(slashIndex);
  1285. what = what.substr(0, slashIndex);
  1286. }
  1287. what = $.query("#" + what).get(0);
  1288. if (!what) {
  1289. $(document).trigger("missingpanel", null, { missingTarget: target });
  1290. return;
  1291. }
  1292. if (what == this.activeDiv && !back) {
  1293. //toggle the menu if applicable
  1294. if (this.isSideMenuOn()) this.toggleSideMenu(false);
  1295. return;
  1296. }
  1297. this.transitionType = transition;
  1298. var oldDiv = this.activeDiv;
  1299. var currWhat = what;
  1300. if (what.getAttribute("data-modal") == "true" || what.getAttribute("modal") == "true") {
  1301. /*var fnc = what.getAttribute("data-load");
  1302. if (typeof fnc == "string" && window[fnc]) {
  1303. window[fnc](what);
  1304. }
  1305. $(what).trigger("loadpanel");
  1306. */
  1307. return this.showModal(what.id);
  1308. }
  1309. if (oldDiv == currWhat) //prevent it from going to itself
  1310. return;
  1311. if (newTab) {
  1312. this.clearHistory();
  1313. this.pushHistory("#" + this.firstDiv.id, what.id, transition, hashLink);
  1314. } else if (!back) {
  1315. this.pushHistory(previousTarget, what.id, transition, hashLink);
  1316. }
  1317. previousTarget = '#' + what.id + hashLink;
  1318. this.doingTransition = true;
  1319. oldDiv.style.display = "block";
  1320. currWhat.style.display = "block";
  1321. this.runTransition(transition, oldDiv, currWhat, back);
  1322. //Let's check if it has a function to run to update the data
  1323. this.parsePanelFunctions(what, oldDiv, back);
  1324. //Need to call after parsePanelFunctions, since new headers can override
  1325. this.loadContentData(what, newTab, back, transition);
  1326. setTimeout(function () {
  1327. if (that.scrollingDivs[oldDiv.id]) {
  1328. that.scrollingDivs[oldDiv.id].disable();
  1329. }
  1330. }, numOnly(that.transitionTime) + 50);
  1331. },
  1332. /**
  1333. * This is called internally by loadDiv. This sets up the back button in the header and scroller for the panel
  1334. ```
  1335. $.ui.loadContentData("#main",false,false,"up");
  1336. ```
  1337. * @param {String} target
  1338. * @param {Boolean} newtab (resets history)
  1339. * @param {Boolean} go back (initiate the back click)
  1340. * @param {String} transition
  1341. * @title $.ui.loadDiv(target,newTab,goBack,transition);
  1342. * @api private
  1343. */
  1344. loadContentData: function (what, newTab, back, transition) {
  1345. var prevId, el, val, slashIndex;
  1346. if (back) {
  1347. if (this.history.length > 0) {
  1348. val = this.history[this.history.length - 1];
  1349. slashIndex = val.target.indexOf('/');
  1350. if (slashIndex != -1) {
  1351. prevId = val.target.substr(0, slashIndex);
  1352. } else prevId = val.target;
  1353. el = $.query(prevId).get(0);
  1354. //make sure panel is there
  1355. if (el) this.setBackButtonText(el.getAttribute("data-title"));
  1356. else this.setBackButtonText("Back");
  1357. }
  1358. } else if (this.activeDiv.getAttribute("data-title")) this.setBackButtonText(this.activeDiv.getAttribute("data-title"));
  1359. else this.setBackButtonText("Back");
  1360. if (what.getAttribute("data-title")) {
  1361. this.setTitle(what.getAttribute("data-title"));
  1362. }
  1363. if (newTab) {
  1364. this.setBackButtonText(this.firstDiv.getAttribute("data-title"));
  1365. if (what == this.firstDiv) {
  1366. this.history.length = 0;
  1367. }
  1368. }
  1369. $("#header #menubadge").css("float", "right");
  1370. if (this.history.length === 0) {
  1371. this.setBackButtonVisibility(false);
  1372. this.history = [];
  1373. $("#header #menubadge").css("float", "left");
  1374. } else if (this.showBackButton && this.showBackbutton) this.setBackButtonVisibility(true);
  1375. this.activeDiv = what;
  1376. if (this.scrollingDivs[this.activeDiv.id]) {
  1377. this.scrollingDivs[this.activeDiv.id].enable(this.resetScrollers);
  1378. }
  1379. },
  1380. /**
  1381. * This is called internally by loadContent. Here we are using Ajax to fetch the data
  1382. ```
  1383. $.ui.loadDiv("page.html",false,false,"up");
  1384. ```
  1385. * @param {String} target
  1386. * @param {Boolean} newtab (resets history)
  1387. * @param {Boolean} go back (initiate the back click)
  1388. * @param {String} transition
  1389. * @title $.ui.loadDiv(target,newTab,goBack,transition);
  1390. * @api private
  1391. */
  1392. loadAjax: function (target, newTab, back, transition, anchor) {
  1393. // XML Request
  1394. if (this.activeDiv.id == "afui_ajax" && target == this.ajaxUrl) return;
  1395. var urlHash = "url" + crc32(target); //Ajax urls
  1396. var that = this;
  1397. if (target.indexOf("http") == -1) target = intel.xdk.webRoot + target;
  1398. var xmlhttp = new XMLHttpRequest();
  1399. if (anchor && typeof (anchor) !== "object") {
  1400. anchor = document.createElement("a");
  1401. anchor.setAttribute("data-persist-ajax", true);
  1402. }
  1403. anchor = anchor || document.createElement("a");
  1404. xmlhttp.onreadystatechange = function () {
  1405. if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  1406. this.doingTransition = false;
  1407. var refreshFunction;
  1408. var doReturn = false;
  1409. var retainDiv = $.query("#" + urlHash);
  1410. //Here we check to see if we are retaining the div, if so update it
  1411. if (retainDiv.length > 0) {
  1412. that.updatePanel(urlHash, xmlhttp.responseText);
  1413. retainDiv.get(0).setAttribute("data-title", anchor.title ? anchor.title : target);
  1414. } else if (anchor.getAttribute("data-persist-ajax") || that.isAjaxApp) {
  1415. var refresh = (anchor.getAttribute("data-pull-scroller") === 'true') ? true : false;
  1416. refreshFunction = refresh ? function () {
  1417. anchor.refresh = true;
  1418. that.loadContent(target, newTab, back, transition, anchor);
  1419. anchor.refresh = false;
  1420. } : null;
  1421. //that.addContentDiv(urlHash, xmlhttp.responseText, refresh, refreshFunction);
  1422. var contents = $(xmlhttp.responseText);
  1423. if (contents.hasClass("panel")) {
  1424. urlHash = contents.attr("id");
  1425. contents = contents.get(0).innerHTML;
  1426. }
  1427. else
  1428. contents = contents.html();
  1429. if ($("#" + urlHash).length > 0) {
  1430. that.updatePanel("#" + urlHash, contents);
  1431. } else if ($("div.panel[data-crc='" + urlHash + "']").length > 0) {
  1432. that.updatePanel($("div.panel[data-crc='" + urlHash + "']").get(0).id, contents);
  1433. urlHash = $("div.panel[data-crc='" + urlHash + "']").get(0).id;
  1434. } else
  1435. urlHash = that.addContentDiv(urlHash, xmlhttp.responseText, anchor.title ? anchor.title : target, refresh, refreshFunction);
  1436. } else {
  1437. that.updatePanel("afui_ajax", xmlhttp.responseText);
  1438. $.query("#afui_ajax").get(0).setAttribute("data-title", anchor.title ? anchor.title : target);
  1439. that.loadContent("#afui_ajax", newTab, back, transition);
  1440. doReturn = true;
  1441. }
  1442. //Let's load the content now.
  1443. //We need to check for any script tags and handle them
  1444. var div = document.createElement("div");
  1445. $(div).html(xmlhttp.responseText);
  1446. that.parseScriptTags(div);
  1447. if (doReturn) {
  1448. if (that.showLoading) that.hideMask();
  1449. return;
  1450. }
  1451. that.loadContent("#" + urlHash, newTab, back, transition);
  1452. if (that.showLoading) that.hideMask();
  1453. return null;
  1454. }
  1455. };
  1456. this.ajaxUrl = target;
  1457. var newtarget = this.useAjaxCacheBuster ? target + (target.split('?')[1] ? '&' : '?') + "cache=" + Math.random() * 10000000000000000 : target;
  1458. xmlhttp.open("GET", newtarget, true);
  1459. xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  1460. xmlhttp.send();
  1461. // show Ajax Mask
  1462. if (this.showLoading) this.showMask();
  1463. },
  1464. /**
  1465. * This executes the transition for the panel
  1466. ```
  1467. $.ui.runTransition(transition,oldDiv,currDiv,back)
  1468. ```
  1469. * @api private
  1470. * @title $.ui.runTransition(transition,oldDiv,currDiv,back)
  1471. */
  1472. runTransition: function (transition, oldDiv, currWhat, back) {
  1473. if (!this.availableTransitions[transition]) transition = 'default';
  1474. this.availableTransitions[transition].call(this, oldDiv, currWhat, back);
  1475. },
  1476. /**
  1477. * This is callled when you want to launch afui. If autoLaunch is set to true, it gets called on DOMContentLoaded.
  1478. * If autoLaunch is set to false, you can manually invoke it.
  1479. ```
  1480. $.ui.autoLaunch=false;
  1481. $.ui.launch();
  1482. ```
  1483. * @title $.ui.launch();
  1484. */
  1485. launch: function () {
  1486. if (this.hasLaunched === false || this.launchCompleted) {
  1487. this.hasLaunched = true;
  1488. return;
  1489. }
  1490. var that = this;
  1491. this.viewportContainer = af.query("#afui");
  1492. this.navbar = af.query("#navbar").get(0);
  1493. this.content = af.query("#content").get(0);
  1494. this.header = af.query("#header").get(0);
  1495. this.menu = af.query("#menu").get(0);
  1496. //set anchor click handler for UI
  1497. this.viewportContainer.on("click", "a", function (e) {
  1498. if (that.useInteralRouting)
  1499. checkAnchorClick(e, e.currentTarget);
  1500. });
  1501. //enter-edit scroll paddings fix
  1502. //focus scroll adjust fix
  1503. var enterEditEl = null;
  1504. //on enter-edit keep a reference of the actioned element
  1505. $.bind($.touchLayer, 'enter-edit', function (el) {
  1506. enterEditEl = el;
  1507. });
  1508. //enter-edit-reshape panel padding and scroll adjust
  1509. $.bind($.touchLayer, 'enter-edit-reshape', function () {
  1510. //onReshape UI fixes
  1511. //check if focused element is within active panel
  1512. var jQel = $(enterEditEl);
  1513. var jQactive = jQel.closest(that.activeDiv);
  1514. if (jQactive && jQactive.size() > 0) {
  1515. if ($.os.ios || $.os.chrome) {
  1516. var paddingTop, paddingBottom;
  1517. if (document.body.scrollTop) {
  1518. paddingTop = document.body.scrollTop - jQactive.offset().top;
  1519. } else {
  1520. paddingTop = 0;
  1521. }
  1522. //not exact, can be a little above the actual value
  1523. //but we haven't found an accurate way to measure it and this is the best so far
  1524. //paddingBottom = jQactive.offset().bottom - jQel.offset().bottom;
  1525. paddingBottom = 0;
  1526. that.scrollingDivs[that.activeDiv.id].setPaddings(paddingTop, paddingBottom);
  1527. } else if ($.os.android || $.os.blackberry) {
  1528. var elPos = jQel.offset();
  1529. var containerPos = jQactive.offset();
  1530. if (elPos.bottom > containerPos.bottom && elPos.height < containerPos.height) {
  1531. //apply fix
  1532. that.scrollingDivs[that.activeDiv.id].scrollToItem(jQel, 'bottom');
  1533. }
  1534. }
  1535. }
  1536. });
  1537. if ($.os.ios) {
  1538. $.bind($.touchLayer, 'exit-edit-reshape', function () {
  1539. if (that.activeDiv && that.activeDiv.id && that.scrollingDivs.hasOwnProperty(that.activeDiv.id)) {
  1540. that.scrollingDivs[that.activeDiv.id].setPaddings(0, 0);
  1541. }
  1542. });
  1543. }
  1544. //elements setup
  1545. if (!this.navbar) {
  1546. this.navbar = $.create("div", {
  1547. id: "navbar"
  1548. }).get(0);
  1549. this.viewportContainer.append(this.navbar);
  1550. }
  1551. if (!this.header) {
  1552. this.header = $.create("div", {
  1553. id: "header"
  1554. }).get(0);
  1555. this.viewportContainer.prepend(this.header);
  1556. }
  1557. if (!this.menu) {
  1558. this.menu = $.create("div", {
  1559. id: "menu",
  1560. html: '<div id="menu_scroller"></div>'
  1561. }).get(0);
  1562. this.viewportContainer.append(this.menu);
  1563. this.menu.style.overflow = "hidden";
  1564. this.scrollingDivs.menu_scroller = $.query("#menu_scroller").scroller({
  1565. scrollBars: true,
  1566. verticalScroll: true,
  1567. vScrollCSS: "afScrollbar",
  1568. useJsScroll: !$.feat.nativeTouchScroll,
  1569. noParent: $.feat.nativeTouchScroll,
  1570. autoEnable: true,
  1571. lockBounce: this.lockPageBounce
  1572. });
  1573. if ($.feat.nativeTouchScroll) $.query("#menu_scroller").css("height", "100%");
  1574. /*this.asideMenu = $.create("div", {
  1575. id: "aside_menu",
  1576. html: '<div id="aside_menu_scroller"></div>'
  1577. }).get(0);
  1578. this.viewportContainer.append(this.asideMenu);
  1579. this.asideMenu.style.overflow = "hidden";
  1580. this.scrollingDivs.menu_scroller = $.query("#aside_menu_scroller").scroller({
  1581. scrollBars: true,
  1582. verticalScroll: true,
  1583. vScrollCSS: "afScrollbar",
  1584. useJsScroll: !$.feat.nativeTouchScroll,
  1585. noParent: $.feat.nativeTouchScroll,
  1586. autoEnable: true,
  1587. lockBounce: this.lockPageBounce
  1588. });
  1589. if ($.feat.nativeTouchScroll) $.query("#aside_menu_scroller").css("height", "100%");
  1590. */
  1591. }
  1592. if (!this.content) {
  1593. this.content = $.create("div", {
  1594. id: "content"
  1595. }).get(0);
  1596. this.viewportContainer.append(this.content);
  1597. }
  1598. //insert backbutton (should optionally be left to developer..)
  1599. $(this.header).html('<a id="backButton" class="icon back"></a>' + this.header.innerHTML);
  1600. // this.backButton = $.query("#header #backButton").css("visibility", "hidden");
  1601. $(document).on("click", "#header #backButton", function (e) {
  1602. e.preventDefault();
  1603. that.goBackHistory();
  1604. });
  1605. //page title (should optionally be left to developer..)
  1606. this.titleBar = $.query("#header #pageTitle").get(0);
  1607. //setup ajax mask
  1608. // this.addContentDiv("afui_ajax", "");
  1609. var maskDiv = $.create("div", {
  1610. id: "afui_mask",
  1611. className: "ui-loader",
  1612. html: "<span class='ui-icon ui-icon-loading spin'></span><h1>Loading Content</h1>"
  1613. }).css({
  1614. 'z-index': 20000,
  1615. display: "none"
  1616. });
  1617. document.body.appendChild(maskDiv.get(0));
  1618. //setup modalDiv//ax4
  1619. var modalDiv = $.create("div", {
  1620. id: "afui_modal"
  1621. }).get(0);
  1622. modalDiv.appendChild($.create("div", {
  1623. id: 'modalContainer'
  1624. }).get(0));
  1625. this.modalTransContainer = $.create("div", {
  1626. id: "modalTransContainer"
  1627. }).appendTo(modalDiv).get(0);
  1628. this.viewportContainer.append(modalDiv);
  1629. this.scrollingDivs.modal_container = $.query("#modalContainer").scroller({
  1630. scrollBars: true,
  1631. vertical: true,
  1632. vScrollCSS: "afScrollbar",
  1633. noParent: true,
  1634. lockBounce: this.lockPageBounce
  1635. });
  1636. this.modalWindow = modalDiv;
  1637. //get first div, defer
  1638. var defer = {};
  1639. var contentDivs = this.viewportContainer.get(0).querySelectorAll(".panel");
  1640. for (var i = 0; i < contentDivs.length; i++) {
  1641. var el = contentDivs[i];
  1642. var tmp = el;
  1643. var id;
  1644. var prevSibling = el.previousSibling;
  1645. if (el.parentNode && el.parentNode.id != "content") {
  1646. el.parentNode.removeChild(el);
  1647. id = el.id;
  1648. if (tmp.getAttribute("selected")) this.firstDiv = $.query("#" + id).get(0);
  1649. this.addDivAndScroll(tmp);
  1650. $.query("#" + id).insertAfter(prevSibling);
  1651. } else if (!el.parsedContent) {
  1652. el.parsedContent = 1;
  1653. el.parentNode.removeChild(el);
  1654. id = el.id;
  1655. if (tmp.getAttribute("selected")) this.firstDiv = $.query("#" + id).get(0);
  1656. this.addDivAndScroll(tmp);
  1657. $.query("#" + id).insertAfter(prevSibling);
  1658. }
  1659. if (el.getAttribute("data-defer")) {
  1660. defer[id] = el.getAttribute("data-defer");
  1661. }
  1662. if (!this.firstDiv) this.firstDiv = $.query("#" + id).get(0);
  1663. el = null;
  1664. }
  1665. contentDivs = null;
  1666. var loadingDefer = false;
  1667. var toLoad = Object.keys(defer).length;
  1668. if (toLoad > 0) {
  1669. loadingDefer = true;
  1670. var loaded = 0;
  1671. for (var j in defer) {
  1672. (function (j) {
  1673. $.ajax({
  1674. url: intel.xdk.webRoot + defer[j],
  1675. success: function (data) {
  1676. if (data.length > 0) {
  1677. that.updatePanel(j, data);
  1678. that.parseScriptTags($.query("#" + j).get(0));
  1679. }
  1680. loaded++;
  1681. if (loaded >= toLoad) {
  1682. loadingDefer = false;
  1683. $(document).trigger("defer:loaded");
  1684. }
  1685. },
  1686. error: function (msg) {
  1687. //still trigger the file as being loaded to not block $.ui.ready
  1688. console.log("Error with deferred load " + intel.xdk.webRoot + defer[j]);
  1689. loaded++;
  1690. if (loaded >= toLoad) {
  1691. loadingDefer = false;
  1692. $(document).trigger("defer:loaded");
  1693. }
  1694. }
  1695. });
  1696. })(j);
  1697. }
  1698. }
  1699. if (this.firstDiv) {
  1700. this.activeDiv = this.firstDiv;
  1701. if (this.scrollingDivs[this.activeDiv.id]) {
  1702. this.scrollingDivs[this.activeDiv.id].enable();
  1703. }
  1704. var loadFirstDiv = function () {
  1705. //ax5
  1706. $.query("#navbar").append($.create("footer", {
  1707. id: 'defaultNav'
  1708. }).append($.query("#navbar").children()));
  1709. that.defaultFooter = "defaultNav";
  1710. that.prevFooter = $.query("#defaultNav");
  1711. that.updateNavbarElements(that.prevFooter);
  1712. //setup initial menu
  1713. var firstMenu = $.query("nav").get(0);
  1714. if (firstMenu) {
  1715. that.defaultMenu = $(firstMenu);
  1716. that.updateSideMenuElements(that.defaultMenu);
  1717. that.prevMenu = that.defaultMenu;
  1718. }
  1719. //get default header
  1720. that.defaultHeader = "defaultHeader";
  1721. $.query("#header").append($.create("header", {
  1722. id: 'defaultHeader'
  1723. }).append($.query("#header").children()));
  1724. that.prevHeader = $.query("#defaultHeader");
  1725. //ax6
  1726. $.query("#navbar >*>*").on("click", function (e) {
  1727. $.query("#navbar >*>*").not(e.currentTarget).removeClass("pressed");
  1728. $(e.currentTarget).addClass("pressed");
  1729. });
  1730. //update the width
  1731. var footerLinks = $.query("#navbar >*>*");
  1732. if (footerLinks.length > 0) {
  1733. footerLinks.css("width", (100 / footerLinks.length) + "%");
  1734. }
  1735. //There is a bug in chrome with @media queries where the header was not getting repainted
  1736. if ($.query("nav").length > 0) {
  1737. $.query("#afui #header").addClass("hasMenu");
  1738. $.query("#afui #content").addClass("hasMenu");
  1739. $.query("#afui #navbar").addClass("hasMenu");
  1740. $.query("#afui #menu").addClass("tabletMenu");
  1741. }
  1742. //go to activeDiv
  1743. var firstPanelId = that.getPanelId(defaultHash);
  1744. //that.history=[{target:'#'+that.firstDiv.id}]; //set the first id as origin of path
  1745. var isFirstPanel = !!(firstPanelId == "#" + that.firstDiv.id);
  1746. if (firstPanelId.length > 0 && that.loadDefaultHash && !isFirstPanel) {
  1747. that.loadContent(defaultHash, true, false, 'none'); //load the active page as a newTab with no transition
  1748. }
  1749. else {
  1750. previousTarget = "#" + that.firstDiv.id;
  1751. that.firstDiv.style.display = "block";
  1752. //Let's check if it has a function to run to update the data
  1753. that.parsePanelFunctions(that.firstDiv);
  1754. //Need to call after parsePanelFunctions, since new headers can override
  1755. that.loadContentData(that.firstDiv);
  1756. // $.query("#header #backButton").css("visibility", "hidden");
  1757. if (that.firstDiv.getAttribute("data-modal") == "true" || that.firstDiv.getAttribute("modal") == "true") {
  1758. that.showModal(that.firstDiv.id);
  1759. }
  1760. }
  1761. that.launchCompleted = true;
  1762. //trigger ui ready
  1763. $.query("#afui #splashscreen").remove();
  1764. //Fix a bug with android dispatching events in the middle of other code execution
  1765. setTimeout(function () {
  1766. $(document).trigger("afui:ready");
  1767. });
  1768. };
  1769. if (loadingDefer) {
  1770. $(document).one("defer:loaded", loadFirstDiv);
  1771. } else loadFirstDiv();
  1772. }
  1773. $.bind(that, "content-loaded", function () {
  1774. if (that.loadContentQueue.length > 0) {
  1775. var tmp = that.loadContentQueue.splice(0, 1)[0];
  1776. that.loadContent(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
  1777. }
  1778. });
  1779. if (window.navigator.standalone || this.isIntel) {
  1780. this.blockPageScroll();
  1781. }
  1782. this.topClickScroll();
  1783. },
  1784. /**
  1785. * This simulates the click and scroll to top of browsers
  1786. */
  1787. topClickScroll: function () {
  1788. var that = this;
  1789. document.getElementById("header").addEventListener("click", function (e) {
  1790. if (e.clientY <= 15 && e.target.nodeName.toLowerCase() == "h1") //hack - the title spans the whole width of the header
  1791. that.scrollingDivs[that.activeDiv.id].scrollToTop("100");
  1792. });
  1793. },
  1794. /**
  1795. * This blocks the page from scrolling/panning. Usefull for native apps
  1796. */
  1797. blockPageScroll: function () {
  1798. $.query("#afui #header").bind("touchmove", function (e) {
  1799. e.preventDefault();
  1800. });
  1801. },
  1802. /**
  1803. * This is the default transition. It simply shows the new panel and hides the old
  1804. */
  1805. noTransition: function (oldDiv, currDiv, back) {
  1806. currDiv.style.display = "block";
  1807. oldDiv.style.display = "block";
  1808. var that = this;
  1809. that.clearAnimations(currDiv);
  1810. that.css3animate(oldDiv, {
  1811. x: "0%",
  1812. y: 0
  1813. });
  1814. that.finishTransition(oldDiv);
  1815. currDiv.style.zIndex = 2;
  1816. oldDiv.style.zIndex = 1;
  1817. },
  1818. /**
  1819. * This must be called at the end of every transition to hide the old div and reset the doingTransition variable
  1820. *
  1821. * @param {Object} Div that transitioned out
  1822. * @title $.ui.finishTransition(oldDiv)
  1823. */
  1824. finishTransition: function (oldDiv, currDiv) {
  1825. oldDiv.style.display = 'none';
  1826. this.doingTransition = false;
  1827. if (currDiv) this.clearAnimations(currDiv);
  1828. if (oldDiv) this.clearAnimations(oldDiv);
  1829. $.trigger(this, "content-loaded");
  1830. },
  1831. /**
  1832. * This must be called at the end of every transition to remove all transforms and transitions attached to the inView object (performance + native scroll)
  1833. *
  1834. * @param {Object} Div that transitioned out
  1835. * @title $.ui.finishTransition(oldDiv)
  1836. */
  1837. clearAnimations: function (inViewDiv) {
  1838. inViewDiv.style[$.feat.cssPrefix + 'Transform'] = "none";
  1839. inViewDiv.style[$.feat.cssPrefix + 'Transition'] = "none";
  1840. }
  1841. /**
  1842. * END
  1843. * @api private
  1844. */
  1845. };
  1846. //lookup for a clicked anchor recursively and fire UI own actions when applicable
  1847. var checkAnchorClick = function (e, theTarget) {
  1848. if (theTarget == (afui)) {
  1849. return;
  1850. }
  1851. //this technique fails when considerable content exists inside anchor, should be recursive ?
  1852. if (theTarget.tagName.toLowerCase() != "a" && theTarget.parentNode) return checkAnchorClick(e, theTarget.parentNode); //let's try the parent (recursive)
  1853. //anchors
  1854. if (theTarget.tagName !== "undefined" && theTarget.tagName.toLowerCase() == "a") {
  1855. var theTargetAjax = theTarget.getAttribute("data-load-ajax");//ax1
  1856. if (!theTargetAjax) return false;//ax2
  1857. var custom = (typeof $.ui.customClickHandler == "function") ? $.ui.customClickHandler : false;
  1858. if (custom !== false) {
  1859. if ($.ui.customClickHandler(theTarget, e)) return e.preventDefault();
  1860. }
  1861. if (theTarget.href.toLowerCase().indexOf("javascript:") !== -1 || theTarget.getAttribute("data-ignore")) {
  1862. return;
  1863. }
  1864. if (theTarget.href.indexOf("tel:") === 0) return false;
  1865. //external links
  1866. if (theTarget.hash.indexOf("#") === -1 && theTarget.target.length > 0) {
  1867. if (theTarget.href.toLowerCase().indexOf("javascript:") !== 0) {
  1868. if ($.ui.isIntel) {
  1869. e.preventDefault();
  1870. intel.xdk.device.launchExternal(theTarget.href);
  1871. } else if (!$.os.desktop) {
  1872. e.target.target = "_blank";
  1873. }
  1874. }
  1875. return;
  1876. }
  1877. /* IE 10 fixes*/
  1878. var href = theTarget.href,
  1879. prefix = location.protocol + "//" + location.hostname + ":" + location.port + location.pathname;
  1880. if (href.indexOf(prefix) === 0) {
  1881. href = href.substring(prefix.length);
  1882. }
  1883. //empty links
  1884. if (href == "#" || (href.indexOf("#") === href.length - 1) || (href.length === 0 && theTarget.hash.length === 0)) return e.preventDefault();
  1885. //internal links
  1886. e.preventDefault();
  1887. var mytransition = theTarget.getAttribute("data-transition");
  1888. var resetHistory = theTarget.getAttribute("data-resetHistory");
  1889. resetHistory = resetHistory && resetHistory.toLowerCase() == "true" ? true : false;
  1890. href = theTarget.hash.length > 0 ? theTarget.hash : href;
  1891. $.ui.loadContent(href, resetHistory, 0, mytransition, theTarget);
  1892. return;
  1893. }
  1894. };
  1895. 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 */
  1896. var crc32 = function ( /* String */ str, /* Number */ crc) {
  1897. if (crc === undefined) crc = 0;
  1898. var n = 0; //a number between 0 and 255
  1899. var x = 0; //an hex number
  1900. crc = crc ^ (-1);
  1901. for (var i = 0, iTop = str.length; i < iTop; i++) {
  1902. n = (crc ^ str.charCodeAt(i)) & 0xFF;
  1903. x = "0x" + table.substr(n * 9, 8);
  1904. crc = (crc >>> 8) ^ x;
  1905. }
  1906. return crc ^ (-1);
  1907. };
  1908. $.ui = new ui();
  1909. $.ui.init = true;
  1910. $(window).trigger('afui:preinit');
  1911. $(window).trigger('afui:init');
  1912. })(af);
  1913. //The following functions are utilitiy functions for afui within intel xdk.
  1914. (function ($) {
  1915. $(document).one("intel.xdk.device.ready", function () { //in intel xdk, we need to undo the height stuff since it causes issues.
  1916. $.ui.isIntel = true;
  1917. setTimeout(function () {
  1918. document.getElementById('afui').style.height = "100%";
  1919. document.body.style.height = "100%";
  1920. document.documentElement.style.minHeight = window.innerHeight;
  1921. }, 300);
  1922. $.ui.ready(function () {
  1923. $.ui.blockPageScroll();
  1924. });
  1925. });
  1926. //Fix an ios bug where scrolling will not work with rotation
  1927. if ($.feat.nativeTouchScroll) {
  1928. document.addEventListener("orientationchange", function (e) {
  1929. if ($.ui.scrollingDivs[$.ui.activeDiv.id]) {
  1930. var tmpscroller = $.ui.scrollingDivs[$.ui.activeDiv.id];
  1931. if (tmpscroller.el.scrollTop === 0) {
  1932. tmpscroller.disable();
  1933. setTimeout(function () {
  1934. tmpscroller.enable();
  1935. }, 300);
  1936. }
  1937. }
  1938. });
  1939. }
  1940. })(af);