-
Notifications
You must be signed in to change notification settings - Fork 0
/
lwp-example-sidebar.js
2 lines (2 loc) · 12.8 KB
/
lwp-example-sidebar.js
1
2
var addFormsAndCategoriesToToolbar,addMathQuillCSS,allNaturalLanguages,codeFormHierarchy,codeFormTranslators,formsAndCategories,groupToExplanation,isGroupRight,lastLanguageChoice,makeSelect,makeText,makeTextarea,markGroupRight,markGroupWith,markGroupWrong,menuItems,niceText,runTranslation,topLevelCategories,hasProp={}.hasOwnProperty,indexOf=[].indexOf||function(e){for(var t=0,n=this.length;t<n;t++)if(t in this&&this[t]===e)return t;return-1},slice=[].slice;setAppName("SidebarApp"),addHelpMenuSourceCodeLink("lwp-example-sidebar/blob/master/lwp-example-sidebar.litcoffee"),window.helpAboutText='<p>See the fully documented <a target="top" href="https://github.com/lurchmath/lwp-example-sidebar/blob/master/lwp-example-sidebar.litcoffee" >source code for this demo app</a>.</p>',window.pluginsToLoad.push("equationeditor"),formsAndCategories={},topLevelCategories=function(){var e,t,n,r,o,a,i,l,s,d;e=[],n=[];for(l in formsAndCategories)if(hasProp.call(formsAndCategories,l)&&"category"===(t=formsAndCategories[l]).type)for(e.push(l),r=0,a=(s=t.contents).length;r<a;r++)l=s[r],indexOf.call(n,l)<0&&n.push(l);for(d=[],o=0,i=e.length;o<i;o++)l=e[o],indexOf.call(n,l)<0&&d.push(l);return d},codeFormHierarchy=function(e){var t,n,r,o,a,i,l,s,d;for(t=[],o=function(t){return{text:t,onclick:null!=e?function(){return e(t)}:function(){var e,n,r,o,a,i,l,s,d,c,u,p;if(r=tinymce.activeEditor,null!=(n=codeFormTranslators[t].example.en)&&(null!=r&&null!=(c=r.selection)&&null!=(u=c.getRng())?u.collapsed:void 0)){for(i=[],l={},o="";s=/<(\/?)([^>]+)>/.exec(n);)o+=n.slice(0,s.index),codeFormTranslators.hasOwnProperty(s[2])?(""===s[1]?(d="open",i.push(a=r.Groups.nextFreeId())):(d="close",a=i.pop()),l[a]=s[2],o+=grouperHTML("codexp",d,a,!1,r.Groups.groupTypes.codexp[d+"Image"])):o+=s[0],e=s.index+s[0].length,n=n.slice(e);o+=n,r.insertContent(o);for(a in l)hasProp.call(l,a)&&(p=l[a],r.Groups[a].set("tagName",p));return $(r.getDoc()).find(".math").each(function(e,t){return t.setAttribute("contenteditable","false"),$(t).addClass("rendered-latex"),$(t).mathquill()})}r.Groups.groupCurrentSelection("codexp").set("tagName",t)}}},r=function(e){var n;return indexOf.call(t,e)>=0?{text:e,menu:[]}:(t.push(e),{text:e,menu:function(){var t,r,o,a;for(a=[],t=0,r=(o=formsAndCategories[e].contents).length;t<r;t++)n=o[t],a.push(d(n));return a}()})},d=function(e){return"form"===formsAndCategories[e].type?o(e):r(e)},s=[],a=0,i=(l=topLevelCategories()).length;a<i;a++)n=l[a],s.push(d(n));return s},addFormsAndCategoriesToToolbar=function(){var e,t,n,r,o;for(window.groupToolbarButtons={},e=1,t=0,n=(o=codeFormHierarchy()).length;t<n;t++)(r=o[t]).type="menubutton",window.groupToolbarButtons["category"+e++]=r;return window.groupToolbarButtons.toggleSidebar={text:"Toggle code view",onclick:function(){var e,t;return e=document.getElementById("sidebar"),t=window.splitter.find(".vsplitter").get(0),"none"===e.style.display?(e.style.display="block",t.style.display="block",splitter.position(splitter.position())):(e.style.display="none",t.style.display="none",window.editorContainer.style.width="100%")}}},window.registerCodeForm=function(e,t){return formsAndCategories[e]={type:"form",validator:t},addFormsAndCategoriesToToolbar(),e},window.registerCategory=function(e,t){return formsAndCategories[e]={type:"category",contents:t},addFormsAndCategoriesToToolbar(),e},codeFormTranslators={},window.registerTranslator=function(e,t,n,r){var o,a;return null==codeFormTranslators[e]&&(codeFormTranslators[e]={}),null==(o=codeFormTranslators[e])[n]&&(o[n]={}),null!=(a=codeFormTranslators[e][n])[t]?a[t]:a[t]=r},runTranslation=function(e,t,n){var r,o,a,i,l,s,d,c,u,p,g,h,m;if(g=function(e){return runTranslation(e,t,n)},i=e.get("tagName"),!codeFormTranslators.hasOwnProperty(i))return"Translation error: Form "+i+" has no translators registered";if(!codeFormTranslators[i].hasOwnProperty(n))return"Translation error: Form "+i+" has no translators of type "+n+" registered";if(!codeFormTranslators[i][n].hasOwnProperty(t))return"Translation error: Form "+i+" has no "+t+" translators of type "+n+" registered";if("string"==typeof(m=codeFormTranslators[i][n][t])){for(c="ABCDEFGHIJKLMNOPQRSTUVWXYZ",u=0;-1<m.indexOf("__"+c[u]+"__");)u++;if(e.children.length!==u)return"Translation error: Form "+i+" requires "+u+" parts, but contains only "+e.children.length;for(l=s=0,d=(h=e.children).length;s<d;l=++s){if(r=h[l],p="__"+c[l]+"__",o=g(r),/^Translation error/.test(o))return o;m=m.replace(RegExp(p,"g"),o)}return m}try{return m(e,g)}catch(e){return a=e,console.log("User-defined translator error:",a)}},window.groupContentsChanged=function(e,t){return window.validate(e)},window.groupDeleted=function(e){if(null!=e.parent)return window.validate(e.parent)},menuItems=function(e){return[{text:"Change this to...",menu:codeFormHierarchy(function(t){return e.set("tagName",t)})},{text:"Explain this...",onclick:function(){var t,n,r,o,a;for(t="",n=0,o=(a=allNaturalLanguages()).length;n<o;n++)t+="<h2>Language: "+(r=a[n])+"</h2>\n"+groupToExplanation(e,r);return tinymce.activeEditor.Dialogs.alert({title:"Explanation of one structure",message:addMathQuillCSS(t),width:600,height:450})}},{text:"Explain all...",onclick:function(){var t,n,r,o,a,i,l,s;for(t="",n=0,a=(l=allNaturalLanguages()).length;n<a;n++)for(t+="<h2>Language: "+(o=l[n])+"</h2>\n",r=0,i=(s=tinymce.activeEditor.Groups.topLevel).length;r<i;r++)e=s[r],""!==t&&(t+="<hr>"),t+=groupToExplanation(e,o);return tinymce.activeEditor.Dialogs.alert({title:"Explanation of all structures",message:addMathQuillCSS(t),width:600,height:450})}}]},allNaturalLanguages=function(){var e,t,n,r;n=[];for(e in codeFormTranslators)if(hasProp.call(codeFormTranslators,e)){r=codeFormTranslators[e].explanation;for(t in r)hasProp.call(r,t)&&indexOf.call(n,t)<0&&n.push(t)}return n},addMathQuillCSS=function(e){return"<html> <head><link rel='stylesheet' href='"+window.location.href.split("/").slice(0,-1).join("/")+"/eqed/mathquill.css'></head> <head><style> "+tinymce.activeEditor.Groups.styleSheet+" </style></head> <body>"+e+"</body> </html>"},groupToExplanation=function(e,t){return"<h4>Structure:</h4>\n <p style='margin-left: 2em;'>"+e.groupAsHTML()+"</p>\n <h4>Explanation:</h4>\n <p style='margin-left: 2em;' >"+runTranslation(e,t,"explanation")+"</p>\n"},window.groupTypes=[{name:"codexp",color:"#6666cc",text:"Code expression",tooltip:"Tag the selection",imageHTML:'<font color="#6666cc">[ ]</font>',openImageHTML:'<font color="#6666cc">[</font>',closeImageHTML:'<font color="#6666cc">]</font>',onToolbar:!1,contentsChanged:window.groupContentsChanged,deleted:window.groupDeleted,tagContents:function(e){return e.get("tagName")},tagMenuItems:function(e){return menuItems(e)},contextMenuItems:function(e){return menuItems(e)}}],markGroupRight=function(e){return e.set("valid",!0),e.clear("closeDecoration"),e.clear("closeHoverText"),window.createSidebarContent()},markGroupWrong=function(e,t){return e.set("valid",!1),e.set("closeDecoration",'<font color="red">✖</font>'),e.set("closeHoverText",t),window.createSidebarContent()},markGroupWith=function(e,t){return e.set("validationResult",t),"valid"===t.result?markGroupRight(e):markGroupWrong(e,t.message)},isGroupRight=function(e){return e.get("valid")},window.validate=function(e,t){var n,r,o,a,i,l,s,d,c,u;{if(e.children){if(null==(a=e.get("tagName")))return markGroupWith(e,{result:"invalid",message:"Each group must have a tag, but this one does not. Add a tag using the context menu."});if((u=formsAndCategories[a].validator)instanceof Array){if(c={result:"valid",message:"All inner groups have the correct types."},o=e.children,u.length!==o.length)return markGroupWith(e,{result:"invalid",message:"This form needs "+u.length+" parts: "+u.join(", ")+"."});for(i=l=0,s=o.length;l<s;i=++l)if(n=o[i],r=n.get("tagName"),d=u[i].split("/"),indexOf.call(d,r)<0)return markGroupWith(e,{result:"invalid",message:"Part "+(i+1)+" must be of type "+u[i]+", but it is of type "+r+" instead."})}else c=u(e,t);return markGroupWith(e,c)}setTimeout(function(){return window.validate(e)},100)}},window.fullScreenEditor=!1,window.editorContainer=function(){return document.getElementById("editorContainer")},window.afterEditorReady=function(e){var t,n;return n=window.editorContainer.parentNode,t=function(){var t,r,o;return t=e.getContainer(),r=e.getContentAreaContainer().firstChild,o=tinymce.DOM.getViewPort(),r.style.width=r.style.height=n.style.height="100%",t.style.width=t.style.height="",r.style.height=n.clientHeight-2-(t.clientHeight-r.clientHeight),window.scrollTo(o.x,o.y)},window.splitter=$(n).split({orientation:"vertical",limit:100,position:"75%",onDrag:t}),$(window).resize(t),t(),window.createSidebarContent()},window.createSidebarContent=function(){var e,t,n,r,o,a;for((a=document.getElementById("sidebar")).innerHTML="<div style=\"border-bottom: solid 1px black;\n text-align: center;\">\n <p>Choose a language:\n <select onchange='updateSidebarContent();'\n id='languagePicker'>\n <option value='javascript'>JavaScript</option>\n <option value='python'>Python</option>\n <option value='r'>R</option>\n </select></p>\n <p id='runJSLink'><a href='#'\n onclick='runGeneratedJavaScript();'\n >Run this code</a></p>\n <p><a href='#' onclick='copyGeneratedCode();'\n >Copy this code</a></p>\n</div>",$("#languagePicker").val(lastLanguageChoice),$("#runJSLink").get(0).style.display="javascript"===lastLanguageChoice?"block":"none",window.lastSidebarContent="",n=0,r=(o=tinymce.activeEditor.Groups.topLevel).length;n<r;n++)t=o[n],(e=document.createElement("div")).setAttribute("id","codeForGroup"+t.id()),$(e).click(t.id(),function(e){return t=tinymce.activeEditor.Groups[e.data],tinymce.activeEditor.selection.setRng(t.innerRange())}),e.style.padding="1em",e.style.borderBottom="dotted 1px black",e.innerHTML=window.createSidebarEntryHTML(t),a.appendChild(e),lastSidebarContent+=e.textContent+"\n";return $(a).find("pre").each(function(e,t){return hljs.highlightBlock(t)})},lastLanguageChoice="javascript",window.updateSidebarContent=function(){return lastLanguageChoice=$("#languagePicker").val(),createSidebarContent()},window.runGeneratedJavaScript=function(){var e;try{return eval(lastSidebarContent)}catch(t){return e=t,alert("Error when running code:\n\n"+e.message)}},window.copyGeneratedCode=function(){return tinymce.activeEditor.Dialogs.alert({title:"Copy the code, then close",width:600,message:"<textarea id='codeToCopy'\n style='width: 100%; height: 100%;\n font-family: monospace;'\n >"+lastSidebarContent+"</textarea>\n<script>\n var T = document.getElementById( 'codeToCopy' );\n T.select();\n T.focus();\n<\/script>"})},window.createSidebarEntryHTML=function(e){var t,n,r;return t=runTranslation(e,lastLanguageChoice,"code"),n=codeFormTranslators.COMMENT.code[lastLanguageChoice],r=n.replace("__A__",niceText.apply(null,e.contentNodes())),"<pre class='javascript'>"+(r+="\n"+(/^Translation error/.test(t)?n.replace("__A__",t):t))+"</pre>"},niceText=function(){var e,t,n,r,o;for(o="",e=0,t=(r=1<=arguments.length?slice.call(arguments,0):[]).length;e<t;e++)3===(null!=(n=r[e])?n.nodeType:void 0)?o+=n.textContent:$(n).hasClass("mathquill-rendered-math")?o+=n.childNodes[0].textContent:o+=niceText.apply(null,n.childNodes);return o},setInterval(function(){var e,t;if(t=tinymce.activeEditor.selection.getRng(),$(sidebar).find("div").css("background-color","#fff"),e=tinymce.activeEditor.Groups.groupAboveSelection(t)){for(;e.parent;)e=e.parent;return $("#codeForGroup"+e.id()).css("background-color","#eee")}},100),window.openInJSFiddle=function(e){var t,n;return n=String.fromCharCode(160),e=e.replace(RegExp(n,"g")," "),(t=document.createElement("form")).setAttribute("method","post"),t.setAttribute("action","http://jsfiddle.net/api/post/mootools/1.2/dependencies/more/"),t.setAttribute("target","_blank"),t.style.display="none",t.appendChild(makeSelect("panel_html",{0:"HTML"})),t.appendChild(makeTextarea("html")),t.appendChild(makeSelect("panel_js",{0:"JavaScript",1:"CoffeeScript",2:"JavaScript 1.7"})),t.appendChild(makeTextarea("js",e)),t.appendChild(makeSelect("panel_css",{0:"CSS",1:"SCSS"})),t.appendChild(makeTextarea("css")),t.appendChild(makeText("title","Fiddle Title Here")),t.appendChild(makeTextarea("description","Fiddle Description Here")),t.appendChild(makeTextarea("resources")),t.appendChild(makeText("dtd","html 4")),t.appendChild(makeText("wrap","l")),document.body.appendChild(t),t.submit(),document.body.removeChild(t)},makeSelect=function(e,t){var n,r,o,a;(o=document.createElement("select")).setAttribute("name",e);for(a in t)hasProp.call(t,a)&&(r=t[a],(n=document.createElement("option")).setAttribute("value",a),n.innerHTML=r,o.appendChild(n));return o},makeTextarea=function(e,t){var n;return(n=document.createElement("textarea")).setAttribute("name",e),n.innerHTML=t||"",n},makeText=function(e,t){var n;return(n=document.createElement("input")).setAttribute("type","text"),n.setAttribute("name",e),n.setAttribute("value",t),n};
//# sourceMappingURL=lwp-example-sidebar.js.map