Skip to content

Commit

Permalink
Read manifest file using asynchronous request. Fixes #127.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmartel committed Mar 2, 2015
1 parent 7bdee85 commit a392f00
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
15 changes: 11 additions & 4 deletions src/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,7 @@ dwv.App = function ()
}
// possible load from URL
if ( typeof config.skipLoadUrl === "undefined" ) {
var inputUrls = dwv.html.getUriParam();
if ( inputUrls && inputUrls.length > 0 ) {
this.loadURL(inputUrls);
}
dwv.html.getUriParam(window.location.href, this.onInputURLs);
}
else{
console.log("Not loading url from adress since skipLoadUrl is defined.");
Expand Down Expand Up @@ -731,6 +728,16 @@ dwv.App = function ()
self.loadURL([event.target.value]);
};

/**
* Handle input urls.
* @method onInputURLs
* @param {Array} urls The list of input urls.
*/
this.onInputURLs = function (urls)
{
self.loadURL(urls);
};

/**
* Handle change files event.
* @method onChangeFiles
Expand Down
36 changes: 19 additions & 17 deletions src/gui/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,13 +401,13 @@ dwv.html.createHtmlSelect = function(name, list) {
* @method getUriParam
* @static
* @param {String } uri The URI to decode.
* @return {Array} The array of parameters.
* @param {Function} The function to call with the decoded urls.
* @return {Array} The list of urls if in uri, null otherwise.
*/
dwv.html.getUriParam = function(uri)
dwv.html.getUriParam = function(uri, callback)
{
var inputUri = uri || window.location.href;
// split key/value pairs
var mainQueryPairs = dwv.utils.splitQueryString(inputUri);
var mainQueryPairs = dwv.utils.splitQueryString(uri);
// check pairs
if( Object.keys(mainQueryPairs).length === 0 ) {
return null;
Expand All @@ -418,17 +418,22 @@ dwv.html.getUriParam = function(uri)
throw new Error("No input parameter in query URI.");
}

var result = [];
// if manifest
if( query.type && query.type === "manifest" ) {
result = dwv.html.decodeManifestUri( query.input, query.nslices );
dwv.html.decodeManifestUri( query.input, query.nslices, callback );
}
// if key/value uri
else {
result = dwv.html.decodeKeyValueUri( query.input, query.dwvReplaceMode );
var urls = dwv.html.decodeKeyValueUri( query.input, query.dwvReplaceMode );
if ( typeof callback != "undefined" ) {
callback(urls);
}
else {
return urls;
}
}

return result;
// default return
return null;
};

/**
Expand Down Expand Up @@ -523,11 +528,10 @@ dwv.html.decodeKeyValueUri = function(uri, replaceMode)
* @static
* @param {String} uri The uri to decode.
* @param {number} nslices The number of slices to load.
* @param {Function} The function to call with the decoded urls.
*/
dwv.html.decodeManifestUri = function(uri, nslices)
dwv.html.decodeManifestUri = function(uri, nslices, callback)
{
var result = [];

// Request error
var onErrorRequest = function(/*event*/)
{
Expand All @@ -537,20 +541,18 @@ dwv.html.decodeManifestUri = function(uri, nslices)
// Request handler
var onLoadRequest = function(/*event*/)
{
result = dwv.html.decodeManifest(this.responseXML, nslices);
var urls = dwv.html.decodeManifest(this.responseXML, nslices);
callback(urls);
};

var request = new XMLHttpRequest();
// synchronous request (third parameter)
request.open('GET', decodeURIComponent(uri), false);
request.open('GET', decodeURIComponent(uri), true);
request.responseType = "xml";
request.onload = onLoadRequest;
request.onerror = onErrorRequest;
//request.onprogress = dwv.gui.updateProgress;
request.send(null);

// return
return result;
};

/**
Expand Down

0 comments on commit a392f00

Please sign in to comment.