From 4f35fb0b5e991bd7102369d0ef8667379ef3a50b Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Wed, 15 Aug 2018 22:09:30 +0200 Subject: [PATCH] fixes the share autocomplete js plugin. nextcloud/server#10114 Signed-off-by: Michael Weimann --- js/app.js | 169 +++++++----------------------------------------------- 1 file changed, 22 insertions(+), 147 deletions(-) diff --git a/js/app.js b/js/app.js index 36bdd58c..671dc19f 100644 --- a/js/app.js +++ b/js/app.js @@ -166,164 +166,39 @@ $(document).ready(function () { OC.Plugins.register('OC.Share.ShareDialogView', { attach: function (obj) { - // Override ShareDigalogView + var originalAutocompleteHandler = obj.autocompleteHandler; obj.autocompleteHandler = function (search, response) { - var view = obj; - var $loading = obj.$el.find('.shareWithLoading'); - $loading.removeClass('hidden'); - $loading.addClass('inlineblock'); - $.get( - OC.linkToOCS('apps/files_sharing/api/v1') + 'sharees', - { - format: 'json', - search: search.term.trim(), - perPage: 200, - itemType: view.model.get('itemType') - }, - function (result) { - $loading.addClass('hidden'); - $loading.removeClass('inlineblock'); - if (result.ocs.meta.statuscode == 100) { - var searchTerm = search.term.trim(); - var users = result.ocs.data.exact.users.concat(result.ocs.data.users); - 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 unknown = []; - - var usersLength; - var groupsLength; - var remotesLength; - - var i, j; - - // Add potential guests to the suggestions - unknown = [{ - label: t('core', 'Create guest account for {searchterm}', {searchterm: searchTerm}), - value: { - shareType: OC.Share.SHARE_TYPE_GUEST, - shareWith: searchTerm - } - }]; - - //Filter out the current user - usersLength = users.length; - for (i = 0; i < usersLength; i++) { - 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; - } - } - } - - 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; - } - } - } - } - - var suggestions = users.concat(groups).concat(remotes).concat(unknown); - - if (suggestions.length > 0) { - $('.shareWithField').removeClass('error') - .tooltip('hide') - .autocomplete("option", "autoFocus", true); - response(suggestions); - } 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) - .tooltip('hide') - .tooltip({ - placement: 'bottom', - trigger: 'manual' - }) - .tooltip('fixTitle') - .tooltip('show'); - response(); - } - } else { - response(); + originalAutocompleteHandler(search, function(suggestions) { + + var searchTerm = search.term.trim(); + + suggestions.push({ + label: t('core', 'Create guest account for {searchterm}', { searchterm: searchTerm }), + value: { + shareType: OC.Share.SHARE_TYPE_GUEST, + shareWith: searchTerm } - } - ).fail(function () { - $loading.addClass('hidden'); - $loading.removeClass('inlineblock'); - OC.Notification.show(t('core', 'An error occurred. Please try again')); - window.setTimeout(OC.Notification.hide, 5000); + }); + + response(suggestions); }); - }; - obj._onSelectRecipient = function (e, s) { - e.preventDefault(); + }; - // vars starting with $ are jQuery DOM objects - // --- - var $this = $(e.target), - $loading = obj.$el.find('.shareWithLoading'); + var original_onSelectRecipient = obj._onSelectRecipient; - $this.attr('disabled', true).val(s.item.label); - $loading.removeClass('hidden').addClass('inlineblock'); + obj._onSelectRecipient = function(e, s) { - // Init OCA.Guests.App if share is of type guest - // --- if (s.item.value.shareType === OC.Share.SHARE_TYPE_GUEST) { + e.preventDefault(); + e.stopImmediatePropagation(); OCA.Guests.App.populate(obj.model, s.item.value.shareWith); OCA.Guests.App.openModal(); + } else { + original_onSelectRecipient(e, s); } - else { - obj.model.addShare(s.item.value, { - success: function () { - $this.val('').attr('disabled', false); - $loading.addClass('hidden').removeClass('inlineblock'); - }, error: function (obj, msg) { - OC.Notification.showTemporary(msg); - $this.attr('disabled', false).autocomplete('search', $this.val()); - $loading.addClass('hidden').removeClass('inlineblock'); - } - }); - } - } + + }; } });