Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extract code to get suggestions to its own method
Signed-off-by: Daniel Calviño Sánchez <[email protected]>
  • Loading branch information
danxuliu committed Mar 20, 2018
commit d6062195766fc21b53be1990e6633da7da58d732
165 changes: 92 additions & 73 deletions core/js/sharedialogview.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,53 +134,18 @@
this.$el.find('.shareWithField').autocomplete("search");
},

autocompleteHandler: function (search, response) {
var $shareWithField = $('.shareWithField'),
view = this,
$loading = this.$el.find('.shareWithLoading'),
$confirm = this.$el.find('.shareWithConfirm');

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;
}

$loading.removeClass('hidden');
$loading.addClass('inlineblock');
$confirm.addClass('hidden');

$shareWithField.removeClass('error')
.tooltip('hide');
_getSuggestions: function(searchTerm, perPage, model) {
var deferred = $.Deferred();

var perPage = 200;
$.get(
OC.linkToOCS('apps/files_sharing/api/v1') + 'sharees',
{
format: 'json',
search: search.term.trim(),
search: searchTerm,
perPage: perPage,
itemType: view.model.get('itemType')
itemType: model.get('itemType')
},
function (result) {
$loading.addClass('hidden');
$loading.removeClass('inlineblock');
$confirm.removeClass('hidden');
if (result.ocs.meta.statuscode === 100) {
var users = result.ocs.data.exact.users.concat(result.ocs.data.users);
var groups = result.ocs.data.exact.groups.concat(result.ocs.data.groups);
Expand Down Expand Up @@ -213,17 +178,17 @@
}

// Filter out the owner of the share
if (view.model.hasReshare()) {
if (model.hasReshare()) {
usersLength = users.length;
for (i = 0 ; i < usersLength; i++) {
if (users[i].value.shareWith === view.model.getReshareOwner()) {
if (users[i].value.shareWith === model.getReshareOwner()) {
users.splice(i, 1);
break;
}
}
}

var shares = view.model.get('shares');
var shares = model.get('shares');
var sharesLength = shares.length;

// Now filter out all sharees that are already shared with
Expand Down Expand Up @@ -275,43 +240,97 @@

var suggestions = users.concat(groups).concat(remotes).concat(emails).concat(circles).concat(lookup);

if (suggestions.length > 0) {
$shareWithField
.autocomplete("option", "autoFocus", true);
deferred.resolve(suggestions);
} else {
deferred.resolve(null);
}
}
).fail(function() {
deferred.reject();
});

response(suggestions);
return deferred.promise();
},

// 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)) {
autocompleteHandler: function (search, response) {
var $shareWithField = $('.shareWithField'),
view = this,
$loading = this.$el.find('.shareWithLoading'),
$confirm = this.$el.find('.shareWithConfirm');

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>');
}
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;
}

} 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();
$loading.removeClass('hidden');
$loading.addClass('inlineblock');
$confirm.addClass('hidden');

$shareWithField.removeClass('error')
.tooltip('hide');

var perPage = 200;
this._getSuggestions(
search.term.trim(),
perPage,
view.model
).done(function(suggestions) {
$loading.addClass('hidden');
$loading.removeClass('inlineblock');
$confirm.removeClass('hidden');

if (suggestions && suggestions.length > 0) {
$shareWithField
.autocomplete("option", "autoFocus", true);

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>');
}

} else if (suggestions) {
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();
}
).fail(function() {
}).fail(function() {
$loading.addClass('hidden');
$loading.removeClass('inlineblock');
$confirm.removeClass('hidden');
Expand Down