summaryrefslogtreecommitdiff
path: root/frontend/static/js/register.js
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/static/js/register.js')
-rw-r--r--frontend/static/js/register.js292
1 files changed, 292 insertions, 0 deletions
diff --git a/frontend/static/js/register.js b/frontend/static/js/register.js
new file mode 100644
index 0000000..24e8fa0
--- /dev/null
+++ b/frontend/static/js/register.js
@@ -0,0 +1,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()
+