HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/ste.komma.pro/resources/js/components/RegisterController.js
class RegisterController {
    /**
     * @param {HTMLDivElement} wrapper
     * @param {GlobalisationService} globalisationService
     */
    constructor(wrapper, globalisationService) {
        this._wrapper = wrapper;
        if(!this._wrapper || this._wrapper.tagName !== "FORM")
        {
            console.error('RegisterController: The Register form must be a div element but was not or wasn\'t even specified.');
            return;
        }

        this._countrySelect = this._wrapper.querySelector('select[name="country"]');
        if(!this._countrySelect) {
            console.error('RegisterController: The Register form must have a select element with name "country" but did not have it.');
            return;
        }

        this._languageSelect = this._wrapper.querySelector('select[name="language"]');
        if(!this._countrySelect) {
            console.error('RegisterController: The Register form must have a select element with name "language" but did not have it.');
            return;
        }

        this._languageSelectWrapper = this._wrapper.querySelector('.js-language-wrapper');
        if(!this._languageSelectWrapper || this._languageSelectWrapper.tagName !== "DIV") {
            console.error('RegisterController: The Register form must have a wrapper DIV element with class "js-language-wrapper" but did not have it.');
            return;
        }


        this._globalisationService = globalisationService;
        if(!globalisationService) {
            console.error('RegisterController: The Register form was expected to get an instance of RegisterService, but did not get one.');
            return;
        }

        this._hiddenClass = 'u-hidden'; //The name of a class that can be applied to things to hide them.

        this._domParser = new DOMParser();

        this._countryChanged = this._countryChanged.bind(this);
        this.controlListeners(true);

        this._countryChanged();
    }

    controlListeners(enable = true) {
        this._countrySelect.removeEventListener('change', this._countryChanged);

        if(enable) {
            this._countrySelect.addEventListener('change', this._countryChanged);
        }
    }

    /**
     * @private
     */
    _countryChanged() {
        let countryIso3 = this._countrySelect.value;
        let self = this;

        this._globalisationService.getLanguagesForCountryWithIso3(countryIso3).then(
            /** @param { CountryLanguagesResponse } countryLanguagesResponse **/
            function(countryLanguagesResponse) {
                self._clearLanguageSelect();
                let languageCount = countryLanguagesResponse.languages.length;
                for(let index = 0; index < languageCount; index++)
                {
                    let language = countryLanguagesResponse.languages[index];
                    let optionElement = self._createHtmlElementFromString('<option value="'+language.threeLetterIsoLanguageName+'">'+language.nativeName+'</option>');
                    self._languageSelect.appendChild(optionElement);
                }

                if(languageCount <= 1) {
                    self._languageSelectWrapper.classList.add(self._hiddenClass);
                } else {
                    self._languageSelectWrapper.classList.remove(self._hiddenClass);
                }
            }
        ).catch(
            /** @param { ErrorResponse } errorResponse **/
            function(errorResponse) {
                console.error(errorResponse);
            }
        )
    }

    /**
     * @private
     */
    _clearLanguageSelect()
    {
        while(this._languageSelect.children.length) {
            this._languageSelect.removeChild(this._languageSelect.firstChild);
        }
    }

    /**
     * @private
     * @param {string} from
     * @return {HTMLElement}
     */
    _createHtmlElementFromString(from)
    {
        let document = this._domParser.parseFromString(from, 'text/html');
        return document.body.firstChild;
    }
}

export { RegisterController }