Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWMagicLinkNodeInspector class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Inspector for editing MediaWiki magic links (RFC/ISBN/PMID).
10 *
11 * @class
12 * @extends ve.ui.NodeInspector
13 *
14 * @constructor
15 * @param {Object} [config] Configuration options
16 */
17 ve.ui.MWMagicLinkNodeInspector = function VeUiMWMagicLinkNodeInspector() {
18 // Parent constructor
19 ve.ui.MWMagicLinkNodeInspector.super.apply( this, arguments );
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ui.MWMagicLinkNodeInspector, ve.ui.NodeInspector );
25
26 /* Static properties */
27
28 ve.ui.MWMagicLinkNodeInspector.static.name = 'linkMagicNode';
29
30 ve.ui.MWMagicLinkNodeInspector.static.title = null; // see #getSetupProcess
31
32 ve.ui.MWMagicLinkNodeInspector.static.modelClasses = [ ve.dm.MWMagicLinkNode ];
33
34 ve.ui.MWMagicLinkNodeInspector.static.actions = [
35 ...ve.ui.MWMagicLinkNodeInspector.super.static.actions,
36 {
37 action: 'convert',
38 label: OO.ui.deferMsg( 'visualeditor-magiclinknodeinspector-convert-link' ),
39 modes: [ 'edit' ]
40 }
41 ];
42
43 /* Methods */
44
45 /**
46 * @inheritdoc
47 */
48 ve.ui.MWMagicLinkNodeInspector.prototype.initialize = function () {
49 // Parent method
50 ve.ui.MWMagicLinkNodeInspector.super.prototype.initialize.call( this );
51
52 // Properties
53 this.targetInput = new OO.ui.TextInputWidget( {
54 validate: this.validate.bind( this )
55 } );
56 this.targetInput.on( 'change', this.onChange.bind( this ) );
57
58 // Initialization
59 this.form.$element.append( this.targetInput.$element );
60 };
61
62 /**
63 * Return true if the given string is a valid magic link of the
64 * appropriate type.
65 *
66 * @private
67 * @param {string} str String to validate
68 * @return {boolean} String is valid
69 */
70 ve.ui.MWMagicLinkNodeInspector.prototype.validate = function ( str ) {
71 const node = this.getFragment().getSelectedNode();
72 return node.constructor.static.validateContent( str, node.getMagicType() );
73 };
74
75 ve.ui.MWMagicLinkNodeInspector.prototype.onChange = function ( value ) {
76 // Disable the unsafe action buttons if the input isn't valid
77 const isValid = this.validate( value );
78 this.actions.forEach( null, ( action ) => {
79 if ( !action.hasFlag( 'safe' ) ) {
80 action.setDisabled( !isValid );
81 }
82 } );
83 };
84
85 /**
86 * @inheritdoc
87 */
88 ve.ui.MWMagicLinkNodeInspector.prototype.getActionProcess = function ( action ) {
89 if ( ( action === 'done' || action === 'convert' ) &&
90 !this.validate( this.targetInput.getValue() ) ) {
91 // Don't close dialog: input isn't valid.
92 return new OO.ui.Process( 0 );
93 }
94 if ( action === 'convert' ) {
95 return new OO.ui.Process( () => {
96 this.close( { action: action } );
97 } );
98 }
99 return ve.ui.MWMagicLinkNodeInspector.super.prototype.getActionProcess.call( this, action );
100 };
101
102 /**
103 * @inheritdoc
104 */
105 ve.ui.MWMagicLinkNodeInspector.prototype.getSetupProcess = function ( data ) {
106 // Set the title based on the node type
107 const fragment = data.fragment,
108 node = fragment instanceof ve.dm.SurfaceFragment ?
109 fragment.getSelectedNode() : null,
110 type = node instanceof ve.dm.MWMagicLinkNode ?
111 node.getMagicType() : null,
112 msg = type ?
113 'visualeditor-magiclinknodeinspector-title-' + type.toLowerCase() :
114 null;
115
116 data = ve.extendObject( {
117 // The following messages are used here
118 // * visualeditor-magiclinknodeinspector-title-isbn
119 // * visualeditor-magiclinknodeinspector-title-pmid
120 // * visualeditor-magiclinknodeinspector-title-rfc
121 title: msg ? OO.ui.deferMsg( msg ) : null
122 }, data );
123 return ve.ui.MWMagicLinkNodeInspector.super.prototype.getSetupProcess.call( this, data )
124 .next( () => {
125 // Initialization
126 this.targetInput.setValue(
127 this.selectedNode ? this.selectedNode.getAttribute( 'content' ) : ''
128 ).setReadOnly( this.isReadOnly() );
129 } );
130 };
131
132 /**
133 * @inheritdoc
134 */
135 ve.ui.MWMagicLinkNodeInspector.prototype.getReadyProcess = function ( data ) {
136 return ve.ui.MWMagicLinkNodeInspector.super.prototype.getReadyProcess.call( this, data )
137 .next( () => {
138 this.targetInput.focus().select();
139 } );
140 };
141
142 /**
143 * @inheritdoc
144 */
145 ve.ui.MWMagicLinkNodeInspector.prototype.getTeardownProcess = function ( data ) {
146 data = data || {};
147 return ve.ui.MWMagicLinkNodeInspector.super.prototype.getTeardownProcess.call( this, data )
148 .first( () => {
149 const surfaceView = this.manager.getSurface().getView(),
150 surfaceModel = this.getFragment().getSurface(),
151 doc = surfaceModel.getDocument(),
152 nodeRange = this.selectedNode.getOuterRange(),
153 value = this.targetInput.getValue(),
154 done = data.action === 'done',
155 convert = data.action === 'convert',
156 remove = data.action === 'remove' || ( done && !value );
157
158 if ( remove ) {
159 surfaceModel.change(
160 ve.dm.TransactionBuilder.static.newFromRemoval( doc, nodeRange )
161 );
162 } else if ( convert ) {
163 const annotation = ve.dm.MWMagicLinkNode.static.annotationFromContent(
164 value
165 );
166 if ( annotation ) {
167 const annotations = doc.data.getAnnotationsFromOffset( nodeRange.start ).clone();
168 annotations.push( annotation );
169 const content = value.split( '' );
170 ve.dm.Document.static.addAnnotationsToData( content, annotations );
171 surfaceModel.change(
172 ve.dm.TransactionBuilder.static.newFromReplacement( doc, nodeRange, content )
173 );
174 setTimeout( () => {
175 surfaceView.selectAnnotation( ( view ) => view.model instanceof ve.dm.LinkAnnotation );
176 } );
177 }
178 } else if ( done && this.validate( value ) ) {
179 surfaceModel.change(
180 ve.dm.TransactionBuilder.static.newFromAttributeChanges(
181 doc, nodeRange.start, { content: value }
182 )
183 );
184 }
185 } );
186 };
187
188 /* Registration */
189
190 ve.ui.windowFactory.register( ve.ui.MWMagicLinkNodeInspector );