PageRenderTime 198ms CodeModel.GetById 141ms app.highlight 40ms RepoModel.GetById 10ms app.codeStats 1ms

/static/scripts/tiny_mce/plugins/table/js/table.js

http://n23.googlecode.com/
JavaScript | 410 lines | 295 code | 83 blank | 32 comment | 72 complexity | 427314b4196fb4c485c1c7fb266b5226 MD5 | raw file
  1tinyMCEPopup.requireLangPack();
  2
  3var action, orgTableWidth, orgTableHeight, dom = tinyMCEPopup.editor.dom;
  4
  5function insertTable() {
  6	var formObj = document.forms[0];
  7	var inst = tinyMCEPopup.editor, dom = inst.dom;
  8	var cols = 2, rows = 2, border = 0, cellpadding = -1, cellspacing = -1, align, width, height, className, caption, frame, rules;
  9	var html = '', capEl, elm;
 10	var cellLimit, rowLimit, colLimit;
 11
 12	tinyMCEPopup.restoreSelection();
 13
 14	if (!AutoValidator.validate(formObj)) {
 15		alert(inst.getLang('invalid_data'));
 16		return false;
 17	}
 18
 19	elm = dom.getParent(inst.selection.getNode(), 'table');
 20
 21	// Get form data
 22	cols = formObj.elements['cols'].value;
 23	rows = formObj.elements['rows'].value;
 24	border = formObj.elements['border'].value != "" ? formObj.elements['border'].value  : 0;
 25	cellpadding = formObj.elements['cellpadding'].value != "" ? formObj.elements['cellpadding'].value : "";
 26	cellspacing = formObj.elements['cellspacing'].value != "" ? formObj.elements['cellspacing'].value : "";
 27	align = formObj.elements['align'].options[formObj.elements['align'].selectedIndex].value;
 28	frame = formObj.elements['frame'].options[formObj.elements['frame'].selectedIndex].value;
 29	rules = formObj.elements['rules'].options[formObj.elements['rules'].selectedIndex].value;
 30	width = formObj.elements['width'].value;
 31	height = formObj.elements['height'].value;
 32	bordercolor = formObj.elements['bordercolor'].value;
 33	bgcolor = formObj.elements['bgcolor'].value;
 34	className = formObj.elements['class'].options[formObj.elements['class'].selectedIndex].value;
 35	id = formObj.elements['id'].value;
 36	summary = formObj.elements['summary'].value;
 37	style = formObj.elements['style'].value;
 38	dir = formObj.elements['dir'].value;
 39	lang = formObj.elements['lang'].value;
 40	background = formObj.elements['backgroundimage'].value;
 41	caption = formObj.elements['caption'].checked;
 42
 43	cellLimit = tinyMCEPopup.getParam('table_cell_limit', false);
 44	rowLimit = tinyMCEPopup.getParam('table_row_limit', false);
 45	colLimit = tinyMCEPopup.getParam('table_col_limit', false);
 46
 47	// Validate table size
 48	if (colLimit && cols > colLimit) {
 49		alert(inst.getLang('table_col_limit', '', true, {cols : colLimit}));
 50		return false;
 51	} else if (rowLimit && rows > rowLimit) {
 52		alert(inst.getLang('table_row_limit', '', true, {rows : rowLimit}));
 53		return false;
 54	} else if (cellLimit && cols * rows > cellLimit) {
 55		alert(inst.getLang('table_cell_limit', '', true, {cells : cellLimit}));
 56		return false;
 57	}
 58
 59	// Update table
 60	if (action == "update") {
 61		inst.execCommand('mceBeginUndoLevel');
 62
 63		dom.setAttrib(elm, 'cellPadding', cellpadding, true);
 64		dom.setAttrib(elm, 'cellSpacing', cellspacing, true);
 65		dom.setAttrib(elm, 'border', border);
 66		dom.setAttrib(elm, 'align', align);
 67		dom.setAttrib(elm, 'frame', frame);
 68		dom.setAttrib(elm, 'rules', rules);
 69		dom.setAttrib(elm, 'class', className);
 70		dom.setAttrib(elm, 'style', style);
 71		dom.setAttrib(elm, 'id', id);
 72		dom.setAttrib(elm, 'summary', summary);
 73		dom.setAttrib(elm, 'dir', dir);
 74		dom.setAttrib(elm, 'lang', lang);
 75
 76		capEl = inst.dom.select('caption', elm)[0];
 77
 78		if (capEl && !caption)
 79			capEl.parentNode.removeChild(capEl);
 80
 81		if (!capEl && caption) {
 82			capEl = elm.ownerDocument.createElement('caption');
 83
 84			if (!tinymce.isIE)
 85				capEl.innerHTML = '<br mce_bogus="1"/>';
 86
 87			elm.insertBefore(capEl, elm.firstChild);
 88		}
 89
 90		if (width && /(pt|em|cm)$/.test(width)) {
 91			dom.setStyle(elm, 'width', width);
 92			dom.setAttrib(elm, 'width', '');
 93		} else {
 94			dom.setAttrib(elm, 'width', width, true);
 95			dom.setStyle(elm, 'width', '');
 96		}
 97
 98		// Remove these since they are not valid XHTML
 99		dom.setAttrib(elm, 'borderColor', '');
100		dom.setAttrib(elm, 'bgColor', '');
101		dom.setAttrib(elm, 'background', '');
102
103		if (height) {
104			dom.setStyle(elm, 'height', height);
105			dom.setAttrib(elm, 'height', '');
106		}
107
108		if (background != '')
109			elm.style.backgroundImage = "url('" + background + "')";
110		else
111			elm.style.backgroundImage = '';
112
113/*		if (tinyMCEPopup.getParam("inline_styles")) {
114			if (width != '')
115				elm.style.width = getCSSSize(width);
116		}*/
117
118		if (bordercolor != "") {
119			elm.style.borderColor = bordercolor;
120			elm.style.borderStyle = elm.style.borderStyle == "" ? "solid" : elm.style.borderStyle;
121			elm.style.borderWidth = border == "" ? "1px" : border;
122		} else
123			elm.style.borderColor = '';
124
125		elm.style.backgroundColor = bgcolor;
126		elm.style.height = getCSSSize(height);
127
128		inst.addVisual();
129
130		// Fix for stange MSIE align bug
131		//elm.outerHTML = elm.outerHTML;
132
133		inst.nodeChanged();
134		inst.execCommand('mceEndUndoLevel');
135
136		// Repaint if dimensions changed
137		if (formObj.width.value != orgTableWidth || formObj.height.value != orgTableHeight)
138			inst.execCommand('mceRepaint');
139
140		tinyMCEPopup.close();
141		return true;
142	}
143
144	// Create new table
145	html += '<table';
146
147	html += makeAttrib('id', id);
148	html += makeAttrib('border', border);
149	html += makeAttrib('cellpadding', cellpadding);
150	html += makeAttrib('cellspacing', cellspacing);
151
152	if (width && /(pt|em|cm)$/.test(width)) {
153		if (style)
154			style += '; ';
155
156		style += 'width: ' + width;
157	} else
158		html += makeAttrib('width', width);
159
160/*	if (height) {
161		if (style)
162			style += '; ';
163
164		style += 'height: ' + height;
165	}*/
166
167	//html += makeAttrib('height', height);
168	//html += makeAttrib('bordercolor', bordercolor);
169	//html += makeAttrib('bgcolor', bgcolor);
170	html += makeAttrib('align', align);
171	html += makeAttrib('frame', frame);
172	html += makeAttrib('rules', rules);
173	html += makeAttrib('class', className);
174	html += makeAttrib('style', style);
175	html += makeAttrib('summary', summary);
176	html += makeAttrib('dir', dir);
177	html += makeAttrib('lang', lang);
178	html += '>';
179
180	if (caption) {
181		if (!tinymce.isIE)
182			html += '<caption><br mce_bogus="1"/></caption>';
183		else
184			html += '<caption></caption>';
185	}
186
187	for (var y=0; y<rows; y++) {
188		html += "<tr>";
189
190		for (var x=0; x<cols; x++) {
191			if (!tinymce.isIE)
192				html += '<td><br mce_bogus="1"/></td>';
193			else
194				html += '<td></td>';
195		}
196
197		html += "</tr>";
198	}
199
200	html += "</table>";
201
202	inst.execCommand('mceBeginUndoLevel');
203	inst.execCommand('mceInsertContent', false, html);
204	inst.addVisual();
205	inst.execCommand('mceEndUndoLevel');
206
207	tinyMCEPopup.close();
208}
209
210function makeAttrib(attrib, value) {
211	var formObj = document.forms[0];
212	var valueElm = formObj.elements[attrib];
213
214	if (typeof(value) == "undefined" || value == null) {
215		value = "";
216
217		if (valueElm)
218			value = valueElm.value;
219	}
220
221	if (value == "")
222		return "";
223
224	// XML encode it
225	value = value.replace(/&/g, '&amp;');
226	value = value.replace(/\"/g, '&quot;');
227	value = value.replace(/</g, '&lt;');
228	value = value.replace(/>/g, '&gt;');
229
230	return ' ' + attrib + '="' + value + '"';
231}
232
233function init() {
234	tinyMCEPopup.resizeToInnerSize();
235
236	document.getElementById('backgroundimagebrowsercontainer').innerHTML = getBrowserHTML('backgroundimagebrowser','backgroundimage','image','table');
237	document.getElementById('backgroundimagebrowsercontainer').innerHTML = getBrowserHTML('backgroundimagebrowser','backgroundimage','image','table');
238	document.getElementById('bordercolor_pickcontainer').innerHTML = getColorPickerHTML('bordercolor_pick','bordercolor');
239	document.getElementById('bgcolor_pickcontainer').innerHTML = getColorPickerHTML('bgcolor_pick','bgcolor');
240
241	var cols = 2, rows = 2, border = tinyMCEPopup.getParam('table_default_border', '0'), cellpadding = tinyMCEPopup.getParam('table_default_cellpadding', ''), cellspacing = tinyMCEPopup.getParam('table_default_cellspacing', '');
242	var align = "", width = "", height = "", bordercolor = "", bgcolor = "", className = "";
243	var id = "", summary = "", style = "", dir = "", lang = "", background = "", bgcolor = "", bordercolor = "", rules, frame;
244	var inst = tinyMCEPopup.editor, dom = inst.dom;
245	var formObj = document.forms[0];
246	var elm = dom.getParent(inst.selection.getNode(), "table");
247
248	action = tinyMCEPopup.getWindowArg('action');
249
250	if (!action)
251		action = elm ? "update" : "insert";
252
253	if (elm && action != "insert") {
254		var rowsAr = elm.rows;
255		var cols = 0;
256		for (var i=0; i<rowsAr.length; i++)
257			if (rowsAr[i].cells.length > cols)
258				cols = rowsAr[i].cells.length;
259
260		cols = cols;
261		rows = rowsAr.length;
262
263		st = dom.parseStyle(dom.getAttrib(elm, "style"));
264		border = trimSize(getStyle(elm, 'border', 'borderWidth'));
265		cellpadding = dom.getAttrib(elm, 'cellpadding', "");
266		cellspacing = dom.getAttrib(elm, 'cellspacing', "");
267		width = trimSize(getStyle(elm, 'width', 'width'));
268		height = trimSize(getStyle(elm, 'height', 'height'));
269		bordercolor = convertRGBToHex(getStyle(elm, 'bordercolor', 'borderLeftColor'));
270		bgcolor = convertRGBToHex(getStyle(elm, 'bgcolor', 'backgroundColor'));
271		align = dom.getAttrib(elm, 'align', align);
272		frame = dom.getAttrib(elm, 'frame');
273		rules = dom.getAttrib(elm, 'rules');
274		className = tinymce.trim(dom.getAttrib(elm, 'class').replace(/mceItem.+/g, ''));
275		id = dom.getAttrib(elm, 'id');
276		summary = dom.getAttrib(elm, 'summary');
277		style = dom.serializeStyle(st);
278		dir = dom.getAttrib(elm, 'dir');
279		lang = dom.getAttrib(elm, 'lang');
280		background = getStyle(elm, 'background', 'backgroundImage').replace(new RegExp("url\\('?([^']*)'?\\)", 'gi'), "$1");
281		formObj.caption.checked = elm.getElementsByTagName('caption').length > 0;
282
283		orgTableWidth = width;
284		orgTableHeight = height;
285
286		action = "update";
287		formObj.insert.value = inst.getLang('update');
288	}
289
290	addClassesToList('class', "table_styles");
291	TinyMCE_EditableSelects.init();
292
293	// Update form
294	selectByValue(formObj, 'align', align);
295	selectByValue(formObj, 'frame', frame);
296	selectByValue(formObj, 'rules', rules);
297	selectByValue(formObj, 'class', className, true, true);
298	formObj.cols.value = cols;
299	formObj.rows.value = rows;
300	formObj.border.value = border;
301	formObj.cellpadding.value = cellpadding;
302	formObj.cellspacing.value = cellspacing;
303	formObj.width.value = width;
304	formObj.height.value = height;
305	formObj.bordercolor.value = bordercolor;
306	formObj.bgcolor.value = bgcolor;
307	formObj.id.value = id;
308	formObj.summary.value = summary;
309	formObj.style.value = style;
310	formObj.dir.value = dir;
311	formObj.lang.value = lang;
312	formObj.backgroundimage.value = background;
313
314	updateColor('bordercolor_pick', 'bordercolor');
315	updateColor('bgcolor_pick', 'bgcolor');
316
317	// Resize some elements
318	if (isVisible('backgroundimagebrowser'))
319		document.getElementById('backgroundimage').style.width = '180px';
320
321	// Disable some fields in update mode
322	if (action == "update") {
323		formObj.cols.disabled = true;
324		formObj.rows.disabled = true;
325	}
326}
327
328function changedSize() {
329	var formObj = document.forms[0];
330	var st = dom.parseStyle(formObj.style.value);
331
332/*	var width = formObj.width.value;
333	if (width != "")
334		st['width'] = tinyMCEPopup.getParam("inline_styles") ? getCSSSize(width) : "";
335	else
336		st['width'] = "";*/
337
338	var height = formObj.height.value;
339	if (height != "")
340		st['height'] = getCSSSize(height);
341	else
342		st['height'] = "";
343
344	formObj.style.value = dom.serializeStyle(st);
345}
346
347function changedBackgroundImage() {
348	var formObj = document.forms[0];
349	var st = dom.parseStyle(formObj.style.value);
350
351	st['background-image'] = "url('" + formObj.backgroundimage.value + "')";
352
353	formObj.style.value = dom.serializeStyle(st);
354}
355
356function changedBorder() {
357	var formObj = document.forms[0];
358	var st = dom.parseStyle(formObj.style.value);
359
360	// Update border width if the element has a color
361	if (formObj.border.value != "" && formObj.bordercolor.value != "")
362		st['border-width'] = formObj.border.value + "px";
363
364	formObj.style.value = dom.serializeStyle(st);
365}
366
367function changedColor() {
368	var formObj = document.forms[0];
369	var st = dom.parseStyle(formObj.style.value);
370
371	st['background-color'] = formObj.bgcolor.value;
372
373	if (formObj.bordercolor.value != "") {
374		st['border-color'] = formObj.bordercolor.value;
375
376		// Add border-width if it's missing
377		if (!st['border-width'])
378			st['border-width'] = formObj.border.value == "" ? "1px" : formObj.border.value + "px";
379	}
380
381	formObj.style.value = dom.serializeStyle(st);
382}
383
384function changedStyle() {
385	var formObj = document.forms[0];
386	var st = dom.parseStyle(formObj.style.value);
387
388	if (st['background-image'])
389		formObj.backgroundimage.value = st['background-image'].replace(new RegExp("url\\('?([^']*)'?\\)", 'gi'), "$1");
390	else
391		formObj.backgroundimage.value = '';
392
393	if (st['width'])
394		formObj.width.value = trimSize(st['width']);
395
396	if (st['height'])
397		formObj.height.value = trimSize(st['height']);
398
399	if (st['background-color']) {
400		formObj.bgcolor.value = st['background-color'];
401		updateColor('bgcolor_pick','bgcolor');
402	}
403
404	if (st['border-color']) {
405		formObj.bordercolor.value = st['border-color'];
406		updateColor('bordercolor_pick','bordercolor');
407	}
408}
409
410tinyMCEPopup.onInit.add(init);