diff --git a/src/uiSelectController.js b/src/uiSelectController.js index 9fe273ac2..056735018 100644 --- a/src/uiSelectController.js +++ b/src/uiSelectController.js @@ -441,12 +441,12 @@ 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'); - if (data && data.length > 0 && ctrl.taggingTokens.isActivated && ctrl.tagging.fct) { + var data = (e.originalEvent || e).clipboardData.getData('text/plain'); + if (data && data.length > 0 && ctrl.taggingTokens.isActivated) { 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); + var newItem = ctrl.tagging.fct ? ctrl.tagging.fct(item) : item; if (newItem) { ctrl.select(newItem, true); } diff --git a/test/select.spec.js b/test/select.spec.js index d502040b4..3d9907864 100644 --- a/test/select.spec.js +++ b/test/select.spec.js @@ -151,15 +151,23 @@ describe('ui-select tests', function() { e.keyCode = keyCode; element.trigger(e); } - function triggerPaste(element, text) { + function triggerPaste(element, text, isClipboardEvent) { var e = jQuery.Event("paste"); - e.originalEvent = { + if (isClipboardEvent) { + e.clipboardData = { + getData : function() { + return text; + } + }; + } else { + e.originalEvent = { clipboardData : { - getData : function() { - return text; - } + getData : function() { + return text; + } } - }; + }; + } element.trigger(e); } @@ -211,7 +219,7 @@ describe('ui-select tests', function() { expect(getMatchLabel(el)).toEqual('Adam'); }); - + it('should correctly render initial state with track by feature', function() { var el = compileTemplate( ' \ @@ -319,13 +327,13 @@ describe('ui-select tests', function() { it('should toggle allow-clear directive', function() { scope.selection.selected = scope.people[0]; scope.isClearAllowed = false; - + var el = createUiSelect({theme : 'select2', allowClear: '{{isClearAllowed}}'}); var $select = el.scope().$select; expect($select.allowClear).toEqual(false); expect(el.find('.select2-search-choice-close').length).toEqual(0); - + // Turn clear on scope.isClearAllowed = true; scope.$digest(); @@ -1741,6 +1749,25 @@ describe('ui-select tests', function() { triggerPaste(el.find('input'), 'tag1'); expect($(el).scope().$select.selected.length).toBe(1); + expect($(el).scope().$select.selected[0].name).toBe('tag1'); + }); + + it('should allow paste tag from clipboard for generic ClipboardEvent', 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', true); + + expect($(el).scope().$select.selected.length).toBe(1); + expect($(el).scope().$select.selected[0].name).toBe('tag1'); }); it('should allow paste multiple tags', function() { @@ -1759,6 +1786,23 @@ describe('ui-select tests', function() { expect($(el).scope().$select.selected.length).toBe(5); }); + + it('should allow paste multiple tags with generic ClipboardEvent', 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,', true); + + expect($(el).scope().$select.selected.length).toBe(5); + }); }); describe('default configuration via uiSelectConfig', function() {