source: sandbox/filemanager/js/common_functions.js @ 1803

Revision 1803, 11.0 KB checked in by fpcorrea, 14 years ago (diff)

Ticket #597 - Melhorias no módulo Gerenciador de Arquivos

Line 
1function load(path,el){
2        currentPath = path;
3        contentFolders = document.getElementById('content_folders');
4        for (i=0; i < contentFolders.childNodes.length; i++)
5                if (contentFolders.childNodes[i].className == "sl")
6                        contentFolders.childNodes[i].className = "l";
7        el.className = "sl";
8        toolbar.control('reload');
9}
10
11var denyFileExtensions = new Array('exe','com','reg','chm','cnf','hta','ins',
12                                        'jse','job','lnk','pif','src','scf','sct','shb',
13                                        'vbe','vbs','wsc','wsf','wsh','cer','its','mau',
14                                        'mda','mar','mdz','prf','pst');
15function validateFileExtension(fileName){
16        var error_flag = false;
17        var fileExtension = fileName.split(".");
18        fileExtension = fileExtension[(fileExtension.length-1)];
19        for(var i=0; i<denyFileExtensions.length; i++)
20        {
21                if(denyFileExtensions[i] == fileExtension)
22                {
23                        error_flag = true;
24                        break;
25                }
26
27        }
28
29        if ( error_flag == true )
30        {
31                write_error(get_lang('File extension forbidden or invalid file') + '.');
32                return false;
33        }
34        return true;
35}
36
37function get_lang(_key){
38        var key = _key.toLowerCase();
39        if(array_lang[key])
40                var _value = array_lang[key];
41        else
42                var _value = _key+"*";
43
44        if(arguments.length > 1)
45                for(j = 1; typeof(arguments[j]) != 'undefined'; j++)
46                        _value = _value.replace("%"+j,arguments[j]);
47        return _value;
48
49}
50
51
52function newEmptyFile(){
53        var name = prompt(get_lang('Enter with the name of new file/directory'), '');
54        var input_text = document.getElementById('newfile_or_dir');
55        if (name != null && name != '' && validateFileExtension(name))
56        {
57                var fileExtension = name.split(".");
58                fileExtension = fileExtension[1];
59                if (typeof(fileExtension) == 'undefined')
60                        input_text.value = name+".html";
61                else
62                        input_text.value = name;
63                address = document.location.toString();
64                address = address.split("&");
65                document.location = address[0]+"&newfile.x=1&newfile_or_dir="+input_text.value;
66
67        }
68
69}
70
71
72/*
73 * base64.js - Base64 encoding and decoding functions
74 *
75 * Copyright (c) 2007, David Lindquist <david.lindquist@gmail.com>
76 * Released under the MIT license
77 */
78
79function base64_encode(str) {
80        var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
81        var encoded = [];
82        var c = 0;
83        while (c < str.length) {
84                var b0 = str.charCodeAt(c++);
85                var b1 = str.charCodeAt(c++);
86                var b2 = str.charCodeAt(c++);
87                var buf = (b0 << 16) + ((b1 || 0) << 8) + (b2 || 0);
88                var i0 = (buf & (63 << 18)) >> 18;
89                var i1 = (buf & (63 << 12)) >> 12;
90                var i2 = isNaN(b1) ? 64 : (buf & (63 << 6)) >> 6;
91                var i3 = isNaN(b2) ? 64 : (buf & 63);
92                encoded[encoded.length] = chars.charAt(i0);
93                encoded[encoded.length] = chars.charAt(i1);
94                encoded[encoded.length] = chars.charAt(i2);
95                encoded[encoded.length] = chars.charAt(i3);
96        }
97        return encoded.join('');
98}
99
100function base64_decode(str) {
101        var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
102        var invalid = {
103        strlen: (str.length % 4 != 0),
104        chars:  new RegExp('[^' + chars + ']').test(str),
105        equals: (/=/.test(str) && (/=[^=]/.test(str) || /={3}/.test(str)))
106        };
107        if (invalid.strlen || invalid.chars || invalid.equals)
108                throw new Error('Invalid base64 data');
109        var decoded = [];
110        var c = 0;
111        while (c < str.length) {
112                var i0 = chars.indexOf(str.charAt(c++));
113                var i1 = chars.indexOf(str.charAt(c++));
114                var i2 = chars.indexOf(str.charAt(c++));
115                var i3 = chars.indexOf(str.charAt(c++));
116                var buf = (i0 << 18) + (i1 << 12) + ((i2 & 63) << 6) + (i3 & 63);
117                var b0 = (buf & (255 << 16)) >> 16;
118                var b1 = (i2 == 64) ? -1 : (buf & (255 << 8)) >> 8;
119                var b2 = (i3 == 64) ? -1 : (buf & 255);
120                decoded[decoded.length] = String.fromCharCode(b0);
121                if (b1 >= 0) decoded[decoded.length] = String.fromCharCode(b1);
122                if (b2 >= 0) decoded[decoded.length] = String.fromCharCode(b2);
123        }
124        return decoded.join('');
125}
126
127
128function setRestricted(name){
129        var continue_set = confirm(get_lang('This property will change the visibility of all users that have access to this file, continue?'));
130        if (continue_set)
131                cExecute('./index.php?menuaction=filemanager.vfs_functions.setRestricted&file='+base64_encode(name)+'&path='+base64_encode(currentPath),setRestricted_handler);
132}
133
134function setRestricted_handler(data){
135        if (data.indexOf("True") == 0){
136                returnVal = data.split(':');
137                var img_lock = document.getElementById('restrict_'+returnVal[1]);
138                if (img_lock.src.indexOf('button_unlock') > 0)
139                {
140                        img_lock.src = img_lock.src.replace(/button_unlock/g,'button_lock');
141                        write_msg(get_lang('%1 marked as restricted',returnVal[1]));
142                }
143                else
144                {
145                        img_lock.src = img_lock.src.replace(/button_lock/g,'button_unlock');
146                        write_msg(get_lang('%1 unmarked as restricted',returnVal[1]));
147                }
148        }
149        else
150                write_error("Could not mark as restricted");
151}
152
153function presetComments(el){
154        if (permissions['edit'] == 0){
155                el.blur();
156                write_error(get_lang('You have no permission to access this file'));
157        }
158        oldValue = el.value;
159}
160
161function setComments(el){
162        if (el.value == oldValue) return;
163        var filename = base64_encode(el.id);
164        cExecute('./index.php?menuaction=filemanager.vfs_functions.editComment&file='+filename+'&comment='+base64_encode(el.value),updateComment);
165}
166
167function enterComments(e,el){
168        if (e.keyCode == KEY_ENTER)
169                el.blur();
170}
171
172function updateComment(data) {
173        var returnVal = data.split(':');
174        if (data.indexOf("True") == 0){
175                write_msg(get_lang('Updated comment for %1',returnVal[1]));
176        }
177        else
178        {
179                if (returnVal[1] == "badchar")
180                        write_error(get_lang('Comments cannot contain "%1"',returnVal[2]));
181                else
182                        write_error(get_lang('You have no permission to access this file'));
183        }
184
185}
186
187function handlerDelete(data){
188        var returnVal = data.split(':');
189        for (i=0; i < returnVal.length; i++)
190                if (returnVal[i] == 'False'){
191                        write_error(get_lang('Could not delete %1',returnVal[i+1]));
192                        return;
193                }else
194                {
195                        if (returnVal[i] != ""){
196                                write_msg(get_lang('Deleted %1',returnVal[i]));
197                                var element = document.getElementById(returnVal[i]);
198                                var pai = element.parentNode.parentNode;
199                                pai.parentNode.removeChild(pai);
200                                //Repaint stripes
201                        }
202                }
203        folderList.drawStripes();
204}
205
206function handlerRename(data) {
207        if (data == null){
208/*              var nameLink = document.createElement('A');
209                var inputName = document.getElementById('input_'+oldValue);
210                nameLink.innerHTML = oldValue;
211                nameLink.href="./index.php?menuaction=filemanager.uifilemanager.view&file="+base64_encode(oldValue)+"&path="+base64_encode(currentPath);
212                nameLink.target = "_blank";
213                nameLink.id = "name_"+oldValue;
214                inputName.parentNode.appendChild(nameLink);
215                inputName.parentNode.removeChild(inputName);*/
216                var returnVal = new Array ("True",oldValue,oldValue);
217        }
218        else
219                var returnVal = data.split(':');
220        if ( returnVal[0] == "True" ){
221                if (returnVal[1] != returnVal[2]) write_msg(get_lang('Renamed %1 to %2',returnVal[1],returnVal[2]));
222                var nameLink = document.createElement('A');
223                var inputName = document.getElementById('input_'+returnVal[1]);
224                nameLink.innerHTML = returnVal[2];
225                nameLink.href="./index.php?menuaction=filemanager.uifilemanager.view&file="+base64_encode(returnVal[2])+"&path="+base64_encode(currentPath);
226                nameLink.target = "_blank";
227                nameLink.id = "name_"+returnVal[2];
228               
229                /*Value da checkbox correspondente ao arquivo é atualizada*/
230                inputName.parentNode.parentNode.firstChild.firstChild.value = returnVal[2];
231
232                inputName.parentNode.appendChild(nameLink);
233                inputName.parentNode.removeChild(inputName);
234        }
235        else
236        {
237                if (returnVal[1] == "badchar")
238                        write_error(get_lang('File names cannot contain "%1"',returnVal[2]));
239                else
240                        if (returnVal[1] == "slashes")
241                                write_error(get_lang('File names cannot contain \\ or /'));
242                        if (returnVal[1] == "editing")
243                                write_error(get_lang('This file is being edited right now'));
244                        else
245                                write_error(get_lang('Could not rename %1 to %2', returnVal[1], returnVal[2]));
246        }
247
248}
249
250function EditColumns(param){
251        if (param == 'close')
252        {
253                var menu = document.getElementById('menu_col_pref');
254                menu.parentNode.removeChild(menu);
255                return;
256        }
257        if(param == 'save')
258        {
259                var checkBoxes = document.getElementsByName('prefView');
260                var url="";
261                for (var i=0; i < checkBoxes.length; i++)
262                {
263                        if (checkBoxes[i].checked)
264                                preferences[checkBoxes[i].value] = '1';
265                        else
266                                preferences[checkBoxes[i].value] = '0';
267                }
268                cExecute('./index.php?menuaction=filemanager.user.save_preferences&preferences='+base64_encode(serialize(preferences)),function () { toolbar.control('reload'); EditColumns('close'); })
269                return;
270        }
271        var check = function(type) { if (preferences[type] =='1') return 'checked'; else return '';};
272        var inputHTML = '<input name="prefView" type="checkbox" value="';
273        form = inputHTML+'mime_type" '+check('mime_type')+'>'+get_lang('type')+'<br>'+
274                inputHTML+'size" '+check('size')+'>'+get_lang('size')+'<br>'+
275                inputHTML+'created" '+check('created')+'>'+get_lang('created')+'<br>'+
276                inputHTML+'modified" '+check('modified')+'>'+get_lang('modified')+'<br>'+
277                inputHTML+'owner" '+check('owner')+'>'+get_lang('owner')+'<br>'+
278                inputHTML+'createdby_id" '+check('createdby_id')+'>'+get_lang('created by')+'<br>'+
279                inputHTML+'modifiedby_id" '+check('modifiedby_id')+'>'+get_lang('modified by')+'<br>'+
280        //      inputHTML+'application" '+check('application')+'>'+get_lang('application')+'<br>'+
281                inputHTML+'comment" '+check('comment')+'>'+get_lang('comment')+'<br>'+
282                inputHTML+'version" '+check('version')+'>'+get_lang('version')+'<br>'+
283                '<input value="'+get_lang('save')+'" onclick="EditColumns(\'save\')" type="button">&nbsp;'+
284                '<input value="'+get_lang('cancel')+'" onclick="EditColumns(\'close\')" type="button">';
285
286                menu = document.createElement('DIV');
287                menu.id = "menu_col_pref";
288                menu.style.left = "100px";
289                menu.style.top = "200px";
290                menu.className = 'menubox';
291                menu.style.zIndex='1';
292                menu.innerHTML = form;
293                document.getElementById('divAppboxHeader').appendChild(menu);
294}
295
296
297function searchFile(){
298        var inputText = document.getElementById('em_message_search');
299        if (inputText.value.length < 4)
300        {
301                alert(get_lang('Your search must have at least 4 characters'));
302                return;
303        }
304        cExecute('./index.php?menuaction=filemanager.uifilemanager.search&text='+inputText.value,folderList.drawSearch);
305}
306function selectAll(el){
307        checkBoxes = document.getElementsByName('fileman');
308        if (el.checked)
309                for (i=0; i < checkBoxes.length; i++)
310                        checkBoxes[i].checked = true;
311        else
312                for (i=0; i < checkBoxes.length; i++)
313                        checkBoxes[i].checked = false;
314
315}
316function borkb(size){
317                kbyte = 1024;
318                mbyte = kbyte*1024;
319                gbyte = mbyte*1024;
320                if (!size)
321                        size = 0;
322                if (size < kbyte)
323                        return size + 'B';
324                else if (size < mbyte)
325                        return parseInt(size/kbyte) + 'KB';
326                else if (size < gbyte)
327                        if (size/mbyte > 100)
328                                return (size/mbyte).toFixed(0) + 'MB';
329                        else
330                                return (size/mbyte).toFixed(1) + 'MB';
331                else
332                        return parseInt(size/gbyte).toFixed(1) + 'GB';
333}
Note: See TracBrowser for help on using the repository browser.