]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/debian/missing-sources/framework/Web/Javascripts/source/tinymce-405/plugins/template/plugin.js
baculum: Add missing-sources directory in debian metadata structure
[bacula/bacula] / gui / baculum / debian / missing-sources / framework / Web / Javascripts / source / tinymce-405 / plugins / template / plugin.js
1 /**
2  * plugin.js
3  *
4  * Copyright, Moxiecode Systems AB
5  * Released under LGPL License.
6  *
7  * License: http://www.tinymce.com/license
8  * Contributing: http://www.tinymce.com/contributing
9  */
10
11 /*global tinymce:true */
12
13 tinymce.PluginManager.add('template', function(editor) {
14         var each = tinymce.each;
15
16         function showDialog() {
17                 var win, values = [], templateHtml;
18
19                 if (!editor.settings.templates) {
20                         editor.windowManager.alert('No templates defined');
21                         return;
22                 }
23
24                 tinymce.each(editor.settings.templates, function(template) {
25                         values.push({
26                                 selected: !values.length,
27                                 text: template.title,
28                                 value: {
29                                         url: template.url,
30                                         content: template.content,
31                                         description: template.description
32                                 }
33                         });
34                 });
35
36                 function onSelectTemplate(e) {
37                         var value = e.control.value();
38
39                         if (value.url) {
40                                 tinymce.util.XHR.send({
41                                         url: value.url,
42                                         success: function(html) {
43                                                 templateHtml = html;
44                                                 win.find('iframe')[0].html(html);
45                                         }
46                                 });
47                         } else {
48                                 templateHtml = value.content;
49                                 win.find('iframe')[0].html(value.content);
50                         }
51
52                         win.find('#description')[0].text(e.control.value().description);
53                 }
54
55                 win = editor.windowManager.open({
56                         title: 'Insert template',
57
58                         body: [
59                                 {type: 'container', label: 'Templates', items: {
60                                         type: 'listbox', name: 'template', values: values, onselect: onSelectTemplate
61                                 }},
62                                 {type: 'label', name: 'description', label: 'Description', text: '\u00a0'},
63                                 {type: 'iframe', minWidth: 600, minHeight: 400, border: 1}
64                         ],
65
66                         onsubmit: function() {
67                                 insertTemplate(false, templateHtml);
68                         }
69                 });
70
71                 win.find('listbox')[0].fire('select');
72         }
73
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(' ');
79
80                 function addZeros(value, len) {
81                         value = "" + value;
82
83                         if (value.length < len) {
84                                 for (var i = 0; i < (len - value.length); i++) {
85                                         value = "0" + value;
86                                 }
87                         }
88
89                         return value;
90                 }
91
92                 date = date || new Date();
93
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("%%", "%");
110
111                 return fmt;
112         }
113
114         function replaceVals(e) {
115                 var dom = editor.dom, vl = editor.getParam('template_replace_values');
116
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') {
121                                                 vl[k](e);
122                                         }
123                                 }
124                         });
125                 });
126         }
127
128         function insertTemplate(ui, html) {
129                 var el, n, dom = editor.dom, sel = editor.selection.getContent();
130
131                 each(editor.getParam('template_replace_values'), function(v, k) {
132                         if (typeof(v) != 'function') {
133                                 html = html.replace(new RegExp('\\{\\$' + k + '\\}', 'g'), v);
134                         }
135                 });
136
137                 el = dom.create('div', null, html);
138
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));
144                 }
145
146                 function hasClass(n, c) {
147                         return new RegExp('\\b' + c + '\\b', 'g').test(n.className);
148                 }
149
150                 each(dom.select('*', el), function(n) {
151                         // Replace cdate
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")));
154                         }
155
156                         // Replace mdate
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")));
159                         }
160
161                         // Replace selection
162                         if (hasClass(n, editor.getParam('template_selected_content_classes', 'selcontent').replace(/\s+/g, '|'))) {
163                                 n.innerHTML = sel;
164                         }
165                 });
166
167                 replaceVals(el);
168
169                 editor.execCommand('mceInsertContent', false, el.innerHTML);
170                 editor.addVisual();
171         }
172
173         editor.addCommand('mceInsertTemplate', insertTemplate);
174
175         editor.addButton('template', {
176                 title: 'Insert template',
177                 onclick: showDialog
178         });
179
180         editor.addMenuItem('template', {
181                 text: 'Insert template',
182                 onclick: showDialog,
183                 context: 'insert'
184         });
185
186         editor.on('PreProcess', function(o) {
187                 var dom = editor.dom;
188
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")));
194                                         }
195                                 });
196
197                                 replaceVals(e);
198                         }
199                 });
200         });
201 });