2 * editor_plugin_src.js
\r
4 * Copyright 2009, Moxiecode Systems AB
\r
5 * Released under LGPL License.
\r
7 * License: http://tinymce.moxiecode.com/license
\r
8 * Contributing: http://tinymce.moxiecode.com/contributing
\r
12 var each = tinymce.each;
\r
14 tinymce.create('tinymce.plugins.TemplatePlugin', {
\r
15 init : function(ed, url) {
\r
20 // Register commands
\r
21 ed.addCommand('mceTemplate', function(ui) {
\r
22 ed.windowManager.open({
\r
23 file : url + '/template.htm',
\r
24 width : ed.getParam('template_popup_width', 750),
\r
25 height : ed.getParam('template_popup_height', 600),
\r
32 ed.addCommand('mceInsertTemplate', t._insertTemplate, t);
\r
35 ed.addButton('template', {title : 'template.desc', cmd : 'mceTemplate'});
\r
37 ed.onPreProcess.add(function(ed, o) {
\r
40 each(dom.select('div', o.node), function(e) {
\r
41 if (dom.hasClass(e, 'mceTmpl')) {
\r
42 each(dom.select('*', e), function(e) {
\r
43 if (dom.hasClass(e, ed.getParam('template_mdate_classes', 'mdate').replace(/\s+/g, '|')))
\r
44 e.innerHTML = t._getDateTime(new Date(), ed.getParam("template_mdate_format", ed.getLang("template.mdate_format")));
\r
53 getInfo : function() {
\r
55 longname : 'Template plugin',
\r
56 author : 'Moxiecode Systems AB',
\r
57 authorurl : 'http://www.moxiecode.com',
\r
58 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/template',
\r
59 version : tinymce.majorVersion + "." + tinymce.minorVersion
\r
63 _insertTemplate : function(ui, v) {
\r
64 var t = this, ed = t.editor, h, el, dom = ed.dom, sel = ed.selection.getContent();
\r
68 each(t.editor.getParam('template_replace_values'), function(v, k) {
\r
69 if (typeof(v) != 'function')
\r
70 h = h.replace(new RegExp('\\{\\$' + k + '\\}', 'g'), v);
\r
73 el = dom.create('div', null, h);
\r
75 // Find template element within div
\r
76 n = dom.select('.mceTmpl', el);
\r
77 if (n && n.length > 0) {
\r
78 el = dom.create('div', null);
\r
79 el.appendChild(n[0].cloneNode(true));
\r
82 function hasClass(n, c) {
\r
83 return new RegExp('\\b' + c + '\\b', 'g').test(n.className);
\r
86 each(dom.select('*', el), function(n) {
\r
88 if (hasClass(n, ed.getParam('template_cdate_classes', 'cdate').replace(/\s+/g, '|')))
\r
89 n.innerHTML = t._getDateTime(new Date(), ed.getParam("template_cdate_format", ed.getLang("template.cdate_format")));
\r
92 if (hasClass(n, ed.getParam('template_mdate_classes', 'mdate').replace(/\s+/g, '|')))
\r
93 n.innerHTML = t._getDateTime(new Date(), ed.getParam("template_mdate_format", ed.getLang("template.mdate_format")));
\r
95 // Replace selection
\r
96 if (hasClass(n, ed.getParam('template_selected_content_classes', 'selcontent').replace(/\s+/g, '|')))
\r
100 t._replaceVals(el);
\r
102 ed.execCommand('mceInsertContent', false, el.innerHTML);
\r
106 _replaceVals : function(e) {
\r
107 var dom = this.editor.dom, vl = this.editor.getParam('template_replace_values');
\r
109 each(dom.select('*', e), function(e) {
\r
110 each(vl, function(v, k) {
\r
111 if (dom.hasClass(e, k)) {
\r
112 if (typeof(vl[k]) == 'function')
\r
119 _getDateTime : function(d, fmt) {
\r
123 function addZeros(value, len) {
\r
126 value = "" + value;
\r
128 if (value.length < len) {
\r
129 for (i=0; i<(len-value.length); i++)
\r
130 value = "0" + value;
\r
136 fmt = fmt.replace("%D", "%m/%d/%y");
\r
137 fmt = fmt.replace("%r", "%I:%M:%S %p");
\r
138 fmt = fmt.replace("%Y", "" + d.getFullYear());
\r
139 fmt = fmt.replace("%y", "" + d.getYear());
\r
140 fmt = fmt.replace("%m", addZeros(d.getMonth()+1, 2));
\r
141 fmt = fmt.replace("%d", addZeros(d.getDate(), 2));
\r
142 fmt = fmt.replace("%H", "" + addZeros(d.getHours(), 2));
\r
143 fmt = fmt.replace("%M", "" + addZeros(d.getMinutes(), 2));
\r
144 fmt = fmt.replace("%S", "" + addZeros(d.getSeconds(), 2));
\r
145 fmt = fmt.replace("%I", "" + ((d.getHours() + 11) % 12 + 1));
\r
146 fmt = fmt.replace("%p", "" + (d.getHours() < 12 ? "AM" : "PM"));
\r
147 fmt = fmt.replace("%B", "" + this.editor.getLang("template_months_long").split(',')[d.getMonth()]);
\r
148 fmt = fmt.replace("%b", "" + this.editor.getLang("template_months_short").split(',')[d.getMonth()]);
\r
149 fmt = fmt.replace("%A", "" + this.editor.getLang("template_day_long").split(',')[d.getDay()]);
\r
150 fmt = fmt.replace("%a", "" + this.editor.getLang("template_day_short").split(',')[d.getDay()]);
\r
151 fmt = fmt.replace("%%", "%");
\r
158 tinymce.PluginManager.add('template', tinymce.plugins.TemplatePlugin);
\r