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 under the License.
19 * @Functions for the samplecontainer
23 * Public Shindig namespace with samplecontainer object
26 var shindig = shindig || {};
27 shindig.samplecontainer = {};
30 * Hide our functions and variables from other javascript
39 var parentUrl = document.location.href;
40 var baseUrl = parentUrl.substring(0, parentUrl.indexOf('samplecontainer.html'));
42 // TODO: This is gross, it needs to use the config just like the gadget js does
43 var socialDataPath = document.location.protocol + "//" + document.location.host
44 + "/social/rest/samplecontainer/";
46 var gadgetUrlMatches = /[?&]url=((?:[^#&]+|&)+)/.exec(parentUrl);
47 var gadgetUrl = (gadgetUrlMatches)
49 : baseUrl + 'examples/SocialHelloWorld.xml';
51 var gadgetUrlCookie = 'sampleContainerGadgetUrl';
53 var stateFileUrl = baseUrl + '../sampledata/canonicaldb.json';
54 var stateFileUrlCookie = 'sampleContainerStateFileUrl';
62 var viewerId = "john.doe";
63 var ownerId = "john.doe";
65 var viewMatches = /[?&]view=((?:[^#&]+|&)+)/.exec(parentUrl);
66 var current_view = (viewMatches)
78 function generateSecureToken() {
79 // TODO: Use a less silly mechanism of mapping a gadget URL to an appid
81 for (var i = 0; i < gadgetUrl.length; i++) {
82 appId += gadgetUrl.charCodeAt(i);
84 var fields = [ownerId, viewerId, appId, "shindig", gadgetUrl, "0", "default"];
85 for (var i = 0; i < fields.length; i++) {
86 // escape each field individually, for metachars in URL
87 fields[i] = escape(fields[i]);
89 return fields.join(":");
92 SampleContainerGadget = function(opt_params) {
93 gadgets.IfrGadget.call(this, opt_params);
96 SampleContainerGadget.inherits(gadgets.IfrGadget);
98 SampleContainerGadget.prototype.getAdditionalParams = function() {
105 params += "&usepermissive=1";
110 gadgets.container.gadgetClass = SampleContainerGadget;
112 function setEvilBit() {
113 sendRequestToServer('setevilness/' + doEvil, 'POST');
116 function reloadStateFile(opt_callback) {
117 sendRequestToServer('setstate', 'POST',
118 gadgets.io.encodeValues({"fileurl" : stateFileUrl}),
122 function sendRequestToServer(url, method, opt_postParams, opt_callback, opt_excludeSecurityToken) {
123 // TODO: Should re-use the jsoncontainer code somehow
124 opt_postParams = opt_postParams || {};
126 var makeRequestParams = {
127 "CONTENT_TYPE" : "JSON",
129 "POST_DATA" : opt_postParams};
131 if (!opt_excludeSecurityToken) {
132 url = socialDataPath + url + "?st=" + gadget.secureToken;
135 gadgets.io.makeNonProxiedRequest(url,
146 function generateGadgets(metadata) {
147 // TODO: The gadget.js file should really have a clearGadgets method
148 gadgets.container.view_ = current_view;
149 gadgets.container.gadgets_ = {};
150 for (var i = 0; i < metadata.gadgets.length; i++) {
151 gadget = gadgets.container.createGadget(
152 {'specUrl': metadata.gadgets[i].url, 'title': metadata.gadgets[i].title});
153 gadget.setServerBase('../../');
154 gadget.secureToken = escape(generateSecureToken());
155 gadgets.container.addGadget(gadget);
158 gadgets.container.layoutManager.setGadgetChromeIds(['gadget-chrome']);
159 reloadStateFile(function() {
160 gadgets.container.renderGadgets();
164 function refreshGadgets(metadata) {
165 // TODO: The gadget.js file should really have a getGadgets method
166 for (var gadget in gadgets.container.gadgets_) {
167 var newtitle = metadata.gadgets[0].title;
168 var specUrl = metadata.gadgets[0].url;
169 gadgets.container.gadgets_[gadget].title = newtitle;
170 gadgets.container.gadgets_[gadget].specUrl = specUrl;
171 gadgets.container.gadgets_[gadget].secureToken = escape(generateSecureToken());
173 reloadStateFile(function() {
174 gadgets.container.renderGadgets();
178 function requestGadgetMetaData(opt_callback) {
192 sendRequestToServer("/gadgets/metadata", "POST",
193 gadgets.json.stringify(request), opt_callback, true);
199 shindig.samplecontainer.initSampleContainer = function() {
200 // Upon initial load, check for the cache query parameter (we don't want
201 // to overwrite when clicking "refresh all")
202 var cacheUrlMatches = /[?&]cache=([01])/.exec(parentUrl);
203 if (cacheUrlMatches && cacheUrlMatches[1] == "0") {
204 document.getElementById("useCacheCheckbox").checked = false;
208 shindig.samplecontainer.initGadget = function() {
210 var cookieGadgetUrl = decodeURIComponent(shindig.cookies.get(gadgetUrlCookie));
211 if (cookieGadgetUrl && cookieGadgetUrl != "undefined") {
212 gadgetUrl = cookieGadgetUrl;
215 var cookieStateFileUrl = decodeURIComponent(shindig.cookies.get(stateFileUrlCookie));
216 if (cookieStateFileUrl && cookieStateFileUrl != "undefined") {
217 stateFileUrl = cookieStateFileUrl;
221 document.getElementById("stateFileUrl").value = stateFileUrl;
224 document.getElementById("gadgetUrl").value = gadgetUrl;
227 document.getElementById("viewerId").value = viewerId;
228 document.getElementById("ownerId").value = ownerId;
230 requestGadgetMetaData(generateGadgets);
233 shindig.samplecontainer.unpackFormState = function() {
234 useCaja = document.getElementById("useCajaCheckbox").checked;
235 useCache = document.getElementById("useCacheCheckbox").checked;
236 usePermissive = document.getElementById("usePermissiveCheckbox").checked;
237 doEvil = document.getElementById("doEvilCheckbox").checked;
240 shindig.samplecontainer.changeGadgetUrl = function() {
241 shindig.samplecontainer.unpackFormState();
242 gadgets.container.nocache_ = useCache ? 0 : 1;
246 stateFileUrl = document.getElementById("stateFileUrl").value;
247 shindig.cookies.set(stateFileUrlCookie, encodeURIComponent(stateFileUrl));
249 viewerId = document.getElementById("viewerId").value;
250 ownerId = document.getElementById("ownerId").value;
251 gadgetUrl = document.getElementById("gadgetUrl").value;
253 shindig.cookies.set(gadgetUrlCookie, encodeURIComponent(gadgetUrl));
255 requestGadgetMetaData(refreshGadgets);
258 shindig.samplecontainer.dumpStateFile = function() {
259 sendRequestToServer('dumpstate', 'GET', null,
262 alert("Could not dump the current state.");
264 document.getElementById('gadgetState').innerHTML
265 = gadgets.json.stringify(data);