summaryrefslogtreecommitdiffstats
path: root/apps/files_textviewer/js/textviewer.js
blob: b1a564df9bb4693c8704e4a86e3da2b030ae5003 (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
TextViewer=new Object();

TextViewer.type='';
TextViewer.types={
	'ac3':'shBrushAS3',
	'applescript':'shBrushAppleScript',
	'bash':'shBrushBash',
	'sh':'shBrushBash',
	'csharp':'shBrushCSharp',
	'coldfusion':'shBrushColdFusion',
	'cpp':'shBrushCpp',
	'css':'shBrushCss',
	'delphi':'shBrushDelphi',
	'diff':'shBrushDiff',
	'erlang':'shBrushErlang',
	'groovy':'shBrushGroovy',
	'javascript':'shBrushJScript',
	'js':'shBrushJScript',
	'java':'shBrushJava',
	'javafx':'shBrushJavaFX',
	'perl':'shBrushPerl',
	'php':'shBrushPhp',
	'plain':'shBrushPlain',
	'powershell':'shBrushPowerShell',
	'python':'shBrushPython',
	'py':'shBrushPython',
	'ruby':'shBrushRuby',
	'sass':'shBrushSass',
	'scala':'shBrushScala',
	'sql':'shBrushSql',
	'vb':'shBrushVb',
	'xml':'shBrushXml',
}

TextViewer.load=function(ready){
	if(!TextViewer.load.done){
		OC.addStyle('files_textviewer','syntaxhighlighter/shCoreDefault');
		OC.addStyle('files_textviewer','syntaxhighlighter/shCore');
		OC.addStyle('files_textviewer','syntaxhighlighter/shThemeDefault');
		OC.addStyle('files_textviewer','style');
		OC.addScript('files_textviewer','syntaxhighlighter/shCore',function(){
			if(ready){
				ready();
			}
		});
	}else if(ready){
		ready();
	}
}
TextViewer.load.done=false;

TextViewer.showText=function(dir,file){
	var type;
	var parts=file.split('.');
	var ext=parts[parts.length-1];
	if(TextViewer.types[ext]){
		type=ext;
	}else{
		type='plain';
	}
	TextViewer.type=type;
	var div=$('<div id="textframe" class="center"></div>');
	div.click(TextViewer.hideText);
	TextViewer.textFrame=$('<div></div>');
	TextViewer.textFrame.click(function(event){
		event.stopPropagation();
	});
	TextViewer.textFrame.pre=$('<pre></pre>');
	div.append(TextViewer.textFrame);
	TextViewer.textFrame.append(TextViewer.textFrame.pre);
	$('body').append(div);
	TextViewer.loadHighlighter(function(){
		$.ajax({
			url: OC.filePath('files','ajax','download.php')+'?files='+encodeURIComponent(file)+'&dir='+encodeURIComponent(dir),
			complete: function(text){
				TextViewer.textFrame.pre.text(text.responseText);
				TextViewer.textFrame.pre.attr('class','brush: '+TextViewer.type+';');
				SyntaxHighlighter.highlight(null,TextViewer.textFrame.pre[0]);
			}
		});
	});
}

TextViewer.hideText=function(){
	$('#textframe').remove();
} 

TextViewer.loadedTypes=new Array();
TextViewer.loadHighlighter=function(ready){
	TextViewer.load(function(){
		TextViewer.type=(TextViewer.types[TextViewer.type])?TextViewer.type:'plain';
		if(!TextViewer.loadedTypes[TextViewer.type]){
			OC.addScript('files_textviewer','syntaxhighlighter/'+TextViewer.types[TextViewer.type],function(){
				TextViewer.loadedTypes[TextViewer.type]=true;
				SyntaxHighlighter.vars.discoveredBrushes=null; //force the highlighter to refresh it's cache
				if(ready){
					ready();
				}
			});
		}else{
			if(ready){
				ready();
			};
		}
	});
}

$(document).ready(function() {
	if(typeof FileActions!=='undefined'){
		FileActions.register('text','View','',function(filename){
			TextViewer.showText($('#dir').val(),filename);
		});
		FileActions.setDefault('text','View');
		FileActions.register('application/xml','View','',function(filename){
			TextViewer.showText($('#dir').val(),filename);
		});
		FileActions.setDefault('application/xml','View');
	}
	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=$(this).data('file');
			var text=file.split('/').pop();
			var dir=file.substr(0,file.length-file.length-1);
			TextViewer.showText(dir,text);
		});
	}
});