Jdlrobson has uploaded a new change for review.

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


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, 135 insertions(+), 0 deletions(-)


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

diff --git a/MobileFrontend.php b/MobileFrontend.php
index 0d956aa..9eeaaa1 100644
--- a/MobileFrontend.php
+++ b/MobileFrontend.php
@@ -313,6 +313,7 @@
                'stylesheets/modules/mf-tables.css',
        ),
        'scripts' => array(
+               '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 3533bb5..b6c9967 100644
--- a/includes/MobileFrontend.hooks.php
+++ b/includes/MobileFrontend.hooks.php
@@ -196,6 +196,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/donateimage.js', 
'tests/js/specials/donateimage.js',
                                ),
diff --git a/javascripts/views/page.js b/javascripts/views/page.js
new file mode 100644
index 0000000..e0b7dfb
--- /dev/null
+++ b/javascripts/views/page.js
@@ -0,0 +1,76 @@
+( 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 secs, s, i, level, text,
+                               $tmpContainer = $( '<div>' ),
+                               sectionNum = 0,
+                               sectionData = {};
+
+                       if ( options.sections ) {
+                               secs = options.sections;
+                               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: sectionNum, 
heading: s.line };
+
+                                       } else if ( level ) {
+                                               $tmpContainer.html( text );
+                                               $tmpContainer.prepend(
+                                                       $( '<h' + level + '>' 
).attr( 'id', s.anchor ).html( s.line )
+                                               );
+                                               sectionData[ sectionNum 
].content += $tmpContainer.html();
+                                       }
+                               }
+                       }
+                       this.sections = [];
+                       for ( s in sectionData ) {
+                               if ( sectionData.hasOwnProperty( s ) ) {
+                                       sectionData[ s ] = new Section( 
sectionData[ s ] );
+                                       this.sections.push( sectionData[ s ] );
+                               }
+                       }
+                       this._sectionData = sectionData;
+               },
+               getSubSection: function( id ) {
+                       return this._sectionData[ 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..aa8eefa
--- /dev/null
+++ b/tests/js/views/page.js
@@ -0,0 +1,57 @@
+( function ( M ) {
+
+var Page = M.require( 'page' ),
+       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>"
+               } ];
+
+module( 'MobileFrontend: page.js', {} );
+
+test( 'Page', 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>' );
+} );
+
+}( mw.mobileFrontend ) );
+
+

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

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

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

Reply via email to