Mercurial > summit-idp
view static-files/js/summit-idp.js @ 58:422fcf9774b1
added js, css, and img subdirectories to static-files.
author | Atul Varma <avarma@mozilla.com> |
---|---|
date | Mon, 28 Jun 2010 16:51:13 -0700 |
parents | static-files/summit-idp.js@0a9c62c25938 |
children |
line wrap: on
line source
// ----------------------------------------------------------------------- // Ajax object // ----------------------------------------------------------------------- // // This provides simple functions for making Ajax calls easily. ( function(window) { var Ajax = window.Ajax = { getJSON: function getJSON(path, obj, cb) { if (obj) path += "?" + queryString(obj); var req = new XMLHttpRequest(); req.open("GET", path); prepare(req, cb); req.send(null); return req; }, postJSON: function postJSON(path, obj, cb) { var body = JSON.stringify(obj); var req = new XMLHttpRequest(); req.open("POST", path); req.setRequestHeader("Content-Type", "application/json"); prepare(req, cb); req.send(body); return req; } }; function queryString(data) { var parts = []; for (name in data) { var values = data[name]; if (!values.forEach) values = [values]; values.forEach( function(value) { parts.push(encodeURI(name) + "=" + encodeURI(value)); }); } return parts.join("&"); } function prepare(req, cb) { function onLoad(event) { var data = null; try { data = JSON.parse(req.responseText); } catch (e) {} if (req.status >= 200 && req.status < 300) cb(true, data); else cb(false, data); } function onError(event) { var data = null; try { data = JSON.parse(req.responseText); } catch (e) {} cb(false, data); } req.addEventListener("load", onLoad, false); req.addEventListener("error", onError, false); req.addEventListener("abort", onError, false); req.overrideMimeType("application/json"); } } )(window); // ----------------------------------------------------------------------- // Config object // ----------------------------------------------------------------------- // // This manages interactions with client-side persistent storage and the // current login state of the app. ( function(window) { // Amount of time, in milliseconds, that pass before we check // to see if another instance of this page changed our persistent // storage. const REFRESH_INTERVAL = 2000; var Config = window.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: [] }; function ensureStateIsValid() { if (!('state' in Config.value)) Config.setValue({state: "login"}); } Config.observers.push(ensureStateIsValid); // This is useful for finding out whether our configuration has // been changed by another instance of our same window. function setupConfigWatcher() { var lastChanged = Config.lastChanged; function onConfigChanged() { lastChanged = Config.lastChanged; }; Config.observers.push(onConfigChanged); window.setInterval( function() { if (Config.lastChanged > lastChanged) Config.observers.forEach(function(cb) { cb(); }); }, REFRESH_INTERVAL ); } function initConfig() { ensureStateIsValid(); setupConfigWatcher(); } window.addEventListener("DOMContentLoaded", initConfig, false); } )(window); // ----------------------------------------------------------------------- // Attendees object // ----------------------------------------------------------------------- // // This manages the list of Summit attendees and their shared metadata. ( function(window) { var req; var Attendees = window.Attendees = { refresh: function refresh() { if (req) return; var self = this; if (Config.value.token) { req = Ajax.getJSON( "api/profile", {token: Config.value.token}, function(success, data) { req = null; if (success) { self.all = data.contents; self.currentUser = self.all[Config.value.userID]; self.observers.forEach(function(cb) { cb(); }); } else { // TODO: Raise an error? } }); } }, currentUser: null, all: null, observers: [] }; function refreshAttendees() { if (Config.value.state == "logged-in" && !Attendees.all) Attendees.refresh(); } Config.observers.push(refreshAttendees); window.addEventListener("DOMContentLoaded", refreshAttendees, false); } )(window);