(function($){$.widget("lws.lws_css_editor",{_create:function(){this.cssValues=lwsBase64.toObj(this.element.data('cssvalues'));this.defaultValues=lwsBase64.toObj(this.element.data('dftvalues'));this.tagRules=[];this.menuTable={};this.cssObject={};this.miscValues={};this.cssChain="";this._createCssObject(this.cssValues);this._createMaster();this._addBackgroundSwitcher();this._setCssEditor();var centralDiv=this.element.find(".lwss_centraldiv");this.popupShadow=$("<div>").addClass("lwss-popup-shadow").appendTo(centralDiv);this.element.on("mouseover",".lwss_menu_selector",this._bind(this._overSelector,this));this.element.on("mouseover",".lwss_selector",this._bind(this._overSelector,this));this.element.on("mouseout",".lwss_menu_selector",this._bind(this._outSelector,this));this.element.on("mouseout",".lwss_selector",this._bind(this._outSelector,this));this.element.on("click",".lwss_menu_selector",this._bind(this._clickSelector,this));this.element.on("click",".lwss_selector",this._bind(this._clickSelector,this));this.element.on("click",".lwss-main-conteneur",this._bind(this._clickEditor,this));this.element.on("click",".lwss-sidemenu",this._bind(this._clickEditor,this));this.element.on("click",".lwss-btvisual",this._bind(this._switchToVisual,this));this.element.on("click",".lwss-btcss",this._bind(this._switchToCss,this));this.element.on("click",".lwss-reset",this._bind(this._resetCss,this));this.myStylingPanel=$("<div>").addClass("lwss_styling_panel").prependTo(centralDiv);this.myStylingPanel.lws_styling_panel({stygen:this});this._createCssResetList();this.makeCssChain()},_bind:function(fn,me){return function(){return fn.apply(me,arguments)}},_createMaster:function(){var me=this;this.masterObject={};var myMenu=this.element.find(".lwss_sidemenu");var elements=this.element.find(".lwss-visual-conteneur");elements=elements.find("[data-selector], .lwss_selectable");elements.each(function(){var mySelectable=$(this);var myClass='';if(mySelectable.data('selector')!=undefined){mySelectable.css('position','relative');myClass=mySelectable.data('selector').trim()}
if(0==myClass.length)
myClass="."+mySelectable.attr("class").trim().replace(/\s+/g,".");var myStylableObject={Selectable:mySelectable,Selector:me._createSelector(mySelectable,myClass),Class:myClass,Over:"no",Active:"no"};if(me.masterObject[myClass]==undefined){me.masterObject[myClass]={};me.masterObject[myClass].elements=[];me.masterObject[myClass].cssValues=(me.cssObject[myClass]!=undefined)?me.cssObject[myClass]:{'idle':{}};if(me.cssObject[myClass]==undefined){me.cssObject[myClass]=me.masterObject[myClass].cssValues}
var menuItemHtml='';if((-1!=myClass.search("lwss_modify"))||('text'==mySelectable.data('editable'))){menuItemHtml+="<div class='lwss_menu_selector_icon lws-icon lws-icon-edit'></div>"}
menuItemHtml+="<div class='lwss_menu_selector_text'>"+mySelectable.data('type')+"</div>";me.masterObject[myClass].menuItem=$("<div>").addClass("lwss_menu_selector").data('theClass',myClass).html(menuItemHtml).appendTo(myMenu)}
me.masterObject[myClass].elements.push(myStylableObject)})},_createCssResetList:function(){var me=this;this.cssResetList={};this.myStylingPanel.find('.lwss_value').each(function(){me.cssResetList[$(this).data("css")]=''})},_setCssEditor:function(){this.cssConteneur=this.element.find(".lwss-css-conteneur");this.cssConteneur.find(".lwss-css-editor").append($('<textarea>',{'class':'lwss-css-textarea','id':'lwss-cssta-'+this.uuid}))},_switchToVisual:function(event,data){var visButton=this.element.find('.lwss-btvisual');if(visButton.hasClass('lws-active')){return}else{this.myStylingPanel.lws_styling_panel("resetActiveValues");this.element.find('.lwss-btcss').removeClass('lws-active');visButton.addClass('lws-active');this.element.find('.lwss-visual-conteneur').css('display','flex');this.element.find('.lwss-css-conteneur').css('display','none');this.cssEditor.codemirror.off()}},_switchToCss:function(event,data){var Button=this.element.find('.lwss-btcss');if(Button.hasClass('lws-active')){return}else{this.element.find('.lwss-btvisual').removeClass('lws-active');Button.addClass('lws-active');this.element.find('.lwss-css-conteneur').css('display','flex');this.element.find('.lwss-visual-conteneur').css('display','none');if(!this.cssEditor){this.cssEditor=wp.codeEditor.initialize(this.cssConteneur.find('#lwss-cssta-'+this.uuid),cm_settings)}
this.makeCssChain()
this.cssEditor.codemirror.setValue(this.cssChain);var me=this;var typingTimer;this.cssEditor.codemirror.on('keyup',function(){clearTimeout(typingTimer);typingTimer=setTimeout(me._bind(me._editorCssChanged,me),"1000")});this.cssEditor.codemirror.on('keydown',function(){clearTimeout(typingTimer)})}},_overSelector:function(event,data){var theClass=$(event.currentTarget).data("theClass")
var current=this.masterObject[theClass];current.menuItem.addClass('lwss_over_msel');for(let i=0;i<current.elements.length;i++){current.elements[i].Selector.addClass('lwss_over_sel')}},_outSelector:function(event,data){var theClass=$(event.currentTarget).data("theClass")
var current=this.masterObject[theClass];current.menuItem.removeClass('lwss_over_msel');for(let i=0;i<current.elements.length;i++){current.elements[i].Selector.removeClass('lwss_over_sel')}},_createSelector:function(aSelectable,theClass){if(aSelectable.prop('nodeName')=="INPUT"||(aSelectable.prop('nodeName')=="TEXTAREA")){if(aSelectable.attr('type')=="password"){aSelectable.prop('disabled',!0)}
var parent=aSelectable.parent();var wrapper=$("<div>",{"class":"lwss_wrapper"}).appendTo(parent);aSelectable.detach().appendTo(wrapper)
theSelector=$("<div>",{"class":"lwss_selector lwss_inp_sel",}).data('theClass',theClass).appendTo(wrapper)}else{theSelector=$("<div>",{"class":"lwss_selector",}).data('theClass',theClass).prependTo(aSelectable)}
return $(theSelector)},_addBackgroundSwitcher:function(){var main=this.element.find(".lwss-main-conteneur");var bgSwitcher=$("<div>").addClass("lws-bg-switcher").appendTo(main);var whiteSwitcher=$("<div>").addClass("lws-white-switcher").appendTo(bgSwitcher);whiteSwitcher.on('click',function(){main.css("background","#ffffff")})
var blackSwitcher=$("<div>").addClass("lws-black-switcher").appendTo(bgSwitcher);blackSwitcher.on('click',function(){main.css("background","#000000")})},placeSelectors:function(){this.element.find('.lwss_inp_sel').each(function(){var theSelector=$(this);var theSelectable=theSelector.parent().find('.lwss_selectable');theSelector.outerWidth(theSelectable.outerWidth(!0)+4).outerHeight(theSelectable.outerHeight(!0)+4);theSelector.css('top',theSelectable.position().top-2).css('left',theSelectable.position().left-2)})},_clickSelector:function(event){this.element.find('.lwss_selector').removeClass('lwss_sel_sel');this.element.find('.lwss_menu_selector').removeClass('lwss_sel_msel');var theClass=$(event.currentTarget).data("theClass")
var current=this.masterObject[theClass];current.menuItem.addClass('lwss_sel_msel');for(let i=0;i<current.elements.length;i++){current.elements[i].Selector.addClass('lwss_sel_sel')}
var currentItem=this.masterObject[theClass].elements[0].Selectable;var inputId=(currentItem.data('id')!='')?currentItem.data('id'):'';this.element.find(".lwss_info").hide();this.myStylingPanel.lws_styling_panel("openStylingPanel",theClass,inputId,currentItem);this.myStylingPanel.show();event.preventDefault();return!1},_clickEditor:function(event){var element=($(event.target).hasClass('lwss_menu_selector_text')||$(event.target).hasClass('lwss_menu_selector_text'))?$(event.target.parentElement):$(event.target);if(!element.hasClass('lwss_selector')&&!element.hasClass('lwss_menu_selector')){this.element.find('.lwss_selector').removeClass('lwss_sel_sel');this.element.find('.lwss_menu_selector').removeClass('lwss_sel_msel');this.myStylingPanel.lws_styling_panel("setEvent",'idle');this.element.find(".lwss_styling_panel").hide();this.element.find(".lwss_info").show()}},_updateMaster:function(){var me=this;$.each(this.cssObject,function(className,element){$.each(element,function(statekey,statevalues){if(me.masterObject[className]!=undefined){me.masterObject[className].cssValues[statekey]=statevalues}})})},_cleanStyles:function(){$.each(this.cssObject,function(className,element){try{lwssCanvas.find(className).removeAttr('style')}catch(e){}})},_resetCss:function(){this._cleanStyles();this.cssObject={};this.tagRules=[];this._createCssObject(this.defaultValues);this._updateMaster();this._applyCssObject();this.makeCssChain();if(this.cssEditor!=undefined)this.cssEditor.codemirror.setValue(this.cssChain);},_parseCss:function(text){const startPattern=/(\S[^{]*)\{/s;const endPattern=/((?:[^}'"]|(?:"(?:[^"\\\\]|\\\\.)*")|(?:'(?:[^'\\\\]|\\\\.)*'))*)(?:}|$)/s;let cursor={text:text,sections:[]};let selector=cursor.text.match(startPattern);while(selector!==null&&cursor.text.length>0){cursor.text=cursor.text.substring(selector[0].length).trimStart();let element={Selector:selector[1],isAtRule:selector[0].startsWith('@'),Help:'',ID:'',Important:[],Ref:'',Title:'',Type:'',Defaults:{},Values:[],subSections:[]}
if(element.isAtRule){let result=this._parseCss(cursor.text);element.subSections=result.sections;cursor.text=result.text;selector=cursor.text.match(startPattern)}
let content=cursor.text.match(endPattern);if(content!==null){cursor.text=cursor.text.substring(content[0].length).trimStart();selector=cursor.text.match(startPattern)}else{content=['',cursor.text];selector=null;cursor.text=''}
let props=content[1].trim().split(';');for(let line of props){let sep=line.indexOf(':');if(sep!==-1){let prop=line.substring(0,sep).trim();element.Defaults[prop]=line.substring(sep+1).trim()}}
cursor.sections.push(element);if(cursor.text.startsWith('}'))break}
return cursor},_editorCssChanged:function(){var nbErrors=this.cssConteneur.find('.CodeMirror-lint-marker-error').length;if(nbErrors===0&&this.cssEditor!=undefined){this._cleanStyleObjects();var cssContent=this.cssEditor.codemirror.getValue().replace(/\r\n|\r|\n|\t/g,'');this._createCssObject(this._parseCss(cssContent.trim()).sections);this._updateMaster();this._applyCssObject();this.makeCssChain()}},_applyCssObject:function(){$.each(this.cssObject,function(className,element){try{$.each(element,function(stateKey,stateValues){lwssCanvas.find(className).css(stateValues)})}catch(e){console.log('Update: Invalid css near ',element)}})},_cleanStyleObjects:function(){this._cleanStyles();$.each(this.masterObject,function(className,element){element.cssValues={}})
this.cssObject={};this.tagRules=[]},_createCssObject:function(sourceValues){var me=this;lwssCanvas=this.element;$.each(sourceValues,function(index,element){try{if(element.isAtRule){me.tagRules.push(element)}else{element.Selector=element.Selector.replaceAll('::',':');var mainClass=(element.Selector.split(":"))[0];var event=((element.Selector.split(",")[0].split(":"))[1]!=undefined)?(element.Selector.split(",")[0].split(":"))[1]:"idle";var myClass=(me.cssObject[mainClass]!=undefined)?me.cssObject[mainClass]:{};myClass[event]=element.Defaults;if(element.Values!=null&&element.Values!=undefined&&!Array.isArray(element.Values)){myClass[event]=element.Values}
me.cssObject[mainClass]=myClass;lwssCanvas.find(element.Selector).css(element.Defaults);lwssCanvas.find(element.Selector).css(element.Values)}}catch(e){console.log(e);console.log('Setup: Invalid css near ',element)}})},compileTagRules:function(tagRules,self,indent){let chain='';if(undefined===self)self=this;if(undefined===indent)indent='';$.each(tagRules,function(className,element){if(element.isAtRule){chain+=indent+element.Selector+"{\n";chain+=self.compileTagRules(element.subSections,self,indent+"\t");chain+=indent+"}\n"}else{chain+=indent+element.Selector+"{";$.each(element.Defaults,function(prop,values){chain+=prop+': '+values+';'});chain+='}'}});return chain},compileCss:function(cssObject){let chain='';const pseudoElements=['after','backdrop','before','cue','first-letter','first-line','grammar-error','marker','part','placeholder','selection','slotted','spelling-error'];$.each(cssObject,function(className,element){$.each(element,function(stateKey,stateValues){if(stateValues!==''){if(stateKey==="idle"){chain+=className+"{"}else if(stateKey==="hover"){chain+=className+":hover, "+className+".hovered{"}else if(stateKey==="focus"){chain+=className+":focus, "+className+".focused{"}else{chain+=className+":";if(pseudoElements.includes(stateKey))
chain+=":";chain+=stateKey+"{"}
$.each(stateValues,function(cssKey,cssValue){if(cssValue!==undefined&&cssValue!=='')chain+=cssKey+":"+cssValue+";"});chain+="}\n"}})});return chain},makeCssChain:function(){this.cssChain=this.compileCss(this.cssObject)+this.compileTagRules(this.tagRules);this.element.find(".lwss_editor_chain").val(lwsBase64.encode(this.cssChain))},})})(jQuery)
jQuery(function($){$(".lwss_editor").lws_css_editor();$(".lwss_editor").on('click',function(e){if($(e.target).closest(".lwss-color-selector-relative").length==0&&$(e.target).closest(".lwss-fontselect-popup").length==0)
$(".lwss-popup-shadow").hide();})})