-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshenjs.coffee
65 lines (56 loc) · 1.71 KB
/
shenjs.coffee
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
Devices = new Meteor.Collection 'devices'
Jumps = new Meteor.Collection 'jumps'
if Meteor.isClient
currentPageNumber = 1
document.addEventListener 'click', (event) ->
unless event.target.className is 'tip-btn'
onclick = $('.tips').fadeOut()
scrollToPage = (pageNumber) ->
pageDOM = document.querySelectorAll('.page')[pageNumber - 1]
if pageDOM
scrollTo(pageDOM.offsetTop)
Template.counter.helpers
count: -> Devices.find({}).count()
unless Session.get 'myDeviceId'
myDeviceId = Devices.insert
userAgent: window.navigator.userAgent
Session.set 'intervalId', Meteor.setInterval ->
Meteor.call('heartbeat', myDeviceId)
, 1000
Session.set 'myDeviceId', myDeviceId
Jumps.find({}).observe
added: (jump) ->
pageNumber = jump.number
if pageNumber isnt currentPageNumber
currentPageNumber = pageNumber
scrollToPage(currentPageNumber)
setTimeout ->
Jumps.remove jump._id
, 5000
keyguru ['up'], (event) ->
event.preventDefault()
if currentPageNumber > 1
currentPageNumber--
scrollToPage(currentPageNumber)
Jumps.insert
number: currentPageNumber
keyguru ['down'], (event) ->
event.preventDefault()
if currentPageNumber < document.querySelectorAll('.page').length
currentPageNumber++
scrollToPage(currentPageNumber)
Jumps.insert
number: currentPageNumber
if Meteor.isServer
Meteor.methods
heartbeat: (deviceId) ->
Devices.update
_id: deviceId
,
$set:
ts: Date.now()
Meteor.startup ->
Meteor.setInterval ->
Devices.remove {ts: {$lt: Date.now() - 3000}}
console.log Devices.find({}).fetch().length
, 1000