2 * MediaWiki Widgets - TagMultiselectWidget class.
4 * @copyright 2017 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
9 * @classdesc Input list of tags in a single line.
11 * This extends TagMultiselectWidget by adding an invisible textarea
12 * element which will be used to submit the values of the tags
14 * If used inside HTML form the results will be sent as the list of
15 * newline-separated tags.
18 * @extends OO.ui.TagMultiselectWidget
21 * @description Create an instance of `mw.widgets.TagMultiselectWidget`.
22 * @param {Object} [config] Configuration options
23 * @param {string} [config.name] Name of input to submit results (when used in HTML forms)
25 mw.widgets.TagMultiselectWidget = function MwWidgetsTagMultiselectWidget( config ) {
27 mw.widgets.TagMultiselectWidget.super.call( this, Object.assign( {}, config, {} ) );
29 if ( 'name' in config ) {
30 // Use this instead of <input type="hidden">, because hidden inputs do not have separate
31 // 'value' and 'defaultValue' properties.
32 this.$hiddenInput = $( '<textarea>' )
33 .addClass( 'oo-ui-element-hidden' )
34 .attr( 'name', config.name )
35 .appendTo( this.$element );
36 // Update with preset values
37 this.updateHiddenInput();
38 // Set the default value (it might be different from just being empty)
39 this.$hiddenInput.prop( 'defaultValue', this.getValue().join( '\n' ) );
43 // When list of selected tags changes, update hidden input
45 change: 'updateHiddenInput'
51 OO.inheritClass( mw.widgets.TagMultiselectWidget, OO.ui.TagMultiselectWidget );
56 * If used inside HTML form, then update hiddenInput with list of
57 * newline-separated tags.
61 mw.widgets.TagMultiselectWidget.prototype.updateHiddenInput = function () {
62 if ( '$hiddenInput' in this ) {
63 this.$hiddenInput.val( this.getValue().join( '\n' ) );
64 // Trigger a 'change' event as if a user edited the text
65 // (it is not triggered when changing the value from JS code).
66 this.$hiddenInput.trigger( 'change' );