annotate static/js/tiny_mce/plugins/template/editor_plugin_src.js @ 738:df2799f725d8

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