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

Commit 2adc4e1

Browse files
committed
Merge pull request #664 from colvir/issue#485#paste
Parse tags on paste from clipboard
2 parents 5f0a93d + ecac8a7 commit 2adc4e1

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/select.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,24 @@
688688

689689
});
690690

691+
// If tagging try to split by tokens and add items
692+
_searchInput.on('paste', function (e) {
693+
var data = e.originalEvent.clipboardData.getData('text/plain');
694+
if (data && data.length > 0 && ctrl.taggingTokens.isActivated && ctrl.tagging.fct) {
695+
var items = data.split(ctrl.taggingTokens.tokens[0]); // split by first token only
696+
if (items && items.length > 0) {
697+
angular.forEach(items, function (item) {
698+
var newItem = ctrl.tagging.fct(item);
699+
if (newItem) {
700+
ctrl.select(newItem, true);
701+
}
702+
});
703+
e.preventDefault();
704+
e.stopPropagation();
705+
}
706+
}
707+
});
708+
691709
_searchInput.on('keyup', function(e) {
692710
if ( ! KEY.isVerticalMovement(e.which) ) {
693711
$scope.$evalAsync( function () {
@@ -872,7 +890,7 @@
872890
}
873891

874892
$scope.$on('$destroy', function() {
875-
_searchInput.off('keyup keydown tagged blur');
893+
_searchInput.off('keyup keydown tagged blur paste');
876894
});
877895
}])
878896

test/select.spec.js

+48
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ describe('ui-select tests', function() {
6969
if (attrs.theme !== undefined) { attrsHtml += ' theme="' + attrs.theme + '"'; }
7070
if (attrs.tabindex !== undefined) { attrsHtml += ' tabindex="' + attrs.tabindex + '"'; }
7171
if (attrs.tagging !== undefined) { attrsHtml += ' tagging="' + attrs.tagging + '"'; }
72+
if (attrs.taggingTokens !== undefined) { attrsHtml += ' tagging-tokens="' + attrs.taggingTokens + '"'; }
7273
if (attrs.title !== undefined) { attrsHtml += ' title="' + attrs.title + '"'; }
7374
}
7475

@@ -115,6 +116,17 @@ describe('ui-select tests', function() {
115116
e.keyCode = keyCode;
116117
element.trigger(e);
117118
}
119+
function triggerPaste(element, text) {
120+
var e = jQuery.Event("paste");
121+
e.originalEvent = {
122+
clipboardData : {
123+
getData : function() {
124+
return text;
125+
}
126+
}
127+
};
128+
element.trigger(e);
129+
}
118130

119131
function setSearchText(el, text) {
120132
el.scope().$select.search = text;
@@ -1119,6 +1131,8 @@ describe('ui-select tests', function() {
11191131
if (attrs.required !== undefined) { attrsHtml += ' ng-required="' + attrs.required + '"'; }
11201132
if (attrs.tabindex !== undefined) { attrsHtml += ' tabindex="' + attrs.tabindex + '"'; }
11211133
if (attrs.closeOnSelect !== undefined) { attrsHtml += ' close-on-select="' + attrs.closeOnSelect + '"'; }
1134+
if (attrs.tagging !== undefined) { attrsHtml += ' tagging="' + attrs.tagging + '"'; }
1135+
if (attrs.taggingTokens !== undefined) { attrsHtml += ' tagging-tokens="' + attrs.taggingTokens + '"'; }
11221136
}
11231137

11241138
return compileTemplate(
@@ -1611,6 +1625,40 @@ describe('ui-select tests', function() {
16111625

16121626
expect(el.scope().$select.multiple).toBe(true);
16131627
});
1628+
1629+
it('should allow paste tag from clipboard', function() {
1630+
scope.taggingFunc = function (name) {
1631+
return {
1632+
name: name,
1633+
email: name + '@email.com',
1634+
group: 'Foo',
1635+
age: 12
1636+
};
1637+
};
1638+
1639+
var el = createUiSelectMultiple({tagging: 'taggingFunc', taggingTokens: ",|ENTER"});
1640+
clickMatch(el);
1641+
triggerPaste(el.find('input'), 'tag1');
1642+
1643+
expect($(el).scope().$select.selected.length).toBe(1);
1644+
});
1645+
1646+
it('should allow paste multiple tags', function() {
1647+
scope.taggingFunc = function (name) {
1648+
return {
1649+
name: name,
1650+
email: name + '@email.com',
1651+
group: 'Foo',
1652+
age: 12
1653+
};
1654+
};
1655+
1656+
var el = createUiSelectMultiple({tagging: 'taggingFunc', taggingTokens: ",|ENTER"});
1657+
clickMatch(el);
1658+
triggerPaste(el.find('input'), ',tag1,tag2,tag3,,tag5,');
1659+
1660+
expect($(el).scope().$select.selected.length).toBe(5);
1661+
});
16141662
});
16151663

16161664
describe('default configuration via uiSelectConfig', function() {

0 commit comments

Comments
 (0)