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

Commit

Permalink
Merge branch 'paste' of https://github.com/atomx/ui-select into atomx…
Browse files Browse the repository at this point in the history
…-paste

# Conflicts:
#	src/uiSelectController.js
#	test/select.spec.js
  • Loading branch information
aaronroberson committed Feb 18, 2016
2 parents b41f433 + 5b852b3 commit 1f25ec5
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 28 deletions.
44 changes: 31 additions & 13 deletions src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ uis.controller('uiSelectCtrl',
ctrl.searchEnabled = uiSelectConfig.searchEnabled;
ctrl.sortable = uiSelectConfig.sortable;
ctrl.refreshDelay = uiSelectConfig.refreshDelay;
ctrl.paste = uiSelectConfig.paste;

ctrl.removeSelected = false; //If selected item(s) should be removed from dropdown list
ctrl.closeOnSelect = true; //Initialized inside uiSelect directive link function
Expand Down Expand Up @@ -340,7 +341,7 @@ uis.controller('uiSelectCtrl',
// create new item on the fly if we don't already have one;
// use tagging function if we have one
if ( ctrl.tagging.fct !== undefined && typeof item === 'string' ) {
item = ctrl.tagging.fct(ctrl.search);
item = ctrl.tagging.fct(item);
if (!item) return;
// if item type is 'string', apply the tagging label
} else if ( typeof item === 'string' ) {
Expand Down Expand Up @@ -536,20 +537,37 @@ 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) {
// split by first token only
var separator = KEY.toSeparator(ctrl.taggingTokens.tokens[0]);
var items = data.split(separator);
if (items && items.length > 0) {
var data;

if (window.clipboardData && window.clipboardData.getData) { // IE
data = window.clipboardData.getData('Text');
} else {
data = (e.originalEvent || e).clipboardData.getData('text/plain');
}

// Prepend the current input field text to the paste buffer.
data = ctrl.search + data;

if (data && data.length > 0) {
// If tagging try to split by tokens and add items
if (ctrl.taggingTokens.isActivated) {
var items = data.split(ctrl.taggingTokens.tokens[0]); // split by first token only
if (items && items.length > 0) {
var oldsearch = ctrl.search;
angular.forEach(items, function (item) {
ctrl.search = item;
ctrl.select(item, true);
});
ctrl.search = oldsearch;
angular.forEach(items, function (item) {
var newItem = ctrl.tagging.fct ? ctrl.tagging.fct(item) : item;
if (newItem) {
ctrl.select(newItem, true);
}
});
ctrl.search = EMPTY_SEARCH;
e.preventDefault();
e.stopPropagation();
}
} else if (ctrl.paste) {
ctrl.paste(data);
ctrl.search = EMPTY_SEARCH;
e.preventDefault();
e.stopPropagation();
}
Expand Down
4 changes: 4 additions & 0 deletions src/uiSelectDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ uis.directive('uiSelect',
$select.resetSearchInput = resetSearchInput !== undefined ? resetSearchInput : true;
});

attrs.$observe('paste', function() {
$select.paste = scope.$eval(attrs.paste);
});

attrs.$observe('tagging', function() {
if(attrs.tagging !== undefined)
{
Expand Down
74 changes: 59 additions & 15 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,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;
}
}
};
};
}
element.trigger(e);
}

Expand Down Expand Up @@ -2095,20 +2103,39 @@ describe('ui-select tests', function() {
});

it('should allow paste tag from clipboard', function() {
scope.taggingFunc = function (name) {
return {
name: name,
email: name + '@email.com',
group: 'Foo',
age: 12
};
};
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);
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');
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.length).toBe(1);
expect($(el).scope().$select.selected[0].name).toBe('tag1');
});

it('should allow paste multiple tags', function() {
Expand All @@ -2128,6 +2155,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);
});

it('should split pastes on ENTER (and with undefined tagging function)', function() {
var el = createUiSelectMultiple({tagging: true, taggingTokens: "ENTER|,"});
clickMatch(el);
Expand Down

0 comments on commit 1f25ec5

Please sign in to comment.