/*globals $,OC,fileDownloadPath,t,document,odf,alert,require,dojo,runtime */ $.widget('oc.documentGrid', { options : { context : '.documentslist', documents : {}, sessions : {}, members : {} }, _create : function (){ }, render : function(){ var that = this; jQuery.when(this._load()) .then(function(){ that._render(); }); }, 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 (){ var that = this; var def = new $.Deferred(); $.getJSON(OC.generateUrl('apps/richdocuments/ajax/documents/list')) .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; } def.resolve(); }) .fail(function(data){ console.log(t('richdocuments','Failed to load documents.')); }); return def; }, _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, isGuest : false, memberId : false, esId : false, ready :false, fileName: null, baseName: null, canShare : false, loadError : false, loadErrorMessage : '', loadErrorHint : '', toolbar : '
', UI : { /* Editor wrapper HTML */ container : '
' + '
', /* Previous window title */ mainTitle : '', init : function(){ documentsMain.UI.mainTitle = $('title').text(); }, showEditor : function(title){ if (documentsMain.isGuest){ // !Login page mess wih WebODF toolbars $(document.body).attr('id', 'body-user'); } if (documentsMain.loadError) { documentsMain.onEditorShutdown(t('richdocuments', documentsMain.loadErrorMessage + '\n' + documentsMain.loadErrorHint)); return; } $(document.body).addClass("claro"); $(document.body).prepend(documentsMain.UI.container); $('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){ documentsMain.IU.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 // https://: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"; // 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 var frame = '