ব্যবহারকারী:মোহাম্মদ মারুফ/wikial.js

উইকিপিডিয়া, মুক্ত বিশ্বকোষ থেকে

লক্ষ্য করুন: প্রকাশ করার পর, পরিবর্তনগুলো দেখতে আপনাকে আপনার ব্রাউজারের ক্যাশে পরিষ্কার করার প্রয়োজন হতে পারে।

  • ফায়ারফক্স / সাফারি: পুনরায় লোড-এ ক্লিক করার সময় শিফট টিপে ধরে রাখুন, অথবা হয় Ctrl-F5 বা Ctrl-R টিপুন (ম্যাকে ⌘-R টিপুন)
  • গুগল ক্রোম: Ctrl-Shift-R (ম্যাকে ⌘-Shift-R) টিপুন
  • ইন্টারনেট এক্সপ্লোরার / এজ: Ctrl ধরে রাখা অবস্থায় Refresh-এ ক্লিক করুন, অথবা Ctrl-F5 টিপুন
  • অপেরা: Ctrl-F5 টিপুন।
/*
Title: wikial.js
Author: Md. Maruf Hasan (মোহাম্মদ মারুফ)
Description: wikial.js is a javascript framework for creating smart and beautifull alert.
Date: 15 May, 2023
version: 1.0.0
*/
function Wikial(t,e){window.wikialdata=window.wikialdata||{labelcount:0,labels:{}},Array.prototype.remove=function(t){var e=this.indexOf(t);-1!=e&&this.splice(e,1)};var n=e||{};if(window.wikialdata.labels[t])throw new Error("This Wikial label already exists");wikialdata.labelcount+=1,wikialdata.labels[wikialdata.labelcount]=t||"wikial-"+wikialdata.labelcount;var i={};i.id="wikial-"+t||wikialdata.labels[wikialdata.labelcount];var o,r,l,a=!1,d=0,c={label:t,displaying:!1,lines:[],lineCount:0,type:n.type||"default",done:!1};i.init=function(){return c.done&&(c.done=!1),(o=document.createElement("div")).id=i.id,o.className="wikial",o.style.display="block",c.displaying=!0,document.body.appendChild(o),o.innerHTML=`<div id="wikial-title">${c.label||"Wikial"}</div><div id="wikial-content"></div><div id="wikial-footer"></div>`,i},i.add=function(t){if(!t)throw new Error('No data provided in "add" method');c.displaying||i.init(),t.title&&(document.getElementById("wikial-title").innerHTML=t.title),c.label=t.title;var e=document.createElement("div");return c.lineCount++,e.className="wikial-line",e.id="wikial-line"+c.lineCount,(r=r||document.getElementById("wikial-content")).appendChild(e),e.innerHTML=t.content,styling(e,t),c.lines.push(e),i},i.change=function(t){if(!c.done){if(!t)throw new Error('No data provided in "change" method');if(c.displaying){var e=t.line&&t.line<=c.lineCount?t.line:c.lineCount,n=document.getElementById("wikial-line"+e);n.innerHTML=t.content,styling(n,t)}else i.add({title:t.title||c.label,content:t.content})}},i.remove=function(t){if(!c.done){var e=t.line&&t.line<=c.lineCount?t.line:c.lineCount,n=document.getElementById("wikial-line"+e);n.remove(),c.lines.remove(n)}},i.clear=function(){if(!c.done&&c.displaying)return(r=r||document.getElementById("wikial-content")).innerHTML="",(l=l||document.getElementById("wikial-footer")).innerHTML="",c.lines=[],c.lineCount=0,i},i.end=function(){if(!c.done)return(o=o||document.getElementById(i.label)).style.display="none",document.body.removeChild(o),c.displaying=!1,c.done=!0,i},i.timeout=function(t){if(!c.done){if(d=t,!t)throw new Error('No time provided in "timeout" method');if(a=!0,"string"==typeof t)if(t.includes("s"))t=1e3*parseInt(t.replace("s",""));else if(t.includes("m"))t=1e3*parseInt(t.replace("m",""))*60;else{if(!t.includes("h"))throw new Error('Invalid time string provided in "timeout" method');t=1e3*parseInt(t.replace("h",""))*60*60}else if("number"!=typeof t)throw new Error('Invalid time string provided in "timeout" method');return t&&setTimeout((function(){d>t?(d-=t,i.timeout(d)):i.end()}),t),i}},i.break=function(){if(!c.done){if(!a)throw new Error("This Wikial instance is not set to autohide");i.end()}},i.continue=function(t){if(!c.done){if(t="number"==typeof t?1e3*t:0,!a)throw new Error("This Wikial instance is not set to autohide");return d+=t,i}},i.hideAfter=function(t){if(!c.done){if("function"!=typeof t)throw new Error("Invalid callback provided in afterHide method");return t((()=>{new Promise((t=>{t()})).then((()=>{i.clear(),i.end(),c.done=!0}))})),i}},i.time=function(t){if(!c.done){if("function"!=typeof t)throw new Error("Invalid callback provided in time method");var e=performance.now();return t(),performance.now()-e}},i.addButton=function(t){if(!c.done){if(!t)throw new Error('No data provided in "button" method');c.displaying||i.init();var e=document.createElement("button");return e.className="wikial-button",e.id="wikial-button"+c.lineCount,e.innerHTML=t.content,e.onclick=t.onclick,(l=l||document.getElementById("wikial-footer")).appendChild(e),i}},i.isPresnt=function(t){return!c.done&&(t<=c.lineCount&&t>0&&-1!=c.lines.indexOf(t))},i.setTitle=function(t){if(!c.done){if(!t)throw new Error('No title provided in "setTitle" method');return(t=document.getElementById("wikial-title")).innerHTML=t,i}},this.__proto__=i,this.alert=function(t){if(c.type="alert",c.done||i.clear(),!t)throw new Error('No data provided in "alert" method');if("string"==typeof t)t={content:t};else if("object"!=typeof t)throw new Error("Invalid data provided");i.add({title:t.title||c.label,content:t.content,type:t.type||"warning"}),null!=t.autoHide?i.timeout(t.autoHide):i.hideAfter((e=>{i.addButton({content:t.button||"OK",onclick:e})})),o.className="wikial wikial-alert"},this.log=function(...t){c.type="log";for(var e=0;e<t.length;e++){var n=t[e];switch(typeof n){case"object":n instanceof Array?i.add({content:arrayToString(n)}):i.add({content:objectToString(n)});break;case"function":i.add({content:n()});break;default:i.add({content:n})}}o.className="wikial wikial-log"},this.error=function(t){if(c.type="error",!t.content)throw new Error("No content provided in error method");i.add({title:t.title||c.label,content:t.content}),null!=t.autoHide?i.timeout(t.autoHide):i.hideAfter((e=>{null==t.button?t.button=["I understood","copy to clipboard"]:"string"==typeof t.button?t.button=[t.button,"copy to clipboard"]:1==t.button.length&&t.button instanceof Array&&t.button.push("copy to clipboard"),i.addButton({content:t.button[0],onclick:e}),i.addButton({content:t.button[1],onclick:function(){var t=document.getElementById("wikial-content").innerText;navigator.clipboard.writeText(t.replace(/<br>/g,"")),e()}})})),o.className="wikial wikial-error"},this.case=function(t){if(c.type="case",t.from||(t.from=0),!t.to)throw new Error("No ending provided in case method");if(!t.content)throw new Error("No content provided in case method");var e=t.content;return i.add({title:t.title||c.label,content:e.replace(/\$to/i,t.to).replace(/\$from/i,t.from),type:t.type||"info"}),this.update=function(n){n&&n.from&&(t.from=n.from),t.from++,t.from<t.to?i.change({content:e.replace(/\$to/i,t.to).replace(/\$from/i,t.from),type:n&&n.type||t.type||"info"}):t.from>=t.to&&(i.change({content:e.replace(/\$to/i,t.to).replace(/\$from/i,t.from),type:n&&n.type||t.type||"info"}),i.hideAfter((t=>{i.addButton({content:"Ok",onclick:t})})))},o.className="wikial wikial-case",this}}function arrayToString(t){for(var e="[",n=0;n<t.length;n++)t[n]instanceof Array?e+=arrayToString(t[n])+",<br>":"object"==typeof t[n]?e+=objectToString(t[n])+",":"function"==typeof t[n]?e+=t[n]()+",<br>":e+=t[n]+" ";return e+"]"}function objectToString(t){var e="{<br>";for(var n in t)"object"==typeof t[n]?e+=objectToString(t[n]):t[n]instanceof Array?e+=arrayToString(t[n]):"function"==typeof t[n]?e+=n+": "+t[n]()+",<br>":e+=n+": "+t[n]+" ,<br>";return e+"}"}function styling(t,e){"error"==e.type?(t.style.color="red",t.style.fontWeight="bold",t.style.fontStyle="normal",t.style.textShadow="0px 0px 1px #800"):"success"==e.type?(t.style.color="#49af41",t.style.fontWeight="bold",t.style.fontStyle="normal",t.style.textShadow="0px 0px 1px #222"):"warning"==e.type?(t.style.color="orange",t.style.fontStyle="italic",t.style.textShadow="none",t.style.fontWeight="300"):"info"==e.type?(t.style.color="blue",t.style.fontStyle="italic",t.style.textShadow="none",t.style.fontWeight="400"):(t.style.color=e.color||"black",t.style.backgroundColor=e.bgcolor||"none",t.style.fontWeight=e.bold?"bold":"normal",t.style.fontStyle=e.italic?"italic":"normal",t.style.textShadow=e.shadow?"0px 0px 3px #000":"none")}window.mr?window.mr.Wikial=Wikial:window.mr={Wikial:Wikial};