praiadeseselle/wire/modules/Page/PageFrontEdit/PageFrontEdit.min.js

1 line
6.7 KiB
JavaScript
Raw Normal View History

2022-03-08 15:55:41 +01:00
function PageFrontEditInit($){var buttons=$(".pw-edit-buttons");var ckeditors={};var isTouch="ontouchstart"in window||navigator.MaxTouchPoints>0||navigator.msMaxTouchPoints>0;var busy=false;function setBusy(value){busy=value;if(busy){$("body").addClass("pw-busy")}else{$("body").removeClass("pw-busy")}}function loadCSS(file){$("<link/>",{rel:"stylesheet",type:"text/css",href:file}).appendTo("head")}function ckeBlurEvent(event){var editor=event.editor;if(editor.checkDirty()){var el=$(editor.element.$);if(el.length){el.closest(".pw-edit").addClass("pw-changed")}}}function inlineEditEvent(e,t,orig,copy){if(t.hasClass("pw-editing")||busy)return;var copyID=copy.attr("id");var name=t.attr("data-name");t.addClass("pw-editing pw-edited");if(!copy.data("prev"))copy.data("prev",copy.html());orig.hide();copy.show();buttons.show();if(t.hasClass("pw-edit-InputfieldCKEditor")&&typeof CKEDITOR!="undefined"){if(typeof ckeditors[copyID]=="undefined"){var editor=CKEDITOR.inline(copyID,ProcessWire.config["InputfieldCKEditor_"+name]);ckeditors[copyID]=editor;editor.on("blur",function(e){t.removeClass("pw-editing");ckeBlurEvent(e)});editor.on("change",ckeBlurEvent)}}setTimeout(function(){copy.focus()},250)}function inlineInitEditableRegion(t){var orig=t.children(".pw-edit-orig");var copy=t.children(".pw-edit-copy");var name=t.attr("data-name");copy.hide();if(isTouch)orig.on("pwdoubletap",function(e){inlineEditEvent(e,t,orig,copy);return false});orig.dblclick(function(e){inlineEditEvent(e,t,orig,copy);return false});if(t.is("span")){copy.keydown(function(e){if(e.keyCode==13){e.preventDefault();$(this).blur()}})}if(orig.find("a").length){var clicks=0,timer=null,allowClick=false;orig.on("click","a",function(){var $a=jQuery(this);if(allowClick){allowClick=false;return true}clicks++;if(clicks===1){timer=setTimeout(function(){clicks=0;allowClick=true;$a[0].click();return true},700)}else{clearTimeout(timer);allowClick=false;clicks=0;orig.trigger("dblclick")}return false});orig.on("dblclick","a",function(){return false})}if(!t.hasClass("pw-edit-InputfieldCKEditor")){copy.blur(function(){var copy=$(this);var t=copy.closest(".pw-editing");if(t.length==0)return;if(copy.html()!=copy.data("prev")){t.addClass("pw-changed")}t.removeClass("pw-editing")})}}function inlineAbandonAllChanges(){$(".pw-edited").each(function(){var t=$(this);var copy=t.children(".pw-edit-copy");var orig=t.children(".pw-edit-orig");copy.hide().html(copy.data("prev"));orig.show();copy.data("prev",null);t.removeClass("pw-changed pw-edited pw-editing")});buttons.hide()}function inlineCancelClickEvent(){if($(".pw-changed").length>0){if(confirm(ProcessWire.config.PageFrontEdit.labels.cancelConfirm)){inlineAbandonAllChanges();buttons.hide()}else{}}else{inlineAbandonAllChanges()}return false}function inlineSaveClickEvent(){if(busy)return;setBusy(true);var pageID=parseInt($("#Inputfield_id").val());var langID=parseInt($("#pw-edit-lang").val());var btnSave=$(".pw-edit-save");var btnCancel=$(".pw-edit-cancel");var btnSaving=$(".pw-edit-saving");var btnSaved=$(".pw-edit-saved");var edited=$(".pw-changed");var postData={action:"PageFrontEditSave",id:pageID,language:langID,fields:{}};var postToken=$("input._post_token");var csrfName=postToken.attr("name");var csrfValue=postToken.val();postData[csrfName]=csrfValue;edited.each(function(){var t=$(this);var name=t.attr("data-name");var page=parseInt(t.attr("data-page"));var orig=t.children(".pw-edit-orig");var copy=t.children(".pw-edit-copy");var key=page+"__"+name;if(t.hasClass("pw-edit-InputfieldCKEditor")){var editor=ckeditors[copy.attr("id")];editor.getSelection().reset();editor.getSelection().removeAllRanges();postData.fields[key]=editor.getData()}else{var textarea=document.createElement("textarea");textarea.innerHTML=copy[0].innerHTML;postData.fields[key]=textarea.value}});btnSave.hide();btnCancel.hide();btnSaving.show();$.post(ProcessWire.config.PageFrontEdit.pageURL,postData,function(data){btnSaving.hide();if(data.status>0){edited.each(function(){var t=$(this);var name=t.attr("data-name");var page=t.attr("data-page");var orig=t.children(".p