Debian Codegraph @

skins/MinervaNeue/resources/skins.minerva.scripts/downloadPageAction.js

raw symbols SCIP
1 const track = mw.track;
2 const MAX_PRINT_TIMEOUT = 3000;
3 let printSetTimeoutReference = 0;
4 const mobile = require( 'mobile.startup' );
5
6 /**
7 * Helper function to detect iOs
8 *
9 * @ignore
10 * @param {string} userAgent User Agent
11 * @return {boolean}
12 */
13 function isIos( userAgent ) {
14 return /ipad|iphone|ipod/i.test( userAgent );
15 }
16
17 /**
18 * Helper function to retrieve the Android version
19 *
20 * @ignore
21 * @param {string} userAgent User Agent
22 * @return {number|boolean} Integer version number, or false if not found
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 * Helper function to retrieve the Chrome/Chromium version
31 *
32 * @ignore
33 * @param {string} userAgent User Agent
34 * @return {number|boolean} Integer version number, or false if not found
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 * Checks whether DownloadIcon is available for given user agent
43 *
44 * @memberof DownloadIcon
45 * @instance
46 * @param {Window} windowObj
47 * @param {Page} page to download
48 * @param {string} userAgent User agent
49 * @param {number[]} supportedNamespaces where printing is possible
50 * @return {boolean}
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 // T309591: No window.print support
58 return false;
59 }
60
61 // Download button is restricted to certain namespaces T181152.
62 // Not shown on missing pages
63 // Defaults to 0, in case cached JS has been served.
64 if ( supportedNamespaces.indexOf( page.getNamespaceId() ) === -1 ||
65 page.isMainPage() || page.isMissing ) {
66 // namespace is not supported or it's a main page
67 return false;
68 }
69
70 if ( isIos( userAgent ) || chromeVersion === false ||
71 windowObj.chrome === undefined
72 ) {
73 // we support only chrome/chromium on desktop/android
74 return false;
75 }
76 if ( ( androidVersion && androidVersion < 5 ) || chromeVersion < 41 ) {
77 return false;
78 }
79 return true;
80 }
81 /**
82 * onClick handler for button that invokes print function
83 *
84 * @private
85 * @param {HTMLElement} portletItem
86 * @param {Icon} spinner
87 * @param {Function} [loadAllImagesInPage]
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 // The click handler may be invoked multiple times so if a pending print is occurring
107 // do nothing.
108 if ( !printSetTimeoutReference ) {
109 track( 'minerva.downloadAsPDF', {
110 action: 'fetchImages'
111 } );
112 $( icon ).hide();
113 spinner.$el.show();
114 // If all image downloads are taking longer to load then the MAX_PRINT_TIMEOUT
115 // abort the spinner and print regardless.
116 printSetTimeoutReference = setTimeout( doPrint, MAX_PRINT_TIMEOUT );
117 ( loadAllImagesInPage || mobile.loadAllImagesInPage )()
118 .then( doPrintBeforeTimeout, doPrintBeforeTimeout );
119 }
120 }
121
122 /**
123 * Generate a download icon for triggering print functionality if
124 * printing is available.
125 * Calling this method has side effects:
126 * It calls mw.util.addPortletLink and may inject an element into the page.
127 *
128 * @ignore
129 * @param {Page} page
130 * @param {number[]} supportedNamespaces
131 * @param {Window} [windowObj] window object
132 * @param {boolean} [overflowList] Append to overflow list
133 * @return {jQuery|null}
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 // FIXME: Use p-views when cache has cleared.
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 // id
154 'minerva-download',
155 // tooltip
156 mw.msg( 'minerva-download' ),
157 // access key
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 };