-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapplication.js.coffee.erb
More file actions
128 lines (103 loc) · 3.03 KB
/
application.js.coffee.erb
File metadata and controls
128 lines (103 loc) · 3.03 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -------------------------------------
# Setup
# -> Namespace setup
# -------------------------------------
@Leo = {}
@Leo.Pager = {}
# -------------------------------------
# Heading Links
# -> Adds an anchor link next to each heading
#
# options.els - the heading elements
# -------------------------------------
Leo.headingLinks = (options) ->
options.els.each ->
el = $(@)
text = Leo.slugify( el.text() )
el.attr('id', text)
el.prepend("<a class='anchor' href='##{text}'>#</a>")
# -------------------------------------
# Read Time
# -> Adds an estimated reading time to each page
#
# options.text - the text of the page
# options.element - the element to append the reading time to
# options.wordsPerMinute - the words-per-minute to calculate against
# -------------------------------------
Leo.readTime = (options) ->
time = Math.ceil(options.text.split(' ').length / options.wordsPerMinute)
options.element.append("<b>#{time} minute read</b>")
# -------------------------------------
# Pager
# -> Adds keyboard navigation for pages
#
# id - the page id
# -------------------------------------
Leo.Pager = do ->
pageIndex = 0
pages = []
shortcuts =
'next': 39
'prev': 37
init = (id) ->
pageIndex = id
getPages()
setEventHandlers()
console.log pageIndex
getPages = ->
<% get_pages.each do |p| %>
pages.push("<%= p.url %>")
<% end %>
setEventHandlers = ->
$(document).on 'keydown', (e) ->
switch getKeyCode(e)
when shortcuts.next then gotoPage('next')
when shortcuts.prev then gotoPage('prev')
getKeyCode = (e) ->
e = e || window.event
charCode = e.keyCode || e.which
charCode
gotoPage = (dir) ->
switch dir
when 'next'
unless pageIndex == pages.length - 1
window.location = pages[pageIndex + 1]
when 'prev'
unless pageIndex == 0
window.location = pages[pageIndex - 1]
init: init
# -------------------------------------
# Slugify
# -> Converts a string into a title slug
#
# text - the text to convert
# -------------------------------------
Leo.slugify = (text) ->
text.toLowerCase().replace(/\s+/g, '-').replace(/[^\w\-]+/g, '').replace(/\-\-+/g, '-').replace(/^-+/, '').replace(/-+$/, '')
#-------------------------------------
# Document Ready
#-------------------------------------
jQuery ($) ->
Leo.headingLinks({
els: $('h2, h3, h4, h5, h6')
})
<% if data.leo.reading_time %>
Leo.readTime({
wordsPerMinute: 200
text: $('.content').text()
element: $('.content h1')
})
<% end %>
Leo.Pager.init( $('body').data('id') )
$('.toggle').on 'click', (e) ->
e.preventDefault()
$(@).toggleClass('is-active')
$('body').toggleClass('is-sidebar-open')
$('.sidebar').toggleClass('is-open')
$(document).on 'keyup', (event) ->
switch event.which
# 's' keyboard shortcut
when 83
$('.toggle').toggleClass('is-active')
$('body').toggleClass('is-sidebar-open')
$('.sidebar').toggleClass('is-open')