UNPKG

jlate

Version:

New Born jlate: javascript framework to build dynamic template

11 lines 2.77 kB
/*! * weblate JavaScript Library v0.3.10 * https://webphonix.com/ * * * Copyright webphonix * Released under the MIT license * Author: Gurudev Kumar * Date: 06-08-2021 */ !function(){Number.prototype.toCurrency=function(){return numberFormatting(this)},String.prototype.toCurrency=function(){return numberFormatting(this)};let brand_element=document.getElementsByClassName("wp-branding")[0];function mouseOver(){brand_element.setAttribute("title","Webphonix")}function mouseOut(){brand_element.removeAttribute("title")}brand_element&&(brand_element.addEventListener("mouseover",mouseOver),brand_element.addEventListener("mouseout",mouseOut))}();var weblate=$$=function(selector){const self={element:document.querySelector(selector),weblate:dataObj=>{load_weblate(selector,dataObj)},on:(event,dataObj,callback)=>{if("weblate"!=event)throw"weblateError: event listener name is not correct";load_weblate(selector,dataObj),callback(dataObj)},hide:()=>{self.element.style.display="none"},attr:(name,value)=>{null==value?self.element.getAttribute(name):self.element.setAttribute(name,value)}};return self};function getParameterByName(name,url=window.location.href){name=name.replace(/[\[\]]/g,"\\$&");var regex,results=new RegExp("[?&]"+name+"(=([^&#]*)|&|#|$)").exec(url);return results?results[2]?decodeURIComponent(results[2].replace(/\+/g," ")):"":null}function load_weblate(selector,dataObj){const template_tag=document.querySelectorAll(selector);_.each(template_tag,i=>{var weblate_type;switch(i.getAttribute("type")){case"template":load_dynamic_template(i,dataObj);break;case"select":load_dropdown(i,dataObj);break;default:console.warn("Weblate Type not defined."),load_dynamic_template(i,dataObj)}})}function load_dropdown(i,dataObj){attrs_array=[],0==dataObj.length&&console.warn("Weblate select warning: empty options data"),_.each(i.attributes,attr=>{"type"!=attr.name&&attrs_array.push(attr.name+" = '"+attr.value+"'")}),attrs=attrs_array.join(" "),options=[],dataObj.map((item,i)=>{options.push(`<option value="${item.value}">${item.text}</option>`)}),options=options.join(""),templateHTML=`<select ${attrs}>${options}</select>`,i.insertAdjacentHTML("afterend",templateHTML),i.remove()}function load_dynamic_template(i,dataObj){let filePath=i.getAttribute("src");fetch(filePath).then(file=>{file.text().then(content=>{var templateFn,templateHTML=_.template(content)(dataObj);i.insertAdjacentHTML("afterend",templateHTML),i.remove()})})}weblate.url={href:window.location.href,searchKey:function(searchKey){return getParameterByName(searchKey)}};let numberFormatting=function(number){const numberString=String(number).replace(/^\d+/,number=>[...number].map((digit,index,digits)=>(!index||(digits.length-index)%3?"":",")+digit).join(""));return numberString};