Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/widgets/ve.ui.MWExternalLinkAnnotationWidget.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWExternalLinkAnnotationWidget class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Creates an ve.ui.MWExternalLinkAnnotationWidget object.
10 *
11 * @class
12 * @extends ve.ui.LinkAnnotationWidget
13 *
14 * @constructor
15 * @param {Object} [config] Configuration options
16 */
17 ve.ui.MWExternalLinkAnnotationWidget = function VeUiMWExternalLinkAnnotationWidget() {
18 // Parent constructor
19 ve.ui.MWExternalLinkAnnotationWidget.super.apply( this, arguments );
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ui.MWExternalLinkAnnotationWidget, ve.ui.LinkAnnotationWidget );
25
26 /* Static Methods */
27
28 /**
29 * @inheritdoc
30 */
31 ve.ui.MWExternalLinkAnnotationWidget.static.getAnnotationFromText = function ( value ) {
32 const href = value.trim();
33
34 // Keep annotation in sync with value
35 if ( href === '' ) {
36 return null;
37 } else {
38 return new ve.dm.MWExternalLinkAnnotation( {
39 type: 'link/mwExternal',
40 attributes: {
41 href: href
42 }
43 } );
44 }
45 };
46
47 /**
48 * Create an external link input widget.
49 *
50 * @param {Object} [config] Configuration options
51 * @return {OO.ui.TextInputWidget} Text input widget
52 */
53 ve.ui.MWExternalLinkAnnotationWidget.static.createExternalLinkInputWidget = function ( config ) {
54 const inputWidget = new OO.ui.TextInputWidget( ve.extendObject( {}, config, {
55 icon: 'linkExternal',
56 type: 'url',
57 validate: ( text ) => !!ve.init.platform.getExternalLinkUrlProtocolsRegExp().exec( text.trim() )
58 } ) );
59
60 inputWidget.$input.attr( 'aria-label', mw.msg( 'visualeditor-linkinspector-button-link-external' ) );
61 return inputWidget;
62 };
63
64 /* Methods */
65
66 /**
67 * Create a text input widget to be used by the annotation widget
68 *
69 * @param {Object} [config] Configuration options
70 * @return {OO.ui.TextInputWidget} Text input widget
71 */
72 ve.ui.MWExternalLinkAnnotationWidget.prototype.createInputWidget = function ( config ) {
73 return this.constructor.static.createExternalLinkInputWidget( config );
74 };
75
76 /**
77 * Get the validity of current value
78 *
79 * @see OO.ui.TextInputWidget#getValidity
80 *
81 * @return {jQuery.Promise} A promise that resolves if the value is valid,
82 * rejects if not. If it's rejected, it'll resolve with an error code.
83 */
84 ve.ui.MWExternalLinkAnnotationWidget.prototype.getValidity = function () {
85 const url = this.input.getValue().trim();
86 return this.input.getValidity().then(
87 // input validity check covers whether it's a valid external link, now check whether it's blocked:
88 () => {
89 if ( mw.config.get( 'wgVisualEditorConfig' ).editCheckReliabilityAvailable ) {
90 return ( new mw.Api().get( {
91 action: 'editcheckreferenceurl',
92 url: url,
93 formatversion: 2
94 } ) ).then( ( reliablityResults ) => {
95 if ( reliablityResults && reliablityResults.editcheckreferenceurl[ url ] === 'blocked' ) {
96 return ve.createDeferred().reject( 'invalid-blocked' );
97 }
98 } );
99 }
100 },
101 // invalid link, so provide a reason
102 () => ve.createDeferred().reject( 'invalid-external' )
103 );
104 };