Dateimanagement: größere Vorschaubilder beim Drüberfahren
[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       $('#upload_selected_button').prop('disabled',$('#upload_files').val() === '');
103   }
104
105   ns.upload_status_dialog = function() {
106     $('#files_upload').remove();
107     $('#upload_status_dialog').remove();
108
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);
113
114     kivi.popup_dialog({id: 'upload_status_dialog',
115                        dialog: {title:  kivi.t8('Upload Status'),
116                                 height: 200,
117                                 width:  650 }});
118   };
119
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;
123
124       ns.upload_files(id, type, filetype, maxsize,is_global, myfiles, myform);
125   }
126
127   ns.upload_files = function(id, type, filetype, maxsize, is_global, myfiles, myform) {
128       var filesize  = 0;
129       for ( i=0; i < myfiles.length; i++ ) {
130           var fname ='';
131           try {
132               filesize  += myfiles[i].size;
133               fname = encodeURIComponent(myfiles[i].name);
134           }
135           catch(err) {
136               fname ='';
137               try {
138                   fname = myfiles[i].name;
139               }
140               catch(err2) { fname ='';}
141               $("#upload_result").html(kivi.t8("filename has not uploadable characters ")+fname);
142               return;
143           }
144       }
145       if ( filesize > maxsize ) {
146           $("#upload_result").html(kivi.t8("filesize too big: ")+
147                                    filesize+ kivi.t8(" bytes, max=") + maxsize );
148           return;
149       }
150
151       var fd = new FormData(myform);
152       if (!myform) {
153         $(myfiles).each(function(idx, elt) {
154           fd.append('uploadfiles[+]', elt);
155         });
156       }
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);
163
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"));
171       oReq.send(fd);
172   }
173
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"));
178       }
179   }
180
181   ns.attFailed = function(evt) {
182       $('#files_upload').dialog('close');
183       $("#upload_result").html(kivi.t8("An error occurred while transferring the file."));
184   }
185
186   ns.attCanceled = function(evt) {
187       $('#files_upload').dialog('close');
188       $("#upload_result").html(kivi.t8("The transfer has been canceled by the user."));
189   }
190
191   ns.attSuccess = function() {
192       $('#upload_status_dialog').dialog('close');
193       $('#files_upload').dialog('close');
194       kivi.eval_json_result(jQuery.parseJSON(this.response));
195   }
196
197   ns.delete = function(id,type,file_type,checkbox_class,is_global) {
198     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
199
200     if ((checkboxes.size() === 0) ||
201         !confirm(kivi.t8('Do you really want to delete the selected documents?')))
202       return false;
203     var data = {
204       action     :  'File/ajax_delete',
205       object_id  :  id,
206       object_type:  type,
207       file_type  :  file_type,
208       ids        :  checkbox_class,
209       is_global  :  is_global,
210     };
211     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
212     return false;
213   }
214
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);
218     });
219   };
220
221   ns.unimport = function(id,type,file_type,checkbox_class) {
222     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
223
224     if ((checkboxes.size() === 0) ||
225         !confirm(kivi.t8('Do you really want to unimport the selected documents?')))
226       return false;
227     var data = {
228       action     :  'File/ajax_unimport',
229       object_id  :  id,
230       object_type:  type,
231       file_type  :  file_type,
232       ids        :  checkbox_class,
233     };
234     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
235     return false;
236   }
237
238   ns.update = function(id,type,file_type,is_global) {
239     var data = {
240       action:       'File/list',
241       json:         1,
242       object_type:  type,
243       object_id:    id,
244       file_type:    file_type,
245       is_global:    is_global
246     };
247
248     $.post("controller.pl", data, kivi.eval_json_result);
249     return false;
250   }
251
252   ns.import = function (id,type,file_type,fromwhere,frompath) {
253     kivi.popup_dialog({ url:     'controller.pl',
254                         data:    { action      : 'File/ajax_importdialog',
255                                    object_type : type,
256                                    source      : fromwhere,
257                                    path        : frompath,
258                                    file_type   : file_type,
259                                    object_id   : id
260                                  },
261                         id:     'import_dialog',
262                         dialog: { title: kivi.t8('Import documents from #1',[fromwhere]), width: 420, height: 540 }
263                       });
264     return true;
265   }
266
267   ns.importclose = function() {
268     $("#import_dialog").dialog('close');
269     return false;
270   }
271
272   ns.importaction = function(id,type,file_type,fromwhere,frompath,checkbox_class) {
273     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
274
275     $("#import_dialog").dialog('close');
276     if (checkboxes.size() === 0) {
277       return false;
278     }
279     var data = {
280         action     : 'File/ajax_import',
281         object_id  : id,
282         object_type: type,
283         file_type  : file_type,
284         source     : fromwhere,
285         path       : frompath,
286         ids        : checkbox_class
287     };
288     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
289     return true;
290   }
291
292   ns.downloadOrderitemsFiles = function(type,id) {
293     var data = {
294       action:       'DownloadZip/download_orderitems_files',
295       object_type:  type,
296       object_id:    id,
297       element_type: 'part',
298       zipname:      'Order_Files_'+id,
299     };
300     $.download("controller.pl", data);
301     return false;
302   }
303
304   ns.add_enlarged_thumbnail = function(e) {
305     var file_id        = $(e.target).data('file-id');
306     var overlay_img_id = 'enlarged_thumb_' + file_id;
307     var overlay_img    = $('#' + overlay_img_id);
308
309     if (overlay_img.data('is-overlay-shown') == 1) return;
310
311     $('.thumbnail').off('mouseover');
312     overlay_img.data('is-overlay-shown', 1);
313     overlay_img.show();
314
315     if (overlay_img.data('is-overlay-loaded') == 1) return;
316
317     var data = {
318       action:         'File/ajax_get_thumbnail',
319       file_id:        file_id,
320       file_version:   $(e.target).data('file-version'),
321       size:           512
322     };
323
324     $.post("controller.pl", data, kivi.eval_json_result);
325   };
326
327   ns.remove_enlarged_thumbnail = function(e) {
328     $(e.target).hide();
329     $(e.target).data('is-overlay-shown', 0);
330     $('.thumbnail').on('mouseover', ns.add_enlarged_thumbnail);
331   };
332
333   ns.init = function() {
334     // Preventing page from redirecting
335     $("#" + ns.list_div_id).on("dragover", function(e) {
336       e.preventDefault();
337       e.stopPropagation();
338     });
339
340     $("#" + ns.list_div_id).on("drop", function(e) {
341       e.preventDefault();
342       e.stopPropagation();
343     });
344
345     // Drag enter
346     $('.upload_drop_zone').on('dragenter', function (e) {
347       e.stopPropagation();
348       e.preventDefault();
349     });
350
351     // Drag over
352     $('.upload_drop_zone').on('dragover', function (e) {
353       e.stopPropagation();
354       e.preventDefault();
355     });
356
357     // Drop
358     $('.upload_drop_zone').on('drop', function (e) {
359       e.stopPropagation();
360       e.preventDefault();
361
362       ns.upload_status_dialog();
363
364       var object_type = $(e.target).data('object-type');
365       var object_id   = $(e.target).data('object-id');
366       var file_type   = $(e.target).data('file-type');
367       var is_global   = $(e.target).data('is-global');
368       var maxsize     = $(e.target).data('maxsize');
369       var files       = e.originalEvent.dataTransfer.files;
370       ns.upload_files(object_id, object_type, file_type, maxsize, is_global, files);
371     });
372
373     $('.thumbnail').on('mouseover', ns.add_enlarged_thumbnail);
374     $('.overlay_img').on('click', ns.remove_enlarged_thumbnail);
375     $('.overlay_img').on('mouseout', ns.remove_enlarged_thumbnail);
376   };
377
378 });