Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/contextitems/ve.ui.LinkContextItem.js

raw symbols SCIP
1 /*!
2 * VisualEditor LinkContextItem class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Context item for a link.
9 *
10 * @class
11 * @extends ve.ui.AnnotationContextItem
12 *
13 * @param {ve.ui.LinearContext} context Context the item is in
14 * @param {ve.dm.Model} model Model the item is related to
15 * @param {Object} [config] Configuration options
16 */
17 ve.ui.LinkContextItem = function VeUiLinkContextItem( context, model, config ) {
18 // Parent constructor
19 ve.ui.LinkContextItem.super.call( this, context, model, config );
20
21 // Initialization
22 this.$element.addClass( 've-ui-linkContextItem' );
23
24 this.labelPreview = new OO.ui.LabelWidget();
25 if ( this.context.isMobile() ) {
26 this.$labelLayout = $( '<div>' ).addClass( 've-ui-linkContextItem-label' ).append(
27 $( '<div>' ).addClass( 've-ui-linkContextItem-label-body' ).append(
28 new OO.ui.LabelWidget( {
29 classes: [ 've-ui-linkContextItem-label-label' ],
30 label: OO.ui.deferMsg( 'visualeditor-linkcontext-label-label' )
31 } ).$element,
32 $( '<div>' ).addClass( 've-ui-linkContextItem-label-preview' ).append( this.labelPreview.$element )
33 )
34 );
35 this.$innerBody = $( '<div>' ).addClass( 've-ui-linkContextItem-inner-body' );
36 this.$body.append(
37 this.$labelLayout,
38 new OO.ui.LabelWidget( {
39 classes: [ 've-ui-linkContextItem-link-label' ],
40 label: OO.ui.deferMsg( 'visualeditor-linkinspector-title' )
41 } ).$element,
42 this.$innerBody
43 );
44 // Sub-classes should now append content to $innerBody
45 this.$body = this.$innerBody;
46 } else {
47 this.labelButton = new OO.ui.ButtonWidget( {
48 label: OO.ui.deferMsg( 'visualeditor-linkcontext-label-change' ),
49 framed: false,
50 flags: [ 'progressive' ]
51 } );
52 this.$labelLayout = $( '<div>' ).addClass( 've-ui-linkContextItem-label' ).append(
53 $( '<div>' ).addClass( 've-ui-linkContextItem-label-label' ).append(
54 new OO.ui.IconWidget( { icon: 'quotes' } ).$element,
55 new OO.ui.LabelWidget( { label: OO.ui.deferMsg( 'visualeditor-linkcontext-label-label' ) } ).$element
56 ),
57 $( '<div>' ).addClass( 've-ui-linkContextItem-label-preview' ).append( this.labelPreview.$element )
58 );
59 this.labelButton.connect( this, { click: 'onLabelButtonClick' } );
60
61 if ( !this.isReadOnly() ) {
62 this.$labelLayout.append( $( '<div>' ).addClass( 've-ui-linkContextItem-label-action' ).append( this.labelButton.$element ) );
63 }
64 }
65 };
66
67 /* Inheritance */
68
69 OO.inheritClass( ve.ui.LinkContextItem, ve.ui.AnnotationContextItem );
70
71 /* Static Properties */
72
73 ve.ui.LinkContextItem.static.name = 'link';
74
75 ve.ui.LinkContextItem.static.icon = 'link';
76
77 ve.ui.LinkContextItem.static.label = OO.ui.deferMsg( 'visualeditor-linkinspector-title' );
78
79 ve.ui.LinkContextItem.static.modelClasses = [ ve.dm.LinkAnnotation ];
80
81 ve.ui.LinkContextItem.static.embeddable = false;
82
83 ve.ui.LinkContextItem.static.commandName = 'link';
84
85 ve.ui.LinkContextItem.static.clearable = true;
86
87 ve.ui.LinkContextItem.static.clearMsg = OO.ui.deferMsg( 'visualeditor-linkcontext-remove' );
88
89 ve.ui.LinkContextItem.static.clearIcon = 'unLink';
90
91 /* Methods */
92
93 /**
94 * @inheritdoc
95 */
96 ve.ui.LinkContextItem.prototype.getDescription = function () {
97 return this.model.getHref();
98 };
99
100 /**
101 * @inheritdoc
102 */
103 ve.ui.LinkContextItem.prototype.renderBody = function () {
104 const htmlDoc = this.context.getSurface().getModel().getDocument().getHtmlDocument();
105 const $link = $( '<a>' )
106 .addClass( 've-ui-linkContextItem-link' )
107 .text( this.getDescription() )
108 .attr( {
109 target: '_blank',
110 rel: 'noopener'
111 } );
112 // T322704
113 ve.setAttributeSafe( $link[ 0 ], 'href', ve.resolveUrl( this.model.getHref(), htmlDoc ), '#' );
114 this.$body.empty().append( $link );
115 if ( !this.context.isMobile() ) {
116 this.$body.append( this.$labelLayout );
117 }
118 this.updateLabelPreview();
119 };
120
121 /**
122 * Set the preview of the label
123 *
124 * @protected
125 */
126 ve.ui.LinkContextItem.prototype.updateLabelPreview = function () {
127 const surfaceModel = this.context.getSurface().getModel(),
128 annotationView = this.getAnnotationView();
129
130 // annotationView is a potentially old view node from when the context was
131 // first focused in the document. If the annotation model has been changed
132 // as well, this may be a problem.
133 let label;
134 if ( annotationView ) {
135 label = surfaceModel.getFragment().expandLinearSelection( 'annotation', annotationView.getModel() ).getText();
136 }
137
138 this.labelPreview.setLabel( label || ve.msg( 'visualeditor-linkcontext-label-fallback' ) );
139 };
140
141 /**
142 * Handle label-edit button click events.
143 *
144 * @localdoc Selects the contents of the link annotation
145 *
146 * @protected
147 */
148 ve.ui.LinkContextItem.prototype.onLabelButtonClick = function () {
149 const surface = this.context.getSurface().getView(),
150 annotationView = this.getAnnotationView();
151
152 surface.selectNodeContents(
153 annotationView.$element[ 0 ],
154 this.context.isMobile() ? 'end' : undefined
155 );
156
157 ve.track( 'activity.' + this.constructor.static.name, { action: 'context-label' } );
158 };
159
160 /* Registration */
161
162 ve.ui.contextItemFactory.register( ve.ui.LinkContextItem );