Yuvipanda has uploaded a new change for review. https://gerrit.wikimedia.org/r/108518
Change subject: Moved section display JS code to its own file ...................................................................... Moved section display JS code to its own file Change-Id: I820bb1a94378ff5181ae9cbb5a60718dc4e0ba35 --- M wikipedia/assets/bundle-test.js M wikipedia/assets/bundle.js M www/Gruntfile.js M www/js/main.js A www/js/sections.js 5 files changed, 91 insertions(+), 272 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia refs/changes/18/108518/1 diff --git a/wikipedia/assets/bundle-test.js b/wikipedia/assets/bundle-test.js index a06cd5c..01bacac 100644 --- a/wikipedia/assets/bundle-test.js +++ b/wikipedia/assets/bundle-test.js @@ -38,63 +38,6 @@ }; },{}],2:[function(require,module,exports){ var bridge = require("./bridge"); -var transforms = require("./transforms"); - -bridge.registerListener( "displayLeadSection", function( payload ) { - // This might be a refresh! Clear out all contents! - document.getElementById( "content" ).innerHTML = ""; - - var title = document.createElement( "h1" ); - title.textContent = payload.title; - title.id = "heading_" + payload.section.id; - document.getElementById( "content" ).appendChild( title ); - - var content = document.createElement( "div" ); - content.innerHTML = payload.section.text; - content.id = "#content_block_0"; - content = transforms.transform( "lead", content ); - document.getElementById( "content" ).appendChild( content ); - - document.getElementById( "loading_sections").className = "loading"; -}); - -function elementsForSection( section ) { - var heading = document.createElement( "h" + ( section.toclevel + 1 ) ); - heading.textContent = section.line; - heading.id = "heading_" + section.id; - heading.setAttribute( 'data-id', section.id ); - - var editButton = document.createElement( "a" ); - editButton.setAttribute( 'data-id', section.id ); - editButton.setAttribute( 'data-action', "edit_section" ); - editButton.className = "edit_section_button"; - heading.appendChild( editButton ); - - var content = document.createElement( "div" ); - content.innerHTML = section.text; - content.id = "content_block_" + section.id; - content = transforms.transform( "body", content ); - - return [ heading, content ]; -} - -bridge.registerListener( "displaySection", function ( payload ) { - var contentWrapper = document.getElementById( "content" ); - - elementsForSection( payload.section ).forEach( function( element ) { - contentWrapper.appendChild( element ); - }); - if ( !payload.isLast ) { - bridge.sendMessage( "requestSection", { index: payload.index + 1 } ); - } else { - document.getElementById( "loading_sections").className = ""; - } -}); - -bridge.registerListener( "startSectionsDisplay", function() { - bridge.sendMessage( "requestSection", { index: 1 } ); -}); - bridge.registerListener( "displayAttribution", function( payload ) { var lastUpdatedA = document.getElementById( "lastupdated" ); lastUpdatedA.innerText = payload.historyText; @@ -111,103 +54,16 @@ } bridge.sendMessage( "imagesListResponse", { "images": imageURLs }); } ); - -bridge.registerListener( "scrollToSection", function ( payload ) { - var el = document.getElementById( "heading_" + payload.sectionID); - // Make sure there's exactly as much space on the left as on the top. - // The 48 accounts for the search bar - var scrollY = el.offsetTop - 48 - el.offsetLeft; - window.scrollTo(0, scrollY); -}); - - -var actionHandlers = { - "edit_section": function( el, event ) { - bridge.sendMessage( 'editSectionClicked', { sectionID: el.getAttribute( 'data-id' ) } ); - event.preventDefault(); - } -}; - -document.onclick = function() { - if ( event.target.tagName === "A" ) { - if ( event.target.hasAttribute( "data-action" ) ) { - var action = event.target.getAttribute( "data-action" ); - actionHandlers[ action ]( event.target, event ); - } else { - bridge.sendMessage( 'linkClicked', { href: event.target.getAttribute( "href" ) }); - event.preventDefault(); - } - } -}; -},{"./bridge":1,"./transforms":3}],3:[function(require,module,exports){ -var bridge = require("./bridge"); -var Transforms = function () {}; - -function moveInfobox( leadContent ) { - // Move infobox to the bottom of the lead section - var infobox = leadContent.querySelector( "table.infobox" ); - if ( infobox ) { - infobox.parentNode.removeChild( infobox ); - var pTags = leadContent.getElementsByTagName( "p" ); - if ( pTags.length ) { - pTags[0].appendChild( infobox ); - } else { - leadContent.appendChild( infobox ); - } - } - return leadContent; -} - -function useLocalImagesForSavedPages( content ) { - var images = content.querySelectorAll( "img" ); - function onError() { - var img = event.target; - // Only work on http or https URLs. If we do not have this check, we might go on an infinte loop - if ( img.src.substring( 0, 4 ) === "http" ) { - // if it is already not a file URL! - var resp = bridge.sendMessage( "imageUrlToFilePath", { "imageUrl": img.src } ); - console.log( "new filepath is " + resp.filePath ); - img.src = "file://" + resp.filePath; - } - } - for ( var i = 0; i < images.length; i++ ) { - images[i].onerror = onError; - } - return content; -} - -// List of transformation functions by their target type -var transformsByType = { - 'lead': [ - moveInfobox, - useLocalImagesForSavedPages - ], - 'body': [ - useLocalImagesForSavedPages - ] -}; - -Transforms.prototype.transform = function( type, content ) { - var transforms = transformsByType[ type ]; - if ( transforms.length ) { - transforms.forEach( function ( transform ) { - content = transform( content ); - } ); - } - return content; -}; - -module.exports = new Transforms(); -},{"./bridge":1}],4:[function(require,module,exports){ +},{"./bridge":1}],3:[function(require,module,exports){ var bridge = require("../js/bridge"); bridge.registerListener( "injectScript", function( payload ) { require(payload.src); }); -},{"../js/bridge":1}],5:[function(require,module,exports){ +},{"../js/bridge":1}],4:[function(require,module,exports){ var bridge = require("../js/bridge"); console.log("Something!"); bridge.registerListener( "ping", function( payload ) { bridge.sendMessage( "pong", payload ); }); -},{"../js/bridge":1}]},{},[2,1,4,5]) \ No newline at end of file +},{"../js/bridge":1}]},{},[2,1,3,4]) \ No newline at end of file diff --git a/wikipedia/assets/bundle.js b/wikipedia/assets/bundle.js index db7e5af..8a41953 100644 --- a/wikipedia/assets/bundle.js +++ b/wikipedia/assets/bundle.js @@ -59,6 +59,24 @@ }; },{"./bridge":1}],3:[function(require,module,exports){ var bridge = require("./bridge"); +bridge.registerListener( "displayAttribution", function( payload ) { + var lastUpdatedA = document.getElementById( "lastupdated" ); + lastUpdatedA.innerText = payload.historyText; + lastUpdatedA.href = payload.historyTarget; + var licenseText = document.getElementById( "licensetext" ); + licenseText.innerHTML = payload.licenseHTML; +}); + +bridge.registerListener( "requestImagesList", function () { + var imageURLs = []; + var images = document.querySelectorAll( "img" ); + for ( var i = 0; i < images.length; i++ ) { + imageURLs.push( images[i].src ); + } + bridge.sendMessage( "imagesListResponse", { "images": imageURLs }); +} ); +},{"./bridge":1}],4:[function(require,module,exports){ +var bridge = require("./bridge"); var transforms = require("./transforms"); bridge.registerListener( "displayLeadSection", function( payload ) { @@ -116,23 +134,6 @@ bridge.sendMessage( "requestSection", { index: 1 } ); }); -bridge.registerListener( "displayAttribution", function( payload ) { - var lastUpdatedA = document.getElementById( "lastupdated" ); - lastUpdatedA.innerText = payload.historyText; - lastUpdatedA.href = payload.historyTarget; - var licenseText = document.getElementById( "licensetext" ); - licenseText.innerHTML = payload.licenseHTML; -}); - -bridge.registerListener( "requestImagesList", function () { - var imageURLs = []; - var images = document.querySelectorAll( "img" ); - for ( var i = 0; i < images.length; i++ ) { - imageURLs.push( images[i].src ); - } - bridge.sendMessage( "imagesListResponse", { "images": imageURLs }); -} ); - bridge.registerListener( "scrollToSection", function ( payload ) { var el = document.getElementById( "heading_" + payload.sectionID); // Make sure there's exactly as much space on the left as on the top. @@ -141,26 +142,7 @@ window.scrollTo(0, scrollY); }); - -var actionHandlers = { - "edit_section": function( el, event ) { - bridge.sendMessage( 'editSectionClicked', { sectionID: el.getAttribute( 'data-id' ) } ); - event.preventDefault(); - } -}; - -document.onclick = function() { - if ( event.target.tagName === "A" ) { - if ( event.target.hasAttribute( "data-action" ) ) { - var action = event.target.getAttribute( "data-action" ); - actionHandlers[ action ]( event.target, event ); - } else { - bridge.sendMessage( 'linkClicked', { href: event.target.getAttribute( "href" ) }); - event.preventDefault(); - } - } -}; -},{"./bridge":1,"./transforms":4}],4:[function(require,module,exports){ +},{"./bridge":1,"./transforms":5}],5:[function(require,module,exports){ var bridge = require("./bridge"); var Transforms = function () {}; @@ -219,4 +201,4 @@ }; module.exports = new Transforms(); -},{"./bridge":1}]},{},[3,4,1,2]) \ No newline at end of file +},{"./bridge":1}]},{},[3,5,1,2,4]) \ No newline at end of file diff --git a/www/Gruntfile.js b/www/Gruntfile.js index ae90fb0..388f757 100644 --- a/www/Gruntfile.js +++ b/www/Gruntfile.js @@ -4,6 +4,7 @@ "js/transforms.js", "js/bridge.js", "js/linkactions.js", + "js/sections.js", "tests/*.js" ]; var allStyleFiles = [ @@ -19,7 +20,7 @@ browserify: { dist: { files: { - "bundle.js": [ "js/main.js", "js/transforms.js", "js/bridge.js", "js/linkactions.js"], + "bundle.js": [ "js/main.js", "js/transforms.js", "js/bridge.js", "js/linkactions.js", "js/sections.js"], "bundle-test.js": [ "js/main.js", "js/bridge.js", "tests/*.js" ] } } diff --git a/www/js/main.js b/www/js/main.js index ce53f4b..52c744a 100644 --- a/www/js/main.js +++ b/www/js/main.js @@ -1,61 +1,4 @@ var bridge = require("./bridge"); -var transforms = require("./transforms"); - -bridge.registerListener( "displayLeadSection", function( payload ) { - // This might be a refresh! Clear out all contents! - document.getElementById( "content" ).innerHTML = ""; - - var title = document.createElement( "h1" ); - title.textContent = payload.title; - title.id = "heading_" + payload.section.id; - document.getElementById( "content" ).appendChild( title ); - - var content = document.createElement( "div" ); - content.innerHTML = payload.section.text; - content.id = "#content_block_0"; - content = transforms.transform( "lead", content ); - document.getElementById( "content" ).appendChild( content ); - - document.getElementById( "loading_sections").className = "loading"; -}); - -function elementsForSection( section ) { - var heading = document.createElement( "h" + ( section.toclevel + 1 ) ); - heading.textContent = section.line; - heading.id = "heading_" + section.id; - heading.setAttribute( 'data-id', section.id ); - - var editButton = document.createElement( "a" ); - editButton.setAttribute( 'data-id', section.id ); - editButton.setAttribute( 'data-action', "edit_section" ); - editButton.className = "edit_section_button"; - heading.appendChild( editButton ); - - var content = document.createElement( "div" ); - content.innerHTML = section.text; - content.id = "content_block_" + section.id; - content = transforms.transform( "body", content ); - - return [ heading, content ]; -} - -bridge.registerListener( "displaySection", function ( payload ) { - var contentWrapper = document.getElementById( "content" ); - - elementsForSection( payload.section ).forEach( function( element ) { - contentWrapper.appendChild( element ); - }); - if ( !payload.isLast ) { - bridge.sendMessage( "requestSection", { index: payload.index + 1 } ); - } else { - document.getElementById( "loading_sections").className = ""; - } -}); - -bridge.registerListener( "startSectionsDisplay", function() { - bridge.sendMessage( "requestSection", { index: 1 } ); -}); - bridge.registerListener( "displayAttribution", function( payload ) { var lastUpdatedA = document.getElementById( "lastupdated" ); lastUpdatedA.innerText = payload.historyText; @@ -71,32 +14,4 @@ imageURLs.push( images[i].src ); } bridge.sendMessage( "imagesListResponse", { "images": imageURLs }); -} ); - -bridge.registerListener( "scrollToSection", function ( payload ) { - var el = document.getElementById( "heading_" + payload.sectionID); - // Make sure there's exactly as much space on the left as on the top. - // The 48 accounts for the search bar - var scrollY = el.offsetTop - 48 - el.offsetLeft; - window.scrollTo(0, scrollY); -}); - - -var actionHandlers = { - "edit_section": function( el, event ) { - bridge.sendMessage( 'editSectionClicked', { sectionID: el.getAttribute( 'data-id' ) } ); - event.preventDefault(); - } -}; - -document.onclick = function() { - if ( event.target.tagName === "A" ) { - if ( event.target.hasAttribute( "data-action" ) ) { - var action = event.target.getAttribute( "data-action" ); - actionHandlers[ action ]( event.target, event ); - } else { - bridge.sendMessage( 'linkClicked', { href: event.target.getAttribute( "href" ) }); - event.preventDefault(); - } - } -}; \ No newline at end of file +} ); \ No newline at end of file diff --git a/www/js/sections.js b/www/js/sections.js new file mode 100644 index 0000000..a9b939e --- /dev/null +++ b/www/js/sections.js @@ -0,0 +1,65 @@ +var bridge = require("./bridge"); +var transforms = require("./transforms"); + +bridge.registerListener( "displayLeadSection", function( payload ) { + // This might be a refresh! Clear out all contents! + document.getElementById( "content" ).innerHTML = ""; + + var title = document.createElement( "h1" ); + title.textContent = payload.title; + title.id = "heading_" + payload.section.id; + document.getElementById( "content" ).appendChild( title ); + + var content = document.createElement( "div" ); + content.innerHTML = payload.section.text; + content.id = "#content_block_0"; + content = transforms.transform( "lead", content ); + document.getElementById( "content" ).appendChild( content ); + + document.getElementById( "loading_sections").className = "loading"; +}); + +function elementsForSection( section ) { + var heading = document.createElement( "h" + ( section.toclevel + 1 ) ); + heading.textContent = section.line; + heading.id = "heading_" + section.id; + heading.setAttribute( 'data-id', section.id ); + + var editButton = document.createElement( "a" ); + editButton.setAttribute( 'data-id', section.id ); + editButton.setAttribute( 'data-action', "edit_section" ); + editButton.className = "edit_section_button"; + heading.appendChild( editButton ); + + var content = document.createElement( "div" ); + content.innerHTML = section.text; + content.id = "content_block_" + section.id; + content = transforms.transform( "body", content ); + + return [ heading, content ]; +} + +bridge.registerListener( "displaySection", function ( payload ) { + var contentWrapper = document.getElementById( "content" ); + + elementsForSection( payload.section ).forEach( function( element ) { + contentWrapper.appendChild( element ); + }); + if ( !payload.isLast ) { + bridge.sendMessage( "requestSection", { index: payload.index + 1 } ); + } else { + document.getElementById( "loading_sections").className = ""; + } +}); + +bridge.registerListener( "startSectionsDisplay", function() { + bridge.sendMessage( "requestSection", { index: 1 } ); +}); + +bridge.registerListener( "scrollToSection", function ( payload ) { + var el = document.getElementById( "heading_" + payload.sectionID); + // Make sure there's exactly as much space on the left as on the top. + // The 48 accounts for the search bar + var scrollY = el.offsetTop - 48 - el.offsetLeft; + window.scrollTo(0, scrollY); +}); -- To view, visit https://gerrit.wikimedia.org/r/108518 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I820bb1a94378ff5181ae9cbb5a60718dc4e0ba35 Gerrit-PatchSet: 1 Gerrit-Project: apps/android/wikipedia Gerrit-Branch: master Gerrit-Owner: Yuvipanda <yuvipa...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits