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