nextcloud/core/js/sharedialogview.js

529 lines
16 KiB
JavaScript
Raw Normal View History

/*
* 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_BASE =
'<div class="resharerInfoView subView"></div>' +
2015-09-15 16:29:30 +03:00
'{{#if isSharingAllowed}}' +
'<label for="shareWith-{{cid}}" class="hidden-visually">{{shareLabel}}</label>' +
'<div class="oneline">' +
' <input id="shareWith-{{cid}}" class="shareWithField" type="text" placeholder="{{sharePlaceholder}}" />' +
' <span class="shareWithLoading icon-loading-small hidden"></span>'+
'{{{remoteShareInfo}}}' +
'</div>' +
2015-09-15 16:29:30 +03:00
'{{/if}}' +
'<div class="shareeListView subView"></div>' +
'<div class="linkShareView subView"></div>' +
'<div class="expirationView subView"></div>' +
'<div class="loading hidden" style="height: 50px"></div>';
var TEMPLATE_REMOTE_SHARE_INFO =
'<a target="_blank" class="icon icon-info shareWithRemoteInfo hasTooltip" href="{{docLink}}" ' +
'title="{{tooltip}}"></a>';
/**
* @class OCA.Share.ShareDialogView
* @member {OC.Share.ShareItemModel} model
* @member {jQuery} $el
* @memberof OCA.Sharing
* @classdesc
*
* Represents the GUI of the share dialogue
*
*/
var ShareDialogView = OC.Backbone.View.extend({
/** @type {Object} **/
_templates: {},
/** @type {boolean} **/
_showLink: true,
/** @type {string} **/
tagName: 'div',
/** @type {OC.Share.ShareConfigModel} **/
configModel: undefined,
/** @type {object} **/
resharerInfoView: undefined,
2015-08-21 16:40:50 +03:00
/** @type {object} **/
linkShareView: undefined,
2015-08-21 20:35:13 +03:00
/** @type {object} **/
expirationView: undefined,
/** @type {object} **/
shareeListView: undefined,
2016-02-12 12:25:42 +03:00
events: {
'input .shareWithField': 'onShareWithFieldChanged'
},
initialize: function(options) {
var view = this;
this.model.on('fetchError', function() {
OC.Notification.showTemporary(t('core', 'Share details could not be loaded for this item.'));
});
if(!_.isUndefined(options.configModel)) {
this.configModel = options.configModel;
} else {
throw 'missing OC.Share.ShareConfigModel';
}
this.configModel.on('change:isRemoteShareAllowed', function() {
view.render();
});
2015-09-15 16:29:30 +03:00
this.model.on('change:permissions', function() {
view.render();
});
this.model.on('request', this._onRequest, this);
this.model.on('sync', this._onEndRequest, this);
var subViewOptions = {
model: this.model,
configModel: this.configModel
};
2015-08-21 21:05:50 +03:00
var subViews = {
resharerInfoView: 'ShareDialogResharerInfoView',
linkShareView: 'ShareDialogLinkShareView',
expirationView: 'ShareDialogExpirationView',
shareeListView: 'ShareDialogShareeListView'
2015-08-21 21:05:50 +03:00
};
2015-08-21 20:35:13 +03:00
2015-08-21 21:05:50 +03:00
for(var name in subViews) {
var className = subViews[name];
this[name] = _.isUndefined(options[name])
? new OC.Share[className](subViewOptions)
: options[name];
}
2015-09-05 03:02:55 +03:00
2016-02-12 12:25:42 +03:00
_.bindAll(this,
'autocompleteHandler',
'_onSelectRecipient',
'onShareWithFieldChanged'
);
OC.Plugins.attach('OCA.Share.ShareDialogView', this);
2016-02-12 12:25:42 +03:00
},
onShareWithFieldChanged: function() {
var $el = this.$el.find('.shareWithField');
if ($el.val().length < 2) {
$el.removeClass('error').tooltip('hide');
}
2015-09-05 03:02:55 +03:00
},
autocompleteHandler: function (search, response) {
var $shareWithField = $('.shareWithField'),
view = this,
$loading = this.$el.find('.shareWithLoading'),
$remoteShareInfo = this.$el.find('.shareWithRemoteInfo');
var count = oc_config['sharing.minSearchStringLength'];
if (search.term.trim().length < count) {
var title = n('core',
'At least {count} character is needed for autocompletion',
'At least {count} characters are needed for autocompletion',
count,
{ count: count }
);
$shareWithField.addClass('error')
.attr('data-original-title', title)
.tooltip('hide')
.tooltip({
placement: 'bottom',
trigger: 'manual'
})
.tooltip('fixTitle')
.tooltip('show');
response();
return;
}
2015-09-05 03:02:55 +03:00
$loading.removeClass('hidden');
$loading.addClass('inlineblock');
$remoteShareInfo.addClass('hidden');
$shareWithField.removeClass('error')
.tooltip('hide');
var perPage = 200;
2015-12-23 12:16:57 +03:00
$.get(
OC.linkToOCS('apps/files_sharing/api/v1') + 'sharees',
{
format: 'json',
search: search.term.trim(),
perPage: perPage,
2015-12-23 12:16:57 +03:00
itemType: view.model.get('itemType')
},
function (result) {
$loading.addClass('hidden');
$loading.removeClass('inlineblock');
$remoteShareInfo.removeClass('hidden');
if (result.ocs.meta.statuscode === 100) {
2015-12-23 12:49:27 +03:00
var users = result.ocs.data.exact.users.concat(result.ocs.data.users);
2015-12-23 12:16:57 +03:00
var groups = result.ocs.data.exact.groups.concat(result.ocs.data.groups);
var remotes = result.ocs.data.exact.remotes.concat(result.ocs.data.remotes);
var lookup = result.ocs.data.lookup;
if (typeof(result.ocs.data.emails) !== 'undefined') {
var emails = result.ocs.data.exact.emails.concat(result.ocs.data.emails);
} else {
var emails = [];
}
if (typeof(result.ocs.data.circles) !== 'undefined') {
var circles = result.ocs.data.exact.circles.concat(result.ocs.data.circles);
} else {
var circles = [];
}
2015-12-23 12:16:57 +03:00
var usersLength;
var groupsLength;
var remotesLength;
var emailsLength;
var circlesLength;
var lookupLength;
2015-12-23 12:16:57 +03:00
var i, j;
//Filter out the current user
usersLength = users.length;
for (i = 0; i < usersLength; i++) {
2015-12-23 12:16:57 +03:00
if (users[i].value.shareWith === OC.currentUser) {
users.splice(i, 1);
break;
}
}
// Filter out the owner of the share
if (view.model.hasReshare()) {
usersLength = users.length;
for (i = 0 ; i < usersLength; i++) {
if (users[i].value.shareWith === view.model.getReshareOwner()) {
users.splice(i, 1);
break;
}
}
}
2015-12-23 12:16:57 +03:00
var shares = view.model.get('shares');
var sharesLength = shares.length;
// Now filter out all sharees that are already shared with
for (i = 0; i < sharesLength; i++) {
var share = shares[i];
if (share.share_type === OC.Share.SHARE_TYPE_USER) {
usersLength = users.length;
for (j = 0; j < usersLength; j++) {
if (users[j].value.shareWith === share.share_with) {
users.splice(j, 1);
break;
}
}
} else if (share.share_type === OC.Share.SHARE_TYPE_GROUP) {
groupsLength = groups.length;
for (j = 0; j < groupsLength; j++) {
if (groups[j].value.shareWith === share.share_with) {
groups.splice(j, 1);
break;
}
}
} else if (share.share_type === OC.Share.SHARE_TYPE_REMOTE) {
remotesLength = remotes.length;
for (j = 0; j < remotesLength; j++) {
if (remotes[j].value.shareWith === share.share_with) {
remotes.splice(j, 1);
break;
}
}
} else if (share.share_type === OC.Share.SHARE_TYPE_EMAIL) {
emailsLength = emails.length;
for (j = 0; j < emailsLength; j++) {
if (emails[j].value.shareWith === share.share_with) {
emails.splice(j, 1);
break;
}
}
} else if (share.share_type === OC.Share.SHARE_TYPE_CIRCLE) {
circlesLength = circles.length;
for (j = 0; j < circlesLength; j++) {
if (circles[j].value.shareWith === share.share_with) {
circles.splice(j, 1);
break;
}
}
2015-12-23 12:16:57 +03:00
}
}
var suggestions = users.concat(groups).concat(remotes).concat(emails).concat(circles).concat(lookup);
2015-12-23 12:16:57 +03:00
2015-12-23 12:49:27 +03:00
if (suggestions.length > 0) {
$shareWithField
2016-02-12 12:04:11 +03:00
.autocomplete("option", "autoFocus", true);
2015-12-23 12:49:27 +03:00
response(suggestions);
// show a notice that the list is truncated
// this is the case if one of the search results is at least as long as the max result config option
if(oc_config['sharing.maxAutocompleteResults'] > 0 &&
Math.min(perPage, oc_config['sharing.maxAutocompleteResults'])
<= Math.max(users.length, groups.length, remotes.length, emails.length, lookup.length)) {
var message = t('core', 'This list is maybe truncated - please refine your search term to see more results.');
$('.ui-autocomplete').append('<li class="autocomplete-note">' + message + '</li>');
}
2015-12-23 12:49:27 +03:00
} else {
var title = t('core', 'No users or groups found for {search}', {search: $shareWithField.val()});
if (!view.configModel.get('allowGroupSharing')) {
title = t('core', 'No users found for {search}', {search: $('.shareWithField').val()});
}
$shareWithField.addClass('error')
.attr('data-original-title', title)
2016-02-12 12:04:11 +03:00
.tooltip('hide')
.tooltip({
placement: 'bottom',
trigger: 'manual'
2016-02-12 12:04:11 +03:00
})
.tooltip('fixTitle')
.tooltip('show');
2015-12-23 12:49:27 +03:00
response();
}
2015-12-23 12:16:57 +03:00
} else {
response();
}
2015-09-05 03:02:55 +03:00
}
2015-12-23 12:16:57 +03:00
).fail(function() {
2015-09-05 03:02:55 +03:00
$loading.addClass('hidden');
$loading.removeClass('inlineblock');
$remoteShareInfo.removeClass('hidden');
OC.Notification.show(t('core', 'An error occurred. Please try again'));
2015-09-05 03:02:55 +03:00
window.setTimeout(OC.Notification.hide, 5000);
});
},
autocompleteRenderItem: function(ul, item) {
var text = item.label;
if (item.value.shareType === OC.Share.SHARE_TYPE_GROUP) {
text = t('core', '{sharee} (group)', { sharee: text }, undefined, { escape: false });
} else if (item.value.shareType === OC.Share.SHARE_TYPE_REMOTE) {
text = t('core', '{sharee} (remote)', { sharee: text }, undefined, { escape: false });
} else if (item.value.shareType === OC.Share.SHARE_TYPE_EMAIL) {
text = t('core', '{sharee} (email)', { sharee: text }, undefined, { escape: false });
} else if (item.value.shareType === OC.Share.SHARE_TYPE_CIRCLE) {
text = t('core', '{sharee} ({type}, {owner})', {sharee: text, type: item.value.circleInfo, owner: item.value.circleOwner}, undefined, {escape: false});
}
var insert = $("<div class='share-autocomplete-item'/>");
var avatar = $("<div class='avatardiv'></div>").appendTo(insert);
if (item.value.shareType === OC.Share.SHARE_TYPE_USER) {
avatar.avatar(item.value.shareWith, 32, undefined, undefined, undefined, item.label);
} else {
avatar.imageplaceholder(text, undefined, 32);
}
$("<div class='autocomplete-item-text'></div>")
.text(text)
.appendTo(insert);
insert.attr('title', item.value.shareWith);
insert = $("<a>")
.append(insert);
return $("<li>")
.addClass((item.value.shareType === OC.Share.SHARE_TYPE_GROUP) ? 'group' : 'user')
.append(insert)
.appendTo(ul);
},
_onSelectRecipient: function(e, s) {
e.preventDefault();
$(e.target).attr('disabled', true)
.val(s.item.label);
var $loading = this.$el.find('.shareWithLoading');
$loading.removeClass('hidden')
.addClass('inlineblock');
var $remoteShareInfo = this.$el.find('.shareWithRemoteInfo');
$remoteShareInfo.addClass('hidden');
this.model.addShare(s.item.value, {success: function() {
$(e.target).val('')
.attr('disabled', false);
$loading.addClass('hidden')
.removeClass('inlineblock');
$remoteShareInfo.removeClass('hidden');
}, error: function(obj, msg) {
OC.Notification.showTemporary(msg);
$(e.target).attr('disabled', false)
.autocomplete('search', $(e.target).val());
$loading.addClass('hidden')
.removeClass('inlineblock');
$remoteShareInfo.removeClass('hidden');
}});
},
_toggleLoading: function(state) {
this._loading = state;
this.$el.find('.subView').toggleClass('hidden', state);
this.$el.find('.loading').toggleClass('hidden', !state);
},
_onRequest: function() {
// only show the loading spinner for the first request (for now)
if (!this._loadingOnce) {
this._toggleLoading(true);
}
},
_onEndRequest: function() {
var self = this;
this._toggleLoading(false);
if (!this._loadingOnce) {
this._loadingOnce = true;
// the first time, focus on the share field after the spinner disappeared
_.defer(function() {
self.$('.shareWithField').focus();
});
}
},
render: function() {
var baseTemplate = this._getTemplate('base', TEMPLATE_BASE);
this.$el.html(baseTemplate({
cid: this.cid,
shareLabel: t('core', 'Share'),
sharePlaceholder: this._renderSharePlaceholderPart(),
2015-09-15 16:29:30 +03:00
remoteShareInfo: this._renderRemoteShareInfoPart(),
isSharingAllowed: this.model.sharePermissionPossible()
}));
var $shareField = this.$el.find('.shareWithField');
2015-09-15 16:29:30 +03:00
if ($shareField.length) {
$shareField.autocomplete({
2016-02-16 18:23:09 +03:00
minLength: 1,
2015-09-15 16:29:30 +03:00
delay: 750,
focus: function(event) {
event.preventDefault();
},
2015-09-15 16:29:30 +03:00
source: this.autocompleteHandler,
select: this._onSelectRecipient
}).data('ui-autocomplete')._renderItem = this.autocompleteRenderItem;
}
2015-09-05 03:02:55 +03:00
2015-08-21 20:35:13 +03:00
this.resharerInfoView.$el = this.$el.find('.resharerInfoView');
this.resharerInfoView.render();
this.linkShareView.$el = this.$el.find('.linkShareView');
this.linkShareView.render();
2015-08-21 20:35:13 +03:00
this.expirationView.$el = this.$el.find('.expirationView');
this.expirationView.render();
this.shareeListView.$el = this.$el.find('.shareeListView');
this.shareeListView.render();
2015-08-18 18:23:32 +03:00
this.$el.find('.hasTooltip').tooltip();
return this;
},
/**
* sets whether share by link should be displayed or not. Default is
* true.
*
* @param {bool} showLink
*/
setShowLink: function(showLink) {
this._showLink = (typeof showLink === 'boolean') ? showLink : true;
2015-08-21 16:40:50 +03:00
this.linkShareView.showLink = this._showLink;
},
_renderRemoteShareInfoPart: function() {
var remoteShareInfo = '';
if(this.configModel.get('isRemoteShareAllowed')) {
var infoTemplate = this._getRemoteShareInfoTemplate();
remoteShareInfo = infoTemplate({
docLink: this.configModel.getFederatedShareDocLink(),
tooltip: t('core', 'Share with people on other servers using their Federated Cloud ID username@example.com/nextcloud')
});
}
2015-08-18 18:23:32 +03:00
return remoteShareInfo;
},
_renderSharePlaceholderPart: function () {
var allowGroupSharing = this.configModel.get('allowGroupSharing');
var allowRemoteSharing = this.configModel.get('isRemoteShareAllowed');
var allowMailSharing = this.configModel.get('isMailShareAllowed');
if (!allowGroupSharing && !allowRemoteSharing && allowMailSharing) {
return t('core', 'Share with users or by mail...');
}
if (!allowGroupSharing && allowRemoteSharing && !allowMailSharing) {
return t('core', 'Share with users or remote users...');
}
if (!allowGroupSharing && allowRemoteSharing && allowMailSharing) {
return t('core', 'Share with users, remote users or by mail...');
}
if (allowGroupSharing && !allowRemoteSharing && !allowMailSharing) {
return t('core', 'Share with users or groups...');
}
if (allowGroupSharing && !allowRemoteSharing && allowMailSharing) {
return t('core', 'Share with users, groups or by mail...');
}
if (allowGroupSharing && allowRemoteSharing && !allowMailSharing) {
return t('core', 'Share with users, groups or remote users...');
}
if (allowGroupSharing && allowRemoteSharing && allowMailSharing) {
return t('core', 'Share with users, groups, remote users or by mail...');
}
return t('core', 'Share with users...');
},
/**
*
* @param {string} key - an identifier for the template
* @param {string} template - the HTML to be compiled by Handlebars
* @returns {Function} from Handlebars
* @private
*/
_getTemplate: function (key, template) {
if (!this._templates[key]) {
this._templates[key] = Handlebars.compile(template);
}
return this._templates[key];
},
/**
* returns the info template for remote sharing
*
* @returns {Function}
* @private
*/
_getRemoteShareInfoTemplate: function() {
return this._getTemplate('remoteShareInfo', TEMPLATE_REMOTE_SHARE_INFO);
}
});
OC.Share.ShareDialogView = ShareDialogView;
})();