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