if (! toggle_visibility) { var toggle_visibility = function(id) { var e = document.getElementById(id); if(e.style.display == 'block') e.style.display = 'none'; else e.style.display = 'block'; } } if (!fileBrowser) { var fileBrowser = Class.create(); fileBrowser.addMethods({ initialize: function() { var options = { url: '/', target: $('_fileBrowser'), spinner: '/css/images/spinner.gif', channelId: '', withFilter: false }; Object.extend(options, arguments[0]); this.target = options.target; this.spinner = options.spinner; this.url = options.url; this.withFilter = options.withFilter; this.withSection = options.withSection; this.buildFrame.bind(this)(); this.setLoading(); this.lastLoadFromFilter = false; this.aborted = 0; this.channelId = this.getChannelId(); if (!this.channelId) { this.channelId = options.channelId; } this.loadFiles(); }, loadFiles: function() { this.lastLoadFromFilter = false; this.setLoading(); var path = arguments[0] || '/'; this.currentpath = path; var qUrl = this.url + 'f/' + this.channelId + '/' + 'json/' + '?' + 'folder=' + encodeURIComponent(path); myreader = new Ajax.Request(qUrl, { onSuccess: function(t,json) { this.cleanFrame(); if (!json) { if (t.responseJSON) json = t.responseJSON; } if (!json) { this.setText('Unexptected Error!'); return false; } count = 0; if (path != '/') { var d = this.createBackWrapper("..", "background-color: #f8f8f8"); this.appendNode(d); count++; } for (var i = 0; i < json.files.length; i++) { if (count % 2) { style = "background-color: #ffffff"; } else { style = "background-color: #f8f8f8"; } var name = json.files[i].filename; var url = json.files[i].url; var size = json.files[i].size; if (json.files[i].filetype == 'folder') { var d = this.createDirectoryWrapper(name, style); } else if (json.files[i].filetype == 'back') { var d = this.createBackWrapper(name, style); } else { var d = this.createDocumentWrapper(name + " [" + size + "]",url, style); } this.appendNode(d); count++; } if (json.files.length < 1) { if (path == '/') { //Root, no files! Delete the broser! this.removeBrowser(); } } }.bind(this), onComplete: function(t,json) { this.cleanFrame(); if (!json) { if (t.responseJSON) json = t.responseJSON; } if (!json) { this.setText('Unexptected Error!'); return false; } count = 0; if (path != '/') { var d = this.createBackWrapper("..", "background-color: #f8f8f8"); this.appendNode(d); count++; } for (var i = 0; i < json.files.length; i++) { if (count % 2) { style = "background-color: #ffffff"; } else { style = "background-color: #f8f8f8"; } var name = json.files[i].filename; var url = json.files[i].url; var size = json.files[i].size; if (json.files[i].filetype == 'folder') { var d = this.createDirectoryWrapper(name, style); } else if (json.files[i].filetype == 'back') { var d = this.createBackWrapper(name, style); } else { var d = this.createDocumentWrapper(name + " [" + size + "]",url, style); } this.appendNode(d); count++; } if (json.files.length < 1) { if (path == '/') { //Root, no files! Delete the broser! this.removeBrowser(); } } }.bind(this), onFailure: function(){ this.cleanFrame(); this.setText('Failure...'); }.bind(this) }); }, loadFilesFromFilter: function(value) { if (value == this.lastLoadFilter && this.lastLoadFromFilter == true) { return; } if (!this.lastLoadFromFilter) { this.setLoading(); } this.lastLoadFromFilter = true; this.lastLoadFilter = value; var qUrl = this.url + 'l/documentfilter/' + this.channelId + '/?displaymode=json&q=' + encodeURIComponent(value + "*"); try { this.reader.transport.abort(); this.aborted++; } catch(e) { } this.loading = true; this.reader = new Ajax.Request(qUrl, { onSuccess: function(t,json) { this.cleanFrame(); if (!json) { if (t.responseJSON) json = t.responseJSON; } if (!json) { if (!this.aborted) { this.setText('Nessun file... prova a specificare un filtro diverso!'); this.loading = false; return true; } else { this.aborted--; } } json.files = json.result; if (json.files && !json.files.length) { tmp = [json.files]; json.files = tmp; } for (var i = 0; i < json.files.length; i++) { if (count % 2) { style = "background-color: #ffffff"; } else { style = "background-color: #f8f8f8"; } var name = json.files[i].filename; var url = json.files[i].url.url; var size = json.files[i].size; if (json.files[i].filetype == 'folder') { var d = this.createDirectoryWrapper(name, style); } else if (json.files[i].filetype == 'back') { var d = this.createBackWrapper(name, style); } else { var d = this.createDocumentWrapper(name + " [" + size + "]",url, style); } this.appendNode(d); count++; } this.loading = false; }.bind(this), onFailure: function(){ this.cleanFrame(); this.setText('Failure...'); this.loading = false; }.bind(this) }); }, createDocumentWrapper: function(name,url, style) { var d = document.createElement('DIV'); d.setAttribute('style',style); d.className = 'fileBrowserDocument'; d.onclick = function() { try { //document.write( this.url + 'f/' + this.channelId + '/getDocument/' + id ); window.open(url,''); } catch (e) { alert("A popup blocker prevented the file to be downloaded. Please disable your popup blockers"); } }.bind(this); d.appendChild(document.createTextNode(name)); return d; }, createDirectoryWrapper: function(name, style) { var d = document.createElement('DIV'); d.setAttribute('style',style); d.className = 'fileBrowserDirectory'; d.onclick = function() { var path = this.currentpath + name + "/"; this.loadFiles(path); //document.location = this.url + 'f/' + this.channelId + '/getDocument?' + 'id=' + id; }.bind(this); d.appendChild( document.createTextNode(name)); return d; }, createBackWrapper: function(name, style) { var d = document.createElement('DIV'); d.setAttribute('style',style); d.className = 'fileBrowserBack'; d.onclick = function() { var path = this.currentpath; path = path.split("/"); var newpath = "/"; for (var i = 1; i < (path.length - 2); i++) { newpath += path[i] + "/"; } this.loadFiles(newpath); //document.location = this.url + 'f/' + this.channelId + '/getDocument?' + 'id=' + id; }.bind(this); d.appendChild( document.createTextNode(name)); return d; }, getChannelId: function() { return ajaxCMS.channelId; }, buildFrame: function() { if (this.withSection) { var s = document.createElement('h1'); s.innerHTML = ajaxCMS.channelName; this.target.appendChild(s); } var d = document.createElement('DIV'); d.className = 'fileBrowser'; this.target.appendChild(d); this.frame = d; if (this.withFilter) { var filter = document.createElement('DIV'); filter.className = 'fileFilter'; var input = document.createElement('input'); input.setAttribute('type','text'); input.className = 'fileFilterInput'; input.onkeyup = this.onFilterChange.bind(this); input.setAttribute('title','Inserisci le parole chiave separate da uno spazio'); //input.onchange = this.onFilterChange.bind(this); this.inputField = input; this.target.appendChild(document.createElement('DIV').appendChild(document.createTextNode('Cerca in questa sezione'))); filter.appendChild(input); this.target.appendChild(filter); } }, onFilterChange: function() { value = this.inputField.value; if (value.length < 4 || value == '') { if (value == '' && this.lastLoadFromFilter) { this.loadFiles(); } return true; //wait! } this.loadFilesFromFilter(value); //console.log("Value: '" + this.inputField.value + "'"); }, cleanFrame: function() { var el = false; while( el = this.frame.firstChild ) { this.frame.removeChild(el); } }, removeBrowser: function() { var parent = this.target.parentNode; parent.removeChild(this.target); }, appendNode: function(n) { this.frame.appendChild(n); }, setText: function(t) { this.frame.appendChild(document.createTextNode(t)); }, setLoading: function() { this.cleanFrame(); var d = document.createElement('DIV'); d.className = 'spinner'; var i = document.createElement('IMG'); i.src = this.spinner; this.frame.appendChild (i); this.frame.appendChild(document.createTextNode(' Ricerca dei files...')); } }); function parseUri (str) { var o = parseUri.options, m = o.parser[o.strictMode ? "strict" : "loose"].exec(str), uri = {}, i = 14; while (i--) uri[o.key[i]] = m[i] || ""; uri[o.q.name] = {}; uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) { if ($1) uri[o.q.name][$1] = $2; }); return uri; }; parseUri.options = { strictMode: false, key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"], q: { name: "queryKey", parser: /(?:^|&)([^&=]*)=?([^&]*)/g }, parser: { strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ } }; }