bludit/bl-kernel/js/bludit-tags.js

65 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-02-13 06:50:49 +01:00
function insertTag() {
2017-11-01 19:38:56 +01:00
var newTag = sanitizeHTML( $("#jstagInput").val() );
2016-02-13 06:50:49 +01:00
2017-11-01 19:38:56 +01:00
if (newTag.trim()=="") {
2016-02-13 06:50:49 +01:00
return true;
}
2017-11-01 19:38:56 +01:00
// Search if the tag exists
2016-02-14 01:15:19 +01:00
var findTag = $("span[data-tag]").filter(function() {
2016-02-25 02:50:27 +01:00
return $(this).attr('data-tag').toLowerCase() == newTag.toLowerCase();
2016-02-14 01:15:19 +01:00
});
2017-11-01 19:38:56 +01:00
// If the tag exits select
// If the tag doesn't exist, insert on the list and select
if (findTag.length > 0) {
2016-02-14 01:15:19 +01:00
findTag.removeClass("unselect").addClass("select");
2017-11-01 19:38:56 +01:00
} else {
2016-02-14 01:15:19 +01:00
$("#jstagList").append("<span data-tag=\""+newTag+"\" class=\"select\">"+newTag+"</span>");
}
2016-02-13 06:50:49 +01:00
2017-11-01 19:38:56 +01:00
// Clean the input field
2016-02-13 06:50:49 +01:00
$("#jstagInput").val("");
2017-11-01 19:38:56 +01:00
return newTag;
2016-02-13 06:50:49 +01:00
}
$(document).ready(function() {
2017-11-01 19:38:56 +01:00
// Click on tag unselected
2016-02-13 06:50:49 +01:00
$(document).on("click", ".unselect", function() {
$(this).removeClass("unselect").addClass("select");
});
2017-11-01 19:38:56 +01:00
// Click on tag selected
2016-02-13 06:50:49 +01:00
$(document).on("click", ".select", function() {
$(this).removeClass("select").addClass("unselect");
});
2017-11-01 19:38:56 +01:00
// Insert tag when click on the button "ADD"
2016-02-13 06:50:49 +01:00
$(document).on("click", "#jstagAdd", function(e) {
2017-11-01 19:38:56 +01:00
// Prevent forum submit
2016-02-13 06:50:49 +01:00
e.preventDefault();
insertTag();
});
2017-11-01 19:38:56 +01:00
// Insert tag when press enter key
2016-02-13 06:50:49 +01:00
$("#jstagInput").keypress(function(e) {
2017-11-01 19:38:56 +01:00
if (e.which == 13) {
2016-02-13 06:50:49 +01:00
insertTag();
}
});
2017-11-01 19:38:56 +01:00
// Before form submit
2016-02-13 06:50:49 +01:00
$("form").submit(function(e) {
2017-11-01 19:38:56 +01:00
// For each span.select make an array then implode with comma glue
2016-02-13 06:50:49 +01:00
var list = $("#jstagList > span.select").map(function() {
return $(this).html();
}).get().join(",");
2017-11-01 19:38:56 +01:00
// Insert the tags separated by comma in the input hidden field
2016-02-13 06:50:49 +01:00
$("#jstags").val( list );
2017-11-01 19:38:56 +01:00
return true;
2016-02-13 06:50:49 +01:00
});
2017-11-01 19:38:56 +01:00
});