/* * Copyright (c) 2014, The Linux Foundation. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * Neither the name of The Linux Foundation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ var cardElements; var currentActiveCard = null; var defaultActiveCard = null; // Forward navigation >= 0, backward navigation == -1, unknown navigation == -2 var currentNavigationType = -2; var blankRegEx = /^\s*$/; var WMLBrowser = { name: "WMLBrowserContext", variables: new Object(), setVar: function (key, value) { this.variables[key] = value; }, getVar: function (key) { var value = this.variables[key]; if (value == null) value = ''; return value; }, newContext: function () { this.variables = new Object(); }, } function isBlank(str) { return (!str || blankRegEx.test(str)); } function isURLHash(str) { return (!isBlank(str) && str.length > 1 && str.indexOf("#") == 0); } function getIdFromURLHash(hash) { return hash.substr(1); } window.onhashchange = function() { console.log("window.onhashchange currentNavigationType = " + currentNavigationType); var newHash = window.location.hash; if (!isURLHash(newHash)) { currentActiveCard.style.display = "none"; defaultActiveCard.style.display = "initial"; currentActiveCard = defaultActiveCard; } else { showCardById(newHash.substr(1)); } updateWMLVariables(); scheduleTimerTaskIfNeeded(currentActiveCard); handleOnNavigationIntrinsicEvent(); }; window.onload = function() { // Consider all the load/reload on this deck as forward navigation. currentNavigationType = 1; var cardHash = window.location.hash; console.log("window.onload card = " + cardHash); cardElements = document.getElementsByClassName('wml_card'); defaultActiveCard = cardElements[0]; // All the cards are hidden by default. // Show the active card onload. if (isURLHash(cardHash)) { var cardId = cardHash.substr(1); for(var i=0, l=cardElements.length; i= 0) { executeOnenterforwardTask(); } else if (navigationType == -1) { executeOnenterbackwardTask(); } else { console.log("WARNING: Cannot determine the navigation event type on this card!"); } } ////////////////////////// WML Variables //////////////////////////////////////// function replaceVariablesInTextContentBySpan() { var pattern1 = /(\$\(([_a-z]{1}[_a-z0-9]*)([:]{1}((([e]{1})(scape)?)|(([n]{1})(oesc)?)|(([u]{1})(nesc)?)))?\))/gi; var pattern2 = /(\$([_a-zA-z]{1}[_a-zA-Z0-9]*))/g; var whitespace = /^\s*$/g; var replacer = function () { var name = arguments[2]; var escape = ""; if (arguments[12]) escape = arguments[12]; else if (arguments[9]) escape = arguments[9]; else if (arguments[6]) escape = arguments[6]; var wml_variable_span = "\\<\/span\>"; console.log("replaceVariablesInTextContentBySpan() Found variable " + arguments[0]); return wml_variable_span; }; var textNodes = []; var treeWalker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false); while(treeWalker.nextNode()) { textNodes.push(treeWalker.currentNode); } for(var i=0, l=textNodes.length; i 0 && index <= optionsCount) { options[index-1].selected = 'selected'; } } } // Keep a copy of selected options to process 'onpick' events var options = select.selectedOptions; var optionsCount = options.length; var optionsList = []; for(var j=0; j href = " + href); refreshVariablesInControlElements(); if (!isBlank(search)) { node.search = substituteVariablesInURL(search); } else { node.href = href.split("?")[0]; } event.preventDefault(); navigateToURL(node.href); return false; } //////////////////////// Variables ///////////////////////// function substituteVariablesInURL(url) { var pattern = /(((\%24)|(\$))\(([_a-z]{1}[_a-z0-9]*)([:]{1}((([e]{1})(scape)?)|(([n]{1})(oesc)?)|(([u]{1})(nesc)?)))?\))/gi; var replacer = function () { var name = arguments[2]; // TODO: Do the URL escaping here console.log("substituteVariablesInURL() found variable : " + arguments[0]); return WMLBrowser.getVar(name); }; return url.replace(pattern, replacer); } function substituteVariablesInPostfield(value) { var pattern1 = /(\$\(([_a-z]{1}[_a-z0-9]*)([:]{1}((([e]{1})(scape)?)|(([n]{1})(oesc)?)|(([u]{1})(nesc)?)))?\))/gi; var pattern2 = /(\$([_a-zA-z]{1}[_a-zA-Z0-9]*))/g; var replacer = function () { var name = arguments[2]; // TODO: Do the URL escaping here console.log("substituteVariablesInPostfield() found variable : " + arguments[0]); return WMLBrowser.getVar(name); }; if (pattern1.test(value)) { return value.replace(pattern1, replacer); } else if (pattern2.test(value)) { return value.replace(pattern2, replacer); } } function refreshVariableInSelectElement(select) { var options = select.selectedOptions; var value = ""; var ivalue = ""; var optionsCount = options.length; if (optionsCount > 0) { var op = options[0]; value = op.value; ivalue = (op.index + 1); for(var i=1; i name = " + input.name + ", value = " + input.value); } var selectElements = currentActiveCard.getElementsByClassName('wml_select'); for(var i=0, l=selectElements.length; i\?\@\\\^\_\`\{\|\}\~\\-\\\\[\\]"; var inputElements = document.getElementsByClassName('wml_input'); for(var i=0, l=inputElements.length; i name = " + input.name + ", value = " + input.value); } } /////////////// Navigation //////////////////// function navigateToURL(url) { console.log("navigateToURL: url = " + url); cancelScheduledTimerTask(); currentNavigationType = 1; window.location = url; } function navigateToCard(card) { console.log("navigateToCard: card = " + card); cancelScheduledTimerTask(); currentNavigationType = 1; window.location.hash = card; } function navigateBack() { console.log("navigateBack: currentState = "); console.log(window.history.state); cancelScheduledTimerTask(); currentNavigationType = -1; window.history.back(); } ////////////// WML Tasks ////////////////////// // function updateWMLVariables() { var wmlVariables = currentActiveCard.getElementsByClassName('wml_variable'); for(var i=0, l=wmlVariables.length; i " + setvar.dataset.wml_name + " = " + setvar.dataset.wml_value); } updateWMLVariables(); } function executeRefreshTask(event, node) { event.preventDefault(); internal_executeRefreshTask(node.parentNode); return false; } // function internal_executePrevTask(node) { console.log("internal_executePrevTask"); internal_executeRefreshTask(node.parentNode); navigateBack(); } function executePrevTask(event, node) { event.preventDefault(); internal_executePrevTask(node); return false; } //