{if $__wcf->getLanguage()->getLanguages()|count > 1}
<li id="pageLanguageContainer">
<script data-relocate="true">
- //<![CDATA[
- $(function() {
- var $languages = {
- {implode from=$__wcf->getLanguage()->getLanguages() item=language}
- '{@$language->languageID}': {
- iconPath: '{@$language->getIconPath()}',
- languageName: '{$language}'
+ require(['WoltLab/WCF/Language/Chooser'], function(LanguageChooser) {
+ var languages = {
+ {implode from=$__wcf->getLanguage()->getLanguages() item=__language}
+ '{@$__language->languageID}': {
+ iconPath: '{@$__language->getIconPath()|encodeJS}',
+ languageName: '{$__language}'
}
{/implode}
};
- new WCF.Language.Chooser('pageLanguageContainer', 'languageID', {@$__wcf->getLanguage()->languageID}, $languages, function(item) {
- var $location = window.location.toString().replace(/#.*/, '').replace(/(\?|&)l=[0-9]+/g, '');
- var $delimiter = ($location.indexOf('?') == -1) ? '?' : '&';
+ LanguageChooser.init('pageLanguageContainer', 'languageID', {@$__wcf->getLanguage()->languageID}, languages, function(listItem) {
+ var location = window.location.toString().replace(/#.*/, '').replace(/(\?|&)l=[0-9]+/g, '');
+ var delimiter = (location.indexOf('?') == -1) ? '?' : '&';
- window.location = $location + $delimiter + 'l=' + item.data('languageID') + window.location.hash;
+ window.location = location + delimiter + 'l=' + elData(listItem, 'language-id') + window.location.hash;
});
});
- //]]>
</script>
</li>
{/if}
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @module WoltLab/WCF/Language/Chooser
*/
-define(['Dictionary', 'Language', 'Dom/Traverse', 'Dom/Util', 'Ui/SimpleDropdown'], function(Dictionary, Language, DomTraverse, DomUtil, UiSimpleDropdown) {
+define(['Dictionary', 'Language', 'Dom/Traverse', 'Dom/Util', 'ObjectMap', 'Ui/SimpleDropdown'], function(Dictionary, Language, DomTraverse, DomUtil, ObjectMap, UiSimpleDropdown) {
"use strict";
var _choosers = new Dictionary();
var _didInit = false;
+ var _forms = new ObjectMap();
- var _callbackDropdownToggle = null;
var _callbackSubmit = null;
/**
* @exports WoltLab/WCF/Language/Chooser
*/
- var LanguageChooser = {
+ return {
/**
* Initializes a language chooser.
*
* @param {string} chooserId input element id
* @param {integer} languageId selected language id
* @param {object<integer, object<string, string>>} languages data of available languages
- * @param callback
- * @param allowEmptyValue
+ * @param {function} callback function called after a language is selected
+ * @param {boolean} allowEmptyValue true if no language may be selected
*/
init: function(containerId, chooserId, languageId, languages, callback, allowEmptyValue) {
if (_choosers.has(chooserId)) {
container.appendChild(element);
}
- // todo: callback
-
- this._initElement(chooserId, element, languageId, languages, allowEmptyValue);
+ this._initElement(chooserId, element, languageId, languages, callback, allowEmptyValue);
},
/**
_callbackSubmit = this._submit.bind(this);
},
- _initElement: function(chooserId, element, languageId, languages, allowEmptyValue) {
+ /**
+ * Sets up DOM and event listeners for a language chooser.
+ *
+ * @param {string} chooserId chooser id
+ * @param {Element} element chooser element
+ * @param {integer} languageId selected language id
+ * @param {object<integer, object<string, string>>} languages data of available languages
+ * @param {boolean} allowEmptyValue true if no language may be selected
+ */
+ _initElement: function(chooserId, element, languageId, languages, callback, allowEmptyValue) {
var container = element.parentNode;
if (!container.classList.contains('inputAddon')) {
container = elCreate('div');
UiSimpleDropdown.init(dropdownToggle);
_choosers.set(chooserId, {
+ callback: callback,
dropdownMenu: dropdownMenu,
dropdownToggle: dropdownToggle,
element: element
});
// bind to submit event
- var submit = DomTraverse.parentByTag(element, 'FORM');
- if (submit !== null) {
- submit.addEventListener('submit', _callbackSubmit);
+ var form = DomTraverse.parentByTag(element, 'FORM');
+ if (form !== null) {
+ form.addEventListener('submit', _callbackSubmit);
- // TODO: WHAT?
- return;
- var chooserIds = _forms.get(submit);
+ var chooserIds = _forms.get(form);
if (chooserIds === undefined) {
chooserIds = [];
- _forms.set(submit, chooserIds);
+ _forms.set(form, chooserIds);
}
chooserIds.push(chooserId);
chooser.dropdownToggle.innerHTML = listItem.firstChild.innerHTML;
_choosers.set(chooserId, chooser);
+
+ // execute callback
+ if (typeof chooser.callback === 'function') {
+ chooser.callback(listItem);
+ }
+ },
+
+ /**
+ * Inserts hidden fields for the language chooser value on submit.
+ *
+ * @param {object} event event object
+ */
+ _submit: function(event) {
+ var elementIds = _forms.get(event.currentTarget);
+
+ var input;
+ for (var i = 0, length = elementIds.length; i < length; i++) {
+ input = elCreate('input');
+ input.type = 'hidden';
+ input.name = elementIds[i];
+ input.value = this.getLanguageId(elementId);
+
+ event.currentTarget.appendChild(input);
+ }
},
/**
this._select(chooserId, languageId);
}
};
-
- return LanguageChooser;
});