Commit 5e269eda authored by Sven Franck's avatar Sven Franck

updated first example to only using Ajax links

parent 8a1e595d
// > grab URL with parameters of file to open
// > call addGadget with those parameters?
// > display a file from some storage (local/session)
// > storage type will also be a parameter in the url?
// return "browser://localstorage/foo"
// file=browser%3A%2F%2Flocalstorage%2Ffoo
/*global document, jQuery */ /*global document, jQuery */
"use strict"; "use strict";
(function (document, $) { (function (document, $) {
...@@ -24,6 +17,10 @@ ...@@ -24,6 +17,10 @@
}; };
var handler = function (event) { var handler = function (event) {
var type = event.data.type,
method = type ? type.split("/")[0] : undefined;
// prevent both renderJs and page events triggering on "run"
if (type === undefined || method !== "run") {
ajaxGet(event.data, function(value, status, jqXHR) { ajaxGet(event.data, function(value, status, jqXHR) {
ajaxGet(value._links.enclosure.href, function(value, status, jqXHR) { ajaxGet(value._links.enclosure.href, function(value, status, jqXHR) {
if (value === "") { if (value === "") {
...@@ -34,13 +31,26 @@ ...@@ -34,13 +31,26 @@
}); });
}); });
} }
}
var getParameter = function(searchString, paramName) {
var i, val, params = searchString.split("&");
for (i=0;i<params.length;i++) {
val = params[i].split("=");
if (val[0] == paramName) {
return decodeURIComponent(val[1]);
}
}
return null;
};
var mapUrl = function (url) { var mapUrl = function (searchString) {
var searchString = url.href.split("?")[1], var fileToDisplay = getParameter(searchString, "file"),
fileToDisplay; scope,
register,
service;
if (searchString) {
fileToDisplay = getParameter(searchString, "file");
if (fileToDisplay) { if (fileToDisplay) {
$.ajax({ $.ajax({
method: 'GET', method: 'GET',
...@@ -50,43 +60,49 @@ ...@@ -50,43 +60,49 @@
$(this).text(errorThrown); $(this).text(errorThrown);
}, },
success: function (value, textStatus, jqXHR) { success: function (value, textStatus, jqXHR) {
if (value === "") {
$(this).text("file not found"); scope = value._links.scope.href.slice(0,-1).split(/[/]+/).pop();
} else { register = value._links.call.href
$(this).text(value); .replace("{method}", "register")
} .replace("{scope}", scope )
.replace("{interaction}", "");
service = {
"type": "service/test",
"src": encodeURIComponent(window.location.href),
"rel": "preview",
"self": window.frameElement.id
};
$.ajax({
method: 'POST',
url: register,
context: $(this),
data: JSON.stringify(service),
error: function (jqXHR, textStatus, errorThrown) {
console.log("registration failed: " + errorThrown);
}, },
}); success: function (value, textStatus, jqXHR) {
// console.log("registration successful");
} }
} else { });
$(this).text("no file to display");
} }
}; });
var getParameter = function(searchString, paramName) {
var i, val, params = searchString.split("&");
for (i=0;i<params.length;i++) {
val = params[i].split("=");
if (val[0] == paramName) {
return decodeURIComponent(val[1]);
} }
} }
return null;
};
$(document).ready(function () { $(document).ready(function () {
// mapUrl(window.location); var search = window.location.search;
if (search) {
mapUrl(search.slice(1));
} else {
$("body").text("No parameter found in url");
}
if (window.addEventListener){ if (window.addEventListener){
window.addEventListener("message", handler, false) window.addEventListener("message", handler, false)
} else { } else {
window.attachEvent("onmessage", handler) window.attachEvent("onmessage", handler)
} }
// $(window).on('hashchange', function() {
// console.log("LE HASH CHANGED");
// });
// $(window).trigger("hashchange");
}); });
}(document, jQuery)); }(document, jQuery));
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment