summaryrefslogtreecommitdiffstats
path: root/apps/files_texteditor/js/editor.js
blob: fd93ac5a7a811e8d437cc65e49f4b950644599c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
function setEditorSize(){
	// Sets the size of the text editor window.
	fillWindow($('#editor'));
}

function getFileExtension(file){
	// Extracts the file extension from the full filename
	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();
    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";
	filetype["css"] = "css";
    filetype["groovy"] = "groovy";
	filetype["html"] = "html";
    filetype["java"] = "java";
	filetype["js"] = "javascript";
    filetype["json"] = "json";
    filetype["ml"] = "ocaml";
    filetype["mli"] = "ocaml";
	filetype["pl"] = "perl";
	filetype["php"] = "php";
	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
	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){
	// 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="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);
		$('.actions').before(savebtnhtml);
	});
}
 
function bindControlEvents(){
	$("#editor_save").die('click', doFileSave).live('click', doFileSave);	
	$('#editor_close').live('click', hideFileEditor);
}

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(is_editor_shown){
		$('#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').remove();
								}, 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(); });
								}, 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(); });
						}, 2000);
				   	}
				} 
				else if(jsondata.status == 'success'){
					// Success
					$('#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(); });
					}, 2000);
				}
			}, 'json');
		giveEditorFocus();
	} else {
		$('#editor').data('editor','false');
		return;	
	}	
};

function giveEditorFocus(){
	window.aceEditor.focus();
};

function showFileEditor(dir,filename){
	if(!is_editor_shown){
		// 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");
						});
					});
				// End success
				}
				// End ajax
				});
		is_editor_shown = true;
	}
}

function hideFileEditor(){
	if(is_editor_shown){
		$('#editor').attr('editorshown','false');	
		// 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;
	}
}

$(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');
	}
	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);
			// TODO this will only work in the files app.
			showFileEditor(dir,file);
		});
	}
	// Binds the file save and close editor events to the buttons
	bindControlEvents();
});