| 1 |
var LinkTypeField = require( './LinkTypeField.js' ); |
| 2 |
var TitleInputWidget = require( './TitleInputWidget.js' ); |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
function TitleInputField() { |
| 14 |
|
| 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 |
|
| 24 |
this.urlMode = LinkTypeField.static.LINK_MODE_INTERNAL; |
| 25 |
|
| 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 |
|
| 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 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 65 |
|
| 66 |
|
| 67 |
TitleInputField.prototype.isExternal = function () { |
| 68 |
return this.urlMode === LinkTypeField.static.LINK_MODE_EXTERNAL; |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
|
| 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 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
TitleInputField.prototype.setMessage = function ( icon, message, type ) { |
| 91 |
this.setNotices( [ message ] ); |
| 92 |
|
| 93 |
this.message |
| 94 |
.setIcon( icon ) |
| 95 |
.setType( type ); |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 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 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 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 |
|
| 125 |
this.setNotices( [] ); |
| 126 |
this.emit( 'change', value ); |
| 127 |
} |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
|
| 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 |
|
| 151 |
this.setMessage( icon, mw.message( msg ).parse() ); |
| 152 |
}; |
| 153 |
|
| 154 |
module.exports = TitleInputField; |