diff options
author | rubenwardy <rw@rubenwardy.com> | 2020-07-10 21:27:41 +0100 |
---|---|---|
committer | rubenwardy <rw@rubenwardy.com> | 2020-07-10 21:27:41 +0100 |
commit | ba3b1082394d12b206d10b3b183f5f41a0e2c4de (patch) | |
tree | 71b98dde143cb8d53f62773385b467d0bd2f4faf | |
parent | 42b08f9bcde76bb592f9181bf2bfa51e79a42697 (diff) | |
download | cheatdb-ba3b1082394d12b206d10b3b183f5f41a0e2c4de.tar.xz |
Fix tag selector losing all tags on remove
Fixes #148
-rw-r--r-- | app/public/static/tagselector.js | 32 | ||||
-rw-r--r-- | app/templates/macros/forms.html | 2 |
2 files changed, 14 insertions, 20 deletions
diff --git a/app/public/static/tagselector.js b/app/public/static/tagselector.js index 5b9af55..9bbb764 100644 --- a/app/public/static/tagselector.js +++ b/app/public/static/tagselector.js @@ -17,7 +17,7 @@ err.show(); } - $.fn.selectSelector = function(source, name, select) { + $.fn.selectSelector = function(source, select) { return this.each(function() { var selector = $(this), input = $('input[type=text]', this); @@ -25,33 +25,28 @@ selector.click(function() { input.focus(); }) .delegate('.badge a', 'click', function() { var id = $(this).parent().data("id"); - for (var i = 0; i < source.length; i++) { - if (source[i].id == id) { - source[i].selected = null; - } - } - select.find("option[value=" + id + "]").attr("selected", null) + select.find("option[value=" + id + "]").attr("selected", false) recreate(); }); - function addTag(item) { - var tag = $('<span class="badge badge-pill badge-primary"/>') - .text(item.toString() + ' ') - .data("id", item.id) + function addTag(id, text) { + $('<span class="badge badge-pill badge-primary"/>') + .text(text + ' ') + .data("id", id) .append('<a>x</a>') .insertBefore(input); input.attr("placeholder", null); - select.find("option[value=" + item.id + "]").attr("selected", "selected") + select.find("option[value='" + id + "']").attr("selected", "selected") hide_error(input); } function recreate() { selector.find("span").remove(); - for (var i = 0; i < source.length; i++) { - if (source[i].selected) { - addTag(source[i]); + select.find("option").each(function() { + if (this.hasAttribute("selected")) { + addTag(this.getAttribute("value"), this.innerText); } - } + }); } recreate(); @@ -70,7 +65,7 @@ minLength: 0, source: source, select: function(event, ui) { - addTag(ui.item); + addTag(ui.item.id, ui.item.toString()); input.val(""); return false; } @@ -253,8 +248,7 @@ }); }); - console.log(options); - ele.selectSelector(options, sel.attr("name"), sel); + ele.selectSelector(options, sel); }); $(".metapackage_selector").each(function() { diff --git a/app/templates/macros/forms.html b/app/templates/macros/forms.html index 89525f9..d28199e 100644 --- a/app/templates/macros/forms.html +++ b/app/templates/macros/forms.html @@ -16,7 +16,7 @@ {% macro form_scripts() -%} <link href="/static/jquery-ui.min.css" rel="stylesheet" type="text/css"> <script src="/static/jquery-ui.min.js"></script> - <script src="/static/tagselector.js"></script> + <script src="/static/tagselector.js?v=2"></script> {% endmacro %} {% macro package_lists() -%} |