jsharmony-cms-sdk-next
Version:
jsHarmony CMS SDK for Next.js
20 lines (15 loc) • 8.33 kB
JavaScript
/*!
Copyright 2024 apHarmony
This file is part of jsHarmony.
jsHarmony is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
jsHarmony is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this package. If not, see <http://www.gnu.org/licenses/>.
*/
!function(){function e(e){var I=this,n={};function t(e,t){if(t)for(var n in t)e[n]=t[n];return e}e=t({access_keys:[]},e),this.onError=function(e){console.error(e.message)},t(this,e),this.liveRenderActive=!1,this.liveRenderTriggers=[],this.timers={},this.isLoading=!1,this.loadQueue=[],this.onInit=function(){n=I.parseGET(),I.isInEditor()&&I.initEditor()},this.isInEditor=function(){return!!n.jshcms_token},this.initEditor=function(){if(n.jshcms_token)return n.jshcms_access_key?n.jshcms_url?document&&"CSS1Compat"===document.compatMode?(I.startLoading(I),void I.validateAccessKey(n.jshcms_access_key,n.jshcms_url,n._,function(e,t){if(!t)return alert("Invalid CMS Access Key");t=n.jshcms_url+"js/jsHarmonyCMS.js";I.loadScript(t,function(){I.stopLoading(I)})})):alert('CMS Editor requires Standards Document mode. Please add "<!DOCTYPE html>" to the top of the HTML'):alert("Missing CMS URL querystring parameter"):alert("Missing CMS Access Key querystring parameter")},this.validateAccessKey=function(e,d,l,t){if(e=e.toString(),d=d.toLowerCase(),!(l=parseInt(l||""))||l<(new Date).getTime()-6048e5||l>(new Date).getTime()+864e5)return t(null,!1);if(!(e.length<64)){for(var c=e.substr(32),g="",n=0;n<8;n++){var i=parseInt(e.substr(4*n,4),16),r=parseInt(c.substr(4*n,4),16);g+=I.pad((i^r).toString(16).toLowerCase(),"0",4)}Array.isArray(I.access_keys)||(I.access_keys=[I.access_keys]);var u=!1;I.eachParallel(I.access_keys,function(e,t,n){if("*"==(e=(e||"").toString()))return u=!0,n();if(!e||e.length<64)return n();for(var i=e.substr(32),r="",s=0;s<8;s++){var o=parseInt(e.substr(4*s,4),16),a=parseInt(i.substr(4*s,4),16);r+=I.pad((o^a).toString(16).toLowerCase(),"0",4)}if(g!==r)return n();window.crypto.subtle.digest("SHA-256",Uint8Array.from((r+"-"+d).split("").map(function(e){return e.charCodeAt(0)}))).then(function(e){if(Array.prototype.slice.call(new Uint8Array(e)).map(function(e){return("00"+e.toString(16)).slice(-2)}).join("")!==i)return n();window.crypto.subtle.digest("SHA-256",Uint8Array.from((r+"-"+d+"-"+(l||"").toString()).split("").map(function(e){return e.charCodeAt(0)}))).then(function(e){e=Array.prototype.slice.call(new Uint8Array(e)).map(function(e){return("00"+e.toString(16)).slice(-2)}).join("");return c===e&&(u=!0),n()},function(e){return n(e)})},function(e){return n(e)})},function(e){return e?t(e):t(null,u)})}},this.liveRenderRefreshAll=function(){if(I.liveRenderActive)for(var e=this.liveRenderTriggers.slice(0),t=0;t<e.length;t++)I.liveRenderRefresh(e[t])},this.liveRenderRefresh=function(e){if(e.selector){for(var t="function"==typeof e.selector?e.selector():document.querySelectorAll(e.selector),n=[],i=0;i<t.length;i++){for(var r=!1,s=0;s<e.lastNodes.length;s++)if(t[i]==e.lastNodes[s]){r=!0;break}r||n.push(t[i])}e.lastNodes=t;for(i=0;i<n.length;i++){var o=n[i];if(e.options.addClass){if(o.classList.contains("jshcms_rendered_"+e.id))continue;o.classList.add("jshcms_rendered_"+e.id)}e.action(o)}}},this.liveRender=function(e,t,n,i){t=t||function(){},i=i||function(){},"addClass"in(n=n||{})||(n.addClass=!0);var r,e={selector:e,action:t,lastNodes:[],onComplete:i,options:n,id:I.liveRenderTriggers.length+1};I.liveRenderTriggers.push(e),I.liveRenderRefresh(e),I.liveRenderActive||(I.liveRenderActive=!0,document.addEventListener("readystatechange",function(){I.liveRenderRefreshAll()}),r=null,document&&(r=new MutationObserver(function(e,t){I.liveRenderRefreshAll()})).observe(document,{childList:!0,subtree:!0}),setTimeout(function(){r&&r.disconnect(),I.liveRenderRefreshAll();for(var e=0;e<I.liveRenderTriggers.length;e++)I.liveRenderTriggers[e].onComplete();for(e=0;e<I.liveRenderTriggers.length;e++)for(var t="jshcms_rendered_"+I.liveRenderTriggers[e].id,n=document.querySelectorAll("."+t),i=0;i<n.length;i++)n[i].classList.remove(t);I.liveRenderTriggers=[],I.liveRenderActive=!1},0))},this.extend=t,this.eachParallel=function(e,t,r){for(var s=[],o=!1,n=0;n<e.length;n++)s.push(!1);for(n=0;n<e.length;n++)(()=>{var i=n;t(e[i],i,function(e){if(!o){if(e)return o=!0,r(e);for(var t=s[i]=!0,n=0;n<s.length;n++)s[n]||(t=!1);return t?r():void 0}})})()},this.pad=function(e,t,n){for(var i=e.toString();i.length<n;)i=t+i;return i},this.loadScript=function(e,t){var n=document.createElement("script");t&&(n.onload=t),n.src=e,n.classList&&n.classList.add("removeOnPublish"),document.head.appendChild(n)},this.parseGET=function(e){if(""==(e=void 0===e?window.location.search:e)||1==e.length)return{};for(var t=(e="?"!=e[0]&&"#"!=e[0]?e:e.substr(1)).split("&"),n={},i=0;i<t.length;i++){var r=t[i].split("=",2);1==r.length?n[r[0]]="":n[r[0]]=decodeURIComponent(r[1].replace(/\+/g," "))}return n},this.fade=function(e,t,n,i,r,s){e in I.timers&&window.clearTimeout(I.timers[e]),r?I.timers[e]=setTimeout(function(){I.fade(e,t,n,i,0,s)},r):(r=i/100,void 0===s&&(s=Math.round((1==n?t.style.opacity:1-t.style.opacity)*r)),t.style.opacity=1==n?s/r:1-s/r,r<=s?-1==n&&(t.style.display="none"):I.timers[e]=setTimeout(function(){I.fade(e,t,n,i,0,s+1)},i/r))},this.fadeIn=function(e,t,n,i,r){I.fade(e,t,1,n,i,r)},this.fadeOut=function(e,t,n,i,r){I.fade(e,t,-1,n,i,r)},this.startLoading=function(e,i){i=I.extend({fadeIn:!1},i);for(var r,t=!1,n=0;n<this.loadQueue.length;n++)e===this.loadQueue[n]&&(t=!0);t||this.loadQueue.push(e),this.isLoading||(this.isLoading=!0,(r=document.getElementById("jsHarmonyCmsEditorLoading"))?(r.style.opacity=i.fadeIn?0:1,r.style.display="block",i.fadeIn&&I.fadeIn("jsHarmonyCmsEditorLoading",r,1e3,500)):I.liveRender(function(){return document&&document.body?[document.body]:[]},function(e){(r=document.createElement("div")).id="jsHarmonyCmsEditorLoading",r.style.backgroundColor="rgba(255,255,255,1)",r.style.position="fixed",r.style.top="0px",r.style.left="0px",r.style.bottom="0px",r.style.width="100%",r.style.zIndex=2147483643,r.style.cursor="wait",r.style.opacity=i.fadeIn?0:1,document.body.appendChild(r);var t=document.createElement("div"),n=(t.style.position="absolute",t.style.top="50%",t.style.left="50%",r.appendChild(t),document.createElement("img"));n.src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzgiIGhlaWdodD0iMzgiIHZpZXdCb3g9IjAgMCAzOCAzOCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiBzdHJva2U9IiNhYWEiPg0KICAgIDxnIGZpbGw9Im5vbmUiIGZpbGwtcnVsZT0iZXZlbm9kZCI+DQogICAgICAgIDxnIHRyYW5zZm9ybT0idHJhbnNsYXRlKDEgMSkiIHN0cm9rZS13aWR0aD0iMiI+DQogICAgICAgICAgICA8Y2lyY2xlIHN0cm9rZS1vcGFjaXR5PSIuNSIgY3g9IjE4IiBjeT0iMTgiIHI9IjE4Ii8+DQogICAgICAgICAgICA8cGF0aCBkPSJNMzYgMThjMC05Ljk0LTguMDYtMTgtMTgtMTgiPg0KICAgICAgICAgICAgICAgIDxhbmltYXRlVHJhbnNmb3JtDQogICAgICAgICAgICAgICAgICAgIGF0dHJpYnV0ZU5hbWU9InRyYW5zZm9ybSINCiAgICAgICAgICAgICAgICAgICAgdHlwZT0icm90YXRlIg0KICAgICAgICAgICAgICAgICAgICBmcm9tPSIwIDE4IDE4Ig0KICAgICAgICAgICAgICAgICAgICB0bz0iMzYwIDE4IDE4Ig0KICAgICAgICAgICAgICAgICAgICBkdXI9IjFzIg0KICAgICAgICAgICAgICAgICAgICByZXBlYXRDb3VudD0iaW5kZWZpbml0ZSIvPg0KICAgICAgICAgICAgPC9wYXRoPg0KICAgICAgICA8L2c+DQogICAgPC9nPg0KPC9zdmc+",n.style.height="100px",n.style.width="100px",n.style.position="relative",n.style.top="-50px",n.style.left="-50px",t.appendChild(n),i.fadeIn&&I.fadeIn("jsHarmonyCmsEditorLoading",r,1e3,500)},{addClass:!1}))},this.stopLoading=function(e){for(var t=0;t<this.loadQueue.length;t++)e===this.loadQueue[t]&&(this.loadQueue.splice(t,1),t--);this.loadQueue.length||(this.isLoading=!1,I.fadeOut("jsHarmonyCmsEditorLoading",document.getElementById("jsHarmonyCmsEditorLoading"),500))},I.onInit()}var t="undefined"!=typeof self&&self||{},n="undefined"!=typeof global&&global||t,i="undefined"!=typeof module&&module;return i&&(i.exports=exports=e),"jsHarmonyCmsEditor"in n||(n.jsHarmonyCmsEditor=e),"jsHarmonyCmsEditor"in t||(t.jsHarmonyCmsEditor=e),e}.call(this);