Debian Codegraph @

extensions/Cite/modules/ext.cite.referencePreviews/createReferencePreview.js

raw symbols SCIP
1 /**
2 * @module referencePreview
3 */
4 const { isTrackingEnabled, LOGGING_SCHEMA } = require( './referencePreviewsInstrumentation.js' );
5
6 const TEMPLATE = document.createElement( 'template' );
7 TEMPLATE.innerHTML = `
8 <div class="mwe-popups mwe-popups mwe-popups-type-reference" aria-hidden>
9 <div class="mwe-popups-container">
10 <div class="mwe-popups-extract">
11 <div class="mwe-popups-scroll">
12 <strong class="mwe-popups-title">
13 <span class="popups-icon"></span>
14 <span class="mwe-popups-title-placeholder"></span>
15 </strong>
16 <bdi><div class="mw-parser-output"></div></bdi>
17 </div>
18 <div class="mwe-popups-fade"></div>
19 </div>
20 <footer>
21 <div class="mwe-popups-settings"></div>
22 </footer>
23 </div>
24 </div>`;
25
26 /**
27 * @param {HTMLElement} node
28 * @param {HTMLElement|string} htmlOrOtherNode
29 */
30 const replaceWith = ( node, htmlOrOtherNode ) => {
31 if ( typeof htmlOrOtherNode === 'string' ) {
32 node.insertAdjacentHTML( 'afterend', htmlOrOtherNode );
33 } else {
34 node.parentNode.appendChild( htmlOrOtherNode );
35 }
36 node.remove();
37 };
38
39 /**
40 * @param {ext.popups.PreviewModel} model
41 * @return {jQuery}
42 */
43 function renderReferencePreview(
44 model
45 ) {
46 const type = model.referenceType || 'generic';
47 // The following messages are used here:
48 // * cite-reference-previews-book
49 // * cite-reference-previews-journal
50 // * cite-reference-previews-news
51 // * cite-reference-previews-note
52 // * cite-reference-previews-web
53 let titleMsg = mw.message( `cite-reference-previews-${ type }` );
54 if ( !titleMsg.exists() ) {
55 titleMsg = mw.message( 'cite-reference-previews-reference' );
56 }
57
58 const el = TEMPLATE.content.cloneNode( true ).children[ 0 ];
59
60 replaceWith(
61 el.querySelector( '.mwe-popups-title-placeholder' ),
62 mw.html.escape( titleMsg.text() )
63 );
64 // The following classes are used here:
65 // * popups-icon--reference-generic
66 // * popups-icon--reference-book
67 // * popups-icon--reference-journal
68 // * popups-icon--reference-news
69 // * popups-icon--reference-note
70 // * popups-icon--reference-web
71 el.querySelector( '.mwe-popups-title .popups-icon' )
72 .classList.add( `popups-icon--reference-${ type }` );
73 el.querySelector( '.mw-parser-output' )
74 .innerHTML = model.extract;
75
76 // Make sure to not destroy existing targets, if any
77 Array.prototype.forEach.call(
78 el.querySelectorAll( '.mwe-popups-extract a[href][class~="external"]:not([target])' ),
79 ( a ) => {
80 a.target = '_blank';
81 // Don't let the external site access and possibly manipulate window.opener.location
82 a.rel = `${ a.rel ? `${ a.rel } ` : '' }noopener`;
83 }
84 );
85
86 // We assume elements that benefit from being collapsible are to large for the popup
87 Array.prototype.forEach.call( el.querySelectorAll( '.mw-collapsible' ), ( node ) => {
88 const otherNode = document.createElement( 'div' );
89 otherNode.classList.add( 'mwe-collapsible-placeholder' );
90 const icon = document.createElement( 'span' );
91 icon.classList.add( 'popups-icon', 'popups-icon--infoFilled' );
92 const label = document.createElement( 'span' );
93 label.classList.add( 'mwe-collapsible-placeholder-label' );
94 label.textContent = mw.msg( 'cite-reference-previews-collapsible-placeholder' );
95 otherNode.appendChild( icon );
96 otherNode.appendChild( label );
97 replaceWith( node, otherNode );
98 } );
99
100 // Undo remaining effects from the jquery.tablesorter.js plugin
101 const undoHeaderSort = ( headerSort ) => {
102 headerSort.classList.remove( 'headerSort' );
103 headerSort.removeAttribute( 'tabindex' );
104 headerSort.removeAttribute( 'title' );
105 };
106 Array.prototype.forEach.call( el.querySelectorAll( 'table.sortable' ), ( node ) => {
107 node.classList.remove( 'sortable', 'jquery-tablesorter' );
108 Array.prototype.forEach.call( node.querySelectorAll( '.headerSort' ), undoHeaderSort );
109 } );
110
111 // TODO: Do not remove this but move it up into the templateHTML constant!
112 const settingsButton = document.createElement( 'a' );
113 settingsButton.classList.add( 'cdx-button', 'cdx-button--fake-button', 'cdx-button--fake-button--enabled', 'cdx-button--weight-quiet', 'cdx-button--icon-only', 'mwe-popups-settings-button' );
114 const settingsIcon = document.createElement( 'span' );
115 settingsIcon.classList.add( 'popups-icon', 'popups-icon--size-small', 'popups-icon--settings' );
116 const settingsButtonLabel = document.createElement( 'span' );
117 settingsButtonLabel.textContent = mw.msg( 'popups-settings-icon-gear-title' );
118 settingsButton.append( settingsIcon );
119 settingsButton.append( settingsButtonLabel );
120 el.querySelector( '.mwe-popups-settings' ).appendChild( settingsButton );
121
122 if ( isTrackingEnabled() ) {
123 el.querySelector( '.mw-parser-output' ).addEventListener( 'click', ( ev ) => {
124 if ( !ev.target.matches( 'a' ) ) {
125 return;
126 }
127 mw.track( LOGGING_SCHEMA, {
128 action: 'clickedReferencePreviewsContentLink'
129 } );
130 } );
131 }
132
133 el.querySelector( '.mwe-popups-scroll' ).addEventListener( 'scroll', ( e ) => {
134 const element = e.target,
135 // We are dealing with floating point numbers here when the page is zoomed!
136 scrolledToBottom = element.scrollTop >= element.scrollHeight - element.clientHeight - 1;
137
138 if ( isTrackingEnabled() ) {
139 if ( !element.isOpenRecorded ) {
140 mw.track( LOGGING_SCHEMA, {
141 action: 'poppedOpen',
142 scrollbarsPresent: element.scrollHeight > element.clientHeight
143 } );
144 element.isOpenRecorded = true;
145 }
146
147 if (
148 element.scrollTop > 0 &&
149 !element.isScrollRecorded
150 ) {
151 mw.track( LOGGING_SCHEMA, {
152 action: 'scrolled'
153 } );
154 element.isScrollRecorded = true;
155 }
156 }
157
158 if ( !scrolledToBottom && element.isScrolling ) {
159 return;
160 }
161
162 const extract = element.parentNode,
163 hasHorizontalScroll = element.scrollWidth > element.clientWidth,
164 scrollbarHeight = element.offsetHeight - element.clientHeight,
165 hasVerticalScroll = element.scrollHeight > element.clientHeight,
166 scrollbarWidth = element.offsetWidth - element.clientWidth;
167 const fade = extract.querySelector( '.mwe-popups-fade' );
168 fade.style.bottom = hasHorizontalScroll ? `${ scrollbarHeight }px` : 0;
169 fade.style.right = hasVerticalScroll ? `${ scrollbarWidth }px` : 0;
170
171 element.isScrolling = !scrolledToBottom;
172 extract.classList.toggle( 'mwe-popups-fade-out', element.isScrolling );
173 extract.setAttribute( 'lang', mw.config.get( 'wgPageContentLanguage' ) );
174 } );
175
176 return el;
177 }
178
179 /**
180 * @param {ext.popups.PreviewModel} model
181 * @return {ext.popups.Preview}
182 */
183 function createReferencePreview( model ) {
184 return {
185 el: renderReferencePreview( model ),
186 hasThumbnail: false,
187 isTall: false
188 };
189 }
190
191 module.exports = createReferencePreview;