/** * Button functions */ class ButtonFunctions { /** * HTML of a download button * * @param {string} ext * @param {string} identifier * @param {string} media * @returns {HTMLAnchorElement} */ static getDownloadButton(ext, identifier, media = '') { const button = document.createElement('button'); button.className = 'diagrams-btn'; const icon = ButtonFunctions.getButtonIcon('download'); button.prepend(icon); const link = document.createElement('a'); if (ext === 'png') { button.append(LANG.plugins.diagrams.downloadPNGButtonShort); button.title = LANG.plugins.diagrams.downloadPNGButton; let href = DOKU_BASE + 'lib/exe/ajax.php?call=plugin_diagrams_pngdownload' + '&pngcache=' + encodeURIComponent(identifier); let param; if (media.length) { param = '&media=' + encodeURIComponent(media); } else { param = '&id=' + JSINFO.id; } link.href = href + param; } else { link.href = identifier; let downloadName; if (media.length) { downloadName = media; } else { downloadName = JSINFO.id.split(':').pop() + `.${ext}`; } link.setAttribute('download', downloadName); button.append(LANG.plugins.diagrams.downloadSVGButtonShort); button.title = LANG.plugins.diagrams.downloadSVGButton; } link.appendChild(button); return link; } /** * HTML of an open button * * @param {string} url * @returns {HTMLButtonElement} */ static getOpenButton(url) { const button = document.createElement('button'); button.className = 'diagrams-btn'; button.innerText = LANG.plugins.diagrams.openButtonShort; button.title = LANG.plugins.diagrams.openButton; button.prepend(ButtonFunctions.getButtonIcon('open')); button.addEventListener('click', event => { event.preventDefault(); window.location = url; }); return button; } /** * Icon HTML * * @param {string} button * @returns {HTMLSpanElement} */ static getButtonIcon(button) { const icon = document.createElement('span'); icon.className = `icon-${button}`; switch (button) { case "open": icon.innerHTML = ''; break; case "edit": icon.innerHTML = ''; break; case "download": icon.innerHTML = ''; break; } return icon; } }