PageRenderTime 387ms CodeModel.GetById 70ms app.highlight 249ms RepoModel.GetById 54ms app.codeStats 1ms

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

http://n23.googlecode.com/
JavaScript | 259 lines | 195 code | 58 blank | 6 comment | 47 complexity | 77573fb1f34efa6612ec52c565d7a4f1 MD5 | raw file
  1tinyMCEPopup.requireLangPack();
  2
  3var ed;
  4
  5function init() {
  6	ed = tinyMCEPopup.editor;
  7	tinyMCEPopup.resizeToInnerSize();
  8
  9	document.getElementById('backgroundimagebrowsercontainer').innerHTML = getBrowserHTML('backgroundimagebrowser','backgroundimage','image','table');
 10	document.getElementById('bordercolor_pickcontainer').innerHTML = getColorPickerHTML('bordercolor_pick','bordercolor');
 11	document.getElementById('bgcolor_pickcontainer').innerHTML = getColorPickerHTML('bgcolor_pick','bgcolor')
 12
 13	var inst = ed;
 14	var tdElm = ed.dom.getParent(ed.selection.getNode(), "td,th");
 15	var formObj = document.forms[0];
 16	var st = ed.dom.parseStyle(ed.dom.getAttrib(tdElm, "style"));
 17
 18	// Get table cell data
 19	var celltype = tdElm.nodeName.toLowerCase();
 20	var align = ed.dom.getAttrib(tdElm, 'align');
 21	var valign = ed.dom.getAttrib(tdElm, 'valign');
 22	var width = trimSize(getStyle(tdElm, 'width', 'width'));
 23	var height = trimSize(getStyle(tdElm, 'height', 'height'));
 24	var bordercolor = convertRGBToHex(getStyle(tdElm, 'bordercolor', 'borderLeftColor'));
 25	var bgcolor = convertRGBToHex(getStyle(tdElm, 'bgcolor', 'backgroundColor'));
 26	var className = ed.dom.getAttrib(tdElm, 'class');
 27	var backgroundimage = getStyle(tdElm, 'background', 'backgroundImage').replace(new RegExp("url\\('?([^']*)'?\\)", 'gi'), "$1");;
 28	var id = ed.dom.getAttrib(tdElm, 'id');
 29	var lang = ed.dom.getAttrib(tdElm, 'lang');
 30	var dir = ed.dom.getAttrib(tdElm, 'dir');
 31	var scope = ed.dom.getAttrib(tdElm, 'scope');
 32
 33	// Setup form
 34	addClassesToList('class', 'table_cell_styles');
 35	TinyMCE_EditableSelects.init();
 36
 37	formObj.bordercolor.value = bordercolor;
 38	formObj.bgcolor.value = bgcolor;
 39	formObj.backgroundimage.value = backgroundimage;
 40	formObj.width.value = width;
 41	formObj.height.value = height;
 42	formObj.id.value = id;
 43	formObj.lang.value = lang;
 44	formObj.style.value = ed.dom.serializeStyle(st);
 45	selectByValue(formObj, 'align', align);
 46	selectByValue(formObj, 'valign', valign);
 47	selectByValue(formObj, 'class', className, true, true);
 48	selectByValue(formObj, 'celltype', celltype);
 49	selectByValue(formObj, 'dir', dir);
 50	selectByValue(formObj, 'scope', scope);
 51
 52	// Resize some elements
 53	if (isVisible('backgroundimagebrowser'))
 54		document.getElementById('backgroundimage').style.width = '180px';
 55
 56	updateColor('bordercolor_pick', 'bordercolor');
 57	updateColor('bgcolor_pick', 'bgcolor');
 58}
 59
 60function updateAction() {
 61	var el, inst = ed, tdElm, trElm, tableElm, formObj = document.forms[0];
 62
 63	tinyMCEPopup.restoreSelection();
 64	el = ed.selection.getNode();
 65	tdElm = ed.dom.getParent(el, "td,th");
 66	trElm = ed.dom.getParent(el, "tr");
 67	tableElm = ed.dom.getParent(el, "table");
 68
 69	ed.execCommand('mceBeginUndoLevel');
 70
 71	switch (getSelectValue(formObj, 'action')) {
 72		case "cell":
 73			var celltype = getSelectValue(formObj, 'celltype');
 74			var scope = getSelectValue(formObj, 'scope');
 75
 76			if (ed.getParam("accessibility_warnings")) {
 77				if (celltype == "th" && scope == "")
 78					var answer = confirm(ed.getLang('table_dlg.missing_scope', '', true));
 79				else
 80					var answer = true;
 81
 82				if (!answer)
 83					return;
 84			}
 85
 86			updateCell(tdElm);
 87			break;
 88
 89		case "row":
 90			var cell = trElm.firstChild;
 91
 92			if (cell.nodeName != "TD" && cell.nodeName != "TH")
 93				cell = nextCell(cell);
 94
 95			do {
 96				cell = updateCell(cell, true);
 97			} while ((cell = nextCell(cell)) != null);
 98
 99			break;
100
101		case "all":
102			var rows = tableElm.getElementsByTagName("tr");
103
104			for (var i=0; i<rows.length; i++) {
105				var cell = rows[i].firstChild;
106
107				if (cell.nodeName != "TD" && cell.nodeName != "TH")
108					cell = nextCell(cell);
109
110				do {
111					cell = updateCell(cell, true);
112				} while ((cell = nextCell(cell)) != null);
113			}
114
115			break;
116	}
117
118	ed.addVisual();
119	ed.nodeChanged();
120	inst.execCommand('mceEndUndoLevel');
121	tinyMCEPopup.close();
122}
123
124function nextCell(elm) {
125	while ((elm = elm.nextSibling) != null) {
126		if (elm.nodeName == "TD" || elm.nodeName == "TH")
127			return elm;
128	}
129
130	return null;
131}
132
133function updateCell(td, skip_id) {
134	var inst = ed;
135	var formObj = document.forms[0];
136	var curCellType = td.nodeName.toLowerCase();
137	var celltype = getSelectValue(formObj, 'celltype');
138	var doc = inst.getDoc();
139	var dom = ed.dom;
140
141	if (!skip_id)
142		td.setAttribute('id', formObj.id.value);
143
144	td.setAttribute('align', formObj.align.value);
145	td.setAttribute('vAlign', formObj.valign.value);
146	td.setAttribute('lang', formObj.lang.value);
147	td.setAttribute('dir', getSelectValue(formObj, 'dir'));
148	td.setAttribute('style', ed.dom.serializeStyle(ed.dom.parseStyle(formObj.style.value)));
149	td.setAttribute('scope', formObj.scope.value);
150	ed.dom.setAttrib(td, 'class', getSelectValue(formObj, 'class'));
151
152	// Clear deprecated attributes
153	ed.dom.setAttrib(td, 'width', '');
154	ed.dom.setAttrib(td, 'height', '');
155	ed.dom.setAttrib(td, 'bgColor', '');
156	ed.dom.setAttrib(td, 'borderColor', '');
157	ed.dom.setAttrib(td, 'background', '');
158
159	// Set styles
160	td.style.width = getCSSSize(formObj.width.value);
161	td.style.height = getCSSSize(formObj.height.value);
162	if (formObj.bordercolor.value != "") {
163		td.style.borderColor = formObj.bordercolor.value;
164		td.style.borderStyle = td.style.borderStyle == "" ? "solid" : td.style.borderStyle;
165		td.style.borderWidth = td.style.borderWidth == "" ? "1px" : td.style.borderWidth;
166	} else
167		td.style.borderColor = '';
168
169	td.style.backgroundColor = formObj.bgcolor.value;
170
171	if (formObj.backgroundimage.value != "")
172		td.style.backgroundImage = "url('" + formObj.backgroundimage.value + "')";
173	else
174		td.style.backgroundImage = '';
175
176	if (curCellType != celltype) {
177		// changing to a different node type
178		var newCell = doc.createElement(celltype);
179
180		for (var c=0; c<td.childNodes.length; c++)
181			newCell.appendChild(td.childNodes[c].cloneNode(1));
182
183		for (var a=0; a<td.attributes.length; a++)
184			ed.dom.setAttrib(newCell, td.attributes[a].name, ed.dom.getAttrib(td, td.attributes[a].name));
185
186		td.parentNode.replaceChild(newCell, td);
187		td = newCell;
188	}
189
190	dom.setAttrib(td, 'style', dom.serializeStyle(dom.parseStyle(td.style.cssText)));
191
192	return td;
193}
194
195function changedBackgroundImage() {
196	var formObj = document.forms[0];
197	var st = ed.dom.parseStyle(formObj.style.value);
198
199	st['background-image'] = "url('" + formObj.backgroundimage.value + "')";
200
201	formObj.style.value = ed.dom.serializeStyle(st);
202}
203
204function changedSize() {
205	var formObj = document.forms[0];
206	var st = ed.dom.parseStyle(formObj.style.value);
207
208	var width = formObj.width.value;
209	if (width != "")
210		st['width'] = getCSSSize(width);
211	else
212		st['width'] = "";
213
214	var height = formObj.height.value;
215	if (height != "")
216		st['height'] = getCSSSize(height);
217	else
218		st['height'] = "";
219
220	formObj.style.value = ed.dom.serializeStyle(st);
221}
222
223function changedColor() {
224	var formObj = document.forms[0];
225	var st = ed.dom.parseStyle(formObj.style.value);
226
227	st['background-color'] = formObj.bgcolor.value;
228	st['border-color'] = formObj.bordercolor.value;
229
230	formObj.style.value = ed.dom.serializeStyle(st);
231}
232
233function changedStyle() {
234	var formObj = document.forms[0];
235	var st = ed.dom.parseStyle(formObj.style.value);
236
237	if (st['background-image'])
238		formObj.backgroundimage.value = st['background-image'].replace(new RegExp("url\\('?([^']*)'?\\)", 'gi'), "$1");
239	else
240		formObj.backgroundimage.value = '';
241
242	if (st['width'])
243		formObj.width.value = trimSize(st['width']);
244
245	if (st['height'])
246		formObj.height.value = trimSize(st['height']);
247
248	if (st['background-color']) {
249		formObj.bgcolor.value = st['background-color'];
250		updateColor('bgcolor_pick','bgcolor');
251	}
252
253	if (st['border-color']) {
254		formObj.bordercolor.value = st['border-color'];
255		updateColor('bordercolor_pick','bordercolor');
256	}
257}
258
259tinyMCEPopup.onInit.add(init);