code.gitea.io/gitea@v1.22.3/web_src/js/features/install.js (about)

     1  import {hideElem, showElem} from '../utils/dom.js';
     2  import {GET} from '../modules/fetch.js';
     3  
     4  export function initInstall() {
     5    const page = document.querySelector('.page-content.install');
     6    if (!page) {
     7      return;
     8    }
     9    if (page.classList.contains('post-install')) {
    10      initPostInstall();
    11    } else {
    12      initPreInstall();
    13    }
    14  }
    15  function initPreInstall() {
    16    const defaultDbUser = 'gitea';
    17    const defaultDbName = 'gitea';
    18  
    19    const defaultDbHosts = {
    20      mysql: '127.0.0.1:3306',
    21      postgres: '127.0.0.1:5432',
    22      mssql: '127.0.0.1:1433',
    23    };
    24  
    25    const dbHost = document.getElementById('db_host');
    26    const dbUser = document.getElementById('db_user');
    27    const dbName = document.getElementById('db_name');
    28  
    29    // Database type change detection.
    30    document.getElementById('db_type').addEventListener('change', function () {
    31      const dbType = this.value;
    32      hideElem('div[data-db-setting-for]');
    33      showElem(`div[data-db-setting-for=${dbType}]`);
    34  
    35      if (dbType !== 'sqlite3') {
    36        // for most remote database servers
    37        showElem('div[data-db-setting-for=common-host]');
    38        const lastDbHost = dbHost.value;
    39        const isDbHostDefault = !lastDbHost || Object.values(defaultDbHosts).includes(lastDbHost);
    40        if (isDbHostDefault) {
    41          dbHost.value = defaultDbHosts[dbType] ?? '';
    42        }
    43        if (!dbUser.value && !dbName.value) {
    44          dbUser.value = defaultDbUser;
    45          dbName.value = defaultDbName;
    46        }
    47      } // else: for SQLite3, the default path is always prepared by backend code (setting)
    48    });
    49    document.getElementById('db_type').dispatchEvent(new Event('change'));
    50  
    51    const appUrl = document.getElementById('app_url');
    52    if (appUrl.value.includes('://localhost')) {
    53      appUrl.value = window.location.href;
    54    }
    55  
    56    const domain = document.getElementById('domain');
    57    if (domain.value.trim() === 'localhost') {
    58      domain.value = window.location.hostname;
    59    }
    60  
    61    // TODO: better handling of exclusive relations.
    62    document.querySelector('#offline-mode input').addEventListener('change', function () {
    63      if (this.checked) {
    64        document.querySelector('#disable-gravatar input').checked = true;
    65        document.querySelector('#federated-avatar-lookup input').checked = false;
    66      }
    67    });
    68    document.querySelector('#disable-gravatar input').addEventListener('change', function () {
    69      if (this.checked) {
    70        document.querySelector('#federated-avatar-lookup input').checked = false;
    71      } else {
    72        document.querySelector('#offline-mode input').checked = false;
    73      }
    74    });
    75    document.querySelector('#federated-avatar-lookup input').addEventListener('change', function () {
    76      if (this.checked) {
    77        document.querySelector('#disable-gravatar input').checked = false;
    78        document.querySelector('#offline-mode input').checked = false;
    79      }
    80    });
    81    document.querySelector('#enable-openid-signin input').addEventListener('change', function () {
    82      if (this.checked) {
    83        if (!document.querySelector('#disable-registration input').checked) {
    84          document.querySelector('#enable-openid-signup input').checked = true;
    85        }
    86      } else {
    87        document.querySelector('#enable-openid-signup input').checked = false;
    88      }
    89    });
    90    document.querySelector('#disable-registration input').addEventListener('change', function () {
    91      if (this.checked) {
    92        document.querySelector('#enable-captcha input').checked = false;
    93        document.querySelector('#enable-openid-signup input').checked = false;
    94      } else {
    95        document.querySelector('#enable-openid-signup input').checked = true;
    96      }
    97    });
    98    document.querySelector('#enable-captcha input').addEventListener('change', function () {
    99      if (this.checked) {
   100        document.querySelector('#disable-registration input').checked = false;
   101      }
   102    });
   103  }
   104  
   105  function initPostInstall() {
   106    const el = document.getElementById('goto-user-login');
   107    if (!el) return;
   108  
   109    const targetUrl = el.getAttribute('href');
   110    let tid = setInterval(async () => {
   111      try {
   112        const resp = await GET(targetUrl);
   113        if (tid && resp.status === 200) {
   114          clearInterval(tid);
   115          tid = null;
   116          window.location.href = targetUrl;
   117        }
   118      } catch {}
   119    }, 1000);
   120  }