1 line
No EOL
6.9 KiB
JavaScript
1 line
No EOL
6.9 KiB
JavaScript
function ckeGetProcessWireConfig(editor){var editorName=typeof editor=="string"?editor:editor.name;var configName=editorName.replace("Inputfield_","InputfieldCKEditor_");var $repeaterItem="";var settings={};configName=configName.replace("Inputfield_","InputfieldCKEditor_");if(typeof ProcessWire.config[configName]=="undefined"&&configName.indexOf("_repeater")>0){configName=configName.replace(/_repeater[0-9]+/,"");$repeaterItem=$("#"+editorName).closest(".InputfieldRepeaterItem")}if(typeof ProcessWire.config[configName]=="undefined"&&configName.indexOf("_ckeditor")>0){configName=configName.replace(/_ckeditor$/,"")}if(typeof ProcessWire.config[configName]=="undefined"&&configName.indexOf("__")>0){configName=configName.replace(/__\d+$/,"")}if(typeof ProcessWire.config[configName]=="undefined"){settings.error="Cannot find CKEditor settings for "+configName}else{settings=ProcessWire.config[configName]}if($repeaterItem.length){settings["repeaterItem"]=$repeaterItem}else{settings["repeaterItem"]=""}return settings}function ckeLoadPlugins(){for(var name in ProcessWire.config.InputfieldCKEditor.plugins){var file=ProcessWire.config.InputfieldCKEditor.plugins[name];CKEDITOR.plugins.addExternal(name,file,"")}}ckeLoadPlugins();function ckeBlurEvent(event){var editor=event.editor;var $textarea=$(editor.element.$);if(editor.checkDirty()){if($textarea.length){if($textarea.is("textarea"))$textarea.trigger("change");$textarea.closest(".Inputfield").addClass("InputfieldStateChanged")}}}function ckeFocusEvent(event){var editor=event.editor;var $textarea=$(editor.element.$);$textarea.trigger("pw-focus")}function ckeResizeEvent(event){var editor=event.editor;var $textarea=$(editor.element.$);if($textarea.length){$textarea.closest(".Inputfield").trigger("heightChanged")}}function ckeUploadEvent(event){var xhr=event.data.fileLoader.xhr;var fileLoader=event.data.fileLoader;var settings=ckeGetProcessWireConfig(event.editor);var uploadFieldName=settings?settings.pwUploadField:"_unknown";var $imageInputfield=$("#Inputfield_"+uploadFieldName);if(typeof settings.repeaterItem!="undefined"&&settings.repeaterItem.length){var $repeaterImageField=settings.repeaterItem.find(".InputfieldImage:not(.InputfieldFileSingle)");if($repeaterImageField.length)$imageInputfield=$repeaterImageField}if($imageInputfield.length){xhr.open("POST",fileLoader.uploadUrl,true);$imageInputfield.trigger("pwimageupload",{name:fileLoader.fileName,file:fileLoader.file,xhr:xhr});event.stop()}else{if(typeof settings.error!="undefined"&&settings.error.length){ProcessWire.alert(settings.error)}else{ProcessWire.alert("Unable to find images field for upload")}event.stop();return false}}function ckeInitEvents(editor){editor.on("blur",ckeBlurEvent);editor.on("focus",ckeFocusEvent);editor.on("change",ckeBlurEvent);editor.on("resize",ckeResizeEvent);editor.on("fileUploadRequest",ckeUploadEvent,null,null,4);var $textarea=$(editor.element.$);var $inputfield=$textarea.closest(".Inputfield.InputfieldColumnWidth");if($inputfield.length)setTimeout(function(){$inputfield.trigger("heightChanged")},1e3)}function ckeSaveReadyInline($inputfield){if(!$inputfield.length)return;var $inlines=$inputfield.hasClass(".InputfieldCKEditorInline")?$inputfield:$inputfield.find(".InputfieldCKEditorInline");if($inlines.length)$inlines.each(function(){var $t=$(this);var value;if($t.hasClass("InputfieldCKEditorLoaded")){var editor=CKEDITOR.instances[$t.attr("id")];if(typeof editor!="undefined"){if(editor.focusManager.hasFocus){editor.focusManager.focus(true);editor.focus()}value=editor.getData()}}else{value=$t.html()}var $input=$t.next("input");$input.attr("value",value)})}function ckeSaveReadyNormal($inputfield){var $normals=$inputfield.hasClass("InputfieldCKEditorNormal")?$inputfield:$inputfield.find(".InputfieldCKEditorNormal");$normals.each(function(){var $t=$(this);if(!$t.hasClass("InputfieldCKEditorLoaded"))return;var editor=CKEDITOR.instances[$t.attr("id")];editor.updateElement()})}function ckeGetConfigData($editor){var configName=$editor.attr("data-configName");if(typeof ProcessWire.config[configName]==="undefined"){if(typeof $editor.attr("data-configdata")!=="undefined"){ProcessWire.config[configName]=JSON.parse($editor.attr("data-configdata"))}}var configData=ProcessWire.config[configName];if(typeof configData==="undefined")configData={};return configData}function ckeInlineMouseoverEvent(event){var $t=$(this);if($t.hasClass("InputfieldCKEditorLoaded"))return;$t.effect("highlight",{},500);$t.attr("contenteditable","true");if(event.type=="focusin"){CKEDITOR.once("instanceReady",function(event){$(":focus").trigger("blur");event.editor.focus()})}var editor=CKEDITOR.inline($t.attr("id"),ckeGetConfigData($t));ckeInitEvents(editor);$t.addClass("InputfieldCKEditorLoaded")}function ckeInitTab(event,ui){var $t=ui.newTab;var $a=$t.find("a");if($a.hasClass("InputfieldCKEditor_init"))return;var editorID=$a.attr("data-editorID");var configName=$a.attr("data-configName");var editor=CKEDITOR.replace(editorID,config[configName]);ckeInitEvents(editor);$a.addClass("InputfieldCKEditor_init");ui.oldTab.find("a").addClass("InputfieldCKEditor_init");var $editor=$("#"+editorID);$editor.addClass("InputfieldCKEditorLoaded")}function ckeInitNormal(editorID){var $editor=$("#"+editorID);var $parent=$editor.parent();var configName;if(typeof ProcessWire.config.InputfieldCKEditor.editors[editorID]!="undefined"){configName=ProcessWire.config.InputfieldCKEditor.editors[editorID]}else{configName=$editor.attr("data-configName")}if($parent.hasClass("ui-tabs-panel")&&$parent.css("display")=="none"){var parentID=$editor.parent().attr("id");var $a=$parent.closest(".ui-tabs, .langTabs").find("a[href=#"+parentID+"]");$a.attr("data-editorID",editorID).attr("data-configName",configName);$parent.closest(".ui-tabs, .langTabs").on("tabsactivate",ckeInitTab)}else{var configData=ckeGetConfigData($editor);var editor=CKEDITOR.replace(editorID,configData);if(editor){ckeInitEvents(editor);$editor.addClass("InputfieldCKEditorLoaded")}}}$(document).ready(function(){CKEDITOR.timestamp=ProcessWire.config.InputfieldCKEditor.timestamp;for(var editorID in ProcessWire.config.InputfieldCKEditor.editors){ckeInitNormal(editorID)}$(document).on("reloaded",".InputfieldCKEditor",function(){var $editor=$(this).find(".InputfieldCKEditorNormal:not(.InputfieldCKEditorLoaded)");$editor.each(function(){ckeInitNormal($(this).attr("id"))});return false});$(document).on("image-edit sort-stop",".InputfieldCKEditor",function(){var $editor=$(this).find(".InputfieldCKEditorNormal");$editor.each(function(){var editorID=$(this).attr("id");if(typeof CKEDITOR.instances[editorID]!=="undefined")CKEDITOR.instances[editorID].destroy();ckeInitNormal(editorID)})});CKEDITOR.disableAutoInline=true;$(document).on("mouseover focus",".InputfieldCKEditorInlineEditor",ckeInlineMouseoverEvent);$(document).on("submit","form.InputfieldForm",function(){ckeSaveReadyInline($(this))});$(document).on("saveReady",".InputfieldCKEditor",function(){ckeSaveReadyNormal($(this));ckeSaveReadyInline($(this))})}); |