| 1 |
|
| 2 |
$(document).ready(function() {
|
| 3 |
|
| 4 |
$('.calais_keyword').click(function() {
|
| 5 |
var tags = $('#' + $(this).attr('for'));
|
| 6 |
var keyword = $(this).text();
|
| 7 |
|
| 8 |
// TODO: When we move to a revent jQuery, replace is() with hasClass()
|
| 9 |
if($(this).is('.calais_keyword_selected')) {
|
| 10 |
calaisRemoveKeyword(tags, keyword);
|
| 11 |
$(this).removeClass('calais_keyword_selected');
|
| 12 |
}
|
| 13 |
else {
|
| 14 |
calaisAddKeyword(tags, keyword);
|
| 15 |
$(this).addClass('calais_keyword_selected');
|
| 16 |
}
|
| 17 |
|
| 18 |
});
|
| 19 |
|
| 20 |
$('.calais_keyword').each( function() {
|
| 21 |
var tags = $('#' + $(this).attr('for'));
|
| 22 |
var keyword = $(this).text();
|
| 23 |
|
| 24 |
if (tags.val().indexOf(keyword) != -1) {
|
| 25 |
$(this).addClass('calais_keyword_selected');
|
| 26 |
}
|
| 27 |
|
| 28 |
});
|
| 29 |
});
|
| 30 |
|
| 31 |
/**
|
| 32 |
* Insert keyword, adding a comma if necessary
|
| 33 |
*/
|
| 34 |
function calaisAddKeyword(tags, keyword) {
|
| 35 |
var current = $.trim(tags.val());
|
| 36 |
if(current.indexOf(keyword) == -1) {
|
| 37 |
if(current == '') {
|
| 38 |
tags.val(keyword);
|
| 39 |
}
|
| 40 |
else{
|
| 41 |
tags.val(current + ',' + keyword);
|
| 42 |
}
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
| 46 |
/**
|
| 47 |
* Remove the keyword and cleanup any comma nonsense
|
| 48 |
*/
|
| 49 |
function calaisRemoveKeyword(tags, keyword) {
|
| 50 |
var current = $.trim(tags.val());
|
| 51 |
var index = current.indexOf(keyword);
|
| 52 |
if(index >= 0) {
|
| 53 |
// Deal with funky spaces around commas
|
| 54 |
current = current.replace(/ +,/, ',');
|
| 55 |
current = current.replace(/, +/, ',');
|
| 56 |
|
| 57 |
// Remove the keyword
|
| 58 |
current = current.replace(keyword, '');
|
| 59 |
|
| 60 |
// Deal with a remaining extra comma
|
| 61 |
current = current.replace(/^,/, '');
|
| 62 |
current = current.replace(/,$/, '');
|
| 63 |
current = current.replace(/,,/, ',');
|
| 64 |
tags.val(current);
|
| 65 |
}
|
| 66 |
}
|