[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/media/plg_installer_webinstaller/js/ -> client.js (source)

   1  /**
   2   * @copyright  (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
   3   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   4   */
   5  if (!Joomla) {
   6    throw new Error('Joomla API is not properly initialised');
   7  }
   8  
   9  const allowList = {
  10    button: ['type'],
  11    input: ['type', 'name', 'placeholder', 'inputmode'],
  12    select: ['name'],
  13    option: ['value', 'selected']
  14  };
  15  const webInstallerOptions = {
  16    view: 'dashboard',
  17    id: 0,
  18    ordering: '',
  19    version: 'current',
  20    list: 0,
  21    options: Joomla.getOptions('plg_installer_webinstaller', {})
  22  };
  23  let instance;
  24  
  25  class WebInstaller {
  26    initialise() {
  27      webInstallerOptions.loaded = 1;
  28      const cancelButton = document.getElementById('uploadform-web-cancel');
  29      cancelButton.addEventListener('click', () => {
  30        document.getElementById('uploadform-web').classList.add('hidden');
  31  
  32        if (webInstallerOptions.list && document.querySelector('.list-view')) {
  33          document.querySelector('.list-view').click();
  34        }
  35      });
  36      const installButton = document.getElementById('uploadform-web-install');
  37      installButton.addEventListener('click', () => {
  38        if (webInstallerOptions.options.installFrom === 4) {
  39          this.submitButtonUrl();
  40        } else {
  41          this.submitButtonWeb();
  42        }
  43      });
  44      this.loadweb(`$webInstallerOptions.options.base_url}index.php?format=json&option=com_apps&view=dashboard`);
  45      this.clickforlinks();
  46    }
  47  
  48    loadweb(url) {
  49      if (!url) {
  50        return false;
  51      } // eslint-disable-next-line prefer-regex-literals
  52  
  53  
  54      const pattern1 = new RegExp(webInstallerOptions.options.base_url);
  55      const pattern2 = /^index\.php/;
  56  
  57      if (!(pattern1.test(url) || pattern2.test(url))) {
  58        window.open(url, '_blank');
  59        return false;
  60      }
  61  
  62      let requestUrl = `$url}&product=$webInstallerOptions.options.product}&release=$webInstallerOptions.options.release}&dev_level=$webInstallerOptions.options.dev_level}&list=$webInstallerOptions.list ? 'list' : 'grid'}&lang=$webInstallerOptions.options.language}`;
  63      const orderingSelect = document.getElementById('com-apps-ordering');
  64      const versionSelect = document.getElementById('com-apps-filter-joomla-version');
  65  
  66      if (webInstallerOptions.ordering !== '' && orderingSelect && orderingSelect.value) {
  67        webInstallerOptions.ordering = orderingSelect.value;
  68        requestUrl += `&ordering=$webInstallerOptions.ordering}`;
  69      }
  70  
  71      if (webInstallerOptions.version !== '' && versionSelect && versionSelect.value) {
  72        webInstallerOptions.version = versionSelect.value;
  73        requestUrl += `&filter_version=$webInstallerOptions.version}`;
  74      }
  75  
  76      WebInstaller.showLoadingLayer();
  77      new Promise((resolve, reject) => {
  78        Joomla.request({
  79          url: requestUrl,
  80          onSuccess: resp => {
  81            let response;
  82  
  83            try {
  84              response = JSON.parse(resp);
  85            } catch (error) {
  86              throw new Error('Failed to parse JSON');
  87            }
  88  
  89            if (document.getElementById('web-loader')) {
  90              document.getElementById('web-loader').classList.add('hidden');
  91            }
  92  
  93            const jedContainer = document.getElementById('jed-container');
  94            jedContainer.innerHTML = Joomla.sanitizeHtml(response.data.html, allowList);
  95            document.getElementById('com-apps-searchbox').addEventListener('keypress', ({
  96              which
  97            }) => {
  98              if (which === 13) {
  99                this.initiateSearch();
 100              }
 101            });
 102            document.getElementById('search-extensions').addEventListener('click', () => {
 103              this.initiateSearch();
 104            });
 105            document.getElementById('search-reset').addEventListener('click', () => {
 106              const searchBox = document.getElementById('com-apps-searchbox');
 107              searchBox.value = '';
 108              this.initiateSearch();
 109              document.getElementById('search-reset').setAttribute('disabled', 'disabled');
 110            });
 111  
 112            if (document.getElementById('com-apps-searchbox').value === '') {
 113              document.getElementById('search-reset').setAttribute('disabled', 'disabled');
 114            }
 115  
 116            document.getElementById('search-reset').innerHTML = Joomla.sanitizeHtml(Joomla.Text._('JSEARCH_FILTER_CLEAR')); // eslint-disable-next-line no-shadow
 117  
 118            const orderingSelect = document.getElementById('com-apps-ordering'); // eslint-disable-next-line no-shadow
 119  
 120            const versionSelect = document.getElementById('com-apps-filter-joomla-version');
 121  
 122            if (orderingSelect) {
 123              orderingSelect.addEventListener('change', () => {
 124                const index = orderingSelect.selectedIndex;
 125                webInstallerOptions.ordering = orderingSelect.options[index].value;
 126                this.installfromwebajaxsubmit();
 127              });
 128            }
 129  
 130            if (versionSelect) {
 131              versionSelect.addEventListener('change', () => {
 132                const index = versionSelect.selectedIndex;
 133                webInstallerOptions.version = versionSelect.options[index].value;
 134                this.installfromwebajaxsubmit();
 135              });
 136            }
 137  
 138            if (webInstallerOptions.options.installfrom_url !== '') {
 139              WebInstaller.installfromweb(webInstallerOptions.options.installfrom_url);
 140            }
 141  
 142            resolve();
 143          },
 144          onError: request => {
 145            const errorContainer = document.getElementById('web-loader-error');
 146            const loaderContainer = document.getElementById('web-loader');
 147  
 148            if (request.responseText && errorContainer) {
 149              errorContainer.innerHTML = Joomla.sanitizeHtml(request.responseText);
 150            }
 151  
 152            if (loaderContainer) {
 153              loaderContainer.classList.add('hidden');
 154              errorContainer.classList.remove('hidden');
 155            }
 156  
 157            Joomla.renderMessages({
 158              danger: [Joomla.Text._('PLG_INSTALLER_WEBINSTALLER_INSTALL_WEB_LOADING_ERROR')]
 159            }, '#web-loader-error');
 160            reject();
 161          }
 162        });
 163      }).finally(() => {
 164        // Promise has been settled.
 165        // Run the following whether or not it was a success.
 166        const installAtField = document.getElementById('joomlaapsinstallatinput');
 167  
 168        if (installAtField) {
 169          installAtField.value = webInstallerOptions.options.installat_url;
 170        }
 171  
 172        this.clickforlinks();
 173        WebInstaller.clicker();
 174  
 175        if (webInstallerOptions.view !== 'extension') {
 176          [].slice.call(document.querySelectorAll('div.load-extension')).forEach(element => {
 177            element.addEventListener('click', event => {
 178              event.preventDefault();
 179              this.processLinkClick(element.getAttribute('data-url'));
 180            });
 181            element.setAttribute('href', '#');
 182          });
 183        }
 184  
 185        if (webInstallerOptions.view === 'extension') {
 186          const installExtensionButton = document.getElementById('install-extension');
 187          const installExtensionFromExternalButton = document.getElementById('install-extension-from-external');
 188  
 189          if (installExtensionButton) {
 190            installExtensionButton.addEventListener('click', () => {
 191              WebInstaller.installfromweb(installExtensionButton.getAttribute('data-downloadurl'), installExtensionButton.getAttribute('data-name'));
 192              document.getElementById('uploadform-web-install').scrollIntoView({
 193                behavior: 'smooth',
 194                block: 'start'
 195              });
 196            });
 197          }
 198  
 199          if (installExtensionFromExternalButton) {
 200            installExtensionFromExternalButton.addEventListener('click', () => {
 201              const redirectUrl = installExtensionFromExternalButton.getAttribute('data-downloadurl');
 202              const redirectConfirm = window.confirm(Joomla.Text._('PLG_INSTALLER_WEBINSTALLER_REDIRECT_TO_EXTERNAL_SITE_TO_INSTALL').replace('[SITEURL]', redirectUrl));
 203  
 204              if (redirectConfirm !== true) {
 205                return;
 206              }
 207  
 208              document.getElementById('adminForm').setAttribute('action', redirectUrl);
 209              document.querySelector('input[name=task]').setAttribute('disabled', true);
 210              document.querySelector('input[name=install_directory]').setAttribute('disabled', true);
 211              document.querySelector('input[name=install_url]').setAttribute('disabled', true);
 212              document.querySelector('input[name=installtype]').setAttribute('disabled', true);
 213              document.querySelector('input[name=filter_search]').setAttribute('disabled', true);
 214              document.getElementById('adminForm').submit();
 215            });
 216          }
 217        }
 218  
 219        if (webInstallerOptions.list && document.querySelector('.list-view')) {
 220          document.querySelector('.list-view').click();
 221        }
 222  
 223        WebInstaller.hideLoadingLayer();
 224      });
 225      return true;
 226    }
 227  
 228    clickforlinks() {
 229      [].slice.call(document.querySelectorAll('a.transcode')).forEach(element => {
 230        const ajaxurl = element.getAttribute('href');
 231        element.addEventListener('click', event => {
 232          event.preventDefault();
 233          this.processLinkClick(ajaxurl);
 234        });
 235        element.setAttribute('href', '#');
 236      });
 237    }
 238  
 239    initiateSearch() {
 240      document.getElementById('search-reset').removeAttribute('disabled');
 241      webInstallerOptions.view = 'dashboard';
 242      this.installfromwebajaxsubmit();
 243    }
 244  
 245    installfromwebajaxsubmit() {
 246      let tail = `&view=$webInstallerOptions.view}`;
 247  
 248      if (webInstallerOptions.id) {
 249        tail += `&id=$webInstallerOptions.id}`;
 250      }
 251  
 252      if (document.getElementById('com-apps-searchbox').value) {
 253        const value = encodeURI(document.getElementById('com-apps-searchbox').value.toLowerCase().replace(/ +/g, '_').replace(/[^a-z0-9-_]/g, '').trim());
 254        tail += `&filter_search=$value}`;
 255      }
 256  
 257      const orderingSelect = document.getElementById('com-apps-ordering');
 258      const versionSelect = document.getElementById('com-apps-filter-joomla-version');
 259  
 260      if (webInstallerOptions.ordering !== '' && orderingSelect && orderingSelect.value) {
 261        webInstallerOptions.ordering = orderingSelect.value;
 262      }
 263  
 264      if (webInstallerOptions.ordering) {
 265        tail += `&ordering=$webInstallerOptions.ordering}`;
 266      }
 267  
 268      if (webInstallerOptions.version !== '' && versionSelect && versionSelect.value) {
 269        webInstallerOptions.version = versionSelect.value;
 270      }
 271  
 272      if (webInstallerOptions.version) {
 273        tail += `&filter_version=$webInstallerOptions.version}`;
 274      }
 275  
 276      this.loadweb(`$webInstallerOptions.options.base_url}index.php?format=json&option=com_apps$tail}`);
 277    }
 278  
 279    processLinkClick(url) {
 280      const pattern1 = new RegExp(webInstallerOptions.options.base_url);
 281      const pattern2 = /^index\.php/;
 282  
 283      if (pattern1.test(url) || pattern2.test(url)) {
 284        webInstallerOptions.view = url.replace(/^.+[&?]view=(\w+).*$/, '$1');
 285  
 286        if (webInstallerOptions.view === 'dashboard') {
 287          webInstallerOptions.id = 0;
 288        } else if (webInstallerOptions.view === 'category') {
 289          webInstallerOptions.id = url.replace(/^.+[&?]id=(\d+).*$/, '$1');
 290        }
 291  
 292        this.loadweb(webInstallerOptions.options.base_url + url);
 293      } else {
 294        this.loadweb(url);
 295      }
 296    }
 297  
 298    static showLoadingLayer() {
 299      document.getElementById('web').appendChild(document.createElement('joomla-core-loader'));
 300    }
 301  
 302    static hideLoadingLayer() {
 303      const spinnerElement = document.querySelector('#web joomla-core-loader');
 304      spinnerElement.parentNode.removeChild(spinnerElement);
 305    }
 306  
 307    static clicker() {
 308      if (document.querySelector('.grid-view')) {
 309        document.querySelector('.grid-view').addEventListener('click', () => {
 310          webInstallerOptions.list = 0;
 311          document.querySelector('.list-container').classList.add('hidden');
 312          document.querySelector('.grid-container').classList.remove('hidden');
 313          document.getElementById('btn-list-view').classList.remove('active');
 314          document.getElementById('btn-grid-view').classList.remove('active');
 315        });
 316      }
 317  
 318      if (document.querySelector('.list-view')) {
 319        document.querySelector('.list-view').addEventListener('click', () => {
 320          webInstallerOptions.list = 1;
 321          document.querySelector('.grid-container').classList.add('hidden');
 322          document.querySelector('.list-container').classList.remove('hidden');
 323          document.getElementById('btn-grid-view').classList.remove('active');
 324          document.getElementById('btn-list-view').classList.add('active');
 325        });
 326      }
 327    }
 328    /**
 329     * @param {string} installUrl
 330     * @param {string} name
 331     * @returns {boolean}
 332     */
 333  
 334  
 335    static installfromweb(installUrl, name = null) {
 336      if (!installUrl) {
 337        Joomla.renderMessages({
 338          warning: [Joomla.Text._('PLG_INSTALLER_WEBINSTALLER_CANNOT_INSTALL_EXTENSION_IN_PLUGIN')]
 339        });
 340        return false;
 341      }
 342  
 343      document.getElementById('install_url').value = installUrl;
 344      document.getElementById('uploadform-web-url').innerText = installUrl;
 345  
 346      if (name) {
 347        document.getElementById('uploadform-web-name').innerText = name;
 348        document.getElementById('uploadform-web-name-label').classList.remove('hidden');
 349      } else {
 350        document.getElementById('uploadform-web-name-label').classList.add('hidden');
 351      }
 352  
 353      document.getElementById('uploadform-web').classList.remove('hidden');
 354      return true;
 355    } // eslint-disable-next-line class-methods-use-this
 356  
 357  
 358    submitButtonUrl() {
 359      const form = document.getElementById('adminForm'); // do field validation
 360  
 361      if (form.install_url.value === '' || form.install_url.value === 'http://' || form.install_url.value === 'https://') {
 362        Joomla.renderMessages({
 363          warning: [Joomla.Text._('COM_INSTALLER_MSG_INSTALL_ENTER_A_URL')]
 364        });
 365      } else {
 366        const loading = document.getElementById('loading');
 367  
 368        if (loading) {
 369          loading.classList.remove('hidden');
 370        }
 371  
 372        form.installtype.value = 'url';
 373        form.submit();
 374      }
 375    }
 376  
 377    submitButtonWeb() {
 378      const form = document.getElementById('adminForm'); // do field validation
 379  
 380      if (form.install_url.value !== '' || form.install_url.value !== 'http://' || form.install_url.value !== 'https://') {
 381        this.submitButtonUrl();
 382      } else if (form.install_url.value === '') {
 383        Joomla.renderMessages({
 384          warning: [Joomla.apps.options.btntxt]
 385        });
 386      } else {
 387        document.querySelector('#appsloading').classList.remove('hidden');
 388        form.installtype.value = 'web';
 389        form.submit();
 390      }
 391    }
 392  
 393  }
 394  
 395  customElements.whenDefined('joomla-tab').then(() => {
 396    const installerTabs = document.getElementById('myTab');
 397    const link = installerTabs.querySelector('button[aria-controls=web]'); // Abort if the IFW tab cannot be found
 398  
 399    if (!link) {
 400      return;
 401    }
 402  
 403    if (webInstallerOptions.options.installfromon) {
 404      link.click();
 405    }
 406  
 407    if (link.hasAttribute('aria-expanded') && link.getAttribute('aria-expanded') === 'true' && !instance) {
 408      instance = new WebInstaller();
 409      instance.initialise();
 410    }
 411  
 412    if (webInstallerOptions.options.installfrom_url !== '') {
 413      link.click();
 414    }
 415  
 416    link.addEventListener('joomla.tab.shown', () => {
 417      if (!instance) {
 418        instance = new WebInstaller();
 419        instance.initialise();
 420      }
 421    });
 422  });


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer