1 namespace('kivi.File', function(ns) {
 
   2   ns.list_div_id = undefined;
 
   4   ns.rename = function(id,type,file_type,checkbox_class,is_global) {
 
   5     var $dlg       = $('#rename_dialog_'+file_type);
 
   6     var parent_id  = $dlg.parent("div.ui-tabs-panel").attr('id');
 
   7     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
 
   9     if (checkboxes.size() === 0) {
 
  10       alert(kivi.t8("No file selected, please set one checkbox!"));
 
  13     if (checkboxes.size() > 1) {
 
  14       alert(kivi.t8("More than one file selected, please set only one checkbox!"));
 
  17     var file_id = checkboxes[0].value;
 
  18     $('#newfilename_id_'+file_type).val($('#filename_'+file_id).text());
 
  19     $('#next_ids_id_'+file_type).val('');
 
  20     $('#is_global_id_'+file_type).val(is_global);
 
  21     $('#rename_id_id_'+file_type).val(file_id);
 
  22     $('#sessionfile_id_'+file_type).val('');
 
  23     $('#rename_extra_text_'+file_type).html('');
 
  25                       id:     'rename_dialog_'+file_type,
 
  26                       dialog: { title: kivi.t8("Rename attachment")
 
  31                                  $dlg.remove().appendTo('#' + parent_id);
 
  38   ns.renameclose = function(file_type) {
 
  39     $("#rename_dialog_"+file_type).dialog('close');
 
  43   ns.renameaction = function(file_type) {
 
  44     $("#rename_dialog_"+file_type).dialog('close');
 
  46       action:          'File/ajax_rename',
 
  47       id:              $('#rename_id_id_'+file_type).val(),
 
  48       to:              $('#newfilename_id_'+file_type).val(),
 
  49       next_ids:        $('#next_ids_id_'+file_type).val(),
 
  50       is_global:       $('#is_global_id_'+file_type).val(),
 
  51       sessionfile:     $('#sessionfile_id_'+file_type).val(),
 
  53     $.post("controller.pl", data, kivi.eval_json_result);
 
  57   ns.askForRename = function(file_id,file_type,file_name,sessionfile,next_ids,is_global) {
 
  58     $('#newfilename_id_'+file_type).val(file_name);
 
  59     $('#rename_id_id_'+file_type).val(file_id);
 
  60     $('#is_global_id_'+file_type).val(is_global);
 
  61     $('#next_ids_id_'+file_type).val(next_ids);
 
  62     $('#sessionfile_id_'+file_type).val(sessionfile);
 
  63     $('#rename_extra_text_'+file_type).html(kivi.t8("The uploaded filename still exists.<br>If you not modify the name this is a new version of the file"));
 
  64     var $dlg       = $('#rename_dialog_'+file_type);
 
  65     var parent_id  = $dlg.parent("div.ui-tabs-panel").attr('id');
 
  68         id:     'rename_dialog_'+file_type,
 
  69         dialog: { title: kivi.t8("Rename attachment")
 
  74                     $dlg.remove().appendTo('#' + parent_id);
 
  80   ns.upload = function(id,type,filetype,upload_title,gl) {
 
  81     $('#upload_status_dialog').remove();
 
  83     kivi.popup_dialog({ url:     'controller.pl',
 
  84                         data:    { action: 'File/ajax_upload',
 
  91                         dialog: { title: upload_title, width: 650, height: 240 } });
 
  95   ns.reset_upload_form = function() {
 
  96       $('#attachment_updfile').val('');
 
  97       $("#upload_result").html('');
 
  98       ns.allow_upload_submit();
 
 101   ns.allow_upload_submit = function() {
 
 102       const disable = $('#upload_files').val() === '';
 
 103       $('#upload_selected_button').prop('disabled', disable)
 
 104                                   .toggleClass('disabled', disable);
 
 107   ns.upload_status_dialog = function() {
 
 108     $('#files_upload').remove();
 
 109     $('#upload_status_dialog').remove();
 
 111     var html  = '<div id="upload_status_dialog"><p><div id="upload_result"></div></p>';
 
 112     html      = html + '<p><input type="button" value="' + kivi.t8('close') + '" size="30" onclick="$(\'#upload_status_dialog\').dialog(\'close\');">';
 
 113     html      = html + '</p></div>';
 
 114     $(html).hide().appendTo('#' + ns.list_div_id);
 
 116     kivi.popup_dialog({id: 'upload_status_dialog',
 
 117                        dialog: {title:  kivi.t8('Upload Status'),
 
 122   ns.upload_selected_files = function(id,type,filetype,maxsize,is_global) {
 
 123       var myform = document.getElementById("upload_form");
 
 124       var myfiles = document.getElementById("upload_files").files;
 
 126       ns.upload_files(id, type, filetype, maxsize,is_global, myfiles, myform);
 
 129   ns.upload_files = function(id, type, filetype, maxsize, is_global, myfiles, myform) {
 
 131       for ( i=0; i < myfiles.length; i++ ) {
 
 134               filesize  += myfiles[i].size;
 
 135               fname = encodeURIComponent(myfiles[i].name);
 
 140                   fname = myfiles[i].name;
 
 142               catch(err2) { fname ='';}
 
 143               $("#upload_result").html(kivi.t8("filename has not uploadable characters ")+fname);
 
 147       if ( filesize > maxsize ) {
 
 148           $("#upload_result").html(kivi.t8("filesize too big: ")+
 
 149                                    filesize+ kivi.t8(" bytes, max=") + maxsize );
 
 153       var fd = new FormData(myform);
 
 155         $(myfiles).each(function(idx, elt) {
 
 156           fd.append('uploadfiles[+]', elt);
 
 159       fd.append('action',      'File/ajax_files_uploaded');
 
 160       fd.append('json',        1);
 
 161       fd.append('object_type', type);
 
 162       fd.append('object_id',   id);
 
 163       fd.append('file_type',   filetype);
 
 164       fd.append('is_global',   is_global);
 
 166       var oReq = new XMLHttpRequest();
 
 167       oReq.onload            = ns.attSuccess;
 
 168       oReq.upload.onprogress = ns.attProgress;
 
 169       oReq.upload.onerror    = ns.attFailed;
 
 170       oReq.upload.onabort    = ns.attCanceled;
 
 171       oReq.open("post", 'controller.pl', true);
 
 172       $("#upload_result").html(kivi.t8("start upload"));
 
 176   ns.attProgress = function(oEvent) {
 
 177       if (oEvent.lengthComputable) {
 
 178           var percentComplete = (oEvent.loaded / oEvent.total) * 100;
 
 179           $("#upload_result").html(percentComplete+" % "+ kivi.t8("uploaded"));
 
 183   ns.attFailed = function(evt) {
 
 184       $('#files_upload').dialog('close');
 
 185       $("#upload_result").html(kivi.t8("An error occurred while transferring the file."));
 
 188   ns.attCanceled = function(evt) {
 
 189       $('#files_upload').dialog('close');
 
 190       $("#upload_result").html(kivi.t8("The transfer has been canceled by the user."));
 
 193   ns.attSuccess = function() {
 
 194       $('#upload_status_dialog').dialog('close');
 
 195       $('#files_upload').dialog('close');
 
 196       kivi.eval_json_result(jQuery.parseJSON(this.response));
 
 199   ns.delete = function(id,type,file_type,checkbox_class,is_global) {
 
 200     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
 
 202     if ((checkboxes.size() === 0) ||
 
 203         !confirm(kivi.t8('Do you really want to delete the selected documents?')))
 
 206       action     :  'File/ajax_delete',
 
 209       file_type  :  file_type,
 
 210       ids        :  checkbox_class,
 
 211       is_global  :  is_global,
 
 213     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
 
 217   ns.delete_file = function(id,controller_action) {
 
 218     $.post('controller.pl', { action: controller_action, id: id }, function(data) {
 
 219       kivi.eval_json_result(data);
 
 223   ns.unimport = function(id,type,file_type,checkbox_class) {
 
 224     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
 
 226     if ((checkboxes.size() === 0) ||
 
 227         !confirm(kivi.t8('Do you really want to unimport the selected documents?')))
 
 230       action     :  'File/ajax_unimport',
 
 233       file_type  :  file_type,
 
 234       ids        :  checkbox_class,
 
 236     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
 
 240   ns.update = function(id,type,file_type,is_global) {
 
 246       file_type:    file_type,
 
 250     $.post("controller.pl", data, kivi.eval_json_result);
 
 254   ns.import = function (id,type,file_type,fromwhere,frompath) {
 
 255     kivi.popup_dialog({ url:     'controller.pl',
 
 256                         data:    { action      : 'File/ajax_importdialog',
 
 260                                    file_type   : file_type,
 
 264                         dialog: { title: kivi.t8('Import documents from #1',[fromwhere]), width: 420, height: 540 }
 
 269   ns.importclose = function() {
 
 270     $("#import_dialog").dialog('close');
 
 274   ns.importaction = function(id,type,file_type,fromwhere,frompath,checkbox_class) {
 
 275     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
 
 277     $("#import_dialog").dialog('close');
 
 278     if (checkboxes.size() === 0) {
 
 282         action     : 'File/ajax_import',
 
 285         file_type  : file_type,
 
 290     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
 
 294   ns.downloadOrderitemsFiles = function(type,id) {
 
 296       action:       'DownloadZip/download_orderitems_files',
 
 299       element_type: 'part',
 
 300       zipname:      'Order_Files_'+id,
 
 302     $.download("controller.pl", data);
 
 306   ns.add_enlarged_thumbnail = function(e) {
 
 307     var file_id        = $(e.target).data('file-id');
 
 308     var file_version   = $(e.target).data('file-version');
 
 309     var overlay_img_id = 'enlarged_thumb_' + file_id;
 
 310     if (file_version) { overlay_img_id = overlay_img_id + '_' + file_version };
 
 311     var overlay_img    = $('#' + overlay_img_id);
 
 313     if (overlay_img.data('is-overlay-shown') == 1) return;
 
 315     $('.thumbnail').off('mouseover');
 
 316     overlay_img.data('is-overlay-shown', 1);
 
 319     if (overlay_img.data('is-overlay-loaded') == 1) return;
 
 322       action:         'File/ajax_get_thumbnail',
 
 324       file_version:   file_version,
 
 328     $.post("controller.pl", data, kivi.eval_json_result);
 
 331   ns.remove_enlarged_thumbnail = function(e) {
 
 333     $(e.target).data('is-overlay-shown', 0);
 
 334     $('.thumbnail').on('mouseover', ns.add_enlarged_thumbnail);
 
 337   ns.download = function(e) {
 
 338     var file_id        = $(e.target).data('file-id');
 
 339     var file_version   = $(e.target).data('file-version');
 
 342       action:  'File/download',
 
 344       version: file_version,
 
 347     $.post("controller.pl", data, kivi.eval_json_result);
 
 351   ns.init = function() {
 
 352     // Preventing page from redirecting
 
 353     $("#" + ns.list_div_id).on("dragover", function(e) {
 
 358     $("#" + ns.list_div_id).on("drop", function(e) {
 
 364     $('.upload_drop_zone').on('dragenter', function (e) {
 
 370     $('.upload_drop_zone').on('dragover', function (e) {
 
 376     $('.upload_drop_zone').on('drop', function (e) {
 
 380       ns.upload_status_dialog();
 
 382       var object_type = $(e.target).data('object-type');
 
 383       var object_id   = $(e.target).data('object-id');
 
 384       var file_type   = $(e.target).data('file-type');
 
 385       var is_global   = $(e.target).data('is-global');
 
 386       var maxsize     = $(e.target).data('maxsize');
 
 387       var files       = e.originalEvent.dataTransfer.files;
 
 388       ns.upload_files(object_id, object_type, file_type, maxsize, is_global, files);
 
 391     $('.thumbnail').on('mouseover', ns.add_enlarged_thumbnail);
 
 392     $('.overlay_img').on('mouseout', ns.remove_enlarged_thumbnail);
 
 393     $('.overlay_div img').on('click', ns.download);