server/apps/files_texteditor/js/editor.js
2011-10-04 16:31:26 +01:00

174 lines
No EOL
5.1 KiB
JavaScript

function setEditorSize(){
// Sets the size of the text editor window.
$('#editor').css('height', $(window).height()-81);
$('#editor').css('width', $(window).width()-160);
$('#editor').css('padding-top', '40px');
}
function getFileExtension(file){
var parts=file.split('.');
return parts[parts.length-1];
}
function setSyntaxMode(ext){
// Loads the syntax mode files and tells the editor
var filetype = new Array()
// Todo finish these
filetype["php"] = "php";
filetype["html"] = "html";
filetype["rb"] = "ruby";
filetype["css"] = "css";
filetype["pl"] = "perl";
filetype["py"] = "python";
filetype["xml"] = "xml";
filetype["js"] = "javascript";
if(filetype[ext]!=null){
// Then it must be in the array, so load the custom syntax mode
// Set the syntax mode
OC.addScript('files_texteditor','aceeditor/mode-'+filetype[ext], function(){
var SyntaxMode = require("ace/mode/"+filetype[ext]).Mode;
window.aceEditor.getSession().setMode(new SyntaxMode());
});
}
}
function showControls(filename){
// Loads the control bar at the top.
$('.actions,#file_action_panel').fadeOut('slow').promise().done(function() {
// Load the new toolbar.
var savebtnhtml = '<input type="button" id="editor_save" value="'+t('files_texteditor','Save')+'">';
var html = '<input type="button" id="editor_close" value="'+t('files_texteditor','Close Editor')+'">';
$('#controls').append(html);
$('#editorbar').fadeIn('slow');
var breadcrumbhtml = '<div class="crumb svg" style="background-image:url(&quot;/core/img/breadcrumb.png&quot;)"><a href="#">'+filename+'</a></div>';
$('.actions').before(breadcrumbhtml);
$('.actions').before(savebtnhtml);
});
}
function bindControlEvents(){
$("#editor_save").live('click',function() {
doFileSave();
});
$('#editor_close').live('click',function() {
hideFileEditor();
});
$(window).keypress(function(event) {
if (!(event.which == 115 && (event.ctrlKey || event.metaKey)) && !(event.which == 19)) return true;
event.preventDefault();
alert("Saved file");
doFileSave();
return false;
});
}
function editorIsShown(){
if($('#editor').length!=0){
return true;
} else {
return false;
}
}
function updateSessionFileHash(path){
$.get(OC.filePath('files_texteditor','ajax','loadfile.php'),
{ path: path },
function(jsondata){
if(jsondata.status=='failure'){
alert('Failed to update session file hash.');
}
}, "json");}
function doFileSave(){
if(editorIsShown()){
$('#editor_save').val(t('files_texteditor','Saving')+'...');
var filecontents = window.aceEditor.getSession().getValue();
var dir = $('#editor').attr('data-dir');
var file = $('#editor').attr('data-filename');
$.post(OC.filePath('files_texteditor','ajax','savefile.php'), { filecontents: filecontents, file: file, dir: dir },function(jsondata){
if(jsondata.status == 'failure'){
var answer = confirm(jsondata.data.message);
if(answer){
$.post(OC.filePath('files_texteditor','ajax','savefile.php'),{ filecontents: filecontents, file: file, dir: dir, force: 'true' },function(jsondata){
if(jsondata.status =='success'){
$('#editor_save').val(t('files_texteditor','Save'));
}
else {
// Save error
alert(jsondata.data.message);
}
}, 'json');
}
else {
// Don't save!
$('#editor_save').effect("highlight", {color:'#FF5757'}, 3000);
$('#editor_save').val(t('files_texteditor','Save'));
}
}
else if(jsondata.status == 'success'){
// Success
$('#editor_save').val(t('files_texteditor','Save'));
}
}, 'json');
giveEditorFocus();
} else {
return;
}
};
function giveEditorFocus(){
window.aceEditor.focus();
};
function showFileEditor(dir,filename){
// Loads the file editor and display it.
var data = $.ajax({
url: OC.filePath('files','ajax','download.php')+'?files='+encodeURIComponent(filename)+'&dir='+encodeURIComponent(dir),
complete: function(data){
// Initialise the editor
updateSessionFileHash(dir+'/'+filename);
showControls(filename);
$('table').fadeOut('slow', function() {
$('#editor').text(data.responseText);
// encodeURIComponenet?
$('#editor').attr('data-dir', dir);
$('#editor').attr('data-filename', filename);
window.aceEditor = ace.edit("editor");
aceEditor.setShowPrintMargin(false);
setSyntaxMode(getFileExtension(filename));
OC.addScript('files_texteditor','aceeditor/theme-clouds', function(){
window.aceEditor.setTheme("ace/theme/clouds");
});
});
bindControlEvents();
// End success
}
// End ajax
});
setEditorSize();
}
function hideFileEditor(){
// Fade out controls
$('#editor_close').fadeOut('slow');
// Fade out breadcrumb
$('.actions').prev().fadeOut('slow');
// Fade out editor
$('#editor').fadeOut('slow', function(){
$('#editor_close').remove();
$('#editor').remove();
$('.actions').prev().remove();
var editorhtml = '<div id="editor"></div>';
$('table').after(editorhtml);
$('.actions,#file_access_panel').fadeIn('slow');
$('table').fadeIn('slow');
});
}
$(window).resize(function() {
setEditorSize();
});