2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
21 * API to assist with management of the OAuth popup window.
23 * MAKE A COPY OF THIS FILE. Do not hot link to it.
27 * 1) Gadget attempts to fetch OAuth data for the user and discovers that
28 * approval is needed. The gadget creates two new UI elements:
30 * - a "personalize this gadget" button or link
31 * - a "personalization done" button or link, which is initially hidden.
33 * With any luck, the user will never need to click the "personalization done"
34 * button, but it should be created and displayed in case we can't
35 * automatically detect when the user has approved access to their gadget.
37 * 2) Gadget creates a popup object and associates event handlers with the UI
40 * var popup = shindig.oauth.popup({
41 * destination: response.oauthApprovalUrl,
42 * windowOptions: "height=300,width=200",
43 * onOpen: function() {
44 * $("personalizeDone").style.display = "block"
46 * onClose: function() {
47 * $("personalizeDone").style.display = "none"
48 * $("personalizeDone").style.display = "none"
53 * personalizeButton.onclick = popup.createOpenerOnClick();
54 * personalizeDoneButton.onclick = popup.createApprovedOnClick();
56 * 3) When the user clicks the personalization button/link, a window is opened
57 * to the approval URL.
59 * 4) When the window is closed, the oauth popup calls the onClose function
60 * and the gadget attempts to fetch the user's data.
63 var shindig = shindig || {};
64 shindig.oauth = shindig.oauth || {};
67 * Initialize a new OAuth popup manager. Parameters must be specified as
68 * an object, e.g. shindig.oauth.popup({destination: somewhere,...});
70 * @param {String} destination Target URL for the popup window.
71 * @param {String} windowOptions Options for window.open, used to specify
72 * look and feel of the window.
73 * @param {function} onOpen Function to call when the window is opened.
74 * @param {function} onClose Function to call when the window is closed.
76 shindig.oauth.popup = function(options) {
77 if (!("destination" in options)) {
78 throw "Must specify options.destination";
80 if (!("windowOptions" in options)) {
81 throw "Must specify options.windowOptions";
83 if (!("onOpen" in options)) {
84 throw "Must specify options.onOpen";
86 if (!("onClose" in options)) {
87 throw "Must specify options.onClose";
89 var destination = options.destination;
90 var windowOptions = options.windowOptions;
91 var onOpen = options.onOpen;
92 var onClose = options.onClose;
99 // Called when we recieve an indication the user has approved access, either
100 // because they closed the popup window or clicked an "I've approved" button.
101 function handleApproval() {
103 window.clearInterval(timer);
114 // Called at intervals to check whether the window has closed. If it has,
115 // we act as if the user had clicked the "I've approved" link.
116 function checkClosed() {
117 if ((!win) || win.closed) {
124 * @return an onclick handler for the "open the approval window" link
126 function createOpenerOnClick() {
128 // If a popup blocker blocks the window, we do nothing. The user will
129 // need to approve the popup, then click again to open the window.
130 // Note that because we don't call window.open until the user has clicked
131 // something the popup blockers *should* let us through.
132 win = window.open(destination, "_blank", windowOptions);
134 // Poll every 100ms to check if the window has been closed
135 timer = window.setInterval(checkClosed, 100);
143 * @return an onclick handler for the "I've approved" link. This may not
144 * ever be called. If we successfully detect that the window was closed,
145 * this link is unnecessary.
147 function createApprovedOnClick() {
148 return handleApproval;
152 createOpenerOnClick: createOpenerOnClick,
153 createApprovedOnClick: createApprovedOnClick