iflrandevu/assets/ext/trumbowyg/plugins/table/trumbowyg.table.js

420 lines
17 KiB
JavaScript
Raw Normal View History

/* ===========================================================
* trumbowyg.table.custom.js v2.0
* Table plugin for Trumbowyg
* http://alex-d.github.com/Trumbowyg
* ===========================================================
* Author : Sven Dunemann [dunemann@forelabs.eu]
*/
(function ($) {
'use strict';
var defaultOptions = {
rows: 8,
columns: 8,
styler: 'table'
};
$.extend(true, $.trumbowyg, {
langs: {
2020-05-12 21:47:37 +03:00
// jshint camelcase:false
en: {
table: 'Insert table',
tableAddRow: 'Add row',
2020-05-12 21:47:37 +03:00
tableAddRowAbove: 'Add row above',
tableAddColumnLeft: 'Add column to the left',
tableAddColumn: 'Add column to the right',
tableDeleteRow: 'Delete row',
tableDeleteColumn: 'Delete column',
tableDestroy: 'Delete table',
error: 'Error'
},
2020-05-12 21:47:37 +03:00
da: {
table: 'Indsæt tabel',
tableAddRow: 'Tilføj række',
tableAddRowAbove: 'Tilføj række',
tableAddColumnLeft: 'Tilføj kolonne',
tableAddColumn: 'Tilføj kolonne',
tableDeleteRow: 'Slet række',
tableDeleteColumn: 'Slet kolonne',
tableDestroy: 'Slet tabel',
error: 'Fejl'
},
de: {
2020-05-12 21:47:37 +03:00
table: 'Tabelle einfügen',
tableAddRow: 'Zeile hinzufügen',
tableAddRowAbove: 'Zeile hinzufügen',
tableAddColumnLeft: 'Spalte hinzufügen',
tableAddColumn: 'Spalte hinzufügen',
tableDeleteRow: 'Zeile löschen',
tableDeleteColumn: 'Spalte löschen',
tableDestroy: 'Tabelle löschen',
error: 'Error'
},
sk: {
table: 'Vytvoriť tabuľky',
tableAddRow: 'Pridať riadok',
2020-05-12 21:47:37 +03:00
tableAddRowAbove: 'Pridať riadok',
tableAddColumnLeft: 'Pridať stĺpec',
tableAddColumn: 'Pridať stĺpec',
error: 'Chyba'
},
fr: {
table: 'Insérer un tableau',
tableAddRow: 'Ajouter des lignes',
2020-05-12 21:47:37 +03:00
tableAddRowAbove: 'Ajouter des lignes',
tableAddColumnLeft: 'Ajouter des colonnes',
tableAddColumn: 'Ajouter des colonnes',
2020-05-12 21:47:37 +03:00
tableDeleteRow: 'Effacer la ligne',
tableDeleteColumn: 'Effacer la colonne',
tableDestroy: 'Effacer le tableau',
error: 'Erreur'
},
cs: {
table: 'Vytvořit příkaz Table',
tableAddRow: 'Přidat řádek',
2020-05-12 21:47:37 +03:00
tableAddRowAbove: 'Přidat řádek',
tableAddColumnLeft: 'Přidat sloupec',
tableAddColumn: 'Přidat sloupec',
error: 'Chyba'
},
ru: {
table: 'Вставить таблицу',
2020-05-12 21:47:37 +03:00
tableAddRow: 'Добавить строку',
tableAddRowAbove: 'Добавить строку',
tableAddColumnLeft: 'Добавить столбец',
tableAddColumn: 'Добавить столбец',
tableDeleteRow: 'Удалить строку',
tableDeleteColumn: 'Удалить столбец',
tableDestroy: 'Удалить таблицу',
error: 'Ошибка'
},
ja: {
table: '表の挿入',
tableAddRow: '行の追加',
2020-05-12 21:47:37 +03:00
tableAddRowAbove: '行の追加',
tableAddColumnLeft: '列の追加',
tableAddColumn: '列の追加',
error: 'エラー'
},
tr: {
table: 'Tablo ekle',
tableAddRow: 'Satır ekle',
2020-05-12 21:47:37 +03:00
tableAddRowAbove: 'Satır ekle',
tableAddColumnLeft: 'Kolon ekle',
tableAddColumn: 'Kolon ekle',
error: 'Hata'
2020-05-12 21:47:37 +03:00
},
zh_tw: {
table: '插入表格',
tableAddRow: '加入行',
tableAddRowAbove: '加入行',
tableAddColumnLeft: '加入列',
tableAddColumn: '加入列',
tableDeleteRow: '刪除行',
tableDeleteColumn: '刪除列',
tableDestroy: '刪除表格',
error: '錯誤'
},
id: {
table: 'Sisipkan tabel',
tableAddRow: 'Sisipkan baris',
tableAddRowAbove: 'Sisipkan baris',
tableAddColumnLeft: 'Sisipkan kolom',
tableAddColumn: 'Sisipkan kolom',
tableDeleteRow: 'Hapus baris',
tableDeleteColumn: 'Hapus kolom',
tableDestroy: 'Hapus tabel',
error: 'Galat'
},
pt_br: {
table: 'Inserir tabela',
tableAddRow: 'Adicionar linha',
tableAddRowAbove: 'Adicionar linha',
tableAddColumnLeft: 'Adicionar coluna',
tableAddColumn: 'Adicionar coluna',
tableDeleteRow: 'Deletar linha',
tableDeleteColumn: 'Deletar coluna',
tableDestroy: 'Deletar tabela',
error: 'Erro'
},
ko: {
table: '표 넣기',
tableAddRow: '줄 추가',
tableAddRowAbove: '줄 추가',
tableAddColumnLeft: '칸 추가',
tableAddColumn: '칸 추가',
tableDeleteRow: '줄 삭제',
tableDeleteColumn: '칸 삭제',
tableDestroy: '표 지우기',
error: '에러'
},
// jshint camelcase:true
},
plugins: {
table: {
init: function (t) {
t.o.plugins.table = $.extend(true, {}, defaultOptions, t.o.plugins.table || {});
var buildButtonDef = {
fn: function () {
2020-05-12 21:47:37 +03:00
t.saveRange();
var btnName = 'table';
var dropdownPrefix = t.o.prefix + 'dropdown',
dropdownOptions = { // the dropdown
class: dropdownPrefix + '-' + btnName + ' ' + dropdownPrefix + ' ' + t.o.prefix + 'fixed-top'
};
dropdownOptions['data-' + dropdownPrefix] = btnName;
var $dropdown = $('<div/>', dropdownOptions);
if (t.$box.find('.' + dropdownPrefix + '-' + btnName).length === 0) {
t.$box.append($dropdown.hide());
} else {
$dropdown = t.$box.find('.' + dropdownPrefix + '-' + btnName);
}
2020-05-12 21:47:37 +03:00
// clear dropdown
$dropdown.html('');
// when active table show AddRow / AddColumn
if (t.$box.find('.' + t.o.prefix + 'table-button').hasClass(t.o.prefix + 'active-button')) {
$dropdown.append(t.buildSubBtn('tableAddRowAbove'));
$dropdown.append(t.buildSubBtn('tableAddRow'));
$dropdown.append(t.buildSubBtn('tableAddColumnLeft'));
$dropdown.append(t.buildSubBtn('tableAddColumn'));
$dropdown.append(t.buildSubBtn('tableDeleteRow'));
$dropdown.append(t.buildSubBtn('tableDeleteColumn'));
$dropdown.append(t.buildSubBtn('tableDestroy'));
} else {
var tableSelect = $('<table/>');
$('<tbody/>').appendTo(tableSelect);
for (var i = 0; i < t.o.plugins.table.rows; i += 1) {
var row = $('<tr/>').appendTo(tableSelect);
for (var j = 0; j < t.o.plugins.table.columns; j += 1) {
$('<td/>').appendTo(row);
}
}
tableSelect.find('td').on('mouseover', tableAnimate);
tableSelect.find('td').on('mousedown', tableBuild);
$dropdown.append(tableSelect);
$dropdown.append($('<div class="trumbowyg-table-size">1x1</div>'));
}
2020-05-12 21:47:37 +03:00
t.dropdown(btnName);
}
};
2020-05-12 21:47:37 +03:00
var tableAnimate = function(columnEvent) {
var column = $(columnEvent.target),
table = column.closest('table'),
colIndex = this.cellIndex,
rowIndex = this.parentNode.rowIndex;
2020-05-12 21:47:37 +03:00
// reset all columns
table.find('td').removeClass('active');
2020-05-12 21:47:37 +03:00
for (var i = 0; i <= rowIndex; i += 1) {
for (var j = 0; j <= colIndex; j += 1) {
table.find('tr:nth-of-type('+(i+1)+')').find('td:nth-of-type('+(j+1)+')').addClass('active');
}
}
2020-05-12 21:47:37 +03:00
// set label
table.next('.trumbowyg-table-size').html((colIndex+1) + 'x' + (rowIndex+1));
};
2020-05-12 21:47:37 +03:00
var tableBuild = function() {
t.saveRange();
2020-05-12 21:47:37 +03:00
var tabler = $('<table/>');
$('<tbody/>').appendTo(tabler);
if (t.o.plugins.table.styler) {
tabler.attr('class', t.o.plugins.table.styler);
}
2020-05-12 21:47:37 +03:00
var colIndex = this.cellIndex,
rowIndex = this.parentNode.rowIndex;
2020-05-12 21:47:37 +03:00
for (var i = 0; i <= rowIndex; i += 1) {
var row = $('<tr></tr>').appendTo(tabler);
for (var j = 0; j <= colIndex; j += 1) {
$('<td/>').appendTo(row);
}
}
2020-05-12 21:47:37 +03:00
t.range.deleteContents();
t.range.insertNode(tabler[0]);
t.$c.trigger('tbwchange');
};
var addRow = {
2020-05-12 21:47:37 +03:00
title: t.lang.tableAddRow,
text: t.lang.tableAddRow,
ico: 'row-below',
2020-05-12 21:47:37 +03:00
fn: function () {
t.saveRange();
var node = t.doc.getSelection().focusNode;
var focusedRow = $(node).closest('tr');
var table = $(node).closest('table');
if(table.length > 0) {
var row = $('<tr/>');
// add columns according to current columns count
for (var i = 0; i < table.find('tr')[0].childElementCount; i += 1) {
$('<td/>').appendTo(row);
}
// add row to table
focusedRow.after(row);
}
2020-05-12 21:47:37 +03:00
t.syncCode();
}
2020-05-12 21:47:37 +03:00
};
var addRowAbove = {
title: t.lang.tableAddRowAbove,
text: t.lang.tableAddRowAbove,
ico: 'row-above',
fn: function () {
t.saveRange();
var node = t.doc.getSelection().focusNode;
var focusedRow = $(node).closest('tr');
var table = $(node).closest('table');
if(table.length > 0) {
var row = $('<tr/>');
// add columns according to current columns count
for (var i = 0; i < table.find('tr')[0].childElementCount; i += 1) {
$('<td/>').appendTo(row);
}
// add row to table
focusedRow.before(row);
}
2020-05-12 21:47:37 +03:00
t.syncCode();
}
};
var addColumn = {
2020-05-12 21:47:37 +03:00
title: t.lang.tableAddColumn,
text: t.lang.tableAddColumn,
ico: 'col-right',
2020-05-12 21:47:37 +03:00
fn: function () {
t.saveRange();
var node = t.doc.getSelection().focusNode;
var focusedCol = $(node).closest('td');
var table = $(node).closest('table');
var focusedColIdx = focusedCol.index();
2020-05-12 21:47:37 +03:00
if(table.length > 0) {
$(table).find('tr').each(function() {
$($(this).children()[focusedColIdx]).after('<td></td>');
});
}
2020-05-12 21:47:37 +03:00
t.syncCode();
}
2020-05-12 21:47:37 +03:00
};
var addColumnLeft = {
title: t.lang.tableAddColumnLeft,
text: t.lang.tableAddColumnLeft,
ico: 'col-left',
fn: function () {
t.saveRange();
var node = t.doc.getSelection().focusNode;
var focusedCol = $(node).closest('td');
var table = $(node).closest('table');
var focusedColIdx = focusedCol.index();
2020-05-12 21:47:37 +03:00
if(table.length > 0) {
$(table).find('tr').each(function() {
$($(this).children()[focusedColIdx]).before('<td></td>');
});
}
t.syncCode();
}
};
var destroy = {
2020-05-12 21:47:37 +03:00
title: t.lang.tableDestroy,
text: t.lang.tableDestroy,
ico: 'table-delete',
2020-05-12 21:47:37 +03:00
fn: function () {
t.saveRange();
2020-05-12 21:47:37 +03:00
var node = t.doc.getSelection().focusNode,
table = $(node).closest('table');
2020-05-12 21:47:37 +03:00
table.remove();
2020-05-12 21:47:37 +03:00
t.syncCode();
}
};
var deleteRow = {
2020-05-12 21:47:37 +03:00
title: t.lang.tableDeleteRow,
text: t.lang.tableDeleteRow,
ico: 'row-delete',
2020-05-12 21:47:37 +03:00
fn: function () {
t.saveRange();
2020-05-12 21:47:37 +03:00
var node = t.doc.getSelection().focusNode,
row = $(node).closest('tr');
2020-05-12 21:47:37 +03:00
row.remove();
2020-05-12 21:47:37 +03:00
t.syncCode();
}
};
var deleteColumn = {
2020-05-12 21:47:37 +03:00
title: t.lang.tableDeleteColumn,
text: t.lang.tableDeleteColumn,
ico: 'col-delete',
2020-05-12 21:47:37 +03:00
fn: function () {
t.saveRange();
2020-05-12 21:47:37 +03:00
var node = t.doc.getSelection().focusNode,
table = $(node).closest('table'),
td = $(node).closest('td'),
cellIndex = td.index();
2020-05-12 21:47:37 +03:00
$(table).find('tr').each(function() {
$(this).find('td:eq(' + cellIndex + ')').remove();
});
2020-05-12 21:47:37 +03:00
t.syncCode();
}
};
t.addBtnDef('table', buildButtonDef);
2020-05-12 21:47:37 +03:00
t.addBtnDef('tableAddRowAbove', addRowAbove);
t.addBtnDef('tableAddRow', addRow);
2020-05-12 21:47:37 +03:00
t.addBtnDef('tableAddColumnLeft', addColumnLeft);
t.addBtnDef('tableAddColumn', addColumn);
t.addBtnDef('tableDeleteRow', deleteRow);
t.addBtnDef('tableDeleteColumn', deleteColumn);
t.addBtnDef('tableDestroy', destroy);
}
}
}
});
})(jQuery);