<commit_before>/* ===========================================================
 * tablemanager.js v1.0
 * Table Management plugin for Trumbowyg
 * http://alex-d.github.com/Trumbowyg
 * ===========================================================
 * Author : <NAME>, Senior Developer at Edsby
 *		  Twitter : @timonorawski, @Edsby
 *		  Website : www.edsby.com
 */

(function ($) {
	'use strict';
	var apply = function(def, pNodes, key, value) {
			var doApply = function(toNode) {
					toNode = $(toNode);
					if (def.e.type == "attr") {
						toNode.attr(key, value);
					} else if (def.e.type == "css") {
						toNode.css(def.e.attr, value);
					}
				};
			if (def.e) {
				for (var i = 0; i < pNodes.length; i++) {
					var node = $(pNodes[i]);
					if (def.e && def.e.applyto) {
						if (node.is(def.e.applyto)) {
							doApply(node);

						} else {
							var applynodes = node.find(def.e.applyto);
							for (var j = 0; j < applynodes.length; j++) {
								doApply(applynodes[j]);
							}
						}
					} else if (def.e) {
						doApply(pNodes[i]);
					}
				}
			}
		},
		fetchInitialValues = function(node, fields) {
			for (var i in fields) {
				if (fields[i].e) {
					if (fields[i].e.type == "attr") {
						fields[i].value = node.attr(i);
					} else if (fields[i].e.type == "css") {
						fields[i].value = node.css(i);
					}
				}
			}
		},
		getCellMap = function(table, rows) {
			var cellMap = [], i;
			for (i =0 ; i < rows.length; i++) {
				cellMap.push([]);
			}
			var tcell = false;
			for (var r = 0; r < table[0].rows.length; r++) {
				var colidx = 0;
				for (var c = 0; c < table[0].rows[r].cells.length; c++) {
					var cellid = {
							"row": r,
							"column": c,
							"truecolumn": colidx,
							"colspan": table[0].rows[r].cells[c].colSpan,
							"rowspan": table[0].rows[r].cells[c].rowSpan,
							"cell": table[0].rows[r].cells[c]
						},
						cellidx = c;
					tcell = table[0].rows[r].cells[c];
					colidx = colidx + cellid.colspan;
					while (cellMap[r][cellidx]) {
						cellidx = cellidx + 1;
					}
					for (i = 0; i < tcell.colSpan; i++) {
						for (var j = 0; j < tcell.rowSpan; j++) {
							cellMap[r+j][cellidx+i] = cellid;
						}
					}
				}
			}
			return cellMap;
		},
		replaceNodeWithHTML = function(trumbowyg, replaceNode, html) {
			var range = trumbowyg.doc.createRange(),
				documentSelection = trumbowyg.doc.getSelection();
			documentSelection.removeAllRanges();
			range.selectNode(replaceNode[0]);
			documentSelection.addRange(range);
			trumbowyg.execCmd('insertHTML', html);
			// TODO: try to restore selection
		},
		showDialog = function(conf) {
			fetchInitialValues(conf.node, conf.fields);
			for (var i in conf.fields) {
				if (!conf.fields[i].label) {
					conf.fields[i].label = i.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
				}
			}
			var saved = JSON.parse(JSON.stringify(conf.fields));
			var isChanged = function(val, key) {
				return val[key] != (saved[key].value || "");
			};
			conf.trumbowyg.openModalInsert(
				// Title
				conf.title,
				// Fields
				conf.fields,
				function (val) { // v is value
					var tmpNode = conf.replacenode.clone();
					for (var i in val) {
						if (isChanged(val, i)) {
							apply(conf.fields[i], conf.node, i, val[i]);
							if (conf.relatedNodes) {
								apply(conf.fields[i], conf.relatedNodes, i, val[i]);
							}
							if (i == "caption" && conf.replacenode.is("table")) {
								var caption = conf.replacenode.find('caption');
								if (caption.length > 0) {
									caption.html(val[i]);
								} else {
									conf.replacenode.prepend("<caption>" + val[i] + "</caption>");
								}
							}
						}
					}

					var range = conf.trumbowyg.doc.createRange(),
						documentSelection = conf.trumbowyg.doc.getSelection();
					documentSelection.removeAllRanges();
					range.selectNode(conf.replacenode[0]);
					documentSelection.addRange(range);
					conf.replacenode.replaceWith(tmpNode);
					conf.trumbowyg.execCmd('insertHTML', conf.replacenode[0].outerHTML);
					return true;
				}
			);
		};
	$.extend(true, $.trumbowyg, {
		langs: {
			en: {
				insertTable: 'Insert Table',
				editTable: 'Format Table',
				formatCell: 'Format Cell',
				formatRow: 'Format Row',
				formatColumn: "Format Column",
				insertLink: "Edit Link",
				rows: 'Rows',
				columns: 'Columns',
				cellwidth: 'Cell Width',
				cellborder: 'Cell Border',
				cellpadding: 'Cell Padding',
				addRow: "Insert Row",
				addColumn: "Insert Column",
				deleteRow: "Delete Row",
				deleteColumn: "Delete Column",
				mergeDown: "Merge Down",
				mergeRight: "Merge Right"
			}
		},
		plugins: {
			tablemanager: {
				init: function (trumbowyg) {
					trumbowyg.addBtnDef('insertTable', {
						ico: "table",
						fn: function () {
							trumbowyg.saveRange();
							trumbowyg.openModalInsert(
								// Title
								trumbowyg.lang.insertTable,
								// Fields
								{
									rows: {
										type: 'number',
										required: true
									},
									columns: {
										type: 'number',
										required: true
									}
								},
								function (v) { // v is value
									/* create basic table */
									if (v.rows > 100 || v.columns > 100 || v.rows <= 0 || v.columns <= 0) {
										return false;
									}
									var table = ['<table width="500px" style="background-color: white;">'];
									for (var i = 0; i < v.rows; i += 1) {
										table.push("<tr>");
										for (var j = 0; j < v.columns; j += 1) {
											table.push('<td>&nbsp;</td>');
										}
										table.push("</tr>");
									}
									table.push("</table>");
									/* you must use insertHTML to preserve undo/redo */
									trumbowyg.execCmd('insertHTML', table.join(""));
									return true;
								});
						}
					});
					trumbowyg.addBtnDef('formatCell', {
						fn: function () {
							trumbowyg.saveRange();
							var range = trumbowyg.range,
								fields = {
									width: {
										type: 'string',
										required: false,
										e: {<commit_msg>ie11 work - incomplete<commit_after>/* ===========================================================
 * tablemanager.js v1.0
 * Table Management plugin for Trumbowyg
 * http://alex-d.github.com/Trumbowyg
 * ===========================================================
 * Author : <NAME>, Senior Developer at Edsby
 *		  Twitter : @timonorawski, @Edsby
 *		  Website : www.edsby.com
 */

(function ($) {
	'use strict';
	var isIE11 = !!(navigator.userAgent.match(/Trident/) && navigator.userAgent.match(/rv[ :]11/));

	var insertHTML = function(trumbowyg, html) {
		if (isIE11) {
			var range = trumbowyg.doc.createRange(),
				documentSelection = trumbowyg.doc.getSelection();
			trumbowyg.range.deleteContents();
			trumbowyg.range.insertNode($(html)[0]);
		} else {
			trumbowyg.execCmd('insertHTML', html);
		}
	};
	var apply = function(def, pNodes, key, value) {
			var doApply = function(toNode) {
					toNode = $(toNode);
					if (def.e.type == "attr") {
						toNode.attr(key, value);
					} else if (def.e.type == "css") {
						toNode.css(def.e.attr, value);
					}
				};
			if (def.e) {
				for (var i = 0; i < pNodes.length; i++) {
					var node = $(pNodes[i]);
					if (def.e && def.e.applyto) {
						if (node.is(def.e.applyto)) {
							doApply(node);

						} else {
							var applynodes = node.find(def.e.applyto);
							for (var j = 0; j < applynodes.length; j++) {
								doApply(applynodes[j]);
							}
						}
					} else if (def.e) {
						doApply(pNodes[i]);
					}
				}
			}
		},
		fetchInitialValues = function(node, fields) {
			for (var i in fields) {
				if (fields[i].e) {
					if (fields[i].e.type == "attr") {
						fields[i].value = node.attr(i);
					} else if (fields[i].e.type == "css") {
						fields[i].value = node.css(i);
					}
				}
			}
		},
		getCellMap = function(table, rows) {
			var cellMap = [], i;
			for (i =0 ; i < rows.length; i++) {
				cellMap.push([]);
			}
			var tcell = false;
			for (var r = 0; r < table[0].rows.length; r++) {
				var colidx = 0;
				for (var c = 0; c < table[0].rows[r].cells.length; c++) {
					var cellid = {
							"row": r,
							"column": c,
							"truecolumn": colidx,
							"colspan": table[0].rows[r].cells[c].colSpan,
							"rowspan": table[0].rows[r].cells[c].rowSpan,
							"cell": table[0].rows[r].cells[c]
						},
						cellidx = c;
					tcell = table[0].rows[r].cells[c];
					colidx = colidx + cellid.colspan;
					while (cellMap[r][cellidx]) {
						cellidx = cellidx + 1;
					}
					for (i = 0; i < tcell.colSpan; i++) {
						for (var j = 0; j < tcell.rowSpan; j++) {
							cellMap[r+j][cellidx+i] = cellid;
						}
					}
				}
			}
			return cellMap;
		},
		replaceNodeWithHTML = function(trumbowyg, replaceNode, html) {
			var range = trumbowyg.doc.createRange(),
				documentSelection = trumbowyg.doc.getSelection();
			documentSelection.removeAllRanges();
			range.selectNode(replaceNode[0]);
			documentSelection.addRange(range);
			insertHTML(trumbowyg, html);
			// TODO: try to restore selection
		},
		showDialog = function(conf) {
			fetchInitialValues(conf.node, conf.fields);
			for (var i in conf.fields) {
				if (!conf.fields[i].label) {
					conf.fields[i].label = i.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
				}
			}
			var saved = JSON.parse(JSON.stringify(conf.fields));
			var isChanged = function(val, key) {
				return val[key] != (saved[key].value || "");
			};
			conf.trumbowyg.openModalInsert(
				// Title
				conf.title,
				// Fields
				conf.fields,
				function (val) { // v is value
					var tmpNode = conf.replacenode.clone();
					for (var i in val) {
						if (isChanged(val, i)) {
							apply(conf.fields[i], conf.node, i, val[i]);
							if (conf.relatedNodes) {
								apply(conf.fields[i], conf.relatedNodes, i, val[i]);
							}
							if (i == "caption" && conf.replacenode.is("table")) {
								var caption = conf.replacenode.find('caption');
								if (caption.length > 0) {
									caption.html(val[i]);
								} else {
									conf.replacenode.prepend("<caption>" + val[i] + "</caption>");
								}
							}
						}
					}

					var range = conf.trumbowyg.doc.createRange(),
						documentSelection = conf.trumbowyg.doc.getSelection();
					documentSelection.removeAllRanges();
					range.selectNode(conf.replacenode[0]);
					documentSelection.addRange(range);
					conf.replacenode.replaceWith(tmpNode);
					insertHTML(conf.trumbowyg, conf.replacenode[0].outerHTML);
//					conf.trumbowyg.execCmd('insertHTML', conf.replacenode[0].outerHTML);
					return true;
				}
			);
		};
	$.extend(true, $.trumbowyg, {
		langs: {
			en: {
				insertTable: 'Insert Table',
				editTable: 'Format Table',
				formatCell: 'Format Cell',
				formatRow: 'Format Row',
				formatColumn: "Format Column",
				insertLink: "Edit Link",
				rows: 'Rows',
				columns: 'Columns',
				cellwidth: 'Cell Width',
				cellborder: 'Cell Border',
				cellpadding: 'Cell Padding',
				addRow: "Insert Row",
				addColumn: "Insert Column",
				deleteRow: "Delete Row",
				deleteColumn: "Delete Column",
				mergeDown: "Merge Down",
				mergeRight: "Merge Right"
			}
		},
		plugins: {
			tablemanager: {
				init: function (trumbowyg) {
					trumbowyg.addBtnDef('insertTable', {
						ico: "table",
						fn: function () {
							trumbowyg.saveRange();
							trumbowyg.openModalInsert(
								// Title
								trumbowyg.lang.insertTable,
								// Fields
								{
									rows: {
										type: 'number',
										required: true
									},
									columns: {
										type: 'number',
										required: true
									}
								},
								function (v) { // v is value
									/* create basic table */
									if (v.rows > 100 || v.columns > 100 || v.rows <= 0 || v.columns <= 0) {
										return false;
									}
									var table = ['<table width="500px" style="background-color: white;">'];
									for (var i = 0; i < v.rows; i += 1) {
										table.push("<tr>");
										for (var j = 0; j < v.columns; j += 1) {
											table.push('<td>&nbsp;</td>');
										}
										table.push("</tr>");
									}
									table.push("</table>");
									/* you must use insertHTML to preserve undo/redo */
									insertHTML(trumbowyg, table.join(""));
//									trumbowyg.execCmd('insertHTML', table.join(""));
									return true;
								});
						}
					});
					trumbowyg.addBtnDef('formatCell', {
						fn: function () {
							trumbowyg.saveRange();
							var range = trumbowyg.range,
								fields = {
									width: {
										type: 'string',
										required: false,
										e: {
{'fraction_non_alphanumeric': 0.18386284995281535, 'fraction_numerical': 0.006763133060710916, 'mean_word_length': 2.254927054005631, 'pattern_counts': {'":': 12, '<': 43, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1160275', 'n_tokens_mistral': 6195, 'n_tokens_neox': 4320, 'n_words': 883}