Difference between revisions of "Javascript API"

Jump to navigation Jump to search
805 bytes removed ,  14:31, 18 October 2017
 
(3 intermediate revisions by the same user not shown)
Line 33: Line 33:


==== Table of content ====
==== Table of content ====
<code>function TableOfContents () {</code>
<syntaxhighlight lang="javascript" line='line'>
function TableOfContents () {
    this.getSections = function() {
        /*
        - return an array of section objects
        - in case there there is no table of content, return an empty array
        */
    }


    <code>this.getSections = function() {</code>
    this.scrollToSection = function(index: number) {
        /*
        - scroll to a section with index in array returned by this.getSections
        - perform change to the page so that the section become available for reading, such as expanding the collapsed section 
        */
    }


        <code>/*</code>
    // OPTIONAL
    this.visibleSectionIndexes = function() {
        /*
        - return an array of visible section's indexes
        - in case there there is no table of content, return an empty array
        */
    }


        <code>- return an array of section objects</code>
    // OPTIONAL
    this.startVisibleSectionCallBack = function () {
        /*
        - start page visible section call back
        - using this call back, the reader app could know which section is currently visible to the user and highlighting the location in table of content
        - implementation to be discussed. It might not be a good idea to modify window.onscroll directly
        */
        var handleScroll = function() {
            var visible = tableOfContents.visibleSectionIndexes();
            if (visible.length > 0) {
                window.location = 'pagescroll:scrollEnded?start=' + visible[0] + '&length=' + visible.length;
            }
        }
        window.onscroll = handleScroll;
        handleScroll();
    }


        <code>- in case there there is no table of content, return an empty array</code>
    // OPTIONAL
    this.stopVisibleSectionCallBack = function () {
        /*
        - stop page visible section call back
        */
        window.onscroll = undefined;
    }
   
}


        <code>*/</code>
function Section(title: string, level: number) {}
 
</syntaxhighlight>
    <code>}</code>
 
<code><br>
</code>
 
    <code>this.scrollToSection = function(index: number) {</code>
 
        <code>/*</code>
 
        <code>- scroll to a section with index in array returned by this.getSections</code>
 
        <code>- perform change to the page so that the section become available for reading, such as expanding the collapsed section</code>
 
         <code>*/</code>
 
    <code>}</code>
 
<code><br>
</code>
 
    <code>// OPTIONAL</code>
 
    <code>this.visibleSectionIndexes = function() {</code>
 
        <code>/*</code>
 
        <code>- return an array of visible section's indexes</code>
 
        <code>- in case there there is no table of content, return an empty array</code>
 
        <code>*/</code>
 
    <code>}</code>
 
<code><br>
</code>
 
    <code>// OPTIONAL</code>
 
    <code>this.startVisibleSectionCallBack = function () {</code>
 
        <code>/*</code>
 
        <code>- start page visible section call back</code>
 
        <code>- using this call back, the reader app could know which section is currently visible to the user and highlighting the location in table of content</code>
 
        <code>- implementation to be discussed. It might not be a good idea to modify window.onscroll directly</code>
 
        <code>*/</code>
 
        <code>var handleScroll = function() {</code>
 
            <code>var visible = tableOfContents.visibleSectionIndexes();</code>
 
            <code>if (visible.length > 0) {</code>
 
                <code>window.location = 'pagescroll:scrollEnded?start=' + visible[0] + '&length=' + visible.length;</code>
 
            <code>}</code>
 
        <code>}</code>
 
        <code>window.onscroll = handleScroll;</code>
 
        <code>handleScroll();</code>
 
    <code>}</code>
 
<code><br>
</code>
 
    <code>// OPTIONAL</code>
 
    <code>this.stopVisibleSectionCallBack = function () {</code>
 
        <code>/*</code>
 
        <code>- stop page visible section call back</code>
 
        <code>*/</code>
 
        <code>window.onscroll = undefined;</code>
 
    <code>}</code>
 
<code>}</code>
 
<code><br>
</code>
 
<code>function Section(title: string, level: number) {}</code>
12

edits

Navigation menu