2018-01-09 15:40:36 +00:00
|
|
|
( function ( M, track ) {
|
2017-10-18 21:03:51 +00:00
|
|
|
var msg = mw.msg,
|
2017-11-15 22:37:37 +00:00
|
|
|
MAX_PRINT_TIMEOUT = 3000,
|
2018-01-05 18:52:17 +00:00
|
|
|
GLYPH = 'download',
|
2018-01-12 17:45:39 +00:00
|
|
|
Icon = M.require( 'mobile.startup/Icon' ),
|
|
|
|
browser = M.require( 'mobile.startup/Browser' ).getSingleton();
|
|
|
|
|
|
|
|
/**
|
2018-08-23 22:27:22 +00:00
|
|
|
* Helper function to retrieve the Android version
|
2018-01-12 17:45:39 +00:00
|
|
|
* @ignore
|
2018-07-03 14:50:09 +00:00
|
|
|
* @param {string} userAgent User Agent
|
2018-07-11 18:39:55 +00:00
|
|
|
* @return {number|false} An integer.
|
2018-01-12 17:45:39 +00:00
|
|
|
*/
|
|
|
|
function getAndroidVersion( userAgent ) {
|
|
|
|
var match = userAgent.toLowerCase().match( /android\s(\d\.]*)/ );
|
|
|
|
return match ? parseInt( match[1] ) : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to retrieve the Chrome/Chromium version
|
|
|
|
* @ignore
|
2018-07-03 14:50:09 +00:00
|
|
|
* @param {string} userAgent User Agent
|
2018-07-11 18:39:55 +00:00
|
|
|
* @return {number|false} An integer.
|
2018-01-12 17:45:39 +00:00
|
|
|
*/
|
|
|
|
function getChromeVersion( userAgent ) {
|
|
|
|
var match = userAgent.toLowerCase().match( /chrom(e|ium)\/(\d+)\./ );
|
|
|
|
return match ? parseInt( match[2] ) : false;
|
|
|
|
}
|
2017-10-18 21:03:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A download icon for triggering print functionality
|
|
|
|
* @class DownloadIcon
|
|
|
|
* @extends Icon
|
|
|
|
*
|
2017-11-15 22:37:37 +00:00
|
|
|
* @param {Skin} skin
|
2018-07-03 14:50:09 +00:00
|
|
|
* @param {number[]} [supportedNamespaces]
|
2018-02-01 18:59:18 +00:00
|
|
|
* @param {Window} [windowObj] window object
|
2017-10-18 21:03:51 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
2018-02-01 18:59:18 +00:00
|
|
|
function DownloadIcon( skin, supportedNamespaces, windowObj ) {
|
2017-10-18 21:03:51 +00:00
|
|
|
var options = {};
|
2017-11-15 22:37:37 +00:00
|
|
|
this.skin = skin;
|
2018-02-01 18:59:18 +00:00
|
|
|
this.window = windowObj || {};
|
2018-01-12 17:45:39 +00:00
|
|
|
this.supportedNamespaces = supportedNamespaces || [ 0 ];
|
2017-10-18 21:03:51 +00:00
|
|
|
options.tagName = 'li';
|
2018-01-05 18:52:17 +00:00
|
|
|
options.glyphPrefix = 'minerva';
|
2017-10-18 21:03:51 +00:00
|
|
|
options.title = msg( 'minerva-download' );
|
2017-12-07 19:58:06 +00:00
|
|
|
options.name = GLYPH;
|
2017-10-18 21:03:51 +00:00
|
|
|
Icon.call( this, options );
|
|
|
|
}
|
|
|
|
|
|
|
|
OO.mfExtend( DownloadIcon, Icon, {
|
2018-01-12 17:45:39 +00:00
|
|
|
/**
|
|
|
|
* Checks whether DownloadIcon is available for given user agent
|
2018-08-20 23:40:40 +00:00
|
|
|
*
|
|
|
|
* @memberof DownloadIcon
|
|
|
|
* @instance
|
2018-01-12 17:45:39 +00:00
|
|
|
* @param {string} userAgent User agent
|
2018-07-03 14:50:09 +00:00
|
|
|
* @return {boolean}
|
2018-01-12 17:45:39 +00:00
|
|
|
*/
|
|
|
|
isAvailable: function ( userAgent ) {
|
|
|
|
var androidVersion = getAndroidVersion( userAgent ),
|
|
|
|
chromeVersion = getChromeVersion( userAgent ),
|
|
|
|
page = this.skin.page;
|
|
|
|
|
|
|
|
// Download button is restricted to certain namespaces T181152.
|
|
|
|
// Defaults to 0, in case cached JS has been served.
|
|
|
|
if ( this.supportedNamespaces.indexOf( page.getNamespaceId() ) === -1 ||
|
|
|
|
page.isMainPage() ) {
|
|
|
|
// namespace is not supported or it's a main page
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-01 18:59:18 +00:00
|
|
|
if ( browser.isIos() || chromeVersion === false ||
|
|
|
|
this.window.chrome === undefined
|
|
|
|
) {
|
2018-01-12 17:45:39 +00:00
|
|
|
// we support only chrome/chromium on desktop/android
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( ( androidVersion && androidVersion < 5 ) || chromeVersion < 41 ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2017-11-15 22:37:37 +00:00
|
|
|
/**
|
|
|
|
* Replace download icon with a spinner
|
2018-08-20 23:40:40 +00:00
|
|
|
* @memberof DownloadIcon
|
|
|
|
* @instance
|
2017-11-15 22:37:37 +00:00
|
|
|
*/
|
|
|
|
showSpinner: function () {
|
2018-01-29 18:22:09 +00:00
|
|
|
// FIXME: There is no spinner icon in Minerva, only in MobileFrontend
|
|
|
|
// Hopefully when T177432 is resolved this and corresponding change in hideSpinner
|
|
|
|
// should be unnecessary.
|
|
|
|
this.options.glyphPrefix = 'mf';
|
2017-11-15 22:37:37 +00:00
|
|
|
this.options.name = 'spinner';
|
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Restore download icon from spinner state
|
2018-08-20 23:40:40 +00:00
|
|
|
* @memberof DownloadIcon
|
|
|
|
* @instance
|
2017-11-15 22:37:37 +00:00
|
|
|
*/
|
|
|
|
hideSpinner: function () {
|
2018-01-29 18:22:09 +00:00
|
|
|
this.options.glyphPrefix = 'minerva';
|
2017-12-07 19:58:06 +00:00
|
|
|
this.options.name = GLYPH;
|
2017-11-15 22:37:37 +00:00
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
isTemplateMode: false,
|
|
|
|
/**
|
|
|
|
* onClick handler for button that invokes print function
|
2018-08-20 23:40:40 +00:00
|
|
|
* @memberof DownloadIcon
|
|
|
|
* @instance
|
2017-11-15 22:37:37 +00:00
|
|
|
*/
|
2017-10-18 21:03:51 +00:00
|
|
|
onClick: function () {
|
2017-11-15 22:37:37 +00:00
|
|
|
var self = this,
|
|
|
|
hideSpinner = this.hideSpinner.bind( this );
|
|
|
|
|
|
|
|
function doPrint() {
|
|
|
|
self.timeout = clearTimeout( self.timeout );
|
2018-01-09 15:40:36 +00:00
|
|
|
track( 'minerva.downloadAsPDF', {
|
|
|
|
action: 'callPrint'
|
|
|
|
} );
|
2017-11-15 22:37:37 +00:00
|
|
|
window.print();
|
2017-11-27 20:33:14 +00:00
|
|
|
hideSpinner();
|
2017-11-15 22:37:37 +00:00
|
|
|
}
|
2018-08-22 00:26:46 +00:00
|
|
|
|
|
|
|
function doPrintBeforeTimeout() {
|
|
|
|
if ( self.timeout ) {
|
|
|
|
doPrint();
|
|
|
|
}
|
|
|
|
}
|
2017-11-15 22:37:37 +00:00
|
|
|
// The click handler may be invoked multiple times so if a pending print is occurring
|
|
|
|
// do nothing.
|
|
|
|
if ( !this.timeout ) {
|
2018-01-09 15:40:36 +00:00
|
|
|
track( 'minerva.downloadAsPDF', {
|
|
|
|
action: 'fetchImages'
|
|
|
|
} );
|
2017-11-15 22:37:37 +00:00
|
|
|
this.showSpinner();
|
|
|
|
// If all image downloads are taking longer to load then the MAX_PRINT_TIMEOUT
|
|
|
|
// abort the spinner and print regardless.
|
|
|
|
this.timeout = setTimeout( doPrint, MAX_PRINT_TIMEOUT );
|
2018-08-22 00:26:46 +00:00
|
|
|
this.skin.loadImagesList().then( doPrintBeforeTimeout, doPrintBeforeTimeout );
|
2017-11-15 22:37:37 +00:00
|
|
|
}
|
2017-10-18 21:03:51 +00:00
|
|
|
},
|
|
|
|
events: {
|
|
|
|
click: 'onClick'
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
M.define( 'skins.minerva.scripts/DownloadIcon', DownloadIcon );
|
2018-01-09 15:40:36 +00:00
|
|
|
}( mw.mobileFrontend, mw.track ) );
|