/* Dynamically fetched templates/2k11/admin/serendipity_editor.js.tpl on 2024-03-29 08:33, called from: include/plugin_api.inc.php:external_plugin */
/*
# Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)
# All rights reserved. See LICENSE file for licensing details
*/
(function(serendipity, $, undefined ) {
// Fires functions which are generated dynamically in backend PHP files
// (i.e. include/functions_entries_admin.inc.php) which load the various
// WYSIWYG editors in entries editor, HTML nuggets etc.
serendipity.spawn = function() {
if (self.Spawnextended) {
Spawnextended();
}
if (self.Spawnbody) {
Spawnbody();
}
if (self.Spawnnugget) {
Spawnnugget();
}
}
// Generic function to set cookies
serendipity.SetCookie = function(name, value) {
var today = new Date();
var expire = new Date();
expire.setTime(today.getTime() + (60*60*24*30*1000));
// get array like or simple string argument items
if (name.indexOf("[") != -1) {
document.cookie = 'serendipity' + name + '=' + escape(value) + ';expires=' + expire.toGMTString();
} else {
document.cookie = 'serendipity[' + name + ']=' + escape(value) + ';expires=' + expire.toGMTString();
}
}
serendipity.GetCookie = function(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
/**
* Based upon code written by chris wetherell
* http://www.massless.org
* chris [THE AT SIGN] massless.org
*/
// Returns "position" of selection in textarea
// Used internally by wrapSelectionWithLink()
serendipity.getSelection = function($txtarea) {
var start = $txtarea[0].selectionStart;
var end = $txtarea[0].selectionEnd;
return $txtarea.val().substring(start, end);
}
// Used by non-wysiwyg editor toolbar buttons to wrap selection
// in a element associated with toolbar button
serendipity.wrapSelection = function(txtarea, openTag, closeTag) {
scrollPos = false;
if (txtarea.scrollTop) {
scrollPos = txtarea.scrollTop;
}
// http://stackoverflow.com/questions/1712417/jquery-wrap-selected-text-in-a-textarea
var $txtarea = $(txtarea);
if (!$txtarea.length) {
return;
}
var len = $txtarea.val().length;
var start = $txtarea[0].selectionStart;
var end = $txtarea[0].selectionEnd;
var selectedText = $txtarea.val().substring(start, end);
var replacement = openTag + selectedText + closeTag;
$txtarea.val($txtarea.val().substring(0, start) + replacement + $txtarea.val().substring(end, len));
$txtarea[0].selectionStart = start + replacement.length;
$txtarea[0].selectionEnd = start + replacement.length;
if (scrollPos) {
txtarea.focus();
txtarea.scrollTop = scrollPos;
}
}
// Used by non-wysiwyg editor toolbar buttons to wrap selection
// in element (only)
serendipity.wrapSelectionWithLink = function(txtarea) {
var my_link = prompt("Enter URL:","http://");
if (my_link) {
if (serendipity.getSelection($(txtarea) ) == "") {
var my_desc = prompt("Enter Description", '');
}
var my_title = prompt("Enter title/tooltip:", "");
}
html_title = "";
if (my_title != "" && my_title != null) {
html_title = ' title="' + my_title + '"';
}
if (my_link != null) {
lft = "";
if (my_desc != null && my_desc != "") {
rgt = my_desc + "";
} else {
rgt = "";
}
serendipity.wrapSelection(txtarea, lft, rgt);
}
return;
}
/* end chris w. script */
// Adds img element to selected text
// Used internally by wrapInsImage()
serendipity.insertText = function(txtarea, str) {
$txtarea = $(txtarea);
var selLength = $txtarea.val().length;
var selStart = $txtarea[0].selectionStart;
var selEnd = $txtarea[0].selectionEnd;
if (selEnd==1 || selEnd==2) {
selEnd=selLength;
}
var before = $txtarea.val().substring(0,selStart);
var after = $txtarea.val().substring(selStart);
$txtarea.val(before + str + after);
$txtarea[0].selectionStart = selStart + str.length
$txtarea[0].selectionEnd = selStart + str.length
}
// Used by non-wysiwyg editor toolbar buttons to wrap selection
// in element (only); does not really "wrap", merely inserts
// an element before selected text
serendipity.wrapInsImage = function(txtarea) {
var loc = prompt('Enter the image location: ');
if (loc) {
var alttxt = prompt('Enter alternative text for this image: ');
serendipity.insertText(txtarea,'');
}
}
/* end Better-Editor functions */
// Switches preview of image selected from media db
serendipity.change_preview = function(input, output) {
var filename = document.getElementById(input).value;
var $target = $('#' + output + '_preview > img');
$target.attr('src', filename);
var $container = document.getElementById( output + '_preview');
$container.style.display = "block";
}
// Opens media db image selection in new window
serendipity.choose_media = function(id) {
serendipity.openPopup('serendipity_admin.php?serendipity[adminModule]=media&serendipity[noBanner]=true&serendipity[noSidebar]=true&serendipity[noFooter]=true&serendipity[showMediaToolbar]=false&serendipity[multiselect]=false&serendipity[showUpload]=true&serendipity[htmltarget]=' + id + '&serendipity[filename_only]=true');
}
// "Transfer" value from media db popup to form element, used for example for selecting a category-icon
serendipity.serendipity_imageSelector_addToElement = function(str, id) {
id = serendipity.escapeBrackets(id);
var $input = $('#' + id);
$input.val(str);
if ($input.attr('type') != 'hidden') {
$input.focus(); // IE would generate an error when focusing an hidden element
}
// calling the change-event for doing stuff like generating the preview-image
$input.change();
}
// Escape [ and ] to be able to use the string as selector
// jQuery fails to select the input when the selector contains unescaped [ or ]
serendipity.escapeBrackets = function(str) {
str = str.replace(/\[/g, "\\[");
str = str.replace(/\]/g, "\\]");
return str;
}
// Add another (image) keyword
serendipity.AddKeyword = function(keyword) {
s = document.getElementById('keyword_input').value;
document.getElementById('keyword_input').value = (s != '' ? s + ';' : '') + keyword;
}
// "Transfer" value from media db popup to textarea, including wysiwyg
// This gets textarea="body"/"extended" and tries to insert into the textarea
// named serendipity[body]/serendipity[extended]
serendipity.serendipity_imageSelector_addToBody = function(str, textarea) {
var oEditor;
if (typeof(FCKeditorAPI) != 'undefined') {
oEditor = FCKeditorAPI.GetInstance('serendipity[' + textarea + ']') ;
if (oEditor.EditMode == FCK_EDITMODE_WYSIWYG) {
oEditor.InsertHtml(str);
return;
}
} else if(typeof(xinha_editors) != 'undefined') {
if (typeof(xinha_editors['serendipity[' + textarea + ']']) != 'undefined') {
oEditor = xinha_editors['serendipity['+ textarea +']'];
}
if (oEditor) {
oEditor.insertHTML(str);
return;
}
} else if(typeof(HTMLArea) != 'undefined') {
if (textarea == 'body' && typeof(editorbody) != 'undefined') {
oEditor = editorbody;
} else if (textarea == 'extended' && typeof(editorextended) != 'undefined') {
oEditor = editorextended;
} else if (typeof(htmlarea_editors) != 'undefined' && typeof(htmlarea_editors[textarea]) != 'undefined') {
oEditor = htmlarea_editors[textarea];
}
if (oEditor._editMode != 'textmode') {
oEditor.insertHTML(str);
return;
}
} else if(typeof(TinyMCE) != 'undefined') {
// for the TinyMCE editor we do not have a text mode insert
tinyMCE.execInstanceCommand('serendipity[' + textarea + ']', 'mceInsertContent', false, str);
return;
} else if (typeof(CKEDITOR) != 'undefined') {
oEditor = (typeof(isinstance) == 'undefined') ? CKEDITOR.instances[textarea] : isinstance;
if (typeof(oEditor) == 'undefined') oEditor = popupEditorInstance;
if (oEditor.mode == "wysiwyg") {
oEditor.insertHtml(str);
return;
}
}
serendipity.noWysiwygAdd(str, textarea);
}
// The noWysiwygAdd JS function is the vanila serendipity_imageSelector_addToBody js function
// which works fine in NO WYSIWYG mode
// NOTE: the serendipity_imageSelector_addToBody could add any valid HTML string to the textarea
serendipity.noWysiwygAdd = function(str, textarea) {
escapedElement = serendipity.escapeBrackets(textarea);
if ($('#' + escapedElement).length) {
// Proper ID was specified (hopefully by plugins)
} else {
// Let us try the serendipity[] prefix
escapedElement = serendipity.escapeBrackets('serendipity[' + textarea + ']');
if (!$('#' + escapedElement).length) {
console.log("Serendipity plugin error: " + escapedElement + " not found.");
}
}
serendipity.wrapSelection($('#'+escapedElement), str, '');
}
// Inserting media db img markup including s9y-specific container markup
serendipity.serendipity_imageSelector_done = function(textarea) {
var insert = '';
var img = '';
var src = '';
var alt = '';
var title = '';
var f = document.forms['serendipity[selForm]'].elements;
img = f['imgName'].value;
var imgWidth = f['imgWidth'].value;
var imgHeight = f['imgHeight'].value;
if (f['serendipity[linkThumbnail]'] && f['serendipity[linkThumbnail]'][0].checked == true) {
img = f['thumbName'].value;
imgWidth = f['imgThumbWidth'].value;
imgHeight = f['imgThumbHeight'].value;
}
if (parent.self.opener == undefined) {
// in iframes, there is no opener, and the magnific popup is wrapped
parent.self = window.parent.parent.$.magnificPopup;
parent.self.opener = window.parent.parent;
}
if (f['serendipity[filename_only]']) {
// this part is used when selecting only the image without further markup (-> category-icon)
var starget = f['serendipity[htmltarget]'] ? f['serendipity[htmltarget]'].value : 'serendipity[' + textarea + ']';
switch(f['serendipity[filename_only]'].value) {
case 'true':
parent.self.opener.serendipity.serendipity_imageSelector_addToElement(img, f['serendipity[htmltarget]'].value);
parent.self.close();
return true;
case 'id':
parent.self.opener.serendipity.serendipity_imageSelector_addToElement(f['imgID'].value, starget);
parent.self.close();
return true;
case 'thumb':
parent.self.opener.serendipity.serendipity_imageSelector_addToElement(f['thumbName'].value, starget);
parent.self.close();
return true;
case 'big':
parent.self.opener.serendipity.serendipity_imageSelector_addToElement(f['imgName'].value, starget);
parent.self.close();
return true;
}
}
alt = f['serendipity[alt]'].value.replace(/"/g, """);
title = f['serendipity[title]'].value.replace(/"/g, """);
var imgID = 0;
if (f['imgID']) {
imgID = f['imgID'].value;
}
var floating = $(':input[name="serendipity[align]"]:checked').val();
if (floating == "") {
floating = "center";
}
var lazyload = '';
if (imgWidth && imgHeight) {
lazyload = 'loading="lazy"';
}
img = "";
if ($(':input[name="serendipity[isLink]"]:checked').val() == "yes") {
// wrap the img in a link to the image. TODO: The label in the media_chooser.tpl explains it wrong
var targetval = $('#select_image_target').val();
var prepend = '';
var ilink = f['serendipity[url]'].value;
var itarget = '';
switch (targetval) {
case 'js':
var itarget = ' onclick="F1 = window.open(\'' + f['serendipity[url]'].value + '\',\'Zoom\',\''
+ 'height=' + (parseInt(f['imgHeight'].value) + 15) + ','
+ 'width=' + (parseInt(f['imgWidth'].value) + 15) + ','
+ 'top=' + (screen.height - f['imgHeight'].value) /2 + ','
+ 'left=' + (screen.width - f['imgWidth'].value) /2 + ','
+ 'toolbar=no,menubar=no,location=no,resize=1,resizable=1,scrollbars=yes\'); return false;"';
break;
case '_blank':
var itarget = ' target="_blank"';
break;
case 'plugin':
var itarget = ' id="s9yisphref' + imgID + '" onclick="javascript:this.href = this.href + \'&serendipity[from]=\' + self.location.href;"';
prepend = '';
ilink = f['baseURL'].value + 'serendipity_admin_image_selector.php?serendipity[step]=showItem&serendipity[image]=' + imgID;
break;
}
var img = prepend + "" + img + "";
}
if ($('#serendipity_imagecomment').val() != '') {
var comment = f['serendipity[imagecomment]'].value;
var img = '';
}
if (parent.self.opener.serendipity == undefined) {
// in iframes, there is no opener, and the magnific popup is wrapped
parent.self = window.parent.parent.$.magnificPopup;
parent.self.opener = window.parent.parent;
}
parent.self.opener.serendipity.serendipity_imageSelector_addToBody(img, textarea);
parent.self.close();
}
// Toggle extended entry editor
serendipity.toggle_extended = function(setCookie) {
if ($('#toggle_extended').length == 0) {
// this function got called on load of the editor
var toggleButton = '#toggle_extended';
$('#extended_entry_editor').parent().find('label').first().wrap('');
$(toggleButton).prepend(' ');
$(toggleButton).click(function(e) {
e.preventDefault();
serendipity.toggle_extended(true);
});
if (localStorage !== null && localStorage.show_extended_editor == "true") {
// the editor is visible by default - note the string, as bool is not supported yet in localStorage
return;
}
}
if ($('#extended_entry_editor:hidden').length > 0) {
$('#extended_entry_editor').show(); // use name selector instead of id here; id does not work
$('#tools_extended').show();
$('#toggle_extended').find('> .icon-right-dir').removeClass('icon-right-dir').addClass('icon-down-dir');
if (localStorage !== null) {
localStorage.show_extended_editor = "true";
}
} else {
$('#extended_entry_editor').hide();
$('#tools_extended').hide();
$('#toggle_extended').find('> .icon-down-dir').removeClass('icon-down-dir').addClass('icon-right-dir');
if (localStorage !== null) {
localStorage.show_extended_editor = "false";
}
}
if (setCookie) {
document.cookie = 'serendipity[toggle_extended]=' + (($('#extended_entry_editor:hidden').length == 0) ? "true" : "") + ';';
}
}
// Collapses/expands the category selector
var categoryselector_stored_categories = null;
serendipity.toggle_category_selector = function(id) {
if ($('#toggle_' + id).length == 0) {
// this function got called on load of the editor
var toggleButton = '#toggle_' + id;
$('#'+id).before('');
$(toggleButton).click(function(e) {
e.preventDefault();
$(this).toggleClass('active');
serendipity.toggle_category_selector(id);
});
$('#'+id).change(function(e) {
categoryselector_stored_categories = null;
});
if ($('#'+id).children('*[selected="selected"]').length > 1) {
// when loading the page new for the preview and more than one category was
// selected, collapsing the category-selector would lose those categories
$('#'+id).attr("size", $('#'+id).children().size);
$('#toggle_' + id).find('> .icon-right-dir').removeClass('icon-right-dir').addClass('icon-down-dir');
return
}
}
if ($('#'+id).attr("multiple")) {
if ($('#'+id).children(':selected').filter('[value!="0"]').length > 1) {
// when collapsing, all multiple selection needs to be saved to be restoreable if the click was a mistake
var selected_categories = '';
$('#'+id).children(':selected').filter('[value!="0"]').each(function(i, child) {
selected_categories += child.value + ','
});
categoryselector_stored_categories = selected_categories;
}
$('#'+id).removeAttr("multiple");
$('#'+id).removeAttr("size");
$('#toggle_' + id).find('> .icon-down-dir').removeClass('icon-down-dir').addClass('icon-right-dir');
} else {
$('#'+id).attr("multiple", "");
$('#'+id).attr("size", $('#'+id).children().size);
$('#toggle_' + id).find('> .icon-right-dir').removeClass('icon-right-dir').addClass('icon-down-dir');
var selected_categories = categoryselector_stored_categories;
if (selected_categories != null) {
selected_categories = selected_categories.split(',');
selected_categories.forEach(function(cat_id) {
if(cat_id) {
$('#'+id).find('[value="'+ cat_id +'"]').attr('selected', 'selected');
}
});
}
}
}
// save in the cookie which options were selected when inserting a image from the media db
serendipity.rememberMediaOptions = function() {
$('#imageForm :input').each(function(index, element) {
if (! (element.type == 'radio' && element.checked == false)) {
serendipity.SetCookie(element.name.replace(/\[/g, '_').replace(/\]/g, ''), $(element).val());
}
});
}
// Rescale image
serendipity.rescale = function(dim, newval) {
var ratio = $('#serendipityScaleImg').attr('data-imgheight')/$('#serendipityScaleImg').attr('data-imgwidth');
var trans = new Array();
trans['width'] = new Array('serendipity[height]', ratio);
trans['height'] = new Array('serendipity[width]', 1/ratio);
if ($('#resize_keepprops').is(':checked')) {
document.serendipityScaleForm.elements[trans[dim][0]].value=Math.round(trans[dim][1]*newval);
}
}
// Rename file in media db
serendipity.rename = function(id, fname) {
var newname;
var media_rename = 'Bitte einen neuen Namen eingeben für: ';
if (newname = prompt(media_rename + fname, fname)) {
var media_token_url = $('input[name*="serendipity[token]"]').val();
$.ajax({
type: 'POST',
url: '?serendipity[adminModule]=images&serendipity[adminAction]=rename&serendipity[fid]='+ escape(id) +'&serendipity[newname]='+ escape(newname) +'&serendipity[token]='+ media_token_url,
async: true,
cache: false,
success: function(response) {
$response = (response.trim() == '')
? '