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