2018-11-08 00:06:54 +00:00
|
|
|
( function ( M, track, msg ) {
|
2018-12-20 22:02:57 +00:00
|
|
|
var MAX_PRINT_TIMEOUT = 3000,
|
2019-01-08 20:35:28 +00:00
|
|
|
mobile = M.require( 'mobile.startup' ),
|
|
|
|
Icon = mobile.Icon,
|
|
|
|
icons = mobile.icons,
|
|
|
|
lazyImageLoader = mobile.lazyImages.lazyImageLoader,
|
2019-02-07 16:34:18 +00:00
|
|
|
browser = mobile.Browser.getSingleton(),
|
2019-01-08 20:35:28 +00:00
|
|
|
GLYPH = 'download';
|
2018-11-08 00:06:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to retrieve the Android version
|
2020-06-02 21:21:44 +00:00
|
|
|
*
|
2018-11-08 00:06:54 +00:00
|
|
|
* @ignore
|
|
|
|
* @param {string} userAgent User Agent
|
|
|
|
* @return {number|false} An integer.
|
|
|
|
*/
|
|
|
|
function getAndroidVersion( userAgent ) {
|
|
|
|
var match = userAgent.toLowerCase().match( /android\s(\d\.]*)/ );
|
2018-11-27 12:29:39 +00:00
|
|
|
return match ? parseInt( match[ 1 ] ) : false;
|
2018-11-08 00:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to retrieve the Chrome/Chromium version
|
2020-06-02 21:21:44 +00:00
|
|
|
*
|
2018-11-08 00:06:54 +00:00
|
|
|
* @ignore
|
|
|
|
* @param {string} userAgent User Agent
|
|
|
|
* @return {number|false} An integer.
|
|
|
|
*/
|
|
|
|
function getChromeVersion( userAgent ) {
|
|
|
|
var match = userAgent.toLowerCase().match( /chrom(e|ium)\/(\d+)\./ );
|
2018-11-27 12:29:39 +00:00
|
|
|
return match ? parseInt( match[ 2 ] ) : false;
|
2018-11-08 00:06:54 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 22:02:57 +00:00
|
|
|
/**
|
|
|
|
* Checks whether DownloadIcon is available for given user agent
|
|
|
|
*
|
|
|
|
* @memberof DownloadIcon
|
|
|
|
* @instance
|
|
|
|
* @param {Window} windowObj
|
|
|
|
* @param {Page} page to download
|
|
|
|
* @param {string} userAgent User agent
|
|
|
|
* @param {number[]} supportedNamespaces where printing is possible
|
|
|
|
* @return {boolean}
|
2020-06-02 21:21:44 +00:00
|
|
|
*/
|
2018-12-20 22:02:57 +00:00
|
|
|
function isAvailable( windowObj, page, userAgent, supportedNamespaces ) {
|
|
|
|
var androidVersion = getAndroidVersion( userAgent ),
|
|
|
|
chromeVersion = getChromeVersion( userAgent );
|
2018-11-08 00:06:54 +00:00
|
|
|
|
2018-12-20 22:02:57 +00:00
|
|
|
// Download button is restricted to certain namespaces T181152.
|
2019-07-31 23:17:20 +00:00
|
|
|
// Not shown on missing pages
|
2018-12-20 22:02:57 +00:00
|
|
|
// Defaults to 0, in case cached JS has been served.
|
|
|
|
if ( supportedNamespaces.indexOf( page.getNamespaceId() ) === -1 ||
|
2019-07-31 23:17:20 +00:00
|
|
|
page.isMainPage() || page.isMissing ) {
|
2018-12-20 22:02:57 +00:00
|
|
|
// namespace is not supported or it's a main page
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-08 00:06:54 +00:00
|
|
|
|
2018-12-20 22:02:57 +00:00
|
|
|
if ( browser.isIos() || chromeVersion === false ||
|
|
|
|
windowObj.chrome === undefined
|
|
|
|
) {
|
|
|
|
// we support only chrome/chromium on desktop/android
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( ( androidVersion && androidVersion < 5 ) || chromeVersion < 41 ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* onClick handler for button that invokes print function
|
2020-06-02 21:21:44 +00:00
|
|
|
*
|
2018-12-20 22:02:57 +00:00
|
|
|
* @param {Icon} icon
|
|
|
|
* @param {Icon} spinner
|
|
|
|
*/
|
2019-04-10 20:23:58 +00:00
|
|
|
function onClick( icon, spinner ) {
|
2018-12-20 22:02:57 +00:00
|
|
|
function doPrint() {
|
|
|
|
icon.timeout = clearTimeout( icon.timeout );
|
|
|
|
track( 'minerva.downloadAsPDF', {
|
|
|
|
action: 'callPrint'
|
|
|
|
} );
|
|
|
|
window.print();
|
|
|
|
icon.$el.show();
|
|
|
|
spinner.$el.hide();
|
|
|
|
}
|
2018-11-08 00:06:54 +00:00
|
|
|
|
2018-12-20 22:02:57 +00:00
|
|
|
function doPrintBeforeTimeout() {
|
|
|
|
if ( icon.timeout ) {
|
|
|
|
doPrint();
|
2018-11-08 00:06:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-20 22:02:57 +00:00
|
|
|
// The click handler may be invoked multiple times so if a pending print is occurring
|
|
|
|
// do nothing.
|
|
|
|
if ( !icon.timeout ) {
|
|
|
|
track( 'minerva.downloadAsPDF', {
|
|
|
|
action: 'fetchImages'
|
|
|
|
} );
|
|
|
|
icon.$el.hide();
|
|
|
|
spinner.$el.show();
|
|
|
|
// If all image downloads are taking longer to load then the MAX_PRINT_TIMEOUT
|
|
|
|
// abort the spinner and print regardless.
|
|
|
|
icon.timeout = setTimeout( doPrint, MAX_PRINT_TIMEOUT );
|
2019-01-10 20:53:28 +00:00
|
|
|
lazyImageLoader.loadImages( lazyImageLoader.queryPlaceholders( document.getElementById( 'content' ) ) )
|
|
|
|
.then( doPrintBeforeTimeout, doPrintBeforeTimeout );
|
2018-12-20 22:02:57 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-08 00:06:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a click handler for the download icon
|
|
|
|
* Expects to be run in the context of an icon using `Function.bind`
|
|
|
|
*
|
|
|
|
* @param {Icon} spinner
|
2020-02-07 20:59:24 +00:00
|
|
|
* @return {Function}
|
2018-11-08 00:06:54 +00:00
|
|
|
*/
|
2019-04-10 20:23:58 +00:00
|
|
|
function getOnClickHandler( spinner ) {
|
2018-11-08 00:06:54 +00:00
|
|
|
return function () {
|
2019-04-10 20:23:58 +00:00
|
|
|
onClick( this, spinner );
|
2018-11-08 00:06:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a download icon for triggering print functionality if
|
|
|
|
* printing is available
|
|
|
|
*
|
2019-07-31 23:17:20 +00:00
|
|
|
* @param {Page} page
|
2018-11-08 00:06:54 +00:00
|
|
|
* @param {number[]} supportedNamespaces
|
|
|
|
* @param {Window} [windowObj] window object
|
2019-04-04 21:20:39 +00:00
|
|
|
* @param {boolean} [hasText] Use icon + button style.
|
2020-02-07 20:59:24 +00:00
|
|
|
* @return {jQuery.Object|null}
|
2018-11-08 00:06:54 +00:00
|
|
|
*/
|
2019-07-31 23:17:20 +00:00
|
|
|
function downloadPageAction( page, supportedNamespaces, windowObj, hasText ) {
|
2019-04-04 21:20:39 +00:00
|
|
|
var
|
2019-11-05 18:52:01 +00:00
|
|
|
modifier = hasText ? 'toggle-list-item__anchor toggle-list-item__label' : 'mw-ui-icon-element mw-ui-icon-with-label-desktop',
|
2019-04-04 21:20:39 +00:00
|
|
|
icon,
|
|
|
|
spinner = icons.spinner( {
|
|
|
|
hasText: hasText,
|
|
|
|
modifier: modifier
|
|
|
|
} );
|
2019-04-10 20:31:14 +00:00
|
|
|
|
2018-11-08 00:06:54 +00:00
|
|
|
if (
|
2018-12-20 22:02:57 +00:00
|
|
|
isAvailable(
|
2019-07-31 23:17:20 +00:00
|
|
|
windowObj, page, navigator.userAgent,
|
2018-11-08 00:06:54 +00:00
|
|
|
supportedNamespaces
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
icon = new Icon( {
|
|
|
|
glyphPrefix: 'minerva',
|
|
|
|
title: msg( 'minerva-download' ),
|
|
|
|
name: GLYPH,
|
2019-04-10 20:31:14 +00:00
|
|
|
tagName: 'button',
|
2018-11-08 00:06:54 +00:00
|
|
|
events: {
|
|
|
|
// will be bound to `this`
|
2019-04-10 20:23:58 +00:00
|
|
|
click: getOnClickHandler( spinner )
|
2019-04-04 21:20:39 +00:00
|
|
|
},
|
|
|
|
hasText: hasText,
|
2019-08-30 17:23:55 +00:00
|
|
|
label: mw.msg( 'minerva-download' ),
|
2019-04-04 21:20:39 +00:00
|
|
|
modifier: modifier
|
2018-11-08 00:06:54 +00:00
|
|
|
} );
|
2019-04-10 20:31:14 +00:00
|
|
|
|
2019-08-30 17:23:55 +00:00
|
|
|
return $( '<li>' ).addClass( hasText ? 'toggle-list-item' : 'page-actions-menu__list-item' ).append( icon.$el ).append( spinner.$el.hide() );
|
2018-11-08 00:06:54 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 21:10:10 +00:00
|
|
|
module.exports = {
|
|
|
|
downloadPageAction: downloadPageAction,
|
|
|
|
test: {
|
|
|
|
isAvailable: isAvailable,
|
|
|
|
getOnClickHandler: getOnClickHandler
|
|
|
|
}
|
|
|
|
};
|
2019-09-27 18:53:01 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line no-restricted-properties
|
2018-11-08 00:06:54 +00:00
|
|
|
}( mw.mobileFrontend, mw.track, mw.msg ) );
|