| 1 |
|
| 2 |
|
| 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 |
|
| 28 |
|
| 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 |
|
| 41 |
|
| 42 |
|
| 43 |
function renderReferencePreview( |
| 44 |
model |
| 45 |
) { |
| 46 |
const type = model.referenceType || 'generic'; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 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 |
|
| 77 |
Array.prototype.forEach.call( |
| 78 |
el.querySelectorAll( '.mwe-popups-extract a[href][class~="external"]:not([target])' ), |
| 79 |
( a ) => { |
| 80 |
a.target = '_blank'; |
| 81 |
|
| 82 |
a.rel = `${ a.rel ? `${ a.rel } ` : '' }noopener`; |
| 83 |
} |
| 84 |
); |
| 85 |
|
| 86 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 181 |
|
| 182 |
|
| 183 |
function createReferencePreview( model ) { |
| 184 |
return { |
| 185 |
el: renderReferencePreview( model ), |
| 186 |
hasThumbnail: false, |
| 187 |
isTall: false |
| 188 |
}; |
| 189 |
} |
| 190 |
|
| 191 |
module.exports = createReferencePreview; |