Bmansurov has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/287143

Change subject: WIP
......................................................................

WIP

Change-Id: I2605a89289ee7d665c5ec49ad55016b9ada045c2
---
M extension.json
A resources/skins.minerva.beta.scripts/print.js
A resources/skins.minerva.print/Printer.js
A resources/skins.minerva.print/print.less
4 files changed, 230 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/43/287143/1

diff --git a/extension.json b/extension.json
index 210cf29..10d3f57 100644
--- a/extension.json
+++ b/extension.json
@@ -1785,7 +1785,8 @@
                        ],
                        "scripts": [
                                
"resources/skins.minerva.beta.scripts/commonsCategory.js",
-                               
"resources/skins.minerva.beta.scripts/fontchanger.js"
+                               
"resources/skins.minerva.beta.scripts/fontchanger.js",
+                               "resources/skins.minerva.beta.scripts/print.js"
                        ],
                        "messages": [
                                "mobile-frontend-commons-category-view"
@@ -1801,6 +1802,22 @@
                        ],
                        "scripts": [
                                "resources/skins.minerva.tablet.scripts/toc.js"
+                       ]
+               },
+               "skins.minerva.print": {
+                       "targets": [
+                               "mobile",
+                               "desktop"
+                       ],
+                       "dependencies": [
+                               "mediawiki.api",
+                               "mediawiki.Title"
+                       ],
+                       "scripts": [
+                               "resources/skins.minerva.print/Printer.js"
+                       ],
+                       "styles": [
+                               "resources/skins.minerva.print/print.less"
                        ]
                }
        },
@@ -1997,7 +2014,7 @@
                },
                "MFLazyLoadImages": {
                        "base": false,
-                       "beta": true
+                       "beta": false
                },
                "MFLazyLoadReferences": {
                        "base": false,
diff --git a/resources/skins.minerva.beta.scripts/print.js 
b/resources/skins.minerva.beta.scripts/print.js
new file mode 100644
index 0000000..bca4a3b
--- /dev/null
+++ b/resources/skins.minerva.beta.scripts/print.js
@@ -0,0 +1,17 @@
+( function ( M, $ ) {
+       var Button = M.require( 'mobile.startup/Button' ),
+               printButton = new Button( {
+                       label: 'print for offline reading'  // TODO: use mw.msg
+               } );
+
+       printButton.$el.on( 'click', function () {
+               //console.log('printing...');
+               mw.loader.using( 'skins.minerva.print' ).done( function () {
+                       var Printer = M.require( 'skins.minerva.print/Printer' 
);
+
+                       new Printer().print();
+               } );
+       } );
+
+       $( '#page-secondary-actions' ).append( printButton.$el );
+}( mw.mobileFrontend, jQuery ) );
diff --git a/resources/skins.minerva.print/Printer.js 
b/resources/skins.minerva.print/Printer.js
new file mode 100644
index 0000000..d3b57ab
--- /dev/null
+++ b/resources/skins.minerva.print/Printer.js
@@ -0,0 +1,150 @@
+( function ( M, $ ) {
+       var contentLinks;
+
+       var IGNORE_CLASSES = [
+               '.extiw',
+               '.image',
+               '.new',
+               '.internal',
+               '.external',
+               '.oo-ui-buttonedElement-button',
+               '.cancelLink a',
+               '.edit-page'
+       ];
+       /**
+        * Given an href string for the local wiki, return the title, or 
undefined if
+        * the link is external, has extra query parameters, or contains no 
title.
+        *
+        * Note that the returned title is not sanitized (may contain 
underscores).
+        *
+        * @param {string} href
+        * @return {string|undefined}
+        *
+        * stolen from the Popups extension
+        */
+        function getTitle( href ) {
+               var title, titleRegex, matches, linkHref;
+
+               // Skip every URI that mw.Uri cannot parse
+               try {
+                       linkHref = new mw.Uri( href );
+               } catch ( e ) {
+                       return undefined;
+               }
+
+               // External links
+               if ( linkHref.host !== location.hostname ) {
+                       return undefined;
+               }
+
+               if ( linkHref.query.hasOwnProperty( 'title' ) ) {
+                       // linkHref is not a pretty URL, e.g. 
/w/index.php?title=Foo
+
+                       title = linkHref.query.title;
+                       // Return undefined if there are query parameters other 
than title
+                       delete linkHref.query.title;
+                       return $.isEmptyObject( linkHref.query ) ? title : 
undefined;
+               } else {
+                       // linkHref is a pretty URL, e.g. /wiki/Foo
+
+                       // Return undefined if there are any query parameters
+                       if ( !$.isEmptyObject( linkHref.query ) ) {
+                               return undefined;
+                       }
+                       titleRegex = new RegExp( mw.RegExp.escape( 
mw.config.get( 'wgArticlePath' ) )
+                               .replace( '\\$1', '(.+)' ) );
+                       matches = titleRegex.exec( linkHref.path );
+                       return matches ? decodeURIComponent( matches[ 1 ] ) : 
undefined;
+               }
+       }
+
+       function applyPrintStyles() {
+               $( 'body' ).addClass( 'print' );
+       }
+
+       function removePrintStyles() {
+               $( 'body' ).removeClass( 'print' );
+       }
+
+       function getContentLinks() {
+               var contentNamespaces = mw.config.get( 'wgContentNamespaces' );
+
+               if ( !contentLinks ) {
+                       contentLinks = $.grep( $( 'a[href][title]:not(' + 
IGNORE_CLASSES.join( ', ' ) + ')', '#bodyContent' ), function () {
+                               var title,
+                                       titleText = getTitle( this.href );
+                               if ( !titleText ) {
+                                       return false;
+                               }
+                               // Is titleText in a content namespace?
+                               title = mw.Title.newFromText( titleText );
+                               return title && ( $.inArray( title.namespace, 
contentNamespaces ) >= 0 );
+                       } );
+               }
+               return contentLinks;
+       }
+
+       function getPageExtracts( pageTitles ) {
+               var api = new mw.Api();
+               console.log( pageTitles );
+
+               return api.get( {
+                       action: 'query',
+                       prop: 'extracts',
+                       formatversion: 2,
+                       redirects: true,
+                       exintro: true,
+                       exsentences: 5,
+                       explaintext: true,
+                       titles: pageTitles.join('|'),
+                       smaxage: 300,
+                       maxage: 300,
+                       uselang: 'content'
+               } ).then( function ( result ) {
+                       console.log( result) ;
+                       var pages = {};
+
+                       $.each( result.query.pages, function ( i, page ) {
+                               if ( page.extract ) {
+                                       pages[ page.title ] = page.extract;
+                               }
+                       } );
+
+                       return pages;
+               } );
+       }
+
+       function addExtractsToPage( pages ) {
+               $.each( pages, function ( title, extract ) {
+                       $( '<span class="print-extract">' + extract + '</span>' 
)
+                               .insertAfter( $( 'a[title="' + title + '"]', 
'#bodyContent' ) );
+               } );
+       }
+
+       function removeExtractsFromPage() {
+               $( '.print-extract', '#bodyContent' ).remove();
+       }
+
+       function Printer () {
+
+       }
+
+       Printer.prototype.print = function () {
+               var contentLinks = getContentLinks(),
+                       pageTitles = [];
+
+               $.each( contentLinks, function ( i, link ) {
+                       pageTitles.push( link.title );
+               } );
+               getPageExtracts( pageTitles ).done( function ( pages ) {
+                       addExtractsToPage( pages );
+                       applyPrintStyles();
+                       console.log('printing');
+                       window.print();
+                       removeExtractsFromPage();
+                       removePrintStyles();
+               } );
+       };
+
+       M.define( 'skins.minerva.print/Printer', Printer );
+}( mw.mobileFrontend, jQuery ) );
diff --git a/resources/skins.minerva.print/print.less 
b/resources/skins.minerva.print/print.less
new file mode 100644
index 0000000..e1c7d19
--- /dev/null
+++ b/resources/skins.minerva.print/print.less
@@ -0,0 +1,44 @@
+@import "minerva.variables";
+
+.print-extract {
+       display: none;
+}
+
+@media print {
+       body.print {
+               overflow: hidden;
+
+               h1, h2, h3, h4, h5 {
+                       page-break-after: avoid;
+               }
+               table, figure, img {
+                       page-break-inside: avoid;
+               }
+
+               .banner-container,
+               .header,
+               .pre-content,
+               .post-content,
+               .collapsible-heading .indicator,
+               .collapsible-heading .edit-page {
+                       display: none;
+               }
+
+               .collapsible-block {
+                       display: block;
+               }
+
+               #bodyContent img {
+                       max-width: 100%;
+               }
+
+               .print-extract {
+                       display: inline-block;
+                       margin: 0 5px;
+                       padding: 1px 5px;
+                       font-size: 90%;
+                       color: @colorGray10;
+                       border: 1px solid @colorGray8;
+               }
+       }
+}
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/287143
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2605a89289ee7d665c5ec49ad55016b9ada045c2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Bmansurov <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to