From ecac8a7aeabf123e432d3153349ad3f989527dc4 Mon Sep 17 00:00:00 2001 From: Alexey Kuznetsov Date: Sat, 14 Feb 2015 18:06:08 +0300 Subject: [PATCH] Parse tags on paste from clipboard --- src/select.js | 20 ++++++++++++++++++- test/select.spec.js | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/select.js b/src/select.js index f8906c235..fe5bd0316 100644 --- a/src/select.js +++ b/src/select.js @@ -688,6 +688,24 @@ }); + // If tagging try to split by tokens and add items + _searchInput.on('paste', function (e) { + var data = e.originalEvent.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) { + angular.forEach(items, function (item) { + var newItem = ctrl.tagging.fct(item); + if (newItem) { + ctrl.select(newItem, true); + } + }); + e.preventDefault(); + e.stopPropagation(); + } + } + }); + _searchInput.on('keyup', function(e) { if ( ! KEY.isVerticalMovement(e.which) ) { $scope.$evalAsync( function () { @@ -872,7 +890,7 @@ } $scope.$on('$destroy', function() { - _searchInput.off('keyup keydown tagged blur'); + _searchInput.off('keyup keydown tagged blur paste'); }); }]) diff --git a/test/select.spec.js b/test/select.spec.js index 32271d606..d0c5d1cf0 100644 --- a/test/select.spec.js +++ b/test/select.spec.js @@ -69,6 +69,7 @@ describe('ui-select tests', function() { if (attrs.theme !== undefined) { attrsHtml += ' theme="' + attrs.theme + '"'; } if (attrs.tabindex !== undefined) { attrsHtml += ' tabindex="' + attrs.tabindex + '"'; } if (attrs.tagging !== undefined) { attrsHtml += ' tagging="' + attrs.tagging + '"'; } + if (attrs.taggingTokens !== undefined) { attrsHtml += ' tagging-tokens="' + attrs.taggingTokens + '"'; } if (attrs.title !== undefined) { attrsHtml += ' title="' + attrs.title + '"'; } } @@ -115,6 +116,17 @@ describe('ui-select tests', function() { e.keyCode = keyCode; element.trigger(e); } + function triggerPaste(element, text) { + var e = jQuery.Event("paste"); + e.originalEvent = { + clipboardData : { + getData : function() { + return text; + } + } + }; + element.trigger(e); + } function setSearchText(el, text) { el.scope().$select.search = text; @@ -1119,6 +1131,8 @@ describe('ui-select tests', function() { if (attrs.required !== undefined) { attrsHtml += ' ng-required="' + attrs.required + '"'; } if (attrs.tabindex !== undefined) { attrsHtml += ' tabindex="' + attrs.tabindex + '"'; } if (attrs.closeOnSelect !== undefined) { attrsHtml += ' close-on-select="' + attrs.closeOnSelect + '"'; } + if (attrs.tagging !== undefined) { attrsHtml += ' tagging="' + attrs.tagging + '"'; } + if (attrs.taggingTokens !== undefined) { attrsHtml += ' tagging-tokens="' + attrs.taggingTokens + '"'; } } return compileTemplate( @@ -1611,6 +1625,40 @@ describe('ui-select tests', function() { expect(el.scope().$select.multiple).toBe(true); }); + + it('should allow paste tag from clipboard', function() { + scope.taggingFunc = function (name) { + return { + name: name, + email: name + '@email.com', + group: 'Foo', + age: 12 + }; + }; + + var el = createUiSelectMultiple({tagging: 'taggingFunc', taggingTokens: ",|ENTER"}); + clickMatch(el); + triggerPaste(el.find('input'), 'tag1'); + + expect($(el).scope().$select.selected.length).toBe(1); + }); + + it('should allow paste multiple tags', function() { + scope.taggingFunc = function (name) { + return { + name: name, + email: name + '@email.com', + group: 'Foo', + age: 12 + }; + }; + + var el = createUiSelectMultiple({tagging: 'taggingFunc', taggingTokens: ",|ENTER"}); + clickMatch(el); + triggerPaste(el.find('input'), ',tag1,tag2,tag3,,tag5,'); + + expect($(el).scope().$select.selected.length).toBe(5); + }); }); describe('default configuration via uiSelectConfig', function() {