diff -r c72b545f1304 -r 67bd3121a12e includes/clientside/tinymce/plugins/template/editor_plugin_src.js --- a/includes/clientside/tinymce/plugins/template/editor_plugin_src.js Wed Dec 26 00:37:26 2007 -0500 +++ b/includes/clientside/tinymce/plugins/template/editor_plugin_src.js Thu Dec 27 22:09:33 2007 -0500 @@ -1,551 +1,149 @@ /** - * $Id: editor_plugin_src.js 162 2007-01-03 16:16:52Z spocke $ + * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $ * * @author Moxiecode * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved. */ -tinyMCE.importPluginLanguagePack('template'); +(function() { + var each = tinymce.each; -var TinyMCE_TemplatePlugin = { - getInfo : function() { - return { - longname : 'Template plugin', - author : 'Moxiecode Systems AB', - authorurl : 'http://www.moxiecode.com', - infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/template', - version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion - }; - }, + tinymce.create('tinymce.plugins.TemplatePlugin', { + init : function(ed, url) { + var t = this; - initInstance : function(inst) { - var cdate, mdate, content, x = 0, key, value, rvals, ds = inst.getData('template'); - - // ensure the required elements and sttributes are added - //inst.cleanup.addRuleStr('*[' + TinyMCE_TemplatePlugin.TMPL_DATE_SRC_ATTR + '],div[title,tsrc]'); + t.editor = ed; - //setup template content functions - // creation date and modified date classes - cdate = tinyMCE.getParam("template_cdate_classes", '').split(/\s+/); - mdate = tinyMCE.getParam("template_mdate_classes", '').split(/\s+/); - - // classes that specify where selected content should go - content = tinyMCE.getParam("template_selected_content_classes", '').split(/\s+/); - - for (x = 0; x < cdate.length; x++) - TinyMCE_TemplatePlugin.functions[cdate[x]] = TinyMCE_TemplatePlugin.functions['cdate']; + // Register commands + ed.addCommand('mceTemplate', function(ui) { + ed.windowManager.open({ + file : url + '/template.htm', + width : ed.getParam('template_popup_width', 750), + height : ed.getParam('template_popup_height', 600), + inline : 1 + }, { + plugin_url : url + }); + }); - for (x = 0; x < mdate.length; x++) - TinyMCE_TemplatePlugin.functions[mdate[x]] = TinyMCE_TemplatePlugin.functions['mdate']; + ed.addCommand('mceInsertTemplate', t._insertTemplate, t); - for (x = 0; x < content.length; x++) - TinyMCE_TemplatePlugin.functions[content[x]] = TinyMCE_TemplatePlugin.functions['selectedContent']; - - // special template functions for replacing template content - rvals = tinyMCE.getParam("template_replace_values", false); - for (key in rvals) { - value = rvals[key]; + // Register buttons + ed.addButton('template', {title : 'template.desc', cmd : 'mceTemplate'}); - if (typeof value == "function") - TinyMCE_TemplatePlugin.functions[key] = value; - else - TinyMCE_TemplatePlugin.functions[key] = TinyMCE_TemplatePlugin.functions['generateReplacer'](value); - } - - // Setup replace_items - rvals = tinyMCE.getParam("template_replace_values", false); - ds.replace_items = {}; + ed.onPreProcess.add(function(ed, o) { + var dom = ed.dom; - for (key in rvals) - ds.replace_items[key] = rvals[key]; - - inst.addShortcut('ctrl', 't', 'lang_template_desc', 'mceTemplate'); - - // Setup data storage - ds.currentAction = "insert"; - ds.currentTmplNode = null; - }, + each(dom.select('div', o.node), function(e) { + if (dom.hasClass(e, 'mceTmpl')) { + each(dom.select('*', e), function(e) { + if (dom.hasClass(e, ed.getParam('template_mdate_classes').replace(/\s+/g, '|'))) + e.innerHTML = t._getDateTime(new Date(), ed.getParam("template_mdate_format", ed.getLang("template.mdate_format"))); + }); - getControlHTML : function(cn) { - switch (cn) { - case "template": - return tinyMCE.getButtonHTML(cn, 'lang_template_desc', '{$pluginurl}/images/template.gif', 'mceTemplate', true); - } - - return ""; - }, - - execCommand : function(editor_id, element, command, user_interface, value) { - var nodeArray, current, newTmpl, x, inst = tinyMCE.getInstanceById(editor_id), ds = inst.getData('template'), telm; + t._replaceVals(e); + } + }); + }); + }, - switch (command) { - case "mceTemplate": - if (user_interface) { - // called from toolbar button - show the popup - tinyMCE.openWindow({ - file : '../../plugins/template/template.htm', // Relative to theme - width : tinyMCE.getParam('template_popup_width', 750), - height : tinyMCE.getParam('template_popup_height', 600) - }, {editor_id : editor_id, resizable : "yes", scrollbars : "no", pluginObj : TinyMCE_TemplatePlugin}); - } else { - // internal command do the template stuff + getInfo : function() { + return { + longname : 'Template plugin', + author : 'Moxiecode Systems AB', + authorurl : 'http://www.moxiecode.com', + infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/template', + version : tinymce.majorVersion + "." + tinymce.minorVersion + }; + }, - // get the returned HTML string from the pop-up and appened it to a DIV element - telm = TinyMCE_TemplatePlugin._convertToNode(value.body); + _insertTemplate : function(ui, v) { + var t = this, ed = t.editor, h, el, dom = ed.dom, sel = ed.selection.getContent(); + + h = v.content; - // Find template body - nodeArray = tinyMCE.selectElements(telm, 'div', function(n) { - return tinyMCE.hasCSSClass(n, TinyMCE_TemplatePlugin.TMPL); - }); + each(t.editor.getParam('template_replace_values'), function(v, k) { + if (typeof(v) != 'function') + h = h.replace(new RegExp('\\{\\$' + k + '\\}', 'g'), v); + }); - telm = nodeArray.length > 0 ? nodeArray[0] : null; - nodeArray = []; + el = dom.create('div', null, h); - if (ds.currentAction == "insert") { - //insert new template after applying all the template content functions + function hasClass(n, c) { + return new RegExp('\\b' + c + '\\b', 'g').test(n.className); + }; - // Is it a template or snippet - if (telm) { - tinyMCE.execCommand('mceBeginUndoLevel'); - ds.currentAction = "insert-new"; - TinyMCE_TemplatePlugin._insertTemplate(editor_id, telm, value.title, value.tsrc, true); - ds.currentAction == "insert"; - tinyMCE.execCommand('mceEndUndoLevel'); - tinyMCE.execInstanceCommand(editor_id, 'mceCleanup', false); - } else - tinyMCE.execCommand('mceInsertContent', false, this._replaceValues(value.body)); - } else { - // First collect the selected template in the editor - nodeArray = TinyMCE_TemplatePlugin._collectTemplateElements(ds.currentTmplNode); - current = []; - newTmpl = []; - tinyMCE.getNodeTree(telm, newTmpl); + each(dom.select('*', el), function(n) { + // Replace cdate + if (hasClass(n, ed.getParam('template_cdate_classes', 'cdate').replace(/\s+/g, '|'))) + n.innerHTML = t._getDateTime(new Date(), ed.getParam("template_cdate_format", ed.getLang("template.cdate_format"))); - for (x=0; x', 'gi'), - '' - ); - - // delete any empty template wrappers - content = content.replace( - new RegExp('
(\s| | )?(|\s)?
', 'gi'), - '' - ); - - // replace the closing wrapper tag - content = content.replace( - new RegExp('', 'gi'), - '' - ); - - break; - - case "insert_to_editor": - // replace HTML comment with DIV wrapper - content = content.replace( - new RegExp('', 'gi'), - '
' - ); - - content = content.replace( - new RegExp('', 'gi'), - '
' - ); - - break; - - case "get_from_editor_dom": - // apply template content replacement functions - nodes = tinyMCE.selectNodes(content, function(n) { - return tinyMCE.hasCSSClass(n, TinyMCE_TemplatePlugin.TMPL_ELEMENT); - } - ); - - TinyMCE_TemplatePlugin._applyFunctions(nodes, type); - - break; - - case "insert_to_editor_dom": - // apply template content replacement functions - nodes = tinyMCE.selectNodes(content, function(n) { - return tinyMCE.hasCSSClass(n, TinyMCE_TemplatePlugin.TMPL_ELEMENT); - } - ); - - TinyMCE_TemplatePlugin._applyFunctions(nodes, type); - - break; - } - - return content; - }, - - // Private plugin internal methods - - /** - * Creates a HTML DIV element and sets the innerHTML to equal the temlate innerHTML so that the template can be manipulated as DOM nodes. - * - * @param {string} Template innerHTML - * @return a HTML Element - * @type HTMLElement - */ - _convertToNode : function(html) { - var elm = document.createElement('div'); - - elm.innerHTML = html; - - return elm; - }, - - /** - * pass an array of template html elements and they will have the template class name added and any template functions applied - * - * @param {array} template HTML elements - * @return array of template HTML elements - * @type array - */ - _prepareTemplateContent : function(elms) { - var x, n, nodes = []; - - if (!elms) - return {}; - - if (!elms.length) - elms = [elms]; - - for (x = 0; x'; - html += elm.innerHTML; - html += ''; - - tinyMCE.execInstanceCommand(editor_id, 'mceInsertContent', false, html); - }, - - /** - * template functions - functions for modifying template content - */ - functions : { - blank : function(elm, editor_event) {}, - - cdate : function(elm, editor_event) { - var d, dsrc; - - if (editor_event != TinyMCE_TemplatePlugin.TMPL_TEMPLATE_EVENT) - return; - - d = new Date(); - // find out if the creation date was previously stored - dsrc = elm.innerHTML.match(new RegExp("", "gi")); + return fmt; + } + }); - if (dsrc) - d = new Date(RegExp.$1); - - elm.innerHTML = TinyMCE_TemplatePlugin._getDateTime(d, tinyMCE.getParam("template_cdate_format", tinyMCE.getLang("lang_template_def_date_format"))); - //now we have to store the date value in a format easily read again, in case a future template change changes the date format... - elm.innerHTML += ""; - }, - - mdate : function(elm, editor_event) { - var d = new Date(); - elm.innerHTML = TinyMCE_TemplatePlugin._getDateTime(d, tinyMCE.getParam("template_mdate_format", tinyMCE.getLang("lang_template_def_date_format"))); - }, - - /** - * This will insert the currently selected editor content into the template element. - * It only does this if the template inserted is a new one and if the element does not have the special class. - * The special class name prevents this from happening more than once. - */ - selectedContent : function(elm, editor_event) { - var ds = tinyMCE.selectedInstance.getData('template'); - - if (editor_event != TinyMCE_TemplatePlugin.TMPL_TEMPLATE_EVENT) - return; - - if (ds.currentAction == "insert-new" && !tinyMCE.hasCSSClass(elm, TinyMCE_TemplatePlugin.TMPL_SEL_HTML_DONE)) { - elm.innerHTML = tinyMCE.selectedInstance.selection.getSelectedHTML(); - tinyMCE.addCSSClass(elm, TinyMCE_TemplatePlugin.TMPL_SEL_HTML_DONE); - } - }, - - /** - * When the plugin is initialised this generates the functions that insert configured strings into template elements. - */ - generateReplacer : function(s) { - return function(elm, editor_event) {elm.innerHTML = "" + s;}; - } - }, - - /** - * formats a date according to the format string - straight from the 'insert date/time' plugin - * - * @param {Date} date object - * @param {string} format string - * @return formatted date - * @type string - */ - _getDateTime : function(d,fmt) { - if (!fmt) - return ""; - - function addZeros(value, len) { - var i; - - value = "" + value; - - if (value.length < len) { - for (i=0; i<(len-value.length); i++) - value = "0" + value; - } - - return value; - } - - fmt = fmt.replace("%D", "%m/%d/%y"); - fmt = fmt.replace("%r", "%I:%M:%S %p"); - fmt = fmt.replace("%Y", "" + d.getFullYear()); - fmt = fmt.replace("%y", "" + d.getYear()); - fmt = fmt.replace("%m", addZeros(d.getMonth()+1, 2)); - fmt = fmt.replace("%d", addZeros(d.getDate(), 2)); - fmt = fmt.replace("%H", "" + addZeros(d.getHours(), 2)); - fmt = fmt.replace("%M", "" + addZeros(d.getMinutes(), 2)); - fmt = fmt.replace("%S", "" + addZeros(d.getSeconds(), 2)); - fmt = fmt.replace("%I", "" + ((d.getHours() + 11) % 12 + 1)); - fmt = fmt.replace("%p", "" + (d.getHours() < 12 ? "AM" : "PM")); - fmt = fmt.replace("%B", "" + tinyMCE.getLang("lang_template_months_long")[d.getMonth()]); - fmt = fmt.replace("%b", "" + tinyMCE.getLang("lang_template_months_short")[d.getMonth()]); - fmt = fmt.replace("%A", "" + tinyMCE.getLang("lang_template_day_long")[d.getDay()]); - fmt = fmt.replace("%a", "" + tinyMCE.getLang("lang_template_day_short")[d.getDay()]); - fmt = fmt.replace("%%", "%"); - - return fmt; - }, - - TMPL_ELEMENT : 'mceTmplElm', - TMPL : 'mceTmpl', - TMPL_BEGINS : 'mceTmplBegins', - TMPL_SEL_HTML_DONE : 'mceSelHTMLDone', - TMPL_ENDS : 'mceTmplEnds', - TMPL_DATE_SRC_ATTR : 'mcetmpldtesrc', - TMPL_TEMPLATE_EVENT : 'prepare_template' -}; - -tinyMCE.addPlugin("template", TinyMCE_TemplatePlugin); + // Register plugin + tinymce.PluginManager.add('template', tinymce.plugins.TemplatePlugin); +})(); \ No newline at end of file