4 * Copyright 2009, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
12 var each = tinymce.each;
14 tinymce.create('tinymce.plugins.TemplatePlugin', {
15 init : function(ed, url) {
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),
32 ed.addCommand('mceInsertTemplate', t._insertTemplate, t);
35 ed.addButton('template', {title : 'template.desc', cmd : 'mceTemplate'});
37 ed.onPreProcess.add(function(ed, o) {
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")));
53 getInfo : function() {
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
63 _insertTemplate : function(ui, v) {
64 var t = this, ed = t.editor, h, el, dom = ed.dom, sel = ed.selection.getContent();
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);
73 el = dom.create('div', null, h);
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));
82 function hasClass(n, c) {
83 return new RegExp('\\b' + c + '\\b', 'g').test(n.className);
86 each(dom.select('*', el), function(n) {
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")));
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")));
96 if (hasClass(n, ed.getParam('template_selected_content_classes', 'selcontent').replace(/\s+/g, '|')))
102 ed.execCommand('mceInsertContent', false, el.innerHTML);
106 _replaceVals : function(e) {
107 var dom = this.editor.dom, vl = this.editor.getParam('template_replace_values');
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')
119 _getDateTime : function(d, fmt) {
123 function addZeros(value, len) {
128 if (value.length < len) {
129 for (i=0; i<(len-value.length); i++)
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("%%", "%");
158 tinymce.PluginManager.add('template', tinymce.plugins.TemplatePlugin);