4 * Copyright, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.com/contributing
11 /*global tinymce:true */
13 tinymce.PluginManager.add('template', function(editor) {
14 var each = tinymce.each;
16 function showDialog() {
17 var win, values = [], templateHtml;
19 if (!editor.settings.templates) {
20 editor.windowManager.alert('No templates defined');
24 tinymce.each(editor.settings.templates, function(template) {
26 selected: !values.length,
30 content: template.content,
31 description: template.description
36 function onSelectTemplate(e) {
37 var value = e.control.value();
40 tinymce.util.XHR.send({
42 success: function(html) {
44 win.find('iframe')[0].html(html);
48 templateHtml = value.content;
49 win.find('iframe')[0].html(value.content);
52 win.find('#description')[0].text(e.control.value().description);
55 win = editor.windowManager.open({
56 title: 'Insert template',
59 {type: 'container', label: 'Templates', items: {
60 type: 'listbox', name: 'template', values: values, onselect: onSelectTemplate
62 {type: 'label', name: 'description', label: 'Description', text: '\u00a0'},
63 {type: 'iframe', minWidth: 600, minHeight: 400, border: 1}
66 onsubmit: function() {
67 insertTemplate(false, templateHtml);
71 win.find('listbox')[0].fire('select');
74 function getDateTime(fmt, date) {
75 var daysShort = "Sun Mon Tue Wed Thu Fri Sat Sun".split(' ');
76 var daysLong = "Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday".split(' ');
77 var monthsShort = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(' ');
78 var monthsLong = "January February March April May June July August September October November December".split(' ');
80 function addZeros(value, len) {
83 if (value.length < len) {
84 for (var i = 0; i < (len - value.length); i++) {
92 date = date || new Date();
94 fmt = fmt.replace("%D", "%m/%d/%Y");
95 fmt = fmt.replace("%r", "%I:%M:%S %p");
96 fmt = fmt.replace("%Y", "" + date.getFullYear());
97 fmt = fmt.replace("%y", "" + date.getYear());
98 fmt = fmt.replace("%m", addZeros(date.getMonth() + 1, 2));
99 fmt = fmt.replace("%d", addZeros(date.getDate(), 2));
100 fmt = fmt.replace("%H", "" + addZeros(date.getHours(), 2));
101 fmt = fmt.replace("%M", "" + addZeros(date.getMinutes(), 2));
102 fmt = fmt.replace("%S", "" + addZeros(date.getSeconds(), 2));
103 fmt = fmt.replace("%I", "" + ((date.getHours() + 11) % 12 + 1));
104 fmt = fmt.replace("%p", "" + (date.getHours() < 12 ? "AM" : "PM"));
105 fmt = fmt.replace("%B", "" + editor.translate(monthsLong[date.getMonth()]));
106 fmt = fmt.replace("%b", "" + editor.translate(monthsShort[date.getMonth()]));
107 fmt = fmt.replace("%A", "" + editor.translate(daysLong[date.getDay()]));
108 fmt = fmt.replace("%a", "" + editor.translate(daysShort[date.getDay()]));
109 fmt = fmt.replace("%%", "%");
114 function replaceVals(e) {
115 var dom = editor.dom, vl = editor.getParam('template_replace_values');
117 each(dom.select('*', e), function(e) {
118 each(vl, function(v, k) {
119 if (dom.hasClass(e, k)) {
120 if (typeof(vl[k]) == 'function') {
128 function insertTemplate(ui, html) {
129 var el, n, dom = editor.dom, sel = editor.selection.getContent();
131 each(editor.getParam('template_replace_values'), function(v, k) {
132 if (typeof(v) != 'function') {
133 html = html.replace(new RegExp('\\{\\$' + k + '\\}', 'g'), v);
137 el = dom.create('div', null, html);
139 // Find template element within div
140 n = dom.select('.mceTmpl', el);
141 if (n && n.length > 0) {
142 el = dom.create('div', null);
143 el.appendChild(n[0].cloneNode(true));
146 function hasClass(n, c) {
147 return new RegExp('\\b' + c + '\\b', 'g').test(n.className);
150 each(dom.select('*', el), function(n) {
152 if (hasClass(n, editor.getParam('template_cdate_classes', 'cdate').replace(/\s+/g, '|'))) {
153 n.innerHTML = getDateTime(editor.getParam("template_cdate_format", editor.getLang("template.cdate_format")));
157 if (hasClass(n, editor.getParam('template_mdate_classes', 'mdate').replace(/\s+/g, '|'))) {
158 n.innerHTML = getDateTime(editor.getParam("template_mdate_format", editor.getLang("template.mdate_format")));
162 if (hasClass(n, editor.getParam('template_selected_content_classes', 'selcontent').replace(/\s+/g, '|'))) {
169 editor.execCommand('mceInsertContent', false, el.innerHTML);
173 editor.addCommand('mceInsertTemplate', insertTemplate);
175 editor.addButton('template', {
176 title: 'Insert template',
180 editor.addMenuItem('template', {
181 text: 'Insert template',
186 editor.on('PreProcess', function(o) {
187 var dom = editor.dom;
189 each(dom.select('div', o.node), function(e) {
190 if (dom.hasClass(e, 'mceTmpl')) {
191 each(dom.select('*', e), function(e) {
192 if (dom.hasClass(e, editor.getParam('template_mdate_classes', 'mdate').replace(/\s+/g, '|'))) {
193 e.innerHTML = getDateTime(editor.getParam("template_mdate_format", editor.getLang("template.mdate_format")));