158 lines
7.6 KiB
JavaScript
158 lines
7.6 KiB
JavaScript
"use strict";
|
|
(function () {
|
|
'use strict';
|
|
angular.module('myitsmApp')
|
|
.directive('attachmentsPreviewer', ['attachmentService', '$timeout',
|
|
function (attachmentService, $timeout) {
|
|
// additional class to render files
|
|
function FilePreview(container) {
|
|
this.container = container;
|
|
var self = this;
|
|
var renderPDF = function (fileUrl) {
|
|
if (!window.navigator.msSaveOrOpenBlob) {
|
|
var object = document.createElement('object');
|
|
object.setAttribute('data', fileUrl);
|
|
self.container.appendChild(object);
|
|
}
|
|
};
|
|
var renderImg = function (fileUrl) {
|
|
var img = document.createElement('img');
|
|
img.setAttribute('src', fileUrl);
|
|
img.setAttribute('class', 'img-responsive center-block');
|
|
self.container.appendChild(img);
|
|
};
|
|
this.render = function (fileUrl, fileType) {
|
|
if (!fileUrl || !fileType || !this.container) {
|
|
return false;
|
|
}
|
|
var renderMap = {
|
|
pdf: renderPDF,
|
|
jpg: renderImg,
|
|
png: renderImg,
|
|
gif: renderImg
|
|
};
|
|
var func = renderMap[fileType.toLowerCase()];
|
|
if (!func) {
|
|
return false;
|
|
}
|
|
func(fileUrl);
|
|
};
|
|
this.clearContainer = function () {
|
|
this.container.innerHTML = '';
|
|
};
|
|
}
|
|
// main directive object
|
|
return {
|
|
restrict: 'E',
|
|
replace: true,
|
|
scope: {
|
|
plan: '='
|
|
},
|
|
templateUrl: 'views/common/attachment-preview-popup.html',
|
|
link: function ($scope) {
|
|
var supportedFileTypes = ['pdf', 'jpg', 'png', 'gif'];
|
|
// FIXME: do not use jQuery
|
|
var container = $('.attachments-previewer__container_body-holder')[0];
|
|
var filePreview = new FilePreview(container);
|
|
var showSaveFileAs = function (fileObj) {
|
|
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
|
|
// IE Specific
|
|
var blobObject = new Blob([fileObj.fileBlob]);
|
|
window.navigator.msSaveOrOpenBlob(blobObject, fileObj.name);
|
|
}
|
|
else {
|
|
$timeout(function () {
|
|
var a = document.createElement('a');
|
|
document.body.appendChild(a);
|
|
a.style.display = 'none';
|
|
a.href = fileObj.url;
|
|
a.download = fileObj.name;
|
|
a.click();
|
|
a.remove();
|
|
});
|
|
}
|
|
};
|
|
$scope.plan = $scope.plan || {};
|
|
$scope.workNote = $scope.plan.workNote || {};
|
|
$scope.attachments = $scope.workNote.attachmentCount ? $scope.plan.attachmentInfos : [];
|
|
$scope.currentFile = $scope.workNote.attachmentCount ? $scope.plan.attachmentInfos[0] : {};
|
|
$scope.loading = false;
|
|
$scope.fileTypeNotSupported = false;
|
|
$scope.currentIndex = 0;
|
|
$scope.getAttachmentShowFile = function (type) {
|
|
attachmentService.getAttachmentFile(type, $scope.currentFile, 1)
|
|
.then(function (fileBlob) {
|
|
$scope.loading = false;
|
|
$scope.currentFile.url = URL.createObjectURL(fileBlob.data);
|
|
// blobFile is used only while serving file for a download in IE
|
|
$scope.currentFile.fileBlob = fileBlob.data;
|
|
filePreview.render($scope.currentFile.url, $scope.currentFile.contentType);
|
|
});
|
|
};
|
|
$scope.getAttachmentDownloadFile = function (type) {
|
|
attachmentService.getAttachmentFile(type, $scope.currentFile, 1)
|
|
.then(function (fileBlob) {
|
|
$scope.loading = false;
|
|
$scope.currentFile.url = URL.createObjectURL(fileBlob.data);
|
|
// blobFile is used only while serving file for a download in IE
|
|
$scope.currentFile.fileBlob = fileBlob.data;
|
|
showSaveFileAs($scope.currentFile);
|
|
});
|
|
};
|
|
$scope.showFile = function (index) {
|
|
if ($scope.loading) {
|
|
return false;
|
|
}
|
|
$scope.currentIndex = index;
|
|
$scope.currentFile = $scope.attachments[$scope.currentIndex];
|
|
filePreview.clearContainer();
|
|
if (supportedFileTypes.indexOf($scope.currentFile.contentType.toLowerCase()) === -1) {
|
|
$scope.fileTypeNotSupported = true;
|
|
return false;
|
|
}
|
|
$scope.fileTypeNotSupported = false;
|
|
// do not send request to a server if we have URL already
|
|
if ($scope.currentFile.url) {
|
|
filePreview.render($scope.currentFile.url, $scope.currentFile.contentType);
|
|
return true;
|
|
}
|
|
// no URL - get it from a server
|
|
$scope.loading = true;
|
|
//check for release vs change for reuse in release case.
|
|
if (location.hash.indexOf('release') !== -1) {
|
|
$scope.getAttachmentShowFile('release');
|
|
}
|
|
else {
|
|
$scope.getAttachmentShowFile('change');
|
|
}
|
|
};
|
|
$scope.downloadFile = function () {
|
|
if ($scope.loading) {
|
|
return false;
|
|
}
|
|
if ($scope.currentFile.url) {
|
|
showSaveFileAs($scope.currentFile);
|
|
return true;
|
|
}
|
|
$scope.loading = true;
|
|
//check for release vs change for reuse in release case.
|
|
if (location.hash.indexOf('release') !== -1) {
|
|
$scope.getAttachmentDownloadFile('release');
|
|
}
|
|
else {
|
|
$scope.getAttachmentDownloadFile('change');
|
|
}
|
|
};
|
|
$scope.close = function () {
|
|
$scope.$parent.$dismiss();
|
|
};
|
|
if ($scope.workNote.attachmentCount) {
|
|
// show the first attachment on load
|
|
$scope.showFile(0);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
]);
|
|
})();
|