Skip to content

Commit 36313d6

Browse files
committed
Numbers for tags and tags URLs added. pill HTML structure changes. no core styles. Styles added manually.
1 parent ad618aa commit 36313d6

File tree

4 files changed

+80
-40
lines changed

4 files changed

+80
-40
lines changed

css/bootstrap-tags.css

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1 @@
1-
*[data-tag-id] {
2-
margin-right: 5px;
3-
font-weight: 100;
4-
font-size: 14px;
5-
line-height: 18px;
6-
}
7-
*[data-tag-id] a {
8-
cursor: pointer;
9-
margin-left: 5px;
10-
}
1+

index.html

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
<h1>Bootstrap Tags Demo</h1>
1212
<p>Bootstrap tags is an UI component to manage values with Ajax</p>
1313
<p>
14-
<a href="https://github.com/Serhioromano/bootstrap-tags" class="btn">Fork on GitHub</a>
14+
<a href="https://github.com/Serhioromano/bootstrap-tags" class="btn btn-inverse">Fork on GitHub</a>
15+
<a class="btn btn-success" href="https://github.com/Serhioromano/bootstrap-tags">Documentation</a>
1516
</p>
1617
</div>
1718
<div class="row">
@@ -20,6 +21,22 @@ <h1>Bootstrap Tags Demo</h1>
2021
</div>
2122
<div class="span8">
2223
<div id="bs-tags"></div>
24+
<style type="text/css">
25+
#bs-tags *[data-tag-id] {
26+
margin-right: 5px;
27+
font-weight: 100;
28+
font-size: 14px;
29+
line-height: 20px;
30+
}
31+
#bs-tags *[data-tag-id] a.tag-remove {
32+
margin-left: 5px;
33+
margin-right: -3px;
34+
}
35+
#bs-tags *[data-tag-id] a.tag-link {
36+
color: #ffffff;
37+
text-decoration: underline;
38+
}
39+
</style>
2340
</div>
2441
</div>
2542
</div>

js/bootstrap-tags.js

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
;
2-
(function ($) {
2+
(function($) {
33
"use strict";
44

55
var defaults = {
@@ -9,9 +9,12 @@
99
templates: {
1010
// {0} - text, {1} - id, {2} - delete icon
1111
pill: '<span class="badge badge-info" data-tag-id="{1}">{0}</span>',
12-
delete_icon: '<i class="icon-remove-sign"></i>'
12+
delete_icon: '<i class="icon-remove-sign"></i>',
13+
number: ' <sup><small>{0}</small></sup>'
1314
},
1415

16+
tag_link_target: '', // may be _blank or other.
17+
1518
can_delete: true,
1619
can_add: true,
1720

@@ -21,87 +24,113 @@
2124
delete: "Delete"
2225
},
2326

24-
onLoadDefaults: function (values) {
27+
remove_url: '',
28+
29+
onLoadDefaults: function(values) {
2530
return values;
31+
},
32+
onRemove: function(pill) {
2633
}
34+
2735
};
2836
var options = {};
2937

3038
function Tags(context) {
3139
var $self = this;
3240

33-
if (options.values_url) {
41+
if(options.values_url) {
3442
$.ajax({
3543
dataType: 'json', type: 'get', async: false, url: options.values_url
36-
}).done(function (json) {
37-
if (typeof json == "object") {
44+
}).done(function(json) {
45+
if(typeof json == "object") {
3846
options.values = $.merge(options.values, json);
3947
}
4048
});
4149
}
4250
options.values = options.onLoadDefaults(options.values);
4351

4452
var pills_list = $(document.createElement('div')).appendTo(context);
45-
var hidden_list = $(document.createElement('div')).appendTo(context);
4653

47-
$.each(options.values, function (key, value) {
48-
$self.addTag(pills_list, hidden_list, value);
54+
$.each(options.values, function(key, value) {
55+
$self.addTag(pills_list, value);
4956
});
5057

5158
$('[data-toggle="tooltip"]').tooltip();
5259
}
5360

54-
Tags.prototype.addTag = function (pills_list, hidden_list, value) {
61+
Tags.prototype.addTag = function(pills_list, value) {
5562
var $self = this;
5663

57-
if (typeof value == "string") {
64+
if(typeof value == "string") {
5865
value = {id: value, text: value, html: value};
5966
}
6067
value.html = value.html || value.text;
68+
value.url = value.url || '';
69+
value.num = parseInt(value.num || '0');
70+
71+
if(!value.id || !value.text) {
72+
$.error('Not correct object format to create tag/pill');
73+
}
74+
75+
if(value.url) {
76+
value.text = '<a class="tag-link" target="' + options.tag_link_target + '" href="' + value.url + '">' + value.text + '</a>';
77+
}
6178

6279
var icon = '';
63-
if(options.can_delete){
80+
if(options.can_delete) {
6481
icon = $(document.createElement('a'))
6582
.attr({
83+
"href": "javascript:void(0)",
84+
"class": "tag-remove",
6685
"data-toggle": "tooltip",
6786
"title": options.lang.delete
6887
})
6988
.html(options.templates.delete_icon.toString())
70-
.click(function(){
89+
.click(function() {
7190
$self.removeTag(this);
7291
});
7392
}
7493

75-
var tag = $(options.templates.pill.format(value.text, value.id)).append(icon);
94+
var num = value.num > 0 ? options.templates.number.format(value.num) : '';
7695

96+
var tag = $(options.templates.pill.format(value.text, value.id))
97+
.append(num, icon, $(document.createElement('input'))
98+
.attr({
99+
"data-tag-hidden": value.id,
100+
"name": options.input_name,
101+
"type": "hidden",
102+
"value": value.id
103+
})
104+
);
77105

78106
pills_list.append(tag);
79-
hidden_list.append($(document.createElement('input')).attr({
80-
"data-tag-hidden": value.id,
81-
"name": options.input_name,
82-
"type": "hidden",
83-
"value": value.id
84-
}));
85107
}
86108

87109
Tags.prototype.removeTag = function(tag) {
88-
$(tag).closest('[data-tag-id]').animate({width:0, padding:0}, 200, 'swing', function(){
89-
$(this).remove();
110+
$(tag).closest('[data-tag-id]').animate({width: 0, "padding-right": 0, "padding-left": 0}, 200, 'swing', function() {
111+
var $this = $(this);
112+
if(options.remove_url) {
113+
$.ajax({
114+
dataType: 'json', type: 'post', async: false, url: options.remove_url, data: {id: $this.data('tag-id')}
115+
});
116+
}
117+
options.onRemove($this);
118+
$this.remove();
90119
});
91120
}
92121

93-
$.fn.tags = function (params) {
122+
$.fn.tags = function(params) {
94123
options = $.extend(true, {}, defaults, params);
95-
return this.each(function () {
124+
return this.each(function() {
96125
new Tags($(this));
97126
})
98127
}
99128
}(window.jQuery));
100129

101-
if (!String.prototype.format) {
102-
String.prototype.format = function () {
130+
if(!String.prototype.format) {
131+
String.prototype.format = function() {
103132
var args = arguments;
104-
return this.replace(/{(\d+)}/g, function (match, number) {
133+
return this.replace(/{(\d+)}/g, function(match, number) {
105134
return typeof args[number] != 'undefined' ? args[number] : match;
106135
});
107136
};

urls/defaults.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
{
33
"id": "2",
44
"text": "Apple",
5-
"html": "This is <b>Apple</b>!"
5+
"html": "This is <b>Apple</b>!",
6+
"num": 5,
7+
"url": "http://tags/"
68
},
79
{
810
"id": "3",
911
"text": "Mango",
10-
"html": "I like <b>Mango</b>!"
12+
"html": "I like <b>Mango</b>!",
13+
"url": "/"
1114
},
1215
{
1316
"id": "5",

0 commit comments

Comments
 (0)