/* Wenn im IE6+7 ein Meta Tag (meta name="description" content="") steht klappt das komplette insertAtCaret nicht, da dann createTextRange nicht als Eigenschaft erkennt und in den dritten else Zweig geht. sonst geht er in den Zweig else if (textField.createTextRange) { und alles klappt. */ function show_preview(textarea_obj) { } // Constants var CONST_NL = "\n"; // Add a handler to an event function addEventHandler(obj, eventType, handler) { if (window.opera && obj.addEventListener) { obj.addEventListener(eventType, handler, false); return true; } else if (obj.addEventListener) { obj.addEventListener(eventType, handler, true); return true; } else if (obj.attachEvent) { var r = obj.attachEvent("on"+eventType, handler); return r; } else { return false; } } function controlsSetBold(textField) { insertAtCaret(textField, '[b]', '[/b]'); } function controlsSetItalic(textField) { insertAtCaret(textField, '[i]', '[/i]'); } function controlsSetUnderline(textField) { insertAtCaret(textField, '[u]', '[/u]'); } // A dummy string replacer function /* function controlsReplace(textField) { insertAtCaret(textField, 'replaced', null, true); } */ function controlsSetLink(textField) { var LinkToInsert = false; LinkToInsert = prompt("Bitte geben Sie den Link ein", "www."); insertAtCaret(textField,LinkToInsert, null, true); if (LinkToInsert != null) { var Ergebnis = LinkToInsert.match(/^(www\.)?[\w-]+\.[a-zA-Z]{2,6}(\/{1}[\w_-]*)*(\.[a-zA-Z]{2,5})?(\?{1}[a-zA-Z0-9_=&]*)?$/i); if (Ergebnis) { TextforLink = prompt("Bitte geben Sie den Text für den Link ein","Linktext"); if (TextforLink == null) { TextforLink = LinkToInsert; } var linkstr='[a('+LinkToInsert+')('+TextforLink+')] '; insertAtCaret(textField,linkstr, null, true); } else alert("Der Link scheint nicht korrekt. Bitte überprüfen Sie die Schreibweise!\nBeispiele:\nwww.a-s-b.eu\nwww.a-s-b.eu/kurse.php") } } /** * Insert string at the current caret position. * * @param {object} textField The subject of the function. * @param {string} beforeStr This string will be inserted before the caret. * @param {string} afterStr This string will be inserted after the caret. Its default is ''. * @param {boolean} replace If this flag is set to true, the inserted string * replace the formerly selected text by the caret. * Its default is false. * @param {boolean} select If this flag is set to true, the inserted string (and the * formerly selected text if replace was false) is selected. * Its default is true. */ function insertAtCaret(textField, before, after, replace, select) { if (arguments.length < 3 || !after) after = ''; if (arguments.length < 4 || !replace) replace = false; if (arguments.length < 5 || select) select = true; if (typeof textField.selectionStart != 'undefined') { var startPos = textField.selectionStart; var endPos = textField.selectionEnd; textField.value = textField.value.substr(0, startPos) + before + (replace ? "" : textField.value.substring(startPos, endPos)) + after + textField.value.substr(endPos); if (select) { textField.setSelectionRange(startPos, endPos + before.length + after.length - (replace ? endPos-startPos : 0)); } else { startPos = startPos + before.length + after.length; textField.setSelectionRange(startPos , startPos); } } else if (textField.createTextRange) { textField.focus(); var pos = getCaret(textField); var range = document.selection.createRange(); // Adding the before/after text to the textRange range.text = before+(replace ? "" : range.text)+after; // Selecting the needed text if (select) { pos.selectionLength = replace ? before.length + after.length : pos.selectionLength + before.length + after.length; } else { pos.position += before.length + after.length; pos.selectionLength = 0; } setCaret(textField, pos); } else { var position = textField.value.length+before.length; textField.value += (before + after); setCursorPosition(textField, position); } } /** * Get the caret position info of the given textfield * * @param {object} textField The subject of the function. * @param {boolean} MSIEReturnNotLogicalPosition This parameter is used for inner purpose. * * @return {object} Returns a simple JavaScript object with position and selectionLength properties */ function getCaret(textField, MSIEReturnNotLogicalPosition) { if (arguments.length < 2) MSIEReturnNotLogicalPosition = false; if (typeof textField.selectionStart != "undefined") { textField.focus(); return { position: textField.selectionStart, selectionLength: textField.selectionEnd-textField.selectionStart }; // Handling this issue in case of Internet Explorer need a little hack. // The problem is, that IE handle the concept of new line different // from text field value and textRange "character" unit point of view. // In the value the \r,\n charcters exist separately, but when use // the textRange.move("charcter", 1) new line is only one step. // That's why we need a little correction. } else if (textField.createTextRange) { var newLineNumAfterCursor = 0; match = textField.value.match(/\n/g); if (match) { newLineNumAfterCursor = match.length; } textField.focus(); var position = textField.value.length; var selectionLength = document.selection.createRange().text.length; var cursor = document.selection.createRange().duplicate(); while (cursor.parentElement() == textField && cursor.move('character', 1)) { if (textField.value.charAt(position - 1) == CONST_NL) { position -= 1; newLineNumAfterCursor--; } position--; } if (MSIEReturnNotLogicalPosition) { return { position: position + 1, selectionLength: selectionLength }; } else { return { position: position + 1 - (newLineNumAfterCursor), selectionLength: selectionLength }; } } } /** * Set the caret in the given textfield * * @param {object} textField The subject of the function. * @param {object} pos This parameter could be a number or a simple JavaScript * object with position and selectionLength properties. * If number was given the function creates a position * object, where the position property is the given number * and the selectionLength is 0. If the parameter is an * object and has no position property, then the function * set it to the current caret position. The selectionLength * property could be negative. * @param {number} scrollTop If this parameter is peresent, the function * try to set the textField's scrollTop property. */ function setCaret(textField, pos, scrollTop) { // If the function was called with a number only if (typeof pos == "number") { pos = {position: pos, selectionLength: 0} } // If the function was called with an object only with a selectionLength property if (typeof pos.position == "undefined") { pos.position = getCaret(textField).position; } // If negative selectionLength was given if (pos.selectionLength < 0) { pos.position += pos.selectionLength; pos.selectionLength = -pos.selectionLength; } if (typeof textField.selectionStart != 'undefined') { textField.focus(); textField.setSelectionRange(pos.position, pos.position+pos.selectionLength); } else if (textField.createTextRange) { var textRange = textField.createTextRange(); textRange.moveStart("character", pos.position); textRange.collapse(); textRange.moveEnd("character", pos.selectionLength); textRange.select(); } if (typeof scrollTop != "undefined" && typeof textField.scrollTop != "undefined") { textField.scrollTop = scrollTop; } }