commit 7a34cbd328e8501c1bca5e40804a38c95adc76e2
Author: Jan Pašek <janpasek97@gmail.com>
Date:   Tue Apr 20 15:08:39 2021 +0200

    Re #8583 - create_certificate.js moved initialization code into mounted

diff --git a/static/js/create_certificate.js b/static/js/create_certificate.js
index e91e768..de2462b 100644
--- a/static/js/create_certificate.js
+++ b/static/js/create_certificate.js
@@ -1,159 +1,154 @@
-
-    // VUE instance of certificate creation page
-    var createCertificateApp = new Vue({
-            el: "#create-certificate-content",
-            data: {
-                notBefore: "",
-                notAfter: "",
-                isSelfSigned: false,
-                invalidCN: false,
-                // available certificate authorities
-                authorities: [],
-                // data of the selected certificate authorities to be displayed in the form
-                selectedCAData: {
-                    CN: "",
-                    C:  "",
-                    L:  "",
-                    ST: "",
-                    O:  "",
-                    OU: "",
-                    emailAddress: ""
-                },
-                // Data of the new certificate to be created received from the input fields
-                certificateData: {
-                    subject: {
-                        CN: "",
-                        C: "",
-                        L: "",
-                        ST: "",
-                        O: "",
-                        OU: "",
-                        emailAddress: ""
-                    },
-                    validityDays: 30,
-                    usage: {
-                        CA: false,
-                        authentication: false,
-                        digitalSignature: false,
-                        SSL: false
-                    },
-                    CA: null
-                },
-                errorMessage: ""
-            },
-            // actions to be performed when the page is loaded
-            // - initialize notBefore and notAfter with current date and current date + 1 month respectively
-            mounted () {
-              this.notBefore = new Date().toDateInputValue(); // init notBefore to current date
-              var endDate = new Date(new Date().getTime()+(30*24*60*60*1000));
-              this.notAfter = endDate.toDateInputValue(); // init notAfter to notBefore + 30 days
+// VUE instance of certificate creation page
+var createCertificateApp = new Vue({
+    el: "#create-certificate-content",
+    data: {
+        notBefore: "",
+        notAfter: "",
+        isSelfSigned: false,
+        invalidCN: false,
+        // available certificate authorities
+        authorities: [],
+        // data of the selected certificate authorities to be displayed in the form
+        selectedCAData: {
+            CN: "",
+            C: "",
+            L: "",
+            ST: "",
+            O: "",
+            OU: "",
+            emailAddress: ""
+        },
+        // Data of the new certificate to be created received from the input fields
+        certificateData: {
+            subject: {
+                CN: "",
+                C: "",
+                L: "",
+                ST: "",
+                O: "",
+                OU: "",
+                emailAddress: ""
             },
-            methods: {
-                showError: function (message) {
-                    document.body.scrollTop = 0;
-                    document.documentElement.scrollTop = 0;
-                    this.errorMessage = message;
-                },
-                // handle certificate creation request
-                onCreateCertificate: function () {
-                    // validate input data
-                    // - validate if subject CN is filled in
-                    if(!this.isSelfSigned && this.certificateData.CA == null)
-                    {
-                        this.showError("Issuer must be selected or 'Self-signed' option must be checked!")
-                        return;
-                    }
-                    if(this.certificateData.subject.CN === "") {
-                        this.showError("CN field must be filled in!")
-                        this.invalidCN = true;
-                        return;
-                    }
-                    this.certificateData.validityDays = parseInt(this.certificateData.validityDays);
-                    axios.post(API_URL + "certificates", this.certificateData)
-                        .then(function (response) {
-                            // on success return to index page
-                            if(response.data["success"]) {
-                                window.location.href = "/static/index.html?success=Certificate+successfully+created";
-                            }
-                            // on error display server response message
-                            else {
-                                createCertificateApp.showError(response.data["data"]);
-                            }
-                        })
-                        .catch(function (error) {
-                            console.log(error);
-                        });
-                }
+            validityDays: 30,
+            usage: {
+                CA: false,
+                authentication: false,
+                digitalSignature: false,
+                SSL: false
             },
-            // data watches
-            watch: {
-                authorities: function (val, oldVal) {
-                    this.isSelfSigned = val.length === 0;
-                },
-                isSelfSigned: function (val, oldVal) {
-                    if (val) {
-                        this.certificateData.CA = null;
-                        this.certificateData.usage.CA = true;
-                    } else {
-                        this.certificateData.usage.CA = false;
-                    }
-                },
-                // if the selected CA is changed, the Issuer input fileds must be filled in
-                'certificateData.validityDays': function (val, oldVal) {
-                    var endDate = new Date(new Date().getTime()+(val*24*60*60*1000));
-                    this.notAfter = endDate.toDateInputValue(); // init notAfter to today + validityDays
-                },
-                'certificateData.subject.CN': function (val, oldVal) {
-                    if (val !== '') this.invalidCN = false;
-                },
-                'certificateData.CA': function (val, oldVal) {
-                    // self-signed certificate - all fields are empty
-                    if (val === "null" || val == null) {
-                        createCertificateApp.selectedCAData = {
-                            CN: "",
-                            C:  "",
-                            L:  "",
-                            ST: "",
-                            O:  "",
-                            OU: "",
-                            emailAddress: ""
-                        };
-                    }
-                    // a CA is selected - get CA's details and display them
-                    else {
-                        axios.get(API_URL + "certificates/" + val + "/details")
-                            .then(function (response) {
-                                if (response.data["success"]) {
-                                    createCertificateApp.selectedCAData = response.data["data"]["subject"];
-                                }
-                                else
-                                    console.log("Error occurred while fetching CA details");
-                            })
-                            .catch(function (error) {
-                                console.log(error);
-                            });
-                    }
-                }
-            }
-        });
+            CA: null
+        },
+        errorMessage: ""
+    },
+    // actions to be performed when the page is loaded
+    // - initialize notBefore and notAfter with current date and current date + 1 month respectively
+    mounted() {
+        this.notBefore = new Date().toDateInputValue(); // init notBefore to current date
+        var endDate = new Date(new Date().getTime() + (30 * 24 * 60 * 60 * 1000));
+        this.notAfter = endDate.toDateInputValue(); // init notAfter to notBefore + 30 days
 
-    // Initialize available CA select values
-    axios.get(API_URL+"certificates", {
+        // Initialize available CA select values
+        axios.get(API_URL + "certificates", {
             params: {
                 filtering: {
                     CA: true
                 }
             }
         })
-        .then(function (response) {
-            if (response.data["success"]) {
-                createCertificateApp.authorities = response.data["data"];
+            .then(function (response) {
+                if (response.data["success"]) {
+                    createCertificateApp.authorities = response.data["data"];
+                } else {
+                    createCertificateApp.authorities = []
+                }
+            })
+            .catch(function (error) {
+                console.log(error);
+            });
+    },
+    methods: {
+        showError: function (message) {
+            document.body.scrollTop = 0;
+            document.documentElement.scrollTop = 0;
+            this.errorMessage = message;
+        },
+        // handle certificate creation request
+        onCreateCertificate: function () {
+            // validate input data
+            // - validate if subject CN is filled in
+            if (!this.isSelfSigned && this.certificateData.CA == null) {
+                this.showError("Issuer must be selected or 'Self-signed' option must be checked!")
+                return;
             }
-            else
-            {
-                createCertificateApp.authorities = []
+            if (this.certificateData.subject.CN === "") {
+                this.showError("CN field must be filled in!")
+                this.invalidCN = true;
+                return;
             }
-        })
-        .catch(function (error) {
-            console.log(error);
-        });
\ No newline at end of file
+            this.certificateData.validityDays = parseInt(this.certificateData.validityDays);
+            axios.post(API_URL + "certificates", this.certificateData)
+                .then(function (response) {
+                    // on success return to index page
+                    if (response.data["success"]) {
+                        window.location.href = "/static/index.html?success=Certificate+successfully+created";
+                    }
+                    // on error display server response message
+                    else {
+                        createCertificateApp.showError(response.data["data"]);
+                    }
+                })
+                .catch(function (error) {
+                    console.log(error);
+                });
+        }
+    },
+    // data watches
+    watch: {
+        authorities: function (val, oldVal) {
+            this.isSelfSigned = val.length === 0;
+        },
+        isSelfSigned: function (val, oldVal) {
+            if (val) {
+                this.certificateData.CA = null;
+                this.certificateData.usage.CA = true;
+            } else {
+                this.certificateData.usage.CA = false;
+            }
+        },
+        // if the selected CA is changed, the Issuer input fileds must be filled in
+        'certificateData.validityDays': function (val, oldVal) {
+            var endDate = new Date(new Date().getTime() + (val * 24 * 60 * 60 * 1000));
+            this.notAfter = endDate.toDateInputValue(); // init notAfter to today + validityDays
+        },
+        'certificateData.subject.CN': function (val, oldVal) {
+            if (val !== '') this.invalidCN = false;
+        },
+        'certificateData.CA': function (val, oldVal) {
+            // self-signed certificate - all fields are empty
+            if (val === "null" || val == null) {
+                createCertificateApp.selectedCAData = {
+                    CN: "",
+                    C: "",
+                    L: "",
+                    ST: "",
+                    O: "",
+                    OU: "",
+                    emailAddress: ""
+                };
+            }
+            // a CA is selected - get CA's details and display them
+            else {
+                axios.get(API_URL + "certificates/" + val + "/details")
+                    .then(function (response) {
+                        if (response.data["success"]) {
+                            createCertificateApp.selectedCAData = response.data["data"]["subject"];
+                        } else
+                            console.log("Error occurred while fetching CA details");
+                    })
+                    .catch(function (error) {
+                        console.log(error);
+                    });
+            }
+        }
+    }
+});
\ No newline at end of file
