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