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

Parse tags on paste from clipboard #664

Merged
merged 1 commit into from
Feb 16, 2015
Merged
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
20 changes: 19 additions & 1 deletion src/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -872,7 +890,7 @@
}

$scope.$on('$destroy', function() {
_searchInput.off('keyup keydown tagged blur');
_searchInput.off('keyup keydown tagged blur paste');
});
}])

Expand Down
48 changes: 48 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '"'; }
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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() {
Expand Down