Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 115 lines (95 sloc) 3.562 kB
8c94069 @ajoslin initial commit
authored
1 # Code that runs on the client
2 if Meteor.is_client
3
4 # Create a variable for a client-side DB that syncs with the server-side DB 'entries'
5 Entries = new Meteor.Collection 'entries'
6
7 # Local variable to store current login cookie
8 _cookie = null
9
10 # 'Entries' item of leaderboard template
11 # is just the whole db sorted by wins in reverse
12 Template.leaderboard.entries = ->
13 return Entries.find {}, sort: {wins: -1}
14
15 # leaderboard and entry template have the same isAdmin function
16 # returns if Session.isAdmin == _cookie
17 Template.leaderboard.isAdmin = Template.entry.isAdmin = ->
18 Session.get 'isAdmin'
19 return Session.equals 'isAdmin', _cookie
20
21 # Creates a new editable field in the list
22 # with given properties, blank defaults if no properties given
23 newEdit = (name,wins,points,rep) ->
24 id = Entries.insert
25 name: name or ''
26 wins: wins or ''
27 points: points or ''
28 rep: rep or ''
29 Session.set "editing-#{id}", true
30
31 # All the events for the leaderboard template
32 Template.leaderboard.events =
33 'click #logout': ->
34 Session.set 'isAdmin', false
35 'click #auth': ->
36 # Hides/shows login-form
37 $("#login-form").toggle()
38 'click #addnew': ->
39 # Add new just creates a new blank editable item
40 newEdit()
41 'click #submit-login': ->
42 # Calls the server's login function with username/password field values
43 Meteor.call 'login', $('#login-text').val(), $('#password-text').val(),
44 # Once server responds, it will call the below function
45 (error, result) ->
46 if error?
47 # If error, show it and hide the login form for 1.5sec
48 $('#error-text').text('Error: '+error.reason).show()
49 $('#login-form').toggle()
50 $("#auth").hide()
51 Meteor.setTimeout ->
52 $('#error-text').hide()
53 $("#auth").show()
54 $("#login-form").show()
55 , 1500
56 else
57 # Else, set local cookie variable and isAdmin to the returned cookie
58 _cookie = result
59 Session.set 'isAdmin', result
60
61
62
63 # Whether an entry is being edited right now, for the template
64 Template.entry.editing = ->
65 return Session.get "editing-#{@_id}"
66
67 # all the events for an entry, save/edit/delete
68 Template.entry.events =
69 'click #save': ->
70 Entries.update { _id: @_id },
71 name: $("#input-name").val()
72 wins: $("#input-wins").val()
73 points: $("#input-points").val()
74 rep: $("#input-rep").val()
75 Session.set "editing-#{@_id}", false
76
77 'click #edit': ->
78 # Hack: When editing an entry, have to remove the entry then
79 # re-add it as editable in the same place for it to show up right in the table
80 newEdit @name, @wins, @points, @rep
81 Entries.remove _id: @_id
82
83 'click #delete': ->
84 # Delete the entry
85 Entries.remove { _id: @_id }
86
87
88
89
90 # Code that runs on the server
91 if Meteor.is_server
92
93 # Creates server-side db and allows client-side to have an 'entries' db
94 new Meteor.Collection 'entries'
95
96 # Makes a cookie (random string) of length len
97 makeCookie = (len) ->
98 chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
99 cookie = ""
100 for i in [1..len]
101 index = Math.floor Math.random()*chars.length
102 cookie += chars.substring index, index+1
103 return cookie
104
105 # Meteor.methods has all the methods that the client can call
106 Meteor.methods
107 # Called by client with Meteor.call 'login'
108 login: (username, password) ->
109 if username is "admin" and password is "admin"
110 # If successful, make a cookie of length 15
111 return makeCookie 15
112 else
113 # Else, throw a bad username/pass error and 401 (unauthorized)
114 throw new Meteor.Error 401, "Bad username or password!"
Something went wrong with that request. Please try again.