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       $('#upload_selected_button').prop('disabled',$('#upload_files').val() === '');
 
 105   ns.upload_status_dialog = function() {
 
 106     $('#files_upload').remove();
 
 107     $('#upload_status_dialog').remove();
 
 109     var html  = '<div id="upload_status_dialog"><p><div id="upload_result"></div></p>';
 
 110     html      = html + '<p><input type="button" value="' + kivi.t8('close') + '" size="30" onclick="$(\'#upload_status_dialog\').dialog(\'close\');">';
 
 111     html      = html + '</p></div>';
 
 112     $(html).hide().appendTo('#' + ns.list_div_id);
 
 114     kivi.popup_dialog({id: 'upload_status_dialog',
 
 115                        dialog: {title:  kivi.t8('Upload Status'),
 
 120   ns.upload_selected_files = function(id,type,filetype,maxsize,is_global) {
 
 121       var myform = document.getElementById("upload_form");
 
 122       var myfiles = document.getElementById("upload_files").files;
 
 124       ns.upload_files(id, type, filetype, maxsize,is_global, myfiles, myform);
 
 127   ns.upload_files = function(id, type, filetype, maxsize, is_global, myfiles, myform) {
 
 129       for ( i=0; i < myfiles.length; i++ ) {
 
 132               filesize  += myfiles[i].size;
 
 133               fname = encodeURIComponent(myfiles[i].name);
 
 138                   fname = myfiles[i].name;
 
 140               catch(err2) { fname ='';}
 
 141               $("#upload_result").html(kivi.t8("filename has not uploadable characters ")+fname);
 
 145       if ( filesize > maxsize ) {
 
 146           $("#upload_result").html(kivi.t8("filesize too big: ")+
 
 147                                    filesize+ kivi.t8(" bytes, max=") + maxsize );
 
 151       var fd = new FormData(myform);
 
 153         $(myfiles).each(function(idx, elt) {
 
 154           fd.append('uploadfiles[+]', elt);
 
 157       fd.append('action',      'File/ajax_files_uploaded');
 
 158       fd.append('json',        1);
 
 159       fd.append('object_type', type);
 
 160       fd.append('object_id',   id);
 
 161       fd.append('file_type',   filetype);
 
 162       fd.append('is_global',   is_global);
 
 164       var oReq = new XMLHttpRequest();
 
 165       oReq.onload            = ns.attSuccess;
 
 166       oReq.upload.onprogress = ns.attProgress;
 
 167       oReq.upload.onerror    = ns.attFailed;
 
 168       oReq.upload.onabort    = ns.attCanceled;
 
 169       oReq.open("post", 'controller.pl', true);
 
 170       $("#upload_result").html(kivi.t8("start upload"));
 
 174   ns.attProgress = function(oEvent) {
 
 175       if (oEvent.lengthComputable) {
 
 176           var percentComplete = (oEvent.loaded / oEvent.total) * 100;
 
 177           $("#upload_result").html(percentComplete+" % "+ kivi.t8("uploaded"));
 
 181   ns.attFailed = function(evt) {
 
 182       $('#files_upload').dialog('close');
 
 183       $("#upload_result").html(kivi.t8("An error occurred while transferring the file."));
 
 186   ns.attCanceled = function(evt) {
 
 187       $('#files_upload').dialog('close');
 
 188       $("#upload_result").html(kivi.t8("The transfer has been canceled by the user."));
 
 191   ns.attSuccess = function() {
 
 192       $('#upload_status_dialog').dialog('close');
 
 193       $('#files_upload').dialog('close');
 
 194       kivi.eval_json_result(jQuery.parseJSON(this.response));
 
 197   ns.delete = function(id,type,file_type,checkbox_class,is_global) {
 
 198     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
 
 200     if ((checkboxes.size() === 0) ||
 
 201         !confirm(kivi.t8('Do you really want to delete the selected documents?')))
 
 204       action     :  'File/ajax_delete',
 
 207       file_type  :  file_type,
 
 208       ids        :  checkbox_class,
 
 209       is_global  :  is_global,
 
 211     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
 
 215   ns.delete_file = function(id,controller_action) {
 
 216     $.post('controller.pl', { action: controller_action, id: id }, function(data) {
 
 217       kivi.eval_json_result(data);
 
 221   ns.unimport = function(id,type,file_type,checkbox_class) {
 
 222     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
 
 224     if ((checkboxes.size() === 0) ||
 
 225         !confirm(kivi.t8('Do you really want to unimport the selected documents?')))
 
 228       action     :  'File/ajax_unimport',
 
 231       file_type  :  file_type,
 
 232       ids        :  checkbox_class,
 
 234     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
 
 238   ns.update = function(id,type,file_type,is_global) {
 
 244       file_type:    file_type,
 
 248     $.post("controller.pl", data, kivi.eval_json_result);
 
 252   ns.import = function (id,type,file_type,fromwhere,frompath) {
 
 253     kivi.popup_dialog({ url:     'controller.pl',
 
 254                         data:    { action      : 'File/ajax_importdialog',
 
 258                                    file_type   : file_type,
 
 262                         dialog: { title: kivi.t8('Import documents from #1',[fromwhere]), width: 420, height: 540 }
 
 267   ns.importclose = function() {
 
 268     $("#import_dialog").dialog('close');
 
 272   ns.importaction = function(id,type,file_type,fromwhere,frompath,checkbox_class) {
 
 273     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
 
 275     $("#import_dialog").dialog('close');
 
 276     if (checkboxes.size() === 0) {
 
 280         action     : 'File/ajax_import',
 
 283         file_type  : file_type,
 
 288     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
 
 292   ns.downloadOrderitemsFiles = function(type,id) {
 
 294       action:       'DownloadZip/download_orderitems_files',
 
 297       element_type: 'part',
 
 298       zipname:      'Order_Files_'+id,
 
 300     $.download("controller.pl", data);
 
 304   ns.add_enlarged_thumbnail = function(e) {
 
 305     var file_id        = $(e.target).data('file-id');
 
 306     var file_version   = $(e.target).data('file-version');
 
 307     var overlay_img_id = 'enlarged_thumb_' + file_id;
 
 308     if (file_version) { overlay_img_id = overlay_img_id + '_' + file_version };
 
 309     var overlay_img    = $('#' + overlay_img_id);
 
 311     if (overlay_img.data('is-overlay-shown') == 1) return;
 
 313     $('.thumbnail').off('mouseover');
 
 314     overlay_img.data('is-overlay-shown', 1);
 
 317     if (overlay_img.data('is-overlay-loaded') == 1) return;
 
 320       action:         'File/ajax_get_thumbnail',
 
 322       file_version:   file_version,
 
 326     $.post("controller.pl", data, kivi.eval_json_result);
 
 329   ns.remove_enlarged_thumbnail = function(e) {
 
 331     $(e.target).data('is-overlay-shown', 0);
 
 332     $('.thumbnail').on('mouseover', ns.add_enlarged_thumbnail);
 
 335   ns.download = function(e) {
 
 336     var file_id        = $(e.target).data('file-id');
 
 337     var file_version   = $(e.target).data('file-version');
 
 340       action:  'File/download',
 
 342       version: file_version,
 
 345     $.post("controller.pl", data, kivi.eval_json_result);
 
 349   ns.init = function() {
 
 350     // Preventing page from redirecting
 
 351     $("#" + ns.list_div_id).on("dragover", function(e) {
 
 356     $("#" + ns.list_div_id).on("drop", function(e) {
 
 362     $('.upload_drop_zone').on('dragenter', function (e) {
 
 368     $('.upload_drop_zone').on('dragover', function (e) {
 
 374     $('.upload_drop_zone').on('drop', function (e) {
 
 378       ns.upload_status_dialog();
 
 380       var object_type = $(e.target).data('object-type');
 
 381       var object_id   = $(e.target).data('object-id');
 
 382       var file_type   = $(e.target).data('file-type');
 
 383       var is_global   = $(e.target).data('is-global');
 
 384       var maxsize     = $(e.target).data('maxsize');
 
 385       var files       = e.originalEvent.dataTransfer.files;
 
 386       ns.upload_files(object_id, object_type, file_type, maxsize, is_global, files);
 
 389     $('.thumbnail').on('mouseover', ns.add_enlarged_thumbnail);
 
 390     $('.overlay_img').on('mouseout', ns.remove_enlarged_thumbnail);
 
 391     $('.overlay_div img').on('click', ns.download);