--- /dev/null
+(function() {
+ "use strict";
+
+ angular
+ .module("nero.app")
+ .directive("mediaDisplayDirective", mediaDisplayDirective);
+
+ function mediaDisplayDirective() {
+ return {
+ restrict: "E",
+ scope: {
+ attachment: "=",
+ idProperties: "=?"
+ },
+ templateUrl: Global.Theme.getURLPortal() + Liferay.ThemeDisplay.getPathJavaScript() + "/angularjs/media-display/media-display.html?t="+Liferay.extTimestamp,
+ controller: MediaDisplayController,
+ controllerAs: "MediaDisplayCtrl",
+ bindToController: true
+ };
+ }
+
+ MediaDisplayController.$inject = ["attachmentService", "toastr", "$interval", "$scope"];
+
+ function MediaDisplayController(attachmentService, toastr, $interval, $scope) {
+ var vm = this;
+ var attachment = vm.attachment;
+
+ vm.max = 100;
+ vm.conversionProgress = 0;
+ vm.isConversionInProgress = false;
+ vm.conversion;
+ vm.isContentReady = true;
+ vm.isEpub = false;
+ vm.isDocument = false;
+ vm.isImage = false;
+ vm.isAudio = false;
+ vm.isVideo = false;
+ vm.isHTML = false;
+ vm.isOther = false;
+ vm.url = "";
+ vm.description = undefined;
+ vm.id = "area" + (Math.random() * 100);
+ vm.width = "auto";
+ vm.height = "auto";
+
+ initViewer();
+
+ $scope.$on("$destroy", function() {
+ stopConversionProgress();
+ });
+
+ /**
+ Get attachment type and return its extension
+ */
+ function getAttachmentType() {
+ return attachment.fileName.substr(attachment.fileName.lastIndexOf('.') + 1);
+ }
+
+ /**
+ Initialise viewer url, width and height depending on the attachment type
+ */
+ function initViewer() {
+ var fileExtension = getAttachmentType().toLowerCase();
+
+ if (fileExtension == "epub") {
+ vm.isEpub = true;
+ vm.width = 600;
+ vm.height = 600;
+
+ getFile();
+ }
+ else if (EntSavoie.documentExtensionArray.indexOf(fileExtension) != -1) {
+ vm.url = Global.Theme.getURLPortal() + "/PDFjs/web/viewer.html?file=" + encodeURIComponent(attachment.fileLink);
+ vm.width = 600;
+ vm.height = 600;
+
+ if (fileExtension != "pdf") {
+ vm.url += "%26targetExtension%3Dpdf";
+ }
+ vm.isDocument = true;
+ }
+ else if (EntSavoie.imageExtensionArray.indexOf(fileExtension) != -1) {
+ vm.url = Global.Theme.getURLPortal() + decodeURIComponent(attachment.fileLink);
+ vm.isImage = true;
+ }
+ else if (EntSavoie.audioExtensionArray.indexOf(fileExtension) != -1) {
+ vm.url = Global.Theme.getURLPortal() + decodeURIComponent(attachment.fileLink);
+
+ if (fileExtension != "mp3") {
+ vm.url += "&targetExtension=mp3";
+ vm.isContentReady = false;
+ initAttachmentConversionProgress();
+ }
+ vm.isAudio = true;
+ }
+ else if (EntSavoie.videoExtensionArray.indexOf(fileExtension) != -1) {
+ vm.url = Global.Theme.getURLPortal() + decodeURIComponent(attachment.fileLink);
+
+ if (fileExtension != "mp4") {
+ vm.url += "&targetExtension=mp4";
+ vm.isContentReady = false;
+ initAttachmentConversionProgress();
+ }
+ vm.isVideo = true;
+
+ }
+ else if (fileExtension === "html") {
+ vm.url = Global.Theme.getURLPortal() + decodeURIComponent(attachment.fileLink);
+ vm.isHTML = true;
+ getFile();
+ }
+ else if (EntSavoie.viewableExtensionArray.indexOf(fileExtension) != -1) {
+ vm.width = 600;
+ vm.height = 600;
+ vm.url = Global.Theme.getURLPortal() + decodeURIComponent(attachment.fileLink);
+ vm.isOther = true;
+ }
+ }
+
+ /*
+ Get the attachment key and launch progress bar
+ */
+ function initAttachmentConversionProgress() {
+ attachmentService.getAttachmentConversionKey(attachment, vm.idProperties)
+ .then(function(data) {
+ attachment.idSecure = data.idSecure;
+ getConversionProgress();
+ })
+ ["catch"](function(message) {
+ toastr.error(message);
+ });
+ }
+
+ /*
+ Get conversion state to refresh the progress bar
+ */
+ function getConversionProgress() {
+ vm.isConversionInProgress = true;
+
+ vm.conversion = $interval(function() {
+ attachmentService.getConversionProgress(attachment.idSecure)
+ .then(function(data) {
+ if (!isNaN(data.state)) {
+ vm.conversionProgress = data.state;
+ }
+ else if (data.state == "done") {
+ toastr.success(data.message);
+ vm.isContentReady = true;
+ stopConversionProgress();
+ }
+ })
+ ["catch"](function(message) {
+ toastr.error(message);
+ stopConversionProgress();
+ });
+ }, 1000);
+ }
+
+ function getFile() {
+ var reader = new FileReader();
+ reader.addEventListener("load", function() {
+ if (vm.isEpub) {
+ vm.epubViewer = new ePub(reader.result, { width: "400px", height: "500px", spreads: true });
+ vm.epubViewer.renderTo("area");
+ }
+ else {
+ vm.description = reader.result;
+ //Refresh interface
+ $scope.$apply();
+ }
+ }, false);
+
+ var blob = null;
+ var xhr = new XMLHttpRequest();
+ xhr.open("GET", decodeURIComponent(attachment.fileLink));
+ xhr.responseType = "blob";
+ xhr.onload = function() {
+ blob = xhr.response
+ if (vm.isEpub) {
+ reader.readAsArrayBuffer(blob);
+ }
+ else {
+ reader.readAsText(blob);
+ }
+ };
+ xhr.send();
+ }
+
+ /*
+ Stop interval function getting conversion progress
+ Kill conversion process if not finished properly
+ */
+ function stopConversionProgress() {
+ if (attachment.idSecure != undefined && vm.isContentReady == false) {
+ attachmentService.killConversionProcess(attachment.idSecure);
+ }
+
+ if (angular.isDefined(vm.conversion)) {
+ $interval.cancel(vm.conversion);
+ vm.conversion= undefined;
+ }
+ vm.isConversionInProgress = false;
+ }
+
+ }
+})();