-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfiles_etherpad.js
More file actions
224 lines (217 loc) · 9.21 KB
/
Copy pathfiles_etherpad.js
File metadata and controls
224 lines (217 loc) · 9.21 KB
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
224
$.fn.filterExtension = function(extension) {
return this.filter(function() {
var filename = $(this).attr('data-file');
if(filename.lastIndexOf('.')){
return filename.substr(filename.lastIndexOf('.')+1) === extension;
}
});
};
$.fn.filterSubExtension = function(extension) {
return this.filter(function() {
var filename = $(this).attr('data-file');
if(filename.lastIndexOf('.')){
filename = filename.substr(0,filename.lastIndexOf('.'));
return filename.substr(filename.lastIndexOf('.')+1) === extension;
}
});
};
function FilenameToMD5(filename, type) {
d = new Date();
return CryptoJS.MD5('owncloud|' + filename + "|" + d.getTime() + d.getMilliseconds() + "|" + type);
}
function FileToPad(dir,file){
pad=this;
pad.dir=dir;
pad.file=file;
pad.url='about:blank';
//get file content using files app api
$.get(
OC.filePath('files','ajax','download.php') + '?dir='+dir+'&files='+file,
function name(link) {
pad.url=link.split('\n')[1].substr(4);
pad.show();
}
);
}
FileToPad.prototype={
show:function(){
$("#editor").hide();
$('#content table').hide();
$('#controls').hide();
//pad headline
$('#content').append('<div id="filetopad_bar"><span>'+t('files_etherpad', 'Title')+':</span> <strong>'+pad.file.replace('.url','').replace('.pad','').replace('.calc','')+'</strong><span>, '+t('files_etherpad', 'public link')+': <a href="'+pad.url+'">'+pad.url+'</a></span>'+' <a id="filetopad_close">x</a></div><iframe style="width:100%;height:90%;display:block;" id="filetopad_frame" src="'+pad.url+'"></iframe>');
$('#pageWidthOption').attr('selected','selected');
$('#filetopad_close').click(function(){pad.hide();});
},
hide:function(){
$('#controls').show();
$('#content table').show();
$('#editor').show();
$('iframe').remove();
//$('a.action').remove();
$('#filetopad_bar').remove();
}
};
function checkPadIcons(){
if($('#fileList').length>0) {
$('tr').filterAttr('data-type', 'file').filterExtension( 'url').attr('data-mime','text/x-url');
$('tr').filterAttr('data-type', 'file').filterExtension( 'url').filterSubExtension( 'pad').children('td:first-child').css('background-image','url('+OC.imagePath('files_etherpad', 'pad.png')+')');
$('tr').filterAttr('data-type', 'file').filterExtension( 'url').filterSubExtension( 'calc').children('td:first-child').css('background-image','url('+OC.imagePath('files_etherpad', 'calc.png')+')');
if (typeof FileActions!=='undefined'){
FileActions.register('text/x-url',t('files_etherpad','Edit'), OC.PERMISSION_READ, '',function(filename){
pad=new FileToPad($('#dir').val(),filename);
});
FileActions.setDefault('text/x-url',t('files_etherpad','Edit'));
}
}
}
$(document).ready(function(){
//update file list for pads
checkPadIcons();
$('#fileList').bind('html', function( event ) {
checkPadIcons();
}).initMutation('html');
// New button hook
if($('div#new>ul>li').length > 0) {
//create pad file type
Files.getMimeIcon('text/plain', function(icon) {
$('<li><p>' + t('files_etherpad', 'Pad') + '</p></li>')
.attr('id', 'newPadFile')
.appendTo('div#new>ul')
.css('background-image', 'url(' + OC.imagePath('files_etherpad', 'pad.png') + ')')
.data('type', 'text')
.off('click')
.click(function() {
//prevent double input
if($(this).children('p').length==0)
return;
//show input for pad name
$(this).children('p').remove();
var form=$('<form></form>');
var input=$('<input>');
form.append(input);
form.appendTo('#newPadFile');
input.focus();
form.submit(function(event){
event.stopPropagation();
event.preventDefault();
//check pad name
var rawfilename = input.val();
if (rawfilename.length == 0) {
$("#notification").text('');
OC.Notification.show(t('files_etherpad', 'Padname cannot be empty'));
return false;
}
var filename = getUniqueName(rawfilename+'.pad.url');
//get etherpad url
$.get(
OC.filePath('files_etherpad', 'ajax', 'host.php'),
{type:'pad'},
function (url) {
//generate .url file
var content='[InternetShortcut]\nURL='+url+'/p/'+FilenameToMD5(rawfilename, 'etherpad');
//send file to server using files app api
$.post(
OC.filePath('files','ajax','newfile.php'),
{dir:$('#dir').val(),filename:filename,content:content},
function(result){
if (result.status == 'success') {
//update file list
var date=new Date();
FileList.addFile(filename,0,date,false,false);
var tr=$('tr').filterAttr('data-file',filename);
tr.attr('data-mime','text/x-url');
tr.attr('data-id', result.data.id);
var path = $('#dir').val();
Files.getMimeIcon('text/plain',function(path){
tr.find('td.filename').attr('style','background-image:url('+OC.imagePath('files_etherpad', 'pad.png')+')');
});
$("#notification").text('');
OC.Notification.show(t('files_etherpad', t('files_etherpad', 'New pad named ')+filename+t('files_etherpad', ' was created.')));
}
else
OC.dialogs.alert(result.data.message, 'Error');
}
);
}
);
//restore description, close new dialogue
var li=form.parent();
form.remove();
li.append('<p>'+t('files_etherpad', 'Pad')+'</p>');
$('#new>a').click();
});
});
});
//create calc file type
Files.getMimeIcon('text/plain', function(icon) {
$('<li><p>' + t('files_etherpad', 'Calc') + '</p></li>')
.attr('id', 'newCalcFile')
.appendTo('div#new>ul')
.css('background-image', 'url(' + OC.imagePath('files_etherpad', 'calc.png') + ')')
.data('type', 'text')
.off('click')
.click(function() {
//prevent double input
if($(this).children('p').length==0)
return;
//show input for pad name
$(this).children('p').remove();
var form=$('<form></form>');
var input=$('<input>');
form.append(input);
form.appendTo('#newCalcFile');
input.focus();
form.submit(function(event){
event.stopPropagation();
event.preventDefault();
//check pad name
var rawfilename = input.val();
if (rawfilename.length == 0) {
$("#notification").text('');
OC.Notification.show(t('files_etherpad', 'Document name cannot be empty'));
return false;
}
var filename = getUniqueName(rawfilename+'.calc.url');
//get etherpad url
$.get(
OC.filePath('files_etherpad', 'ajax', 'host.php'),
{type:'calc'},
function (url) {
//generate .url file
var content='[InternetShortcut]\nURL='+url+'/'+FilenameToMD5(rawfilename, 'ethercalc');;
//send file to server using files app api
$.post(
OC.filePath('files','ajax','newfile.php'),
{dir:$('#dir').val(),filename:filename,content:content},
function(result){
if (result.status == 'success') {
//update file list
var date=new Date();
FileList.addFile(filename,0,date,false,false);
var tr=$('tr').filterAttr('data-file',filename);
tr.attr('data-mime','text/x-url');
tr.attr('data-id', result.data.id);
var path = $('#dir').val();
Files.getMimeIcon('text/plain',function(path){
tr.find('td.filename').attr('style','background-image:url('+OC.imagePath('files_etherpad', 'calc.png')+')');
});
$("#notification").text('');
OC.Notification.show(t('files_etherpad', t('files_etherpad', 'New document named ')+filename+t('files_etherpad', ' was created.')));
}
else
OC.dialogs.alert(result.data.message, 'Error');
}
);
}
);
//restore description, close new dialogue
var li=form.parent();
form.remove();
li.append('<p>'+t('files_etherpad', 'Calc')+'</p>');
$('#new>a').click();
});
});
});
}
});