summaryrefslogtreecommitdiff
path: root/frontend/static/js/register.js
blob: 24e8fa016bca077dba150e2df5e42a3caae8d8ba (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
function warn (s)
	{
	$('#msg').append(s+"<br/>")
	scrollToBottom('#msg')
	}
function scrollToTop (div)
	{ $(div).scrollTop( 0 ) }
function scrollToBottom (div)
	{ $(div).scrollTop( $(div)[0].scrollHeight ) }
function trim (s)
	{ if (s) { return s.replace(/^\s+|\s+$/g,"") } else { return s } }
function supports_html5_storage ()
	{
	try
		{ return 'localStorage' in window && window['localStorage'] !== null; }
	catch (e)
		{ return false }
	}
function noop() { return false }
var API =
	{
	HEADER: "#@scanjam 0.2",
	LIKE_STRING: "like",
	HAS_LOCAL_STORAGE: supports_html5_storage(),
	BASE_URL: "http://scannerjammer.com:19898",
	URL:
		{
		auth:
			{
			register: "/api/auth/register",
			available: "/api/auth/available",
			login: "/api/auth/login",
			logout: "/api/auth/logout",
			},
		room:
			{
			join: "/api/room/join",
			poll: "/api/room/poll",
			say: "/api/room/say",
			},
		video:
			{
			like: "/api/video/like",
			unlike: "/api/video/unlike",
			},
		},
	parse: function (api,raw)
		{
		if (! raw)
			{ warn(api+": no result"); return false }
		var lines = raw.split("\n")
		if (lines.shift() !== API.HEADER)
			{ warn(api+": no header"); return false }
		if (! lines.length)
			{ warn(api+": no content"); return false }
		return lines
		},
	init: function ()
		{
		for (type in API.URL)
			{
			for (name in API.URL[type])
				{
				API.URL[type][name] = API.BASE_URL + API.URL[type][name]
				}
			}
		},
	}
var Local = API.HAS_LOCAL_STORAGE ?
	{
	getOrSet: function (key, value)
		{
		if (value)
			localStorage["scanjam."+key] = value
		else
			return localStorage.getItem("scanjam."+key)
		},
	userid: function (id)
		{ return Local.getOrSet("userid", id) },
	username: function (name)
		{ return Local.getOrSet("username", name) },
	isLiked: function (videoid)
		{ return localStorage.getItem("scanjam.like."+videoid) === "true" },
	unlike: function (videoid)
		{ localStorage["scanjam.like."+videoid] = false },
	like: function (videoid)
		{ localStorage["scanjam.like."+videoid] = true },
	} : { isLiked: noop, unlike: noop, like: noop, getOrSet: noop, username: noop, userid: noop, }
var Register =
	{
	userok: false,
	pwok: false,
	username: false,
	pwtimeout: false,
	activated: false,
	submit: function ()
		{
		if (! Register.userok || ! Register.pwok)
			return
		$("#register-go").unbind("click")
		var username = $("#register-username").val()
		var password = $("#register-pw2").val()
		var pwhash = $.md5("scanjam"+password)
		Register.username = username
		var data = 
			{
			'username': username,
			'password': pwhash,
			}
		$("#success-username").html(username)
		$.post(API.URL.auth.register, data).success(Register.submitCallback).error(Register.errorCallback)
		},
	errorCallback: function (raw)
		{
		Register.error("#username-available","weird problem, err try again")
		Register.error("#password-match", "")
		Register.deactivateGoButton()
		Register.activateGoButton()
		return
		},
	submitCallback: function (raw)
		{
		lines = API.parse("/api/register",raw)
		if (! lines || lines[0] !== "OK")
			return Register.errorCallback()
		var u = lines[1].split("\t")
		Local.userid(u[0])
		Local.username(u[1])
		document.cookie = "session="+u[2]+";path=/;domain=.scannerjammer.com;max-age=1086400"

		$("#register").fadeOut(1000, function ()
			{
			$("#success").fadeIn(1000, function()
				{
				$("#bg").fadeIn(700, 'linear')
				})
		 	})
		},
	checkPassword: function ()
		{
		var pw1 = $("#register-pw").val()
		var pw2 = $("#register-pw2").val()
		if (! pw1 && ! pw2)
			return
		if (pw1 && ! pw2)
			return
		if (pw1 !== pw2)
			{
			Register.error("#password-match", "passwords don't match..")
			Register.pwok = false
			return
			}
		$("#password-match").removeClass("error")
		$("#password-match").html("passwords match!")
		Register.pwok = true
		if (Register.userok)
			Register.activateGoButton()
		},
	deactivateGoButton: function ()
		{
		$("#register-go").css("color", "#000")
		$("#register-go").animate({opacity: 0.2}, 500)
		Register.activated = false
		},
	activateGoButton: function ()
		{
		$("#register-go").css("color", "#00f")
		$("#register-go").animate({opacity: 1.0}, 500)
		if (! Register.activated)
			{
			Register.activated = true
			$("#register-go").bind("click", Register.submit)
			}
		},
	error: function (id, msg)
		{
		$(id).addClass("error")
		$(id).html(msg)
		Register.deactivateGoButton()
		},
	checkAvailability: function ()
		{
		var isalphanumeric = /^[a-zA-Z0-9]+$/
		var username = $("#register-username").val()
		if (! username)
			{
			Register.error("#username-available", "please enter a username..")
			Register.userok = false
			return
			}
		if (isalphanumeric.test(username) === false)
			{
			Register.error("#username-available", "just letters/numbers plz..")
			Register.userok = false
			return
			}
		$.post(API.URL.auth.available, {'username':username}, Register.checkAvailabilityCallback)
		},
	checkAvailabilityCallback: function (raw)
		{
		lines = API.parse("/user/register", raw)
		if (! lines || lines[0] !== "OK")
			{
			alert(raw)
			Register.error("#username-available", "name already taken..")
			Register.userok = false
			return
			}
		$("#username-available").removeClass("error")
		$("#username-available").html("available!")
		Register.userok = true
		if (Register.pwok)
			Register.activateGoButton()
		},
	usernameUpdate: function (event)
		{
		Register.userok = false
		return true
		},
	pwUpdate: function (event)
		{
		// if (event.keyCode === 13)
		// 	{
		// 	Register.submit()
		// 	return false
		// 	}
		if (Register.pwtimeout)
			clearTimeout(Register.pwtimeout)
		Register.pwtimeout  = setTimeout(Register.checkPassword, 100)
		return true
		},
	init: function ()
		{
		$("#register-username").val("")
		$("#register-pw").val("")
		$("#register-pw2").val("")
		$("#register-username").bind("blur", Register.checkAvailability)
		$("#register-pw2").bind("blur", Register.checkPassword)
		$("#register-username").bind("keydown", Register.usernameUpdate)
		$("#register-pw").bind("keydown", Register.pwUpdate)
		$("#register-pw2").bind("keydown", Register.pwUpdate)
		$("#register").fadeIn(1000, function ()
			{
			$("#plant").fadeIn(1000)
			})
		$("#register-username").focus()
		},
	}
var Main =
	{
	roomName: false,
	enter: false,
	resize: function ()
		{
		var w = $(window).width()
		var h = $(window).height()

		$("#bg img").css("width", w)
		$("#bg img").css("height", h)

		$("#msg").css("max-height", h-130)
		},
	kp: function (event)
		{
		if (event.keyCode === 13)
			{
			if (Main.enter)
				Main.enter()
			return false
			}
		return true
		},
	init: function ()
		{
		warn("INIT")
		$("#msg").hide()

		$(window).resize(Main.resize)
		Main.resize()

		API.init()
		$(window).load(Register.init)

		if (window.location.hash)
			{
			Main.roomName = window.location.hash.replace("#","")
			$("#sj-link").attr("href", "/"+Main.roomName+"/")
			}
		},
	}
Main.init()