Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Commit

Permalink
Merge cf65fba into bc0f0bd
Browse files Browse the repository at this point in the history
  • Loading branch information
offsky committed Sep 15, 2014
2 parents bc0f0bd + cf65fba commit 9a1ae3c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/tags-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,25 @@ tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig)
options = scope.options,
input = element.find('input'),
validationOptions = ['minTags', 'maxTags', 'allowLeftoverText'],
setElementValidity;
setElementValidity,
blurOnTouch,
waitForBlur;

setElementValidity = function() {
ngModelCtrl.$setValidity('maxTags', scope.tags.length <= options.maxTags);
ngModelCtrl.$setValidity('minTags', scope.tags.length >= options.minTags);
ngModelCtrl.$setValidity('leftoverText', options.allowLeftoverText ? true : !scope.newTag.text);
};


blurOnTouch = function(e) {
if(!element[0].contains(e.target)) {
if(input[0]) {
input[0].blur();
}
}
};

events
.on('tag-added', scope.onTagAdded)
.on('tag-removed', scope.onTagRemoved)
Expand All @@ -211,6 +222,9 @@ tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig)
})
.on('input-focus', function() {
ngModelCtrl.$setValidity('leftoverText', true);

//blur on outside tap when on touch device
$document.on('touchend', blurOnTouch);
})
.on('input-blur', function() {
if (!options.addFromAutocompleteOnly) {
Expand All @@ -220,6 +234,7 @@ tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig)

setElementValidity();
}
$document.off('touchend', blurOnTouch);
})
.on('option-change', function(e) {
if (validationOptions.indexOf(e.name) !== -1) {
Expand Down Expand Up @@ -302,7 +317,7 @@ tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig)
scope.$apply();
})
.on('blur', function() {
$timeout(function() {
waitForBlur = $timeout(function() {
var activeElement = $document.prop('activeElement'),
lostFocusToBrowserWindow = activeElement === input[0],
lostFocusToChildElement = element[0].contains(activeElement);
Expand All @@ -315,6 +330,7 @@ tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig)
});

element.find('div').on('click', function() {
$timeout.cancel(waitForBlur);
input[0].focus();
});
}
Expand Down
13 changes: 13 additions & 0 deletions test/tags-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,19 @@ describe('tags-input directive', function() {
// Assert
expect($scope.tags).toEqual([]);
});

// it('adds a tag when the input field loses focus to any element on the page but the directive itself via a tap instead of a click', function() {
// // Arrange
// isolateScope.newTag.text = 'foo';
// getInput().triggerHandler('focus');

// // Act
// $document.triggerHandler('touchend');
// $timeout.flush();

// // Assert
// expect($scope.tags).toEqual([{ text: 'foo' }]);
// });
});

describe('option is off', function() {
Expand Down

0 comments on commit 9a1ae3c

Please sign in to comment.