[Patchew-devel] [PATCH 04/17] mods: move epilog to a .js file

Paolo Bonzini posted 17 patches 5 years, 7 months ago
[Patchew-devel] [PATCH 04/17] mods: move epilog to a .js file
Posted by Paolo Bonzini 5 years, 7 months ago
Load the config editor Javascript via <script> from the template,
instead of including it once per plugin.
---
 mod.py                            | 132 +-----------------------------
 static/js/config-editor.js        | 127 ++++++++++++++++++++++++++++
 www/templates/project-detail.html |   1 +
 3 files changed, 129 insertions(+), 131 deletions(-)
 create mode 100644 static/js/config-editor.js

diff --git a/mod.py b/mod.py
index 696dda1..7184211 100644
--- a/mod.py
+++ b/mod.py
@@ -148,9 +148,7 @@ class PatchewModule(object):
         assert not isinstance(self.project_property_schema, StringSchema)
         assert not isinstance(self.project_property_schema, IntegerSchema)
         scm = self.project_property_schema
-        tmpl = self._build_one(request, project, scm.name + ".", scm)
-        tmpl += self._render_template(request, project, TMPL_END)
-        return tmpl
+        return self._build_one(request, project, scm.name + ".", scm)
 
 _loaded_modules = {}
 
@@ -328,131 +326,3 @@ TMPL_MAP = """
     </div>
 </div>
 """
-
-TMPL_END = """
-<script type="text/javascript">
-function save_done(btn, succeeded, error) {
-    $(btn).text("Save");
-    $(btn).removeClass("disabled");
-    info = $("<div class=\\"alert save-message\\"></div>");
-    if (succeeded) {
-        info.addClass("alert-success");
-        info.html("Saved");
-    } else {
-        info.addClass("alert-danger");
-        info.html("Error: " + error);
-    }
-    info.insertBefore($(btn));
-}
-
-function collect_properties(btn, check_required) {
-    prefix = $(btn).parent().parent().find("#property-prefix").val();
-    properties = {};
-    $(btn).parent().parent().find(".project-property").each(function () {
-        if (check_required && this.required && !this.value) {
-            alert($(this).parent().find("label").html() + " is required!");
-            $(this).focus();
-            properties = false;
-            return false;
-        }
-        if (this.type == "number") {
-            val = parseInt(this.value);
-            if (isNaN(val)) {
-                alert("Invalid number for " + this.name);
-                $(this).focus();
-                properties = false;
-                return false;
-            }
-        } else if (this.type == "checkbox") {
-            if (this.checked) {
-                val = true;
-            } else {
-                val = false;
-            }
-        } else {
-            val = this.value;
-        }
-        properties[prefix + this.name] = val;
-    });
-    return properties;
-}
-
-function properties_save(btn) {
-    if ($(btn).hasClass("disabled")) {
-        return;
-    }
-    props = collect_properties(btn, true);
-    if (!props) {
-        return;
-    }
-    $(btn).addClass("disabled");
-    $(btn).text("Saving...");
-    $(btn).parent().find(".save-message").remove();
-    patchew_api_do("set-project-properties",
-                   { project: "{{ project.name }}",
-                     properties: props })
-        .done(function (data) {
-            save_done(btn, true);
-        })
-        .fail(function (data, text, error) {
-            save_done(btn, false, error);
-        });
-}
-
-function collect_items(btn) {
-    $(btn).parent().parent().find(".map-item");
-    return {};
-}
-
-function map_add_item(btn) {
-    name = window.prompt("Please input a name");
-    if (!name || name == 'null') {
-        return;
-    }
-    if (name in collect_items(btn)) {
-        alert(test_name + " already exists.");
-        return;
-    }
-    if (name.indexOf(".") >= 0) {
-        alert("Invalid name, no dot is allowed.");
-        return;
-    }
-    container = $(btn).parent().parent();
-    tmpl = container.find("#item-template").html();
-    nt = $(tmpl)
-    nt.find("#item-name").html(name);
-    old = nt.find("#property-prefix").val();
-    nt.find("#property-prefix").val(old + name + ".");
-    container.find(".items").append(nt);
-}
-function map_delete_item(btn) {
-    name = $(btn).parent().parent().parent().find("#item-name").html();
-    prefix = $(btn).parent().parent().parent().find("#prefix").val();
-    if (!window.confirm("Really delete '" + name +"'?")) {
-        return;
-    }
-    $(btn).addClass("disabled");
-    $(btn).text("Deleting...");
-    $(btn).parent().find(".delete-message").remove();
-    patchew_api_do("delete-project-properties-by-prefix",
-                   { project: "{{ project.name }}",
-                     prefix: prefix })
-        .done(function (data) {
-            container = $(btn).parent().parent().parent();
-            container.remove();
-        })
-        .fail(function (data, text, error) {
-            $(btn).removeClass("disabled");
-            $(btn).text("Delete");
-            info = $("<div class=\\"alert alert-danger delete-message\\"></div>");
-            info.html("Error: " + error);
-            info.insertBefore($(btn));
-        });
-}
-function enum_change(which) {
-    val = $(which).val();
-    desc = $(which).parent().find("#enum-desc-" + val).html();
-    $(which).parent().find("#enum-desc").html(desc);
-}
-</script>
-"""
diff --git a/static/js/config-editor.js b/static/js/config-editor.js
new file mode 100644
index 0000000..f8c9454
--- /dev/null
+++ b/static/js/config-editor.js
@@ -0,0 +1,127 @@
+function current_project() {
+    return $('h2').text();
+}
+
+function save_done(btn, succeeded, error) {
+    $(btn).text("Save");
+    $(btn).removeClass("disabled");
+    info = $("<div class=\"alert save-message\"></div>");
+    if (succeeded) {
+        info.addClass("alert-success");
+        info.html("Saved");
+    } else {
+        info.addClass("alert-danger");
+        info.html("Error: " + error);
+    }
+    info.insertBefore($(btn));
+}
+
+function collect_properties(btn, check_required) {
+    prefix = $(btn).parent().parent().find("#property-prefix").val();
+    properties = {};
+    $(btn).parent().parent().find(".project-property").each(function () {
+        if (check_required && this.required && !this.value) {
+            alert($(this).parent().find("label").html() + " is required!");
+            $(this).focus();
+            properties = false;
+            return false;
+        }
+        if (this.type == "number") {
+            val = parseInt(this.value);
+            if (isNaN(val)) {
+                alert("Invalid number for " + this.name);
+                $(this).focus();
+                properties = false;
+                return false;
+            }
+        } else if (this.type == "checkbox") {
+            if (this.checked) {
+                val = true;
+            } else {
+                val = false;
+            }
+        } else {
+            val = this.value;
+        }
+        properties[prefix + this.name] = val;
+    });
+    return properties;
+}
+
+function properties_save(btn) {
+    if ($(btn).hasClass("disabled")) {
+        return;
+    }
+    props = collect_properties(btn, true);
+    if (!props) {
+        return;
+    }
+    $(btn).addClass("disabled");
+    $(btn).text("Saving...");
+    $(btn).parent().find(".save-message").remove();
+    patchew_api_do("set-project-properties",
+                   { project: current_project(),
+                     properties: props })
+        .done(function (data) {
+            save_done(btn, true);
+        })
+        .fail(function (data, text, error) {
+            save_done(btn, false, error);
+        });
+}
+
+function collect_items(btn) {
+    $(btn).parent().parent().find(".map-item");
+    return {};
+}
+
+function map_add_item(btn) {
+    name = window.prompt("Please input a name");
+    if (!name || name == 'null') {
+        return;
+    }
+    if (name in collect_items(btn)) {
+        alert(test_name + " already exists.");
+        return;
+    }
+    if (name.indexOf(".") >= 0) {
+        alert("Invalid name, no dot is allowed.");
+        return;
+    }
+    container = $(btn).parent().parent();
+    tmpl = container.find("#item-template").html();
+    nt = $(tmpl)
+    nt.find("#item-name").html(name);
+    old = nt.find("#property-prefix").val();
+    nt.find("#property-prefix").val(old + name + ".");
+    container.find(".items").append(nt);
+}
+function map_delete_item(btn) {
+    name = $(btn).parent().parent().parent().find("#item-name").html();
+    prefix = $(btn).parent().parent().parent().find("#prefix").val();
+    if (!window.confirm("Really delete '" + name +"'?")) {
+        return;
+    }
+    $(btn).addClass("disabled");
+    $(btn).text("Deleting...");
+    $(btn).parent().find(".delete-message").remove();
+    patchew_api_do("delete-project-properties-by-prefix",
+                   { project: current_project(),
+                     prefix: prefix })
+        .done(function (data) {
+            container = $(btn).parent().parent().parent();
+            container.remove();
+        })
+        .fail(function (data, text, error) {
+            $(btn).removeClass("disabled");
+            $(btn).text("Delete");
+            info = $("<div class=\"alert alert-danger delete-message\"></div>");
+            info.html("Error: " + error);
+            info.insertBefore($(btn));
+        });
+}
+function enum_change(which) {
+    val = $(which).val();
+    desc = $(which).parent().find("#enum-desc-" + val).html();
+    $(which).parent().find("#enum-desc").html(desc);
+}
diff --git a/www/templates/project-detail.html b/www/templates/project-detail.html
index 35ee845..adf90a3 100644
--- a/www/templates/project-detail.html
+++ b/www/templates/project-detail.html
@@ -4,6 +4,7 @@
 <link rel="stylesheet" href="/static/css/project-detail.css">
 <link rel="stylesheet" href="/static/css/colorbox.css">
 <script src="/static/js/jquery.colorbox-min.js"></script>
+<script src="/static/js/config-editor.js"></script>
 {% endblock %}
 
 {% block content %}
-- 
2.21.0


_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel