Dateimanagement: Stammdaten: DOM-Elemente bei multiples Tab-Aufrufen nicht dupliziere...
[kivitendo-erp.git] / js / kivi.File.js
1 namespace('kivi.File', function(ns) {
2
3   ns.rename = function(id,type,file_type,checkbox_class,is_global) {
4     var $dlg       = $('#rename_dialog');
5     var parent_id  = $dlg.parent("div.ui-tabs-panel").attr('id');
6     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
7
8     if (checkboxes.size() === 0) {
9                   alert(kivi.t8("No file selected, please set one checkbox!"));
10                   return false;
11           }
12     if (checkboxes.size() > 1) {
13             alert(kivi.t8("More than one file selected, please set only one checkbox!"));
14                   return false;
15           }
16     var file_id = checkboxes[0].value;
17     $('#newfilename_id').val($('#filename_'+file_id).text());
18     $('#next_ids_id').val('');
19     $('#is_global_id').val(is_global);
20     $('#rename_id_id').val(file_id);
21     $('#sessionfile_id').val('');
22         $('#rename_extra_text').html('');
23     kivi.popup_dialog({
24                       id:     'rename_dialog',
25                       dialog: { title: kivi.t8("Rename attachment")
26                                , width:  400
27                                , height: 200
28                                , modal:  true
29                                , close: function() {
30                                  $dlg.remove().appendTo('#' + parent_id);
31                                }
32                               }
33     });
34     return true;
35   }
36
37   ns.renameclose = function() {
38     $("#rename_dialog").dialog('close');
39     return false;
40   }
41
42   ns.renameaction = function() {
43           $("#rename_dialog").dialog('close');
44     var data = {
45       action:          'File/ajax_rename',
46       id:              $('#rename_id_id').val(),
47             to:              $('#newfilename_id').val(),
48       next_ids:        $('#next_ids_id').val(),
49       is_global:       $('#is_global_id').val(),
50       sessionfile:     $('#sessionfile_id').val(),
51     };
52     $.post("controller.pl", data, kivi.eval_json_result);
53     return true;
54   }
55
56   ns.askForRename = function(file_id,file_name,sessionfile,next_ids,is_global) {
57     $('#newfilename_id').val(file_name);
58     $('#rename_id_id').val(file_id);
59     $('#is_global_id').val(is_global);
60     $('#next_ids_id').val(next_ids);
61     $('#sessionfile_id').val(sessionfile);
62     $('#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"));
63     var $dlg       = $('#rename_dialog');
64     var parent_id  = $dlg.parent("div.ui-tabs-panel").attr('id');
65     kivi.popup_dialog(
66       {
67         id:     'rename_dialog',
68         dialog: { title: kivi.t8("Rename attachment")
69                   , width:  400
70                   , height: 200
71                   , modal:  true
72                   , close: function() {
73                     $dlg.remove().appendTo('#' + parent_id);
74                   } }
75       }
76     );
77   }
78
79   ns.upload = function(id,type,filetype,upload_title,gl) {
80     kivi.popup_dialog({ url:     'controller.pl',
81                         data:    { action: 'File/ajax_upload',
82                                    file_type:   filetype,
83                                    object_type: type,
84                                    object_id:   id,
85                                    is_global:   gl
86                                  },
87                         id:     'files_upload',
88                         dialog: { title: upload_title, width: 650, height: 240 } });
89     return true;
90   }
91
92   ns.reset_upload_form = function() {
93       $('#attachment_updfile').val('');
94       $("#upload_result").html('');
95       ns.allow_upload_submit();
96   }
97
98   ns.allow_upload_submit = function() {
99       $('#upload_selected_button').prop('disabled',$('#upload_files').val() === '');
100   }
101
102   ns.upload_selected_files = function(id,type,filetype,maxsize,is_global) {
103       var myform = document.getElementById("upload_form");
104       var filesize  = 0;
105       var myfiles = document.getElementById("upload_files").files;
106       for ( i=0; i < myfiles.length; i++ ) {
107           var fname ='';
108           try {
109               filesize  += myfiles[i].size;
110               fname = encodeURIComponent(myfiles[i].name);
111           }
112           catch(err) {
113               fname ='';
114               try {
115                   fname = myfiles[i].name;
116               }
117               catch(err2) { fname ='';}
118               $("#upload_result").html(kivi.t8("filename has not uploadable characters ")+fname);
119               return;
120           }
121       }
122       if ( filesize > maxsize ) {
123           $("#upload_result").html(kivi.t8("filesize too big: ")+
124                                    filesize+ kivi.t8(" bytes, max=") + maxsize );
125           return;
126       }
127
128       myform.action ="controller.pl?action=File/ajax_files_uploaded&json=1&object_type="+
129           type+'&object_id='+id+'&file_type='+filetype+'&is_global='+is_global;
130       var oReq = new XMLHttpRequest();
131       oReq.onload            = ns.attSuccess;
132       oReq.upload.onprogress = ns.attProgress;
133       oReq.upload.onerror    = ns.attFailed;
134       oReq.upload.onabort    = ns.attCanceled;
135       oReq.open("post",myform.action, true);
136       $("#upload_result").html(kivi.t8("start upload"));
137       oReq.send(new FormData(myform));
138   }
139
140   ns.attProgress = function(oEvent) {
141       if (oEvent.lengthComputable) {
142           var percentComplete = (oEvent.loaded / oEvent.total) * 100;
143           $("#upload_result").html(percentComplete+" % "+ kivi.t8("uploaded"));
144       }
145   }
146
147   ns.attFailed = function(evt) {
148       $('#files_upload').dialog('close');
149       $("#upload_result").html(kivi.t8("An error occurred while transferring the file."));
150   }
151
152   ns.attCanceled = function(evt) {
153       $('#files_upload').dialog('close');
154       $("#upload_result").html(kivi.t8("The transfer has been canceled by the user."));
155   }
156
157   ns.attSuccess = function() {
158       $('#files_upload').dialog('close');
159       kivi.eval_json_result(jQuery.parseJSON(this.response));
160   }
161
162   ns.delete = function(id,type,file_type,checkbox_class,is_global) {
163     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
164
165     if ((checkboxes.size() === 0) ||
166         !confirm(kivi.t8('Do you really want to delete the selected documents?')))
167       return false;
168     var data = {
169       action     :  'File/ajax_delete',
170       object_id  :  id,
171       object_type:  type,
172       file_type  :  file_type,
173       ids        :  checkbox_class,
174       is_global  :  is_global,
175     };
176     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
177     return false;
178   }
179
180   ns.delete_file = function(id,controller_action) {
181     $.post('controller.pl', { action: controller_action, id: id }, function(data) {
182       kivi.eval_json_result(data);
183     });
184   };
185
186   ns.unimport = function(id,type,file_type,checkbox_class) {
187     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
188
189     if ((checkboxes.size() === 0) ||
190         !confirm(kivi.t8('Do you really want to unimport the selected documents?')))
191       return false;
192     var data = {
193       action     :  'File/ajax_unimport',
194       object_id  :  id,
195       object_type:  type,
196       file_type  :  file_type,
197       ids        :  checkbox_class,
198     };
199     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
200     return false;
201   }
202
203   ns.update = function(id,type,file_type,is_global) {
204     var data = {
205       action:       'File/list',
206       json:         1,
207       object_type:  type,
208       object_id:    id,
209       file_type:    file_type,
210       is_global:    is_global
211     };
212
213     $.post("controller.pl", data, kivi.eval_json_result);
214     return false;
215   }
216
217   ns.import = function (id,type,file_type,fromwhere,frompath) {
218     kivi.popup_dialog({ url:     'controller.pl',
219                         data:    { action      : 'File/ajax_importdialog',
220                                    object_type : type,
221                                    source      : fromwhere,
222                                    path        : frompath,
223                                    file_type   : file_type,
224                                    object_id   : id
225                                  },
226                         id:     'import_dialog',
227                         dialog: { title: kivi.t8('Import documents from #1',[fromwhere]), width: 420, height: 540 }
228                       });
229     return true;
230   }
231
232   ns.importclose = function() {
233     $("#import_dialog").dialog('close');
234     return false;
235   }
236
237   ns.importaction = function(id,type,file_type,fromwhere,frompath,checkbox_class) {
238     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
239
240           $("#import_dialog").dialog('close');
241     if (checkboxes.size() === 0) {
242                   return false;
243           }
244         var data = {
245         action     : 'File/ajax_import',
246         object_id  : id,
247         object_type: type,
248         file_type  : file_type,
249         source     : fromwhere,
250         path       : frompath,
251         ids        : checkbox_class
252     };
253     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
254     return true;
255   }
256
257   ns.downloadOrderitemsFiles = function(type,id) {
258           var data = {
259       action:       'DownloadZip/download_orderitems_files',
260       object_type:  type,
261       object_id:    id,
262       element_type: 'part',
263       zipname:      'Order_Files_'+id,
264     };
265     $.download("controller.pl", data);
266     return false;
267   }
268
269   ns.init = function() {
270   }
271
272 });