summaryrefslogtreecommitdiff
path: root/lib/epub.js/examples/renderless.html
blob: d5bb1069822f1ec0ec68822e63f481d8dbb73b45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>EPUB.js Basic Example</title>

  <script src="../dist/epub.js"></script>

  <link rel="stylesheet" type="text/css" href="examples.css">

</head>
<body>
  <select id="toc"></select>
  <div id="viewer" class="scrolled"></div>
  <div id="prev" class="arrow">‹</div>
  <div id="next" class="arrow">›</div>

  <script>
    var $viewer = document.getElementById("viewer");
    var $next = document.getElementById("next");
    var $prev = document.getElementById("prev");
    var currentSection;
    var currentSectionIndex = 6;

    var book = ePub("https://s3.amazonaws.com/epubjs/books/moby-dick/OPS/package.opf");
    book.loaded.navigation.then(function(toc){
      var $select = document.getElementById("toc"),
          docfrag = document.createDocumentFragment();

      toc.forEach(function(chapter) {
        var option = document.createElement("option");
        option.textContent = chapter.label;
        option.ref = chapter.href;

        docfrag.appendChild(option);
      });

      $select.appendChild(docfrag);

      $select.onchange = function(){
          var index = $select.selectedIndex,
              url = $select.options[index].ref;
          display(url);
          return false;
      };

      book.opened.then(function(){
        display(currentSectionIndex);
      });

      $next.addEventListener("click", function(){
        var displayed = display(currentSectionIndex+1);
        if(displayed) currentSectionIndex++;
      }, false);

      $prev.addEventListener("click", function(){
        var displayed = display(currentSectionIndex-1);
        if(displayed) currentSectionIndex--;
      }, false);

      function display(item){
        var section = book.spine.get(item);
        if(section) {
          currentSection = section;
          section.render().then(function(html){
            // $viewer.srcdoc = html;
            $viewer.innerHTML = html;
          });
        }
        return section;
      }

    });
  </script>

</body>
</html>