Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Issue 994: Change guard against closing the Select when in a multisel… #995

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 19 additions & 6 deletions src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ uis.controller('uiSelectCtrl',
ctrl.tagging = {isActivated: false, fct: undefined};
ctrl.taggingTokens = {isActivated: false, tokens: undefined};
ctrl.lockChoiceExpression = undefined; // Initialized inside uiSelectMatch directive link function
ctrl.clickTriggeredSelect = false;
ctrl.$filter = $filter;

ctrl.searchInput = $element.querySelectorAll('input.ui-select-search');
Expand Down Expand Up @@ -239,7 +238,7 @@ uis.controller('uiSelectCtrl',


// When the user selects an item with ENTER or clicks the dropdown
ctrl.select = function(item, skipFocusser, $event) {
ctrl.select = function(item, skipFocusser) {
if (item === undefined || !item._uiSelectChoiceDisabled) {

if ( ! ctrl.items && ! ctrl.search ) return;
Expand Down Expand Up @@ -299,9 +298,6 @@ uis.controller('uiSelectCtrl',
if (ctrl.closeOnSelect) {
ctrl.close(skipFocusser);
}
if ($event && $event.type === 'click') {
ctrl.clickTriggeredSelect = true;
}
}
}
};
Expand Down Expand Up @@ -466,10 +462,11 @@ uis.controller('uiSelectCtrl',

// If tagging try to split by tokens and add items
ctrl.searchInput.on('paste', function (e) {
var data = e.originalEvent.clipboardData.getData('text/plain');
var data = (e.originalEvent || e).clipboardData.getData('text/plain');
if (data && data.length > 0 && ctrl.taggingTokens.isActivated && ctrl.tagging.fct) {
var items = data.split(ctrl.taggingTokens.tokens[0]); // split by first token only
if (items && items.length > 0) {
_removeDuplicates(items);
angular.forEach(items, function (item) {
var newItem = ctrl.tagging.fct(item);
if (newItem) {
Expand All @@ -488,6 +485,22 @@ uis.controller('uiSelectCtrl',
});
});

function _removeDuplicates(arr) {
var index = 0;
while(index < arr.length){
var item = arr[index];
if(_count(arr, item).length > 1){
arr.splice(index, 1);
} else {
index++;
}
}

function _count(a, item){
return a.filter(function(arrItem) { return angular.equals(item, arrItem); });
}
}

// See https://github.com/ivaynberg/select2/blob/3.4.6/select2.js#L1431
function _ensureHighlightVisible() {
var container = $element.querySelectorAll('.ui-select-choices-content');
Expand Down
3 changes: 1 addition & 2 deletions src/uiSelectDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ uis.directive('uiSelect',
contains = element[0].contains(e.target);
}

if (!contains && !$select.clickTriggeredSelect) {
if (!contains) {
//Will lose focus only with certain targets
var focusableControls = ['input','button','textarea'];
var targetScope = angular.element(e.target).scope(); //To check if target is other ui-select
Expand All @@ -152,7 +152,6 @@ uis.directive('uiSelect',
$select.close(skipFocusser);
scope.$digest();
}
$select.clickTriggeredSelect = false;
}

// See Click everywhere but here event http://stackoverflow.com/questions/12931369
Expand Down
9 changes: 7 additions & 2 deletions src/uiSelectMultipleDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,13 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
};

scope.$on('uis:select', function (event, item) {
$select.selected.push(item);
$selectMultiple.updateModel();
// The model should be updated so that selected items may be removed, however
// this should be done after the onDocumentClick handler has run so that the
// item's list item is still in the DOM.
$timeout(function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fragile - why is this necessary?

$select.selected.push(item);
$selectMultiple.updateModel();
});
});

scope.$on('uis:activate', function () {
Expand Down
12 changes: 12 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,7 @@ describe('ui-select tests', function() {

clickItem(el, 'Samantha');
clickItem(el, 'Adrian');
$timeout.flush();
el.find('.ui-select-match-item').first().find('.ui-select-match-close').click();
$timeout.flush();

Expand Down Expand Up @@ -1065,6 +1066,7 @@ describe('ui-select tests', function() {

clickItem(el, 'Samantha');
clickItem(el, 'Adrian');
$timeout.flush();
el.find('.ui-select-match-item').first().find('.ui-select-match-close').click();
$timeout.flush();

Expand Down Expand Up @@ -1120,6 +1122,7 @@ describe('ui-select tests', function() {
setSearchText(el, 'idontexist');

triggerKeydown(searchInput, Key.Enter);
$timeout.flush();

expect($(el).scope().$select.selected).toEqual(['idontexist']);
});
Expand Down Expand Up @@ -1358,6 +1361,7 @@ describe('ui-select tests', function() {
var el = createUiSelectMultiple();
expect(scope.selection.selectedMultiple instanceof Array).toBe(false);
clickItem(el, 'Samantha');
$timeout.flush();
expect(scope.selection.selectedMultiple instanceof Array).toBe(true);
});

Expand Down Expand Up @@ -1547,6 +1551,7 @@ describe('ui-select tests', function() {
var searchInput = el.find('.ui-select-search');

clickItem(el, 'Wladimir');
$timeout.flush();
expect(scope.selection.selectedMultiple).toEqual([scope.people[5], scope.people[4]]); //Samantha & Wladimir

});
Expand Down Expand Up @@ -1605,6 +1610,7 @@ describe('ui-select tests', function() {

triggerKeydown(searchInput, Key.Down)
triggerKeydown(searchInput, Key.Enter)
$timeout.flush();
expect(scope.selection.selectedMultiple.length).toEqual(2);

});
Expand Down Expand Up @@ -1707,6 +1713,7 @@ describe('ui-select tests', function() {
var searchInput = el.find('.ui-select-search');

clickItem(el, 'Natasha');
$timeout.flush();

expect(el.scope().$select.selected).toEqual([scope.people[4], scope.people[5], scope.people[7]]);
scope.selection.selectedMultiple = ['[email protected]', '[email protected]', '[email protected]'];
Expand Down Expand Up @@ -1743,6 +1750,7 @@ describe('ui-select tests', function() {

setSearchText(el, 'n')
clickItem(el, 'Nicole');
$timeout.flush();

expect(el.find('.ui-select-match-item [uis-transclude-append]:not(.ng-hide)').text())
.toBe("Wladimir <[email protected]>Samantha <[email protected]>Nicole <[email protected]>");
Expand Down Expand Up @@ -1775,6 +1783,7 @@ describe('ui-select tests', function() {
.toBe("Wladimir <[email protected]>Samantha <[email protected]>");

clickItem(el, 'Nicole');
$timeout.flush();

expect(el.find('.ui-select-match-item [uis-transclude-append]:not(.ng-hide)').text())
.toBe("Wladimir <[email protected]>Samantha <[email protected]>Nicole <[email protected]>");
Expand Down Expand Up @@ -1804,6 +1813,7 @@ describe('ui-select tests', function() {
}

clickItem(el, 'Nicole');
$timeout.flush();

expect(scope.counter).toBe(1);

Expand Down Expand Up @@ -1866,6 +1876,7 @@ describe('ui-select tests', function() {
var el = createUiSelectMultiple({tagging: 'taggingFunc', taggingTokens: ",|ENTER"});
clickMatch(el);
triggerPaste(el.find('input'), 'tag1');
$timeout.flush();

expect($(el).scope().$select.selected.length).toBe(1);
});
Expand All @@ -1883,6 +1894,7 @@ describe('ui-select tests', function() {
var el = createUiSelectMultiple({tagging: 'taggingFunc', taggingTokens: ",|ENTER"});
clickMatch(el);
triggerPaste(el.find('input'), ',tag1,tag2,tag3,,tag5,');
$timeout.flush();

expect($(el).scope().$select.selected.length).toBe(5);
});
Expand Down