import Component from '../lib/component.js'; import store from '../store/index.js'; /** * @classdesc UI component for the sets of environments. */ export default class EnvsSet extends Component{ /** * @constructor */ constructor() { super({ store, element: document.querySelector('#getting-started-tab'), eventName: 'envsSetChange' }); } /** * Renders the sets of environments and adds event listeners to the different elements. */ render(){ let dict = window.lang_dict[store.state.language]['envsSets']; this.element.querySelector('#baseSetText').innerHTML = dict['baseSetText']; this.element.querySelector('#customSetText').innerHTML = dict['customSetText']; /* Base envs set */ let baseEnvs = this.element.querySelector('#baseEnvsSet'); baseEnvs.innerHTML = store.state.envsSets.baseEnvsSet.map(e => { // Creates a card for each environment in the base set return `
${e.description.name}

${e.description[store.state.language]['name']}

${e.description[store.state.language]['text']}

` }).join(''); /* Custom envs set */ let customEnvs = this.element.querySelector('#customEnvsSet'); // Creates a special card for uploading files let uploadCard = `

${dict['uploadCard']['title']}

${dict['uploadCard']['text']}

`; let envCards = store.state.envsSets.customEnvsSet.map((e, index) => { // Creates a card for each environment in the custom set return `
${e.description.name}

${e.description.name}

${e.description.text}

` }).join(''); customEnvs.innerHTML = [uploadCard, envCards].join(''); /* EVENT LISTENERS */ // Loads a base environment baseEnvs.querySelectorAll('div[name="env-card"]').forEach((span, index) => { span.addEventListener('click', () => { store.dispatch('loadEnv', store.state.envsSets.baseEnvsSet[index]); }) }); // Loads a custom environment customEnvs.querySelectorAll('div[name="env-card"]').forEach((span, index) => { span.addEventListener('click', () => { store.dispatch('loadEnv', store.state.envsSets.customEnvsSet[index]); }) }); // Parses the uploaded file and adds the corresponding environment to the custom set customEnvs.querySelector('#uploadEnvBtn').addEventListener('click', () => { let reader = new FileReader(); reader.addEventListener('load', () => { let env = JSON.parse(reader.result); store.dispatch('addEnv',{set: 'custom', env: env}); }); let file = customEnvs.querySelector('#uploadEnvFile').files[0]; if(file != null){ reader.readAsText(file); } }); // Downloads a custom environment customEnvs.querySelectorAll('button[name="downloadEnvBtn"]').forEach((span, index) => { span.addEventListener('click', () => { downloadObjectAsJson(store.state.envsSets.customEnvsSet[index], store.state.envsSets.customEnvsSet[index].description.name.split(' ').join('_')); }) }); // Deletes a custom environment customEnvs.querySelectorAll('button[name="deleteEnvBtn"]').forEach((span, index) => { span.addEventListener('click', () => { store.dispatch('deleteEnv', index); }) }); /* Initializes tooltips */ this.element.querySelectorAll('[data-bs-toggle="tooltip"]').forEach((el, index) => { return new bootstrap.Tooltip(el, { trigger: 'hover' }); }); } };