Mercurial > summit-idp
view static-files/api.js @ 53:18de6b362cc5
made server.html and fixed example-client.html to work with it instead of index.html.
author | Atul Varma <avarma@mozilla.com> |
---|---|
date | Sat, 26 Jun 2010 20:51:47 -0700 |
parents | e1b3e9916b57 |
children | fe5a2f26787d |
line wrap: on
line source
( // Set up the public API for the Summit IDP. function(window) { var currId = 0; function MessageBroker(handlers, postMessage) { var awaitingResponses = {}; function sendResponse(id, value, target) { } this.callCmd = function callCmd(cmd, options, cb) { var msg = { type: "cmd", name: cmd, id: ++currId, options: options }; if (cb) { awaitingResponses[currId] = cb; msg.cb = true; } postMessage(msg); }; this.onMessage = function onMessage(msg, origin, source) { switch (msg.type) { case "response": if (msg.id in awaitingResponses) { var cb = awaitingResponses[msg.id]; delete awaitingResponses[msg.id]; cb(msg.value); } break; case "cmd": var handler = msg.name; if (handler in handlers) { var cb = undefined; if (msg.cb) cb = function(response) { postMessage({type: "response", id: msg.id, value: response }, source); }; handlers[handler](msg.options, cb, origin); } break; } }; } function Server(handlers) { var broker = new MessageBroker(handlers, postMessage); var originBrokers = {}; function brokerForOrigin(origin) { function postMessage(msg, target) { target.postMessage(JSON.stringify(msg), origin); } return new MessageBroker(handlers, postMessage); } function onMessage(event) { if (!(event.origin in originBrokers)) originBrokers[event.origin] = brokerForOrigin(event.origin); originBrokers[event.origin].onMessage(JSON.parse(event.data), event.origin, event.source); } window.addEventListener("message", onMessage, false); } function GenericClient(origin, path) { var broker = new MessageBroker({}, postMessage); var iframe = window.document.createElement("iframe"); var otherWindow; var queuedMessages = []; if (!path) path = "/"; path += "server.html"; iframe.src = origin + path; iframe.onload = function() { otherWindow = iframe.contentWindow; queuedMessages.forEach(postMessage); queuedMessages = []; }; iframe.style.display = "none"; window.document.documentElement.appendChild(iframe); function postMessage(msg) { if (!otherWindow) queuedMessages.push(msg); else otherWindow.postMessage(JSON.stringify(msg), origin); } function onMessage(event) { if (event.origin == origin) broker.onMessage(JSON.parse(event.data), event.origin, event.source); } this.callCmd = broker.callCmd; window.addEventListener("message", onMessage, false); }; function Client(origin, path) { var self = this; var client = new GenericClient(origin, path); function addMethod(name) { self[name] = function(options, cb) { if (typeof(options) == "function" && !cb) { cb = options; options = null; } client.callCmd(name, options, cb); }; } addMethod("getAllUsers"); } window.Summit = { Client: Client, Server: Server }; } )(window);