nextcloud/apps/files_texteditor/js/editor.js

252 lines
8.0 KiB
JavaScript
Raw Normal View History

2011-10-02 01:48:00 +04:00
function setEditorSize(){
// Sets the size of the text editor window.
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
var filetype = new Array();
// Todo finish these
filetype["h"] = "c_cpp";
filetype["c"] = "c_cpp";
filetype["cpp"] = "c_cpp";
filetype["clj"] = "clojure";
filetype["coffee"] = "coffee"; // coffescript can be compiled to javascript
filetype["cs"] = "csharp";
2011-10-02 01:48:00 +04:00
filetype["css"] = "css";
filetype["groovy"] = "groovy";
filetype["html"] = "html";
filetype["java"] = "java";
filetype["js"] = "javascript";
filetype["json"] = "json";
filetype["ml"] = "ocaml";
filetype["mli"] = "ocaml";
2011-10-02 01:48:00 +04:00
filetype["pl"] = "perl";
filetype["php"] = "php";
2011-10-02 01:48:00 +04:00
filetype["py"] = "python";
filetype["rb"] = "ruby";
filetype["scad"] = "scad"; // seems to be something like 3d model files printed with e.g. reprap
filetype["scala"] = "scala";
filetype["scss"] = "scss"; // "sassy css"
filetype["svg"] = "svg";
filetype["textile"] = "textile"; // related to markdown
2011-10-02 01:48:00 +04:00
filetype["xml"] = "xml";
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){
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.
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">';
$('#controls').append(html);
$('#editorbar').fadeIn('slow');
var breadcrumbhtml = '<div class="crumb svg" id="breadcrumb_file" style="background-image:url(&quot;../core/img/breadcrumb.png&quot;)"><p>'+filename+'</p></div>';
$('.actions').before(breadcrumbhtml).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(){
$("#editor_save").die('click',doFileSave).live('click',doFileSave);
$('#editor_close').die('click',hideFileEditor).live('click',hideFileEditor);
}
function editorIsShown(){
// Not working as intended. Always returns true.
return is_editor_shown;
2011-10-04 01:59:40 +04:00
}
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 02:28:47 +04:00
2011-10-04 01:59:40 +04:00
function doFileSave(){
if(editorIsShown()){
$("#editor_save").die('click',doFileSave);
$('#editor_save').after('<img id="saving_icon" src="'+OC.filePath('core','img','loading.gif')+'"></img>');
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'){
$('#saving_icon').remove();
$('#editor_save').after('<p id="save_result" style="float: left">Saved!</p>')
setTimeout(function() {
$('#save_result').fadeOut('slow',function(){
$(this).remove();
$("#editor_save").live('click',doFileSave);
});
}, 2000);
}
else {
// Save error
$('#saving_icon').remove();
$('#editor_save').after('<p id="save_result" style="float: left">Failed!</p>');
setTimeout(function() {
$('#save_result').fadeOut('slow',function(){
$(this).remove();
$("#editor_save").live('click',doFileSave);
});
}, 2000);
}
}, 'json');
}
else {
// Don't save!
$('#saving_icon').remove();
// Temporary measure until we get a tick icon
$('#editor_save').after('<p id="save_result" style="float: left">Saved!</p>');
setTimeout(function() {
$('#save_result').fadeOut('slow',function(){
$(this).remove();
$("#editor_save").live('click',doFileSave);
});
}, 2000);
}
2011-10-02 01:48:00 +04:00
}
else if(jsondata.status == 'success'){
// Success
2011-10-06 00:59:03 +04:00
$('#saving_icon').remove();
// Temporary measure until we get a tick icon
$('#editor_save').after('<p id="save_result" style="float: left">Saved!</p>');
setTimeout(function() {
$('#save_result').fadeOut('slow',function(){
$(this).remove();
$("#editor_save").live('click',doFileSave);
});
2011-10-06 00:59:03 +04:00
}, 2000);
}
}, 'json');
giveEditorFocus();
} else {
return;
2011-10-04 01:59:40 +04:00
}
};
function giveEditorFocus(){
window.aceEditor.focus();
};
2011-10-02 01:48:00 +04:00
function showFileEditor(dir,filename){
if(!editorIsShown()){
// 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);
setEditorSize();
setSyntaxMode(getFileExtension(filename));
OC.addScript('files_texteditor','aceeditor/theme-clouds', function(){
window.aceEditor.setTheme("ace/theme/clouds");
});
2011-10-02 01:48:00 +04:00
});
// End success
}
// End ajax
2011-10-02 01:48:00 +04:00
});
is_editor_shown = true;
}
2011-10-02 01:48:00 +04:00
}
function hideFileEditor(){
// Fade out controls
$('#editor_close').fadeOut('slow');
// Fade out the save button
$('#editor_save').fadeOut('slow');
// Fade out breadcrumb
$('#breadcrumb_file').fadeOut('slow', function(){ $(this).remove();});
// Fade out editor
$('#editor').fadeOut('slow', function(){
$('#editor_close').remove();
$('#editor_save').remove();
$('#editor').remove();
var editorhtml = '<div id="editor"></div>';
$('table').after(editorhtml);
$('.actions,#file_access_panel').fadeIn('slow');
$('table').fadeIn('slow');
});
is_editor_shown = false;
2011-10-02 01:48:00 +04:00
}
// Keyboard Shortcuts
var ctrlBtn = false;
function checkForCtrlKey(e){
if(e.which == 17 || e.which == 91) ctrlBtn=false;
}
function checkForSaveKeyPress(e){
if(e.which == 17 || e.which == 91) ctrlBtn=true;
if(e.which == 83 && ctrlBtn == true) {
e.preventDefault();
$('#editor_save').trigger('click');
return false;
}
}
2011-10-02 01:48:00 +04:00
$(window).resize(function() {
setEditorSize();
});
var is_editor_shown = false;
$(document).ready(function(){
if(typeof FileActions!=='undefined'){
FileActions.register('text','Edit','',function(filename){
showFileEditor($('#dir').val(),filename);
});
FileActions.setDefault('text','Edit');
FileActions.register('application/xml','Edit','',function(filename){
showFileEditor($('#dir').val(),filename);
});
FileActions.setDefault('application/xml','Edit');
}
2011-10-05 04:44:24 +04:00
OC.search.customResults.Text=function(row,item){
var text=item.link.substr(item.link.indexOf('file=')+5);
var a=row.find('a');
a.data('file',text);
a.attr('href','#');
a.click(function(){
var file=text.split('/').pop();
var dir=text.substr(0,text.length-file.length-1);
showFileEditor(dir,file);
});
}
// Binds the file save and close editor events
bindControlEvents();
// Binds the save keyboard shortcut events
$(document).unbind('keyup').bind('keyup',checkForCtrlKey).unbind('keydown').bind('keydown',checkForSaveKeyPress);
});