/*globals $,OC,fileDownloadPath,t,document,odf,alert,require,dojo,runtime,Handlebars */ $.widget('oc.documentGrid', { options : { context : '.documentslist', documents : {}, sessions : {}, members : {} }, render : function(fileId) { var that = this; jQuery.when(this._load(fileId)) .then(function(){ that._render(); documentsMain.renderComplete = true; }); }, _load : function(fileId) { // Handle guest user case (let users which are able to write set their name) if (!richdocuments_directEdit && window.parent.oc_current_user == null && this._getGuestNameCookie() == '' && (richdocuments_permissions & OC.PERMISSION_UPDATE)) { $('#documentslist').remove(); var text = document.createElement('div'); $(text).attr('style', 'margin: 0 auto; margin-top: 100px; text-align: center;'); var para = t('richdocuments', 'Please choose your nickname to continue as guest user.'); text.innerHTML = para; var div = document.createElement('div'); $(div).attr('style', 'margin: 0 auto; width: 250px; display: flex;'); var nick = ''; var btn = ''; div.innerHTML = nick + btn; $('#documents-content').prepend(div); $('#documents-content').prepend(text); var that = this; $('#nickname').keyup(function(event) { if (event.which === 13) { that._setGuestNameCookie(); } }); $('#btn').click(that._setGuestNameCookie); $('#preview').hide(); // return such that the editor rendering is skipped. The (parent) page // will be reloaded with the cookie set when the user submits the form. return; } // standard case, render the editor documentsMain.initSession(); }, _getGuestNameCookie: function() { var name = 'guestUser='; var decodedCookie = decodeURIComponent(document.cookie); var cookieArr = decodedCookie.split(';'); for (var i = 0; i < cookieArr.length; i++) { var c = cookieArr[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ''; }, _setGuestNameCookie: function() { var username = $('#nickname').val(); if (username != '') { document.cookie = 'guestUser=' + encodeURIComponent(username) + '; path=/'; } location.reload(true); }, _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; } } }); $.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'); } }); /** * Type definitions for WOPI Post message objects * * @typedef {Object} View * @property {Number} ViewId * @property {string} UserName * @property {string} UserId * @property {Number} Color * @property {Boolean} ReadOnly * @property {Boolean} IsCurrentView */ // Polyfill for Number.isInteger // FIXME: Remove once Nextcloud 15 is the minimum required version // since es6-shim is shipped for that Number.isInteger = Number.isInteger || function(value) { return typeof value === 'number' && isFinite(value) && Math.floor(value) === value; }; var documentsMain = { isEditorMode : false, isViewerMode: false, ready :false, fileName: null, baseName: null, canShare : false, canEdit: false, loadError : false, loadErrorMessage : '', loadErrorHint : '', fileModel: null, renderComplete: false, // false till page is rendered with all required data about the document(s) toolbar : '
', $deferredVersionRestoreAck: null, wopiClientFeatures: null, // generates docKey for given fileId _generateDocKey: function(wopiFileId) { var ocurl = OC.generateUrl('apps/richdocuments/wopi/files/{file_id}', {file_id: wopiFileId}); if (richdocuments_canonical_webroot) { if (!richdocuments_canonical_webroot.startsWith('/')) richdocuments_canonical_webroot = '/' + richdocuments_canonical_webroot; ocurl = ocurl.replace(OC.getRootPath(), richdocuments_canonical_webroot); } return ocurl; }, getFileList: function() { if (window === parent) { return null; } if (parent.OCA.Files.App) { return parent.OCA.Files.App.fileList; } if (parent.OCA.Sharing.PublicApp) { return parent.OCA.Sharing.PublicApp.fileList; } return null; }, getLanguage: function() { return OC.getLanguage ? OC.getLanguage() : OC.getLocale(); }, UI : { /* Editor wrapper HTML */ container : '
' + '
', viewContainer: '
' + '
' + '
', /* Previous window title */ mainTitle : '', /* Number of revisions already loaded */ revisionsStart: 0, /* Views: people currently editing the file */ views: {}, followingEditor: false, following: null, init : function(){ documentsMain.UI.mainTitle = parent.document.title; //Add the avatar toolbar if possible var avatarList = $('
'); avatarList.on('click', function(e) { e.stopPropagation(); parent.$('#editors-menu').toggle(); }); var headerRight = parent.$('#header .header-right'); headerRight.prepend(avatarList); this.addVersionSidebarEvents(); }, _addHeaderFileActions: function() { parent.OC.unregisterMenu(parent.$('#richdocuments-actions .icon-more'), parent.$('#richdocuments-actions-menu')); parent.$('#richdocuments-actions').remove(); var actionsContainer = $('
'); var actions = actionsContainer.find('#richdocuments-actions-menu').empty(); var context = { '$file': documentsMain.getFileList().$el.find('[data-id=' + documentsMain.originalFileId + ']').first(), fileActions: documentsMain.getFileList().fileActions, fileList: documentsMain.getFileList(), fileInfoModel: documentsMain.getFileModel() }; var isFavorite = function(fileInfo) { return fileInfo.get('tags') && fileInfo.get('tags').indexOf(parent.OC.TAG_FAVORITE) >= 0; }; var $favorite = $('
  • ').click(function(e) { $favorite.find('a').removeClass('icon-starred').removeClass('icon-star-dark').addClass('icon-loading-small'); documentsMain.getFileList().fileActions.triggerAction('Favorite', documentsMain.getFileModel(), documentsMain.getFileList()); documentsMain.getFileModel().trigger('change', documentsMain.getFileModel()); }); if (isFavorite(context.fileInfoModel)) { $favorite.find('a').text(parent.t('files', 'Remove from favorites')); $favorite.find('a').addClass('icon-starred'); } else { $favorite.find('a').text(parent.t('files', 'Add to favorites')); $favorite.find('a').addClass('icon-star-dark'); } var $info = $('
  • ').click(function() { documentsMain.getFileList().fileActions.actions.all.Details.action(documentsMain.fileName, context) parent.OC.hideMenus(); }); $info.find('a').text(parent.t('files', 'Details')); var $download = $('
  • Download
  • ').click(function() { documentsMain.getFileList().fileActions.actions.all.Download.action(documentsMain.fileName, context) parent.OC.hideMenus(); }); $download.find('a').text(parent.t('files', 'Download')); actions.append($favorite).append($info).append($download); actionsContainer.insertAfter(parent.$('#header .richdocuments-sharing')); parent.OC.registerMenu(parent.$('#richdocuments-actions .icon-more'), parent.$('#richdocuments-actions-menu'), false, true); }, /** * @param {View} view * @private */ _userEntry: function(view) { var entry = $('
  • '); entry.append(this._avatarForView(view)); var label = $('
    '); label.text(view.UserName); if (view.ReadOnly === '1') { label.text(view.UserName + ' ' + t('richdocuments', '(read only)')); } label.click(function(event) { event.stopPropagation(); documentsMain.UI.followView(view); }); if (this.following === view.ViewId) { parent.$('#editors-menu').find('li').removeClass('active'); entry.addClass('active'); } entry.append(label); if (view.ReadOnly !== '1' && !view.IsCurrentView) { var removeButton = $('
    '); removeButton.click(function() { documentsMain.WOPIPostMessage($('#loleafletframe')[0], 'Action_RemoveView', {ViewId: view.ViewId}); }); entry.append(removeButton) } return entry; }, /** * @param {View} view * @returns {jQuery|HTMLElement} * @private */ _avatarForView: function(view) { var avatarContainer = $('
    '); var avatar = avatarContainer.find('.avatar'); avatar.css({'border-color': view.Color, 'border-width':'2px', 'border-style':'solid'}); if (view.ReadOnly === '1') { avatarContainer.addClass('read-only'); $(avatar).attr('title', view.UserName + ' ' + t('richdocuments', '(read only)')); } else { $(avatar).attr('title', view.UserName); } $(avatar).avatar(view.UserId, 32, undefined, true, undefined, view.UserName); if (parent.OC.currentUser !== null && view.UserId !== '') { //$(avatar).contactsMenu(view.UserId, 0, avatarContainer); } return avatarContainer; }, renderAvatars: function() { var avatardiv = parent.$('#header .header-right #richdocuments-avatars'); avatardiv.empty(); var popover = $(''); var users = []; // Add new avatars var i = 0; for (var viewId in this.views) { /** * @type {View} */ var view = this.views[viewId]; view.UserName = view.UserName !== '' ? view.UserName : t('richdocuments', 'Guest'); popover.find('ul').append(this._userEntry(view)) if (view.UserId === parent.OC.currentUser) { continue; } if (view.UserId !== "" && users.indexOf(view.UserId) > -1) { continue; } users.push(view.UserId); if (i++ < 3) { avatardiv.append(this._avatarForView(view)); } }; var followCurrentEditor = $('
  • '); followCurrentEditor.find('label').click(function(event) { event.stopPropagation(); if (documentsMain.UI.followingEditor) { documentsMain.UI.followReset(); } else { documentsMain.UI.followCurrentEditor(); } }); followCurrentEditor.find('.checkbox').prop('checked', documentsMain.UI.followingEditor); popover.find('ul').append(followCurrentEditor); avatardiv.append(popover) }, followReset: function(event) { documentsMain.WOPIPostMessage($('#loleafletframe')[0], 'Action_FollowUser', {Follow: false}); this.following = null; this.followingEditor = false; this.renderAvatars(); }, followCurrentEditor: function(event) { documentsMain.WOPIPostMessage($('#loleafletframe')[0], 'Action_FollowUser', {Follow: true}); this.following = null; this.followingEditor = true; this.renderAvatars(); }, followView: function(view) { documentsMain.WOPIPostMessage($('#loleafletframe')[0], 'Action_FollowUser', {ViewId: view.ViewId, Follow: true}); documentsMain.UI.following = view.ViewId; documentsMain.UI.followingEditor = false; documentsMain.UI.renderAvatars(); }, showViewer: function(fileId, title){ // remove previous viewer, if open, and set a new one if (documentsMain.isViewerMode) { $('#revViewer').remove(); $('#revViewerContainer').prepend($('
    ')); } else { this.addCurrentVersion(); } // 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 = documentsMain.urlsrc + "WOPISrc=" + wopisrc + "&title=" + encodeURIComponent(title) + "&lang=" + documentsMain.getLanguage().replace('_', '-') + // loleaflet expects a BCP47 language tag syntax "&permission=readonly"; // access_token - must be passed via a form post var access_token = encodeURIComponent(documentsMain.token); // form to post the access token for WOPISrc var form = '
    ' + '
    '; // iframe that contains the Collabora Online Viewer var frame = '