Jdlrobson has submitted this change and it was merged.

Change subject: Add Page and Section view
......................................................................


Add Page and Section view

Change-Id: I50d8425d030ed8d12ae915d7276d1db443009e71
---
M MobileFrontend.php
M includes/MobileFrontend.hooks.php
A javascripts/views/page.js
A tests/js/views/page.js
4 files changed, 152 insertions(+), 0 deletions(-)

Approvals:
  Jdlrobson: Verified; Looks good to me, approved
  jenkins-bot: Checked



diff --git a/MobileFrontend.php b/MobileFrontend.php
index 8a5093d..0bde4e6 100644
--- a/MobileFrontend.php
+++ b/MobileFrontend.php
@@ -341,6 +341,7 @@
        ),
        'scripts' => array(
                'javascripts/common/application-alpha.js',
+               'javascripts/views/page.js',
                'javascripts/modules/mf-inline-style-scrubber.js',
                'javascripts/common/mf-history-jquery.js',
                'javascripts/modules/mf-random.js',
diff --git a/includes/MobileFrontend.hooks.php 
b/includes/MobileFrontend.hooks.php
index 3c8b264..aa86ff6 100644
--- a/includes/MobileFrontend.hooks.php
+++ b/includes/MobileFrontend.hooks.php
@@ -224,6 +224,7 @@
                                'javascripts/modules/mf-references.js', 
'tests/js/test_references.js',
                                'javascripts/modules/mf-watchstar.js', 
'tests/js/test_mf-watchstar.js',
                                'javascripts/modules/mf-last-modified.js', 
'tests/js/test_mf-last-modified.js',
+                               'javascripts/views/page.js', 
'tests/js/views/page.js',
 
                                'javascripts/specials/uploads.js', 
'tests/js/specials/uploads.js',
                                ),
diff --git a/javascripts/views/page.js b/javascripts/views/page.js
new file mode 100644
index 0000000..e206694
--- /dev/null
+++ b/javascripts/views/page.js
@@ -0,0 +1,82 @@
+( function( M,  $ ) {
+
+       var
+               View = M.require( 'view' ),
+               Section, Page;
+
+       Section = View.extend( {
+               defaults: {
+                       heading: '',
+                       content: '',
+                       id: null
+               },
+               initialize: function( options ) {
+                       this.heading = options.heading;
+                       this.content = options.content;
+                       this.id = options.id;
+               }
+       } );
+
+       Page = View.extend( {
+               defaults: {
+                       heading: '',
+                       lead: '',
+                       sections: []
+               },
+               initialize: function( options ) {
+                       var s, i, level, text,
+                               $tmpContainer = $( '<div>' ),
+                               html, section,
+                               sectionNum = 0,
+                               secs = options.sections,
+                               sectionData = {};
+
+                       for ( i = 0; i < secs.length; i++ ) {
+                               s = secs[ i ];
+                               level = s.level;
+                               text = s.text || '';
+
+                               if ( i === 0 ) { // do lead
+                                       this.lead = text;
+                               }
+
+                               if ( level === '2' ) {
+                                       sectionNum = sectionNum + 1;
+                                       sectionData[ sectionNum ] = { content: 
text,
+                                               id: s.id, heading: s.line };
+
+                               } else if ( level ) {
+                                       $tmpContainer.html( text );
+                                       $tmpContainer.prepend(
+                                               $( '<h' + level + '>' ).attr( 
'id', s.anchor ).html( s.line )
+                                       );
+                                       html = $tmpContainer.html();
+                                       // deal with pages which have an h1 at 
the top
+                                       if ( !sectionData[ sectionNum ] ) {
+                                               this.lead += html;
+                                       } else {
+                                               sectionData[ sectionNum 
].content += html;
+                                       }
+                               }
+                       }
+                       this.sections = [];
+                       this._sectionLookup = {};
+                       for ( s in sectionData ) {
+                               if ( sectionData.hasOwnProperty( s ) ) {
+                                       section = new Section( sectionData[ s ] 
);
+                                       this.sections.push( section );
+                                       this._sectionLookup[ section.id ] = 
section; // allow easy lookup of section
+                               }
+                       }
+               },
+               getSubSection: function( id ) {
+                       return this._sectionLookup[ id ];
+               },
+               getSubSections: function() {
+                       return this.sections;
+               }
+       } );
+
+       M.define( 'page', Page );
+
+}( mw.mobileFrontend, jQuery ) );
diff --git a/tests/js/views/page.js b/tests/js/views/page.js
new file mode 100644
index 0000000..29b0134
--- /dev/null
+++ b/tests/js/views/page.js
@@ -0,0 +1,68 @@
+( function ( M, $ ) {
+
+var Page = M.require( 'page' ),
+       nonStandardPage = [{"id":0,"text":""},{"level":"1","line":"Test heading 
h1","anchor":"Test_heading_h1","id":1,"text":"<p>another h1?git fetch 
ssh:\/\/[email protected]:29418\/mediawiki\/extensions\/MobileFrontend 
refs\/changes\/39\/54139\/8 &amp;&amp; git checkout FETCH_HEADgit fetch 
ssh:\/\/[email protected]:29418\/mediawiki\/extensions\/MobileFrontend 
refs\/changes\/39\/54139\/8 &amp;&amp; git checkout FETCH_HEAD\ngit fetch 
ssh:\/\/[email protected]:29418\/mediawiki\/extensions\/MobileFrontend 
refs\/changes\/39\/54139\/8 &amp;&amp; git checkout FETCH_HEAD\ngit fetch 
ssh:\/\/[email protected]:29418\/mediawiki\/extensions\/MobileFrontend 
refs\/changes\/39\/54139\/8 &amp;&amp; git checkout FETCH_HEAD\nv\nv\nv\nvgit 
fetch 
ssh:\/\/[email protected]:29418\/mediawiki\/extensions\/MobileFrontend 
refs\/changes\/39\/54139\/8 &amp;&amp; git checkout FETCH_HEADgit fetch 
ssh:\/\/[email protected]:29418\/mediawiki\/extensions\/MobileFrontend 
refs\/changes\/39\/54139\/8 &amp;&amp; git checkout FETCH_HEAD\ngit fetch 
ssh:\/\/[email protected]:29418\/mediawiki\/extensions\/MobileFrontend 
refs\/changes\/39\/54139\/8 &amp;&amp; git checkout FETCH_HEADgit fetch 
ssh:\/\/[email protected]:29418\/mediawiki\/extensions\/MobileFrontend 
refs\/changes\/39\/54139\/8 &amp;&amp; git checkout FETCH_HEADgit fetch 
ssh:\/\/[email protected]:29418\/mediawiki\/extensions\/MobileFrontend 
refs\/changes\/39\/54139\/8 &amp;&amp; git checkout FETCH_HEAD\nvgit fetch 
ssh:\/\/[email protected]:29418\/mediawiki\/extensions\/MobileFrontend 
refs\/changes\/39\/54139\/8 &amp;&amp; git checkout FETCH_HEAD\ngit fetch 
ssh:\/\/[email protected]:29418\/mediawiki\/extensions\/MobileFrontend 
refs\/changes\/39\/54139\/8 &amp;&amp; git checkout 
FETCH_HEAD\n<\/p>"},{"level":"2","line":"Test heading h2 
(1)","anchor":"Test_heading_h2_.281.29","id":2,"text":"<p>1: You have brains in 
your head. You have feet in your shoes. You can steer yourself any direction 
you choose. You're on your own. And you know what you know. And YOU are the one 
who'll decide where to go...\nSed ut perspiciatis unde omnis iste natus error 
sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa 
quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt 
explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut 
fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi 
nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, 
consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut 
labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, 
quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid 
ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea 
voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem 
eum fugiat quo voluptas nulla pariatur?\nSed ut perspiciatis unde omnis iste 
natus error sit voluptatem accusantium doloremque laudantium, totam rem 
aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae 
vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit 
aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui 
ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum 
quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius 
modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut 
enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit 
laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure 
reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, 
vel illum qui dolorem eum fugiat quo voluptas nulla 
pariatur?\n<\/p>"},{"level":"2","line":"Test heading h2 
2)","anchor":"Test_heading_h2_2.29","id":3,"text":"<p>2: You have brains in 
your head. You have feet in your shoes. You can steer yourself any direction 
you choose. You're on your own. And you know what you know. And YOU are the one 
who'll decide where to go...\nSed ut perspiciatis unde omnis iste natus error 
sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa 
quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt 
explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut 
fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi 
nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, 
consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut 
labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, 
quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid 
ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea 
voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem 
eum fugiat quo voluptas nulla pariatur?\n<\/p>"},{"level":"2","line":"Test 
heading h2 (3)","anchor":"Test_heading_h2_.283.29","id":4,"text":"<p>3: You 
have brains in your head. You have feet in your shoes. You can steer yourself 
any direction you choose. You're on your own. And you know what you know. And 
YOU are the one who'll decide where to go...\n<\/p>"}],
+       apiSections =
+               [ {
+                       "id": 0,
+                       "text": "lead section"
+               },
+               {
+                       "level": "2",
+                       "line": "A",
+                       "id": 1,
+                       "text": "<p>a</p>"
+               },
+               {
+                       "level": "3",
+                       "line": "B",
+                       "id": 2,
+                       "text": "<p>b<\/p>"
+               },
+               {
+                       "level": "3",
+                       "line": "C",
+                       "id": 3,
+                       "text": "<p>c<\/p>"
+               },
+               {
+                       "level": "4",
+                       "line": "D",
+                       "id": 4,
+                       "text": "<div>d<\/div>"
+               },
+               {
+                       "level": "2",
+                       "line": "F",
+                       "id": 5,
+                       "text": "<p>f<\/p>"
+               } ];
+
+QUnit.module( 'MobileFrontend: page.js', {} );
+
+QUnit.test( 'Page', 8, function() {
+       var p = new Page( { sections: apiSections } ),
+               sections = p.getSubSections();
+
+       strictEqual( p.lead, 'lead section', 'lead section set' );
+       strictEqual( sections.length, 2, '2 sub sections found' );
+       strictEqual( sections[ 0 ].heading, 'A' );
+       strictEqual( sections[ 0 ].content,
+               
'<p>a</p><h3>B</h3><p>b</p><h3>C</h3><p>c</p><h4>D</h4><div>d</div>' );
+       strictEqual( sections[ 1 ].heading, 'F' );
+       strictEqual( sections[ 1 ].content, '<p>f</p>' );
+       strictEqual( p.getSubSection( 5 ).heading, 'F', 'check correct sub 
section with id 5 found' );
+       strictEqual( p.getSubSection( 1 ).heading, 'A', 'check correct sub 
section with id 1 found' );
+} );
+
+QUnit.test( 'NonStandardPage', 3, function() {
+       var p = new Page( { sections: nonStandardPage } ),
+               $lead = $( '<div>' ).html( p.lead );
+       strictEqual( $lead.find( 'h1' ).length, 1, 'The h1 is thrown into the 
lead section' );
+       strictEqual( $lead.find( 'h1' ).text(), 'Test heading h1', 'Check text 
of h1' );
+       strictEqual( p.getSubSections().length, 3, 'There are 3 h2s inside the 
content' );
+} );
+
+}( mw.mobileFrontend, jQuery ) );
+
+

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I50d8425d030ed8d12ae915d7276d1db443009e71
Gerrit-PatchSet: 13
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>
Gerrit-Reviewer: JGonera <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: MaxSem <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to