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();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to retreive the Android version
|
|
|
|
* @ignore
|
|
|
|
* @param {String} userAgent User Agent
|
|
|
|
* @return {Integer}
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
* @param {String} userAgent User Agent
|
|
|
|
* @return {Integer}
|
|
|
|
*/
|
|
|
|
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-01-12 17:45:39 +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-03-15 20:27:39 +00:00
|
|
|
* @module skins.minerva.scripts/DownloadIcon
|
2017-10-18 21:03:51 +00:00
|
|
|
*/
|
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
|
|
|
|
* @param {string} userAgent User agent
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
// 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 );
|
|
|
|
this.skin.loadImagesList().always( function () {
|
|
|
|
if ( self.timeout ) {
|
|
|
|
doPrint();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
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 ) );
|