Merge pull request #26 from OptimistikSAS/seConfirmDialog

#seConfirmDialog confirm and alert change to alix AlertDialog
master
JFH 2020-12-28 16:18:12 +01:00 committed by GitHub
commit 9a7d60dfe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 107 additions and 35 deletions

View File

@ -4,3 +4,5 @@ import './svgSourceDialog.js';
import './cmenuDialog.js';
import './cmenuLayersDialog.js';
import './seSelectDialog.js';
import './seConfirmDialog.js';
import './seAlertDialog.js';

View File

@ -0,0 +1,59 @@
// eslint-disable-next-line node/no-unpublished-import
import AlertDialog from 'elix/define/AlertDialog.js';
/**
* @class SeAlertDialog
*/
export class SeAlertDialog extends HTMLElement {
/**
* @function constructor
*/
constructor () {
super();
// create the shadowDom and insert the template
this._shadowRoot = this.attachShadow({mode: 'open'});
this.dialog = new AlertDialog();
}
/**
* @function observedAttributes
* @returns {any} observed
*/
static get observedAttributes () {
return ['title'];
}
/**
* @function attributeChangedCallback
* @param {string} name
* @param {string} oldValue
* @param {string} newValue
* @returns {void}
*/
attributeChangedCallback (name, oldValue, newValue) {
switch (name) {
case 'title':
this.dialog.textContent = newValue;
this.dialog.open();
break;
default:
console.error('unkonw attr for:', name, 'newValue =', newValue);
break;
}
}
/**
* @function get
* @returns {any}
*/
get title () {
return this.getAttribute('title');
}
/**
* @function set
* @returns {void}
*/
set title (value) {
this.setAttribute('title', value);
}
}
// Register
customElements.define('se-alert-dialog', SeAlertDialog);

View File

@ -0,0 +1,13 @@
// eslint-disable-next-line node/no-unpublished-import
import AlertDialog from 'elix/define/AlertDialog.js';
const dialog = new AlertDialog();
const seConfirm = async (text, choices) => {
dialog.textContent = text;
dialog.choices = (choices === undefined) ? ['Ok', 'Cancel'] : choices;
dialog.open();
const response = await dialog.whenClosed();
return response.choice;
};
window.seConfirm = seConfirm;

View File

@ -32,8 +32,7 @@ $('a').click(function () {
data = canvas.toDataURL();
} catch (err) {
// This fails in Firefox with `file:///` URLs :(
// Todo: This could use a generic alert library instead
alert('Data URL conversion failed: ' + err); // eslint-disable-line no-alert
document.getElementById('se-alert-dialog').title = 'Data URL conversion failed: ' + err;
data = '';
}
post({href, data});

View File

@ -39,8 +39,7 @@ async function processResults (url) {
// console.log('json', json);
if (!json || json.msg !== 'success') {
// Todo: This could use a generic alert library instead
alert('There was a problem downloading the results'); // eslint-disable-line no-alert
document.getElementById('se-alert-dialog').title = 'There was a problem downloading the results';
return;
}
const {payload, info: {

View File

@ -202,8 +202,8 @@ export default {
});
} catch (e) {
console.log('Failed loading MathJax.'); // eslint-disable-line no-console
// eslint-disable-next-line no-alert
alert('Failed loading MathJax. You will not be able to change the mathematics.');
// eslint-disable-next-line max-len
document.getElementById('se-alert-dialog').title = 'Failed loading MathJax. You will not be able to change the mathematics.';
}
}
}

View File

@ -64,8 +64,7 @@ export default {
<input type="hidden" name="contenttype" value="application/x-svgdraw">
`).appendTo('body')
.submit().remove();
// eslint-disable-next-line no-alert
alert(strings.saved);
document.getElementById('se-alert-dialog').title = strings.saved;
top.window.location = '/' + name;
}
});

View File

@ -63,8 +63,7 @@ export default {
} */
break;
case 'save-end':
// eslint-disable-next-line no-alert
alert(`save complete for pathID ${pathID}!`);
document.getElementById('se-alert-dialog').title = `save complete for pathID ${pathID}!`;
break;
default:
throw new Error('Unexpected WebAppFind event type');

View File

@ -156,7 +156,7 @@ class LayersPanel {
const newName = prompt(this.uiStrings.notification.enterUniqueLayerName, uniqName);
if (!newName) { return; }
if (this.svgCanvas.getCurrentDrawing().hasLayer(newName)) {
alert(this.uiStrings.notification.dupeLayerName);
document.getElementById('se-alert-dialog').title = this.uiStrings.notification.dupeLayerName;
return;
}
this.svgCanvas.createLayer(newName);
@ -190,7 +190,7 @@ class LayersPanel {
const newName = prompt(this.uiStrings.notification.enterUniqueLayerName, name);
if (!newName) { return; }
if (this.svgCanvas.getCurrentDrawing().hasLayer(newName)) {
alert(this.uiStrings.notification.dupeLayerName);
document.getElementById('se-alert-dialog').title = this.uiStrings.notification.dupeLayerName;
return;
}
this.svgCanvas.cloneLayer(newName);
@ -235,7 +235,7 @@ class LayersPanel {
const newName = prompt(this.uiStrings.notification.enterNewLayerName, '');
if (!newName) { return; }
if (oldName === newName || this.svgCanvas.getCurrentDrawing().hasLayer(newName)) {
alert(this.uiStrings.notification.layerHasThatName);
document.getElementById('se-alert-dialog').title = this.uiStrings.notification.layerHasThatName;
return;
}
this.svgCanvas.renameCurrentLayer(newName);

View File

@ -403,8 +403,7 @@ class TopPanelHandlers {
if (!valid) {
e.target.value = this.selectedElement().getAttribute(attr);
// eslint-disable-next-line no-alert
alert(this.uiStrings.notification.invalidAttrValGiven);
document.getElementById('se-alert-dialog').title = this.uiStrings.notification.invalidAttrValGiven;
return false;
}

View File

@ -222,6 +222,10 @@ editor.init = () => {
const dialogBox = document.createElement('se-cmenu_canvas-dialog');
dialogBox.setAttribute('id', 'se-cmenu_canvas');
document.body.append(dialogBox);
// alertDialog added to DOM
const alertBox = document.createElement('se-alert-dialog');
alertBox.setAttribute('id', 'se-alert-dialog');
document.body.append(alertBox);
} catch (err) {}
editor.configObj.load();
@ -436,7 +440,6 @@ editor.init = () => {
// Alert will only appear the first time saved OR the
// first time the bug is encountered
let done = editor.pref('save_notice_done');
if (done !== 'all') {
let note = uiStrings.notification.saveFromBrowser.replace('%s', 'SVG');
// Check if FF and has <defs/>
@ -453,7 +456,7 @@ editor.init = () => {
editor.pref('save_notice_done', 'all');
}
if (done !== 'part') {
alert(note);
document.getElementById('se-alert-dialog').title = note;
}
}
};
@ -470,7 +473,7 @@ editor.init = () => {
exportWindow = window.open(blankPageObjectURL || '', exportWindowName); // A hack to get the window via JSON-able name without opening a new one
if (!exportWindow || exportWindow.closed) {
alert(uiStrings.notification.popupWindowBlocked);
document.getElementById('se-alert-dialog').title = uiStrings.notification.popupWindowBlocked;
return;
}
@ -1289,7 +1292,7 @@ editor.init = () => {
// fired when user wants to move elements to another layer
let promptMoveLayerOnce = false;
$('#selLayerNames').change((evt) => {
$('#selLayerNames').change( async(evt) => {
const destLayer = evt.currentTarget.options[evt.currentTarget.selectedIndex].value;
const confirmStr = uiStrings.notification.QmoveElemsToLayer.replace('%s', destLayer);
/**
@ -1307,8 +1310,8 @@ editor.init = () => {
if (promptMoveLayerOnce) {
moveToLayer(true);
} else {
const ok = confirm(confirmStr);
if (!ok) {
const ok = await seConfirm(confirmStr, [uiStrings.common.ok, uiStrings.common.cancel]);
if (ok === uiStrings.common.cancel) {
return;
}
moveToLayer(true);
@ -1666,10 +1669,10 @@ editor.init = () => {
* @fires module:svgcanvas.SvgCanvas#event:ext_onNewDocument
* @returns {void}
*/
const clickClear = () => {
const clickClear = async() => {
const [x, y] = editor.configObj.curConfig.dimensions;
const ok = confirm(uiStrings.notification.QwantToClear);
if (!ok) {
const cancel = await seConfirm(uiStrings.notification.QwantToClear, [uiStrings.common.ok, uiStrings.common.cancel]);
if (cancel === uiStrings.common.cancel) {
return;
}
editor.leftPanelHandlers.clickSelect();
@ -1846,7 +1849,7 @@ editor.init = () => {
* @param {Event} e
* @returns {void} Resolves to `undefined`
*/
const saveSourceEditor = (e) => {
const saveSourceEditor = async (e) => {
const $editorDialog = document.getElementById('se-svg-editor-dialog');
if ($editorDialog.getAttribute('dialog') !== 'open') return;
const saveChanges = () => {
@ -1858,8 +1861,8 @@ editor.init = () => {
};
if (!svgCanvas.setSvgString(e.detail.value)) {
const ok = confirm(uiStrings.notification.QerrorsRevertToSource);
if (!ok) {
const resp = await seConfirm(uiStrings.notification.QerrorsRevertToSource, [uiStrings.common.ok, uiStrings.common.cancel]);
if (resp === uiStrings.common.cancel) {
return;
}
saveChanges();
@ -1901,15 +1904,15 @@ editor.init = () => {
svgCanvas.setDocumentTitle(title);
if (w !== 'fit' && !isValidUnit('width', w)) {
alert(uiStrings.notification.invalidAttrValGiven);
document.getElementById('se-alert-dialog').title = uiStrings.notification.invalidAttrValGiven;
return false;
}
if (h !== 'fit' && !isValidUnit('height', h)) {
alert(uiStrings.notification.invalidAttrValGiven);
document.getElementById('se-alert-dialog').title = uiStrings.notification.invalidAttrValGiven;
return false;
}
if (!svgCanvas.setResolution(w, h)) {
alert(uiStrings.notification.noContentToFitTo);
document.getElementById('se-alert-dialog').title = uiStrings.notification.noContentToFitTo;
return false;
}
// Set image save option
@ -1955,7 +1958,7 @@ editor.init = () => {
* @param {Event} e
* @returns {void} Resolves to `undefined`
*/
const cancelOverlays = (e) => {
const cancelOverlays = async (e) => {
$('#dialog_box').hide();
const $editorDialog = document.getElementById('se-svg-editor-dialog');
const editingsource = $editorDialog.getAttribute('dialog') === 'open';
@ -1969,8 +1972,8 @@ editor.init = () => {
if (editingsource) {
const origSource = svgCanvas.getSvgString();
if (origSource !== e.detail.value) {
const ok = confirm(uiStrings.notification.QignoreSourceChanges);
if (ok) {
const resp = await seConfirm(uiStrings.notification.QignoreSourceChanges, [uiStrings.common.ok, uiStrings.common.cancel]);
if (resp === uiStrings.common.ok) {
hideSourceEditor();
}
} else {
@ -2000,7 +2003,7 @@ editor.init = () => {
});
$('#url_notice').click(() => {
alert(this.title);
document.getElementById('se-alert-dialog').title = this.title;
});
$('#stroke_width').val(editor.configObj.curConfig.initStroke.width);
@ -2702,7 +2705,7 @@ editor.loadFromURL = function (url, {cache, noAlert} = {}) {
reject(new Error('URLLoadFail'));
return;
}
alert(uiStrings.notification.URLLoadFail + ': \n' + err);
document.getElementById('se-alert-dialog').title = uiStrings.notification.URLLoadFail + ': \n' + err;
resolve();
},
complete () {