Debian Codegraph @

extensions/WikiEditor/modules/insertlink/TitleInputField.js

raw symbols SCIP
1 var LinkTypeField = require( './LinkTypeField.js' );
2 var TitleInputWidget = require( './TitleInputWidget.js' );
3 /* global InsertLinkTitleOptionWidget */
4
5 /**
6 * A FieldLayout containing a custom TitleInputwidget and message-display system.
7 *
8 * @class
9 * @extends OO.ui.FieldLayout
10 * @mixes OO.EventEmitter
11 * @constructor
12 */
13 function TitleInputField() {
14 // Mixin constructor
15 OO.EventEmitter.call( this );
16
17 var input = new TitleInputWidget();
18 input.connect( this, {
19 change: this.onChange,
20 select: this.onSelect
21 } );
22
23 // The URL mode is set by the user via radio buttons, or automatically for link targets that look like URLs.
24 this.urlMode = LinkTypeField.static.LINK_MODE_INTERNAL;
25 // The 'manual' URL mode flag is set when the user changes the mode, and doesn't change again.
26 this.urlModeManual = false;
27
28 var config = {
29 align: 'top',
30 label: mw.msg( 'wikieditor-toolbar-tool-link-int-target' ),
31 classes: [ 'mw-wikiEditor-InsertLink-TitleInputField' ]
32 };
33 TitleInputField.super.call( this, input, config );
34 }
35
36 OO.inheritClass( TitleInputField, OO.ui.FieldLayout );
37 OO.mixinClass( TitleInputField, OO.EventEmitter );
38
39 /**
40 * Reset the field to initial state.
41 */
42 TitleInputField.prototype.reset = function () {
43 this.getField().setValue( '' );
44 this.urlModeManual = false;
45 this.urlMode = LinkTypeField.static.LINK_MODE_INTERNAL;
46 };
47
48 /**
49 * Set the URL mode and disable automatic detection of external URLs.
50 *
51 * @public
52 * @param {string} urlMode One of the `LinkTypeField.static.LINK_MODE_*` values.
53 */
54 TitleInputField.prototype.setUrlMode = function ( urlMode ) {
55 this.urlMode = urlMode === LinkTypeField.static.LINK_MODE_EXTERNAL ?
56 LinkTypeField.static.LINK_MODE_EXTERNAL :
57 LinkTypeField.static.LINK_MODE_INTERNAL;
58 this.urlModeManual = true;
59 this.getField().selectFirstMatch();
60 this.validate( this.getField().getValue() );
61 };
62
63 /**
64 * @public
65 * @return {boolean}
66 */
67 TitleInputField.prototype.isExternal = function () {
68 return this.urlMode === LinkTypeField.static.LINK_MODE_EXTERNAL;
69 };
70
71 /**
72 * @inheritDoc
73 */
74 TitleInputField.prototype.makeMessage = function ( kind, text ) {
75 this.message = new OO.ui.MessageWidget( {
76 type: kind,
77 inline: true,
78 label: text
79 } );
80 return this.message.$element;
81 };
82
83 /**
84 * Set the displayed field message, replacing any that is already set.
85 *
86 * @param {string} icon
87 * @param {string} message
88 * @param {string} type
89 */
90 TitleInputField.prototype.setMessage = function ( icon, message, type ) {
91 this.setNotices( [ message ] );
92 // Note that setNotices() must be called before this.message is available.
93 this.message
94 .setIcon( icon )
95 .setType( type );
96 };
97
98 /**
99 * @private
100 * @param {string} value
101 */
102 TitleInputField.prototype.onChange = function ( value ) {
103 if ( !this.urlModeManual && this.getField().looksLikeExternalLink( value ) ) {
104 this.urlMode = LinkTypeField.static.LINK_MODE_EXTERNAL;
105 }
106 this.validate( value );
107 };
108
109 /**
110 * Set message and emit event.
111 *
112 * @private
113 * @param {string} value
114 */
115 TitleInputField.prototype.validate = function ( value ) {
116 if ( this.urlMode === LinkTypeField.static.LINK_MODE_INTERNAL && value !== '' && !mw.Title.newFromText( value ) ) {
117 this.setMessage(
118 'error',
119 mw.message( 'wikieditor-toolbar-tool-link-int-target-status-invalid' ).parse(),
120 'error'
121 );
122 this.emit( 'invalid' );
123 } else {
124 // Remove message; it'll be re-added if required (after selection or blur).
125 this.setNotices( [] );
126 this.emit( 'change', value );
127 }
128 };
129
130 /**
131 * @param {InsertLinkTitleOptionWidget} item
132 */
133 TitleInputField.prototype.onSelect = function ( item ) {
134 var icon, msg;
135 if ( this.urlMode === LinkTypeField.static.LINK_MODE_EXTERNAL ||
136 ( !this.urlModeManual && this.urlMode === LinkTypeField.static.LINK_MODE_INTERNAL && item.isExternal() )
137 ) {
138 icon = 'linkExternal';
139 msg = 'wikieditor-toolbar-tool-link-int-target-status-external';
140 } else if ( item.isDisambiguation() ) {
141 icon = 'articleDisambiguation';
142 msg = 'wikieditor-toolbar-tool-link-int-target-status-disambig';
143 } else if ( !item.isMissing() && !item.isExternal() ) {
144 icon = 'article';
145 msg = 'wikieditor-toolbar-tool-link-int-target-status-exists';
146 } else {
147 icon = 'articleNotFound';
148 msg = 'wikieditor-toolbar-tool-link-int-target-status-notexists';
149 }
150 // eslint-disable-next-line mediawiki/msg-doc
151 this.setMessage( icon, mw.message( msg ).parse() );
152 };
153
154 module.exports = TitleInputField;