Advertisement
Guest User

fileupload.js

a guest
Jan 22nd, 2020
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function uploadFile(elem) {
  2.     // Pasta na qual será inserido o arquivo
  3.     var folder = parseInt($("#pastaAnexos").val());
  4.  
  5.     // Nome do campo (pai x filho) que armazena o ID do documento
  6.     var documentIdField = "#anexoId";
  7.     var btnDelete = ".btnDelete";
  8.  
  9.     // Exibir imagem de carregando
  10.     var loading = FLUIGC.loading(window);
  11.  
  12.     // ID do input:file
  13.     var rowId = elem.id.split('___')[1];
  14.  
  15.     $('.file').fileupload({
  16.         dataType: 'json',
  17.         start: function (e, data) {
  18.             loading.show();
  19.         },
  20.         done: function (e, data) {
  21.             var newDocumentId = saveDocument(data.result.files, folder, rowId)[0];
  22.             var newIdfield = documentIdField + "___" + rowId;
  23.  
  24.             $(newIdfield).val(newDocumentId);
  25.             $(newIdfield).parent().find(btnDelete).show();
  26.         },
  27.         fail: function (e, data) {
  28.             console.log('File upload error:');
  29.             console.error(e);
  30.             console.error(data);
  31.             console.log(data.messages);
  32.         },
  33.         stop: function (e, data) {
  34.             loading.hide();
  35.         }
  36.     });
  37. };
  38.  
  39. function saveDocument(documents, folder, rowId) {
  40.     documentIDs = [];
  41.  
  42.     for (let i = 0; i < documents.length; i++) {
  43.         var document = documents[i];
  44.         var data = {
  45.             'description': document.name,
  46.             'parentId': folder,
  47.             'attachments': [{
  48.                 'fileName': document.name
  49.             }]
  50.         };
  51.  
  52.         parent.WCMAPI.Create({
  53.             async: false,
  54.             type: 'POST',
  55.             contentType: 'application/json',
  56.             url: '/api/public/ecm/document/createDocument',
  57.             data: JSON.stringify(data),
  58.             error: function (err) {
  59.                 FLUIGC.toast({
  60.                     title: 'Falha ao Enviar',
  61.                     message: 'Não foi possível enviar o arquivo ' + document.name,
  62.                     type: 'danger'
  63.                 });
  64.  
  65.                 console.error(err);
  66.             },
  67.             success: function (response) {
  68.                 FLUIGC.toast({
  69.                     title: 'Sucesso:',
  70.                     message: 'Documento ' + document.name + ' publicado',
  71.                     type: 'success'
  72.                 });
  73.  
  74.                 console.log("File uploaded:");
  75.                 console.log(response);
  76.  
  77.                 var newDocumentId = response.content.id;
  78.  
  79.                 documentIDs.push(newDocumentId);
  80.             },
  81.         });
  82.     }
  83.  
  84.     return documentIDs;
  85. };
  86.  
  87. function openDocument(elem)  {
  88.     var documentId = $(elem).parent().find(".anexo-id").val();
  89.  
  90.     if (!documentId) {
  91.         FLUIGC.toast({
  92.             title: 'Ops...',
  93.             message: 'O anexo ainda não foi carregado.',
  94.             type: 'warning'
  95.         });
  96.     } else {
  97.         fluigViewDocument(documentId);
  98.     }
  99. }
  100.  
  101. function clickDeleteDocument(elem, force) {
  102.     var documentId = $(elem).parent().find(".anexo-id").val();
  103.  
  104.     if (!documentId) {
  105.         FLUIGC.toast({
  106.             title: 'Ops...',
  107.             message: 'O anexo ainda não foi carregado.',
  108.             type: 'warning'
  109.         });
  110.     } else if (force) {
  111.         deleteAttachment(elem, documentId);
  112.     } else {
  113.         FLUIGC.message.confirm({
  114.             message: 'Quer mesmo remover este anexo?',
  115.             title: 'Remover anexo',
  116.             labelYes: 'Remover',
  117.             labelNo: 'Cancelar'
  118.         }, function(result) {
  119.             if (!result) return;
  120.  
  121.             deleteAttachment(elem, documentId);
  122.         });
  123.     }
  124. }
  125.  
  126. function deleteAttachment(btnDelete, documentId) {
  127.     deleteDocument(documentId).success(data => {
  128.         if (data.content && data.content == "OK") {
  129.             $(btnDelete).hide();
  130.             $(btnDelete).parent().find(".anexo-id").val("");
  131.  
  132.             FLUIGC.toast({
  133.                 title: 'Sucesso: ',
  134.                 message: 'O arquivo de ID <b>' + documentId + '</b> foi removido.',
  135.                 type: 'success'
  136.             });
  137.         } else {
  138.             console.error("Erro ao remover arquivo:");
  139.             console.log(data);
  140.  
  141.             FLUIGC.toast({
  142.                 title: 'Erro: ',
  143.                 message: 'O arquivo de ID <b>' + documentId + '</b> não pôde ser removido.',
  144.                 type: 'danger'
  145.             });
  146.         }
  147.     });
  148. }
  149.  
  150. function fluigViewDocument(documentId) {
  151.     var parentOBJ;
  152.  
  153.     if (window.opener) {
  154.         parentOBJ = window.opener.parent;
  155.     } else {
  156.         parentOBJ = parent;
  157.     }
  158.  
  159.     var data = {
  160.         url: "/ecm_documentview/documentView.ftl",
  161.         maximized: true,
  162.         title: "Visualizador de Documentos",
  163.         callBack: function () {
  164.             parentOBJ.ECM.documentView.getDocument(parseInt(documentId), 1000);
  165.         },
  166.         customButtons: []
  167.     };
  168.  
  169.     parentOBJ.ECM.documentView.panel = parentOBJ.WCMC.panel(data);
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement