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