Files
PVM/public_html/gest-lib/allegati/js/ModalAllegati.js

751 lines
23 KiB
JavaScript

function ModalAllegati() {
this._activityId = null;
this._codJcom = null;
this._codAlis = null;
this._versione = null;
this._codMart = null;
this._partitaMag = null;
this._filter = {};
this._depth = 0; // livello di attivita sotto commessa / -1 = tutte le attivita
this._$button = null;
this._gestione = null;
this._dataOrd = null;
this._numOrd = null;
this._idContratto = null;
this._onBeforeChanging = null;
this._onBeforeCommand = null;
this._createZipFromFilesData = null;
this._flagOnlyCurrentUser = false; // filtro su commessa, true: solo attivita agganciate a utente corrente
this._waitLoader = false;
this._onUpload = null;
this._onRemove = null;
this._visibleSourceTypes = [];
this._deletableSourceTypes = [];
this._uploadableSourceTypes = [];
this._editableDescriptionSourceTypes = [];
this._tableMode = false;
this.grid = null;
this.costructor();
this.waitLoader(false).draggable().gridSize({xs: 12, md: 8}).backhash().okCancel();
}
ModalAllegati.prototype = new ModalBox(); // ModalAllegati extends ModalBox
ModalAllegati.prototype.onChange = function (f) {
return this.onUpload(f).onRemove(f);
};
ModalAllegati.prototype.sourceTypes = function (v) {
this._visibleSourceTypes = v;
return this;
};
ModalAllegati.prototype.$button = function ($) {
this._$button = $;
return this;
};
ModalAllegati.prototype.allowUpload = function (v) {
this._uploadableSourceTypes = v;
return this;
};
ModalAllegati.prototype.allowDelete = function (v) {
this._deletableSourceTypes = v;
return this;
};
ModalAllegati.prototype.editableDescription = function (v) {
this._editableDescriptionSourceTypes = v;
return this;
};
ModalAllegati.prototype.tableMode = function (v = true) {
this._tableMode = v;
return this;
}
ModalAllegati.prototype.onUpload = function (f) {
this._onUpload = f;
return this;
};
ModalAllegati.prototype.onRemove = function (f) {
this._onRemove = f;
return this;
};
ModalAllegati.prototype.activityId = function (v) {
this._activityId = v;
return this;
};
ModalAllegati.prototype.filter = function (v) {
this._filter = v;
return this;
};
ModalAllegati.prototype.codJcom = function (v) {
this._codJcom = v;
return this;
};
ModalAllegati.prototype.depth = function (v) {
this._depth = v;
return this;
};
ModalAllegati.prototype.onlyCurrentUser = function (v) {
this._flagOnlyCurrentUser = is_undefined(v) || v !== false;
return this;
};
ModalAllegati.prototype.waitLoader = function (v) {
this._waitLoader = is_undefined(v) || v !== false;
return this;
};
ModalAllegati.prototype.codAlis = function (v) {
this._codAlis = v;
return this;
};
ModalAllegati.prototype.versione = function (v) {
this._versione = v;
return this;
};
ModalAllegati.prototype.codMart = function (v) {
this._codMart = v;
return this;
};
ModalAllegati.prototype.partitaMag = function (v) {
this._partitaMag = v;
return this;
};
ModalAllegati.prototype.gestione = function (v) {
this._gestione = v;
return this;
};
ModalAllegati.prototype.dataOrd = function (v) {
this._dataOrd = v;
return this;
};
ModalAllegati.prototype.numOrd = function (v) {
this._numOrd = v;
return this;
};
ModalAllegati.prototype.idContratto = function (v) {
this._idContratto = v;
return this;
};
ModalAllegati.prototype.onBeforeChanging = function(f) {
this._onBeforeChanging = f;
return this;
}
ModalAllegati.prototype.onBeforeCommand = function(f) {
this._onBeforeCommand = f;
return this;
}
ModalAllegati.prototype.createZipFromFilesData = function (v) {
this._createZipFromFilesData = v;
return this;
}
ModalAllegati.prototype._getFilter = function (sourceType = null) {
const self = this;
const filter = {
filter: self._filter,
source_type: sourceType || self._getCurrentSourceType(),
visible_sourceTypes: self._visibleSourceTypes,
uploadable_sourceTypes: self._uploadableSourceTypes,
deletableSourceTypes: self._deletableSourceTypes,
editableDescriptionSourceTypes: self._editableDescriptionSourceTypes,
tableMode: self._tableMode
};
if (!is_null(self._idContratto)) {
filter.id_contratto = self._idContratto;
} else if (!is_null(self._codAlis)) {
filter.cod_alis = self._codAlis;
filter.versione = self._versione;
} else if (!is_null(self._activityId)) {
filter.activity_id = self._activityId;
} else if (!is_null(self._codJcom)) {
filter.cod_jcom = self._codJcom;
filter.depth = self._depth;
filter.onlyCurrentUser = self._flagOnlyCurrentUser;
} else if (!is_null(self._codMart)) {
filter.cod_mart = self._codMart;
filter.partita_mag = self._partitaMag;
} else if (!is_null(self._numOrd)) {
filter.num_ord = self._numOrd;
filter.data_ord = self._dataOrd;
filter.gestione = self._gestione;
} else {
return null;
}
return filter;
};
ModalAllegati.prototype.uploadFile = async function (arr_inputFiles) {
const self = this;
const d = $.Deferred();
if (arr_inputFiles.length > 0) {
try {
// for (const file of arr_inputFiles) {
const ret = await self._upload(arr_inputFiles);
if (ret.returnId === 1) {
self._refresh_btDownload();
if (is_function(self._onUpload)) {
self._onUpload(self._countRows());
}
}
// }
} finally {
self._onRefreshSourceTypeTab();
d.resolve();
}
} else {
d.resolve();
}
};
ModalAllegati.prototype.open = async function () {
const self = this;
const filter = self._getFilter();
const ret = await new Ajax()
.get("popup-main")
.module("allegati")
.data(filter)
.$button(self._$button)
.waitToast(self._waitLoader)
.noticeAsModal()
.execute();
let title = "Allegati";
if (!is_null(self._activityId)) {
title += " dell'attivita";
} else if (!is_null(self._codJcom)) {
title += " della commessa " + self._codJcom;
} else if (!is_null(self._codAlis)) {
title += " del listino " + self._codAlis;
} else if (!is_null(self._numOrd) && !is_null(self._dataOrd)) {
title += " dell'ordine n. " + self._numOrd + " del " + moment.unix(self._dataOrd).format("DD/MM/YYYY");
}
await self
.onBeforeShow(function ($div) {
$div.addClass("ModalAllegati");
const $table = self._getShowedTable();
self.get$btOK().enabled($table.find("tbody > tr").length > 0); // downloadAll
if (self._tableMode) {
self.grid = $div.find("#m_tbAllegati").kendoGrid({
dataSource: {
group: [
{
field: "source_type"
}
]
},
pageable: false,
scrollable: false,
selectable: {
mode: "multiple, row",
dragToSelect: false
},
contextMenu: {
body: [
{
name: "CreateZipFromFiles",
text: "Crea zip",
icon: "- fas fa-archive",
command: "CreateZipFromFiles"
}
],
open: (e) => {
const grid = $(e.target).parents(".k-grid-table").data("kendoGrid");
const $target = $(e.target);
if (!grid) {
e.preventDefault();
return;
}
if ($target.parents(".k-grouping-row").length) {
e.preventDefault();
return;
}
const selectedRows = self.grid.select();
const targetTr = $target.parents("tr").get(0);
if (!selectedRows.toArray().some(selectedRow => selectedRow === targetTr)) {
self.grid.clearSelection();
self.grid.select(targetTr);
}
},
select: async (e) => {
if (!e.item) {
return;
}
const $target = $(e.target);
const grid = $target.parents(".k-grid-table").data("kendoGrid");
if (!grid?.length) {
e.preventDefault();
}
const allegati = grid.select().toArray().map(item => grid.dataItem(item));
if (!allegati.length) {
e.preventDefault();
return;
}
try {
const command = $(e.item).data("command");
let ret = null;
if (command && self._onBeforeCommand) {
await self._onBeforeCommand(command, allegati, grid);
}
switch (command) {
case "CreateZipFromFiles": {
ret = await self.createZipFromFiles({
...self._createZipFromFilesData,
files: allegati.map(allegato => ({
sourceType: allegato.source_type,
...allegato.data.key
}))
});
break;
}
}
if (ret?.returnId === 1) {
self.close();
await self.open();
}
} catch (e) {
console.error(e);
}
}
},
changing: (e) => {
const allegato = e.sender.dataItem(e.target);
if (!allegato) {
e.preventDefault();
return;
}
self._onBeforeChanging && self._onBeforeChanging(e, allegato);
},
dataBound: (e) => {
e.sender.tbody.find("tr.k-master-row")
.each((i, row) => {
const $row = $(row);
const dataItem = e.sender.dataItem(row);
if (dataItem.data) {
if (dataItem.eventsSet) {
return;
}
dataItem.data = _ojbc.B64JSON_parse(dataItem.data);
dataItem.eventsSet = true;
self._setRowEvents($row);
} else {
$row.hide();
}
});
},
}).data("kendoGrid");
const columnSourceType = self.grid.columns.find(col => col.field === "source_type");
if (columnSourceType) {
columnSourceType.hidden = true;
columnSourceType.groupHeaderTemplate = (data) => `${data.value}${self._uploadableSourceTypes.includes(data.value) ?
` <button type="button" class="btnUploadFile ml-1"></button>` : ""}`;
}
self.grid.columns.find(col => col.field === "data").hidden = true;
self.grid.columns.find(col => col.field === "Icona").attributes = {
class: "!k-text-center"
};
self.grid.refresh();
}
self.$div.find(".btnUploadFile").kendoButton({
icon: "- far fa-plus !k-text-success m-0",
themeColor: "success",
fillMode: "flat",
click: (e) => {
const $tr = e.sender.element.closest("tr").next();
const dataItem = self.grid.dataItem($tr);
const modalBox = new ModalBox();
let dropzone = null;
modalBox
.content(`<div id="dropzone"></div>`)
.title("Caricamento allegati")
.gridSize(this._gridSize)
.onBeforeShow(($div) => {
dropzone = $div.find("#dropzone").dropzone({
onDrop: async (e, files) => {
const ret = await self._upload(files, dataItem.source_type);
if (ret.returnId === 1) {
modalBox.close();
self.close();
await this.open();
}
return ret.returnId === 1;
},
});
})
.onClose(() => {
dropzone?.destroy();
})
.show();
}
});
if (!self._tableMode) {
$table.find("tbody > tr").each(function () {
self._setRowEvents($(this));
});
$div.find("ul.nav-tabs > li > a[data-tab]").on("tabChange", function () {
self._onRefreshSourceTypeTab();
});
self._onRefreshSourceTypeTab();
self.initDropzone();
}
})
.onClose(function () {
$(document).off("paste");
})
.btOK({
text: "Scarica tutti i file",
style: "success",
icon: "download",
size: {sm: 4, md: 4},
onClick: function (e, $btOK) {
const ajax = new Ajax();
ajax.get("get_all")
.module("allegati")
.data(filter)
.$button($btOK)
.noticeAsToast()
.onSuccess({downloadFile: true})
.execute();
}
})
.okClose()
.content(ret.returnString)
.title(title)
.show();
};
ModalAllegati.prototype.initDropzone = function () {
const self = this;
const $droparea = $(".droparea");
if (self._uploadableSourceTypes.length === 0) {
$droparea.addClass("hidden");
return;
}
$droparea.on("mousedown dragenter dragover", function (e) {
e.preventDefault();
$(this).addClass("green-border");
}).on("mouseup mouseleave dragleave drop", function (e) {
e.preventDefault();
$(this).removeClass("green-border");
}).on("drop", function (e) {
const originalEvent = e.originalEvent;
const dt = originalEvent.dataTransfer;
const files = dt.files;
self.uploadFile(files);
}).on("click", function (e) {
$("#m_fileAllegato").trigger("click");
});
$("#m_fileAllegato").on("change", function () {
const $file = $(this);
const arr_inputFiles = $file[0].files;
self.uploadFile(arr_inputFiles);
});
$(document).on("paste", function (e) {
const $input = $("#m_fileAllegato");
$input.get(0).files = e.originalEvent.clipboardData.files;
$input.trigger("change");
});
};
ModalAllegati.prototype._onRefreshSourceTypeTab = function () {
const self = this;
const sourceType = self._getCurrentSourceType();
const $table = self._getShowedTable();
$table.find("tr[data-source_type]").hide();
$table.find("tr[data-source_type='" + sourceType + "']").show();
if (self._uploadableSourceTypes?.length) {
$(".droparea").toggleClass("hidden", !self._uploadableSourceTypes.includes(sourceType));
}
return self;
};
ModalAllegati.prototype._getCurrentSourceType = function () {
var $active = !is_null(this.$div) ? this.$div.find("ul.nav-tabs > li.active > a") : $();
return $active.exists() ? $active.attr("data-tab") : null;
};
ModalAllegati.prototype._upload = async function (files, sourceType = null) {
const self = this;
const filter = self._getFilter(sourceType);
const formData = new FormData();
Array.from(files).forEach(file => formData.append("files[]", file));
const ret = await new Ajax()
.post("uploadFromModal")
.module("allegati")
.data(filter)
.formData(formData)
.$toDisable(self)
.noticeAsModal()
.execute();
if (!self._tableMode) {
const $tbody = self._getShowedTable().children("tbody");
const $tr = $(ret.returnString);
$tbody.append($tr);
$tr.highlightRow();
$tr.each((_, row) => self._setRowEvents($(row)));
}
return ret;
};
ModalAllegati.prototype._getShowedTable = function () {
return this.$div.find("#m_tbAllegati");
};
ModalAllegati.prototype._countRows = function () {
return this._getShowedTable().find("> tbody > tr").length;
};
ModalAllegati.prototype._refresh_btDownload = function () {
var self = this;
self.get$btOK().enabled(self._countRows() > 0);
return self;
};
ModalAllegati.prototype._setRowEvents = function ($tr) {
const self = this;
$tr.on("dblclick", function () {
let source_type = $tr.getDataAttr("source_type");
let key = $tr.getDataAttr("key");
let file_name = $tr.getDataAttr("file_name");
if (self._tableMode) {
const dataItem = self.grid.dataItem($tr);
source_type = dataItem.source_type;
key = _ojbc.B64JSON_parse(dataItem.key);
file_name = dataItem.data.file_name;
}
_allegati.download({source_type, key, file_name, $tr});
});
$tr.find(".btDownloadAllegato").on("click", function (e) {
e.preventDefault();
$(this).closest("tr").trigger("dblclick");
return false;
});
$tr.find(".btRemoveAllegato").on("click", function (e) {
e.stopPropagation();
let source_type = $tr.getDataAttr("source_type");
let key = $tr.getDataAttr("key");
let file_name = $tr.getDataAttr("file_name");
if (self._tableMode) {
const dataItem = self.grid.dataItem($tr);
source_type = dataItem.source_type;
key = _ojbc.B64JSON_parse(dataItem.key);
file_name = dataItem.data.file_name;
}
_allegati.remove({source_type, key, file_name, $tr}).then(function () {
self._refresh_btDownload();
if (is_function(self._onRemove)) {
self._onRemove(self._countRows());
}
});
});
$tr.find(".image-thumbnail").on("click", function (e) {
e.preventDefault();
e.stopPropagation();
var $tr = $(this).closest("tr");
var $img = $tr.find("img.image-thumbnail");
var thisUrl = $img.getDataAttr("url");
var carousel = new Carousel();
var startFrom = 0;
$tr.closest("tbody").children("tr:has(img.image-thumbnail)").each(function (i) {
var $tr = $(this);
var $img = $tr.find("img.image-thumbnail");
var url = $img.getDataAttr("url");
carousel.append(url);
if (thisUrl == url) {
startFrom = i + 1;
}
});
carousel.startFrom(startFrom).title("Galleria immagini").show();
return false;
});
$tr.find("input.txt_descrizione").on("dblclick enterKey", function (e) {
e.stopPropagation();
var $this = $(this);
if (e.type == "dblclick") {
if ($this.prop("readonly")) {
self.unlock_txtNote($this);
} else {
self._updateDescrizione($this);
}
} else { // enterKey
self._updateDescrizione($this);
return false;
}
});
$tr.find(".btEditDescrizione").on("click", function (e) {
e.stopPropagation();
self.unlock_txtNote($(this));
});
$tr.find(".btUpdateDescrizione").on("click", function (e) {
e.stopPropagation();
self._updateDescrizione($(this));
});
$tr.find(".bt_sendAllegato").on("click", function (e) {
e.stopPropagation();
var $tr = $(this).closest("tr");
_allegati.getCachePath($tr, function (ret) {
_messenger.appendFile(ret.returnString);
});
});
return self;
};
ModalAllegati.prototype.unlock_txtNote = function ($input) {
var $group = $input.closest(".input-group");
$group.find(".btEditDescrizione").hide();
$group.find(".btUpdateDescrizione").show();
$group.find(".txt_descrizione").readonly(false).select();
};
ModalAllegati.prototype._updateDescrizione = function ($input) {
var self = this;
var $tr = $input.closest("tr");
var sourceType = $tr.getDataAttr("source_type");
var key = $tr.getDataAttr("key");
if (sourceType === _allegati.sourceTypes.ATTIVITA) {
var activityId = key.activity_id;
var fileName = key.file_name;
var $group = $input.closest(".input-group");
var $text = $group.find(".txt_descrizione");
var descrizione = $text.getValue();
var ajax = new Ajax();
ajax.post("updateDescrizione")
.module("allegati")
.data({activity_id: activityId, file_name: fileName, descrizione: descrizione})
.$toDisable($group)
.$button($group.find(".btUpdateDescrizione"))
.noticeAsToast()
.onSuccess(function (ret) {
$text.readonly().val(ret.returnString);
$group.find(".btEditDescrizione").show();
$group.find(".btUpdateDescrizione").hide();
})
.execute();
}
};
ModalAllegati.prototype.getFileList = async (data) => {
const ret = await new Ajax()
.get("get_fileList")
.module("allegati")
.data(data)
.noticeAsModal()
.execute();
return ret?.returnData;
}
ModalAllegati.prototype.createZipFromFiles = async (data) => {
return await new Ajax()
.post("createZipFromFiles")
.module("allegati")
.data(data)
.waitModal()
.noticeAsModal()
.execute();
}