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

Revision 1785, 10.0 KB checked in by amuller, 14 years ago (diff)

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