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