forked from seisiuneer/abctools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartdraw-export-min.js
1 lines (1 loc) · 15.6 KB
/
smartdraw-export-min.js
1
var SDExportWidthAll=360,SDBatchImageExportStatusText="",SDTheBatchImageExportOKButton=null,SDBatchImageExportCancelRequested=!1,SDTheOKButton=null,SDTheNotationImages=[],SDTuneOrder=[],SDTuneArray=[],SmartDrawTuneCurrent=null,SmartDrawTitles=null,SDExportFormat="0",SDIncipits=[];function SDGenerateFullTextIncipits(){var e,t,a,r,n,o,l,i,s,d,c,u,p,m,S=[];for(e=0,SDIncipits=[];e<totalTunes;++e){var h=r=getTuneByIndex(SDTuneOrder[e]);for(t=0,r=StripTabOne(r=StripChordsOne(r=StripTextAnnotationsOne(r=StripAnnotationsOne(r)))),i=/^M:.*[\r\n]*/gm,d=(s=(r=escape(r=r.replace(i,""))).split("%0A")).length,m="";t<d&&-1==(m=unescape(s[t])).indexOf("K:");++t);m=(m=(m=(m=(m=(m=(m=(m=(m=(m=(m=m.replace("K:","")).trim()).replace("Major","maj")).replace("Minor","min")).replace("Dorian","dor")).replace("Mixolydian","mix")).replace("major","maj")).replace("minor","min")).replace("dorian","dor")).replace("mixolydian","mix")).replace(" ","");var g=[];for(a=0;a<d;++a)if(-1!=(n=unescape(s[a])).indexOf("|")){for(t=0,o=(n=cleanIncipitLine(n)).split("|"),l=[],c=o.length;t<c;++t)""!=o[t]&&l.push(o[t]);for(t=0,c=l.length,u="";t<c;++t)l[t]=l[t].trim(),u+=l[t],t!=c-1&&(u+=" | ");0==(n=u).indexOf(" | ")&&(n=n.substring(3,n.length)),g.push(n)}p=getTuneTitle(h),""!=m&&(p+=" ("+m+")"),S.push({title:p,incipits:g})}for(e=0;e<totalTunes;++e){var D="";D="\n "+S[e].title+" \n\n";var x=S[e].incipits;for(t=0;t<x.length;++t)D+=" ",D+=x[t],D+=" \n";SDIncipits.push(D)}}function SDGenerateIncipits(){for(u=0,SDIncipits=[];u<totalTunes;++u){var e,t,a,r,n,o,l,i,s,d,c,u,p,m,S=e=getTuneByIndex(SDTuneOrder[u]);for(p=0,e=StripTabOne(e=StripChordsOne(e=StripTextAnnotationsOne(e=StripAnnotationsOne(e)))),n=/^M:.*[\r\n]*/gm,l=(o=(e=escape(e=e.replace(n,""))).split("%0A")).length,c="";p<l&&-1==(c=unescape(o[p])).indexOf("K:");++p);for(p=0,c=(c=(c=(c=(c=(c=(c=(c=(c=(c=(c=c.replace("K:","")).trim()).replace("Major","maj")).replace("Minor","min")).replace("Dorian","dor")).replace("Mixolydian","mix")).replace("major","maj")).replace("minor","min")).replace("dorian","dor")).replace("mixolydian","mix")).replace(" ","");p<l;++p)if(-1==(t=unescape(o[p])).indexOf("%%score")&&-1!=t.indexOf("|")){if(p<=l-2)for(m=p+1;m<l;++m){var h=unescape(o[m]);if(-1!=h.indexOf("|")){t+=h;break}}break}for(p=0,a=(t=cleanIncipitLine(t)).split("|"),r=[],i=a.length;p<i;++p)""!=a[p]&&r.push(a[p]);for((i=r.length)>3&&(i=3),s="",p=0;p<i;++p)r[p]=r[p].trim(),s+=r[p],p!=i-1&&(s+=" | ");0==(t=s).indexOf(" | ")&&(t=t.substring(3,t.length)),t=(t=t.replaceAll(" "," ")).length>40?(t=t.substring(0,40)).trim():t.trim(),d=getTuneTitle(S),""!=c&&(d+=" ("+c+")"),t=" "+d+" \n\n "+t+" ",SDIncipits.push(t)}}function SDEncodeABCToolsShareURL(e,t,a,r,n){t=(t=(t=(t=t.trim()).replace(/[^a-zA-Z0-9_\-. ]+/ig,"")).replace(/\s/g,"_")).replace(/\..+$/,"");var o="https://michaeleskin.com/abctools/abctools.html?lzw="+LZString.compressToEncodedURIComponent(e)+"&format="+a+"&ssp="+r+"&pdf=one&pn=br&fp=yes&name="+t;return(n&&(o+="&play=1"),o.length>8100)?"https://michaeleskin.com/abctools/abctools.html":o}function SDInjectPlaybackHeaders(e){return e=InjectStringBelowTuneHeader(e,"%abcjs_soundfont fatboy"),e=InjectStringBelowTuneHeader(e,"%%MIDI program 0"),e=InjectStringBelowTuneHeader(e,"%%MIDI bassprog 0"),e=InjectStringBelowTuneHeader(e,"%%MIDI chordprog 0"),e=InjectStringBelowTuneHeader(e,"%%MIDI bassvol 64"),e=(e=InjectStringBelowTuneHeader(e,"%%MIDI chordvol 64")).replace("\n\n","")}function SDGenerateTuneArray(e){for(var t=[],a=e.split(/^X:/gm),r=a.length,n=1;n<r;++n){var o="X:"+a[n];o=SDInjectPlaybackHeaders(o);var l=getTuneTitle(o),i=GetRadioValue("notenodertab"),s=SDEncodeABCToolsShareURL(o,l,i,10,!0);t.push({name:l,abc:o,ShareURL:s})}return t}function SDDoTextIncipitsExport(e){SDGenerateIncipits(),e(!0)}function SDDoFullTextIncipitsExport(e){SDGenerateFullTextIncipits(),e(!0)}function SDDoBatchImageExport(e){function t(t){n--,SDTheOKButton.click(),SDBatchImageExportCancelRequested?e(!1):0!=n?setTimeout(function(){var e=getTuneByIndex(SDTuneOrder[++o]),t=getTuneTitle(e);SDBatchImageExportStatusText.innerText="Generating SmartDraw image for tune "+(o+1)+" of "+r+": "+t,SDExportImageDialog(e,a,o,null,!1)},100):(SDTheBatchImageExportOKButton.click(),SDBatchImageExportCancelRequested=!1,e(!0))}function a(e,a){SDDownloadJPG(t,a)}var r,n=CountTunes();if(0!=n){var o=0;SDBatchImageExportCancelRequested=!1,SDTheBatchImageExportOKButton=null,SDTheBatchImageExportStatusText=null;var l="Generating SmartDraw image for tune "+(o+1)+" of "+(r=n);l=makeCenteredPromptString(l),DayPilot.Modal.alert(l,{theme:"modal_flat",top:290,scrollWithPage:AllowDialogsToScroll(),okText:"Cancel"}).then(function(e){SDBatchImageExportCancelRequested=!0});var i=document.getElementsByClassName("modal_flat_main");i[i.length-1].style.zIndex=100001;for(var s=document.getElementsByClassName("modal_flat_ok"),d=null,c=0;c<s.length;++c)if("Cancel"==(d=s[c]).innerText){SDTheBatchImageExportOKButton=d;break}var u=document.getElementsByClassName("modal_flat_content");(SDBatchImageExportStatusText=u[u.length-1]).style.textAlign="center";var p=getTuneByIndex(SDTuneOrder[o]),m=getTuneTitle(p);return SDBatchImageExportStatusText.innerText="Generating SmartDraw image for tune "+(o+1)+" of "+r+": "+m,SDExportImageDialog(p,a,o,null,!1),!0}}function SDExportImageDialog(e,t,a,r,n){SDTheOKButton=null,gPlayerABC=e;var o=GetRadioValue("notenodertab"),l=GetABCJSParams(o);l.oneSvgPerLine=!1,!function r(){modal_msg='<div id="playerholder" style="height:'+(window.innerHeight-340)+'px;overflow-y:auto;margin-bottom:15px;">',modal_msg+='<div id="abcplayer">',modal_msg+='<div id="playback-paper"></div>',modal_msg+="</div>",modal_msg+="</div>",modal_msg+="</p>";var n,o=window.innerWidth,i=GetRadioValue("notenodertab");isDesktopBrowser()?(n=.45*o)<850&&(n=850):n=800,DayPilot.Modal.alert(modal_msg,{theme:"modal_flat",top:40,width:n,okText:"Close",scrollWithPage:isMobileBrowser()});for(var s=document.getElementsByClassName("modal_flat_ok"),d=null,c=0;c<s.length;++c)if("Close"==(d=s[c]).innerText){SDTheOKButton=d;break}postProcessTab([ABCJS.renderAbc("playback-paper",e,l)[0]],"playback-paper",i,!0),t&&setTimeout(function(){t(a,SDTheOKButton)},10)}()}function SDDownloadJPG(e,t){PreProcessSVGImageForDownload();var a=document.querySelector("#playback-paper svg");if(!a){if(!e)return;e(t)}var r=document.createElement("canvas"),n=a.getBoundingClientRect();a=a.cloneNode(!0);var o=n.width,l=n.height,i=SDExportWidthAll,s=SDExportWidthAll*l/o;r.width=2*i,r.height=2*s,r.style.width=2*i,r.style.height=2*s,a.setAttribute("width",i+"px"),a.setAttribute("height",s+"px");var d=r.getContext("2d");d.fillStyle="#ffffff",d.fillRect(0,0,r.width,r.height),d.scale(2,2);var c=document.createElement("img"),u=new XMLSerializer().serializeToString(a);c.setAttribute("src","data:image/svg+xml;base64,"+btoa(unescape(encodeURIComponent(u)))),c.onload=function(){d.drawImage(c,0,0);var n=r.toDataURL("image/jpeg",.9);SDTheNotationImages.push({data:n,width:i,height:s}),PostProcessSVGImageAfterDownload(),a=null,e(t)}}function AddSmartDrawSetName(){DayPilot.Modal.prompt("Please enter a new tune set name:","Set #",{theme:"modal_flat",top:200,autoFocus:!0,scrollWithPage:AllowDialogsToScroll()}).then(function(e){var t,a,r=e.result;if(null!=r){if(SmartDrawTuneCurrent)a=SmartDrawTuneCurrent,t=SmartDrawTuneCurrent.cloneNode(!0),SmartDrawTuneCurrent&&SmartDrawTuneCurrent.classList.remove("draggable_tune_selected");else{let n=document.querySelectorAll("#sortable-tune-list .draggable_tune");t=(a=n[0]).cloneNode(!0)}t.innerHTML=r,t.setAttribute("data_tune_index","-1"),document.getElementById("sortable-tune-list").insertBefore(t,a),(SmartDrawTuneCurrent=t).classList.add("draggable_tune_selected")}})}function DeleteSmartDrawSetName(){SmartDrawTuneCurrent&&"-1"==SmartDrawTuneCurrent.getAttribute("data_tune_index")&&(document.getElementById("sortable-tune-list").removeChild(SmartDrawTuneCurrent),SmartDrawTuneCurrent=null)}function saveVSON(e,t){if(0==t.length){DayPilot.Modal.alert("Nothing to save!",{theme:"modal_flat",top:200});return}if(0==(e=e.replace(/[^a-zA-Z0-9_\-. ]+/ig,"")).length)return null;e=e.replace(/\..+$/,""),e+=".vson";var a=document.createElement("a");document.body.appendChild(a),a.style="display: none";var r=new Blob([t],{type:"text/plain"}),n=window.URL.createObjectURL(r);a.href=n,a.download=e,a.click(),document.body.removeChild(a),setTimeout(function(){window.URL.revokeObjectURL(n)},1e3)}function ExportSmartDrawSetList(){DayPilot.Modal.prompt("Please enter a file name for your exported SmartDraw file:","SmartDraw_Set_List",{theme:"modal_flat",top:200,autoFocus:!0,scrollWithPage:AllowDialogsToScroll()}).then(function(e){var t=e.result;null!=t&&DayPilot.Modal.prompt("Please name for your SmartDraw set list:","My Set List",{theme:"modal_flat",top:200,autoFocus:!0,scrollWithPage:AllowDialogsToScroll()}).then(function(e){var a=document.getElementById("smartdraw_export_width").value;SDExportWidthAll=isNaN(a=parseInt(a))?480:a,sendGoogleAnalytics("export","SmartDraw_"+(SDExportFormat=document.getElementById("smartdraw_format_select").value));var r=e.result;if(null!=r){SDTheNotationImages=[];let n=document.querySelectorAll("#sortable-tune-list .draggable_tune");var o=Array.from(n).map(e=>e.getAttribute("data_tune_index")),l=o.length;SDTuneOrder=[];for(var i=0;i<l;++i){var a=o[i];"-1"!=a&&SDTuneOrder.push(parseInt(a))}switch(SDTuneArray=SDGenerateTuneArray(gTheABC.value),SDExportFormat){case"0":case"3":SDDoBatchImageExport(s);break;case"1":case"4":SDDoFullTextIncipitsExport(s);break;case"2":case"5":SDDoTextIncipitsExport(s)}function s(e){if(e){var a=n.length,o=new VS.Document;o.AddTitle(r).SetTextSize(18);var l=o.GetTheShape();l.SetFillColor("#FFFFFF"),l.SetTextColor("#000000"),l.SetLabel(r),"0"!=SDExportFormat&&"3"!=SDExportFormat&&l.SetTextFont("Courier"),l.SetTextBold(!0);var s=l.AddShapeConnector("Orgchart");s.SetDirection(VS.Directions.Right);var d=s,c=0,u=!1;if("0"==SDExportFormat||"1"==SDExportFormat||"2"==SDExportFormat)for(i=0;i<a;++i){var p=n[i],m=p.innerHTML,S=!1;if("-1"==p.getAttribute("data_tune_index")&&(S=!0),S){var h=s.AddShape();h.SetFillColor("#FFFFFF"),h.SetTextColor("#000000"),h.SetLabel(m),"0"!=SDExportFormat&&"3"!=SDExportFormat&&h.SetTextFont("Courier"),h.SetTextBold(!0),u=!0}else{u&&((d=h.AddShapeConnector("Orgchart")).SetDirection(VS.Directions.Right),u=!1);var h=d.AddShape();h.SetFillColor("#FFFFFF"),h.SetTextColor("#000000");var g=SDTuneOrder[c];switch(SDExportFormat){case"0":h.SetImage(SDTheNotationImages[c].data),h.SetMinWidth(SDTheNotationImages[c].width),h.SetMinHeight(SDTheNotationImages[c].height),h.SetHyperlink(SDTuneArray[g].ShareURL);break;case"1":case"2":h.SetLabel(SDIncipits[c]),h.SetTextFont("Courier"),h.SetTextBold(!0),h.SetTextAlignH("Left"),h.SetTextGrow(VS.TextGrow.Horizontal),h.SetHyperlink(SDTuneArray[g].ShareURL)}c++}}else{var D=null;for(i=0;i<a;++i){var p=n[i],m=p.innerHTML,S=!1;if("-1"==p.getAttribute("data_tune_index")&&(S=!0),S){var h=s.AddShape();h.SetFillColor("#FFFFFF"),h.SetTextColor("#000000"),h.SetLabel(m),"0"!=SDExportFormat&&"3"!=SDExportFormat&&h.SetTextFont("Courier"),h.SetTextBold(!0),u=!0,D=null}else{u&&((d=h.AddShapeConnector("Orgchart")).SetDirection(VS.Directions.Right),u=!1),D&&(d=D.AddShapeConnector("Orgchart")).SetDirection(VS.Directions.Right);var h=d.AddShape();D=h,h.SetFillColor("#FFFFFF"),h.SetTextColor("#000000");var g=SDTuneOrder[c];switch(SDExportFormat){case"3":h.SetImage(SDTheNotationImages[c].data),h.SetMinWidth(SDTheNotationImages[c].width),h.SetMinHeight(SDTheNotationImages[c].height),h.SetHyperlink(SDTuneArray[g].ShareURL);break;case"4":case"5":h.SetLabel(SDIncipits[c]),h.SetTextFont("Courier"),h.SetTextBold(!0),h.SetTextAlignH("Left"),h.SetTextGrow(VS.TextGrow.Horizontal),h.SetHyperlink(SDTuneArray[g].ShareURL)}c++}}}saveVSON(t,o.toJSON())}}}})})}function SmartDrawExport(){var e,t=[];SDBatchImageExportStatusText="",SDTheBatchImageExportOKButton=null,SDBatchImageExportCancelRequested=!1,SDTheOKButton=null,SDTheNotationImages=[],SDTuneOrder=[],SDTuneArray=[],SmartDrawTuneCurrent=null,SmartDrawTitles=null,totalTunes=CountTunes();var a=(SmartDrawTitles=GetTunebookIndexTitles()).length;if(0==a){var r="No tunes to create SmartDraw set list.";r=makeCenteredPromptString(r),DayPilot.Modal.alert(r,{theme:"modal_flat",top:200,scrollWithPage:AllowDialogsToScroll()});return}var n='<div id="sortable-tune-list" style="overflow:auto;height:475px;margin-top:18px">';for(e=0;e<a;++e)n+='<div class="draggable_tune" draggable="true" data_tune_index="'+e+'">'+SmartDrawTitles[e]+"</div>";n+="</div>",modal_msg='<p style="text-align:center;font-size:18pt;font-family:helvetica;margin-left:15px;">SmartDraw Set List Builder <span style="font-size:24pt;" title="View documentation in new tab"><a href="https://michaeleskin.com/abctools/userguide.html#smartdraw_export" target="_blank" style="text-decoration:none;position:absolute;left:20px;top:20px">?</a></span></p>',modal_msg+='<p style="margin-top:18px;font-size:12pt;">Drag and drop the tune names to change the order of the tunes in the set list.</p>',modal_msg+='<p style="margin-top:18px;font-size:12pt;">Add or delete set name markers using the buttons below.</p>',modal_msg+=n,modal_msg+='<p style="text-align:center;margin-top:24px;"><input id="smartdraw_add_set_name" class="advancedcontrols btn btn-injectcontrols-headers" onclick="AddSmartDrawSetName();" type="button" value="Add Set Name" title="Adds a set name element to the list"><input id="smartdraw_delete_set_name" class="advancedcontrols btn btn-injectcontrols-headers" onclick="DeleteSmartDrawSetName();" type="button" value="Delete Set Name" title="Deletes a selected set name element from the list"><input id="smartdraw_export" class="advancedcontrols btn btn-smartdraw-export" onclick="ExportSmartDrawSetList();" type="button" value="Export SmartDraw Set List" title="Exports the set list as a SmartDraw diagram"></p>',modal_msg+='<p class="smartdraw_export_all_text">',modal_msg+='Tune export format: <select id="smartdraw_format_select" title="Select the SmartDraw export format and tune shape flow direction">',modal_msg+='<option value="0">Notation ↓</option>',modal_msg+='<option value="1">ABC Full Text ↓</option>',modal_msg+='<option value="2">ABC Incipits ↓</option>',modal_msg+='<option value="3">Notation →</option>',modal_msg+='<option value="4">ABC Full Text →</option>',modal_msg+='<option value="5">ABC Incipits →</option>',modal_msg+="</select>",modal_msg+=' Notation width to export: <input id="smartdraw_export_width" type="number" min="0" step="1" max="4096" title="Notation width to export" autocomplete="off"/>',modal_msg+="</p>",DayPilot.Modal.alert(modal_msg,{theme:"modal_flat",top:50,width:650,scrollWithPage:AllowDialogsToScroll(),autoFocus:!1}),document.getElementById("smartdraw_export_width").value=SDExportWidthAll,document.getElementById("smartdraw_format_select").value=SDExportFormat;let o=document.getElementById("sortable-tune-list"),l=null;o.addEventListener("click",function(e){var t=e.target;t.classList&&t.classList.contains("draggable_tune")&&(l=t,SmartDrawTuneCurrent&&SmartDrawTuneCurrent.classList.remove("draggable_tune_selected"),SmartDrawTuneCurrent=l,l.classList.add("draggable_tune_selected"))}),o.addEventListener("dragstart",function(e){l=e.target,e.dataTransfer.effectAllowed="move",e.dataTransfer.setData("text/plain",l.innerHTML),SmartDrawTuneCurrent&&SmartDrawTuneCurrent.classList.remove("draggable_tune_selected"),SmartDrawTuneCurrent=l,l.classList.add("draggable_tune_selected")}),o.addEventListener("dragover",function(e){e.preventDefault();let a=e.target;if(a&&a!==l&&a.classList.contains("draggable_tune")){let r=a.getBoundingClientRect(),n=(e.clientY-r.top)/(r.bottom-r.top)>.5;o.insertBefore(l,n?a.nextElementSibling:a);let i=document.querySelectorAll("#sortable-tune-list .draggable_tune");t=Array.from(i).map(e=>e.getAttribute("data_tune_index"))}}),o.addEventListener("dragend",function(){l=null})}