/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
Espo.define('views/fields/attachment-multiple', 'views/fields/base', function (Dep) {
return Dep.extend({
type: 'attachmentMultiple',
listTemplate: 'fields/attachments-multiple/list',
detailTemplate: 'fields/attachments-multiple/detail',
editTemplate: 'fields/attachments-multiple/edit',
searchTemplate: 'fields/link-multiple/search',
previewSize: 'medium',
nameHashName: null,
idsName: null,
nameHash: null,
foreignScope: null,
showPreviews: true,
previewTypeList: [
'image/jpeg',
'image/png',
'image/gif',
],
validations: ['ready', 'required'],
searchTypeList: ['isNotEmpty', 'isEmpty'],
events: {
'click a.remove-attachment': function (e) {
var $div = $(e.currentTarget).parent();
var id = $div.attr('data-id');
if (id) {
this.deleteAttachment(id);
}
$div.parent().remove();
},
'change input.file': function (e) {
var $file = $(e.currentTarget);
var files = e.currentTarget.files;
this.uploadFiles(files);
$file.replaceWith($file.clone(true));
},
'click a.action[data-action="insertFromSource"]': function (e) {
var name = $(e.currentTarget).data('name');
this.insertFromSource(name);
},
'click a[data-action="showImagePreview"]': function (e) {
e.preventDefault();
var id = $(e.currentTarget).data('id');
var attachmentIdList = this.model.get(this.idsName) || [];
var typeHash = this.model.get(this.typeHashName) || {};
var imageIdListRight = [];
var imageIdListLeft = [];
imageIdListLeft.push(id);
var met = false;
attachmentIdList.forEach(function (cId) {
if (cId === id) {
met = true;
return;
}
if (!this.isTypeIsImage(typeHash[cId])) {
return;
}
if (met) {
imageIdListLeft.push(cId);
} else {
imageIdListRight.push(cId);
}
}, this);
var imageIdList = imageIdListLeft.concat(imageIdListRight);
var imageList = [];
imageIdList.forEach(function (cId) {
imageList.push({
id: cId,
name: this.nameHash[cId]
});
}, this);
this.createView('preview', 'views/modals/image-preview', {
id: id,
model: this.model,
name: this.nameHash[id],
imageList: imageList
}, function (view) {
view.render();
});
},
},
data: function () {
var ids = this.model.get(this.idsName);
var data = _.extend({
idValues: this.model.get(this.idsName),
idValuesString: ids ? ids.join(',') : '',
nameHash: this.model.get(this.nameHashName),
foreignScope: this.foreignScope,
valueIsSet: this.model.has(this.idsName)
}, Dep.prototype.data.call(this));
if (this.mode == 'edit') {
data.fileSystem = ~this.sourceList.indexOf('FileSystem');
data.sourceList = this.sourceList;
}
return data;
},
setup: function () {
this.nameHashName = this.name + 'Names';
this.typeHashName = this.name + 'Types';
this.idsName = this.name + 'Ids';
this.foreignScope = 'Attachment';
this.previewSize = this.options.previewSize || this.params.previewSize || this.previewSize;
var self = this;
this.nameHash = _.clone(this.model.get(this.nameHashName)) || {};
if ('showPreviews' in this.params) {
this.showPreviews = this.params.showPreviews;
}
var sourceDefs = this.getMetadata().get(['clientDefs', 'Attachment', 'sourceDefs']) || {};
this.sourceList = Espo.Utils.clone(this.params.sourceList || []).filter(function (item) {
if (!(item in sourceDefs)) return true;
var defs = sourceDefs[item];
if (defs.configCheck) {
var configCheck = defs.configCheck;
if (configCheck) {
var arr = configCheck.split('.');
if (this.getConfig().getByPath(arr)) {
return true;
}
}
}
}, this);
this.listenTo(this.model, 'change:' + this.nameHashName, function () {
this.nameHash = _.clone(this.model.get(this.nameHashName)) || {};
}.bind(this));
this.once('remove', function () {
if (this.resizeIsBeingListened) {
$(window).off('resize.' + this.cid);
}
}.bind(this));
},
setupSearch: function () {
this.events = _.extend({
'change select.search-type': function (e) {
var type = $(e.currentTarget).val();
this.handleSearchType(type);
},
}, this.events || {});
},
empty: function () {
this.clearIds();
this.$attachments.empty();
},
handleResize: function () {
var width = this.$el.width();
this.$el.find('img.image-preview').css('maxWidth', width + 'px');
},
deleteAttachment: function (id) {
this.removeId(id);
if (this.model.isNew()) {
this.getModelFactory().create('Attachment', function (attachment) {
attachment.id = id;
attachment.destroy();
});
}
},
getImageUrl: function (id, size) {
var url = this.getBasePath() + '?entryPoint=image&id=' + id;
if (size) {
url += '&size=' + size;
}
if (this.getUser().get('portalId')) {
url += '&portalId=' + this.getUser().get('portalId');
}
return url;
},
getDownloadUrl: function (id) {
var url = this.getBasePath() + '?entryPoint=download&id=' + id;
if (this.getUser().get('portalId')) {
url += '&portalId=' + this.getUser().get('portalId');
}
return url;
},
removeId: function (id) {
var arr = _.clone(this.model.get(this.idsName) || []);
var i = arr.indexOf(id);
arr.splice(i, 1);
this.model.set(this.idsName, arr);
var nameHash = _.clone(this.model.get(this.nameHashName) || {});
delete nameHash[id];
this.model.set(this.nameHashName, nameHash);
var typeHash = _.clone(this.model.get(this.typeHashName) || {});
delete typeHash[id];
this.model.set(this.typeHashName, typeHash);
},
clearIds: function (silent) {
var silent = silent || false;
this.model.set(this.idsName, [], {silent: silent});
this.model.set(this.nameHashName, {}, {silent: silent});
this.model.set(this.typeHashName, {}, {silent: silent})
},
pushAttachment: function (attachment, link) {
var arr = _.clone(this.model.get(this.idsName) || []);
arr.push(attachment.id);
this.model.set(this.idsName, arr);
var typeHash = _.clone(this.model.get(this.typeHashName) || {});
typeHash[attachment.id] = attachment.get('type');
this.model.set(this.typeHashName, typeHash);
var nameHash = _.clone(this.model.get(this.nameHashName) || {});
nameHash[attachment.id] = attachment.get('name');
this.model.set(this.nameHashName, nameHash);
},
getEditPreview: function (name, type, id) {
name = Handlebars.Utils.escapeExpression(name);
var preview = name;
switch (type) {
case 'image/png':
case 'image/jpeg':
case 'image/gif':
preview = '';
}
return preview;
},
addAttachmentBox: function (name, type, id, link) {
var $attachments = this.$attachments;
var removeLink = '';
var preview = name;
if (this.showPreviews && id) {
preview = this.getEditPreview(name, type, id);
}
if (link && preview === name) {
preview = '' + preview + '';
}
var $att = $('