1 namespace('kivi.File', function(ns) {
 
   3   ns.rename = function(id,type,file_type,checkbox_class,is_global) {
 
   4     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
 
   6     if (checkboxes.size() === 0) {
 
   7                   alert(kivi.t8("No file selected, please set one checkbox!"));
 
  10     if (checkboxes.size() > 1) {
 
  11             alert(kivi.t8("More than one file selected, please set only one checkbox!"));
 
  14     var file_id = checkboxes[0].value;
 
  15     $('#newfilename_id').val($('#filename_'+file_id).text());
 
  16     $('#next_ids_id').val('');
 
  17     $('#is_global_id').val(is_global);
 
  18     $('#rename_id_id').val(file_id);
 
  19     $('#sessionfile_id').val('');
 
  20         $('#rename_extra_text').html('');
 
  23                       dialog: { title: kivi.t8("Rename attachment")
 
  30   ns.renameclose = function() {
 
  31     $("#rename_dialog").dialog('close');
 
  35   ns.renameaction = function() {
 
  36           $("#rename_dialog").dialog('close');
 
  38       action:          'File/ajax_rename',
 
  39       id:              $('#rename_id_id').val(),
 
  40             to:              $('#newfilename_id').val(),
 
  41       next_ids:        $('#next_ids_id').val(),
 
  42       is_global:       $('#is_global_id').val(),
 
  43       sessionfile:     $('#sessionfile_id').val(),
 
  45     $.post("controller.pl", data, kivi.eval_json_result);
 
  49   ns.askForRename = function(file_id,file_name,sessionfile,next_ids,is_global) {
 
  50     $('#newfilename_id').val(file_name);
 
  51     $('#rename_id_id').val(file_id);
 
  52     $('#is_global_id').val(is_global);
 
  53     $('#next_ids_id').val(next_ids);
 
  54     $('#sessionfile_id').val(sessionfile);
 
  55     $('#rename_extra_text').html(kivi.t8("The uploaded filename still exists.<br>If you not modify the name this is a new version of the file"));
 
  59         dialog: { title: kivi.t8("Rename attachment")
 
  66   ns.upload = function(id,type,filetype,upload_title,gl) {
 
  67     kivi.popup_dialog({ url:     'controller.pl',
 
  68                         data:    { action: 'File/ajax_upload',
 
  75                         dialog: { title: upload_title, width: 650, height: 240 } });
 
  79   ns.reset_upload_form = function() {
 
  80       $('#attachment_updfile').val('');
 
  81       $("#upload_result").html('');
 
  82       ns.allow_upload_submit();
 
  85   ns.allow_upload_submit = function() {
 
  86       $('#upload_selected_button').prop('disabled',$('#upload_files').val() === '');
 
  89   ns.upload_selected_files = function(id,type,filetype,maxsize,is_global) {
 
  90       var myform = document.getElementById("upload_form");
 
  92       var myfiles = document.getElementById("upload_files").files;
 
  93       for ( i=0; i < myfiles.length; i++ ) {
 
  96               filesize  += myfiles[i].size;
 
  97               fname = encodeURIComponent(myfiles[i].name);
 
 102                   fname = myfiles[i].name;
 
 104               catch(err2) { fname ='';}
 
 105               $("#upload_result").html(kivi.t8("filename has not uploadable characters ")+fname);
 
 109       if ( filesize > maxsize ) {
 
 110           $("#upload_result").html(kivi.t8("filesize too big: ")+
 
 111                                    filesize+ kivi.t8(" bytes, max=") + maxsize );
 
 115       myform.action ="controller.pl?action=File/ajax_files_uploaded&json=1&object_type="+
 
 116           type+'&object_id='+id+'&file_type='+filetype+'&is_global='+is_global;
 
 117       var oReq = new XMLHttpRequest();
 
 118       oReq.onload            = ns.attSuccess;
 
 119       oReq.upload.onprogress = ns.attProgress;
 
 120       oReq.upload.onerror    = ns.attFailed;
 
 121       oReq.upload.onabort    = ns.attCanceled;
 
 122       oReq.open("post",myform.action, true);
 
 123       $("#upload_result").html(kivi.t8("start upload"));
 
 124       oReq.send(new FormData(myform));
 
 127   ns.attProgress = function(oEvent) {
 
 128       if (oEvent.lengthComputable) {
 
 129           var percentComplete = (oEvent.loaded / oEvent.total) * 100;
 
 130           $("#upload_result").html(percentComplete+" % "+ kivi.t8("uploaded"));
 
 134   ns.attFailed = function(evt) {
 
 135       $('#files_upload').dialog('close');
 
 136       $("#upload_result").html(kivi.t8("An error occurred while transferring the file."));
 
 139   ns.attCanceled = function(evt) {
 
 140       $('#files_upload').dialog('close');
 
 141       $("#upload_result").html(kivi.t8("The transfer has been canceled by the user."));
 
 144   ns.attSuccess = function() {
 
 145       $('#files_upload').dialog('close');
 
 146       kivi.eval_json_result(jQuery.parseJSON(this.response));
 
 149   ns.delete = function(id,type,file_type,checkbox_class,is_global) {
 
 150     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
 
 152     if ((checkboxes.size() === 0) ||
 
 153         !confirm(kivi.t8('Do you really want to delete the selected documents?')))
 
 156       action     :  'File/ajax_delete',
 
 159       file_type  :  file_type,
 
 160       ids        :  checkbox_class,
 
 161       is_global  :  is_global,
 
 163     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
 
 167   ns.unimport = function(id,type,file_type,checkbox_class) {
 
 168     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
 
 170     if ((checkboxes.size() === 0) ||
 
 171         !confirm(kivi.t8('Do you really want to unimport the selected documents?')))
 
 174       action     :  'File/ajax_unimport',
 
 177       file_type  :  file_type,
 
 178       ids        :  checkbox_class,
 
 180     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
 
 184   ns.update = function(id,type,file_type,is_global) {
 
 190       file_type:    file_type,
 
 194     $.post("controller.pl", data, kivi.eval_json_result);
 
 198   ns.import = function (id,type,file_type,fromwhere,frompath) {
 
 199     kivi.popup_dialog({ url:     'controller.pl',
 
 200                         data:    { action      : 'File/ajax_importdialog',
 
 204                                    file_type   : file_type,
 
 208                         dialog: { title: kivi.t8('Import documents from #1',[fromwhere]), width: 420, height: 540 }
 
 213   ns.importclose = function() {
 
 214     $("#import_dialog").dialog('close');
 
 218   ns.importaction = function(id,type,file_type,fromwhere,frompath,checkbox_class) {
 
 219     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
 
 221           $("#import_dialog").dialog('close');
 
 222     if (checkboxes.size() === 0) {
 
 226         action     : 'File/ajax_import',
 
 229         file_type  : file_type,
 
 234     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
 
 238   ns.downloadOrderitemsFiles = function(type,id) {
 
 240       action:       'DownloadZip/download_orderitems_files',
 
 243       element_type: 'part',
 
 244       zipname:      'Order_Files_'+id,
 
 246     $.download("controller.pl", data);
 
 250   ns.init = function() {