/* global OC, Handlebars */ /* * Copyright (c) 2015 * * This file is licensed under the Affero General Public License version 3 * or later. * * See the COPYING-README file. * */ /* globals Handlebars */ (function() { if (!OC.Share) { OC.Share = {}; } var TEMPLATE = '
' ; /** * @class OCA.Share.ShareDialogShareeListView * @member {OC.Share.ShareItemModel} model * @member {jQuery} $el * @memberof OCA.Sharing * @classdesc * * Represents the sharee list part in the GUI of the share dialogue * */ var ShareDialogShareeListView = OC.Backbone.View.extend({ /** @type {string} **/ id: 'shareDialogLinkShare', /** @type {OC.Share.ShareConfigModel} **/ configModel: undefined, /** @type {Function} **/ _template: undefined, _menuOpen: false, events: { 'click .unshare': 'onUnshare', 'click .icon-more': 'onToggleMenu', 'click .permissions': 'onPermissionChange', }, initialize: function(options) { if(!_.isUndefined(options.configModel)) { this.configModel = options.configModel; } else { throw 'missing OC.Share.ShareConfigModel'; } var view = this; this.model.on('change:shares', function() { view.render(); }); }, /** * * @param {OC.Share.Types.ShareInfo} shareInfo * @returns {object} */ getShareeObject: function(shareIndex) { var shareWith = this.model.getShareWith(shareIndex); var shareWithDisplayName = this.model.getShareWithDisplayName(shareIndex); var shareWithTitle = ''; var shareType = this.model.getShareType(shareIndex); var hasPermissionOverride = {}; if (shareType === OC.Share.SHARE_TYPE_GROUP) { shareWithDisplayName = shareWithDisplayName + " (" + t('core', 'group') + ')'; } else if (shareType === OC.Share.SHARE_TYPE_REMOTE) { shareWithDisplayName = shareWithDisplayName + " (" + t('core', 'remote') + ')'; } else if (shareType === OC.Share.SHARE_TYPE_EMAIL) { shareWithDisplayName = shareWithDisplayName + " (" + t('core', 'email') + ')'; } if (shareType === OC.Share.SHARE_TYPE_GROUP) { shareWithTitle = shareWith + " (" + t('core', 'group') + ')'; } else if (shareType === OC.Share.SHARE_TYPE_REMOTE) { shareWithTitle = shareWith + " (" + t('core', 'remote') + ')'; } else if (shareType === OC.Share.SHARE_TYPE_EMAIL) { shareWithTitle = shareWith + " (" + t('core', 'email') + ')'; } return _.extend(hasPermissionOverride, { cid: this.cid, hasSharePermission: this.model.hasSharePermission(shareIndex), hasEditPermission: this.model.hasEditPermission(shareIndex), hasCreatePermission: this.model.hasCreatePermission(shareIndex), hasUpdatePermission: this.model.hasUpdatePermission(shareIndex), hasDeletePermission: this.model.hasDeletePermission(shareIndex), shareWith: shareWith, shareWithDisplayName: shareWithDisplayName, shareWithTitle: shareWithTitle, shareType: shareType, shareId: this.model.get('shares')[shareIndex].id, modSeed: shareType !== OC.Share.SHARE_TYPE_USER, isRemoteShare: shareType === OC.Share.SHARE_TYPE_REMOTE, isMailShare: shareType === OC.Share.SHARE_TYPE_EMAIL, isFileSharedByMail: shareType === OC.Share.SHARE_TYPE_EMAIL && !this.model.isFolder() }); }, getShareeList: function() { var universal = { avatarEnabled: this.configModel.areAvatarsEnabled(), unshareLabel: t('core', 'Unshare'), canShareLabel: t('core', 'can reshare'), canEditLabel: t('core', 'can edit'), createPermissionLabel: t('core', 'create'), updatePermissionLabel: t('core', 'change'), deletePermissionLabel: t('core', 'delete'), crudsLabel: t('core', 'access control'), triangleSImage: OC.imagePath('core', 'actions/triangle-s'), isResharingAllowed: this.configModel.get('isResharingAllowed'), sharePermissionPossible: this.model.sharePermissionPossible(), editPermissionPossible: this.model.editPermissionPossible(), createPermissionPossible: this.model.createPermissionPossible(), updatePermissionPossible: this.model.updatePermissionPossible(), deletePermissionPossible: this.model.deletePermissionPossible(), sharePermission: OC.PERMISSION_SHARE, createPermission: OC.PERMISSION_CREATE, updatePermission: OC.PERMISSION_UPDATE, deletePermission: OC.PERMISSION_DELETE, isFolder: this.model.isFolder() }; if(!this.model.hasUserShares()) { return []; } var shares = this.model.get('shares'); var list = []; for(var index = 0; index < shares.length; index++) { var share = this.getShareeObject(index); if (share.shareType === OC.Share.SHARE_TYPE_LINK) { continue; } // first empty {} is necessary, otherwise we get in trouble // with references list.push(_.extend({}, universal, share)); } return list; }, getLinkReshares: function() { var universal = { unshareLabel: t('core', 'Unshare'), avatarEnabled: this.configModel.areAvatarsEnabled(), }; if(!this.model.hasUserShares()) { return []; } var shares = this.model.get('shares'); var list = []; for(var index = 0; index < shares.length; index++) { var share = this.getShareeObject(index); if (share.shareType !== OC.Share.SHARE_TYPE_LINK) { continue; } // first empty {} is necessary, otherwise we get in trouble // with references list.push(_.extend({}, universal, share, { shareInitiator: shares[index].uid_owner, shareInitiatorDisplayName: shares[index].displayname_owner })); } return list; }, render: function() { this.$el.html(this.template({ cid: this.cid, sharees: this.getShareeList(), linkReshares: this.getLinkReshares() })); if(this.configModel.areAvatarsEnabled()) { this.$('.avatar').each(function() { var $this = $(this); if ($this.hasClass('imageplaceholderseed')) { $this.css({width: 32, height: 32}); $this.imageplaceholder($this.data('seed')); } else { $this.avatar($this.data('username'), 32); } }); } this.$('.has-tooltip').tooltip({ placement: 'bottom' }); var _this = this; this.$('.popovermenu').on('afterHide', function() { _this._menuOpen = false; }); if (this._menuOpen) { // Open menu again if it was opened before OC.showMenu(null, this.$('.popovermenu')); } this.delegateEvents(); return this; }, /** * @returns {Function} from Handlebars * @private */ template: function (data) { if (!this._template) { this._template = Handlebars.compile(TEMPLATE); } return this._template(data); }, onUnshare: function(event) { event.preventDefault(); event.stopPropagation(); var self = this; var $element = $(event.target); if (!$element.is('a')) { $element = $element.closest('a'); } var $loading = $element.find('.icon-loading-small').eq(0); if(!$loading.hasClass('hidden')) { // in process return false; } $loading.removeClass('hidden'); var $li = $element.closest('li[data-share-id]'); var shareId = $li.data('share-id'); self.model.removeShare(shareId) .done(function() { $li.remove(); }) .fail(function() { $loading.addClass('hidden'); OC.Notification.showTemporary(t('core', 'Could not unshare')); }); return false; }, onToggleMenu: function(event) { event.preventDefault(); event.stopPropagation(); var $element = $(event.target); var $li = $element.closest('li[data-share-id]'); var $menu = $li.find('.popovermenu'); OC.showMenu(null, $menu); this._menuOpen = true; }, onPermissionChange: function(event) { event.preventDefault(); event.stopPropagation(); var $element = $(event.target); var $li = $element.closest('li[data-share-id]'); var shareId = $li.data('share-id'); var permissions = OC.PERMISSION_READ; if (this.model.isFolder()) { // adjust checkbox states var $checkboxes = $('.permissions', $li).not('input[name="edit"]').not('input[name="share"]'); var checked; if ($element.attr('name') === 'edit') { checked = $element.is(':checked'); // Check/uncheck Create, Update, and Delete checkboxes if Edit is checked/unck $($checkboxes).prop('checked', checked); } else { var numberChecked = $checkboxes.filter(':checked').length; checked = numberChecked > 0; $('input[name="edit"]', $li).prop('checked', checked); } } else { if ($element.attr('name') === 'edit' && $element.is(':checked')) { permissions |= OC.PERMISSION_UPDATE; } } $('.permissions', $li).not('input[name="edit"]').filter(':checked').each(function(index, checkbox) { permissions |= $(checkbox).data('permissions'); }); this.model.updateShare(shareId, {permissions: permissions}); }, }); OC.Share.ShareDialogShareeListView = ShareDialogShareeListView; })();