Skip to content

Commit

Permalink
Adding title attribute to tags
Browse files Browse the repository at this point in the history
Allow objects added to the tagsinput to have an HTML title attribute for
hover-help.
  • Loading branch information
tlinkenback committed Jan 30, 2015
1 parent a465736 commit 8097e0b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/bootstrap-tagsinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
itemText: function(item) {
return this.itemValue(item);
},
itemTitle: function(item) {
return null;
},
freeInput: true,
addOnBlur: true,
maxTags: undefined,
Expand Down Expand Up @@ -105,7 +108,8 @@

var itemValue = self.options.itemValue(item),
itemText = self.options.itemText(item),
tagClass = self.options.tagClass(item);
tagClass = self.options.tagClass(item),
itemTitle = self.options.itemTitle(item);

// Ignore items allready added
var existing = $.grep(self.itemsArray, function(item) { return self.options.itemValue(item) === itemValue; } )[0];
Expand All @@ -126,7 +130,8 @@
self.itemsArray.push(item);

// add a tag element
var $tag = $('<span class="tag ' + htmlEncode(tagClass) + '">' + htmlEncode(itemText) + '<span data-role="remove"></span></span>');

var $tag = $('<span class="tag ' + htmlEncode(tagClass) + (itemTitle !== null ? ('" title="' + itemTitle) : '') + '">' + htmlEncode(itemText) + '<span data-role="remove"></span></span>');
$tag.data('item', item);
self.findInputWrapper().before($tag);
$tag.after(' ');
Expand Down
25 changes: 25 additions & 0 deletions test/bootstrap-tagsinput/input_with_object_items.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,30 @@ describe("bootstrap-tagsinput", function() {
});
});
});

testTagsInput('<input type="text" />', { itemValue: function(item) { return item.value; }, itemText: function(item) { return item.text; }, itemTitle: function(item) { return item.title; } }, function() {
describe("adding an item with a title", function() {
var item;
beforeEach(function() {
item = { value: 1, text: 'one', title: 'number one' };
this.$element.tagsinput('add', item);
});
it("'items' should return the item", function() {
expect(this.$element.tagsinput('items')[0]).toBe(item);
});
it("'items' should returns exactly 1 item", function() {
expect(this.$element.tagsinput('items').length).toBe(1);
});
it("val() should return the item's value", function() {
expect(this.$element.val()).toBe("1");
});
it("tag's text should be the item's value", function() {
expect($('.tag', this.$sandbox).text()).toBe("one");
});
it("tag's title should be the item's title", function() {
expect($('.tag', this.$sandbox).attr('title')).toBe("number one");
});
});
});
});
});

0 comments on commit 8097e0b

Please sign in to comment.