first commit
This commit is contained in:
173
media/mod_sampledata/js/sampledata-process.js
Normal file
173
media/mod_sampledata/js/sampledata-process.js
Normal file
@ -0,0 +1,173 @@
|
||||
/**
|
||||
* @copyright (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
const SampleData = {
|
||||
inProgress: false
|
||||
};
|
||||
const sampledataAjax = (type, steps, step) => {
|
||||
// Get variables
|
||||
const baseUrl = `index.php?option=com_ajax&format=json&group=sampledata&${Joomla.getOptions('csrf.token')}=1`;
|
||||
const options = Joomla.getOptions('sample-data');
|
||||
|
||||
// Create list
|
||||
const list = document.createElement('div');
|
||||
list.classList.add(`sampledata-steps-${type}-${step}`);
|
||||
list.setAttribute('role', 'region');
|
||||
list.setAttribute('aria-live', 'polite');
|
||||
|
||||
// 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}`).appendChild(list);
|
||||
Joomla.request({
|
||||
url: `${baseUrl}&type=${type}&plugin=SampledataApplyStep${step}&step=${step}`,
|
||||
method: 'GET',
|
||||
perform: true,
|
||||
onSuccess: resp => {
|
||||
// Remove loader image
|
||||
const loader = list.querySelector('.loader-image');
|
||||
loader.parentNode.removeChild(loader);
|
||||
let response = {};
|
||||
try {
|
||||
response = JSON.parse(resp);
|
||||
} catch (e) {
|
||||
Joomla.renderMessages({
|
||||
error: [Joomla.Text._('MOD_SAMPLEDATA_INVALID_RESPONSE')]
|
||||
}, `.sampledata-steps-${type}-${step}`);
|
||||
SampleData.inProgress = false;
|
||||
return;
|
||||
}
|
||||
let progressClass = '';
|
||||
let success;
|
||||
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;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line prefer-destructuring
|
||||
success = value.success;
|
||||
progressClass = success ? 'bg-success' : 'bg-danger';
|
||||
|
||||
// Display success alert
|
||||
if (success) {
|
||||
Joomla.renderMessages({
|
||||
message: [value.message]
|
||||
}, `.sampledata-steps-${type}-${step}`, false, 3000);
|
||||
} else {
|
||||
Joomla.renderMessages({
|
||||
error: [value.message]
|
||||
}, `.sampledata-steps-${type}-${step}`, false);
|
||||
}
|
||||
});
|
||||
|
||||
// Update progress
|
||||
progress.innerText = `${step}/${steps}`;
|
||||
progress.style.width = `${step / steps * 100}%`;
|
||||
progress.setAttribute('aria-valuemin', 0);
|
||||
progress.setAttribute('aria-valuemax', 100);
|
||||
progress.setAttribute('aria-valuenow', step / steps * 100);
|
||||
progress.classList.add(progressClass);
|
||||
|
||||
// Move on next step
|
||||
if (success && step <= steps) {
|
||||
const stepNew = step + 1;
|
||||
if (stepNew <= steps) {
|
||||
sampledataAjax(type, steps, stepNew);
|
||||
} else {
|
||||
const bar = document.querySelector(`.sampledata-progress-${type}`);
|
||||
bar.parentNode.removeChild(bar);
|
||||
Joomla.renderMessages({
|
||||
message: [Joomla.Text._('MOD_SAMPLEDATA_COMPLETED')]
|
||||
});
|
||||
window.scroll({
|
||||
top: 0,
|
||||
left: 0,
|
||||
behavior: 'smooth'
|
||||
});
|
||||
SampleData.inProgress = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Display error alert
|
||||
Joomla.renderMessages({
|
||||
error: [Joomla.Text._('MOD_SAMPLEDATA_INVALID_RESPONSE')]
|
||||
});
|
||||
window.scroll({
|
||||
top: 0,
|
||||
left: 0,
|
||||
behavior: 'smooth'
|
||||
});
|
||||
SampleData.inProgress = false;
|
||||
}
|
||||
},
|
||||
onError: () => {
|
||||
Joomla.renderMessages({
|
||||
error: [Joomla.Text._('MOD_SAMPLEDATA_ERROR_RESPONSE')]
|
||||
});
|
||||
window.scroll({
|
||||
top: 0,
|
||||
left: 0,
|
||||
behavior: 'smooth'
|
||||
});
|
||||
SampleData.inProgress = false;
|
||||
}
|
||||
});
|
||||
};
|
||||
const 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 (SampleData.inProgress) {
|
||||
return;
|
||||
}
|
||||
if (element.getAttribute('data-processed')) {
|
||||
alert(Joomla.Text._('MOD_SAMPLEDATA_ITEM_ALREADY_PROCESSED'));
|
||||
SampleData.inProgress = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure that use run this not by random clicking on the page links
|
||||
// @todo use the CE Modal here
|
||||
if (!window.confirm(Joomla.Text._('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.setAttribute('data-processed', true);
|
||||
SampleData.inProgress = true;
|
||||
sampledataAjax(type, steps, 1);
|
||||
|
||||
// eslint-disable-next-line consistent-return
|
||||
return false;
|
||||
};
|
||||
const sampleDataWrapper = document.getElementById('sample-data-wrapper');
|
||||
if (sampleDataWrapper) {
|
||||
const links = [].slice.call(sampleDataWrapper.querySelectorAll('.apply-sample-data'));
|
||||
links.forEach(link => {
|
||||
link.addEventListener('click', ({
|
||||
currentTarget
|
||||
}) => sampledataApply(currentTarget));
|
||||
});
|
||||
}
|
||||
1
media/mod_sampledata/js/sampledata-process.min.js
vendored
Normal file
1
media/mod_sampledata/js/sampledata-process.min.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
const SampleData={inProgress:!1},sampledataAjax=(e,a,t)=>{const s=`index.php?option=com_ajax&format=json&group=sampledata&${Joomla.getOptions("csrf.token")}=1`,r=Joomla.getOptions("sample-data"),o=document.createElement("div");o.classList.add(`sampledata-steps-${e}-${t}`),o.setAttribute("role","region"),o.setAttribute("aria-live","polite");const l=document.createElement("p");l.classList.add("loader-image"),l.classList.add("text-center");const p=document.createElement("img");p.setAttribute("src",r.icon),p.setAttribute("width",30),p.setAttribute("height",30),l.appendChild(p),o.appendChild(l),document.querySelector(`.sampledata-progress-${e}`).appendChild(o),Joomla.request({url:`${s}&type=${e}&plugin=SampledataApplyStep${t}&step=${t}`,method:"GET",perform:!0,onSuccess:s=>{const r=o.querySelector(".loader-image");r.parentNode.removeChild(r);let l={};try{l=JSON.parse(s)}catch(a){return Joomla.renderMessages({error:[Joomla.Text._("MOD_SAMPLEDATA_INVALID_RESPONSE")]},`.sampledata-steps-${e}-${t}`),void(SampleData.inProgress=!1)}let p,d="";if(l.success&&l.data&&l.data.length>0){const s=document.querySelector(`.sampledata-progress-${e} .progress-bar`);if(l.data.forEach((a=>{null!==a&&(p=a.success,d=p?"bg-success":"bg-danger",p?Joomla.renderMessages({message:[a.message]},`.sampledata-steps-${e}-${t}`,!1,3e3):Joomla.renderMessages({error:[a.message]},`.sampledata-steps-${e}-${t}`,!1))})),s.innerText=`${t}/${a}`,s.style.width=t/a*100+"%",s.setAttribute("aria-valuemin",0),s.setAttribute("aria-valuemax",100),s.setAttribute("aria-valuenow",t/a*100),s.classList.add(d),p&&t<=a){const s=t+1;if(s<=a)sampledataAjax(e,a,s);else{const a=document.querySelector(`.sampledata-progress-${e}`);a.parentNode.removeChild(a),Joomla.renderMessages({message:[Joomla.Text._("MOD_SAMPLEDATA_COMPLETED")]}),window.scroll({top:0,left:0,behavior:"smooth"}),SampleData.inProgress=!1}}}else Joomla.renderMessages({error:[Joomla.Text._("MOD_SAMPLEDATA_INVALID_RESPONSE")]}),window.scroll({top:0,left:0,behavior:"smooth"}),SampleData.inProgress=!1},onError:()=>{Joomla.renderMessages({error:[Joomla.Text._("MOD_SAMPLEDATA_ERROR_RESPONSE")]}),window.scroll({top:0,left:0,behavior:"smooth"}),SampleData.inProgress=!1}})},sampledataApply=e=>{const a=e.getAttribute("data-type"),t=e.getAttribute("data-steps");if(SampleData.inProgress)return;if(e.getAttribute("data-processed"))return alert(Joomla.Text._("MOD_SAMPLEDATA_ITEM_ALREADY_PROCESSED")),void(SampleData.inProgress=!1);if(!window.confirm(Joomla.Text._("MOD_SAMPLEDATA_CONFIRM_START")))return!1;return[].slice.call(document.querySelectorAll(`.sampledata-progress-${a}`)).forEach((e=>{e.classList.remove("d-none")})),e.setAttribute("data-processed",!0),SampleData.inProgress=!0,sampledataAjax(a,t,1),!1},sampleDataWrapper=document.getElementById("sample-data-wrapper");if(sampleDataWrapper){[].slice.call(sampleDataWrapper.querySelectorAll(".apply-sample-data")).forEach((e=>{e.addEventListener("click",(({currentTarget:e})=>sampledataApply(e)))}))}
|
||||
BIN
media/mod_sampledata/js/sampledata-process.min.js.gz
Normal file
BIN
media/mod_sampledata/js/sampledata-process.min.js.gz
Normal file
Binary file not shown.
Reference in New Issue
Block a user