richdocuments/js/documents.js

1047 lines
32 KiB
JavaScript
Raw Normal View History

2016-06-29 17:57:58 +05:30
/*globals $,OC,fileDownloadPath,t,document,odf,alert,require,dojo,runtime,Handlebars */
2014-04-26 02:05:11 +03:00
$.widget('oc.documentGrid', {
options : {
context : '.documentslist',
documents : {},
sessions : {},
members : {}
2014-04-26 02:05:11 +03:00
},
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
_create : function (){
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
},
2015-10-29 19:05:36 +02:00
render : function(fileId){
2014-04-26 02:05:11 +03:00
var that = this;
jQuery.when(this._load(fileId))
2014-04-26 02:05:11 +03:00
.then(function(){
that._render();
documentsMain.renderComplete = true;
2014-04-26 02:05:11 +03:00
});
},
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
add : function(document) {
var docElem = $(this.options.context + ' .template').clone(),
a = docElem.find('a')
;
//Fill an element
docElem.removeClass('template').attr('data-id', document.fileid);
a.css('background-image', 'url("'+document.icon+'")')
.attr('href', OC.generateUrl('apps/files/download{file}',{file:document.path}))
.attr('title', document.path)
2014-09-24 22:29:38 +03:00
.attr('original-title', document.path)
2016-03-10 10:41:07 -04:00
.attr('urlsrc', document.urlsrc)
.attr('action', document.action)
.attr('lolang', document.lolang)
2014-04-26 02:05:11 +03:00
.find('label').text(document.name)
;
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
docElem.appendTo(this.options.context).show();
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
//Preview
var previewURL,
urlSpec = {
file : document.path.replace(/^\/\//, '/'),
x : 200,
y : 200,
c : document.etag,
forceIcon : 0
};
if ( $('#isPublic').length ) {
urlSpec.t = $('#dirToken').val();
}
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
if (!urlSpec.x) {
urlSpec.x = $('#filestable').data('preview-x');
}
if (!urlSpec.y) {
urlSpec.y = $('#filestable').data('preview-y');
}
urlSpec.y *= window.devicePixelRatio;
urlSpec.x *= window.devicePixelRatio;
previewURL = OC.generateUrl('/core/preview.png?') + $.param(urlSpec);
previewURL = previewURL.replace('(', '%28').replace(')', '%29');
2015-10-29 19:05:36 +02:00
if ( $('#previews_enabled').length && document.hasPreview) {
var img = new Image();
img.onload = function(){
var ready = function (node){
return function(path){
node.css('background-image', 'url("'+ path +'")');
2015-10-29 19:05:36 +02:00
};
}(a);
ready(previewURL);
};
img.src = previewURL;
}
2014-04-26 02:05:11 +03:00
},
2015-10-29 19:05:36 +02:00
_load : function (fileId){
2014-04-26 02:05:11 +03:00
var that = this;
var url = 'apps/richdocuments/ajax/documents/list';
var dataObj = {};
if (fileId){
url = 'apps/richdocuments/ajax/documents/get/{fileId}';
dataObj = { fileId: fileId };
}
return $.getJSON(OC.generateUrl(url, dataObj))
.done(function (result) {
if (!result || result.status === 'error') {
documentsMain.loadError = true;
if (result && result.message) {
documentsMain.loadErrorMessage = result.message;
}
else {
documentsMain.loadErrorMessage = t('richdocuments', 'Failed to load the document, please contact your administrator.');
}
if (result && result.hint) {
documentsMain.loadErrorHint = result.hint;
}
}
else {
that.options.documents = result.documents;
that.options.sessions = result.sessions;
that.options.members = result.members;
}
2014-04-26 02:05:11 +03:00
})
.fail(function(data){
2015-12-16 17:57:44 +03:00
console.log(t('richdocuments','Failed to load documents.'));
2014-04-26 02:05:11 +03:00
});
},
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
_render : function (data){
var that = this,
documents = data && data.documents || this.options.documents,
sessions = data && data.sessions || this.options.sessions,
members = data && data.members || this.options.members,
hasDocuments = false
;
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
$(this.options.context + ' .document:not(.template,.progress)').remove();
2015-10-29 19:05:36 +02:00
if (documentsMain.loadError) {
$(this.options.context).after('<div id="errormessage">'
+ '<p>' + documentsMain.loadErrorMessage + '</p><p>'
+ documentsMain.loadErrorHint
+ '</p></div>'
);
return;
}
2014-04-26 02:05:11 +03:00
$.each(documents, function(i, document){
hasDocuments = true;
that.add(document);
});
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
$.each(sessions, function(i, session){
if (members[session.es_id].length > 0) {
var docElem = $(that.options.context + ' .document[data-id="'+session.file_id+'"]');
if (docElem.length > 0) {
docElem.attr('data-esid', session.es_id);
docElem.find('label').after('<img class="svg session-active" src="'+OC.imagePath('core','places/contacts-dark')+'">');
docElem.addClass('session');
} else {
console.log('Could not find file '+session.file_id+' for session '+session.es_id);
}
}
});
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
if (!hasDocuments){
$(this.options.context).before('<div id="emptycontent">'
2015-12-16 17:57:44 +03:00
+ t('richdocuments', 'No documents were found. Upload or create a document to get started!')
2014-04-26 02:05:11 +03:00
+ '</div>'
);
} else {
$('#emptycontent').remove();
}
}
});
$.widget('oc.documentOverlay', {
options : {
parent : 'document.body'
},
_create : function (){
$(this.element).hide().appendTo(document.body);
},
show : function(){
$(this.element).fadeIn('fast');
},
hide : function(){
$(this.element).fadeOut('fast');
}
});
2013-08-28 12:02:27 +02:00
var documentsMain = {
2016-06-26 20:51:06 +05:30
isEditorMode : false,
isViewerMode: false,
isGuest : false,
memberId : false,
esId : false,
ready :false,
fileName: null,
baseName: null,
canShare : false,
canEdit: false,
loadError : false,
loadErrorMessage : '',
loadErrorHint : '',
renderComplete: false, // false till page is rendered with all required data about the document(s)
toolbar : '<div id="ocToolbar"><div id="ocToolbarInside"></div><span id="toolbar" class="claro"></span></div>',
returnToDir : null, // directory where we started from in the 'Files' app
2015-10-29 19:05:36 +02:00
UI : {
2013-09-18 18:48:52 +03:00
/* Editor wrapper HTML */
container : '<div id="mainContainer" class="claro">' +
'</div>',
2015-10-29 19:05:36 +02:00
2016-06-26 20:51:06 +05:30
viewContainer: '<div id="revViewerContainer" class="claro">' +
'<div id="revViewer"></div>' +
'</div>',
revHistoryContainerTemplate: '<div id="revPanelContainer" class="loleaflet-font">' +
'<div id="revPanelHeader">' +
'<h2>Revision History</h2>' +
'<span>{{filename}}</span>' +
'<a class="closeButton"><img src={{closeButtonUrl}} width="22px" height="22px"></a>' +
2016-06-26 20:51:06 +05:30
'</div>' +
'<div id="revisionsContainer" class="loleaflet-font">' +
'<ul></ul>' +
'</div>' +
'<input type="button" id="show-more-versions" class="loleaflet-font" value="{{moreVersionsLabel}}" />' +
'</div>',
revHistoryItemTemplate: '<li>' +
'<a href="{{downloadUrl}}" class="downloadVersion has-tooltip" title="Download"><img src="{{downloadIconUrl}}" />' +
'<a class="versionPreview"><span class="versiondate has-tooltip" title="{{formattedTimestamp}}">{{relativeTimestamp}}</span></a>' +
'<a href="{{restoreUrl}}" class="restoreVersion"><img src="{{restoreIconUrl}}" />' +
2016-06-26 20:51:06 +05:30
'</a>' +
'</li>',
2013-09-18 18:48:52 +03:00
/* Previous window title */
mainTitle : '',
2016-06-26 20:51:06 +05:30
/* Number of revisions already loaded */
revisionsStart: 0,
2015-10-29 19:05:36 +02:00
2013-09-18 18:48:52 +03:00
init : function(){
documentsMain.UI.mainTitle = $('title').text();
},
2015-10-29 19:05:36 +02:00
2016-06-26 20:51:06 +05:30
showViewer: function(fileId, title){
// remove previous viewer, if open, and set a new one
if (documentsMain.isViewerMode) {
$('#revViewer').remove();
$('#revViewerContainer').prepend($('<div id="revViewer">'));
}
$.get(OC.generateUrl('apps/richdocuments/wopi/token/{fileId}', { fileId: fileId }),
function (result) {
// WOPISrc - URL that loolwsd will access (ie. pointing to ownCloud)
var wopiurl = window.location.protocol + '//' + window.location.host + OC.generateUrl('apps/richdocuments/wopi/files/{file_id}', {file_id: fileId});
var wopisrc = encodeURIComponent(wopiurl);
// urlsrc - the URL from discovery xml that we access for the particular
// document; we add various parameters to that.
// The discovery is available at
// https://<loolwsd-server>:9980/hosting/discovery
var urlsrc = $('li[data-id='+ fileId.replace(/_.*/, '') +']>a').attr('urlsrc') +
"WOPISrc=" + wopisrc +
"&title=" + encodeURIComponent(title) +
"&lang=" + $('li[data-id='+ fileId.replace(/_.*/, '') +']>a').attr('lolang') +
2016-06-26 20:51:06 +05:30
"&permission=readonly";
// access_token - must be passed via a form post
var access_token = encodeURIComponent(result.token);
// form to post the access token for WOPISrc
var form = '<form id="loleafletform_viewer" name="loleafletform_viewer" target="loleafletframe_viewer" action="' + urlsrc + '" method="post">' +
'<input name="access_token" value="' + access_token + '" type="hidden"/></form>';
// iframe that contains the Collabora Online Viewer
var frame = '<iframe id="loleafletframe_viewer" name= "loleafletframe_viewer" style="width:100%;height:100%;position:absolute;"/>';
$('#revViewer').append(form);
$('#revViewer').append(frame);
// submit that
$('#loleafletform_viewer').submit();
documentsMain.isViewerMode = true;
});
// for closing revision mode
$('#revPanelHeader .closeButton').click(function(e) {
e.preventDefault();
documentsMain.onCloseViewer();
});
2016-06-26 20:51:06 +05:30
},
addRevision: function(fileId, version, relativeTimestamp, documentPath) {
var formattedTimestamp = OC.Util.formatDate(parseInt(version) * 1000);
var fileName = documentsMain.fileName.substring(0, documentsMain.fileName.indexOf('.'));
var downloadUrl, restoreUrl;
2016-06-26 20:51:06 +05:30
if (version === 0) {
formattedTimestamp = t('richdocuments', 'Latest revision');
downloadUrl = OC.generateUrl('apps/files/download'+ documentPath);
fileId = fileId.replace(/_.*/, '');
} else {
downloadUrl = OC.generateUrl('apps/files_versions/download.php?file={file}&revision={revision}',
{file: documentPath, revision: version});
fileId = fileId + '_' + version;
restoreUrl = OC.generateUrl('apps/files_versions/ajax/rollbackVersion.php?file={file}&revision={revision}',
{file: documentPath, revision: version});
2016-06-26 20:51:06 +05:30
}
var revHistoryItemTemplate = Handlebars.compile(documentsMain.UI.revHistoryItemTemplate);
var html = revHistoryItemTemplate({
downloadUrl: downloadUrl,
downloadIconUrl: OC.imagePath('core', 'actions/download'),
restoreUrl: restoreUrl,
restoreIconUrl: OC.imagePath('core', 'actions/history'),
2016-06-26 20:51:06 +05:30
relativeTimestamp: relativeTimestamp,
formattedTimestamp: formattedTimestamp
});
html = $(html).attr('data-fileid', fileId)
.attr('data-title', fileName + ' - ' + formattedTimestamp);
$('#revisionsContainer ul').append(html);
},
fetchAndFillRevisions: function(documentPath) {
// fill #rev-history with file versions
$.get(OC.generateUrl('apps/files_versions/ajax/getVersions.php?source={documentPath}&start={start}',
{ documentPath: documentPath, start: documentsMain.UI.revisionsStart }),
function(result) {
for(var key in result.data.versions) {
documentsMain.UI.addRevision(documentsMain.fileId,
result.data.versions[key].version,
result.data.versions[key].humanReadableTimestamp,
documentPath);
}
// owncloud only gives 5 version at max in one go
documentsMain.UI.revisionsStart += 5;
if (result.data.endReached) {
// Remove 'More versions' button
$('#show-more-versions').addClass('hidden');
}
});
},
showRevHistory: function(documentPath) {
$(document.body).prepend(documentsMain.UI.viewContainer);
var revHistoryContainerTemplate = Handlebars.compile(documentsMain.UI.revHistoryContainerTemplate);
var revHistoryContainer = revHistoryContainerTemplate({
filename: documentsMain.fileName,
moreVersionsLabel: t('richdocuments', 'More versions...'),
closeButtonUrl: OC.imagePath('core', 'actions/close')
2016-06-26 20:51:06 +05:30
});
$(document.body).prepend(revHistoryContainer);
documentsMain.UI.revisionsStart = 0;
// append current document first
documentsMain.UI.addRevision(documentsMain.fileId, 0, t('richdocuments', 'Just now'), documentPath);
// add "Show more versions" button
$('#show-more-versions').click(function(e) {
e.preventDefault();
documentsMain.UI.fetchAndFillRevisions(documentPath);
});
// fake click to load first 5 versions
$('#show-more-versions').click();
// make these revisions clickable/attach functionality
$('#revisionsContainer').on('click', '.versionPreview', function(e) {
e.preventDefault();
documentsMain.UI.showViewer(e.currentTarget.parentElement.dataset.fileid,
e.currentTarget.parentElement.dataset.title);
// mark only current <li> as active
$(e.currentTarget.parentElement.parentElement).find('li').removeClass('active');
$(e.currentTarget.parentElement).addClass('active');
});
$('#revisionsContainer').on('click', '.restoreVersion', function(e) {
e.preventDefault();
// close the viewer
documentsMain.onCloseViewer();
// close the editor
documentsMain.UI.hideEditor();
// If there are changes in the opened editor, we need to wait
// for sometime before these changes can be saved and a revision is created for it,
// before restoring to requested version.
documentsMain.overlay.documentOverlay('show');
setTimeout(function() {
// restore selected version
$.ajax({
type: 'GET',
url: e.currentTarget.href,
success: function(response) {
if (response.status === 'error') {
documentsMain.UI.notify(t('richdocuments', 'Failed to revert the document to older version'));
}
// generate file id with returnToDir information in it, if any
var fileid = e.currentTarget.parentElement.dataset.fileid.replace(/_.*/, '') +
(documentsMain.returnToDir ? '_' + documentsMain.returnToDir : '');
// load the file again, it should get reverted now
window.location = OC.generateUrl('apps/richdocuments/index#{fileid}', {fileid: fileid});
window.location.reload();
documentsMain.overlay.documentOverlay('hide');
}
});
}, 1000);
});
2016-06-26 20:51:06 +05:30
// fake click on first revision (i.e current revision)
$('#revisionsContainer li').first().find('.versionPreview').click();
},
showEditor : function(title, action){
2014-04-25 20:21:42 +03:00
if (documentsMain.isGuest){
// !Login page mess wih WebODF toolbars
$(document.body).attr('id', 'body-user');
2014-04-25 20:21:42 +03:00
}
if (documentsMain.loadError) {
2016-06-26 17:12:47 +02:00
documentsMain.onEditorShutdown(documentsMain.loadErrorMessage + '\n' + documentsMain.loadErrorHint);
return;
}
if (!documentsMain.renderComplete) {
setTimeout(function() { documentsMain.UI.showEditor(title, action); }, 500);
console.log('Waiting for page to render ...');
return;
}
2013-09-18 18:48:52 +03:00
$(document.body).addClass("claro");
$(document.body).prepend(documentsMain.UI.container);
2014-02-12 16:54:44 +03:00
$('title').text(title + ' - ' + documentsMain.UI.mainTitle);
$.get(OC.generateUrl('apps/richdocuments/wopi/token/{fileId}', { fileId: documentsMain.fileId }),
function (result) {
if (!result || result.status === 'error') {
if (result && result.message){
2016-07-17 21:07:59 +05:30
documentsMain.UI.notify(result.message);
}
documentsMain.onEditorShutdown(t('richdocuments', 'Failed to aquire access token. Please re-login and try again.'));
return;
}
// WOPISrc - URL that loolwsd will access (ie. pointing to ownCloud)
var wopiurl = window.location.protocol + '//' + window.location.host + OC.generateUrl('apps/richdocuments/wopi/files/{file_id}', {file_id: documentsMain.fileId});
var wopisrc = encodeURIComponent(wopiurl);
// urlsrc - the URL from discovery xml that we access for the particular
// document; we add various parameters to that.
// The discovery is available at
2016-06-26 20:51:06 +05:30
// https://<loolwsd-server>:9980/hosting/discovery
var urlsrc = $('li[data-id='+ documentsMain.fileId +']>a').attr('urlsrc') +
"WOPISrc=" + wopisrc +
"&title=" + encodeURIComponent(title) +
"&lang=" + $('li[data-id='+ documentsMain.fileId +']>a').attr('lolang') +
"&closebutton=1" +
"&revisionhistory=1";
if (!documentsMain.canEdit || action === "view") {
urlsrc += "&permission=readonly";
}
// access_token - must be passed via a form post
var access_token = encodeURIComponent(result.token);
// form to post the access token for WOPISrc
2016-04-07 17:53:15 -04:00
var form = '<form id="loleafletform" name="loleafletform" target="loleafletframe" action="' + urlsrc + '" method="post">' +
'<input name="access_token" value="' + access_token + '" type="hidden"/></form>';
// iframe that contains the Collabora Online
var frame = '<iframe id="loleafletframe" name= "loleafletframe" allowfullscreen style="width:100%;height:100%;position:absolute;" onload="this.contentWindow.focus()"/>';
2016-04-07 17:53:15 -04:00
$('#mainContainer').append(form);
$('#mainContainer').append(frame);
// handler for the 'Close' button - we have enabled it via closebutton=1
2016-03-17 15:06:34 -04:00
$('#loleafletframe').load(function(){
documentsMain.overlay.documentOverlay('hide');
window.addEventListener('message', function(e){
2016-06-26 20:51:06 +05:30
if (documentsMain.isViewerMode) {
return;
}
if (e.data === 'close') {
documentsMain.onClose();
2016-06-26 20:51:06 +05:30
} else if (e.data === 'rev-history') {
documentsMain.UI.showRevHistory($('li[data-id=' + documentsMain.fileId + ']>a').attr('original-title'));
}
});
2016-03-17 15:06:34 -04:00
});
2016-04-07 17:53:15 -04:00
// submit that
2016-04-07 17:53:15 -04:00
$('#loleafletform').submit();
});
2013-09-18 18:48:52 +03:00
},
2015-10-29 19:05:36 +02:00
2013-09-18 18:48:52 +03:00
hideEditor : function(){
if (documentsMain.isGuest){
// !Login page mess wih WebODF toolbars
$(document.body).attr('id', 'body-login');
$('footer,nav').show();
}
2015-10-29 19:05:36 +02:00
// Fade out editor
$('#mainContainer').fadeOut('fast', function() {
$('#mainContainer').remove();
2015-06-11 01:31:01 +03:00
$('#content-wrapper').fadeIn('fast');
$(document.body).removeClass('claro');
$('title').text(documentsMain.UI.mainTitle);
});
},
2015-10-29 19:05:36 +02:00
2014-01-21 21:42:52 +03:00
showSave : function (){
$('#odf-close').hide();
$('#saving-document').show();
},
2015-10-29 19:05:36 +02:00
2014-01-21 21:42:52 +03:00
hideSave : function(){
$('#saving-document').hide();
$('#odf-close').show();
},
2015-10-29 19:05:36 +02:00
2013-12-06 03:12:58 +03:00
showProgress : function(message){
if (!message){
message = '&nbsp;';
}
$('.documentslist .progress div').text(message);
$('.documentslist .progress').show();
},
2015-10-29 19:05:36 +02:00
hideProgress : function(){
$('.documentslist .progress').hide();
2013-11-14 19:02:46 +03:00
},
2015-10-29 19:05:36 +02:00
2013-11-14 19:02:46 +03:00
showLostConnection : function(){
$('#memberList .memberListButton').css({opacity : 0.3});
$('#ocToolbar').children(':not(#document-title)').hide();
2013-11-14 19:02:46 +03:00
$('<div id="connection-lost"></div>').prependTo('#memberList');
2015-12-16 17:57:44 +03:00
$('<div id="warning-connection-lost">' + t('richdocuments', 'No connection to server. Trying to reconnect.') +'<img src="'+ OC.imagePath('core', 'loading-dark.gif') +'" alt="" /></div>').prependTo('#ocToolbar');
2013-11-14 19:02:46 +03:00
},
2015-10-29 19:05:36 +02:00
2013-11-14 19:02:46 +03:00
hideLostConnection : function() {
$('#connection-lost,#warning-connection-lost').remove();
$('#ocToolbar').children(':not(#document-title,#saving-document)').show();
2013-11-14 19:02:46 +03:00
$('#memberList .memberListButton').css({opacity : 1});
},
2015-10-29 19:05:36 +02:00
notify : function(message){
OC.Notification.show(message);
2014-04-07 17:18:14 +03:00
setTimeout(OC.Notification.hide, 10000);
2013-09-18 18:48:52 +03:00
}
},
2015-10-29 19:05:36 +02:00
2013-07-12 16:00:07 +02:00
onStartup: function() {
var fileId;
2013-09-18 18:48:52 +03:00
documentsMain.UI.init();
2015-10-29 19:05:36 +02:00
if (!OC.currentUser){
documentsMain.isGuest = true;
2015-10-29 19:05:36 +02:00
if ($("[name='document']").val()){
$(documentsMain.toolbar).appendTo('#header');
documentsMain.prepareSession();
documentsMain.joinSession(
$("[name='document']").val()
);
}
} else {
// Does anything indicate that we need to autostart a session?
fileId = parent.location.hash.replace(/^\W*/, '');
if (fileId.indexOf('_') >= 0) {
documentsMain.returnToDir = unescape(fileId.replace(/^[^_]*_/, ''));
fileId = fileId.replace(/_.*/, '');
}
}
2015-10-29 19:05:36 +02:00
documentsMain.show(fileId);
if (fileId) {
documentsMain.overlay.documentOverlay('show');
documentsMain.prepareSession();
documentsMain.joinSession(fileId);
}
2015-10-29 19:05:36 +02:00
documentsMain.ready = true;
2013-07-12 16:00:07 +02:00
},
2015-10-29 19:05:36 +02:00
2013-09-17 18:06:37 +03:00
prepareSession : function(){
documentsMain.isEditorMode = true;
documentsMain.overlay.documentOverlay('show');
$(window).on("unload", documentsMain.onTerminate);
2013-09-18 18:48:52 +03:00
},
2015-10-29 19:05:36 +02:00
2013-09-18 18:48:52 +03:00
prepareGrid : function(){
documentsMain.isEditorMode = false;
documentsMain.overlay.documentOverlay('hide');
2013-09-17 18:06:37 +03:00
},
2015-10-29 19:05:36 +02:00
2013-08-10 01:49:20 +03:00
initSession: function(response) {
if(response && (response.id && !response.es_id)){
return documentsMain.view(response.id);
2014-04-25 20:21:42 +03:00
}
2015-10-29 19:05:36 +02:00
$('footer,nav').hide();
$(documentsMain.toolbar).appendTo('#header');
2015-10-29 19:05:36 +02:00
if (!response || !response.status || response.status==='error'){
2015-12-16 17:57:44 +03:00
documentsMain.onEditorShutdown(t('richdocuments', 'Failed to load this document. Please check if it can be opened with an external editor. This might also mean it has been unshared or deleted recently.'));
return;
}
2015-10-29 19:05:36 +02:00
//Wait for 3 sec if editor is still loading
if (!documentsMain.ready){
2013-10-24 19:23:59 +03:00
setTimeout(function(){ documentsMain.initSession(response); }, 3000);
console.log('Waiting for the editor to start...');
return;
}
2013-08-07 02:35:46 +03:00
2015-10-29 19:05:36 +02:00
var pollUrl = documentsMain.isGuest
2015-12-16 17:57:44 +03:00
? OC.generateUrl('apps/richdocuments/session/guest/poll/{token}', {'token' : $("[name='document']").val()})
: OC.generateUrl('apps/richdocuments/session/user/poll'),
2015-10-29 19:05:36 +02:00
saveUrl = documentsMain.isGuest
2015-12-16 17:57:44 +03:00
? OC.generateUrl('apps/richdocuments/session/guest/save/{token}', {'token' : $("[name='document']").val()})
: OC.generateUrl('apps/richdocuments/session/user/save')
;
documentsMain.canShare = !documentsMain.isGuest
&& typeof OC.Share !== 'undefined' && response.permissions & OC.PERMISSION_SHARE;
2015-10-29 19:05:36 +02:00
// fade out file list and show the cloudsuite
$('#content-wrapper').fadeOut('fast').promise().done(function() {
2015-10-29 19:05:36 +02:00
documentsMain.fileId = response.file_id;
documentsMain.fileName = response.title;
documentsMain.esId = response.es_id;
documentsMain.memberId = response.member_id;
documentsMain.canEdit = response.permissions & OC.PERMISSION_UPDATE;
documentsMain.loadDocument();
2015-10-29 19:05:36 +02:00
if (documentsMain.isGuest){
2015-12-16 17:57:44 +03:00
$('#odf-close').text(t('richdocuments', 'Save') );
$('#odf-close').removeClass('icon-view-close');
}
});
2013-07-01 22:36:17 +03:00
},
2015-10-29 19:05:36 +02:00
2013-08-07 21:14:36 +03:00
joinSession: function(fileId) {
console.log('joining session '+fileId);
var url;
if (documentsMain.isGuest){
2015-12-16 17:57:44 +03:00
url = OC.generateUrl('apps/richdocuments/session/guest/join/{token}', {token: fileId});
} else {
2015-12-16 17:57:44 +03:00
url = OC.generateUrl('apps/richdocuments/session/user/join/{file_id}', {file_id: fileId});
}
2013-08-22 16:43:14 +02:00
$.post(
url,
2013-09-27 19:33:04 +03:00
{ name : $("[name='memberName']").val() },
2013-08-28 12:02:27 +02:00
documentsMain.initSession
2013-08-22 16:43:14 +02:00
);
2013-08-07 02:35:46 +03:00
},
2015-10-29 19:05:36 +02:00
view : function(id){
2015-12-16 17:57:44 +03:00
OC.addScript('richdocuments', 'viewer/viewer', function() {
documentsMain.prepareGrid();
$(window).off('beforeunload');
$(window).off('unload');
var path = $('li[data-id='+ id +']>a').attr('href');
odfViewer.isDocuments = true;
odfViewer.onView(path);
});
},
2015-10-29 19:05:36 +02:00
onCreateODT: function(event){
2013-09-13 13:18:45 +03:00
event.preventDefault();
documentsMain.create('application/vnd.oasis.opendocument.text');
},
onCreateODS: function(event){
event.preventDefault();
documentsMain.create('application/vnd.oasis.opendocument.spreadsheet');
},
onCreateODP: function(event){
event.preventDefault();
documentsMain.create('application/vnd.oasis.opendocument.presentation');
},
onCreateDOCX: function(event){
event.preventDefault();
documentsMain.create('application/vnd.openxmlformats-officedocument.wordprocessingml.document');
},
onCreateXLSX: function(event){
event.preventDefault();
documentsMain.create('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
},
onCreatePPTX: function(event){
event.preventDefault();
documentsMain.create('application/vnd.openxmlformats-officedocument.presentationml.presentation');
},
create: function(mimetype){
2013-11-13 19:50:29 +03:00
var docElem = $('.documentslist .template').clone();
docElem.removeClass('template');
docElem.addClass('document');
docElem.insertAfter('.documentslist .template');
docElem.show();
2013-09-13 13:18:45 +03:00
$.post(
2015-12-16 17:57:44 +03:00
OC.generateUrl('apps/richdocuments/ajax/documents/create'),
{ mimetype : mimetype },
2014-03-17 23:48:15 +03:00
function(response){
if (response && response.fileid){
docElem.attr('data-id', response.fileid);
docElem.find('a').attr('urlsrc', response.urlsrc);
docElem.find('a').attr('lolang', response.lolang);
2014-03-17 23:48:15 +03:00
documentsMain.prepareSession();
documentsMain.joinSession(response.fileid);
} else {
if (response && response.message){
documentsMain.UI.notify(response.message);
}
2014-03-17 23:48:15 +03:00
documentsMain.show();
}
}
2015-10-29 19:05:36 +02:00
2013-09-13 13:18:45 +03:00
);
},
2015-10-29 19:05:36 +02:00
changeNick: function(memberId, name, node){
2015-12-16 17:57:44 +03:00
var url = OC.generateUrl('apps/richdocuments/ajax/user/rename');
$.ajax({
url: url,
type: "POST",
2015-10-29 19:05:36 +02:00
data: JSON.stringify({
name : name,
memberId : memberId
}),
contentType: 'application/json; charset=utf-8',
dataType:"json",
success: function(result) {
if (result && result.status === 'error') {
if (result.message){
documentsMain.UI.notify(result.message);
}
return;
}
}
});
},
2015-10-29 19:05:36 +02:00
onNickChange: function(memberId, fullNameNode){
if (!documentsMain.isGuest || memberId !== documentsMain.memberId){
return;
}
if ($(fullNameNode.parentNode).children('input').length !== 0){
return;
}
2015-10-29 19:05:36 +02:00
var input = $('<input type="text"/>').val($(fullNameNode).attr('fullname'));
$(fullNameNode.parentNode).append(input);
$(fullNameNode).hide();
input.on('blur', function(){
var newName = input.val();
if (!newName || newName === name) {
input.tipsy('hide');
input.remove();
$(fullNameNode).show();
return;
}
else {
try {
input.tipsy('hide');
input.removeClass('error');
input.tipsy('hide');
input.remove();
$(fullNameNode).show();
documentsMain.changeNick(memberId, newName, fullNameNode);
}
catch (error) {
input.attr('title', error);
input.tipsy({gravity: 'n', trigger: 'manual'});
input.tipsy('show');
input.addClass('error');
}
}
});
input.on('keyup', function(event){
if (event.keyCode === 27) {
// cancel by putting in an empty value
$(this).val('');
$(this).blur();
event.preventDefault();
}
if (event.keyCode === 13) {
$(this).blur();
event.preventDefault();
}
});
input.focus();
input.selectRange(0, name.length);
},
loadDocument: function() {
var action = $('li[data-id='+ documentsMain.fileId +']>a').attr('action');
documentsMain.UI.showEditor(documentsMain.fileName, action);
},
renameDocument: function(name) {
2015-12-16 17:57:44 +03:00
var url = OC.generateUrl('apps/richdocuments/ajax/documents/rename/{file_id}', {file_id: documentsMain.fileId});
$.post(
url,
{ name : name },
function(result) {
if (result && result.status === 'error') {
if (result.message){
2016-07-17 21:07:59 +05:30
documentsMain.UI.notify(result.message);
}
return;
}
documentsMain.fileName = name;
$('title').text(documentsMain.UI.mainTitle + '| ' + name);
$('#document-title').text(name);
}
);
},
onEditorShutdown : function (message){
OC.Notification.show(message);
$(window).off('beforeunload');
$(window).off('unload');
if (documentsMain.isEditorMode){
documentsMain.isEditorMode = false;
parent.location.hash = "";
} else {
setTimeout(OC.Notification.hide, 7000);
}
documentsMain.prepareGrid();
documentsMain.UI.hideEditor();
2015-10-29 19:05:36 +02:00
documentsMain.show();
$('footer,nav').show();
},
2015-10-29 19:05:36 +02:00
onClose: function() {
if (!documentsMain.isEditorMode){
return;
}
documentsMain.isEditorMode = false;
2013-09-27 19:54:31 +03:00
$(window).off('beforeunload');
$(window).off('unload');
2013-09-17 18:06:37 +03:00
parent.location.hash = "";
2013-09-18 18:48:52 +03:00
2015-10-29 20:32:59 -04:00
$('footer,nav').show();
documentsMain.UI.hideEditor();
$('#ocToolbar').remove();
if (documentsMain.returnToDir) {
window.location = OC.generateUrl('apps/files?dir={dir}', {dir: documentsMain.returnToDir});
} else {
documentsMain.show();
}
},
2015-10-29 19:05:36 +02:00
2016-06-26 20:51:06 +05:30
onCloseViewer: function() {
$('#revisionsContainer *').off();
2016-06-26 20:51:06 +05:30
$('#revPanelContainer').remove();
$('#revViewerContainer').remove();
documentsMain.isViewerMode = false;
documentsMain.UI.revisionsStart = 0;
2016-06-26 20:51:06 +05:30
$('#loleafletframe').focus();
},
onTerminate: function(){
var url = '';
if (documentsMain.isGuest){
2015-12-16 17:57:44 +03:00
url = OC.generateUrl('apps/richdocuments/ajax/user/disconnectGuest/{member_id}', {member_id: documentsMain.memberId});
} else {
2015-12-16 17:57:44 +03:00
url = OC.generateUrl('apps/richdocuments/ajax/user/disconnect/{member_id}', {member_id: documentsMain.memberId});
}
$.ajax({
type: "POST",
url: url,
data: {esId: documentsMain.esId},
dataType: "json",
async: false // Should be sync to complete before the page is closed
});
2015-11-05 10:02:45 +01:00
if (documentsMain.isGuest){
$('footer,nav').show();
}
},
2015-10-29 19:05:36 +02:00
show: function(fileId){
if (documentsMain.isGuest){
2013-09-23 22:56:27 +03:00
return;
}
2015-12-16 17:57:44 +03:00
documentsMain.UI.showProgress(t('richdocuments', 'Loading documents...'));
documentsMain.docs.documentGrid('render', fileId);
2014-04-26 02:05:11 +03:00
documentsMain.UI.hideProgress();
2013-07-01 22:36:17 +03:00
}
};
//init
2014-02-05 11:48:23 +03:00
var Files = Files || {
// FIXME: copy/pasted from Files.isFileNameValid, needs refactor into core
isFileNameValid:function (name) {
if (name === '.') {
throw t('files', '\'.\' is an invalid file name.');
} else if (name.length === 0) {
throw t('files', 'File name cannot be empty.');
}
// check for invalid characters
var invalid_characters = ['\\', '/', '<', '>', ':', '"', '|', '?', '*'];
for (var i = 0; i < invalid_characters.length; i++) {
if (name.indexOf(invalid_characters[i]) !== -1) {
throw t('files', "Invalid name, '\\', '/', '<', '>', ':', '\"', '|', '?' and '*' are not allowed.");
}
}
return true;
},
2015-10-29 19:05:36 +02:00
2014-02-05 11:48:23 +03:00
updateStorageStatistics: function(){}
2014-03-10 22:36:26 +03:00
},
FileList = FileList || {};
FileList.getCurrentDirectory = function(){
return $('#dir').val() || '/';
2014-02-05 11:48:23 +03:00
};
FileList.highlightFiles = function(files, highlightFunction) {
};
2015-10-31 18:21:59 -04:00
FileList.findFile = function(filename) {
2015-11-05 10:02:45 +01:00
var documents = documentsMain.docs.documentGrid('option').documents;
return _.find(documents, function(aFile) {
2015-10-31 18:21:59 -04:00
return (aFile.name === filename);
}) || false;
};
FileList.generatePreviewUrl = function(urlSpec) {
2015-11-05 10:02:45 +01:00
urlSpec = urlSpec || {};
if (!urlSpec.x) {
urlSpec.x = 32;
}
if (!urlSpec.y) {
urlSpec.y = 32;
}
urlSpec.x *= window.devicePixelRatio;
urlSpec.y *= window.devicePixelRatio;
urlSpec.x = Math.ceil(urlSpec.x);
urlSpec.y = Math.ceil(urlSpec.y);
urlSpec.forceIcon = 0;
return OC.generateUrl('/core/preview.png?') + $.param(urlSpec);
};
FileList.isFileNameValid = function (name) {
2015-11-05 10:02:45 +01:00
var trimmedName = name.trim();
if (trimmedName === '.' || trimmedName === '..') {
throw t('files', '"{name}" is an invalid file name.', {name: name});
} else if (trimmedName.length === 0) {
throw t('files', 'File name cannot be empty.');
}
return true;
};
FileList.setViewerMode = function(){
};
FileList.findFile = function(fileName){
fullPath = escapeHTML(FileList.getCurrentDirectory + '/' + fileName);
return !!$('.documentslist .document:not(.template,.progress) a[original-title="' + fullPath + '"]').length;
};
2013-07-01 22:36:17 +03:00
$(document).ready(function() {
2015-10-31 18:21:59 -04:00
if (!OCA.Files) {
OCA.Files = {};
OCA.Files.App = {};
OCA.Files.App.fileList = FileList;
}
if (!OC.Share) {
OC.Share = {};
}
window.Files = FileList;
2014-04-26 02:05:11 +03:00
documentsMain.docs = $('.documentslist').documentGrid();
documentsMain.overlay = $('<div id="documents-overlay" class="icon-loading"></div><div id="documents-overlay-below" class="icon-loading-dark"></div>').documentOverlay();
2015-10-29 19:05:36 +02:00
2014-09-24 22:29:38 +03:00
$('li.document a').tipsy({fade: true, live: true});
2015-10-29 19:05:36 +02:00
2013-09-13 13:18:45 +03:00
$('.documentslist').on('click', 'li:not(.add-document)', function(event) {
2013-07-01 22:36:17 +03:00
event.preventDefault();
if (documentsMain.isEditorMode){
return;
}
2015-10-29 19:05:36 +02:00
2016-03-10 10:41:07 -04:00
var item = $(this).find('a');
if (item.attr('urlsrc') === undefined) {
OC.Notification.showTemporary(t('richdocuments', 'Failed to open ' + item.attr('original-title') + ', file not supported.'));
return;
}
2013-09-17 18:10:10 +03:00
documentsMain.prepareSession();
if ($(this).attr('data-id')){
documentsMain.joinSession($(this).attr('data-id'));
2013-08-18 17:11:22 +03:00
}
2013-07-01 22:36:17 +03:00
});
2015-10-29 19:05:36 +02:00
$('.add-document').on('click', '.add-odt', documentsMain.onCreateODT);
$('.add-document').on('click', '.add-ods', documentsMain.onCreateODS);
$('.add-document').on('click', '.add-odp', documentsMain.onCreateODP);
$('.add-document').on('click', '.add-docx', documentsMain.onCreateDOCX);
$('.add-document').on('click', '.add-xlsx', documentsMain.onCreateXLSX);
$('.add-document').on('click', '.add-pptx', documentsMain.onCreatePPTX);
OC.Upload._isReceivedSharedFile = function () {
return false;
};
2013-09-13 17:04:52 +03:00
var file_upload_start = $('#file_upload_start');
2014-03-12 19:41:21 +03:00
if (typeof supportAjaxUploadWithProgress !== 'undefined' && supportAjaxUploadWithProgress()) {
2014-03-10 22:36:26 +03:00
file_upload_start.on('fileuploadstart', function(e, data) {
2014-03-14 14:52:29 +01:00
$('#upload').addClass('icon-loading');
2014-04-25 20:21:42 +03:00
$('.add-document .upload').css({opacity:0});
2014-03-10 22:36:26 +03:00
});
}
file_upload_start.on('fileuploaddone', function(){
2014-03-14 14:52:29 +01:00
$('#upload').removeClass('icon-loading');
2014-04-25 20:21:42 +03:00
$('.add-document .upload').css({opacity:0.7});
2014-03-10 22:36:26 +03:00
documentsMain.show();
});
documentsMain.onStartup();
2013-09-12 23:02:56 +03:00
});