1- Leo = {}
1+ # -------------------------------------
2+ # Setup
3+ # -> Namespace setup
4+ # -------------------------------------
5+
6+ @Leo = {}
7+ @Leo.Pager = {}
28
39# -------------------------------------
410# Heading Links
11+ # -> Adds an anchor link next to each heading
12+ #
13+ # options.els - the heading elements
514# -------------------------------------
615
716Leo.headingLinks = (options) ->
@@ -13,14 +22,71 @@ Leo.headingLinks = (options) ->
1322
1423# -------------------------------------
1524# Read Time
25+ # -> Adds an estimated reading time to each page
26+ #
27+ # options.text - the text of the page
28+ # options.element - the element to append the reading time to
29+ # options.wordsPerMinute - the words-per-minute to calculate against
1630# -------------------------------------
1731
1832Leo.readTime = (options) ->
1933 time = Math.ceil(options.text.split(' ').length / options.wordsPerMinute)
2034 options.element.append("< b > #{time} minute read</ b > ")
2135
36+ # -------------------------------------
37+ # Pager
38+ # -> Adds keyboard navigation for pages
39+ #
40+ # id - the page id
41+ # -------------------------------------
42+
43+ Leo.Pager = do ->
44+
45+ pageIndex = 0
46+
47+ pages = []
48+
49+ shortcuts =
50+ 'next': 39
51+ 'prev': 37
52+
53+ init = (id) ->
54+ pageIndex = id
55+ getPages()
56+ setEventHandlers()
57+
58+ getPages = ->
59+ <% get_pages . each do |p | %>
60+ pages.push("<%= p . url %> ")
61+ <% end %>
62+
63+ setEventHandlers = ->
64+ $(document).on 'keydown', (e) ->
65+ switch getKeyCode(e)
66+ when shortcuts.next then gotoPage('next')
67+ when shortcuts.prev then gotoPage('prev')
68+
69+ getKeyCode = (e) ->
70+ e = e || window.event
71+ charCode = e.keyCode || e.which
72+ charCode
73+
74+ gotoPage = (dir) ->
75+ switch dir
76+ when 'next'
77+ unless pageIndex == pages.length
78+ window.location = pages[pageIndex]
79+ when 'prev'
80+ unless pageIndex == 1
81+ window.location = pages[pageIndex - 2]
82+
83+ init: init
84+
2285# -------------------------------------
2386# Slugify
87+ # -> Converts a string into a title slug
88+ #
89+ # text - the text to convert
2490# -------------------------------------
2591
2692Leo.slugify = (text) ->
@@ -44,6 +110,8 @@ jQuery ($) ->
44110 })
45111 <% end %>
46112
113+ Leo.Pager.init( $('body').data('id') )
114+
47115 $('.toggle').on 'click', (e) ->
48116 e.preventDefault()
49117 $(@).toggleClass('is-active')
0 commit comments