-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[4.0] Decouple messages from core.js #32747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
The head ref may contain hidden characters: "\u2014messages\u20144.0-dev"
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0b7552b
init
dgrammatiko 7e85328
Progressive enhancement
dgrammatiko 9027315
Obviously these strings are now always available in the joomla.storag…
dgrammatiko 7dec40f
Check if errorrs are ok since #32783
dgrammatiko a324052
My bad then
dgrammatiko 2bbd619
noscript wrap
dgrammatiko 7a36b85
Merge branch '4.0-dev' into —messages—4.0-dev
dgrammatiko d16bcf6
Merge branch '4.0-dev' into —messages—4.0-dev
dgrammatiko cf88e16
Fix multiple instances of alerts
dgrammatiko 9c29f8c
Wrong colours, data attributes are safe
dgrammatiko 3f31bd7
clean up
dgrammatiko 455d218
CS
dgrammatiko 9336e37
Bring back the old types
dgrammatiko c3e9760
Merge branch '4.0-dev' into —messages—4.0-dev
dgrammatiko cdcdf66
this also
dgrammatiko aa80075
Merge branch '4.0-dev' into —messages—4.0-dev
dgrammatiko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,123 @@ | ||
| /** | ||
| * @copyright (C) 2020 Open Source Matters, Inc. <https://www.joomla.org> | ||
| * @license GNU General Public License version 2 or later; see LICENSE.txt | ||
| */ | ||
|
|
||
| // Import the Alert Custom Element | ||
| import 'joomla-ui-custom-elements/src/js/alert/alert.js'; | ||
|
|
||
| /** | ||
| * Returns the container of the Messages | ||
| * | ||
| * @param {string|HTMLElement} container The container | ||
| * | ||
| * @returns {HTMLElement} | ||
| */ | ||
| const getMessageContainer = (container) => { | ||
| let messageContainer; | ||
|
|
||
| if (container instanceof HTMLElement) { | ||
| return container; | ||
| } | ||
| if (typeof container === 'undefined' || (container && container === '#system-message-container')) { | ||
| messageContainer = document.getElementById('system-message-container'); | ||
| } else { | ||
| messageContainer = document.querySelector(container); | ||
| } | ||
|
|
||
| return messageContainer; | ||
| }; | ||
|
|
||
| /** | ||
| * Render messages send via JSON | ||
| * Used by some javascripts such as validate.js | ||
| * | ||
| * @param {object} messages JavaScript object containing the messages to render. | ||
| * Example: | ||
| * const messages = { | ||
| * "message": ["This will be a green message", "So will this"], | ||
| * "error": ["This will be a red message", "So will this"], | ||
| * "info": ["This will be a blue message", "So will this"], | ||
| * "notice": ["This will be same as info message", "So will this"], | ||
| * "warning": ["This will be a orange message", "So will this"], | ||
| * "my_custom_type": ["This will be same as info message", "So will this"] | ||
| * }; | ||
| * @param {string} selector The selector of the container where the message will be rendered | ||
| * @param {bool} keepOld If we shall discard old messages | ||
| * @param {int} timeout The milliseconds before the message self destruct | ||
| * @return void | ||
| */ | ||
| Joomla.renderMessages = (messages, selector, keepOld, timeout) => { | ||
| const messageContainer = getMessageContainer(selector); | ||
| if (typeof keepOld === 'undefined' || (keepOld && keepOld === false)) { | ||
| Joomla.removeMessages(messageContainer); | ||
| } | ||
|
|
||
| [].slice.call(Object.keys(messages)).forEach((type) => { | ||
| let alertClass = type; | ||
|
|
||
| // Array of messages of this type | ||
| const typeMessages = messages[type]; | ||
| const messagesBox = document.createElement('joomla-alert'); | ||
|
|
||
| if (['success', 'info', 'danger', 'warning'].indexOf(type) < 0) { | ||
| alertClass = (type === 'notice') ? 'info' : type; | ||
| alertClass = (type === 'message') ? 'success' : alertClass; | ||
| alertClass = (type === 'error') ? 'danger' : alertClass; | ||
| alertClass = (type === 'warning') ? 'warning' : alertClass; | ||
| } | ||
|
|
||
| messagesBox.setAttribute('type', alertClass); | ||
| messagesBox.setAttribute('dismiss', true); | ||
|
|
||
| if (timeout && parseInt(timeout, 10) > 0) { | ||
| messagesBox.setAttribute('auto-dismiss', timeout); | ||
| } | ||
|
|
||
| // Title | ||
| const title = Joomla.Text._(type); | ||
|
|
||
| // Skip titles with untranslated strings | ||
| if (typeof title !== 'undefined') { | ||
| const titleWrapper = document.createElement('div'); | ||
| titleWrapper.className = 'alert-heading'; | ||
| titleWrapper.innerHTML = Joomla.sanitizeHtml(`<span class="${type}"></span><span class="visually-hidden">${Joomla.Text._(type) ? Joomla.Text._(type) : type}</span>`); | ||
| messagesBox.appendChild(titleWrapper); | ||
| } | ||
|
|
||
| // Add messages to the message box | ||
| const messageWrapper = document.createElement('div'); | ||
| messageWrapper.className = 'alert-wrapper'; | ||
| typeMessages.forEach((typeMessage) => { | ||
| messageWrapper.innerHTML += Joomla.sanitizeHtml(`<div class="alert-message">${typeMessage}</div>`); | ||
| }); | ||
| messagesBox.appendChild(messageWrapper); | ||
| messageContainer.appendChild(messagesBox); | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Remove messages | ||
| * | ||
| * @param {element} container The element of the container of the message | ||
| * to be removed | ||
| * | ||
| * @return {void} | ||
| */ | ||
| Joomla.removeMessages = (container) => { | ||
| const messageContainer = getMessageContainer(container); | ||
| const alerts = [].slice.call(messageContainer.querySelectorAll('joomla-alert')); | ||
| if (alerts.length) { | ||
| alerts.forEach((alert) => { | ||
| alert.close(); | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener('DOMContentLoaded', () => { | ||
| const messages = Joomla.getOptions('joomla.messages'); | ||
| if (messages) { | ||
| Object.keys(messages) | ||
| .map((message) => Joomla.renderMessages(messages[message], undefined, true, undefined)); | ||
| } | ||
| }); | ||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.