-
Notifications
You must be signed in to change notification settings - Fork 0
/
cya.min.js
4 lines (4 loc) · 8.06 KB
/
cya.min.js
1
2
3
4
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
// @source ./cya.js
"use strict";var cya={start_page:"start",ending_page:"ending",playfield:"cya_playfield",enable_hotkeys:true,scroll_view:undefined,scroll_spacer:undefined,scroll_to_chosen_choice:false,pivot_choices:false,validate_on_load:false,};(function(){var page_cache={};var current_choices=[];var current_hotkeys={};var DEFAULT_HOTKEYS=["1","2","3","4","5","6","7","8","9","0"];var deferred_pages=[];var inside_page=false;var element_parameter=function(el){if(el==null)return null;else if(el instanceof Element)return el;else return document.getElementById(el);}var find_page=function(page){var ret=undefined;if(page instanceof Element)ret=page;else ret=page_cache[page];if(ret==undefined){if(cya.pages.namedItem)ret=cya.pages.namedItem(page);if(ret==undefined){for(var i=0;cya.pages[i];++i){if(cya.pages[i].getAttribute("name")==page){ret=cya.pages[i];break;}}}page_cache[page]=ret;}return ret;}var clear_old_choices=function(accepted_choice){var node_to_pop=undefined;var last_node=undefined;for(var i=0;current_choices[i];++i){var dis=current_choices[i];dis.node.removeAttribute("href");dis.node.removeAttribute("accesskey");if(dis==accepted_choice){dis.node.setAttribute("class","cya_accepted_choice");if(cya.pivot_choices&¤t_choices[1]!=undefined)node_to_pop=dis.node;}else{dis.node.setAttribute("class","cya_rejected_choice");last_node=dis.node;}dis.node.removeEventListener("click",dis.listener);}if(node_to_pop!=undefined&&last_node!=undefined&&last_node.parentNode!=undefined){node_to_pop.remove();last_node.parentNode.appendChild(node_to_pop);}current_choices=[];current_hotkeys={};}var choice_listener=function(){var first_choice=current_choices[0];clear_old_choices(this);var target_page=this.target_page;if(this.code){var code_page=this.code();if(code_page!=undefined)target_page=code_page;}if(target_page){cya.page(this.target_page);var scroll_view=element_parameter(cya.scroll_view);if(scroll_view!=undefined){var y=0;var el=cya.scroll_to_chosen_choice?this.node:first_choice.node;while(el!=undefined&&el!=scroll_view){y+=(el.offsetTop-el.scrollTop+el.clientTop);el=el.offsetParent;}if(el==scroll_view){var spacer=element_parameter(cya.scroll_spacer);if(spacer!=undefined){spacer.setAttribute("style","height:100%");var space=Math.max(0,scroll_view.clientHeight-(scroll_view.scrollHeight-y-spacer.clientHeight));spacer.setAttribute("style","height:"+space);}scroll_view.scrollTop=y;}else if(console!=undefined)console.log("A <choice> was selected that was not a child of the scroll_view!");}}}var node_code=function(node,lowerName){if(node.cached_code!==undefined)return node.cached_code;if(lowerName==undefined)lowerName=node.tagName.toLowerCase();switch(lowerName){case"choice":var all_code=[];if(node.hasAttribute("execute"))all_code.push(node.getAttribute("execute"));if(node.hasAttribute("eval"))all_code.push("return ("+node.getAttribute("eval")+")");if(all_code.length>0)node.cached_code=new Function(all_code.join("\n"));break;case"if":if(node.hasAttribute("condition"))node.cached_code=new Function("return ("+node.getAttribute("condition")+")");break;case"eval":if(node.cached_code==undefined)node.cached_code=new Function("return ("+node.innerText+")");break;case"execute":if(node.cached_code==undefined)node.cached_code=new Function(node.innerText);break;}if(node.cached_code===undefined)node.cached_code=null;return node.cached_code;}var register_choice=function(real_node,source_node){var choice={node:real_node};current_choices.push(choice);if(source_node.hasAttribute("target")){choice.target_page=source_node.getAttribute("target");}choice.code=node_code(source_node,"choice");choice.listener=choice_listener.bind(choice);real_node.addEventListener("click",choice.listener)var chosen_hotkey=undefined;if(source_node.hasAttribute("hotkey"))chosen_hotkey=source_node.getAttribute("hotkey");if(chosen_hotkey==undefined){for(var i in DEFAULT_HOTKEYS){var hotkey=DEFAULT_HOTKEYS[i];if(current_hotkeys[hotkey]==undefined){chosen_hotkey=hotkey;break;}}}if(chosen_hotkey!=undefined){choice.hotkey=chosen_hotkey;current_hotkeys[chosen_hotkey]=choice var hotspan=document.createElement("span");hotspan.setAttribute("class","cya_hotkey");hotspan.appendChild(document.createTextNode(chosen_hotkey));choice.node.appendChild(hotspan);choice.node.setAttribute("accesskey",chosen_hotkey);}}var append_and_translate_children=function(target,source){if(source.childNodes==undefined)return;for(var i=0;source.childNodes[i];++i){var child=source.childNodes[i];if(child.nodeType==document.ELEMENT_NODE){var lowerName=child.tagName.toLowerCase();switch(lowerName){case"choice":var nu=document.createElement("a");register_choice(nu,child);nu.setAttribute("class","cya_choice");nu.setAttribute("href","javascript:(void)0");append_and_translate_children(nu,child);target.appendChild(nu);break;case"ending":window.ending=child.innerText;cya.page(cya.ending_page);break;case"if":var code=node_code(child,lowerName);if(code){if(code())append_and_translate_children(target,child);}else if(console!=undefined)console.log("There is an <if> without a condition");break;case"eval":case"execute":var code=node_code(child,lowerName);if(code){var result=code();if(result!=undefined){if(result instanceof Element)target.appendChild(result);else target.appendChild(document.createTextNode(result));}}break;default:var nu=child.cloneNode(false);append_and_translate_children(nu,child);target.appendChild(nu);break;}}else{target.appendChild(child.cloneNode(true));}}}var keydown=function(e){if(!cya.enable_hotkeys)return;if(e.ctrlKey||e.shiftKey||e.metaKey||e.altKey)return;var target=e.target;if(target!=undefined&&(target.tagName=="INPUT"||target.tagName=="TEXTAREA"))return;var key;if(e.key)key=e.key;else if(e.keyCode)key=string.fromCharCode(e.keyCode);else return;if(current_hotkeys[key]!=undefined){var choice=current_hotkeys[key];if(e.stopPropagation)e.stopPropagation();if(e.preventDefault)e.preventDefault();choice.listener(e);}}cya.page=function(page){if(inside_page){deferred_pages.push(page);return;}inside_page=true;var target=element_parameter(cya.playfield);if(target==undefined){window.alert("Playfield not found! We can't do anything!");return;}var next_page=find_page(page);if(next_page==undefined){window.alert("Page not found: "+page);}else{var to_append=document.createElement("div");to_append.setAttribute("class","cya_page");append_and_translate_children(to_append,next_page);target.appendChild(to_append);}inside_page=false;var deferred_page=deferred_pages.shift();if(deferred_page!=undefined)return cya.page(deferred_page);}var recursively_validate=function(node,pageName){if(node.nodeType!=document.ELEMENT_NODE)return true;var ret=true;var nodeName=node.tagName.toLowerCase();switch(nodeName){case"choice":if(node.hasAttribute("target")){if(find_page(node.getAttribute("target"))==undefined){console.log("page \""+pageName+"\": references a nonexistent page: "+node.getAttribute("target"));ret=false;}}break;}try{node_code(node);}catch(e){console.log("page \""+pageName+"\": error while compiling JavaScript code: "+e.message);ret=false;}if(node.childNodes!=undefined){for(var i=0;node.childNodes[i];++i){ret=recursively_validate(node.childNodes[i],pageName)&&ret;}}return ret;}cya.validate=function(page){var pageName=page.getAttribute("name");if(pageName==undefined)pageName="(an unnamed page)";return recursively_validate(page,pageName);}cya.findpages=function(){var ok=true;cya.pages=document.getElementsByTagName("page");page_cache={}for(var i=0;cya.pages[i];++i){var name=cya.pages[i].getAttribute("name");if(name!=undefined)page_cache[name]=cya.pages[i];else if(console!=undefined)console.log("Warning: found an unnamed <page>!");if(cya.validate_on_load)ok=cya.validate(cya.pages[i])&&ok;}return ok;}var init=function(){if(cya.findpages()){cya.page(cya.start_page);if(cya.scroll_view){cya.scroll_view.scrollTop=0;}document.addEventListener("keydown",keydown,false);}else{var fakePage=document.createElement("page");fakePage.innerText="Adventure validation failed. See the JavaScript console for details.";cya.page(fakePage);}}document.addEventListener("DOMContentLoaded",init,false);})()
// @license-end