-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move alert to dedicated module
- Loading branch information
1 parent
03f6080
commit a6975cf
Showing
12 changed files
with
135 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { DomUtil, DomEvent } from '../../../vendors/leaflet/leaflet-src.esm.js' | ||
import { translate } from '../i18n.js' | ||
|
||
const ALERTS = [] | ||
let ALERT_ID = null | ||
|
||
export default class Alert { | ||
constructor(map) { | ||
this.parent = map._controlContainer | ||
this.map = map | ||
this.container = DomUtil.create('div', 'with-transition', this.parent) | ||
this.container.id = 'umap-alert-container' | ||
DomEvent.disableClickPropagation(this.container) | ||
DomEvent.on(this.container, 'contextmenu', DomEvent.stopPropagation) // Do not activate our custom context menu. | ||
DomEvent.on(this.container, 'wheel', DomEvent.stopPropagation) | ||
DomEvent.on(this.container, 'MozMousePixelScroll', DomEvent.stopPropagation) | ||
} | ||
|
||
open(params) { | ||
if (DomUtil.hasClass(this.parent, 'umap-alert')) ALERTS.push(params) | ||
else this._open(params) | ||
} | ||
|
||
_open(params) { | ||
if (!params) { | ||
if (ALERTS.length) params = ALERTS.pop() | ||
else return | ||
} | ||
let timeoutID | ||
const level_class = params.level && params.level == 'info' ? 'info' : 'error' | ||
this.container.innerHTML = '' | ||
DomUtil.addClass(this.parent, 'umap-alert') | ||
DomUtil.addClass(this.container, level_class) | ||
const close = () => { | ||
if (timeoutID && timeoutID !== ALERT_ID) { | ||
return | ||
} // Another alert has been forced | ||
this.container.innerHTML = '' | ||
DomUtil.removeClass(this.parent, 'umap-alert') | ||
DomUtil.removeClass(this.container, level_class) | ||
if (timeoutID) window.clearTimeout(timeoutID) | ||
this._open() | ||
} | ||
const closeButton = DomUtil.createButton( | ||
'umap-close-link', | ||
this.container, | ||
'', | ||
close, | ||
this | ||
) | ||
DomUtil.create('i', 'umap-close-icon', closeButton) | ||
const label = DomUtil.create('span', '', closeButton) | ||
label.title = label.textContent = translate('Close') | ||
DomUtil.element({ | ||
tagName: 'div', | ||
innerHTML: params.content, | ||
parent: this.container, | ||
}) | ||
this.addActions(params.actions) | ||
if (params.duration !== Infinity) { | ||
ALERT_ID = timeoutID = window.setTimeout(close, params.duration || 3000) | ||
} | ||
} | ||
|
||
addActions(actions) { | ||
actions = actions || [] | ||
let action, el, input | ||
const form = DomUtil.create('div', 'umap-alert-actions', this.container) | ||
for (let action of actions) { | ||
if (action.input) { | ||
input = DomUtil.element({ | ||
tagName: 'input', | ||
parent: form, | ||
className: 'umap-alert-input', | ||
placeholder: action.input, | ||
}) | ||
} | ||
el = DomUtil.createButton( | ||
'umap-action', | ||
form, | ||
action.label, | ||
action.callback, | ||
action.callbackContext || this.map | ||
) | ||
DomEvent.on(el, 'click', close, this) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.