Dateimanagement: erst nur letzte Version anzeigen / alle Versionen ausklappbar.
[kivitendo-erp.git] / js / kivi.File.js
1 namespace('kivi.File', function(ns) {
2   ns.list_div_id = undefined;
3
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'); });
8
9     if (checkboxes.size() === 0) {
10       alert(kivi.t8("No file selected, please set one checkbox!"));
11       return false;
12     }
13     if (checkboxes.size() > 1) {
14       alert(kivi.t8("More than one file selected, please set only one checkbox!"));
15       return false;
16     }
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('');
24     kivi.popup_dialog({
25                       id:     'rename_dialog_'+file_type,
26                       dialog: { title: kivi.t8("Rename attachment")
27                                , width:  400
28                                , height: 200
29                                , modal:  true
30                                , close: function() {
31                                  $dlg.remove().appendTo('#' + parent_id);
32                                }
33                               }
34     });
35     return true;
36   }
37
38   ns.renameclose = function(file_type) {
39     $("#rename_dialog_"+file_type).dialog('close');
40     return false;
41   }
42
43   ns.renameaction = function(file_type) {
44     $("#rename_dialog_"+file_type).dialog('close');
45     var data = {
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(),
52     };
53     $.post("controller.pl", data, kivi.eval_json_result);
54     return true;
55   }
56
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');
66     kivi.popup_dialog(
67       {
68         id:     'rename_dialog_'+file_type,
69         dialog: { title: kivi.t8("Rename attachment")
70                   , width:  400
71                   , height: 200
72                   , modal:  true
73                   , close: function() {
74                     $dlg.remove().appendTo('#' + parent_id);
75                   } }
76       }
77     );
78   }
79
80   ns.upload = function(id,type,filetype,upload_title,gl) {
81     $('#upload_status_dialog').remove();
82
83     kivi.popup_dialog({ url:     'controller.pl',
84                         data:    { action: 'File/ajax_upload',
85                                    file_type:   filetype,
86                                    object_type: type,
87                                    object_id:   id,
88                                    is_global:   gl
89                                  },
90                         id:     'files_upload',
91                         dialog: { title: upload_title, width: 650, height: 240 } });
92     return true;
93   }
94
95   ns.reset_upload_form = function() {
96       $('#attachment_updfile').val('');
97       $("#upload_result").html('');
98       ns.allow_upload_submit();
99   }
100
101   ns.allow_upload_submit = function() {
102       const disable = $('#upload_files').val() === '';
103       $('#upload_selected_button').prop('disabled', disable)
104                                   .toggleClass('disabled', disable);
105   }
106
107   ns.upload_status_dialog = function() {
108     $('#files_upload').remove();
109     $('#upload_status_dialog').remove();
110
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);
115
116     kivi.popup_dialog({id: 'upload_status_dialog',
117                        dialog: {title:  kivi.t8('Upload Status'),
118                                 height: 200,
119                                 width:  650 }});
120   };
121
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;
125
126       ns.upload_files(id, type, filetype, maxsize,is_global, myfiles, myform);
127   }
128
129   ns.upload_files = function(id, type, filetype, maxsize, is_global, myfiles, myform) {
130       var filesize  = 0;
131       for ( i=0; i < myfiles.length; i++ ) {
132           var fname ='';
133           try {
134               filesize  += myfiles[i].size;
135               fname = encodeURIComponent(myfiles[i].name);
136           }
137           catch(err) {
138               fname ='';
139               try {
140                   fname = myfiles[i].name;
141               }
142               catch(err2) { fname ='';}
143               $("#upload_result").html(kivi.t8("filename has not uploadable characters ")+fname);
144               return;
145           }
146       }
147       if ( filesize > maxsize ) {
148           $("#upload_result").html(kivi.t8("filesize too big: ")+
149                                    filesize+ kivi.t8(" bytes, max=") + maxsize );
150           return;
151       }
152
153       var fd = new FormData(myform);
154       if (!myform) {
155         $(myfiles).each(function(idx, elt) {
156           fd.append('uploadfiles[+]', elt);
157         });
158       }
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);
165
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"));
173       oReq.send(fd);
174   }
175
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"));
180       }
181   }
182
183   ns.attFailed = function(evt) {
184       $('#files_upload').dialog('close');
185       $("#upload_result").html(kivi.t8("An error occurred while transferring the file."));
186   }
187
188   ns.attCanceled = function(evt) {
189       $('#files_upload').dialog('close');
190       $("#upload_result").html(kivi.t8("The transfer has been canceled by the user."));
191   }
192
193   ns.attSuccess = function() {
194       $('#upload_status_dialog').dialog('close');
195       $('#files_upload').dialog('close');
196       kivi.eval_json_result(jQuery.parseJSON(this.response));
197   }
198
199   ns.delete = function(id,type,file_type,checkbox_class,is_global) {
200     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
201
202     if ((checkboxes.size() === 0) ||
203         !confirm(kivi.t8('Do you really want to delete the selected documents?')))
204       return false;
205     var data = {
206       action     :  'File/ajax_delete',
207       object_id  :  id,
208       object_type:  type,
209       file_type  :  file_type,
210       ids        :  checkbox_class,
211       is_global  :  is_global,
212     };
213     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
214     return false;
215   }
216
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);
220     });
221   };
222
223   ns.unimport = function(id,type,file_type,checkbox_class) {
224     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
225
226     if ((checkboxes.size() === 0) ||
227         !confirm(kivi.t8('Do you really want to unimport the selected documents?')))
228       return false;
229     var data = {
230       action     :  'File/ajax_unimport',
231       object_id  :  id,
232       object_type:  type,
233       file_type  :  file_type,
234       ids        :  checkbox_class,
235     };
236     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
237     return false;
238   }
239
240   ns.update = function(id,type,file_type,is_global) {
241     var data = {
242       action:       'File/list',
243       json:         1,
244       object_type:  type,
245       object_id:    id,
246       file_type:    file_type,
247       is_global:    is_global
248     };
249
250     $.post("controller.pl", data, kivi.eval_json_result);
251     return false;
252   }
253
254   ns.import = function (id,type,file_type,fromwhere,frompath) {
255     kivi.popup_dialog({ url:     'controller.pl',
256                         data:    { action      : 'File/ajax_importdialog',
257                                    object_type : type,
258                                    source      : fromwhere,
259                                    path        : frompath,
260                                    file_type   : file_type,
261                                    object_id   : id
262                                  },
263                         id:     'import_dialog',
264                         dialog: { title: kivi.t8('Import documents from #1',[fromwhere]), width: 420, height: 540 }
265                       });
266     return true;
267   }
268
269   ns.importclose = function() {
270     $("#import_dialog").dialog('close');
271     return false;
272   }
273
274   ns.importaction = function(id,type,file_type,fromwhere,frompath,checkbox_class) {
275     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
276
277     $("#import_dialog").dialog('close');
278     if (checkboxes.size() === 0) {
279       return false;
280     }
281     var data = {
282         action     : 'File/ajax_import',
283         object_id  : id,
284         object_type: type,
285         file_type  : file_type,
286         source     : fromwhere,
287         path       : frompath,
288         ids        : checkbox_class
289     };
290     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
291     return true;
292   }
293
294   ns.downloadOrderitemsFiles = function(type,id) {
295     var data = {
296       action:       'DownloadZip/download_orderitems_files',
297       object_type:  type,
298       object_id:    id,
299       element_type: 'part',
300       zipname:      'Order_Files_'+id,
301     };
302     $.download("controller.pl", data);
303     return false;
304   }
305
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);
312
313     if (overlay_img.data('is-overlay-shown') == 1) return;
314
315     $('.thumbnail').off('mouseover');
316     overlay_img.data('is-overlay-shown', 1);
317     overlay_img.show();
318
319     if (overlay_img.data('is-overlay-loaded') == 1) return;
320
321     var data = {
322       action:         'File/ajax_get_thumbnail',
323       file_id:        file_id,
324       file_version:   file_version,
325       size:           512
326     };
327
328     $.post("controller.pl", data, kivi.eval_json_result);
329   };
330
331   ns.remove_enlarged_thumbnail = function(e) {
332     $(e.target).hide();
333     $(e.target).data('is-overlay-shown', 0);
334     $('.thumbnail').on('mouseover', ns.add_enlarged_thumbnail);
335   };
336
337   ns.download = function(e) {
338     var file_id        = $(e.target).data('file-id');
339     var file_version   = $(e.target).data('file-version');
340
341     var data = {
342       action:  'File/download',
343       id:      file_id,
344       version: file_version,
345     };
346
347     $.post("controller.pl", data, kivi.eval_json_result);
348
349   };
350
351   ns.init = function() {
352     // Preventing page from redirecting
353     $("#" + ns.list_div_id).on("dragover", function(e) {
354       e.preventDefault();
355       e.stopPropagation();
356     });
357
358     $("#" + ns.list_div_id).on("drop", function(e) {
359       e.preventDefault();
360       e.stopPropagation();
361     });
362
363     // Drag enter
364     $('.upload_drop_zone').on('dragenter', function (e) {
365       e.stopPropagation();
366       e.preventDefault();
367     });
368
369     // Drag over
370     $('.upload_drop_zone').on('dragover', function (e) {
371       e.stopPropagation();
372       e.preventDefault();
373     });
374
375     // Drop
376     $('.upload_drop_zone').on('drop', function (e) {
377       e.stopPropagation();
378       e.preventDefault();
379
380       ns.upload_status_dialog();
381
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);
389     });
390
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);
394   };
395
396   ns.doc_tab_init = function(tabs_id, doc_tab_id, id, object_type) {
397     var url = 'controller.pl?action=File/list&file_type=document&object_type=' + object_type  + '&object_id=' + $('#id').val();
398
399     $('#' + tabs_id).on('tabsbeforeactivate', function(e, ui) {
400       if (ui.newPanel.attr('id') !== doc_tab_id) return;
401       $('#' + doc_tab_id).html(kivi.t8('Loading...'));
402       $('#' + doc_tab_id).load(url);
403     });
404
405     $('#' + tabs_id).on('tabscreate', function(e, ui) {
406       if (ui.panel.attr('id') !== doc_tab_id) return;
407       $('#' + doc_tab_id).html(kivi.t8('Loading...'));
408       $('#' + doc_tab_id).load(url);
409     });
410   };
411
412   ns.toggle_versions = function(file_id) {
413     if ($('#version_toggle_' + file_id).data('versions_expanded')) {
414       $('.version_row_'    + file_id).hide();
415       $('#version_toggle_' + file_id).data('versions_expanded', 0);
416       $('#version_toggle_' + file_id).html("⏷ ");
417     } else {
418       $('.version_row_'    + file_id).show();
419       $('#version_toggle_' + file_id).data('versions_expanded', 1);
420       $('#version_toggle_' + file_id).html("⏶ ");
421     }
422   };
423
424 });