nouveau style menu_top
[mtweb] / mw / libs / tiny_mce / plugins / layer / 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         tinymce.create('tinymce.plugins.Layer', {\r
13                 init : function(ed, url) {\r
14                         var t = this;\r
15 \r
16                         t.editor = ed;\r
17 \r
18                         // Register commands\r
19                         ed.addCommand('mceInsertLayer', t._insertLayer, t);\r
20 \r
21                         ed.addCommand('mceMoveForward', function() {\r
22                                 t._move(1);\r
23                         });\r
24 \r
25                         ed.addCommand('mceMoveBackward', function() {\r
26                                 t._move(-1);\r
27                         });\r
28 \r
29                         ed.addCommand('mceMakeAbsolute', function() {\r
30                                 t._toggleAbsolute();\r
31                         });\r
32 \r
33                         // Register buttons\r
34                         ed.addButton('moveforward', {title : 'layer.forward_desc', cmd : 'mceMoveForward'});\r
35                         ed.addButton('movebackward', {title : 'layer.backward_desc', cmd : 'mceMoveBackward'});\r
36                         ed.addButton('absolute', {title : 'layer.absolute_desc', cmd : 'mceMakeAbsolute'});\r
37                         ed.addButton('insertlayer', {title : 'layer.insertlayer_desc', cmd : 'mceInsertLayer'});\r
38 \r
39                         ed.onInit.add(function() {\r
40                                 if (tinymce.isIE)\r
41                                         ed.getDoc().execCommand('2D-Position', false, true);\r
42                         });\r
43 \r
44                         ed.onNodeChange.add(t._nodeChange, t);\r
45                         ed.onVisualAid.add(t._visualAid, t);\r
46                 },\r
47 \r
48                 getInfo : function() {\r
49                         return {\r
50                                 longname : 'Layer',\r
51                                 author : 'Moxiecode Systems AB',\r
52                                 authorurl : 'http://tinymce.moxiecode.com',\r
53                                 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/layer',\r
54                                 version : tinymce.majorVersion + "." + tinymce.minorVersion\r
55                         };\r
56                 },\r
57 \r
58                 // Private methods\r
59 \r
60                 _nodeChange : function(ed, cm, n) {\r
61                         var le, p;\r
62 \r
63                         le = this._getParentLayer(n);\r
64                         p = ed.dom.getParent(n, 'DIV,P,IMG');\r
65 \r
66                         if (!p) {\r
67                                 cm.setDisabled('absolute', 1);\r
68                                 cm.setDisabled('moveforward', 1);\r
69                                 cm.setDisabled('movebackward', 1);\r
70                         } else {\r
71                                 cm.setDisabled('absolute', 0);\r
72                                 cm.setDisabled('moveforward', !le);\r
73                                 cm.setDisabled('movebackward', !le);\r
74                                 cm.setActive('absolute', le && le.style.position.toLowerCase() == "absolute");\r
75                         }\r
76                 },\r
77 \r
78                 // Private methods\r
79 \r
80                 _visualAid : function(ed, e, s) {\r
81                         var dom = ed.dom;\r
82 \r
83                         tinymce.each(dom.select('div,p', e), function(e) {\r
84                                 if (/^(absolute|relative|static)$/i.test(e.style.position)) {\r
85                                         if (s)\r
86                                                 dom.addClass(e, 'mceItemVisualAid');\r
87                                         else\r
88                                                 dom.removeClass(e, 'mceItemVisualAid'); \r
89                                 }\r
90                         });\r
91                 },\r
92 \r
93                 _move : function(d) {\r
94                         var ed = this.editor, i, z = [], le = this._getParentLayer(ed.selection.getNode()), ci = -1, fi = -1, nl;\r
95 \r
96                         nl = [];\r
97                         tinymce.walk(ed.getBody(), function(n) {\r
98                                 if (n.nodeType == 1 && /^(absolute|relative|static)$/i.test(n.style.position))\r
99                                         nl.push(n); \r
100                         }, 'childNodes');\r
101 \r
102                         // Find z-indexes\r
103                         for (i=0; i<nl.length; i++) {\r
104                                 z[i] = nl[i].style.zIndex ? parseInt(nl[i].style.zIndex) : 0;\r
105 \r
106                                 if (ci < 0 && nl[i] == le)\r
107                                         ci = i;\r
108                         }\r
109 \r
110                         if (d < 0) {\r
111                                 // Move back\r
112 \r
113                                 // Try find a lower one\r
114                                 for (i=0; i<z.length; i++) {\r
115                                         if (z[i] < z[ci]) {\r
116                                                 fi = i;\r
117                                                 break;\r
118                                         }\r
119                                 }\r
120 \r
121                                 if (fi > -1) {\r
122                                         nl[ci].style.zIndex = z[fi];\r
123                                         nl[fi].style.zIndex = z[ci];\r
124                                 } else {\r
125                                         if (z[ci] > 0)\r
126                                                 nl[ci].style.zIndex = z[ci] - 1;\r
127                                 }\r
128                         } else {\r
129                                 // Move forward\r
130 \r
131                                 // Try find a higher one\r
132                                 for (i=0; i<z.length; i++) {\r
133                                         if (z[i] > z[ci]) {\r
134                                                 fi = i;\r
135                                                 break;\r
136                                         }\r
137                                 }\r
138 \r
139                                 if (fi > -1) {\r
140                                         nl[ci].style.zIndex = z[fi];\r
141                                         nl[fi].style.zIndex = z[ci];\r
142                                 } else\r
143                                         nl[ci].style.zIndex = z[ci] + 1;\r
144                         }\r
145 \r
146                         ed.execCommand('mceRepaint');\r
147                 },\r
148 \r
149                 _getParentLayer : function(n) {\r
150                         return this.editor.dom.getParent(n, function(n) {\r
151                                 return n.nodeType == 1 && /^(absolute|relative|static)$/i.test(n.style.position);\r
152                         });\r
153                 },\r
154 \r
155                 _insertLayer : function() {\r
156                         var ed = this.editor, p = ed.dom.getPos(ed.dom.getParent(ed.selection.getNode(), '*'));\r
157 \r
158                         ed.dom.add(ed.getBody(), 'div', {\r
159                                 style : {\r
160                                         position : 'absolute',\r
161                                         left : p.x,\r
162                                         top : (p.y > 20 ? p.y : 20),\r
163                                         width : 100,\r
164                                         height : 100\r
165                                 },\r
166                                 'class' : 'mceItemVisualAid'\r
167                         }, ed.selection.getContent() || ed.getLang('layer.content'));\r
168                 },\r
169 \r
170                 _toggleAbsolute : function() {\r
171                         var ed = this.editor, le = this._getParentLayer(ed.selection.getNode());\r
172 \r
173                         if (!le)\r
174                                 le = ed.dom.getParent(ed.selection.getNode(), 'DIV,P,IMG');\r
175 \r
176                         if (le) {\r
177                                 if (le.style.position.toLowerCase() == "absolute") {\r
178                                         ed.dom.setStyles(le, {\r
179                                                 position : '',\r
180                                                 left : '',\r
181                                                 top : '',\r
182                                                 width : '',\r
183                                                 height : ''\r
184                                         });\r
185 \r
186                                         ed.dom.removeClass(le, 'mceItemVisualAid');\r
187                                 } else {\r
188                                         if (le.style.left == "")\r
189                                                 le.style.left = 20 + 'px';\r
190 \r
191                                         if (le.style.top == "")\r
192                                                 le.style.top = 20 + 'px';\r
193 \r
194                                         if (le.style.width == "")\r
195                                                 le.style.width = le.width ? (le.width + 'px') : '100px';\r
196 \r
197                                         if (le.style.height == "")\r
198                                                 le.style.height = le.height ? (le.height + 'px') : '100px';\r
199 \r
200                                         le.style.position = "absolute";\r
201                                         ed.addVisual(ed.getBody());\r
202                                 }\r
203 \r
204                                 ed.execCommand('mceRepaint');\r
205                                 ed.nodeChanged();\r
206                         }\r
207                 }\r
208         });\r
209 \r
210         // Register plugin\r
211         tinymce.PluginManager.add('layer', tinymce.plugins.Layer);\r
212 })();