~sivers/sive.rs

ref: 89db8a0db3b15ad1a131b443ccfe2a0d5f08170a sive.rs/templates/books.mustache -rw-r--r-- 3.3 KiB
89db8a0d — Derek Sivers formatting 4 months ago
                                                                                
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<header>
  <h1>Books I’ve read</h1>
  <p>Tiny summary but <strong>detailed notes for each</strong>. Use the ISBN number to find it from your local library or anywhere else. <strong>This page will constantly update as I read more</strong>, so bookmark it if you want to check back in a few months.</p>
</header>
<p id="sorters">Sorted with <strong>my top recommendations up top</strong>. Sort by
  <a href="/book?sort=title" id="sort-title">title</a>,
  <a href="/book?sort=date" id="sort-date">newest</a>, or
  <a href="/book?sort=rating" id="sort-rating">best</a>.
</p><p>
  And <strong>please read <a href="/bfaq">this FAQ page</a> about these notes</strong>, if you have any questions.
  You can also <a href="/contact">email me</a>.
</p>
<form action="https://duckduckgo.com/" method="get" class="inlineform">
  <label for="q">Search these books:</label>
  <input type="text" name="q" value="">
  <input type="hidden" name="sites" value="sive.rs/book">
  <input type="hidden" name="ia" value="web">
  <input type="submit" value="search">
</form>
<hr>

<section id="allbooks">
{{#books}}
<div class="abook" data-rating="{{rating}}" data-title="{{title}}" data-date="{{date}}">
<figure><a href="/book/{{uri}}"><img src="/images/{{uri}}.gif" alt="{{title}}"></a></figure>
<h2><a href="/book/{{uri}}">{{title}}</a></h2>
<small>Date read: {{date}}. How strongly I recommend it: <strong>{{rating}}</strong>/10</small>
<p>{{description}}</p>
<p><strong><a href="/book/{{uri}}">Read my notes</a></strong>, or go to the <a href="http://www.amazon.com/dp/{{isbn}}?tag=sivers-20">Amazon page</a> for details and reviews.</p>
</div>
{{/books}}
</section>

<script>
function getBooks() {
  var books = [];
  var nl = document.querySelectorAll('div.abook');
  var len = nl.length;
  for (var i = 0; i < len; i++) {
    books[i] = nl[i];
  }
  return books;
}

function by(propName) {
  return function(obj1, obj2) {
    var v1 = obj1.getAttribute('data-' + propName);
    var v2 = obj2.getAttribute('data-' + propName);
    if (propName === 'rating') {
      v1 = parseInt(v1);
      v2 = parseInt(v2);
    } else if (propName === 'title') {
      /* because sorted high-to-low by default, flip for title */
      var tmp = v2;
      v2 = v1;
      v1 = tmp;
    }
    if (v1 > v2) { return -1; }
    else if (v1 < v2) { return 1; }
    else { return 0; }
  }
}

function showBooks(books) {
  var len = books.length, nu = document.createElement('section');
  nu.id = 'allbooks';
  for(var i = 0; i < len; i++) {
    nu.appendChild(books[i]);
  }
  document.getElementById('content').replaceChild(nu, document.getElementById('allbooks'));
}

function changeIfMatch(reg, str) {
  var matches = reg.exec(str);
  if(matches) {
    var books = getBooks();
    books.sort(by(matches[1]));
    showBooks(books);
  }
}

function sortBooks(event) {
  if(event.preventDefault) {
    event.preventDefault();
  } else {
    event.returnValue = false;
  }
  var target = event.target || window.event.srcElement;
  changeIfMatch(/^sort-(rating|title|date)$/, target.id);
}

if(location.search) {
  changeIfMatch(/\?sort=(rating|title|date)$/, location.search);
}

var sorters = document.getElementById('sorters');
if(sorters.addEventListener) {
  sorters.addEventListener('click', sortBooks, false);
} else {
  sorters.attachEvent('onclick', sortBooks);
}
</script>