"use strict"; // Insert text into textarea at Caret Position function insert_at_caret(areaId, text) { var txtarea = document.getElementById(areaId), scrollPos = txtarea.scrollTop, strPos = 0, br = ((txtarea.selectionStart || txtarea.selectionStart === '0') ? "ff" : (document.selection ? "ie" : false)), range; if (br === "ie") { txtarea.focus(); range = document.selection.createRange(); range.moveStart('character', -txtarea.value.length); strPos = range.text.length; } else if (br === "ff") { strPos = txtarea.selectionStart; } var front = (txtarea.value).substring(0, strPos), back = (txtarea.value).substring(strPos, txtarea.value.length); txtarea.value = front + text + back; strPos = strPos + text.length; if (br === "ie") { txtarea.focus(); range = document.selection.createRange(); range.moveStart('character', -txtarea.value.length); range.moveStart('character', strPos); range.moveEnd('character', 0); range.select(); } else if (br === "ff") { txtarea.selectionStart = strPos; txtarea.selectionEnd = strPos; txtarea.focus(); } txtarea.scrollTop = scrollPos; } // takes mdl_email_template row as JSON, array with names to use in form fields. function inject_email_template(template_fields, email_template) { $.each(email_template, function (key, val) { // remove prefix from key key = key.replace("email_template_", ""); // if key is in template_fields, apply value to form field if (val && template_fields.indexOf(key) > -1) { if (key === 'body') { $("#" + key).html(val); } else { $("#" + key).val(val); } } }); } function update_email_template_preview() { $('#email-template-preview').contents().find("body").html($('.email-template-body').val()); } // Insert HTML tags into textarea function insert_html_tag(tag_type, destination_id) { var text, sel, text_area, selectedText, startPos, endPos, replace, replaceText, len; switch (tag_type) { case 'text-bold': text = ['', '']; break; case 'text-italic': text = ['', '']; break; case 'text-paragraph': text = ['

', '

']; break; case 'text-h1': text = ['

', '

']; break; case 'text-h2': text = ['

', '

']; break; case 'text-h3': text = ['

', '

']; break; case 'text-h4': text = ['

', '

']; break; case 'text-code': text = ['', '']; break; case 'text-hr': text = ['
', '']; break; case 'text-css': text = ['', '']; break; } // Get the selected text text_area = document.getElementById(destination_id); if (document.selection !== undefined) { text_area.focus(); sel = document.selection.createRange(); selectedText = sel.text; } else if (text_area.selectionStart !== '') { startPos = text_area.selectionStart; endPos = text_area.selectionEnd; selectedText = text_area.value.substring(startPos, endPos); } // Check if