Mercurial > summit-idp
view static-files/index.js @ 18:f6db6f8cbf5b
added same-origin policy for now.
author | Atul Varma <avarma@mozilla.com> |
---|---|
date | Fri, 25 Jun 2010 11:43:51 -0700 |
parents | 18d28e6a9887 |
children | e1b3e9916b57 |
line wrap: on
line source
var Config = { get value() { var val = localStorage.getItem("SUMMIT_CFG"); if (val) return JSON.parse(val); return {}; }, get lastChanged() { var ts = localStorage.getItem("SUMMIT_CFG_TIMESTAMP"); if (ts) return new Date(ts); return new Date(0); }, setValue: function Config_setValue(val) { localStorage.setItem("SUMMIT_CFG", JSON.stringify(val)); localStorage.setItem("SUMMIT_CFG_TIMESTAMP", (new Date()).toString()); this.observers.forEach(function(cb) { cb(); }); }, wipe: function Config_wipe() { localStorage.removeItem("SUMMIT_CFG"); localStorage.removeItem("SUMMIT_CFG_TIMESTAMP"); this.observers.forEach(function(cb) { cb(); }); }, observers: [] }; var Api = { postJSON: function Api_postJSON(path, obj, cb) { var options = { url: path, type: "POST", contentType: "application/json", data: JSON.stringify(obj), dataType: "json", success: function(data) { cb(true, data); }, error: function(req, textStatus, errorThrown) { var data = null; if (req.getResponseHeader("Content-Type") == "application/json") { try { data = JSON.parse(req.responseText); } catch (e) {} } cb(false, data); } }; return jQuery.ajax(options); } }; ( // This anonymous closure sets up the UI. function(window) { function ensureStateIsValid() { if (!('state' in Config.value)) Config.setValue({state: "login"}); } function updateUI() { ensureStateIsValid(); $(".screen").hide(); $("#" + Config.value.state).show(); switch (Config.value.state) { case "login": break; case "wait-for-verify": break; case "logged-in": $(".login-email").text(Config.value.email); break; } } function bindConfigToUI() { var lastChanged = Config.lastChanged; function onConfigChanged() { lastChanged = Config.lastChanged; updateUI(); }; Config.observers.push(onConfigChanged); window.setInterval( function() { if (Config.lastChanged > lastChanged) onConfigChanged(); }, 1000 ); updateUI(); } function initUI() { ensureStateIsValid(); $(".start-over").click(function() { Config.wipe(); }); $("#login form").submit( function(event) { event.preventDefault(); $("#login .error").hide(); Api.postJSON( "/challenge/request", {email: $(this).find("#email").val() }, function(success, data) { if (success) { Config.setValue({state: "wait-for-verify"}); } else { $("#login .error").slideDown(); } }); }); var verify = window.location.hash.match(/#verify=(.+)/); if (verify && Config.value.state != "logged-in") { verify = verify[1]; Config.setValue({state: "wait-for-verify"}); Api.postJSON( "/challenge/respond", {token: verify}, function(success, data) { window.location.hash = ""; bindConfigToUI(); if (success) { Config.setValue({state: "logged-in", token: data.token, email: data.email}); } else { $("#wait-for-verify .error").slideDown(); } }); } else bindConfigToUI(); } $(window).ready(initUI); } )(window); ( // Set up the postMessage API. function(window) { var handlers = { getAllUsers: function(options, cb) { if (Config.value.state != "logged-in") { cb({error: "not logged in"}); return; } jQuery.getJSON( "/profile", {token: Config.value.token}, function(data, textStatus) { if (textStatus == "success") cb({users: data}); else cb({error: "an error occurred retrieving user data."}); }); } }; var myOrigin = window.location.protocol + "//" + window.location.host; function isOriginValid(origin) { if (origin == myOrigin) return true; // TODO: Finish this. return false; } var server = new Summit.Server(handlers, isOriginValid); } )(window);