-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[4.0] ES6 please, sampledata.js #21422
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /** | ||
| * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. | ||
| * @license GNU General Public License version 2 or later; see LICENSE.txt | ||
| */ | ||
| ((window, document, Joomla) => { | ||
| 'use strict'; | ||
|
|
||
| // eslint-disable-next-line no-unused-expressions,func-names | ||
| Joomla.SampleData = { | ||
| inProgress: false, | ||
| }; | ||
|
|
||
| Joomla.sampledataAjax = (type, steps, step) => { | ||
| // Get variables | ||
| const baseUrl = 'index.php?option=com_ajax&format=json&group=sampledata'; | ||
| const options = Joomla.getOptions('sample-data'); | ||
|
|
||
| // Create list | ||
| const list = document.createElement('li'); | ||
| list.classList.add(`sampledata-steps-${type}-${step}`); | ||
|
|
||
| // Create paragraph | ||
| const para = document.createElement('p'); | ||
| para.classList.add('loader-image'); | ||
| para.classList.add('text-center'); | ||
|
|
||
| // Create image | ||
| const img = document.createElement('img'); | ||
| img.setAttribute('src', options.icon); | ||
| img.setAttribute('width', 30); | ||
| img.setAttribute('height', 30); | ||
|
|
||
| // Append everything | ||
| para.appendChild(img); | ||
| list.appendChild(para); | ||
| document.querySelector(`.sampledata-progress-${type} ul`).appendChild(list); | ||
|
|
||
| Joomla.request({ | ||
| url: `${baseUrl}&type=${type}&plugin=SampledataApplyStep${step}&step=${step}`, | ||
| method: 'GET', | ||
| perform: true, | ||
| onSuccess: (resp) => { | ||
| const response = JSON.parse(resp); | ||
| let progressClass = ''; | ||
| let success; | ||
|
|
||
| // Remove loader image | ||
| const loader = list.querySelector('.loader-image'); | ||
| loader.parentNode.removeChild(loader); | ||
|
|
||
| if (response.success && response.data && response.data.length > 0) { | ||
| const progress = document.querySelector(`.sampledata-progress-${type} .progress-bar`); | ||
|
|
||
| // Display all messages that we got | ||
| response.data.forEach((value) => { | ||
| if (value === null) { | ||
| return; | ||
| } | ||
|
|
||
| success = value.success; | ||
| progressClass = success ? 'bg-success' : 'bg-danger'; | ||
|
|
||
| // Display success alert | ||
| if (success) { | ||
| Joomla.renderMessages({ success: [value.message] }, `.sampledata-steps-${type}-${step}`); | ||
| } else { | ||
| Joomla.renderMessages({ error: [value.message] }, `.sampledata-steps-${type}-${step}`); | ||
| } | ||
| }); | ||
|
|
||
| // Update progress | ||
| progress.innerText = `${step}/${steps}`; | ||
| progress.style.width = `${(step / steps) * 100}%`; | ||
| progress.classList.add(progressClass); | ||
|
|
||
| // Move on next step | ||
| if (success && (step <= steps)) { | ||
| const stepNew = step + 1; | ||
| if (stepNew <= steps) { | ||
| Joomla.sampledataAjax(type, steps, stepNew); | ||
| } | ||
| } | ||
| } else { | ||
| // Display error alert | ||
| Joomla.renderMessages({ error: [Joomla.JText._('MOD_SAMPLEDATA_INVALID_RESPONSE')] }, `.sampledata-steps-${type}-${step}`); | ||
|
|
||
| Joomla.SampleData.inProgress = false; | ||
| } | ||
| }, | ||
| onError: () => { | ||
| alert('Something went wrong! Please close and reopen the browser and try again!'); | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| Joomla.sampledataApply = (element) => { | ||
| const type = element.getAttribute('data-type'); | ||
| const steps = element.getAttribute('data-steps'); | ||
|
|
||
| // Check whether the work in progress or we already processed with current item | ||
| if (Joomla.SampleData.inProgress) { | ||
| return; | ||
| } | ||
|
|
||
| if (element.getAttribute('data-processed')) { | ||
| alert(Joomla.JText._('MOD_SAMPLEDATA_ITEM_ALREADY_PROCESSED')); | ||
| return; | ||
| } | ||
|
|
||
| // Make sure that use run this not by random clicking on the page links | ||
| if (!confirm(Joomla.JText._('MOD_SAMPLEDATA_CONFIRM_START'))) { | ||
|
||
| // eslint-disable-next-line consistent-return | ||
| return false; | ||
| } | ||
|
|
||
| // Turn on the progress container | ||
| const progressElements = [].slice.call(document.querySelectorAll(`.sampledata-progress-${type}`)); | ||
|
|
||
| progressElements.forEach((progressElement) => { | ||
| progressElement.classList.remove('d-none'); | ||
| }); | ||
|
|
||
| element.getAttribute('data-processed', true); | ||
|
|
||
| Joomla.SampleData.inProgress = true; | ||
| Joomla.sampledataAjax(type, steps, 1); | ||
|
|
||
| // eslint-disable-next-line consistent-return | ||
| return false; | ||
| }; | ||
|
|
||
| const onBoot = () => { | ||
| const sampleDataWrapper = document.getElementById('sample-data-wrapper'); | ||
| if (sampleDataWrapper) { | ||
| const links = [].slice.call(sampleDataWrapper.querySelectorAll('.apply-sample-data')); | ||
| links.forEach((link) => { | ||
| link.addEventListener('click', event => Joomla.sampledataApply(event.target)); | ||
| }); | ||
| } | ||
|
|
||
| // Cleanup | ||
| document.removeEventListener('DOMContentLoaded', onBoot); | ||
| }; | ||
|
|
||
| // Initialise | ||
| document.addEventListener('DOMContentLoaded', onBoot); | ||
| })(window, document, window.Joomla); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use object destructuring prefer-destructuring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, deal with it