2011-10-02 01:48:00 +04:00
|
|
|
function setEditorSize(){
|
|
|
|
// Sets the size of the text editor window.
|
2011-10-04 23:01:05 +04:00
|
|
|
fillWindow($('#editor'));
|
2011-10-02 01:48:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getFileExtension(file){
|
|
|
|
var parts=file.split('.');
|
|
|
|
return parts[parts.length-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
function setSyntaxMode(ext){
|
|
|
|
// Loads the syntax mode files and tells the editor
|
2011-10-04 23:01:05 +04:00
|
|
|
var filetype = new Array();
|
2011-10-02 01:48:00 +04:00
|
|
|
// 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());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-04 19:31:26 +04:00
|
|
|
function showControls(filename){
|
2011-10-02 01:48:00 +04:00
|
|
|
// Loads the control bar at the top.
|
2011-10-04 01:59:40 +04:00
|
|
|
$('.actions,#file_action_panel').fadeOut('slow').promise().done(function() {
|
2011-10-02 01:48:00 +04:00
|
|
|
// Load the new toolbar.
|
2011-10-04 19:31:26 +04:00
|
|
|
var savebtnhtml = '<input type="button" id="editor_save" value="'+t('files_texteditor','Save')+'">';
|
2011-10-04 22:07:22 +04:00
|
|
|
var html = '<input type="button" id="editor_close" value="Close">';
|
2011-10-04 19:31:26 +04:00
|
|
|
$('#controls').append(html);
|
|
|
|
$('#editorbar').fadeIn('slow');
|
2011-10-04 23:01:05 +04:00
|
|
|
var breadcrumbhtml = '<div class="crumb svg" id="breadcrumb_file" style="background-image:url("../core/img/breadcrumb.png")"><a href="#">'+filename+'</a></div>';
|
2011-10-04 01:59:40 +04:00
|
|
|
$('.actions').before(breadcrumbhtml);
|
2011-10-04 19:31:26 +04:00
|
|
|
$('.actions').before(savebtnhtml);
|
2011-10-02 01:48:00 +04:00
|
|
|
});
|
|
|
|
}
|
2011-10-04 01:59:40 +04:00
|
|
|
|
2011-10-02 01:48:00 +04:00
|
|
|
function bindControlEvents(){
|
2011-10-04 01:59:40 +04:00
|
|
|
$("#editor_save").live('click',function() {
|
|
|
|
doFileSave();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#editor_close').live('click',function() {
|
|
|
|
hideFileEditor();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function editorIsShown(){
|
|
|
|
if($('#editor').length!=0){
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-04 02:28:47 +04:00
|
|
|
function updateSessionFileHash(path){
|
2011-10-04 02:41:32 +04:00
|
|
|
$.get(OC.filePath('files_texteditor','ajax','loadfile.php'),
|
2011-10-04 02:28:47 +04:00
|
|
|
{ path: path },
|
|
|
|
function(jsondata){
|
|
|
|
if(jsondata.status=='failure'){
|
|
|
|
alert('Failed to update session file hash.');
|
|
|
|
}
|
|
|
|
}, "json");}
|
|
|
|
|
2011-10-04 01:59:40 +04:00
|
|
|
function doFileSave(){
|
|
|
|
if(editorIsShown()){
|
2011-10-04 20:43:13 +04:00
|
|
|
$('#editor_save').after('<img id="saving_icon" src="'+OC.filePath('core','img','loading.gif')+'"></img>');
|
2011-10-02 01:48:00 +04:00
|
|
|
var filecontents = window.aceEditor.getSession().getValue();
|
|
|
|
var dir = $('#editor').attr('data-dir');
|
2011-10-04 01:59:40 +04:00
|
|
|
var file = $('#editor').attr('data-filename');
|
2011-10-04 02:41:32 +04:00
|
|
|
$.post(OC.filePath('files_texteditor','ajax','savefile.php'), { filecontents: filecontents, file: file, dir: dir },function(jsondata){
|
2011-10-04 01:59:40 +04:00
|
|
|
|
2011-10-02 01:48:00 +04:00
|
|
|
if(jsondata.status == 'failure'){
|
|
|
|
var answer = confirm(jsondata.data.message);
|
|
|
|
if(answer){
|
2011-10-04 02:41:32 +04:00
|
|
|
$.post(OC.filePath('files_texteditor','ajax','savefile.php'),{ filecontents: filecontents, file: file, dir: dir, force: 'true' },function(jsondata){
|
2011-10-02 01:48:00 +04:00
|
|
|
if(jsondata.status =='success'){
|
2011-10-04 20:43:13 +04:00
|
|
|
$('#saving_icon').remove();
|
2011-10-02 01:48:00 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Save error
|
|
|
|
alert(jsondata.data.message);
|
|
|
|
}
|
|
|
|
}, 'json');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Don't save!
|
2011-10-04 20:43:13 +04:00
|
|
|
$('#editor_save').effect("highlight", {color:'#FF5757'}, 1000);
|
2011-10-02 01:48:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(jsondata.status == 'success'){
|
|
|
|
// Success
|
2011-10-04 20:43:13 +04:00
|
|
|
$('#saving_icon').remove();
|
2011-10-02 01:48:00 +04:00
|
|
|
}
|
|
|
|
}, 'json');
|
2011-10-04 01:59:40 +04:00
|
|
|
giveEditorFocus();
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function giveEditorFocus(){
|
|
|
|
window.aceEditor.focus();
|
|
|
|
};
|
2011-10-02 01:48:00 +04:00
|
|
|
|
|
|
|
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
|
2011-10-04 02:28:47 +04:00
|
|
|
updateSessionFileHash(dir+'/'+filename);
|
2011-10-04 19:31:26 +04:00
|
|
|
showControls(filename);
|
2011-10-02 01:48:00 +04:00
|
|
|
$('table').fadeOut('slow', function() {
|
2011-10-04 18:27:29 +04:00
|
|
|
$('#editor').text(data.responseText);
|
2011-10-02 01:48:00 +04:00
|
|
|
// encodeURIComponenet?
|
|
|
|
$('#editor').attr('data-dir', dir);
|
|
|
|
$('#editor').attr('data-filename', filename);
|
|
|
|
window.aceEditor = ace.edit("editor");
|
|
|
|
aceEditor.setShowPrintMargin(false);
|
2011-10-04 23:01:05 +04:00
|
|
|
setEditorSize();
|
2011-10-02 01:48:00 +04:00
|
|
|
setSyntaxMode(getFileExtension(filename));
|
|
|
|
OC.addScript('files_texteditor','aceeditor/theme-clouds', function(){
|
|
|
|
window.aceEditor.setTheme("ace/theme/clouds");
|
|
|
|
});
|
|
|
|
});
|
2011-10-04 23:01:05 +04:00
|
|
|
bindControlEvents();
|
2011-10-02 01:48:00 +04:00
|
|
|
// End success
|
|
|
|
}
|
|
|
|
// End ajax
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function hideFileEditor(){
|
2011-10-04 01:59:40 +04:00
|
|
|
// Fade out controls
|
2011-10-04 19:31:26 +04:00
|
|
|
$('#editor_close').fadeOut('slow');
|
2011-10-04 22:07:22 +04:00
|
|
|
// Fade out the save button
|
|
|
|
$('#editor_save').fadeOut('slow');
|
2011-10-04 01:59:40 +04:00
|
|
|
// Fade out breadcrumb
|
2011-10-04 22:07:22 +04:00
|
|
|
$('#breadcrumb_file').fadeOut('slow');
|
2011-10-04 01:59:40 +04:00
|
|
|
// Fade out editor
|
2011-10-02 01:48:00 +04:00
|
|
|
$('#editor').fadeOut('slow', function(){
|
2011-10-04 19:31:26 +04:00
|
|
|
$('#editor_close').remove();
|
2011-10-04 01:59:40 +04:00
|
|
|
$('#editor').remove();
|
|
|
|
$('.actions').prev().remove();
|
|
|
|
var editorhtml = '<div id="editor"></div>';
|
|
|
|
$('table').after(editorhtml);
|
2011-10-02 01:48:00 +04:00
|
|
|
$('.actions,#file_access_panel').fadeIn('slow');
|
|
|
|
$('table').fadeIn('slow');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$(window).resize(function() {
|
|
|
|
setEditorSize();
|
|
|
|
});
|