Commit 1bc097e0 authored by Gradl, Tobias's avatar Gradl, Tobias
Browse files

1315: Download button for MWW instance (iframe based)

Task-Url: https://pm.winseda.de/issues/1315
parent a4b44de3
/* Blob.js
* A Blob implementation.
* 2014-07-24
*
* By Eli Grey, http://eligrey.com
* By Devin Samarin, https://github.com/dsamarin
* License: X11/MIT
* See https://github.com/eligrey/Blob.js/blob/master/LICENSE.md
*/
/*global self, unescape */
/*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true,
plusplus: true */
/*! @source http://purl.eligrey.com/github/Blob.js/blob/master/Blob.js */
(function (view) {
"use strict";
view.URL = view.URL || view.webkitURL;
if (view.Blob && view.URL) {
try {
new Blob;
return;
} catch (e) {}
}
// Internally we use a BlobBuilder implementation to base Blob off of
// in order to support older browsers that only have BlobBuilder
var BlobBuilder = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder || (function(view) {
var
get_class = function(object) {
return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
}
, FakeBlobBuilder = function BlobBuilder() {
this.data = [];
}
, FakeBlob = function Blob(data, type, encoding) {
this.data = data;
this.size = data.length;
this.type = type;
this.encoding = encoding;
}
, FBB_proto = FakeBlobBuilder.prototype
, FB_proto = FakeBlob.prototype
, FileReaderSync = view.FileReaderSync
, FileException = function(type) {
this.code = this[this.name = type];
}
, file_ex_codes = (
"NOT_FOUND_ERR SECURITY_ERR ABORT_ERR NOT_READABLE_ERR ENCODING_ERR "
+ "NO_MODIFICATION_ALLOWED_ERR INVALID_STATE_ERR SYNTAX_ERR"
).split(" ")
, file_ex_code = file_ex_codes.length
, real_URL = view.URL || view.webkitURL || view
, real_create_object_URL = real_URL.createObjectURL
, real_revoke_object_URL = real_URL.revokeObjectURL
, URL = real_URL
, btoa = view.btoa
, atob = view.atob
, ArrayBuffer = view.ArrayBuffer
, Uint8Array = view.Uint8Array
, origin = /^[\w-]+:\/*\[?[\w\.:-]+\]?(?::[0-9]+)?/
;
FakeBlob.fake = FB_proto.fake = true;
while (file_ex_code--) {
FileException.prototype[file_ex_codes[file_ex_code]] = file_ex_code + 1;
}
// Polyfill URL
if (!real_URL.createObjectURL) {
URL = view.URL = function(uri) {
var
uri_info = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
, uri_origin
;
uri_info.href = uri;
if (!("origin" in uri_info)) {
if (uri_info.protocol.toLowerCase() === "data:") {
uri_info.origin = null;
} else {
uri_origin = uri.match(origin);
uri_info.origin = uri_origin && uri_origin[1];
}
}
return uri_info;
};
}
URL.createObjectURL = function(blob) {
var
type = blob.type
, data_URI_header
;
if (type === null) {
type = "application/octet-stream";
}
if (blob instanceof FakeBlob) {
data_URI_header = "data:" + type;
if (blob.encoding === "base64") {
return data_URI_header + ";base64," + blob.data;
} else if (blob.encoding === "URI") {
return data_URI_header + "," + decodeURIComponent(blob.data);
} if (btoa) {
return data_URI_header + ";base64," + btoa(blob.data);
} else {
return data_URI_header + "," + encodeURIComponent(blob.data);
}
} else if (real_create_object_URL) {
return real_create_object_URL.call(real_URL, blob);
}
};
URL.revokeObjectURL = function(object_URL) {
if (object_URL.substring(0, 5) !== "data:" && real_revoke_object_URL) {
real_revoke_object_URL.call(real_URL, object_URL);
}
};
FBB_proto.append = function(data/*, endings*/) {
var bb = this.data;
// decode data to a binary string
if (Uint8Array && (data instanceof ArrayBuffer || data instanceof Uint8Array)) {
var
str = ""
, buf = new Uint8Array(data)
, i = 0
, buf_len = buf.length
;
for (; i < buf_len; i++) {
str += String.fromCharCode(buf[i]);
}
bb.push(str);
} else if (get_class(data) === "Blob" || get_class(data) === "File") {
if (FileReaderSync) {
var fr = new FileReaderSync;
bb.push(fr.readAsBinaryString(data));
} else {
// async FileReader won't work as BlobBuilder is sync
throw new FileException("NOT_READABLE_ERR");
}
} else if (data instanceof FakeBlob) {
if (data.encoding === "base64" && atob) {
bb.push(atob(data.data));
} else if (data.encoding === "URI") {
bb.push(decodeURIComponent(data.data));
} else if (data.encoding === "raw") {
bb.push(data.data);
}
} else {
if (typeof data !== "string") {
data += ""; // convert unsupported types to strings
}
// decode UTF-16 to binary string
bb.push(unescape(encodeURIComponent(data)));
}
};
FBB_proto.getBlob = function(type) {
if (!arguments.length) {
type = null;
}
return new FakeBlob(this.data.join(""), type, "raw");
};
FBB_proto.toString = function() {
return "[object BlobBuilder]";
};
FB_proto.slice = function(start, end, type) {
var args = arguments.length;
if (args < 3) {
type = null;
}
return new FakeBlob(
this.data.slice(start, args > 1 ? end : this.data.length)
, type
, this.encoding
);
};
FB_proto.toString = function() {
return "[object Blob]";
};
FB_proto.close = function() {
this.size = 0;
delete this.data;
};
return FakeBlobBuilder;
}(view));
view.Blob = function(blobParts, options) {
var type = options ? (options.type || "") : "";
var builder = new BlobBuilder();
if (blobParts) {
for (var i = 0, len = blobParts.length; i < len; i++) {
if (Uint8Array && blobParts[i] instanceof Uint8Array) {
builder.append(blobParts[i].buffer);
}
else {
builder.append(blobParts[i]);
}
}
}
var blob = builder.getBlob(type);
if (!blob.slice && blob.webkitSlice) {
blob.slice = blob.webkitSlice;
}
return blob;
};
var getPrototypeOf = Object.getPrototypeOf || function(object) {
return object.__proto__;
};
view.Blob.prototype = getPrototypeOf(new view.Blob());
}(typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content || this));
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
var saveAs=saveAs||function(e){"use strict";if("undefined"==typeof navigator||!/MSIE [1-9]\./.test(navigator.userAgent)){var t=e.document,n=function(){return e.URL||e.webkitURL||e},o=t.createElementNS("http://www.w3.org/1999/xhtml","a"),r="download"in o,i=function(e){var t=new MouseEvent("click");e.dispatchEvent(t)},a=e.webkitRequestFileSystem,c=e.requestFileSystem||a||e.mozRequestFileSystem,u=function(t){(e.setImmediate||e.setTimeout)(function(){throw t},0)},f="application/octet-stream",s=0,d=500,l=function(t){var o=function(){"string"==typeof t?n().revokeObjectURL(t):t.remove()};e.chrome?o():setTimeout(o,d)},v=function(e,t,n){t=[].concat(t);for(var o=t.length;o--;){var r=e["on"+t[o]];if("function"==typeof r)try{r.call(e,n||e)}catch(i){u(i)}}},p=function(e){return/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(e.type)?new Blob(["",e],{type:e.type}):e},w=function(t,u,d){d||(t=p(t));var w,y,m,S=this,h=t.type,O=!1,R=function(){v(S,"writestart progress write writeend".split(" "))},b=function(){if((O||!w)&&(w=n().createObjectURL(t)),y)y.location.href=w;else{var o=e.open(w,"_blank");void 0==o&&"undefined"!=typeof safari&&(e.location.href=w)}S.readyState=S.DONE,R(),l(w)},g=function(e){return function(){return S.readyState!==S.DONE?e.apply(this,arguments):void 0}},E={create:!0,exclusive:!1};return S.readyState=S.INIT,u||(u="download"),r?(w=n().createObjectURL(t),o.href=w,o.download=u,void setTimeout(function(){i(o),R(),l(w),S.readyState=S.DONE})):(e.chrome&&h&&h!==f&&(m=t.slice||t.webkitSlice,t=m.call(t,0,t.size,f),O=!0),a&&"download"!==u&&(u+=".download"),(h===f||a)&&(y=e),c?(s+=t.size,void c(e.TEMPORARY,s,g(function(e){e.root.getDirectory("saved",E,g(function(e){var n=function(){e.getFile(u,E,g(function(e){e.createWriter(g(function(n){n.onwriteend=function(t){y.location.href=e.toURL(),S.readyState=S.DONE,v(S,"writeend",t),l(e)},n.onerror=function(){var e=n.error;e.code!==e.ABORT_ERR&&b()},"writestart progress write abort".split(" ").forEach(function(e){n["on"+e]=S["on"+e]}),n.write(t),S.abort=function(){n.abort(),S.readyState=S.DONE},S.readyState=S.WRITING}),b)}),b)};e.getFile(u,{create:!1},g(function(e){e.remove(),n()}),g(function(e){e.code===e.NOT_FOUND_ERR?n():b()}))}),b)}),b)):void b())},y=w.prototype,m=function(e,t,n){return new w(e,t,n)};return"undefined"!=typeof navigator&&navigator.msSaveOrOpenBlob?function(e,t,n){return n||(e=p(e)),navigator.msSaveOrOpenBlob(e,t||"download")}:(y.abort=function(){var e=this;e.readyState=e.DONE,v(e,"abort")},y.readyState=y.INIT=0,y.WRITING=1,y.DONE=2,y.error=y.onwritestart=y.onprogress=y.onwrite=y.onabort=y.onerror=y.onwriteend=null,m)}}("undefined"!=typeof self&&self||"undefined"!=typeof window&&window||this.content);"undefined"!=typeof module&&module.exports?module.exports.saveAs=saveAs:"undefined"!=typeof define&&null!==define&&null!=define.amd&&define([],function(){return saveAs});
\ No newline at end of file
......@@ -2,9 +2,6 @@ var resultItem;
$(document).ready(function() {
resultItem = new ResultItem();
});
$(window).resize(function() {
resultItem.resize();
});
var ResultItem = function(options) {
this.options = $.extend({
......@@ -12,36 +9,34 @@ var ResultItem = function(options) {
maxWidth : 400
}
}, options);
this.resize();
this.modelId = $("#modelId").val();
this.itemId = $("#itemId").val();
this.loadedData = [];
this.iframe = window.location !== window.parent.location;
$('.venobox').venobox();
__translator.addTranslations(["~eu.dariah.de.minfba.search.view.item.no_dataset"]);
__translator.addTranslations([
"~eu.dariah.de.minfba.search.view.item.no_dataset",
"~eu.dariah.de.minfba.search.view.labels.export_dataset_to",
"~eu.dariah.de.minfba.search.view.labels.download",
]);
__translator.getTranslations();
this.init();
};
ResultItem.prototype.resize = function() {
/*var innerWidth = $("#item-images").width();
if (innerWidth > this.options.images.maxWidth) {
innerWidth = this.options.images.maxWidth;
}
var containerWidth = $("#item-images-container").width();
if (containerWidth > this.options.images.maxWidth+150) {
containerWidth = this.options.images.maxWidth+150;
}
ResultItem.prototype.init = function() {
$(".btn-download")
.append("<i class=\"fas fa-lg " + (this.iframe ? "fa-cloud-upload-alt" : "fa-file-download") + "\"></i> ")
.append(__translator.translate("~eu.dariah.de.minfba.search.view.labels." + (this.iframe ? "export_dataset_to" : "download")))
.removeClass("hide");
$("#carousel-item-images").css("width", innerWidth + "px");
$(".item-image").css("height", innerWidth + "px");
$(".item-image").css("width", innerWidth + "px");
$(".collection-image span").css("font-size", Math.floor($(".collection-image").width()*0.07));*/
};
ResultItem.prototype.toggleCollapsed = function(button) {
......@@ -69,15 +64,15 @@ ResultItem.prototype.showErrors = function(button) {
};
ResultItem.prototype.loadOriginalData = function(button) {
this.loadData("original", "#item-detail-dataset-container", "#item-detail-dataset", button);
this.loadData("_content", "#item-detail-dataset-container", "#item-detail-dataset", button);
};
ResultItem.prototype.loadIntegratedData = function(button) {
this.loadData("integrations", "#item-detail-integrated-container", "#item-detail-integrated", button);
this.loadData("_integrations", "#item-detail-integrated-container", "#item-detail-integrated", button);
};
ResultItem.prototype.loadMetadata = function(button) {
this.loadData("metadata", "#item-detail-metadata-container", "#item-detail-metadata", button);
this.loadData("_meta", "#item-detail-metadata-container", "#item-detail-metadata", button);
};
ResultItem.prototype.loadData = function(dataType, containerSelector, contentSelector, button) {
......@@ -93,7 +88,8 @@ ResultItem.prototype.loadData = function(dataType, containerSelector, contentSel
var _this = this;
$(indicator).addClass("fa-spinner fa-spin").removeClass("fa-chevron-circle-down");
$.ajax({
url: __util.getBaseUrl() + "item/" + this.modelId + "/" + this.itemId + "/data?type=" + dataType,
url: __util.getBaseUrl() + "item/" + this.modelId + "/" + this.itemId + "/data",
data: { type: dataType},
type: "GET",
dataType: "html",
success: function(data, textStatus, jqXHR ) {
......@@ -114,18 +110,30 @@ ResultItem.prototype.loadData = function(dataType, containerSelector, contentSel
ResultItem.prototype.downloadData = function(format, model) {
var _this = this;
$.ajax({
url: window.location.href + "download?format=" + format + "&type=" + model,
url: window.location.href + "download",
type: "GET",
data: {
format: format,
type: model
},
dataType: "json",
success: function(data) {
if ( window.location !== window.parent.location ) {
// The page is in an iframe
window.parent.postMessage(data, '*');
if (data==null || data.pojo===undefined || data.pojo===null) {
bootbox.alert(__translator.translate("~eu.dariah.de.minfba.search.view.item.download_error"));
} else {
// The page is not in an iframe
console.log(data);
_this.handleDownloadedData(data);
}
},
error: __util.processServerError
});
};
ResultItem.prototype.handleDownloadedData = function(data) {
if ( window.location !== window.parent.location ) {
window.parent.postMessage(window.btoa(data.pojo), '*');
} else {
// For now only hardcoded xml
var blob = new Blob([data.pojo], {type: "application/xml"});
saveAs(blob, data.statusInfo + ".xml");
}
};
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