| 1 |
const track = mw.track; |
| 2 |
const MAX_PRINT_TIMEOUT = 3000; |
| 3 |
let printSetTimeoutReference = 0; |
| 4 |
const mobile = require( 'mobile.startup' ); |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
function isIos( userAgent ) { |
| 14 |
return /ipad|iphone|ipod/i.test( userAgent ); |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
function getAndroidVersion( userAgent ) { |
| 25 |
const match = userAgent.toLowerCase().match( /android\s(\d\.]*)/ ); |
| 26 |
return match ? parseInt( match[ 1 ] ) : false; |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
function getChromeVersion( userAgent ) { |
| 37 |
const match = userAgent.toLowerCase().match( /chrom(e|ium)\/(\d+)\./ ); |
| 38 |
return match ? parseInt( match[ 2 ] ) : false; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
function isAvailable( windowObj, page, userAgent, supportedNamespaces ) { |
| 53 |
const androidVersion = getAndroidVersion( userAgent ); |
| 54 |
const chromeVersion = getChromeVersion( userAgent ); |
| 55 |
|
| 56 |
if ( typeof window.print !== 'function' ) { |
| 57 |
|
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
if ( supportedNamespaces.indexOf( page.getNamespaceId() ) === -1 || |
| 65 |
page.isMainPage() || page.isMissing ) { |
| 66 |
|
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
if ( isIos( userAgent ) || chromeVersion === false || |
| 71 |
windowObj.chrome === undefined |
| 72 |
) { |
| 73 |
|
| 74 |
return false; |
| 75 |
} |
| 76 |
if ( ( androidVersion && androidVersion < 5 ) || chromeVersion < 41 ) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
function onClick( portletItem, spinner, loadAllImagesInPage ) { |
| 90 |
const icon = portletItem.querySelector( '.minerva-icon--download' ); |
| 91 |
function doPrint() { |
| 92 |
printSetTimeoutReference = clearTimeout( printSetTimeoutReference ); |
| 93 |
track( 'minerva.downloadAsPDF', { |
| 94 |
action: 'callPrint' |
| 95 |
} ); |
| 96 |
window.print(); |
| 97 |
$( icon ).show(); |
| 98 |
spinner.$el.hide(); |
| 99 |
} |
| 100 |
|
| 101 |
function doPrintBeforeTimeout() { |
| 102 |
if ( printSetTimeoutReference ) { |
| 103 |
doPrint(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
if ( !printSetTimeoutReference ) { |
| 109 |
track( 'minerva.downloadAsPDF', { |
| 110 |
action: 'fetchImages' |
| 111 |
} ); |
| 112 |
$( icon ).hide(); |
| 113 |
spinner.$el.show(); |
| 114 |
|
| 115 |
|
| 116 |
printSetTimeoutReference = setTimeout( doPrint, MAX_PRINT_TIMEOUT ); |
| 117 |
( loadAllImagesInPage || mobile.loadAllImagesInPage )() |
| 118 |
.then( doPrintBeforeTimeout, doPrintBeforeTimeout ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
function downloadPageAction( page, supportedNamespaces, windowObj, overflowList ) { |
| 136 |
const spinner = ( overflowList ) ? mobile.spinner( { |
| 137 |
label: '', |
| 138 |
isIconOnly: false |
| 139 |
} ) : mobile.spinner(); |
| 140 |
|
| 141 |
if ( |
| 142 |
isAvailable( |
| 143 |
windowObj, page, navigator.userAgent, |
| 144 |
supportedNamespaces |
| 145 |
) |
| 146 |
) { |
| 147 |
|
| 148 |
const actionID = document.querySelector( '#p-views' ) ? 'p-views' : 'page-actions'; |
| 149 |
const portletLink = mw.util.addPortletLink( |
| 150 |
overflowList ? 'page-actions-overflow' : actionID, |
| 151 |
'#', |
| 152 |
mw.msg( 'minerva-download' ), |
| 153 |
|
| 154 |
'minerva-download', |
| 155 |
|
| 156 |
mw.msg( 'minerva-download' ), |
| 157 |
|
| 158 |
'p', |
| 159 |
overflowList ? null : document.getElementById( 'page-actions-watch' ) |
| 160 |
); |
| 161 |
if ( portletLink ) { |
| 162 |
portletLink.addEventListener( 'click', () => { |
| 163 |
onClick( portletLink, spinner, mobile.loadAllImagesInPage ); |
| 164 |
} ); |
| 165 |
const iconElement = portletLink.querySelector( '.minerva-icon' ); |
| 166 |
if ( iconElement ) { |
| 167 |
iconElement.classList.add( 'minerva-icon--download' ); |
| 168 |
} |
| 169 |
spinner.$el.hide().insertBefore( |
| 170 |
$( portletLink ).find( '.minerva-icon' ) |
| 171 |
); |
| 172 |
} |
| 173 |
return portletLink; |
| 174 |
} else { |
| 175 |
return null; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
module.exports = { |
| 180 |
downloadPageAction, |
| 181 |
test: { |
| 182 |
isAvailable, |
| 183 |
onClick |
| 184 |
} |
| 185 |
}; |