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