Debian Codegraph @

extensions/Cite/modules/ve-cite/ve.ui.MWReferenceSearchWidget.js

raw symbols SCIP
1 'use strict';
2
3 /*!
4 * VisualEditor UserInterface MWReferenceSearchWidget class.
5 *
6 * @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
7 * @license MIT
8 */
9
10 /**
11 * Creates an ve.ui.MWReferenceSearchWidget object.
12 *
13 * @constructor
14 * @extends OO.ui.SearchWidget
15 * @param {Object} config Configuration options
16 * @param {jQuery} config.$overlay Layer to render reuse submenu outside of the parent dialog
17 * @property {Object[]|null} index Null when the index needs to be rebuild
18 */
19 ve.ui.MWReferenceSearchWidget = function VeUiMWReferenceSearchWidget( config ) {
20 // Configuration initialization
21 config = ve.extendObject( {
22 placeholder: ve.msg( 'cite-ve-reference-input-placeholder' )
23 }, config );
24
25 // Parent constructor
26 ve.ui.MWReferenceSearchWidget.super.call( this, config );
27
28 // Properties
29 this.docRefs = null;
30 this.index = null;
31 this.wasUsedActively = false;
32 this.$overlay = config.$overlay;
33
34 // Initialization
35 this.$element.addClass( 've-ui-mwReferenceSearchWidget' );
36 this.$results.on( 'scroll', this.trackActiveUsage.bind( this ) );
37 this.getResults().connect( this, { choose: 'onChoose' } );
38
39 // FIXME: T375053 Should just be temporary to test some UI changes
40 if ( mw.config.get( 'wgCiteBookReferencing' ) ) {
41 this.$element.addClass( 've-ui-mwReferenceSearchReuseHacks' );
42 }
43 };
44
45 /* Inheritance */
46
47 OO.inheritClass( ve.ui.MWReferenceSearchWidget, OO.ui.SearchWidget );
48
49 /* Events */
50
51 /**
52 * User chose a ref for reuse
53 *
54 * @event ve.ui.MWReferenceSearchWidget#reuse
55 * @param {ve.dm.MWReferenceModel} ref
56 */
57
58 /**
59 * Extends popup menu item was clicked
60 *
61 * @event ve.ui.MWReferenceSearchWidget#extends
62 * @param {ve.dm.MWReferenceModel} ref
63 */
64
65 /* Methods */
66
67 ve.ui.MWReferenceSearchWidget.prototype.onQueryChange = function () {
68 // Parent method
69 ve.ui.MWReferenceSearchWidget.super.prototype.onQueryChange.call( this );
70
71 // Populate
72 this.getResults().addItems( this.buildSearchResults( this.getQuery().getValue() ) );
73 };
74
75 /**
76 * @param {jQuery.Event} e Key down event
77 */
78 ve.ui.MWReferenceSearchWidget.prototype.onQueryKeydown = function ( e ) {
79 // Parent method
80 ve.ui.MWReferenceSearchWidget.super.prototype.onQueryKeydown.call( this, e );
81
82 this.trackActiveUsage();
83 };
84
85 ve.ui.MWReferenceSearchWidget.prototype.clearSearch = function () {
86 this.getQuery().setValue( '' );
87 this.wasUsedActively = false;
88 };
89
90 /**
91 * Track when the user looks for references to reuse using scrolling or filtering results
92 */
93 ve.ui.MWReferenceSearchWidget.prototype.trackActiveUsage = function () {
94 if ( this.wasUsedActively ) {
95 return;
96 }
97
98 // https://phabricator.wikimedia.org/T362347
99 ve.track( 'activity.reference', { action: 'reuse-dialog-use' } );
100 this.wasUsedActively = true;
101 };
102
103 ve.ui.MWReferenceSearchWidget.prototype.onChoose = function ( item ) {
104 this.emit( 'reuse', item.getData() );
105 };
106
107 /**
108 * Set the internal list and check if it contains any references
109 *
110 * @param {ve.dm.MWDocumentReferences} docRefs handle to all refs in the original document
111 */
112 ve.ui.MWReferenceSearchWidget.prototype.setDocumentRefs = function ( docRefs ) {
113 this.results.unselectItem();
114
115 this.docRefs = docRefs;
116 };
117
118 /**
119 * Set the internal list and check if it contains any references
120 *
121 * @deprecated use #setDocumentRefs instead.
122 * @param {ve.dm.InternalList} internalList
123 */
124 ve.ui.MWReferenceSearchWidget.prototype.setInternalList = function ( internalList ) {
125 this.setDocumentRefs( ve.dm.MWDocumentReferences.static.refsForDoc( internalList.getDocument() ) );
126 };
127
128 /**
129 * Manually re-build the index and re-populate the list of search results.
130 */
131 ve.ui.MWReferenceSearchWidget.prototype.buildIndex = function () {
132 this.index = null;
133 this.onQueryChange();
134 };
135
136 /**
137 * @private
138 * @return {Object[]}
139 */
140 ve.ui.MWReferenceSearchWidget.prototype.buildSearchIndex = function () {
141 const groupNames = this.docRefs.getAllGroupNames().sort();
142
143 // FIXME: Temporary hack, to be removed soon
144 // eslint-disable-next-line no-jquery/no-class-state
145 const filterExtends = this.$element.hasClass( 've-ui-citoidInspector-extends' );
146
147 let index = [];
148 for ( let i = 0; i < groupNames.length; i++ ) {
149 const groupName = groupNames[ i ];
150 if ( groupName.indexOf( 'mwReference/' ) !== 0 ) {
151 // FIXME: Should be impossible to reach
152 continue;
153 }
154 const groupRefs = this.docRefs.getGroupRefs( groupName );
155 const flatNodes = groupRefs.getAllRefsInDocumentOrder()
156 .filter( ( node ) => !filterExtends || !node.getAttribute( 'extendsRef' ) );
157
158 index = index.concat( flatNodes.map( ( node ) => {
159 const listKey = node.getAttribute( 'listKey' );
160 // remove `mwReference/` prefix
161 const group = groupName.slice( 12 );
162 const footnoteNumber = this.docRefs.getIndexLabel( group, listKey );
163 const footnoteLabel = ( group ? group + ' ' : '' ) + footnoteNumber;
164
165 // Use [\s\S]* instead of .* to catch esoteric whitespace (T263698)
166 const matches = listKey.match( /^literal\/([\s\S]*)$/ );
167 const name = matches && matches[ 1 ] || '';
168
169 let $refContent;
170 // Make visible text, footnoteLabel and reference name searchable
171 let refText = ( footnoteLabel + ' ' + name ).toLowerCase();
172 const itemNode = groupRefs.getInternalModelNode( listKey );
173 if ( itemNode.length ) {
174 $refContent = new ve.ui.MWPreviewElement( itemNode, { useView: true } ).$element;
175 refText = $refContent.text().toLowerCase() + ' ' + refText;
176 // Make URLs searchable
177 $refContent.find( 'a[href]' ).each( ( k, element ) => {
178 refText += ' ' + element.getAttribute( 'href' );
179 } );
180 } else {
181 $refContent = $( '<span>' )
182 .addClass( 've-ce-mwReferencesListNode-muted' )
183 .text( ve.msg( 'cite-ve-referenceslist-missingref-in-list' ) );
184 }
185
186 return {
187 $refContent: $refContent,
188 searchableText: refText,
189 // TODO: return a simple node
190 reference: ve.dm.MWReferenceModel.static.newFromReferenceNode( node ),
191 footnoteLabel: footnoteLabel,
192 isSubRef: !!node.getAttribute( 'extendsRef' ),
193 name: name
194 };
195 } ) );
196 }
197
198 return index;
199 };
200
201 /**
202 * @return {boolean} Index is empty
203 */
204 ve.ui.MWReferenceSearchWidget.prototype.isIndexEmpty = function () {
205 return !this.docRefs.hasRefs();
206 };
207
208 /**
209 * @private
210 * @param {ve.dm.MWReferenceModel} ref
211 * @return {OO.ui.ButtonMenuSelectWidget}
212 */
213 ve.ui.MWReferenceSearchWidget.prototype.buildReuseOptionsMenu = function ( ref ) {
214 // TODO: Select the row on menu button click, so we don't have to wire ref
215 // through the closure.
216 const reuseOptionsMenu = new OO.ui.ButtonMenuSelectWidget( {
217 classes: [ 've-ui-mwReferenceResultsReuseOptions' ],
218 framed: false,
219 icon: 'ellipsis',
220 // TODO: The […] button should have its own title, see T375053
221 title: '',
222 invisibleLabel: true,
223 menu: {
224 classes: [ 've-ui-mwReferenceResultsReuseOptionsItem' ],
225 horizontalPosition: 'end',
226 items: [
227 new OO.ui.MenuOptionWidget( {
228 data: 'reuse',
229 label: ve.msg( 'cite-ve-dialog-reference-useexisting-long-tool' )
230 } ),
231 new OO.ui.MenuOptionWidget( {
232 data: 'extends',
233 label: ve.msg( 'cite-ve-dialog-reference-extend-long-tool' )
234 } )
235 ]
236 },
237 // FIXME: Overlay clips to the dialog, should be full-screen.
238 $overlay: this.$overlay
239 } );
240
241 // Hack to prevent a menu button click from being handled by the top-level
242 // select item.
243 reuseOptionsMenu.$element.on( 'mousedown', ( e ) => {
244 e.stopPropagation();
245 } );
246
247 reuseOptionsMenu.getMenu().on( 'choose', ( menuOption ) => {
248 // Emit 'reuse' or 'extends' events, with the chosen ref.
249 this.emit( menuOption.getData(), ref );
250 } );
251
252 return reuseOptionsMenu;
253 };
254
255 /**
256 * @private
257 * @param {string} query
258 * @return {ve.ui.MWReferenceResultWidget[]}
259 */
260 ve.ui.MWReferenceSearchWidget.prototype.buildSearchResults = function ( query ) {
261 query = query.trim().toLowerCase();
262 const results = [];
263
264 if ( !this.index ) {
265 this.index = this.buildSearchIndex();
266 }
267
268 for ( let i = 0; i < this.index.length; i++ ) {
269 const item = this.index[ i ];
270 if ( item.searchableText.indexOf( query ) >= 0 ) {
271 results.push(
272 new ve.ui.MWReferenceResultWidget( {
273 item: item,
274 reuseMenu: item.isSubRef ? undefined : this.buildReuseOptionsMenu( item.reference )
275 } )
276 );
277 }
278 }
279
280 return results;
281 };