b02d7d0ff6a177e19e60b9c9025929943b886eb2
[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                                  console.log("Closing, moving to original parent " + parent_id);
31                                  $dlg.remove().appendTo('#' + parent_id);
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     kivi.popup_dialog(
64       {
65         id:     'rename_dialog',
66         dialog: { title: kivi.t8("Rename attachment")
67                   , width:  400
68                   , height: 200
69                   , modal:  true }
70       });
71   }
72
73   ns.upload = function(id,type,filetype,upload_title,gl) {
74     kivi.popup_dialog({ url:     'controller.pl',
75                         data:    { action: 'File/ajax_upload',
76                                    file_type:   filetype,
77                                    object_type: type,
78                                    object_id:   id,
79                                    is_global:   gl
80                                  },
81                         id:     'files_upload',
82                         dialog: { title: upload_title, width: 650, height: 240 } });
83     return true;
84   }
85
86   ns.reset_upload_form = function() {
87       $('#attachment_updfile').val('');
88       $("#upload_result").html('');
89       ns.allow_upload_submit();
90   }
91
92   ns.allow_upload_submit = function() {
93       $('#upload_selected_button').prop('disabled',$('#upload_files').val() === '');
94   }
95
96   ns.upload_selected_files = function(id,type,filetype,maxsize,is_global) {
97       var myform = document.getElementById("upload_form");
98       var filesize  = 0;
99       var myfiles = document.getElementById("upload_files").files;
100       for ( i=0; i < myfiles.length; i++ ) {
101           var fname ='';
102           try {
103               filesize  += myfiles[i].size;
104               fname = encodeURIComponent(myfiles[i].name);
105           }
106           catch(err) {
107               fname ='';
108               try {
109                   fname = myfiles[i].name;
110               }
111               catch(err2) { fname ='';}
112               $("#upload_result").html(kivi.t8("filename has not uploadable characters ")+fname);
113               return;
114           }
115       }
116       if ( filesize > maxsize ) {
117           $("#upload_result").html(kivi.t8("filesize too big: ")+
118                                    filesize+ kivi.t8(" bytes, max=") + maxsize );
119           return;
120       }
121
122       myform.action ="controller.pl?action=File/ajax_files_uploaded&json=1&object_type="+
123           type+'&object_id='+id+'&file_type='+filetype+'&is_global='+is_global;
124       var oReq = new XMLHttpRequest();
125       oReq.onload            = ns.attSuccess;
126       oReq.upload.onprogress = ns.attProgress;
127       oReq.upload.onerror    = ns.attFailed;
128       oReq.upload.onabort    = ns.attCanceled;
129       oReq.open("post",myform.action, true);
130       $("#upload_result").html(kivi.t8("start upload"));
131       oReq.send(new FormData(myform));
132   }
133
134   ns.attProgress = function(oEvent) {
135       if (oEvent.lengthComputable) {
136           var percentComplete = (oEvent.loaded / oEvent.total) * 100;
137           $("#upload_result").html(percentComplete+" % "+ kivi.t8("uploaded"));
138       }
139   }
140
141   ns.attFailed = function(evt) {
142       $('#files_upload').dialog('close');
143       $("#upload_result").html(kivi.t8("An error occurred while transferring the file."));
144   }
145
146   ns.attCanceled = function(evt) {
147       $('#files_upload').dialog('close');
148       $("#upload_result").html(kivi.t8("The transfer has been canceled by the user."));
149   }
150
151   ns.attSuccess = function() {
152       $('#files_upload').dialog('close');
153       kivi.eval_json_result(jQuery.parseJSON(this.response));
154   }
155
156   ns.delete = function(id,type,file_type,checkbox_class,is_global) {
157     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
158
159     if ((checkboxes.size() === 0) ||
160         !confirm(kivi.t8('Do you really want to delete the selected documents?')))
161       return false;
162     var data = {
163       action     :  'File/ajax_delete',
164       object_id  :  id,
165       object_type:  type,
166       file_type  :  file_type,
167       ids        :  checkbox_class,
168       is_global  :  is_global,
169     };
170     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
171     return false;
172   }
173
174   ns.unimport = function(id,type,file_type,checkbox_class) {
175     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
176
177     if ((checkboxes.size() === 0) ||
178         !confirm(kivi.t8('Do you really want to unimport the selected documents?')))
179       return false;
180     var data = {
181       action     :  'File/ajax_unimport',
182       object_id  :  id,
183       object_type:  type,
184       file_type  :  file_type,
185       ids        :  checkbox_class,
186     };
187     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
188     return false;
189   }
190
191   ns.update = function(id,type,file_type,is_global) {
192     var data = {
193       action:       'File/list',
194       json:         1,
195       object_type:  type,
196       object_id:    id,
197       file_type:    file_type,
198       is_global:    is_global
199     };
200
201     $.post("controller.pl", data, kivi.eval_json_result);
202     return false;
203   }
204
205   ns.import = function (id,type,file_type,fromwhere,frompath) {
206     kivi.popup_dialog({ url:     'controller.pl',
207                         data:    { action      : 'File/ajax_importdialog',
208                                    object_type : type,
209                                    source      : fromwhere,
210                                    path        : frompath,
211                                    file_type   : file_type,
212                                    object_id   : id
213                                  },
214                         id:     'import_dialog',
215                         dialog: { title: kivi.t8('Import documents from #1',[fromwhere]), width: 420, height: 540 }
216                       });
217     return true;
218   }
219     
220   ns.importclose = function() {
221     $("#import_dialog").dialog('close');
222     return false;
223   }
224   
225   ns.importaction = function(id,type,file_type,fromwhere,frompath,checkbox_class) {
226     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
227
228           $("#import_dialog").dialog('close');
229     if (checkboxes.size() === 0) {
230                   return false;
231           }
232         var data = {
233         action     : 'File/ajax_import',
234         object_id  : id,
235         object_type: type,
236         file_type  : file_type,
237         source     : fromwhere,
238         path       : frompath,
239         ids        : checkbox_class
240     };
241     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
242     return true;
243   }
244
245   ns.downloadOrderitemsFiles = function(type,id) {
246           var data = {
247       action:       'DownloadZip/download_orderitems_files',
248       object_type:  type,
249       object_id:    id,
250       element_type: 'part',
251       zipname:      'Order_Files_'+id,
252     };
253     $.download("controller.pl", data);
254     return false;
255   }
256
257   ns.init = function() {
258   }
259
260 });