Using multiple tag manager instances on the same page
53 |
Note you can have as many instances of tag manager on a single page as you want, with no conflicts; but please read below how to manage them when retrieving user input during <form> submit.
54 |
57 |
58 |
59 |
60 |
Using tag manager in conjunction with typeahead.js
61 |
Note before v3.0 tagmanager was using bootstrap-typeahead, but bootstrap deprecated typeahead from bootstrap v3.0; in this example we are using twitter-typeahead, which is different from bootstrap-typeahead. Both bootstrap-typeahead and twitter-typeahead are developed by the same developers, but they have a different API; and twitter-typeahead is much more feature rich.
98 | Now that we are using typeahead.js from tag manger v3.0 it make sense to use typeahead.js own API to load typeahead from an ajax source, it's very flexible, it's very powerful, and we strongly suggest to read the docs on github to learn how to configure it, to give you a hint, look above at example explaining how to set up tag mangager in conjunction with typeahead.js and check the typeahead.js parameter prefetch.
99 |
100 |
101 |
102 |
103 |
Typeahead via function() source
104 |
105 | Again, now that we are using typeahead.js from tag manger v3.0 you can use the powerful typeahead.js API to load from any source. Check the docs on github to learn how to configure a local source as a function(), look in the docs for typeahead.js parameter local.
106 |
107 |
108 |
109 |
110 |
Retrieving tags on <form> submit
111 |
By default, Tag Manager creates a hidden input field with a random generated name and stores its data in this hidden field as a comma separated list.
112 |
When processing the user input on form submit, simply parse the data of this hidden field.
113 |
Look for this hidden field in the examples above using a browser developer tool (such as Firefox Firebug). In the example you should see: <input type="hidden" value="Pisa,Rome" name="hidden-somerandomgeneratedcode">
114 |
It is also possible to give this field a customer name using the hiddenTagListName parameter, or create your own hidden field and use the hiddenTagListId parameter.
115 |
Note if you are using javascript to post the list of tags you may be much more interested in tag manage API, see below how to retrieve the list of tags entered by the user using the API or how to have tag manager automatically push the tags to a ajax location.
116 |
117 |
118 |
119 |
Pushing Tags via Ajax
120 |
121 | Tagmanager can push entered tag values on-the-fly via Ajax as the user creates/deletes tags to a user-specified destination.
122 | Use the AjaxPushAllTags option to push all tag values on every update, rather than incrementally per created tag.
123 | Use the AjaxPushParameters option to add additional parameters on each ajax request.
124 | Refer to documentation for additional parameters.
125 |
Note to see this working, open firebug or chrome developer tools and chech the XHR ajax request being generated when you add or remove tags from the input field above.
137 |
138 |
139 |
140 |
Using API
141 |
142 | Tagmanager has a nice API you can use to programmatically add or remove tags, see below an example, and check the complete list of methods in the configuration section.
143 |
Note to see this working, open firebug or chrome developer tools and chech the XHR ajax request being generated when you add or remove tags from the input field above.
157 |
158 |
159 |
160 |
Tagmanager Configuration
161 |
The code below show all the configurable options available (as of version 3.0.1):
188 | Populates the initial tag values. Default: null. Allowed formats:
189 |
190 |
an Array of strings
191 |
a String delimited by the first char code in the delimiters parameter (comma by default)
192 |
a Function which returns an array
193 |
if prefill is not provided hiddenTagListId will be used as a fallback if provided. Note the value in hiddenTagListId should be a string delimited by the first char code in the delimiters parameter (comma by default)
194 |
195 |
196 |
197 |
198 |
CapitalizeFirstLetter
199 |
If true, all tags will be displayed with first letter capitalized. Default: false.
200 |
201 |
202 |
AjaxPush
203 |
Well, since we pull from an ajax source we can also push, don't we? So provide a url as AjaxPush and the added tag will be POSTed.
204 |
205 |
206 |
AjaxPushAllTags
207 |
208 | If true, enables a mode to sync the entire tag state via AJAX (rather than incrementally)
209 | each time a tag is added/deleted. Default: false.
210 |
211 |
212 |
213 |
AjaxPushParameters
214 |
Adds an additional parameter payload to push with each AJAX request, for example server authentication parameters. Default: null.
215 |
216 |
217 |
delimiters
218 |
219 | Default: [9,13,44] (tab, enter, comma). Delimiters should be numeric ASCII char codes. Please note the following:
220 |
221 |
The following values are handled as key codes: 9 (tab), 13 (enter), 16 (shift), 17 (ctrl), 18 (alt), 19 (pause/break), 37 (leftarrow), 38 (uparrow), 39 (rightarrow), 40 (downarrow)
222 |
Note that codes 45 (key = insert, char = -) and 46 (key = delete, char = .) are handled as chars, so currently insert and delete keys cannot be used as delimiters
223 |
The first char code (non-key code) specified in the array will be used as the base delimiter for parsing tags to/from the hidden field "list string". This will default to comma if not specified.
When the input field is empty, and some tags are rendered on the left of the input field, and the user hit the backspace the plugin remove the rightest tag (which is the last the user entered). With this option you can provide an array of char codes (like the delimiters above) you want the system to use in place of the backspace (char code 8), or provide an empty array if you don't want this feature at all.
231 |
232 |
233 |
blinkBGColor_1 blinkBGColor_2
234 |
235 | When a duplicate tag is entered the user is notified with a blinking of the corresponding (duplicate) tag, here you can configure the colors.
236 |
Note this is working only if you also include jQuery UI in your project.
237 |
238 |
239 |
240 |
output
241 |
242 | Should be a valid CSS selector, if present this input field will receive the comma separated list of tags entered; tag manager does not check if this selector is really an input field, just tries to fill the value with jQuery .val().
243 |
244 |
245 |
246 |
replace
247 |
248 | (true|false) If set, the element name of the input field will be transfered to the hidden input field storing the comma separated list of tags entered; in other words if you have <input name="tags"/> and you turn it into a tagmanager with this parameter true on form submit you will find the tags posted with name="tag".
249 |
250 |
251 |
252 |
maxTags
253 |
254 | Optionally defines the maximum number of tags accepted. Default: 0 (no limit)
255 |
256 |
257 |
258 |
tagCloseIcon
259 |
260 | Sets the HTML string to be used as the tag close icon. Default: ×
261 |
262 |
263 |
264 |
tagsContainer
265 |
266 | Optional jQuery selector for the element to contain the tags. Default: tags appear immediately before the tag input.
267 |
268 |
269 |
270 |
tagClass
271 |
272 | Optional class to be applied to all tags. Please as see note about automatic tag styling. Default: none
273 |
274 |
275 |
276 |
validator
277 |
278 | An optional callback function to validate the user input. Takes the tag string as input, and must return true or false. Default: null (no validation function used).
279 |
280 |
281 |
282 |
onlyTagList
283 |
284 | If true, rejects tags which do not appear in the typeahead list. Default: false
285 |
286 |
287 |
288 |
289 |
290 |
291 |
Tag Manager API Methods
292 |
You can pop the last added tag (the rightmost being show)
293 |
.tagsManager('popTag');
294 |
You can push a new tag
295 |
.tagsManager('pushTag','I_am_a_new_tag');
296 |
You can remove all tags
297 |
.tagsManager('empty');
298 |
You can retrieve the list of all tags entered so far
299 |
.tagsManager('tags');
300 |
301 |
302 |
303 |
Tag Manager Events
304 |
You can hook on events to get notified when things happen.
tm:popping when the last (rightmost) tag is going to be removed.
331 |
332 | jQuery(".tm-input").on('tm:popping', function(e, tag) {
333 | alert(tag + " is almost removed!");
334 | });
335 |
tm:refresh when the hidden field containing the comma separated list of tags has been updated.
336 |
337 | jQuery(".tm-input").on('tm:refresh', function(e, taglist) {
338 | alert(taglist + " is the new list of tags!");
339 | });
340 |
tm:emptied when all tags have been removed programmatically.
341 |
342 | jQuery(".tm-input").on('tm:emptied', function(e, taglist) {
343 | alert("Tags have been nuked!");
344 | });
345 |
tm:hide when the input field has been hidden due to maxTags limit being hit.
346 |
347 | jQuery(".tm-input").on('tm:hide', function(e, taglist) {
348 | alert("Holy smoke how many tags!");
349 | });
350 |
tm:show when the input field has been show again after being previously removed due to maxTags limit being hit.
351 |
352 | jQuery(".tm-input").on('tm:show', function(e, taglist) {
353 | alert("Here we are again!");
354 | });
355 |
356 |
357 |
358 |
Tag Manager Styles
359 |
360 | Tag Manager comes bundled with a Bootstrap-themed set semantic colors/sizes, and provides multiple methods to define tag styles:
361 |
362 |
the CSS base class tm-tag is applied to all tags
363 |
classes assigned to the input are used to infer the semantic tag classes, for example <input class='tm-input tm-input-success tm-input-small'/> will apply classes tm-tag-success and tm-tag-small to the tags.
364 |
you can apply a custom class(es) to the tags using the tagClass parameter
365 |
366 |
367 |
368 |
In order to have correct alignment we recommend that you give the input the class tm-tag. If using TagManager within a Bootstrap control-group container, please also add tm-group class to the container node.
Before to develop my own tags manager I spent quite a lot of time investigating what other solutions where available, so I think I can spare you some time listing them here and explaining why I developed my own.
379 |
380 |
381 | Tag it! by Levy Carneiro Jr, one of the very first one as far as I know, which inspired all the others.
382 |
383 |
384 | Tag Handler by ioncache, in my opinion is probably the best general purpose tags manager around.
385 |
The four above are the best around in my opinion, but... I developed Tagmanager. I wanted it to be very simple, I didn't need a pencil mouse pointer when the user hover it. I didn't need inplace edit of the tag, I think it's confusing for the user. I didn't want the tag manager to generate a lot of html in the form.
394 |
And I wanted it to be fully integrated with bootstrap and typeahead.
395 |
396 |
397 |
398 |
Version 3.0 is in beta.
399 |
400 | Version 3.0 is in beta, there are a couple of things which doesn't work as they should when used in conjunction with typeahes.js, please check discussion on github, we are trying to fix this.
401 |
If you have choosen to go this way and implement a tags system in one of your application you have probably already thought about a couple of things, like the database structure required to store tags.
416 |
But just in case... You can find some thoughts about that here.
417 |
418 |
419 |
502 |
--------------------------------------------------------------------------------
/jquery.tagsmanager.d.ts:
--------------------------------------------------------------------------------
1 | // Type definitions for jQuery Tags Manager
2 | // Project: http://welldonethings.com/tags/manager/v3
3 | // Definitions by: https://github.com/max-favilli
4 | // Definitions: https://github.com/borisyankov/DefinitelyTyped
5 |
6 | ///
7 |
8 | interface ITagsManagerOptions {
9 | prefilled?: any;
10 | CapitalizeFirstLetter?: bool;
11 | preventSubmitOnEnter?: bool;
12 | isClearInputOnEsc?: bool;
13 | typeahead?: bool;
14 | typeaheadAjaxSource?: string;
15 | typeaheadAjaxPolling?: bool;
16 | typeaheadDelegate?: Function;
17 | typeaheadOverrides?: ITypeaheadOverrides;
18 | typeaheadSource?: any;
19 | AjaxPush?: string; //url
20 | AjaxPushAllTags?: string; //url
21 | AjaxPushParameters: { [key: string]: string; };
22 | delimiters?: number[];
23 | backspace?: number[];
24 | maxTags?: number;
25 | blinkBGColor_1?: string;
26 | blinkBGColor_2?: string;
27 | hiddenTagListName?: string;
28 | hiddenTagListId?: string;
29 | deleteTagsOnBackspace?: bool;
30 | tagsContainer?: HTMLElement;
31 | tagCloseIcon?: string;
32 | tagClass?: string;
33 | validator: Function;
34 | onlyTagList?: bool;
35 | }
36 |
37 | interface ITypeaheadOverrides {
38 | instanceSelectHandler?: Function;
39 | selectedClass?: string;
40 | select?: Function;
41 | }
42 |
43 | interface ITagsManager {
44 | tagManagerOptions: ITagsManagerOptions;
45 | obj: JQuery;
46 | objName: string;
47 | queuedTag: string;
48 | delimiters: number[];
49 | backspace: number[];
50 | tagToManipulate: string;
51 |
52 | initialize(context: JQuery,
53 | options?: ITagsManagerOptions,
54 | tagToManipulate?: string): void;
55 | setupTypeahead(): void;
56 | onTypeaheadAjaxSuccess(data: any, isSetTypeaheadSource: bool, process?: Function): void;
57 | ajaxPolling(query: string, process: Function): void;
58 | setTypeaheadSource(source: any): void;
59 | trimTag(tag: string): string;
60 | popTag(): void;
61 | empty(): void;
62 | refreshHiddenTagList(): void;
63 | spliceTag(tagId: number, eventData: any): void;
64 | pushTag(tag: string, objToPush: any, isValid: bool): void;
65 |
66 | setOptions(options: ITagsManagerOptions): void;
67 | setContext(context: JQuery, tagToManipulate?: string): void;
68 | processCommand(context: JQuery, command: string, tagToManipulate?: string): JQuery;
69 | processTags(command?: string, context?: JQuery, tagToManipulate?: string): JQuery;
70 | }
71 |
72 | interface JQuery {
73 | tagsManager(options?: ITagsManagerOptions): JQuery;
74 | tagsManager(command: string, tagToManipulate?: string): JQuery;
75 | }
--------------------------------------------------------------------------------
/license.txt:
--------------------------------------------------------------------------------
1 | Mozilla Public License Version 2.0
2 | ==================================
3 |
4 | 1. Definitions
5 | --------------
6 |
7 | 1.1. "Contributor"
8 | means each individual or legal entity that creates, contributes to
9 | the creation of, or owns Covered Software.
10 |
11 | 1.2. "Contributor Version"
12 | means the combination of the Contributions of others (if any) used
13 | by a Contributor and that particular Contributor's Contribution.
14 |
15 | 1.3. "Contribution"
16 | means Covered Software of a particular Contributor.
17 |
18 | 1.4. "Covered Software"
19 | means Source Code Form to which the initial Contributor has attached
20 | the notice in Exhibit A, the Executable Form of such Source Code
21 | Form, and Modifications of such Source Code Form, in each case
22 | including portions thereof.
23 |
24 | 1.5. "Incompatible With Secondary Licenses"
25 | means
26 |
27 | (a) that the initial Contributor has attached the notice described
28 | in Exhibit B to the Covered Software; or
29 |
30 | (b) that the Covered Software was made available under the terms of
31 | version 1.1 or earlier of the License, but not also under the
32 | terms of a Secondary License.
33 |
34 | 1.6. "Executable Form"
35 | means any form of the work other than Source Code Form.
36 |
37 | 1.7. "Larger Work"
38 | means a work that combines Covered Software with other material, in
39 | a separate file or files, that is not Covered Software.
40 |
41 | 1.8. "License"
42 | means this document.
43 |
44 | 1.9. "Licensable"
45 | means having the right to grant, to the maximum extent possible,
46 | whether at the time of the initial grant or subsequently, any and
47 | all of the rights conveyed by this License.
48 |
49 | 1.10. "Modifications"
50 | means any of the following:
51 |
52 | (a) any file in Source Code Form that results from an addition to,
53 | deletion from, or modification of the contents of Covered
54 | Software; or
55 |
56 | (b) any new file in Source Code Form that contains any Covered
57 | Software.
58 |
59 | 1.11. "Patent Claims" of a Contributor
60 | means any patent claim(s), including without limitation, method,
61 | process, and apparatus claims, in any patent Licensable by such
62 | Contributor that would be infringed, but for the grant of the
63 | License, by the making, using, selling, offering for sale, having
64 | made, import, or transfer of either its Contributions or its
65 | Contributor Version.
66 |
67 | 1.12. "Secondary License"
68 | means either the GNU General Public License, Version 2.0, the GNU
69 | Lesser General Public License, Version 2.1, the GNU Affero General
70 | Public License, Version 3.0, or any later versions of those
71 | licenses.
72 |
73 | 1.13. "Source Code Form"
74 | means the form of the work preferred for making modifications.
75 |
76 | 1.14. "You" (or "Your")
77 | means an individual or a legal entity exercising rights under this
78 | License. For legal entities, "You" includes any entity that
79 | controls, is controlled by, or is under common control with You. For
80 | purposes of this definition, "control" means (a) the power, direct
81 | or indirect, to cause the direction or management of such entity,
82 | whether by contract or otherwise, or (b) ownership of more than
83 | fifty percent (50%) of the outstanding shares or beneficial
84 | ownership of such entity.
85 |
86 | 2. License Grants and Conditions
87 | --------------------------------
88 |
89 | 2.1. Grants
90 |
91 | Each Contributor hereby grants You a world-wide, royalty-free,
92 | non-exclusive license:
93 |
94 | (a) under intellectual property rights (other than patent or trademark)
95 | Licensable by such Contributor to use, reproduce, make available,
96 | modify, display, perform, distribute, and otherwise exploit its
97 | Contributions, either on an unmodified basis, with Modifications, or
98 | as part of a Larger Work; and
99 |
100 | (b) under Patent Claims of such Contributor to make, use, sell, offer
101 | for sale, have made, import, and otherwise transfer either its
102 | Contributions or its Contributor Version.
103 |
104 | 2.2. Effective Date
105 |
106 | The licenses granted in Section 2.1 with respect to any Contribution
107 | become effective for each Contribution on the date the Contributor first
108 | distributes such Contribution.
109 |
110 | 2.3. Limitations on Grant Scope
111 |
112 | The licenses granted in this Section 2 are the only rights granted under
113 | this License. No additional rights or licenses will be implied from the
114 | distribution or licensing of Covered Software under this License.
115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a
116 | Contributor:
117 |
118 | (a) for any code that a Contributor has removed from Covered Software;
119 | or
120 |
121 | (b) for infringements caused by: (i) Your and any other third party's
122 | modifications of Covered Software, or (ii) the combination of its
123 | Contributions with other software (except as part of its Contributor
124 | Version); or
125 |
126 | (c) under Patent Claims infringed by Covered Software in the absence of
127 | its Contributions.
128 |
129 | This License does not grant any rights in the trademarks, service marks,
130 | or logos of any Contributor (except as may be necessary to comply with
131 | the notice requirements in Section 3.4).
132 |
133 | 2.4. Subsequent Licenses
134 |
135 | No Contributor makes additional grants as a result of Your choice to
136 | distribute the Covered Software under a subsequent version of this
137 | License (see Section 10.2) or under the terms of a Secondary License (if
138 | permitted under the terms of Section 3.3).
139 |
140 | 2.5. Representation
141 |
142 | Each Contributor represents that the Contributor believes its
143 | Contributions are its original creation(s) or it has sufficient rights
144 | to grant the rights to its Contributions conveyed by this License.
145 |
146 | 2.6. Fair Use
147 |
148 | This License is not intended to limit any rights You have under
149 | applicable copyright doctrines of fair use, fair dealing, or other
150 | equivalents.
151 |
152 | 2.7. Conditions
153 |
154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
155 | in Section 2.1.
156 |
157 | 3. Responsibilities
158 | -------------------
159 |
160 | 3.1. Distribution of Source Form
161 |
162 | All distribution of Covered Software in Source Code Form, including any
163 | Modifications that You create or to which You contribute, must be under
164 | the terms of this License. You must inform recipients that the Source
165 | Code Form of the Covered Software is governed by the terms of this
166 | License, and how they can obtain a copy of this License. You may not
167 | attempt to alter or restrict the recipients' rights in the Source Code
168 | Form.
169 |
170 | 3.2. Distribution of Executable Form
171 |
172 | If You distribute Covered Software in Executable Form then:
173 |
174 | (a) such Covered Software must also be made available in Source Code
175 | Form, as described in Section 3.1, and You must inform recipients of
176 | the Executable Form how they can obtain a copy of such Source Code
177 | Form by reasonable means in a timely manner, at a charge no more
178 | than the cost of distribution to the recipient; and
179 |
180 | (b) You may distribute such Executable Form under the terms of this
181 | License, or sublicense it under different terms, provided that the
182 | license for the Executable Form does not attempt to limit or alter
183 | the recipients' rights in the Source Code Form under this License.
184 |
185 | 3.3. Distribution of a Larger Work
186 |
187 | You may create and distribute a Larger Work under terms of Your choice,
188 | provided that You also comply with the requirements of this License for
189 | the Covered Software. If the Larger Work is a combination of Covered
190 | Software with a work governed by one or more Secondary Licenses, and the
191 | Covered Software is not Incompatible With Secondary Licenses, this
192 | License permits You to additionally distribute such Covered Software
193 | under the terms of such Secondary License(s), so that the recipient of
194 | the Larger Work may, at their option, further distribute the Covered
195 | Software under the terms of either this License or such Secondary
196 | License(s).
197 |
198 | 3.4. Notices
199 |
200 | You may not remove or alter the substance of any license notices
201 | (including copyright notices, patent notices, disclaimers of warranty,
202 | or limitations of liability) contained within the Source Code Form of
203 | the Covered Software, except that You may alter any license notices to
204 | the extent required to remedy known factual inaccuracies.
205 |
206 | 3.5. Application of Additional Terms
207 |
208 | You may choose to offer, and to charge a fee for, warranty, support,
209 | indemnity or liability obligations to one or more recipients of Covered
210 | Software. However, You may do so only on Your own behalf, and not on
211 | behalf of any Contributor. You must make it absolutely clear that any
212 | such warranty, support, indemnity, or liability obligation is offered by
213 | You alone, and You hereby agree to indemnify every Contributor for any
214 | liability incurred by such Contributor as a result of warranty, support,
215 | indemnity or liability terms You offer. You may include additional
216 | disclaimers of warranty and limitations of liability specific to any
217 | jurisdiction.
218 |
219 | 4. Inability to Comply Due to Statute or Regulation
220 | ---------------------------------------------------
221 |
222 | If it is impossible for You to comply with any of the terms of this
223 | License with respect to some or all of the Covered Software due to
224 | statute, judicial order, or regulation then You must: (a) comply with
225 | the terms of this License to the maximum extent possible; and (b)
226 | describe the limitations and the code they affect. Such description must
227 | be placed in a text file included with all distributions of the Covered
228 | Software under this License. Except to the extent prohibited by statute
229 | or regulation, such description must be sufficiently detailed for a
230 | recipient of ordinary skill to be able to understand it.
231 |
232 | 5. Termination
233 | --------------
234 |
235 | 5.1. The rights granted under this License will terminate automatically
236 | if You fail to comply with any of its terms. However, if You become
237 | compliant, then the rights granted under this License from a particular
238 | Contributor are reinstated (a) provisionally, unless and until such
239 | Contributor explicitly and finally terminates Your grants, and (b) on an
240 | ongoing basis, if such Contributor fails to notify You of the
241 | non-compliance by some reasonable means prior to 60 days after You have
242 | come back into compliance. Moreover, Your grants from a particular
243 | Contributor are reinstated on an ongoing basis if such Contributor
244 | notifies You of the non-compliance by some reasonable means, this is the
245 | first time You have received notice of non-compliance with this License
246 | from such Contributor, and You become compliant prior to 30 days after
247 | Your receipt of the notice.
248 |
249 | 5.2. If You initiate litigation against any entity by asserting a patent
250 | infringement claim (excluding declaratory judgment actions,
251 | counter-claims, and cross-claims) alleging that a Contributor Version
252 | directly or indirectly infringes any patent, then the rights granted to
253 | You by any and all Contributors for the Covered Software under Section
254 | 2.1 of this License shall terminate.
255 |
256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all
257 | end user license agreements (excluding distributors and resellers) which
258 | have been validly granted by You or Your distributors under this License
259 | prior to termination shall survive termination.
260 |
261 | ************************************************************************
262 | * *
263 | * 6. Disclaimer of Warranty *
264 | * ------------------------- *
265 | * *
266 | * Covered Software is provided under this License on an "as is" *
267 | * basis, without warranty of any kind, either expressed, implied, or *
268 | * statutory, including, without limitation, warranties that the *
269 | * Covered Software is free of defects, merchantable, fit for a *
270 | * particular purpose or non-infringing. The entire risk as to the *
271 | * quality and performance of the Covered Software is with You. *
272 | * Should any Covered Software prove defective in any respect, You *
273 | * (not any Contributor) assume the cost of any necessary servicing, *
274 | * repair, or correction. This disclaimer of warranty constitutes an *
275 | * essential part of this License. No use of any Covered Software is *
276 | * authorized under this License except under this disclaimer. *
277 | * *
278 | ************************************************************************
279 |
280 | ************************************************************************
281 | * *
282 | * 7. Limitation of Liability *
283 | * -------------------------- *
284 | * *
285 | * Under no circumstances and under no legal theory, whether tort *
286 | * (including negligence), contract, or otherwise, shall any *
287 | * Contributor, or anyone who distributes Covered Software as *
288 | * permitted above, be liable to You for any direct, indirect, *
289 | * special, incidental, or consequential damages of any character *
290 | * including, without limitation, damages for lost profits, loss of *
291 | * goodwill, work stoppage, computer failure or malfunction, or any *
292 | * and all other commercial damages or losses, even if such party *
293 | * shall have been informed of the possibility of such damages. This *
294 | * limitation of liability shall not apply to liability for death or *
295 | * personal injury resulting from such party's negligence to the *
296 | * extent applicable law prohibits such limitation. Some *
297 | * jurisdictions do not allow the exclusion or limitation of *
298 | * incidental or consequential damages, so this exclusion and *
299 | * limitation may not apply to You. *
300 | * *
301 | ************************************************************************
302 |
303 | 8. Litigation
304 | -------------
305 |
306 | Any litigation relating to this License may be brought only in the
307 | courts of a jurisdiction where the defendant maintains its principal
308 | place of business and such litigation shall be governed by laws of that
309 | jurisdiction, without reference to its conflict-of-law provisions.
310 | Nothing in this Section shall prevent a party's ability to bring
311 | cross-claims or counter-claims.
312 |
313 | 9. Miscellaneous
314 | ----------------
315 |
316 | This License represents the complete agreement concerning the subject
317 | matter hereof. If any provision of this License is held to be
318 | unenforceable, such provision shall be reformed only to the extent
319 | necessary to make it enforceable. Any law or regulation which provides
320 | that the language of a contract shall be construed against the drafter
321 | shall not be used to construe this License against a Contributor.
322 |
323 | 10. Versions of the License
324 | ---------------------------
325 |
326 | 10.1. New Versions
327 |
328 | Mozilla Foundation is the license steward. Except as provided in Section
329 | 10.3, no one other than the license steward has the right to modify or
330 | publish new versions of this License. Each version will be given a
331 | distinguishing version number.
332 |
333 | 10.2. Effect of New Versions
334 |
335 | You may distribute the Covered Software under the terms of the version
336 | of the License under which You originally received the Covered Software,
337 | or under the terms of any subsequent version published by the license
338 | steward.
339 |
340 | 10.3. Modified Versions
341 |
342 | If you create software not governed by this License, and you want to
343 | create a new license for such software, you may create and use a
344 | modified version of this License if you rename the license and remove
345 | any references to the name of the license steward (except to note that
346 | such modified license differs from this License).
347 |
348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary
349 | Licenses
350 |
351 | If You choose to distribute Source Code Form that is Incompatible With
352 | Secondary Licenses under the terms of this version of the License, the
353 | notice described in Exhibit B of this License must be attached.
354 |
355 | Exhibit A - Source Code Form License Notice
356 | -------------------------------------------
357 |
358 | This Source Code Form is subject to the terms of the Mozilla Public
359 | License, v. 2.0. If a copy of the MPL was not distributed with this
360 | file, You can obtain one at http://mozilla.org/MPL/2.0/.
361 |
362 | If it is not possible or desirable to put the notice in a particular
363 | file, then You may include the notice in a location (such as a LICENSE
364 | file in a relevant directory) where a recipient would be likely to look
365 | for such a notice.
366 |
367 | You may add additional accurate notices of copyright ownership.
368 |
369 | Exhibit B - "Incompatible With Secondary Licenses" Notice
370 | ---------------------------------------------------------
371 |
372 | This Source Code Form is "Incompatible With Secondary Licenses", as
373 | defined by the Mozilla Public License, v. 2.0.
374 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "TagManager",
3 | "version": "3.0.1",
4 | "title": "Tag Manager",
5 | "author": {
6 | "name": "Max Favilli",
7 | "url": "http://maxfavilli.com"
8 | },
9 | "licenses": [
10 | {
11 | "type": "MPL",
12 | "url": "http://www.mozilla.org/MPL/2.0/index.txt"
13 | }
14 | ],
15 | "dependencies": {
16 | "jquery": "1"
17 | },
18 | "description": "A jQuery plugin to create tag input fields, which works nicely with Twitter Typeahead.js and Twitter Bootstrap",
19 | "keywords": [
20 | "tags",
21 | "manager",
22 | "input",
23 | "form"
24 | ],
25 | "homepage": "https://github.com/max-favilli/tagmanager",
26 | "maintainers": [
27 | {
28 | "name": "Max Favilli",
29 | "url": "https://github.com/max-favilli"
30 | },{
31 | "name": "Johnny Shields",
32 | "url": "https://github.com/johnnyshields"
33 | }
34 | ],
35 | "files": [
36 | "README.md",
37 | "tagmanager.css",
38 | "tagmanager.js",
39 | "license.txt"
40 | ]
41 | }
42 |
--------------------------------------------------------------------------------
/tagmanager.css:
--------------------------------------------------------------------------------
1 | /*
2 | * DO NOT EDIT THIS FILE DIRECTLY
3 | * Compiled from bootstrap-tagmanager.less based on Bootstrap 2.3.1 variables
4 | * https://github.com/twitter/bootstrap/blob/master/less/variables.less
5 | */
6 | .tm-tag {
7 | color: #555555;
8 | background-color: #f5f5f5;
9 | border: #bbbbbb 1px solid;
10 | box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset;
11 | display: inline-block;
12 | border-radius: 3px;
13 | font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
14 | font-size: 13px;
15 | margin: 0 5px 5px 0;
16 | padding: 4px;
17 | text-decoration: none;
18 | transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
19 | -moz-transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
20 | -webkit-transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
21 | vertical-align: middle;
22 | }
23 | .tm-tag .tm-tag-remove {
24 | color: #000000;
25 | font-weight: bold;
26 | margin-left: 4px;
27 | opacity: 0.2;
28 | }
29 | .tm-tag .tm-tag-remove:hover {
30 | color: #000000;
31 | text-decoration: none;
32 | opacity: 0.4;
33 | }
34 | .tm-tag.tm-tag-warning {
35 | color: #945203;
36 | background-color: #f2c889;
37 | border-color: #f0a12f;
38 | }
39 | .tm-tag.tm-tag-error {
40 | color: #84212e;
41 | background-color: #e69ca6;
42 | border-color: #d24a5d;
43 | }
44 | .tm-tag.tm-tag-success {
45 | color: #638421;
46 | background-color: #cde69c;
47 | border-color: #a5d24a;
48 | }
49 | .tm-tag.tm-tag-info {
50 | color: #4594b5;
51 | background-color: #c5eefa;
52 | border-color: #5dc8f7;
53 | }
54 | .tm-tag.tm-tag-inverse {
55 | color: #cccccc;
56 | background-color: #555555;
57 | border-color: #333333;
58 | box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2) inset;
59 | }
60 | .tm-tag.tm-tag-inverse .tm-tag-remove {
61 | color: #ffffff;
62 | }
63 | .tm-tag.tm-tag-large {
64 | font-size: 16.25px;
65 | border-radius: 4px;
66 | padding: 11px 7px;
67 | }
68 | .tm-tag.tm-tag-small {
69 | font-size: 11.049999999999999px;
70 | border-radius: 3px;
71 | padding: 2px 4px;
72 | }
73 | .tm-tag.tm-tag-mini {
74 | font-size: 9.75px;
75 | border-radius: 2px;
76 | padding: 0px 2px;
77 | }
78 | .tm-tag.tm-tag-plain {
79 | color: #333333;
80 | box-shadow: none;
81 | background: none;
82 | border: none;
83 | }
84 | .tm-tag.tm-tag-disabled {
85 | color: #aaaaaa;
86 | background-color: #e6e6e6;
87 | border-color: #cccccc;
88 | box-shadow: none;
89 | }
90 | .tm-tag.tm-tag-disabled .tm-tag-remove {
91 | display: none;
92 | }
93 | input[type="text"].tm-input {
94 | margin-bottom: 5px;
95 | vertical-align: middle !important;
96 | }
97 | .control-group.tm-group {
98 | margin-bottom: 5px;
99 | }
100 | .form-horizontal .control-group.tm-group {
101 | margin-bottom: 15px;
102 | }
103 |
--------------------------------------------------------------------------------
/tagmanager.js:
--------------------------------------------------------------------------------
1 | /* ===================================================
2 | * tagmanager.js v3.0.2
3 | * http://welldonethings.com/tags/manager
4 | * ===================================================
5 | * Copyright 2012 Max Favilli
6 | *
7 | * Licensed under the Mozilla Public License, Version 2.0 You may not use this work except in compliance with the License.
8 | *
9 | * http://www.mozilla.org/MPL/2.0/
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | * ========================================================== */
17 | (function ($) {
18 |
19 | "use strict";
20 |
21 | var defaults = {
22 | prefilled: null,
23 | CapitalizeFirstLetter: false,
24 | preventSubmitOnEnter: true, // deprecated
25 | isClearInputOnEsc: true, // deprecated
26 | externalTagId: false,
27 | prefillIdFieldName: 'Id',
28 | prefillValueFieldName: 'Value',
29 | AjaxPush: null,
30 | AjaxPushAllTags: null,
31 | AjaxPushParameters: null,
32 | delimiters: [9, 13, 44], // tab, enter, comma
33 | backspace: [8],
34 | maxTags: 0,
35 | hiddenTagListName: null, // deprecated
36 | hiddenTagListId: null, // deprecated
37 | replace: true,
38 | output: null,
39 | deleteTagsOnBackspace: true, // deprecated
40 | tagsContainer: null,
41 | tagCloseIcon: 'x',
42 | tagClass: '',
43 | validator: null,
44 | onlyTagList: false,
45 | tagList: null,
46 | fillInputOnTagRemove: false,
47 | AjaxPushDataType: 'json' //allow plugin to send data using different encodings (xml, json, script, text, html)
48 | },
49 |
50 | publicMethods = {
51 | pushTag: function (tag, ignoreEvents, externalTagId, ignoreValidator) {
52 | var $self = $(this), opts = $self.data('opts'), alreadyInList, tlisLowerCase, max, tagId,
53 | tlis = $self.data("tlis"), tlid = $self.data("tlid"), idx, newTagId, newTagRemoveId, escaped,
54 | html, $el, lastTagId, lastTagObj;
55 |
56 | tag = privateMethods.trimTag(tag, opts.delimiterChars);
57 |
58 | if (!tag || tag.length <= 0) { return; }
59 |
60 | // check if restricted only to the tagList suggestions
61 | if (opts.onlyTagList && undefined !== opts.tagList) {
62 |
63 | //if the list has been updated by look pushed tag in the tagList. if not found return
64 | if (opts.tagList) {
65 | var $tagList = opts.tagList;
66 |
67 | // change each array item to lower case
68 | $.each($tagList, function (index, item) {
69 | $tagList[index] = item.toLowerCase();
70 | });
71 | var suggestion = $.inArray(tag.toLowerCase(), $tagList);
72 |
73 | if (-1 === suggestion) {
74 | //console.log("tag:" + tag + " not in tagList, not adding it");
75 | return;
76 | }
77 | }
78 |
79 | }
80 |
81 | if (opts.CapitalizeFirstLetter && tag.length > 1) {
82 | tag = tag.charAt(0).toUpperCase() + tag.slice(1).toLowerCase();
83 | }
84 |
85 | // call the validator (if any) and do not let the tag pass if invalid
86 | if (!ignoreValidator && opts.validator && !opts.validator(tag)) {
87 | $self.trigger('tm:invalid', tag)
88 | return;
89 | }
90 |
91 | // dont accept new tags beyond the defined maximum
92 | if (opts.maxTags > 0 && tlis.length >= opts.maxTags) { return; }
93 |
94 | alreadyInList = false;
95 | //use jQuery.map to make this work in IE8 (pure JS map is JS 1.6 but IE8 only supports JS 1.5)
96 | tlisLowerCase = jQuery.map(tlis, function (elem) {
97 | return elem.toLowerCase();
98 | });
99 |
100 | idx = $.inArray(tag.toLowerCase(), tlisLowerCase);
101 |
102 | if (-1 !== idx) {
103 | // console.log("tag:" + tag + " !!already in list!!");
104 | alreadyInList = true;
105 | }
106 |
107 | if (alreadyInList) {
108 | $self.trigger('tm:duplicated', tag);
109 | if (opts.blinkClass) {
110 | for (var i = 0; i < 6; ++i) {
111 | $("#" + $self.data("tm_rndid") + "_" + tlid[idx]).queue(function (next) {
112 | $(this).toggleClass(opts.blinkClass);
113 | next();
114 | }).delay(100);
115 | }
116 | } else {
117 | $("#" + $self.data("tm_rndid") + "_" + tlid[idx]).stop()
118 | .animate({ backgroundColor: opts.blinkBGColor_1 }, 100)
119 | .animate({ backgroundColor: opts.blinkBGColor_2 }, 100)
120 | .animate({ backgroundColor: opts.blinkBGColor_1 }, 100)
121 | .animate({ backgroundColor: opts.blinkBGColor_2 }, 100)
122 | .animate({ backgroundColor: opts.blinkBGColor_1 }, 100)
123 | .animate({ backgroundColor: opts.blinkBGColor_2 }, 100);
124 | }
125 | } else {
126 | if (opts.externalTagId === true) {
127 | if (externalTagId === undefined) {
128 | $.error('externalTagId is not passed for tag -' + tag);
129 | }
130 | tagId = externalTagId;
131 | } else {
132 | max = Math.max.apply(null, tlid);
133 | max = max === -Infinity ? 0 : max;
134 |
135 | tagId = ++max;
136 | }
137 | if (!ignoreEvents) { $self.trigger('tm:pushing', [tag, tagId]); }
138 | tlis.push(tag);
139 | tlid.push(tagId);
140 |
141 | if (!ignoreEvents)
142 | if (opts.AjaxPush !== null && opts.AjaxPushAllTags == null) {
143 | if ($.inArray(tag, opts.prefilled) === -1) {
144 | $.post(opts.AjaxPush, $.extend({ tag: tag }, opts.AjaxPushParameters), null, opts.AjaxPushDataType);
145 | }
146 | }
147 |
148 | // console.log("tagList: " + tlis);
149 |
150 | newTagId = $self.data("tm_rndid") + '_' + tagId;
151 | newTagRemoveId = $self.data("tm_rndid") + '_Remover_' + tagId;
152 | escaped = $("").text(tag).html();
153 |
154 | html = '';
155 | html += '' + escaped + '';
156 | html += '';
157 | html += opts.tagCloseIcon + ' ';
158 | $el = $(html);
159 |
160 |
161 | var typeAheadMess = $self.parents('.twitter-typeahead')[0] !== undefined;
162 | if (opts.tagsContainer !== null) {
163 | $(opts.tagsContainer).append($el);
164 | } else {
165 | if (tlid.length > 1) {
166 | if (typeAheadMess) {
167 | var lastTagId = $self.data("tm_rndid") + '_' + --tagId;
168 | jQuery('#' + lastTagId).after($el);
169 | } else {
170 | lastTagObj = $self.siblings("#" + $self.data("tm_rndid") + "_" + tlid[tlid.length - 2]);
171 | lastTagObj.after($el);
172 | }
173 | } else {
174 | if (typeAheadMess) {
175 | $self.parents('.twitter-typeahead').before($el);
176 | } else {
177 | $self.before($el);
178 | }
179 | }
180 | }
181 |
182 | $el.find("#" + newTagRemoveId).on("click", $self, function (e) {
183 | e.preventDefault();
184 | var TagIdToRemove = parseInt($(this).attr("TagIdToRemove"));
185 | privateMethods.spliceTag.call($self, TagIdToRemove, e.data);
186 | });
187 |
188 | privateMethods.refreshHiddenTagList.call($self);
189 |
190 | if (!ignoreEvents) { $self.trigger('tm:pushed', [tag, tagId]); }
191 |
192 | privateMethods.showOrHide.call($self);
193 | //if (tagManagerOptions.maxTags > 0 && tlis.length >= tagManagerOptions.maxTags) {
194 | // obj.hide();
195 | //}
196 | }
197 | $self.val("");
198 | },
199 |
200 | popTag: function () {
201 | var $self = $(this), tagId, tagBeingRemoved,
202 | tlis = $self.data("tlis"),
203 | tlid = $self.data("tlid");
204 |
205 | if (tlid.length > 0) {
206 | tagId = tlid.pop();
207 |
208 | tagBeingRemoved = tlis[tlis.length - 1];
209 | $self.trigger('tm:popping', [tagBeingRemoved, tagId]);
210 | tlis.pop();
211 |
212 | // console.log("TagIdToRemove: " + tagId);
213 | $("#" + $self.data("tm_rndid") + "_" + tagId).remove();
214 | privateMethods.refreshHiddenTagList.call($self);
215 | $self.trigger('tm:popped', [tagBeingRemoved, tagId]);
216 |
217 | privateMethods.showOrHide.call($self);
218 | // console.log(tlis);
219 | }
220 | },
221 |
222 | empty: function () {
223 | var $self = $(this), tlis = $self.data("tlis"), tlid = $self.data("tlid"), tagId;
224 |
225 | while (tlid.length > 0) {
226 | tagId = tlid.pop();
227 | tlis.pop();
228 | // console.log("TagIdToRemove: " + tagId);
229 | $("#" + $self.data("tm_rndid") + "_" + tagId).remove();
230 | privateMethods.refreshHiddenTagList.call($self);
231 | // console.log(tlis);
232 | }
233 | $self.trigger('tm:emptied', null);
234 |
235 | privateMethods.showOrHide.call($self);
236 | //if (tagManagerOptions.maxTags > 0 && tlis.length < tagManagerOptions.maxTags) {
237 | // obj.show();
238 | //}
239 | },
240 |
241 | tags: function () {
242 | var $self = this, tlis = $self.data("tlis");
243 | return tlis;
244 | }
245 | },
246 |
247 | privateMethods = {
248 | showOrHide: function () {
249 | var $self = this, opts = $self.data('opts'), tlis = $self.data("tlis");
250 |
251 | if (opts.maxTags > 0 && tlis.length < opts.maxTags) {
252 | $self.show();
253 | $self.trigger('tm:show');
254 | }
255 |
256 | if (opts.maxTags > 0 && tlis.length >= opts.maxTags) {
257 | $self.hide();
258 | $self.trigger('tm:hide');
259 | }
260 | },
261 |
262 | tagClasses: function () {
263 | var $self = $(this), opts = $self.data('opts'), tagBaseClass = opts.tagBaseClass,
264 | inputBaseClass = opts.inputBaseClass, cl;
265 | // 1) default class (tm-tag)
266 | cl = tagBaseClass;
267 | // 2) interpolate from input class: tm-input-xxx --> tm-tag-xxx
268 | if ($self.attr('class')) {
269 | $.each($self.attr('class').split(' '), function (index, value) {
270 | if (value.indexOf(inputBaseClass + '-') !== -1) {
271 | cl += ' ' + tagBaseClass + value.substring(inputBaseClass.length);
272 | }
273 | });
274 | }
275 | // 3) tags from tagClass option
276 | cl += (opts.tagClass ? ' ' + opts.tagClass : '');
277 | return cl;
278 | },
279 |
280 | trimTag: function (tag, delimiterChars) {
281 | var i;
282 | tag = $.trim(tag);
283 | // truncate at the first delimiter char
284 | i = 0;
285 | for (i; i < tag.length; i++) {
286 | if ($.inArray(tag.charCodeAt(i), delimiterChars) !== -1) { break; }
287 | }
288 | return tag.substring(0, i);
289 | },
290 |
291 | refreshHiddenTagList: function () {
292 | var $self = $(this), tlis = $self.data("tlis"), lhiddenTagList = $self.data("lhiddenTagList");
293 |
294 | if (lhiddenTagList) {
295 | $(lhiddenTagList).val(tlis.join($self.data('opts').baseDelimiter)).change();
296 | }
297 |
298 | $self.trigger('tm:refresh', tlis.join($self.data('opts').baseDelimiter));
299 | },
300 |
301 | killEvent: function (e) {
302 | e.cancelBubble = true;
303 | e.returnValue = false;
304 | e.stopPropagation();
305 | e.preventDefault();
306 | },
307 |
308 | keyInArray: function (e, ary) {
309 | return $.inArray(e.which, ary) !== -1;
310 | },
311 |
312 | applyDelimiter: function (e) {
313 | var $self = $(this);
314 | publicMethods.pushTag.call($self, $(this).val());
315 | e.preventDefault();
316 | },
317 |
318 | prefill: function (pta) {
319 | var $self = $(this);
320 | var opts = $self.data('opts')
321 | $.each(pta, function (key, val) {
322 | if (opts.externalTagId === true) {
323 | publicMethods.pushTag.call($self, val[opts.prefillValueFieldName], true, val[opts.prefillIdFieldName], true);
324 | } else {
325 | publicMethods.pushTag.call($self, val, true, false, true);
326 | }
327 | });
328 | },
329 |
330 | pushAllTags: function (e, tag) {
331 | var $self = $(this), opts = $self.data('opts'), tlis = $self.data("tlis");
332 | if (opts.AjaxPushAllTags) {
333 | if (e.type !== 'tm:pushed' || $.inArray(tag, opts.prefilled) === -1) {
334 | $.post(opts.AjaxPush, $.extend({ tags: tlis.join(opts.baseDelimiter) }, opts.AjaxPushParameters));
335 | }
336 | }
337 | },
338 |
339 | spliceTag: function (tagId) {
340 | var $self = this, tlis = $self.data("tlis"), tlid = $self.data("tlid"), idx = $.inArray(tagId, tlid),
341 | tagBeingRemoved;
342 |
343 | // console.log("TagIdToRemove: " + tagId);
344 | // console.log("position: " + idx);
345 |
346 | if (-1 !== idx) {
347 | tagBeingRemoved = tlis[idx];
348 | $self.trigger('tm:splicing', [tagBeingRemoved, tagId]);
349 | $("#" + $self.data("tm_rndid") + "_" + tagId).remove();
350 | tlis.splice(idx, 1);
351 | tlid.splice(idx, 1);
352 | privateMethods.refreshHiddenTagList.call($self);
353 | $self.trigger('tm:spliced', [tagBeingRemoved, tagId]);
354 | // console.log(tlis);
355 | }
356 |
357 | privateMethods.showOrHide.call($self);
358 | //if (tagManagerOptions.maxTags > 0 && tlis.length < tagManagerOptions.maxTags) {
359 | // obj.show();
360 | //}
361 | },
362 |
363 | init: function (options) {
364 | var opts = $.extend({}, defaults, options), delimiters, keyNums;
365 |
366 | opts.hiddenTagListName = (opts.hiddenTagListName === null)
367 | ? 'hidden-' + this.attr('name')
368 | : opts.hiddenTagListName;
369 |
370 | delimiters = opts.delimeters || opts.delimiters; // 'delimeter' is deprecated
371 | keyNums = [9, 13, 17, 18, 19, 37, 38, 39, 40]; // delimiter values to be handled as key codes
372 | opts.delimiterChars = [];
373 | opts.delimiterKeys = [];
374 |
375 | $.each(delimiters, function (i, v) {
376 | if ($.inArray(v, keyNums) !== -1) {
377 | opts.delimiterKeys.push(v);
378 | } else {
379 | opts.delimiterChars.push(v);
380 | }
381 | });
382 |
383 | opts.baseDelimiter = String.fromCharCode(opts.delimiterChars[0] || 44);
384 | opts.tagBaseClass = 'tm-tag';
385 | opts.inputBaseClass = 'tm-input';
386 |
387 | if (!$.isFunction(opts.validator)) { opts.validator = null; }
388 |
389 | this.each(function () {
390 | var $self = $(this), hiddenObj = '', rndid = '', albet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
391 |
392 | // prevent double-initialization of TagManager
393 | if ($self.data('tagManager')) { return false; }
394 | $self.data('tagManager', true);
395 |
396 | for (var i = 0; i < 5; i++) {
397 | rndid += albet.charAt(Math.floor(Math.random() * albet.length));
398 | }
399 |
400 | $self.data("tm_rndid", rndid);
401 |
402 | // store instance-specific data in the DOM object
403 | $self.data('opts', opts)
404 | .data('tlis', []) //list of string tags
405 | .data('tlid', []); //list of ID of the string tags
406 |
407 | if (opts.output === null) {
408 | hiddenObj = $('', {
409 | type: 'hidden',
410 | name: opts.hiddenTagListName
411 | });
412 | $self.after(hiddenObj);
413 | $self.data("lhiddenTagList", hiddenObj);
414 | } else {
415 | $self.data("lhiddenTagList", $(opts.output));
416 | }
417 |
418 | if (opts.AjaxPushAllTags) {
419 | $self.on('tm:spliced', privateMethods.pushAllTags);
420 | $self.on('tm:popped', privateMethods.pushAllTags);
421 | $self.on('tm:pushed', privateMethods.pushAllTags);
422 | }
423 |
424 | // hide popovers on focus and keypress events
425 | $self.on('focus keypress', function (e) {
426 | if ($(this).popover) { $(this).popover('hide'); }
427 | });
428 |
429 | // handle ESC (keyup used for browser compatibility)
430 | if (opts.isClearInputOnEsc) {
431 | $self.on('keyup', function (e) {
432 | if (e.which === 27) {
433 | // console.log('esc detected');
434 | $(this).val('');
435 | privateMethods.killEvent(e);
436 | }
437 | });
438 | }
439 |
440 | $self.on('keypress', function (e) {
441 | // push ASCII-based delimiters
442 | if (privateMethods.keyInArray(e, opts.delimiterChars)) {
443 | privateMethods.applyDelimiter.call($self, e);
444 | }
445 | });
446 |
447 | $self.on('keydown', function (e) {
448 | // disable ENTER
449 | if (e.which === 13) {
450 | if (opts.preventSubmitOnEnter) {
451 | privateMethods.killEvent(e);
452 | }
453 | }
454 |
455 | // push key-based delimiters (includes by default)
456 | if (privateMethods.keyInArray(e, opts.delimiterKeys)) {
457 | privateMethods.applyDelimiter.call($self, e);
458 | }
459 | });
460 |
461 | // BACKSPACE (keydown used for browser compatibility)
462 | if (opts.deleteTagsOnBackspace) {
463 | $self.on('keydown', function (e) {
464 | if (privateMethods.keyInArray(e, opts.backspace)) {
465 | // console.log("backspace detected");
466 | if ($(this).val().length <= 0) {
467 | publicMethods.popTag.call($self);
468 | privateMethods.killEvent(e);
469 | }
470 | }
471 | });
472 | }
473 |
474 | // on tag pop fill back the tag's content to the input field
475 | if (opts.fillInputOnTagRemove) {
476 | $self.on('tm:popped', function (e, tag) {
477 | $(this).val(tag);
478 | });
479 | }
480 |
481 | $self.change(function (e) {
482 | if (!/webkit/.test(navigator.userAgent.toLowerCase())) {
483 | $self.focus();
484 | } // why?
485 |
486 | /* unimplemented mode to push tag on blur
487 | else if (tagManagerOptions.pushTagOnBlur) {
488 | console.log('change: pushTagOnBlur ' + tag);
489 | pushTag($(this).val());
490 | } */
491 | privateMethods.killEvent(e);
492 | });
493 |
494 | if (opts.prefilled !== null) {
495 | if (typeof (opts.prefilled) === "object") {
496 | privateMethods.prefill.call($self, opts.prefilled);
497 | } else if (typeof (opts.prefilled) === "string") {
498 | privateMethods.prefill.call($self, opts.prefilled.split(opts.baseDelimiter));
499 | } else if (typeof (opts.prefilled) === "function") {
500 | privateMethods.prefill.call($self, opts.prefilled());
501 | }
502 | } else if (opts.output !== null) {
503 | if ($(opts.output) && $(opts.output).val()) { var existing_tags = $(opts.output); }
504 | privateMethods.prefill.call($self, $(opts.output).val().split(opts.baseDelimiter));
505 | }
506 |
507 | });
508 |
509 | return this;
510 | }
511 | };
512 |
513 | $.fn.tagsManager = function (method) {
514 | var $self = $(this);
515 |
516 | if (!(0 in this)) { return this; }
517 |
518 | if (publicMethods[method]) {
519 | return publicMethods[method].apply($self, Array.prototype.slice.call(arguments, 1));
520 | } else if (typeof method === 'object' || !method) {
521 | return privateMethods.init.apply(this, arguments);
522 | } else {
523 | $.error('Method ' + method + ' does not exist.');
524 | return false;
525 | }
526 | };
527 |
528 | }(jQuery));
529 |
--------------------------------------------------------------------------------
/tagmanager.less:
--------------------------------------------------------------------------------
1 | //
2 | // Bootstrap TagManager
3 | // --------------------------------------------------
4 |
5 | // Tag Variables
6 | // --------------------------------------------------
7 |
8 | // Colors
9 | // -------------------------
10 |
11 | @white: #ffffff;
12 | @black: #000000;
13 | @gray: #555555;
14 | @grayDark: #333333;
15 |
16 | @textColor: @grayDark;
17 |
18 | @tagText: @gray;
19 | @tagBackground: #f5f5f5;
20 | @tagBorder: #bbb;
21 |
22 | @tagWarningText: #945203;
23 | @tagWarningBackground: #f2c889;
24 | @tagWarningBorder: #f0a12f;
25 |
26 | @tagErrorText: #84212e;
27 | @tagErrorBackground: #e69ca6;
28 | @tagErrorBorder: #d24a5d;
29 |
30 | @tagSuccessText: #638421;
31 | @tagSuccessBackground: #cde69c;
32 | @tagSuccessBorder: #a5d24a;
33 |
34 | @tagInfoText: #4594b5;
35 | @tagInfoBackground: #c5eefa;
36 | @tagInfoBorder: #5dc8f7;
37 |
38 | @tagInverseText: #ccc;
39 | @tagInverseBackground: @gray;
40 | @tagInverseBorder: @grayDark;
41 |
42 | @tagDisabledText: #aaa;
43 | @tagDisabledBackground: #e6e6e6;
44 | @tagDisabledBorder: #ccc;
45 |
46 | // Sizing
47 | // -------------------------
48 |
49 | @tagFontSize: 13px;
50 | @tagFontSizeLarge: @tagFontSize * 1.25; // ~16px
51 | @tagFontSizeSmall: @tagFontSize * 0.85; // ~11px
52 | @tagFontSizeMini: @tagFontSize * 0.75; // ~10px
53 |
54 | @tagPadding: 4px;
55 | @tagMargin: 5px;
56 |
57 | @borderRadiusSmall: 3px;
58 | @baseBorderRadius: 4px;
59 |
60 | @baseLineHeight: 20px;
61 |
62 | // Tag Classes
63 | // --------------------------------------------------
64 |
65 | // Fonts
66 | // --------------------------------------------------
67 |
68 | @sansFontFamily: "Helvetica Neue", Helvetica, Arial, sans-serif;
69 |
70 | // Base tag class
71 | // -------------------------
72 |
73 | .tm-tag {
74 | color: @tagText;
75 | background-color: @tagBackground;
76 | border: @tagBorder 1px solid;
77 | box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset;
78 | display: inline-block;
79 | border-radius: @borderRadiusSmall;
80 | font-family: @sansFontFamily;
81 | font-size: @tagFontSize;
82 | margin: 0 @tagMargin @tagMargin 0;
83 | padding: @tagPadding;
84 | text-decoration: none;
85 | transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
86 | -moz-transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
87 | -webkit-transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
88 | vertical-align: middle;
89 |
90 | // Remove button
91 | // -------------------------
92 |
93 | .tm-tag-remove {
94 | color: @black;
95 | font-weight: bold;
96 | margin-left: @tagPadding;
97 | opacity: 0.2;
98 | &:hover {
99 | color: @black;
100 | text-decoration: none;
101 | opacity: 0.4;
102 | }
103 | }
104 |
105 | // Semantic Colors
106 | // -------------------------
107 |
108 | &.tm-tag-warning {
109 | color: @tagWarningText;
110 | background-color: @tagWarningBackground;
111 | border-color: @tagWarningBorder;
112 | }
113 | &.tm-tag-error {
114 | color: @tagErrorText;
115 | background-color: @tagErrorBackground;
116 | border-color: @tagErrorBorder;
117 | }
118 | &.tm-tag-success {
119 | color: @tagSuccessText;
120 | background-color: @tagSuccessBackground;
121 | border-color: @tagSuccessBorder;
122 | }
123 | &.tm-tag-info {
124 | color: @tagInfoText;
125 | background-color: @tagInfoBackground;
126 | border-color: @tagInfoBorder;
127 | }
128 | &.tm-tag-inverse {
129 | color: @tagInverseText;
130 | background-color: @tagInverseBackground;
131 | border-color: @tagInverseBorder;
132 | box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2) inset;
133 | .tm-tag-remove {
134 | color: @white;
135 | }
136 | }
137 |
138 | // Sizes
139 | // -------------------------
140 |
141 | &.tm-tag-large {
142 | font-size: @tagFontSizeLarge;
143 | border-radius: @baseBorderRadius;
144 | padding: 11px 7px;
145 | }
146 | &.tm-tag-small {
147 | font-size: @tagFontSizeSmall;
148 | border-radius: @borderRadiusSmall;
149 | padding: 2px 4px;
150 | }
151 | &.tm-tag-mini {
152 | font-size: @tagFontSizeMini;
153 | border-radius: 2px;
154 | padding: 0px 2px;
155 | }
156 |
157 | // Miscellaneous Styles
158 | // -------------------------
159 |
160 | &.tm-tag-plain {
161 | color: @textColor;
162 | box-shadow: none;
163 | background: none;
164 | border: none;
165 | }
166 | &.tm-tag-disabled {
167 | color: @tagDisabledText;
168 | background-color: @tagDisabledBackground;
169 | border-color: @tagDisabledBorder;
170 | box-shadow: none;
171 | .tm-tag-remove {
172 | display: none;
173 | }
174 | }
175 | }
176 |
177 | // Forms
178 | // --------------------------------------------------
179 |
180 | // Input style (Recommended)
181 | // -------------------------
182 |
183 | input[type="text"].tm-input {
184 | margin-bottom: @tagMargin;
185 | }
186 |
187 | // Form wrappers (Optional)
188 | // -------------------------
189 |
190 | .control-group.tm-group {
191 | margin-bottom: (@baseLineHeight / 2) - @tagMargin;
192 | }
193 | .form-horizontal .control-group.tm-group {
194 | margin-bottom: @baseLineHeight - @tagMargin;
195 | }
196 |
--------------------------------------------------------------------------------
/tagmanager.scss:
--------------------------------------------------------------------------------
1 | //
2 | // Bootstrap TagManager
3 | // --------------------------------------------------
4 |
5 | // Tag Variables
6 | // --------------------------------------------------
7 |
8 | // Colors
9 | // -------------------------
10 |
11 | $white: #ffffff;
12 | $black: #000000;
13 | $gray: #555555;
14 | $grayDark: #333333;
15 |
16 | $textColor: $grayDark;
17 |
18 | $tagText: $gray;
19 | $tagBackground: #f5f5f5;
20 | $tagBorder: #bbb;
21 |
22 | $tagWarningText: #945203;
23 | $tagWarningBackground: #f2c889;
24 | $tagWarningBorder: #f0a12f;
25 |
26 | $tagErrorText: #84212e;
27 | $tagErrorBackground: #e69ca6;
28 | $tagErrorBorder: #d24a5d;
29 |
30 | $tagSuccessText: #638421;
31 | $tagSuccessBackground: #cde69c;
32 | $tagSuccessBorder: #a5d24a;
33 |
34 | $tagInfoText: #4594b5;
35 | $tagInfoBackground: #c5eefa;
36 | $tagInfoBorder: #5dc8f7;
37 |
38 | $tagInverseText: #ccc;
39 | $tagInverseBackground: $gray;
40 | $tagInverseBorder: $grayDark;
41 |
42 | $tagDisabledText: #aaa;
43 | $tagDisabledBackground: #e6e6e6;
44 | $tagDisabledBorder: #ccc;
45 |
46 | // Sizing
47 | // -------------------------
48 |
49 | $tagFontSize: 13px;
50 | $tagFontSizeLarge: $tagFontSize * 1.25; // ~16px
51 | $tagFontSizeSmall: $tagFontSize * 0.85; // ~11px
52 | $tagFontSizeMini: $tagFontSize * 0.75; // ~10px
53 |
54 | $tagPadding: 4px;
55 | $tagMargin: 5px;
56 |
57 | $borderRadiusSmall: 3px;
58 | $baseBorderRadius: 4px;
59 |
60 | $baseLineHeight: 20px;
61 |
62 | // Tag Classes
63 | // --------------------------------------------------
64 |
65 | // Fonts
66 | // --------------------------------------------------
67 |
68 | $sansFontFamily: "Helvetica Neue", Helvetica, Arial, sans-serif;
69 |
70 | // Base tag class
71 | // -------------------------
72 |
73 | .tm-tag {
74 | color: $tagText;
75 | background-color: $tagBackground;
76 | border: $tagBorder 1px solid;
77 | box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset;
78 | display: inline-block;
79 | border-radius: $borderRadiusSmall;
80 | font-family: $sansFontFamily;
81 | font-size: $tagFontSize;
82 | margin: 0 $tagMargin $tagMargin 0;
83 | padding: $tagPadding;
84 | text-decoration: none;
85 | transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
86 | -moz-transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
87 | -webkit-transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
88 | vertical-align: middle;
89 |
90 | // Remove button
91 | // -------------------------
92 |
93 | .tm-tag-remove {
94 | color: $black;
95 | font-weight: bold;
96 | margin-left: $tagPadding;
97 | opacity: 0.2;
98 | &:hover {
99 | color: $black;
100 | text-decoration: none;
101 | opacity: 0.4;
102 | }
103 | }
104 |
105 | // Semantic Colors
106 | // -------------------------
107 |
108 | &.tm-tag-warning {
109 | color: $tagWarningText;
110 | background-color: $tagWarningBackground;
111 | border-color: $tagWarningBorder;
112 | }
113 | &.tm-tag-error {
114 | color: $tagErrorText;
115 | background-color: $tagErrorBackground;
116 | border-color: $tagErrorBorder;
117 | }
118 | &.tm-tag-success {
119 | color: $tagSuccessText;
120 | background-color: $tagSuccessBackground;
121 | border-color: $tagSuccessBorder;
122 | }
123 | &.tm-tag-info {
124 | color: $tagInfoText;
125 | background-color: $tagInfoBackground;
126 | border-color: $tagInfoBorder;
127 | }
128 | &.tm-tag-inverse {
129 | color: $tagInverseText;
130 | background-color: $tagInverseBackground;
131 | border-color: $tagInverseBorder;
132 | box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2) inset;
133 | .tm-tag-remove {
134 | color: $white;
135 | }
136 | }
137 |
138 | // Sizes
139 | // -------------------------
140 |
141 | &.tm-tag-large {
142 | font-size: $tagFontSizeLarge;
143 | border-radius: $baseBorderRadius;
144 | padding: 11px 7px;
145 | }
146 | &.tm-tag-small {
147 | font-size: $tagFontSizeSmall;
148 | border-radius: $borderRadiusSmall;
149 | padding: 2px 4px;
150 | }
151 | &.tm-tag-mini {
152 | font-size: $tagFontSizeMini;
153 | border-radius: 2px;
154 | padding: 0px 2px;
155 | }
156 |
157 | // Miscellaneous Styles
158 | // -------------------------
159 |
160 | &.tm-tag-plain {
161 | color: $textColor;
162 | box-shadow: none;
163 | background: none;
164 | border: none;
165 | }
166 | &.tm-tag-disabled {
167 | color: $tagDisabledText;
168 | background-color: $tagDisabledBackground;
169 | border-color: $tagDisabledBorder;
170 | box-shadow: none;
171 | .tm-tag-remove {
172 | display: none;
173 | }
174 | }
175 | }
176 |
177 | // Forms
178 | // --------------------------------------------------
179 |
180 | // Input style (Recommended)
181 | // -------------------------
182 |
183 | input[type="text"].tm-input {
184 | margin-bottom: $tagMargin;
185 | }
186 |
187 | // Form wrappers (Optional)
188 | // -------------------------
189 |
190 | .control-group.tm-group {
191 | margin-bottom: ($baseLineHeight / 2) - $tagMargin;
192 | }
193 | .form-horizontal .control-group.tm-group {
194 | margin-bottom: $baseLineHeight - $tagMargin;
195 | }
196 |
--------------------------------------------------------------------------------