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

Revision 1791, 10.4 KB checked in by amuller, 14 years ago (diff)

Ticket #806 - Melhorias no módulos gerenciador de arquivos do expresso livre

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        oldComment = el.value;
159}
160
161function setComments(el){
162        if (el.value == oldComment){
163                el.value = oldComment;
164                return;
165        }
166        var filename = base64_encode(el.id);
167        cExecute('./index.php?menuaction=filemanager.vfs_functions.editComment&file='+filename+'&comment='+base64_encode(el.value),updateComment);
168}
169
170function enterComments(e,el){
171        if (e.keyCode == KEY_ENTER)
172                el.blur();
173}
174
175function updateComment(data) {
176        var returnVal = data.split(':');
177        if (data.indexOf("True") == 0){
178                write_msg(get_lang('Updated comment for %1',returnVal[1]));
179        }
180        else
181        {
182                if (returnVal[1] == "badchar")
183                        write_error(get_lang('Comments cannot contain "%1"',returnVal[2]));
184                else
185                        write_error(get_lang('You have no permission to access this file'));
186        }
187
188}
189
190function handlerDelete(data){
191        var returnVal = data.split(':');
192        for (i=0; i < returnVal.length; i++)
193                if (returnVal[i] == 'False'){
194                        write_error(get_lang('Could not delete %1',returnVal[i+1]));
195                        return;
196                }else
197                {
198                        if (returnVal[i] != ""){
199                                write_msg(get_lang('Deleted %1',returnVal[i]));
200                                var element = document.getElementById(returnVal[i]);
201                                var pai = element.parentNode.parentNode;
202                                pai.parentNode.removeChild(pai);
203                                //Repaint stripes
204                        }
205                }
206        folderList.drawStripes();
207}
208
209function handlerRename(data) {
210        var returnVal = data.split(':');
211        if (data.indexOf("True") == 0){
212                write_msg(get_lang('Renamed %1 to %2',returnVal[1],returnVal[2]));
213                var nameLink = document.createElement('A');
214                var inputName = document.getElementById('input_'+returnVal[1]);
215                nameLink.innerHTML = returnVal[2];
216                nameLink.href="./index.php?menuaction=filemanager.uifilemanager.view&file="+base64_encode(returnVal[2])+"&path="+base64_encode(currentPath);
217                nameLink.target = "_blank";
218                nameLink.id = "name_"+returnVal[2];
219               
220                /*Value da checkbox correspondente ao arquivo é atualizada*/
221                inputName.parentNode.parentNode.firstChild.firstChild.value = returnVal[2];
222
223                inputName.parentNode.appendChild(nameLink);
224                inputName.parentNode.removeChild(inputName);
225        }
226        else
227        {
228                if (returnVal[1] == "badchar")
229                        write_error(get_lang('File names cannot contain "%1"',returnVal[2]));
230                else
231                        if (returnVal[1] == "slashes")
232                                write_error(get_lang('File names cannot contain \\ or /'));
233                        if (returnVal[1] == "editing")
234                                write_error(get_lang('This file is being edited right now'));
235                        else
236                                write_error(get_lang('Could not rename %1 to %2', returnVal[1], returnVal[2]));
237        }
238
239}
240
241function EditColumns(param){
242        if (param == 'close')
243        {
244                var menu = document.getElementById('menu_col_pref');
245                menu.parentNode.removeChild(menu);
246                return;
247        }
248        if(param == 'save')
249        {
250                var checkBoxes = document.getElementsByName('prefView');
251                var url="";
252                for (var i=0; i < checkBoxes.length; i++)
253                {
254                        if (checkBoxes[i].checked)
255                                preferences[checkBoxes[i].value] = '1';
256                        else
257                                preferences[checkBoxes[i].value] = '0';
258                }
259                cExecute('./index.php?menuaction=filemanager.user.save_preferences&preferences='+base64_encode(serialize(preferences)),function () { toolbar.control('reload'); EditColumns('close'); })
260                return;
261        }
262        var check = function(type) { if (preferences[type] =='1') return 'checked'; else return '';};
263        var inputHTML = '<input name="prefView" type="checkbox" value="';
264        form = inputHTML+'mime_type" '+check('mime_type')+'>'+get_lang('type')+'<br>'+
265                inputHTML+'size" '+check('size')+'>'+get_lang('size')+'<br>'+
266                inputHTML+'created" '+check('created')+'>'+get_lang('created')+'<br>'+
267                inputHTML+'modified" '+check('modified')+'>'+get_lang('modified')+'<br>'+
268                inputHTML+'owner" '+check('owner')+'>'+get_lang('owner')+'<br>'+
269                inputHTML+'createdby_id" '+check('createdby_id')+'>'+get_lang('created by')+'<br>'+
270                inputHTML+'modifiedby_id" '+check('modifiedby_id')+'>'+get_lang('modified by')+'<br>'+
271        //      inputHTML+'application" '+check('application')+'>'+get_lang('application')+'<br>'+
272                inputHTML+'comment" '+check('comment')+'>'+get_lang('comment')+'<br>'+
273                inputHTML+'version" '+check('version')+'>'+get_lang('version')+'<br>'+
274                '<input value="'+get_lang('save')+'" onclick="EditColumns(\'save\')" type="button">&nbsp;'+
275                '<input value="'+get_lang('cancel')+'" onclick="EditColumns(\'close\')" type="button">';
276
277                menu = document.createElement('DIV');
278                menu.id = "menu_col_pref";
279                menu.style.left = "100px";
280                menu.style.top = "200px";
281                menu.className = 'menubox';
282                menu.style.zIndex='1';
283                menu.innerHTML = form;
284                document.getElementById('divAppboxHeader').appendChild(menu);
285}
286
287
288function searchFile(){
289        var inputText = document.getElementById('em_message_search');
290        if (inputText.value.length < 4)
291        {
292                alert(get_lang('Your search must have at least 4 characters'));
293                return;
294        }
295        cExecute('./index.php?menuaction=filemanager.uifilemanager.search&text='+inputText.value,folderList.drawSearch);
296}
297function selectAll(el){
298        checkBoxes = document.getElementsByName('fileman');
299        if (el.checked)
300                for (i=0; i < checkBoxes.length; i++)
301                        checkBoxes[i].checked = true;
302        else
303                for (i=0; i < checkBoxes.length; i++)
304                        checkBoxes[i].checked = false;
305
306}
307function borkb(size){
308                kbyte = 1024;
309                mbyte = kbyte*1024;
310                gbyte = mbyte*1024;
311                if (!size)
312                        size = 0;
313                if (size < kbyte)
314                        return size + 'B';
315                else if (size < mbyte)
316                        return parseInt(size/kbyte) + 'KB';
317                else if (size < gbyte)
318                        if (size/mbyte > 100)
319                                return (size/mbyte).toFixed(0) + 'MB';
320                        else
321                                return (size/mbyte).toFixed(1) + 'MB';
322                else
323                        return parseInt(size/gbyte).toFixed(1) + 'GB';
324}
Note: See TracBrowser for help on using the repository browser.