|
1 | 1 | ; |
2 | | -(function ($) { |
| 2 | +(function($) { |
3 | 3 | "use strict"; |
4 | 4 |
|
5 | 5 | var defaults = { |
|
9 | 9 | templates: { |
10 | 10 | // {0} - text, {1} - id, {2} - delete icon |
11 | 11 | 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>' |
13 | 14 | }, |
14 | 15 |
|
| 16 | + tag_link_target: '', // may be _blank or other. |
| 17 | + |
15 | 18 | can_delete: true, |
16 | 19 | can_add: true, |
17 | 20 |
|
|
21 | 24 | delete: "Delete" |
22 | 25 | }, |
23 | 26 |
|
24 | | - onLoadDefaults: function (values) { |
| 27 | + remove_url: '', |
| 28 | + |
| 29 | + onLoadDefaults: function(values) { |
25 | 30 | return values; |
| 31 | + }, |
| 32 | + onRemove: function(pill) { |
26 | 33 | } |
| 34 | + |
27 | 35 | }; |
28 | 36 | var options = {}; |
29 | 37 |
|
30 | 38 | function Tags(context) { |
31 | 39 | var $self = this; |
32 | 40 |
|
33 | | - if (options.values_url) { |
| 41 | + if(options.values_url) { |
34 | 42 | $.ajax({ |
35 | 43 | 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") { |
38 | 46 | options.values = $.merge(options.values, json); |
39 | 47 | } |
40 | 48 | }); |
41 | 49 | } |
42 | 50 | options.values = options.onLoadDefaults(options.values); |
43 | 51 |
|
44 | 52 | var pills_list = $(document.createElement('div')).appendTo(context); |
45 | | - var hidden_list = $(document.createElement('div')).appendTo(context); |
46 | 53 |
|
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); |
49 | 56 | }); |
50 | 57 |
|
51 | 58 | $('[data-toggle="tooltip"]').tooltip(); |
52 | 59 | } |
53 | 60 |
|
54 | | - Tags.prototype.addTag = function (pills_list, hidden_list, value) { |
| 61 | + Tags.prototype.addTag = function(pills_list, value) { |
55 | 62 | var $self = this; |
56 | 63 |
|
57 | | - if (typeof value == "string") { |
| 64 | + if(typeof value == "string") { |
58 | 65 | value = {id: value, text: value, html: value}; |
59 | 66 | } |
60 | 67 | 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 | + } |
61 | 78 |
|
62 | 79 | var icon = ''; |
63 | | - if(options.can_delete){ |
| 80 | + if(options.can_delete) { |
64 | 81 | icon = $(document.createElement('a')) |
65 | 82 | .attr({ |
| 83 | + "href": "javascript:void(0)", |
| 84 | + "class": "tag-remove", |
66 | 85 | "data-toggle": "tooltip", |
67 | 86 | "title": options.lang.delete |
68 | 87 | }) |
69 | 88 | .html(options.templates.delete_icon.toString()) |
70 | | - .click(function(){ |
| 89 | + .click(function() { |
71 | 90 | $self.removeTag(this); |
72 | 91 | }); |
73 | 92 | } |
74 | 93 |
|
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) : ''; |
76 | 95 |
|
| 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 | + ); |
77 | 105 |
|
78 | 106 | 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 | | - })); |
85 | 107 | } |
86 | 108 |
|
87 | 109 | 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(); |
90 | 119 | }); |
91 | 120 | } |
92 | 121 |
|
93 | | - $.fn.tags = function (params) { |
| 122 | + $.fn.tags = function(params) { |
94 | 123 | options = $.extend(true, {}, defaults, params); |
95 | | - return this.each(function () { |
| 124 | + return this.each(function() { |
96 | 125 | new Tags($(this)); |
97 | 126 | }) |
98 | 127 | } |
99 | 128 | }(window.jQuery)); |
100 | 129 |
|
101 | | -if (!String.prototype.format) { |
102 | | - String.prototype.format = function () { |
| 130 | +if(!String.prototype.format) { |
| 131 | + String.prototype.format = function() { |
103 | 132 | var args = arguments; |
104 | | - return this.replace(/{(\d+)}/g, function (match, number) { |
| 133 | + return this.replace(/{(\d+)}/g, function(match, number) { |
105 | 134 | return typeof args[number] != 'undefined' ? args[number] : match; |
106 | 135 | }); |
107 | 136 | }; |
|
0 commit comments