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();
|
2012-03-10 18:00:57 +04:00
|
|
|
// add file extensions like this: filetype["extension"] = "filetype":
|
2012-03-11 16:03:41 +04:00
|
|
|
filetype["h"] = "c_cpp";
|
|
|
|
filetype["c"] = "c_cpp";
|
|
|
|
filetype["clj"] = "clojure";
|
|
|
|
filetype["coffee"] = "coffee"; // coffescript can be compiled to javascript
|
|
|
|
filetype["coldfusion"] = "cfc";
|
|
|
|
filetype["cpp"] = "c_cpp";
|
|
|
|
filetype["cs"] = "csharp";
|
2011-10-02 01:48:00 +04:00
|
|
|
filetype["css"] = "css";
|
2012-03-11 16:03:41 +04:00
|
|
|
filetype["groovy"] = "groovy";
|
|
|
|
filetype["haxe"] = "hx";
|
2012-04-07 17:32:00 +04:00
|
|
|
filetype["htm"] = "html";
|
2011-10-05 02:23:16 +04:00
|
|
|
filetype["html"] = "html";
|
2012-03-11 16:03:41 +04:00
|
|
|
filetype["java"] = "java";
|
2011-10-05 02:23:16 +04:00
|
|
|
filetype["js"] = "javascript";
|
2012-04-07 17:32:00 +04:00
|
|
|
filetype["jsm"] = "javascript";
|
2012-03-11 16:03:41 +04:00
|
|
|
filetype["json"] = "json";
|
|
|
|
filetype["latex"] = "latex";
|
2012-04-15 19:09:37 +04:00
|
|
|
filetype["less"] = "less";
|
2012-03-10 18:00:57 +04:00
|
|
|
filetype["ly"] = "latex";
|
|
|
|
filetype["ily"] = "latex";
|
2012-03-11 16:03:41 +04:00
|
|
|
filetype["lua"] = "lua";
|
|
|
|
filetype["markdown"] = "markdown";
|
|
|
|
filetype["md"] = "markdown";
|
|
|
|
filetype["mdown"] = "markdown";
|
|
|
|
filetype["mdwn"] = "markdown";
|
2012-03-10 18:00:57 +04:00
|
|
|
filetype["mkd"] = "markdown";
|
2012-03-11 16:03:41 +04:00
|
|
|
filetype["ml"] = "ocaml";
|
|
|
|
filetype["mli"] = "ocaml";
|
2011-10-02 01:48:00 +04:00
|
|
|
filetype["pl"] = "perl";
|
2011-10-05 02:23:16 +04:00
|
|
|
filetype["php"] = "php";
|
2012-01-02 00:04:46 +04:00
|
|
|
filetype["powershell"] = "ps1";
|
2011-10-02 01:48:00 +04:00
|
|
|
filetype["py"] = "python";
|
2011-10-05 02:23:16 +04:00
|
|
|
filetype["rb"] = "ruby";
|
2012-03-11 16:03:41 +04:00
|
|
|
filetype["scad"] = "scad"; // seems to be something like 3d model files printed with e.g. reprap
|
|
|
|
filetype["scala"] = "scala";
|
|
|
|
filetype["scss"] = "scss"; // "sassy css"
|
2012-04-15 19:09:37 +04:00
|
|
|
filetype["sh"] = "sh";
|
2012-03-11 16:03:41 +04:00
|
|
|
filetype["sql"] = "sql";
|
|
|
|
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());
|
|
|
|
});
|
2012-04-14 16:49:31 +04:00
|
|
|
}
|
2011-10-02 01:48:00 +04:00
|
|
|
}
|
|
|
|
|
2012-01-09 21:22:51 +04:00
|
|
|
function showControls(filename,writeperms){
|
2011-10-02 01:48:00 +04:00
|
|
|
// Loads the control bar at the top.
|
2012-02-15 01:49:51 +04:00
|
|
|
// Load the new toolbar.
|
2012-06-26 16:27:50 +04:00
|
|
|
var editorbarhtml = '<div id="editorcontrols" style="display: none;"><div class="crumb svg last" id="breadcrumb_file" style="background-image:url("'+OC.imagePath('core','breadcrumb.png')+'")"><p>'+filename.replace(/</, "<").replace(/>/, ">")+'</p></div>';
|
2012-02-15 01:49:51 +04:00
|
|
|
if(writeperms=="true"){
|
|
|
|
editorbarhtml += '<button id="editor_save">'+t('files_texteditor','Save')+'</button><div class="separator"></div>';
|
|
|
|
}
|
2012-04-14 16:49:31 +04:00
|
|
|
editorbarhtml += '<label for="editorseachval">Search:</label><input type="text" name="editorsearchval" id="editorsearchval"><div class="separator"></div><button id="editor_close">'+t('files_texteditor','Close')+'</button></div>';
|
2012-02-15 01:49:51 +04:00
|
|
|
// Change breadcrumb classes
|
|
|
|
$('#controls .last').removeClass('last');
|
|
|
|
$('#controls').append(editorbarhtml);
|
|
|
|
$('#editorcontrols').fadeIn('slow');
|
2011-10-02 01:48:00 +04:00
|
|
|
}
|
2012-04-14 16:49:31 +04:00
|
|
|
|
2011-10-02 01:48:00 +04:00
|
|
|
function bindControlEvents(){
|
2012-04-14 16:49:31 +04:00
|
|
|
$("#editor_save").die('click',doFileSave).live('click',doFileSave);
|
2012-04-07 01:37:28 +04:00
|
|
|
$('#editor_close').die('click',hideFileEditor).live('click',hideFileEditor);
|
2012-01-10 02:22:12 +04:00
|
|
|
$('#editorsearchval').die('keyup', doSearch).live('keyup', doSearch);
|
|
|
|
$('#clearsearchbtn').die('click', resetSearch).live('click', resetSearch);
|
|
|
|
$('#nextsearchbtn').die('click', nextSearchResult).live('click', nextSearchResult);
|
2011-11-30 02:11:42 +04:00
|
|
|
}
|
|
|
|
|
2012-01-10 01:16:41 +04:00
|
|
|
// returns true or false if the editor is in view or not
|
2011-11-30 02:11:42 +04:00
|
|
|
function editorIsShown(){
|
|
|
|
// Not working as intended. Always returns true.
|
|
|
|
return is_editor_shown;
|
2011-10-04 01:59:40 +04:00
|
|
|
}
|
|
|
|
|
2012-01-10 02:22:12 +04:00
|
|
|
//resets the search
|
|
|
|
function resetSearch(){
|
|
|
|
$('#editorsearchval').val('');
|
|
|
|
$('#nextsearchbtn').remove();
|
|
|
|
$('#clearsearchbtn').remove();
|
2012-04-14 16:49:31 +04:00
|
|
|
window.aceEditor.gotoLine(0);
|
2012-01-10 02:22:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// moves the cursor to the next search resukt
|
|
|
|
function nextSearchResult(){
|
|
|
|
window.aceEditor.findNext();
|
|
|
|
}
|
|
|
|
// Performs the initial search
|
2012-04-14 16:49:31 +04:00
|
|
|
function doSearch(){
|
2012-01-10 02:22:12 +04:00
|
|
|
// check if search box empty?
|
|
|
|
if($('#editorsearchval').val()==''){
|
2012-04-14 16:49:31 +04:00
|
|
|
// Hide clear button
|
2012-01-10 02:22:12 +04:00
|
|
|
window.aceEditor.gotoLine(0);
|
|
|
|
$('#nextsearchbtn').remove();
|
|
|
|
$('#clearsearchbtn').remove();
|
|
|
|
} else {
|
|
|
|
// New search
|
|
|
|
// Reset cursor
|
|
|
|
window.aceEditor.gotoLine(0);
|
|
|
|
// Do search
|
|
|
|
window.aceEditor.find($('#editorsearchval').val(),{
|
|
|
|
backwards: false,
|
|
|
|
wrap: false,
|
|
|
|
caseSensitive: false,
|
|
|
|
wholeWord: false,
|
|
|
|
regExp: false
|
2012-04-14 16:49:31 +04:00
|
|
|
});
|
2012-01-10 02:22:12 +04:00
|
|
|
// Show next and clear buttons
|
|
|
|
// check if already there
|
|
|
|
if($('#nextsearchbtn').length==0){
|
|
|
|
var nextbtnhtml = '<button id="nextsearchbtn">Next</button>';
|
|
|
|
var clearbtnhtml = '<button id="clearsearchbtn">Clear</button>';
|
|
|
|
$('#editorsearchval').after(nextbtnhtml).after(clearbtnhtml);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 01:16:41 +04:00
|
|
|
// Tries to save the file.
|
2011-10-04 01:59:40 +04:00
|
|
|
function doFileSave(){
|
2011-11-30 02:11:42 +04:00
|
|
|
if(editorIsShown()){
|
2012-04-02 21:27:06 +04:00
|
|
|
// Changed contents?
|
|
|
|
if($('#editor').attr('data-edited')=='true'){
|
|
|
|
// Get file path
|
|
|
|
var path = $('#editor').attr('data-dir')+'/'+$('#editor').attr('data-filename');
|
|
|
|
// Get original mtime
|
|
|
|
var mtime = $('#editor').attr('data-mtime');
|
|
|
|
// Show saving spinner
|
|
|
|
$("#editor_save").die('click',doFileSave);
|
|
|
|
$('#save_result').remove();
|
|
|
|
$('#editor_save').text(t('files_texteditor','Saving...'));
|
|
|
|
// Get the data
|
|
|
|
var filecontents = window.aceEditor.getSession().getValue();
|
|
|
|
// Send the data
|
|
|
|
$.post(OC.filePath('files_texteditor','ajax','savefile.php'), { filecontents: filecontents, path: path, mtime: mtime },function(jsondata){
|
|
|
|
if(jsondata.status!='success'){
|
|
|
|
// Save failed
|
|
|
|
$('#editor_save').text(t('files_texteditor','Save'));
|
|
|
|
$('#editor_save').after('<p id="save_result" style="float: left">Failed to save file</p>');
|
2012-04-14 16:49:31 +04:00
|
|
|
$("#editor_save").live('click',doFileSave);
|
2012-04-02 21:27:06 +04:00
|
|
|
} else {
|
2012-04-14 16:49:31 +04:00
|
|
|
// Save OK
|
2012-04-02 21:27:06 +04:00
|
|
|
// Update mtime
|
|
|
|
$('#editor').attr('data-mtime',jsondata.data.mtime);
|
2012-04-14 16:49:31 +04:00
|
|
|
$('#editor_save').text(t('files_texteditor','Save'));
|
2012-04-02 21:27:06 +04:00
|
|
|
$("#editor_save").live('click',doFileSave);
|
|
|
|
// Update titles
|
|
|
|
$('#editor').attr('data-edited', 'false');
|
2012-04-14 16:49:31 +04:00
|
|
|
$('#breadcrumb_file').text($('#editor').attr('data-filename'));
|
2012-04-02 21:27:06 +04:00
|
|
|
document.title = $('#editor').attr('data-filename')+' - ownCloud';
|
|
|
|
}
|
|
|
|
},'json');
|
|
|
|
}
|
2012-01-09 03:30:50 +04:00
|
|
|
}
|
2012-04-07 01:38:38 +04:00
|
|
|
giveEditorFocus();
|
2011-10-04 01:59:40 +04:00
|
|
|
};
|
|
|
|
|
2012-01-10 01:16:41 +04:00
|
|
|
// Gives the editor focus
|
2011-11-30 02:11:42 +04:00
|
|
|
function giveEditorFocus(){
|
|
|
|
window.aceEditor.focus();
|
|
|
|
};
|
|
|
|
|
2012-01-10 01:16:41 +04:00
|
|
|
// Loads the file editor. Accepts two parameters, dir and filename.
|
2011-10-02 01:48:00 +04:00
|
|
|
function showFileEditor(dir,filename){
|
2012-04-07 01:37:28 +04:00
|
|
|
// Delete any old editors
|
|
|
|
$('#editor').remove();
|
2011-11-30 02:11:42 +04:00
|
|
|
if(!editorIsShown()){
|
2011-10-08 18:46:38 +04:00
|
|
|
// Loads the file editor and display it.
|
2012-02-23 23:35:48 +04:00
|
|
|
$('#content').append('<div id="editor"></div>');
|
2012-01-15 00:58:16 +04:00
|
|
|
var data = $.getJSON(
|
|
|
|
OC.filePath('files_texteditor','ajax','loadfile.php'),
|
|
|
|
{file:filename,dir:dir},
|
|
|
|
function(result){
|
|
|
|
if(result.status == 'success'){
|
|
|
|
// Save mtime
|
|
|
|
$('#editor').attr('data-mtime', result.data.mtime);
|
|
|
|
// Initialise the editor
|
2012-02-15 01:49:51 +04:00
|
|
|
$('.actions,#file_action_panel').fadeOut('slow');
|
2012-01-15 00:58:16 +04:00
|
|
|
$('table').fadeOut('slow', function() {
|
2012-02-15 01:49:51 +04:00
|
|
|
// Show the control bar
|
|
|
|
showControls(filename,result.data.write);
|
2012-01-15 00:58:16 +04:00
|
|
|
// Update document title
|
2012-04-02 21:27:06 +04:00
|
|
|
document.title = filename+' - ownCloud';
|
2012-01-15 00:58:16 +04:00
|
|
|
$('#editor').text(result.data.filecontents);
|
|
|
|
$('#editor').attr('data-dir', dir);
|
|
|
|
$('#editor').attr('data-filename', filename);
|
2012-04-02 21:27:06 +04:00
|
|
|
$('#editor').attr('data-edited', 'false');
|
2012-01-15 00:58:16 +04:00
|
|
|
window.aceEditor = ace.edit("editor");
|
|
|
|
aceEditor.setShowPrintMargin(false);
|
2012-03-10 18:32:57 +04:00
|
|
|
aceEditor.getSession().setUseWrapMode(true);
|
2012-01-15 00:58:16 +04:00
|
|
|
if(result.data.write=='false'){
|
|
|
|
aceEditor.setReadOnly(true);
|
|
|
|
}
|
|
|
|
setEditorSize();
|
|
|
|
setSyntaxMode(getFileExtension(filename));
|
|
|
|
OC.addScript('files_texteditor','aceeditor/theme-clouds', function(){
|
|
|
|
window.aceEditor.setTheme("ace/theme/clouds");
|
2011-10-08 18:46:38 +04:00
|
|
|
});
|
2012-04-02 21:27:06 +04:00
|
|
|
window.aceEditor.getSession().on('change', function(){
|
|
|
|
if($('#editor').attr('data-edited')!='true'){
|
|
|
|
$('#editor').attr('data-edited', 'true');
|
2012-04-14 16:49:31 +04:00
|
|
|
$('#breadcrumb_file').text($('#breadcrumb_file').text()+' *');
|
2012-04-02 21:27:06 +04:00
|
|
|
document.title = $('#editor').attr('data-filename')+' * - ownCloud';
|
|
|
|
}
|
|
|
|
});
|
2012-05-14 00:19:20 +04:00
|
|
|
// Add the ctrl+s event
|
2012-07-26 00:33:08 +04:00
|
|
|
window.aceEditor.commands.addCommand({
|
|
|
|
name: "save",
|
|
|
|
bindKey: {
|
|
|
|
win: "Ctrl-S",
|
|
|
|
mac: "Command-S",
|
|
|
|
sender: "editor"
|
|
|
|
},
|
|
|
|
exec: function(){
|
2012-05-14 00:19:20 +04:00
|
|
|
doFileSave();
|
2012-07-26 00:33:08 +04:00
|
|
|
}
|
|
|
|
});
|
2012-01-15 00:58:16 +04:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Failed to get the file.
|
2012-04-02 20:35:11 +04:00
|
|
|
OC.dialogs.alert(result.data.message, t('files_texteditor','An error occurred!'));
|
2011-10-08 18:46:38 +04:00
|
|
|
}
|
2012-01-15 00:58:16 +04:00
|
|
|
// End success
|
|
|
|
}
|
|
|
|
// End ajax
|
|
|
|
);
|
2011-10-20 00:42:44 +04:00
|
|
|
is_editor_shown = true;
|
2011-10-08 18:46:38 +04:00
|
|
|
}
|
2011-10-02 01:48:00 +04:00
|
|
|
}
|
|
|
|
|
2012-01-10 01:16:41 +04:00
|
|
|
// Fades out the editor.
|
2011-10-02 01:48:00 +04:00
|
|
|
function hideFileEditor(){
|
2012-04-07 01:37:28 +04:00
|
|
|
if($('#editor').attr('data-edited') == 'true'){
|
2012-04-14 16:49:31 +04:00
|
|
|
// Hide, not remove
|
2012-04-07 01:37:28 +04:00
|
|
|
$('#editorcontrols').fadeOut('slow',function(){
|
|
|
|
// Check if there is a folder in the breadcrumb
|
|
|
|
if($('.crumb.ui-droppable').length){
|
|
|
|
$('.crumb.ui-droppable:last').addClass('last');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Fade out editor
|
|
|
|
$('#editor').fadeOut('slow', function(){
|
|
|
|
// Reset document title
|
|
|
|
document.title = "ownCloud";
|
|
|
|
$('.actions,#file_access_panel').fadeIn('slow');
|
2012-04-14 16:49:31 +04:00
|
|
|
$('table').fadeIn('slow');
|
2012-04-07 01:37:28 +04:00
|
|
|
});
|
|
|
|
$('#notification').text(t('files_texteditor','There were unsaved changes, click here to go back'));
|
|
|
|
$('#notification').data('reopeneditor',true);
|
|
|
|
$('#notification').fadeIn();
|
|
|
|
is_editor_shown = false;
|
|
|
|
} else {
|
|
|
|
// Remove editor
|
|
|
|
$('#editorcontrols').fadeOut('slow',function(){
|
|
|
|
$(this).remove();
|
|
|
|
$(".crumb:last").addClass('last');
|
|
|
|
});
|
|
|
|
// Fade out editor
|
|
|
|
$('#editor').fadeOut('slow', function(){
|
|
|
|
$(this).remove();
|
|
|
|
// Reset document title
|
|
|
|
document.title = "ownCloud";
|
|
|
|
$('.actions,#file_access_panel').fadeIn('slow');
|
2012-04-14 16:49:31 +04:00
|
|
|
$('table').fadeIn('slow');
|
2012-04-07 01:37:28 +04:00
|
|
|
});
|
|
|
|
is_editor_shown = false;
|
|
|
|
}
|
2011-10-02 01:48:00 +04:00
|
|
|
}
|
|
|
|
|
2012-04-07 01:37:28 +04:00
|
|
|
// Reopens the last document
|
|
|
|
function reopenEditor(){
|
|
|
|
$('.actions,#file_action_panel').fadeOut('slow');
|
|
|
|
$('table').fadeOut('slow', function(){
|
|
|
|
$('#controls .last').not('#breadcrumb_file').removeClass('last');
|
|
|
|
$('#editor').fadeIn('fast');
|
|
|
|
$('#editorcontrols').fadeIn('fast', function(){
|
2012-01-10 01:16:41 +04:00
|
|
|
|
2012-04-14 16:49:31 +04:00
|
|
|
});
|
2012-04-07 01:37:28 +04:00
|
|
|
});
|
|
|
|
is_editor_shown = true;
|
2011-11-12 00:46:06 +04:00
|
|
|
}
|
|
|
|
|
2012-01-10 01:16:41 +04:00
|
|
|
// resizes the editor window
|
2011-10-02 01:48:00 +04:00
|
|
|
$(window).resize(function() {
|
2011-10-05 19:10:11 +04:00
|
|
|
setEditorSize();
|
2011-10-05 02:23:16 +04:00
|
|
|
});
|
2011-10-20 00:42:44 +04:00
|
|
|
var is_editor_shown = false;
|
2011-10-05 04:36:27 +04:00
|
|
|
$(document).ready(function(){
|
|
|
|
if(typeof FileActions!=='undefined'){
|
2012-07-26 00:33:08 +04:00
|
|
|
FileActions.register('text','Edit', FileActions.PERMISSION_READ, '',function(filename){
|
2011-10-05 04:36:27 +04:00
|
|
|
showFileEditor($('#dir').val(),filename);
|
|
|
|
});
|
|
|
|
FileActions.setDefault('text','Edit');
|
2012-07-26 00:33:08 +04:00
|
|
|
FileActions.register('application/xml','Edit', FileActions.PERMISSION_READ, '',function(filename){
|
2011-10-05 04:36:27 +04:00
|
|
|
showFileEditor($('#dir').val(),filename);
|
|
|
|
});
|
|
|
|
FileActions.setDefault('application/xml','Edit');
|
|
|
|
}
|
2011-10-05 04:44:24 +04:00
|
|
|
OC.search.customResults.Text=function(row,item){
|
2012-05-11 05:44:04 +04:00
|
|
|
var text=item.link.substr(item.link.indexOf('?file=')+6);
|
2011-10-05 04:44:24 +04:00
|
|
|
var a=row.find('a');
|
|
|
|
a.data('file',text);
|
|
|
|
a.attr('href','#');
|
|
|
|
a.click(function(){
|
2012-05-11 05:44:04 +04:00
|
|
|
var pos=text.lastIndexOf('/')
|
2012-05-11 06:02:21 +04:00
|
|
|
var file=text.substr(pos + 1);
|
2012-05-11 05:44:04 +04:00
|
|
|
var dir=text.substr(0,pos);
|
2011-10-05 04:44:24 +04:00
|
|
|
showFileEditor(dir,file);
|
|
|
|
});
|
2012-02-23 23:35:48 +04:00
|
|
|
};
|
2012-01-10 01:16:41 +04:00
|
|
|
// Binds the file save and close editor events, and gotoline button
|
2011-10-05 19:10:11 +04:00
|
|
|
bindControlEvents();
|
2012-02-23 23:35:48 +04:00
|
|
|
$('#editor').remove();
|
2012-04-07 01:37:28 +04:00
|
|
|
$('#notification').click(function(){
|
|
|
|
if($('#notification').data('reopeneditor'))
|
|
|
|
{
|
|
|
|
reopenEditor();
|
|
|
|
}
|
|
|
|
$('#notification').fadeOut();
|
|
|
|
});
|
2011-11-30 02:11:42 +04:00
|
|
|
});
|