Blame view

js/_tiny_mce/plugins/layer/editor_plugin_src.js 6.32 KB
42868d70   andryeyev   Создал GIT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
  /**
   * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
   *
   * @author Moxiecode
   * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
   */
  
  /* Import plugin specific language pack */
  tinyMCE.importPluginLanguagePack('layer');
  
  var TinyMCE_LayerPlugin = {
  	getInfo : function() {
  		return {
  			longname : 'Layer',
  			author : 'Moxiecode Systems AB',
  			authorurl : 'http://tinymce.moxiecode.com',
  			infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/layer',
  			version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
  		};
  	},
  
  	initInstance : function(inst) {
  		if (tinyMCE.isMSIE && !tinyMCE.isOpera)
  			inst.getDoc().execCommand('2D-Position');
  	},
  
  	handleEvent : function(e) {
  		var inst = tinyMCE.selectedInstance;
  		var w = inst.getWin(), le = inst._lastStyleElm, e;
  
  		if (tinyMCE.isGecko) {
  			e = this._getParentLayer(inst.getFocusElement());
  
  			if (e) {
  				if (!inst._lastStyleElm) {
  					e.style.overflow = 'auto';
  					inst._lastStyleElm = e;
  				}
  			} else if (le) {
  				le = inst._lastStyleElm;
  				le.style.width = le.scrollWidth + 'px';
  				le.style.height = le.scrollHeight + 'px';
  				le.style.overflow = '';
  				inst._lastStyleElm = null;
  			}
  		}
  
  		return true;
  	},
  
  	handleVisualAid : function(el, deep, state, inst) {
  		var nl = inst.getDoc().getElementsByTagName("div"), i;
  
  		for (i=0; i<nl.length; i++) {
  			if (new RegExp('absolute|relative|static', 'gi').test(nl[i].style.position)) {
  				if (state)
  					tinyMCE.addCSSClass(nl[i], 'mceVisualAid');
  				else
  					tinyMCE.removeCSSClass(nl[i], 'mceVisualAid');					
  			}
  		}
  	},
  
  	getControlHTML : function(cn) {
  		switch (cn) {
  			case "moveforward":
  				return tinyMCE.getButtonHTML(cn, 'lang_layer_forward_desc', '{$pluginurl}/images/moveforward.gif', 'mceMoveForward', true);
  
  			case "movebackward":
  				return tinyMCE.getButtonHTML(cn, 'lang_layer_backward_desc', '{$pluginurl}/images/movebackward.gif', 'mceMoveBackward', true);
  
  			case "absolute":
  				return tinyMCE.getButtonHTML(cn, 'lang_layer_absolute_desc', '{$pluginurl}/images/absolute.gif', 'mceMakeAbsolute', true);
  
  			case "insertlayer":
  				return tinyMCE.getButtonHTML(cn, 'lang_layer_insertlayer_desc', '{$pluginurl}/images/insertlayer.gif', 'mceInsertLayer', true);
  		}
  
  		return "";
  	},
  
  	execCommand : function(editor_id, element, command, user_interface, value) {
  		// Handle commands
  		switch (command) {
  			case "mceInsertLayer":
  				this._insertLayer();
  				return true;
  
  			case "mceMoveForward":
  				this._move(1);
  				return true;
  
  			case "mceMoveBackward":
  				this._move(-1);
  				return true;
  
  			case "mceMakeAbsolute":
  				this._toggleAbsolute();
  				return true;
  		}
  
  		// Pass to next handler in chain
  		return false;
  	},
  
  	handleNodeChange : function(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
  		var inst = tinyMCE.getInstanceById(editor_id);
  		var le = this._getParentLayer(inst.getFocusElement());
  		var p = tinyMCE.getParentElement(inst.getFocusElement(), 'div,p,img');
  
  		tinyMCE.switchClass(editor_id + '_absolute', 'mceButtonDisabled');
  		tinyMCE.switchClass(editor_id + '_moveforward', 'mceButtonDisabled');
  		tinyMCE.switchClass(editor_id + '_movebackward', 'mceButtonDisabled');
  
  		if (p)
  			tinyMCE.switchClass(editor_id + '_absolute', 'mceButtonNormal');
  
  		if (le && le.style.position.toLowerCase() == "absolute") {
  			tinyMCE.switchClass(editor_id + '_absolute', 'mceButtonSelected');
  			tinyMCE.switchClass(editor_id + '_moveforward', 'mceButtonNormal');
  			tinyMCE.switchClass(editor_id + '_movebackward', 'mceButtonNormal');
  		}
  	},
  
  	// Private plugin specific methods
  
  	_move : function(d) {
  		var inst = tinyMCE.selectedInstance, i, z = new Array();
  		var le = this._getParentLayer(inst.getFocusElement()), ci = -1, fi = -1;
  		var nl = tinyMCE.selectNodes(inst.getBody(), function(n) {
  			return n.nodeType == 1 && new RegExp('absolute|relative|static', 'gi').test(n.style.position);
  		});
  
  		// Find z-indexes
  		for (i=0; i<nl.length; i++) {
  			z[i] = nl[i].style.zIndex ? parseInt(nl[i].style.zIndex) : 0;
  
  			if (ci < 0 && nl[i] == le)
  				ci = i;
  		}
  
  		if (d < 0) {
  			// Move back
  
  			// Try find a lower one
  			for (i=0; i<z.length; i++) {
  				if (z[i] < z[ci]) {
  					fi = i;
  					break;
  				}
  			}
  
  			if (fi > -1) {
  				nl[ci].style.zIndex = z[fi];
  				nl[fi].style.zIndex = z[ci];
  			} else {
  				if (z[ci] > 0)
  					nl[ci].style.zIndex = z[ci] - 1;
  			}
  		} else {
  			// Move forward
  
  			// Try find a higher one
  			for (i=0; i<z.length; i++) {
  				if (z[i] > z[ci]) {
  					fi = i;
  					break;
  				}
  			}
  
  			if (fi > -1) {
  				nl[ci].style.zIndex = z[fi];
  				nl[fi].style.zIndex = z[ci];
  			} else
  				nl[ci].style.zIndex = z[ci] + 1;
  		}
  
  		inst.repaint();
  	},
  
  	_getParentLayer : function(n) {
  		return tinyMCE.getParentNode(n, function(n) {
  			return n.nodeType == 1 && new RegExp('absolute|relative|static', 'gi').test(n.style.position);
  		});
  	},
  
  	_insertLayer : function() {
  		var inst = tinyMCE.selectedInstance;
  		var e = tinyMCE.getParentElement(inst.getFocusElement());
  		var p = tinyMCE.getAbsPosition(e);
  		var d = inst.getDoc();
  		var ne = d.createElement('div');
  		var h = inst.selection.getSelectedHTML();
  
  		// Move div
  		ne.style.position = 'absolute';
  		ne.style.left = p.absLeft + 'px';
  		ne.style.top = (p.absTop > 20 ? p.absTop : 20) + 'px';
  		ne.style.width = '100px';
  		ne.style.height = '100px';
  		ne.className = 'mceVisualAid';
  
  		if (!h)
  			h = tinyMCE.getLang('lang_layer_content');
  
  		ne.innerHTML = h;
  
  		// Add it
  		d.body.appendChild(ne);
  	},
  
  	_toggleAbsolute : function() {
  		var inst = tinyMCE.selectedInstance;
  		var le = this._getParentLayer(inst.getFocusElement());
  
  		if (le == null)
  			le = tinyMCE.getParentElement(inst.getFocusElement(), 'div,p,img');
  
  		if (le) {
  			if (le.style.position.toLowerCase() == "absolute") {
  				le.style.position = "";
  				le.style.left = "";
  				le.style.top = "";
  			} else {
  				le.style.position = "absolute";
  
  				if (le.style.left == "")
  					le.style.left = 20 + 'px';
  
  				if (le.style.top == "")
  					le.style.top = 20 + 'px';
  
  				if (le.style.width == "")
  					le.style.width = le.width ? (le.width + 'px') : '100px';
  
  				if (le.style.height == "")
  					le.style.height = le.height ? (le.height + 'px') : '100px';
  
  				tinyMCE.handleVisualAid(inst.getBody(), true, inst.visualAid, inst);
  			}
  
  			inst.repaint();
  			tinyMCE.triggerNodeChange();
  		}
  	}
  };
  
  tinyMCE.addPlugin("layer", TinyMCE_LayerPlugin);