Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor AnnotationContextItem class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Context item for an annotation.
9 *
10 * @class
11 * @abstract
12 * @extends ve.ui.LinearContextItem
13 *
14 * @param {ve.ui.LinearContext} context Context the item is in
15 * @param {ve.dm.Model} model Model the item is related to
16 * @param {Object} [config] Configuration options
17 */
18 ve.ui.AnnotationContextItem = function VeUiAnnotationContextItem( context, model, config ) {
19 // Parent constructor
20 ve.ui.AnnotationContextItem.super.call( this, context, model, config );
21
22 // Initialization
23 this.$element.addClass( 've-ui-annotationContextItem' );
24
25 if ( this.context.isMobile() ) {
26 this.clearButton = new OO.ui.ButtonWidget( {
27 framed: false,
28 label: this.constructor.static.clearMsg,
29 icon: this.constructor.static.clearIcon,
30 flags: [ 'destructive' ]
31 } );
32 if ( this.isClearable() && !this.isReadOnly() ) {
33 this.$foot.append( this.clearButton.$element );
34 }
35 } else {
36 this.clearButton = new OO.ui.ButtonWidget( {
37 title: this.constructor.static.clearMsg,
38 icon: this.constructor.static.clearIcon,
39 flags: [ 'destructive' ]
40 } );
41 if ( this.isClearable() && !this.isReadOnly() ) {
42 this.actionButtons.addItems( [ this.clearButton ], 0 );
43 }
44 }
45 this.clearButton.connect( this, { click: 'onClearButtonClick' } );
46 };
47
48 /* Inheritance */
49
50 OO.inheritClass( ve.ui.AnnotationContextItem, ve.ui.LinearContextItem );
51
52 /* Static Properties */
53
54 ve.ui.AnnotationContextItem.static.clearable = true;
55 ve.ui.AnnotationContextItem.static.clearIcon = 'cancel';
56 ve.ui.AnnotationContextItem.static.clearMsg = OO.ui.deferMsg( 'visualeditor-clearbutton-tooltip' );
57
58 /* Methods */
59
60 /**
61 * Check if item is clearable.
62 *
63 * @return {boolean} Item is clearable
64 */
65 ve.ui.AnnotationContextItem.prototype.isClearable = function () {
66 return this.constructor.static.clearable;
67 };
68
69 /**
70 * Handle clear button click events.
71 *
72 * @localdoc Removes any modelClasses annotations from the current fragment
73 *
74 * @protected
75 */
76 ve.ui.AnnotationContextItem.prototype.onClearButtonClick = function () {
77 ve.track( 'activity.' + this.constructor.static.name, { action: 'context-clear' } );
78 this.applyToAnnotations( ( fragment, annotation ) => {
79 fragment.annotateContent( 'clear', annotation );
80 } );
81 };
82
83 /**
84 * Apply a callback to every modelClass annotation in the current fragment
85 *
86 * @param {Function} callback Callback, will be passed fragment and annotation
87 */
88 ve.ui.AnnotationContextItem.prototype.applyToAnnotations = function ( callback ) {
89 const modelClasses = this.constructor.static.modelClasses;
90
91 let fragment = this.getFragment(),
92 annotations = fragment.getAnnotations( true ).filter( ( annotation ) => ve.isInstanceOfAny( annotation, modelClasses ) ).get();
93 if (
94 !annotations.length &&
95 fragment.getSelection().isCollapsed() &&
96 fragment.getDocument().data.isContentOffset( fragment.getSelection().getRange().start )
97 ) {
98 // Expand to nearest word and try again
99 fragment = fragment.expandLinearSelection( 'word' );
100
101 annotations = fragment.getAnnotations( true ).filter( ( annotation ) => ve.isInstanceOfAny( annotation, modelClasses ) ).get();
102 }
103 for ( let i = 0, len = annotations.length; i < len; i++ ) {
104 callback( fragment.expandLinearSelection( 'annotation', annotations[ i ] ), annotations[ i ] );
105 }
106 };
107
108 /**
109 * Find the CE view for the annotation related to this context item
110 *
111 * Problem: Going from the model to the view is difficult, as there can be many views of any given model.
112 * Assumption: an active annotation context item must mean the focus is within the relevant annotation.
113 *
114 * @return {ve.ce.Annotation|undefined} The annotation view, if it's found, or undefined if not
115 */
116 ve.ui.AnnotationContextItem.prototype.getAnnotationView = function () {
117 const model = this.model,
118 surfaceView = this.context.getSurface().getView();
119
120 function isThisModel( annotationView ) {
121 return model === annotationView.model;
122 }
123
124 let annotations = [];
125 // Use surfaceView.contexedAnnotations when available, i.e. when
126 // the user clicked/tapped on the annotation.
127 if ( surfaceView.contexedAnnotations ) {
128 annotations = surfaceView.contexedAnnotations.filter( isThisModel );
129 }
130 if ( !annotations.length ) {
131 annotations = surfaceView.annotationsAtModelSelection( isThisModel );
132 }
133
134 return annotations.length ? annotations[ 0 ] : undefined;
135 };