bgneal@45: /** bgneal@45: * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $ bgneal@45: * bgneal@45: * @author Moxiecode bgneal@45: * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved. bgneal@45: */ bgneal@45: bgneal@45: (function() { bgneal@45: var each = tinymce.each; bgneal@45: bgneal@45: tinymce.create('tinymce.plugins.TemplatePlugin', { bgneal@45: init : function(ed, url) { bgneal@45: var t = this; bgneal@45: bgneal@45: t.editor = ed; bgneal@45: bgneal@45: // Register commands bgneal@45: ed.addCommand('mceTemplate', function(ui) { bgneal@45: ed.windowManager.open({ bgneal@45: file : url + '/template.htm', bgneal@45: width : ed.getParam('template_popup_width', 750), bgneal@45: height : ed.getParam('template_popup_height', 600), bgneal@45: inline : 1 bgneal@45: }, { bgneal@45: plugin_url : url bgneal@45: }); bgneal@45: }); bgneal@45: bgneal@45: ed.addCommand('mceInsertTemplate', t._insertTemplate, t); bgneal@45: bgneal@45: // Register buttons bgneal@45: ed.addButton('template', {title : 'template.desc', cmd : 'mceTemplate'}); bgneal@45: bgneal@45: ed.onPreProcess.add(function(ed, o) { bgneal@45: var dom = ed.dom; bgneal@45: bgneal@45: each(dom.select('div', o.node), function(e) { bgneal@45: if (dom.hasClass(e, 'mceTmpl')) { bgneal@45: each(dom.select('*', e), function(e) { bgneal@45: if (dom.hasClass(e, ed.getParam('template_mdate_classes', 'mdate').replace(/\s+/g, '|'))) bgneal@45: e.innerHTML = t._getDateTime(new Date(), ed.getParam("template_mdate_format", ed.getLang("template.mdate_format"))); bgneal@45: }); bgneal@45: bgneal@45: t._replaceVals(e); bgneal@45: } bgneal@45: }); bgneal@45: }); bgneal@45: }, bgneal@45: bgneal@45: getInfo : function() { bgneal@45: return { bgneal@45: longname : 'Template plugin', bgneal@45: author : 'Moxiecode Systems AB', bgneal@45: authorurl : 'http://www.moxiecode.com', bgneal@45: infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/template', bgneal@45: version : tinymce.majorVersion + "." + tinymce.minorVersion bgneal@45: }; bgneal@45: }, bgneal@45: bgneal@45: _insertTemplate : function(ui, v) { bgneal@45: var t = this, ed = t.editor, h, el, dom = ed.dom, sel = ed.selection.getContent(); bgneal@45: bgneal@45: h = v.content; bgneal@45: bgneal@45: each(t.editor.getParam('template_replace_values'), function(v, k) { bgneal@45: if (typeof(v) != 'function') bgneal@45: h = h.replace(new RegExp('\\{\\$' + k + '\\}', 'g'), v); bgneal@45: }); bgneal@45: bgneal@45: el = dom.create('div', null, h); bgneal@45: bgneal@45: // Find template element within div bgneal@45: n = dom.select('.mceTmpl', el); bgneal@45: if (n && n.length > 0) { bgneal@45: el = dom.create('div', null); bgneal@45: el.appendChild(n[0].cloneNode(true)); bgneal@45: } bgneal@45: bgneal@45: function hasClass(n, c) { bgneal@45: return new RegExp('\\b' + c + '\\b', 'g').test(n.className); bgneal@45: }; bgneal@45: bgneal@45: each(dom.select('*', el), function(n) { bgneal@45: // Replace cdate bgneal@45: if (hasClass(n, ed.getParam('template_cdate_classes', 'cdate').replace(/\s+/g, '|'))) bgneal@45: n.innerHTML = t._getDateTime(new Date(), ed.getParam("template_cdate_format", ed.getLang("template.cdate_format"))); bgneal@45: bgneal@45: // Replace mdate bgneal@45: if (hasClass(n, ed.getParam('template_mdate_classes', 'mdate').replace(/\s+/g, '|'))) bgneal@45: n.innerHTML = t._getDateTime(new Date(), ed.getParam("template_mdate_format", ed.getLang("template.mdate_format"))); bgneal@45: bgneal@45: // Replace selection bgneal@45: if (hasClass(n, ed.getParam('template_selected_content_classes', 'selcontent').replace(/\s+/g, '|'))) bgneal@45: n.innerHTML = sel; bgneal@45: }); bgneal@45: bgneal@45: t._replaceVals(el); bgneal@45: bgneal@45: ed.execCommand('mceInsertContent', false, el.innerHTML); bgneal@45: ed.addVisual(); bgneal@45: }, bgneal@45: bgneal@45: _replaceVals : function(e) { bgneal@45: var dom = this.editor.dom, vl = this.editor.getParam('template_replace_values'); bgneal@45: bgneal@45: each(dom.select('*', e), function(e) { bgneal@45: each(vl, function(v, k) { bgneal@45: if (dom.hasClass(e, k)) { bgneal@45: if (typeof(vl[k]) == 'function') bgneal@45: vl[k](e); bgneal@45: } bgneal@45: }); bgneal@45: }); bgneal@45: }, bgneal@45: bgneal@45: _getDateTime : function(d, fmt) { bgneal@45: if (!fmt) bgneal@45: return ""; bgneal@45: bgneal@45: function addZeros(value, len) { bgneal@45: var i; bgneal@45: bgneal@45: value = "" + value; bgneal@45: bgneal@45: if (value.length < len) { bgneal@45: for (i=0; i<(len-value.length); i++) bgneal@45: value = "0" + value; bgneal@45: } bgneal@45: bgneal@45: return value; bgneal@45: } bgneal@45: bgneal@45: fmt = fmt.replace("%D", "%m/%d/%y"); bgneal@45: fmt = fmt.replace("%r", "%I:%M:%S %p"); bgneal@45: fmt = fmt.replace("%Y", "" + d.getFullYear()); bgneal@45: fmt = fmt.replace("%y", "" + d.getYear()); bgneal@45: fmt = fmt.replace("%m", addZeros(d.getMonth()+1, 2)); bgneal@45: fmt = fmt.replace("%d", addZeros(d.getDate(), 2)); bgneal@45: fmt = fmt.replace("%H", "" + addZeros(d.getHours(), 2)); bgneal@45: fmt = fmt.replace("%M", "" + addZeros(d.getMinutes(), 2)); bgneal@45: fmt = fmt.replace("%S", "" + addZeros(d.getSeconds(), 2)); bgneal@45: fmt = fmt.replace("%I", "" + ((d.getHours() + 11) % 12 + 1)); bgneal@45: fmt = fmt.replace("%p", "" + (d.getHours() < 12 ? "AM" : "PM")); bgneal@45: fmt = fmt.replace("%B", "" + tinyMCE.getLang("template_months_long").split(',')[d.getMonth()]); bgneal@45: fmt = fmt.replace("%b", "" + tinyMCE.getLang("template_months_short").split(',')[d.getMonth()]); bgneal@45: fmt = fmt.replace("%A", "" + tinyMCE.getLang("template_day_long").split(',')[d.getDay()]); bgneal@45: fmt = fmt.replace("%a", "" + tinyMCE.getLang("template_day_short").split(',')[d.getDay()]); bgneal@45: fmt = fmt.replace("%%", "%"); bgneal@45: bgneal@45: return fmt; bgneal@45: } bgneal@45: }); bgneal@45: bgneal@45: // Register plugin bgneal@45: tinymce.PluginManager.add('template', tinymce.plugins.TemplatePlugin); bgneal@45: })();