]> git.lizzy.rs Git - cheatdb.git/commitdiff
Fix tag selector losing all tags on remove
authorrubenwardy <rw@rubenwardy.com>
Fri, 10 Jul 2020 20:27:41 +0000 (21:27 +0100)
committerrubenwardy <rw@rubenwardy.com>
Fri, 10 Jul 2020 20:27:41 +0000 (21:27 +0100)
Fixes #148

app/public/static/tagselector.js
app/templates/macros/forms.html

index 5b9af55598c1795074874e64da4a603bdcebbbd4..9bbb764d77fe303ba6b57b5f0cb10c63f2d207a4 100644 (file)
@@ -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);
                                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;
                                                }
                                });
                        });
 
-                       console.log(options);
-                       ele.selectSelector(options, sel.attr("name"), sel);
+                       ele.selectSelector(options, sel);
                });
 
                $(".metapackage_selector").each(function() {
index 89525f9575bd7101ff3dd8edb88b5a54f92024f7..d28199ecf0d46c0f8448a5bf3b6edb74e8ec1467 100644 (file)
@@ -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() -%}