/*globals $,OC,fileDownloadPath,t,document,odf,alert,require,dojo,runtime,Handlebars */ $.widget('oc.documentGrid', { options : { context : '.documentslist', documents : {}, sessions : {}, members : {} }, _create : function (){ }, render : function(fileId){ var that = this; jQuery.when(this._load(fileId)) .then(function(){ that._render(); documentsMain.renderComplete = true; }); }, 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) .attr('original-title', document.path) .attr('urlsrc', document.urlsrc) .attr('lolang', document.lolang) .find('label').text(document.name) ; docElem.appendTo(this.options.context).show(); //Preview var previewURL, urlSpec = { file : document.path.replace(/^\/\//, '/'), x : 200, y : 200, c : document.etag, forceIcon : 0 }; if ( $('#isPublic').length ) { urlSpec.t = $('#dirToken').val(); } 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'); 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 +'")'); }; }(a); ready(previewURL); }; img.src = previewURL; } }, _load : function (fileId){ 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; } }) .fail(function(data){ console.log(t('richdocuments','Failed to load documents.')); }); }, _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 ; $(this.options.context + ' .document:not(.template,.progress)').remove(); if (documentsMain.loadError) { $(this.options.context).after('
' + '

' + documentsMain.loadErrorMessage + '

' + documentsMain.loadErrorHint + '

' ); return; } $.each(documents, function(i, document){ hasDocuments = true; that.add(document); }); $.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(''); docElem.addClass('session'); } else { console.log('Could not find file '+session.file_id+' for session '+session.es_id); } } }); if (!hasDocuments){ $(this.options.context).before('
' + t('richdocuments', 'No documents were found. Upload or create a document to get started!') + '
' ); } 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'); } }); var documentsMain = { isEditorMode : false, isViewerMode: false, isGuest : false, memberId : false, esId : false, ready :false, fileName: null, baseName: null, canShare : false, loadError : false, loadErrorMessage : '', loadErrorHint : '', renderComplete: false, // false till page is rendered with all required data about the document(s) toolbar : '
', returnToDir : null, // directory where we started from in the 'Files' app UI : { /* Editor wrapper HTML */ container : '
' + '
', viewContainer: '
' + '
' + '
', revHistoryContainerTemplate: '
' + '
' + '

Revision History

' + '{{filename}}' + '' + '
' + '
' + '' + '
' + '' + '
', revHistoryItemTemplate: '
  • ' + '' + '{{formattedTimestamp}}' + '' + '' + '
  • ', /* Previous window title */ mainTitle : '', /* Number of revisions already loaded */ revisionsStart: 0, init : function(){ documentsMain.UI.mainTitle = $('title').text(); }, showViewer: function(fileId, title){ // remove previous viewer, if open, and set a new one if (documentsMain.isViewerMode) { $('#revViewer').remove(); $('#revViewerContainer').prepend($('
    ')); } $.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://: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') + "&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 = '
    ' + '
    '; // iframe that contains the Collabora Online Viewer var frame = '