Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/inspectors/ve.ui.MWLinkNodeInspector.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWLinkNodeInspector class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Inspector for editing unlabeled MediaWiki external links.
10 *
11 * @class
12 * @extends ve.ui.NodeInspector
13 *
14 * @constructor
15 * @param {Object} [config] Configuration options
16 */
17 ve.ui.MWLinkNodeInspector = function VeUiMWLinkNodeInspector() {
18 // Parent constructor
19 ve.ui.MWLinkNodeInspector.super.apply( this, arguments );
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ui.MWLinkNodeInspector, ve.ui.NodeInspector );
25
26 /* Static properties */
27
28 ve.ui.MWLinkNodeInspector.static.name = 'linkNode';
29
30 ve.ui.MWLinkNodeInspector.static.title = OO.ui.deferMsg( 'visualeditor-linknodeinspector-title' );
31
32 ve.ui.MWLinkNodeInspector.static.modelClasses = [ ve.dm.MWNumberedExternalLinkNode ];
33
34 ve.ui.MWLinkNodeInspector.static.actions = [
35 ...ve.ui.MWLinkNodeInspector.super.static.actions,
36 {
37 action: 'convert',
38 label: OO.ui.deferMsg( 'visualeditor-linknodeinspector-add-label' ),
39 modes: [ 'edit' ]
40 }
41 ];
42
43 /* Methods */
44
45 /**
46 * @inheritdoc
47 */
48 ve.ui.MWLinkNodeInspector.prototype.initialize = function () {
49 // Parent method
50 ve.ui.MWLinkNodeInspector.super.prototype.initialize.call( this );
51
52 // Properties
53 this.targetInput = new OO.ui.TextInputWidget( {
54 validate: ve.init.platform.getExternalLinkUrlProtocolsRegExp()
55 } );
56
57 // Initialization
58 this.form.$element.append( this.targetInput.$element );
59 };
60
61 /**
62 * @inheritdoc
63 */
64 ve.ui.MWLinkNodeInspector.prototype.getActionProcess = function ( action ) {
65 if ( action === 'convert' ) {
66 return new OO.ui.Process( () => {
67 this.close( { action: action } );
68 } );
69 }
70 return ve.ui.MWLinkNodeInspector.super.prototype.getActionProcess.call( this, action );
71 };
72
73 /**
74 * @inheritdoc
75 */
76 ve.ui.MWLinkNodeInspector.prototype.getSetupProcess = function ( data ) {
77 return ve.ui.MWLinkNodeInspector.super.prototype.getSetupProcess.call( this, data )
78 .next( () => {
79 // Initialization
80 this.targetInput.setValue(
81 this.selectedNode ? this.selectedNode.getAttribute( 'href' ) : ''
82 );
83 this.targetInput.setReadOnly( this.isReadOnly() );
84 } );
85 };
86
87 /**
88 * @inheritdoc
89 */
90 ve.ui.MWLinkNodeInspector.prototype.getReadyProcess = function ( data ) {
91 return ve.ui.MWLinkNodeInspector.super.prototype.getReadyProcess.call( this, data )
92 .next( () => {
93 this.targetInput.focus().select();
94 } );
95 };
96
97 /**
98 * @inheritdoc
99 */
100 ve.ui.MWLinkNodeInspector.prototype.getTeardownProcess = function ( data ) {
101 data = data || {};
102 return ve.ui.MWLinkNodeInspector.super.prototype.getTeardownProcess.call( this, data )
103 .first( () => {
104 let value = this.targetInput.getValue();
105 const surfaceView = this.manager.getSurface().getView(),
106 surfaceModel = this.getFragment().getSurface(),
107 doc = surfaceModel.getDocument(),
108 nodeRange = this.selectedNode.getOuterRange(),
109 convert = data.action === 'convert',
110 remove = data.action === 'remove' || !value;
111
112 // Default to http:// if the external link doesn't already begin with a supported
113 // protocol - this prevents the link from being converted into literal text upon
114 // save and also fixes a common mistake users may make
115 if ( !ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( value ) ) {
116 value = 'http://' + value;
117 }
118
119 if ( remove ) {
120 surfaceModel.change(
121 ve.dm.TransactionBuilder.static.newFromRemoval( doc, nodeRange )
122 );
123 } else if ( convert ) {
124 const annotation = new ve.dm.MWExternalLinkAnnotation( {
125 type: 'link/mwExternal',
126 attributes: {
127 href: value
128 }
129 } );
130 const annotations = doc.data.getAnnotationsFromOffset( nodeRange.start ).clone();
131 annotations.push( annotation );
132 const content = value.split( '' );
133 ve.dm.Document.static.addAnnotationsToData( content, annotations );
134 surfaceModel.change(
135 ve.dm.TransactionBuilder.static.newFromReplacement( doc, nodeRange, content )
136 );
137 setTimeout( () => {
138 surfaceView.selectAnnotation( ( view ) => view.model instanceof ve.dm.MWExternalLinkAnnotation );
139 } );
140 } else {
141 surfaceModel.change(
142 ve.dm.TransactionBuilder.static.newFromAttributeChanges(
143 doc, nodeRange.start, { href: value }
144 )
145 );
146 }
147 } );
148 };
149
150 /* Registration */
151
152 ve.ui.windowFactory.register( ve.ui.MWLinkNodeInspector );