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

fixed pasting issue with generic ClipboardEvent in tagging mode #910

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
62 changes: 53 additions & 9 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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(
'<ui-select ng-model="selection.selected"> \
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down