[1] | 1 | /** |
---|
| 2 | * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $ |
---|
| 3 | * |
---|
| 4 | * @author Moxiecode |
---|
| 5 | * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved. |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | (function() { |
---|
| 9 | var each = tinymce.each; |
---|
| 10 | |
---|
| 11 | tinymce.create('tinymce.plugins.TemplatePlugin', { |
---|
| 12 | init : function(ed, url) { |
---|
| 13 | var t = this; |
---|
| 14 | |
---|
| 15 | t.editor = ed; |
---|
| 16 | |
---|
| 17 | // Register commands |
---|
| 18 | ed.addCommand('mceTemplate', function(ui) { |
---|
| 19 | ed.windowManager.open({ |
---|
| 20 | file : url + '/template.htm', |
---|
| 21 | width : ed.getParam('template_popup_width', 750), |
---|
| 22 | height : ed.getParam('template_popup_height', 600), |
---|
| 23 | inline : 1 |
---|
| 24 | }, { |
---|
| 25 | plugin_url : url |
---|
| 26 | }); |
---|
| 27 | }); |
---|
| 28 | |
---|
| 29 | ed.addCommand('mceInsertTemplate', t._insertTemplate, t); |
---|
| 30 | |
---|
| 31 | // Register buttons |
---|
| 32 | ed.addButton('template', {title : 'template.desc', cmd : 'mceTemplate'}); |
---|
| 33 | |
---|
| 34 | ed.onPreProcess.add(function(ed, o) { |
---|
| 35 | var dom = ed.dom; |
---|
| 36 | |
---|
| 37 | each(dom.select('div', o.node), function(e) { |
---|
| 38 | if (dom.hasClass(e, 'mceTmpl')) { |
---|
| 39 | each(dom.select('*', e), function(e) { |
---|
| 40 | if (dom.hasClass(e, ed.getParam('template_mdate_classes').replace(/\s+/g, '|'))) |
---|
| 41 | e.innerHTML = t._getDateTime(new Date(), ed.getParam("template_mdate_format", ed.getLang("template.mdate_format"))); |
---|
| 42 | }); |
---|
| 43 | |
---|
| 44 | t._replaceVals(e); |
---|
| 45 | } |
---|
| 46 | }); |
---|
| 47 | }); |
---|
| 48 | }, |
---|
| 49 | |
---|
| 50 | getInfo : function() { |
---|
| 51 | return { |
---|
| 52 | longname : 'Template plugin', |
---|
| 53 | author : 'Moxiecode Systems AB', |
---|
| 54 | authorurl : 'http://www.moxiecode.com', |
---|
| 55 | infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/template', |
---|
| 56 | version : tinymce.majorVersion + "." + tinymce.minorVersion |
---|
| 57 | }; |
---|
| 58 | }, |
---|
| 59 | |
---|
| 60 | _insertTemplate : function(ui, v) { |
---|
| 61 | var t = this, ed = t.editor, h, el, dom = ed.dom, sel = ed.selection.getContent(); |
---|
| 62 | |
---|
| 63 | h = v.content; |
---|
| 64 | |
---|
| 65 | each(t.editor.getParam('template_replace_values'), function(v, k) { |
---|
| 66 | if (typeof(v) != 'function') |
---|
| 67 | h = h.replace(new RegExp('\\{\\$' + k + '\\}', 'g'), v); |
---|
| 68 | }); |
---|
| 69 | |
---|
| 70 | el = dom.create('div', null, h); |
---|
| 71 | |
---|
| 72 | function hasClass(n, c) { |
---|
| 73 | return new RegExp('\\b' + c + '\\b', 'g').test(n.className); |
---|
| 74 | }; |
---|
| 75 | |
---|
| 76 | each(dom.select('*', el), function(n) { |
---|
| 77 | // Replace cdate |
---|
| 78 | if (hasClass(n, ed.getParam('template_cdate_classes', 'cdate').replace(/\s+/g, '|'))) |
---|
| 79 | n.innerHTML = t._getDateTime(new Date(), ed.getParam("template_cdate_format", ed.getLang("template.cdate_format"))); |
---|
| 80 | |
---|
| 81 | // Replace mdate |
---|
| 82 | if (hasClass(n, ed.getParam('template_mdate_classes', 'mdate').replace(/\s+/g, '|'))) |
---|
| 83 | n.innerHTML = t._getDateTime(new Date(), ed.getParam("template_mdate_format", ed.getLang("template.mdate_format"))); |
---|
| 84 | |
---|
| 85 | // Replace selection |
---|
| 86 | if (hasClass(n, ed.getParam('template_selected_content_classes', 'selcontent').replace(/\s+/g, '|'))) |
---|
| 87 | n.innerHTML = sel; |
---|
| 88 | }); |
---|
| 89 | |
---|
| 90 | t._replaceVals(el); |
---|
| 91 | |
---|
| 92 | ed.execCommand('mceInsertContent', false, el.innerHTML); |
---|
| 93 | ed.addVisual(); |
---|
| 94 | }, |
---|
| 95 | |
---|
| 96 | _replaceVals : function(e) { |
---|
| 97 | var dom = this.editor.dom, vl = this.editor.getParam('template_replace_values'); |
---|
| 98 | |
---|
| 99 | each(dom.select('*', e), function(e) { |
---|
| 100 | each(vl, function(v, k) { |
---|
| 101 | if (dom.hasClass(e, k)) { |
---|
| 102 | if (typeof(vl[k]) == 'function') |
---|
| 103 | vl[k](e); |
---|
| 104 | } |
---|
| 105 | }); |
---|
| 106 | }); |
---|
| 107 | }, |
---|
| 108 | |
---|
| 109 | _getDateTime : function(d, fmt) { |
---|
| 110 | if (!fmt) |
---|
| 111 | return ""; |
---|
| 112 | |
---|
| 113 | function addZeros(value, len) { |
---|
| 114 | var i; |
---|
| 115 | |
---|
| 116 | value = "" + value; |
---|
| 117 | |
---|
| 118 | if (value.length < len) { |
---|
| 119 | for (i=0; i<(len-value.length); i++) |
---|
| 120 | value = "0" + value; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | return value; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | fmt = fmt.replace("%D", "%m/%d/%y"); |
---|
| 127 | fmt = fmt.replace("%r", "%I:%M:%S %p"); |
---|
| 128 | fmt = fmt.replace("%Y", "" + d.getFullYear()); |
---|
| 129 | fmt = fmt.replace("%y", "" + d.getYear()); |
---|
| 130 | fmt = fmt.replace("%m", addZeros(d.getMonth()+1, 2)); |
---|
| 131 | fmt = fmt.replace("%d", addZeros(d.getDate(), 2)); |
---|
| 132 | fmt = fmt.replace("%H", "" + addZeros(d.getHours(), 2)); |
---|
| 133 | fmt = fmt.replace("%M", "" + addZeros(d.getMinutes(), 2)); |
---|
| 134 | fmt = fmt.replace("%S", "" + addZeros(d.getSeconds(), 2)); |
---|
| 135 | fmt = fmt.replace("%I", "" + ((d.getHours() + 11) % 12 + 1)); |
---|
| 136 | fmt = fmt.replace("%p", "" + (d.getHours() < 12 ? "AM" : "PM")); |
---|
| 137 | fmt = fmt.replace("%B", "" + tinyMCE.getLang("template_months_long").split(',')[d.getMonth()]); |
---|
| 138 | fmt = fmt.replace("%b", "" + tinyMCE.getLang("template_months_short").split(',')[d.getMonth()]); |
---|
| 139 | fmt = fmt.replace("%A", "" + tinyMCE.getLang("template_day_long").split(',')[d.getDay()]); |
---|
| 140 | fmt = fmt.replace("%a", "" + tinyMCE.getLang("template_day_short").split(',')[d.getDay()]); |
---|
| 141 | fmt = fmt.replace("%%", "%"); |
---|
| 142 | |
---|
| 143 | return fmt; |
---|
| 144 | } |
---|
| 145 | }); |
---|
| 146 | |
---|
| 147 | // Register plugin |
---|
| 148 | tinymce.PluginManager.add('template', tinymce.plugins.TemplatePlugin); |
---|
| 149 | })(); |
---|