Jdlrobson has uploaded a new change for review.

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


Change subject: Make sections aware of their subsections
......................................................................

Make sections aware of their subsections

Introduce concept of child Sections
A Section has multiple child Sections
This will make it easier to build table of contents

Change-Id: If88596659b9bb0d2da86aa7a709ef186bb238030
---
M javascripts/common/Page.js
M javascripts/common/PageApi.js
M tests/javascripts/common/test_PageApi.js
3 files changed, 50 insertions(+), 6 deletions(-)


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

diff --git a/javascripts/common/Page.js b/javascripts/common/Page.js
index 1da81a7..f3e6579 100644
--- a/javascripts/common/Page.js
+++ b/javascripts/common/Page.js
@@ -17,11 +17,16 @@
                        editLabel: mw.msg( 'mobile-frontend-editor-edit' )
                },
                initialize: function( options ) {
+                       var self = this;
                        this.line = options.line;
                        this.text = options.text;
                        this.hasReferences = options.hasReferences || false;
                        this.id = options.id || null;
                        this.anchor = options.anchor;
+                       this.children = [];
+                       $.each( options.children, function() {
+                               self.children.push( new Section( this ) );
+                       } );
                        this._super( options );
                }
        } );
diff --git a/javascripts/common/PageApi.js b/javascripts/common/PageApi.js
index 7d4cc18..b8f5547 100644
--- a/javascripts/common/PageApi.js
+++ b/javascripts/common/PageApi.js
@@ -2,23 +2,44 @@
 
        var Api = M.require( 'api' ).Api, PageApi;
 
+       function assignToParent( listOfSections, child ) {
+               var section;
+               if ( listOfSections.length === 0 ) {
+                       listOfSections.push( child );
+               } else {
+                       // take a look at the last child
+                       section = listOfSections[listOfSections.length - 1];
+                       // If the level is the same as another section in this 
list it is a sibling
+                       if ( parseInt( section.level, 10 ) === parseInt( 
child.level, 10 ) ) {
+                               listOfSections.push( child );
+                       } else {
+                               // Otherwise take a look at that sections 
children recursively
+                               assignToParent( section.children, child );
+                       }
+               }
+       }
+
        function transformSections( sections ) {
                var
                        collapseLevel = Math.min.apply( this, $.map( sections, 
function( s ) { return s.level; } ) ) + '',
+                       lastSection,
                        result = [], $tmpContainer = $( '<div>' );
 
                $.each( sections, function( i, section ) {
                        // FIXME: [API] should probably do this for us - we 
want to be able to control the styling of these headings - no inline styles!
                        section.line = $( '<div>' ).html( section.line ).text();
+                       section.children = [];
                        if ( !section.level || section.level === collapseLevel 
) {
                                result.push( section );
+                               lastSection = section;
                        } else {
                                // FIXME: ugly, maintain structure returned by 
API and use templates instead
                                $tmpContainer.html( section.text );
                                $tmpContainer.prepend(
                                        $( '<h' + section.level + '>' ).attr( 
'id', section.anchor ).html( section.line )
                                );
-                               result[result.length - 1].text += 
$tmpContainer.html();
+                               assignToParent( lastSection.children, section );
+                               lastSection.text += $tmpContainer.html();
                        }
                } );
 
diff --git a/tests/javascripts/common/test_PageApi.js 
b/tests/javascripts/common/test_PageApi.js
index e203386..a2cfb9e 100644
--- a/tests/javascripts/common/test_PageApi.js
+++ b/tests/javascripts/common/test_PageApi.js
@@ -44,14 +44,20 @@
                                                "line": "1",
                                                "anchor": "1",
                                                "id": 1,
-                                               "text": '<p>Text of 1\n</p><h2 
id="1.1">1.1</h2><p>Text of 1.1\n</p>'
+                                               "text": '<p>Text of 1\n</p><h2 
id="1.1">1.1</h2><p>Text of 1.1\n</p>',
+                                               "children": [
+                                                       
{"level":"2","line":"1.1","anchor":"1.1","id":2,"text":"<p>Text of 1.1\n</p>", 
children: [] }
+                                               ]
                                        },
                                        {
                                                "level": "1",
                                                "line": "2",
                                                "anchor": "2",
                                                "id": 3,
-                                               "text": '<p>Text of 2\n</p><h2 
id="2.1">2.1</h2><p>Text of 2.1\n</p>'
+                                               "text": '<p>Text of 2\n</p><h2 
id="2.1">2.1</h2><p>Text of 2.1\n</p>',
+                                               "children": [
+                                                       
{"level":"2","line":"2.1","anchor":"2.1","id":4,"text":"<p>Text of 2.1\n</p>", 
children: [] }
+                                               ]
                                        }
                                ]
                        }, 'return lead and sections' );
@@ -122,14 +128,25 @@
                                                "line": "Aaa section",
                                                "anchor": "Aaa_section",
                                                "id": 1,
-                                               "text": 'aaa content<h3 
id="Subaaa_section">Subaaa section</h3>subaaa content'
+                                               "text": 'aaa content<h3 
id="Subaaa_section">Subaaa section</h3>subaaa content',
+                                               "children": [
+                                                       {
+                                                               "level": "3",
+                                                               "line": "Subaaa 
section",
+                                                               "anchor": 
"Subaaa_section",
+                                                               "id": 2,
+                                                               "text": "subaaa 
content",
+                                                               "children": []
+                                                       }
+                                               ]
                                        },
                                        {
                                                "level": "2",
                                                "line": "Bbb section",
                                                "anchor": "Bbb_section",
                                                "id": 3,
-                                               "text": "bbb content"
+                                               "text": "bbb content",
+                                               "children": []
                                        },
                                        {
                                                "level": "2",
@@ -137,7 +154,8 @@
                                                "references": "",
                                                "anchor": "References",
                                                "id": 4,
-                                               "text": "references"
+                                               "text": "references",
+                                               "children": []
                                        }
                                ]
                        }, 'return lead and sections' );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If88596659b9bb0d2da86aa7a709ef186bb238030
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