MediaWiki:Common.js: Unterschied zwischen den Versionen
Aus WORLDFISH WIKI
Web16 (Diskussion | Beiträge) |
Web16 (Diskussion | Beiträge) |
||
Zeile 47: | Zeile 47: | ||
var numRows = 0; | var numRows = 0; | ||
− | for(var i = 0; i | + | for(var i = 0; i < buttonList.length; i++) |
{ | { | ||
− | if(buttonList[i].row | + | if(buttonList[i].row > numRows) |
numRows = buttonList[i].row; | numRows = buttonList[i].row; | ||
} | } | ||
Zeile 55: | Zeile 55: | ||
//Save all headers by their row index: | //Save all headers by their row index: | ||
const rowData = []; | const rowData = []; | ||
− | for(var i = 0; i | + | for(var i = 0; i < headers.length; i++) |
{ | { | ||
rowData[headers[i].row - 1] = headers[i]; | rowData[headers[i].row - 1] = headers[i]; | ||
− | if(headers[i].row | + | if(headers[i].row > numRows) |
numRows = headers[i].row; | numRows = headers[i].row; | ||
} | } | ||
Zeile 64: | Zeile 64: | ||
const td = []; | const td = []; | ||
− | for(var i = 0; i | + | for(var i = 0; i < numRows; i++) |
{ | { | ||
const tr = document.createElement("tr"); | const tr = document.createElement("tr"); | ||
Zeile 72: | Zeile 72: | ||
headerCell.innerText = rowData[i].text; | headerCell.innerText = rowData[i].text; | ||
headerCell.setAttribute("class", CLASS_HEADER); | headerCell.setAttribute("class", CLASS_HEADER); | ||
− | if(rowData[i].rowSpan | + | if(rowData[i].rowSpan > 1) |
headerCell.setAttribute("rowspan", rowData[i].rowSpan); | headerCell.setAttribute("rowspan", rowData[i].rowSpan); | ||
tr.appendChild(headerCell); | tr.appendChild(headerCell); | ||
Zeile 81: | Zeile 81: | ||
} | } | ||
− | for(var i = 0; i | + | for(var i = 0; i < buttons.length; i++) |
{ | { | ||
const row = buttonList[i].row; | const row = buttonList[i].row; | ||
Zeile 118: | Zeile 118: | ||
function createButtons() { | function createButtons() { | ||
const buttonElements = []; | const buttonElements = []; | ||
− | for(var i = 0; i | + | for(var i = 0; i < buttonList.length; i++) |
{ | { | ||
const button = buttonList[i]; | const button = buttonList[i]; | ||
Zeile 143: | Zeile 143: | ||
− | buttonList = [{"content":" | + | buttonList = [{"content":"#redirect","row":0,"prefix":"#redirect[[","suffix":"]]"},{"content":"doi+","row":0,"prefix":"[","suffix":" (doi)] <includeonly>[https://wf-wiki.de/index.php/Vorlage:Common.js Zitatseite]</includeonly> <noinclude>[[Category:Literaturvorlage]]</noinclude>"},{"content":"subst:reftemp","row":0,"prefix":"","suffix":" <includeonly>[https://wf-wiki.de/index.php/Vorlage:Common.js Zitatseite]</includeonly> <noinclude>[[Category:Literaturvorlage]]</noinclude>"},{"content":"falsche Art","row":0,"prefix":"<span style=\"color: red; text-decoration: line-through;\">''Misidentifizierter Artname''</span> -->''[[richtiger Artname]]''","suffix":""},{"content":"!!","row":0,"prefix":"<span style=\"color: red\">'''!!'''</span>","suffix":""},{"content":"ETY","row":0,"prefix":"(Übersetzt aus: [","suffix":" The ETYFish Project])"},{"content":"Autoren++","row":0,"prefix":"Der Autorenname '''{{BASEPAGENAME}}''' könnte auf folgende Personen verweisen:","suffix":""},{"content":"disambig","row":0,"prefix":"{{disambig}}","suffix":""},{"content":"[ ]","row":0,"prefix":"[","suffix":"]"},{"content":"[[ ]]","row":0,"prefix":"[[","suffix":"]]"},{"content":"{{ }}","row":0,"prefix":"{{","suffix":"}}"},{"content":"{{a| }}","row":0,"prefix":"{{a|","suffix":"}}"},{"content":"{{aut| }}","row":0,"prefix":"{{aut|","suffix":"}}"},{"content":"†","row":0,"prefix":"†","suffix":""},{"content":"♂","row":0,"prefix":"♂","suffix":""},{"content":"♀","row":0,"prefix":"♀","suffix":""},{"content":"br","row":0,"prefix":"<br />","suffix":""}]; |
− | + | headers = []; | |
nameOfTargetBox = "wpTextbox1"; | nameOfTargetBox = "wpTextbox1"; | ||
toolbarCSS = "" + | toolbarCSS = "" + |
Version vom 22. Februar 2019, 16:04 Uhr
const POSITION_ABOVE = 0; const POSITION_BEYOND = 1; const ID_TOOLBAR = "cebToolbar"; const ID_TOOLBAR_STYLE = "toolbarStyle"; const CLASS_TOOLBAR = "buttonToolbar"; const CLASS_TOP_BUTTON_BAR = "topButtonBar"; const CLASS_HEADER = "header"; // Set this value to the ID of the target text area: var nameOfTargetBox = null; var targetNode = null; //Insert buttons with properties text, icon, prefix, suffix, etc. (see createButtons for details) var buttonList = []; var headers = []; var topButtonContainer = null; var toolbarCSS = null; function addToolbar(referenceNode, position) { if(!referenceNode) { console.error("Cannot add tool bar, invalid reference node"); return; } const buttons = createButtons(); const toolbar = document.createElement("div"); toolbar.setAttribute("id", ID_TOOLBAR); toolbar.setAttribute("class", CLASS_TOOLBAR); if(toolbarCSS != null) { createToolbarCSS(toolbar); } topButtonContainer = document.createElement("div"); topButtonContainer.setAttribute("class", CLASS_TOP_BUTTON_BAR); toolbar.appendChild(topButtonContainer); const toolbarTable = document.createElement("table"); toolbarTable.setAttribute("style", "width: 100%"); toolbar.appendChild(toolbarTable); var numRows = 0; for(var i = 0; i < buttonList.length; i++) { if(buttonList[i].row > numRows) numRows = buttonList[i].row; } //Save all headers by their row index: const rowData = []; for(var i = 0; i < headers.length; i++) { rowData[headers[i].row - 1] = headers[i]; if(headers[i].row > numRows) numRows = headers[i].row; } const td = []; for(var i = 0; i < numRows; i++) { const tr = document.createElement("tr"); if(rowData[i]) { const headerCell = document.createElement("td"); headerCell.innerText = rowData[i].text; headerCell.setAttribute("class", CLASS_HEADER); if(rowData[i].rowSpan > 1) headerCell.setAttribute("rowspan", rowData[i].rowSpan); tr.appendChild(headerCell); } td[i] = document.createElement("td"); tr.appendChild(td[i]); toolbarTable.appendChild(tr); } for(var i = 0; i < buttons.length; i++) { const row = buttonList[i].row; if(row == 0) topButtonContainer.appendChild(buttons[i]); else td[row - 1].appendChild(buttons[i]); } if(position == POSITION_BEYOND) referenceNode.insertAdjacentElement("afterend", toolbar); else referenceNode.insertAdjacentElement("beforebegin", toolbar); } function createToolbarCSS(targetElement) { const element = document.createElement("style"); element.innerHTML = toolbarCSS; targetElement.appendChild(element); } function insertText(textBeforeCursor, textAfterCursor) { const start = targetNode.selectionStart; const end = targetNode.selectionEnd; const text = targetNode.value; var output = text.substr(0, start) + textBeforeCursor; output += text.substr(start, end - start); output += textAfterCursor + text.substr(end, text.length - end); targetNode.value = output; // targetNode.selectionStart = start + textBeforeCursor.length; // targetNode.selectionEnd = end + textBeforeCursor.length; targetNode.focus(); } function createButtons() { const buttonElements = []; for(var i = 0; i < buttonList.length; i++) { const button = buttonList[i]; const buttonElement = document.createElement("button"); buttonElement.type = "button"; buttonElement.innerHTML = button.content; buttonElement.onclick = function () { insertText(button.prefix, button.suffix); }; buttonElements.push(buttonElement); } return buttonElements; } function setup() { /* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */ if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1) { mw.loader.using('user.options').then(function () { targetNode = document.getElementById(nameOfTargetBox); addToolbar(targetNode, POSITION_BEYOND); }); } } buttonList = [{"content":"#redirect","row":0,"prefix":"#redirect[[","suffix":"]]"},{"content":"doi+","row":0,"prefix":"[","suffix":" (doi)] <includeonly>[https://wf-wiki.de/index.php/Vorlage:Common.js Zitatseite]</includeonly> <noinclude>[[Category:Literaturvorlage]]</noinclude>"},{"content":"subst:reftemp","row":0,"prefix":"","suffix":" <includeonly>[https://wf-wiki.de/index.php/Vorlage:Common.js Zitatseite]</includeonly> <noinclude>[[Category:Literaturvorlage]]</noinclude>"},{"content":"falsche Art","row":0,"prefix":"<span style=\"color: red; text-decoration: line-through;\">''Misidentifizierter Artname''</span> -->''[[richtiger Artname]]''","suffix":""},{"content":"!!","row":0,"prefix":"<span style=\"color: red\">'''!!'''</span>","suffix":""},{"content":"ETY","row":0,"prefix":"(Übersetzt aus: [","suffix":" The ETYFish Project])"},{"content":"Autoren++","row":0,"prefix":"Der Autorenname '''{{BASEPAGENAME}}''' könnte auf folgende Personen verweisen:","suffix":""},{"content":"disambig","row":0,"prefix":"{{disambig}}","suffix":""},{"content":"[ ]","row":0,"prefix":"[","suffix":"]"},{"content":"[[ ]]","row":0,"prefix":"[[","suffix":"]]"},{"content":"{{ }}","row":0,"prefix":"{{","suffix":"}}"},{"content":"{{a| }}","row":0,"prefix":"{{a|","suffix":"}}"},{"content":"{{aut| }}","row":0,"prefix":"{{aut|","suffix":"}}"},{"content":"†","row":0,"prefix":"†","suffix":""},{"content":"♂","row":0,"prefix":"♂","suffix":""},{"content":"♀","row":0,"prefix":"♀","suffix":""},{"content":"br","row":0,"prefix":"<br />","suffix":""}]; headers = []; nameOfTargetBox = "wpTextbox1"; toolbarCSS = "" + " .buttonToolbar td" + " {" + " vertical-align: top;" + " }" + " .buttonToolbar .header" + " {" + " width: 1%;" + " padding: 2px;" + " vertical-align: center;" + " white-space: nowrap;" + " }" + " .buttonToolbar button" + " {" + " width: auto;" + " margin: 2px;" + " }" + " "; setup();