|
1 | 1 | (function() {
|
2 | 2 | "use strict";
|
3 | 3 |
|
| 4 | + // jQuery document ready fn |
4 | 5 | $(function() {
|
5 |
| - // Use smooth scrolling when clicking on navigation |
6 |
| - |
7 |
| - var topoffset = 54; //variable for menu height |
8 |
| - |
9 |
| - $('.navbar-nav a:not(#search-link, .dropdown-toggle, .dropdown-item)').click(function() { |
10 |
| - if (location.pathname.replace(/^\//,'') === |
11 |
| - this.pathname.replace(/^\//,'') && |
12 |
| - location.hostname === this.hostname) { |
13 |
| - var target = $(this.hash); |
14 |
| - target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); |
15 |
| - if (target.length) { |
16 |
| - $('html,body').animate({ |
17 |
| - scrollTop: target.offset().top-topoffset |
18 |
| - }, 500); |
19 |
| - return false; |
20 |
| - } //target.length |
21 |
| - } //click function |
22 |
| - }); //smooth scrolling |
23 |
| - |
24 |
| - // Hide and show navigation form |
25 |
| - var $searchLink = $("#search-link"); // variable for search link |
26 |
| - $searchLink.click(toggleNavForm); |
27 |
| - |
28 |
| - function toggleNavForm(e) { |
29 |
| - e.preventDefault(); |
30 |
| - |
31 |
| - // Toggle search link aria-expanded attribute value |
32 |
| - if ($searchLink.attr("aria-expanded") == "false") { |
33 |
| - $searchLink.attr("aria-expanded", "true"); |
34 |
| - } else { |
35 |
| - $searchLink.attr("aria-expanded", "false"); |
| 6 | + // Use smooth scrolling when clicking on navigation |
| 7 | + |
| 8 | + var topoffset = 54; //variable for menu height |
| 9 | + |
| 10 | + $('.navbar-nav a:not(#search-link, .dropdown-toggle, .dropdown-item)').click(function() { |
| 11 | + if (location.pathname.replace(/^\//,'') === |
| 12 | + this.pathname.replace(/^\//,'') && |
| 13 | + location.hostname === this.hostname) { |
| 14 | + var target = $(this.hash); |
| 15 | + target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); |
| 16 | + if (target.length) { |
| 17 | + $('html,body').animate({ |
| 18 | + scrollTop: target.offset().top-topoffset |
| 19 | + }, 500); |
| 20 | + return false; |
| 21 | + } //target.length |
| 22 | + } //click function |
| 23 | + }); //smooth scrolling |
| 24 | + |
| 25 | + // Hide and show navigation form |
| 26 | + var $searchLink = $("#search-link"); // variable for search link |
| 27 | + $searchLink.click(toggleNavForm); |
| 28 | + |
| 29 | + function toggleNavForm(e) { |
| 30 | + // Prevent default link behaviour |
| 31 | + e.preventDefault(); |
| 32 | + |
| 33 | + // Toggle search link aria-expanded attribute value |
| 34 | + if ($searchLink.attr("aria-expanded") == "false") { |
| 35 | + $searchLink.attr("aria-expanded", "true"); |
| 36 | + } else { |
| 37 | + $searchLink.attr("aria-expanded", "false"); |
| 38 | + } |
| 39 | + |
| 40 | + // Toggle site search form |
| 41 | + $("#nav-search").toggleClass("d-none"); |
| 42 | + |
| 43 | + // Toggle site search icon |
| 44 | + var $searchIcon = $("#search-icon"); // variable for search icon |
| 45 | + $searchIcon.toggleClass("fa-search"); |
| 46 | + $searchIcon.toggleClass("fa-times"); |
36 | 47 | }
|
37 | 48 |
|
38 |
| - // Toggle site search form |
39 |
| - $("#nav-search").toggleClass("d-none"); |
| 49 | + // Form validation |
| 50 | + $(".contact-form input, .contact-form textarea").jqBootstrapValidation({ |
| 51 | + // Valid inputs success callback fn |
| 52 | + submitSuccess: function ($form, event) { |
| 53 | + // Prevent default submit behaviour |
| 54 | + event.preventDefault(); |
| 55 | + |
| 56 | + // Get values from form |
| 57 | + var $name = $(".contact-form #v-name").val(); |
| 58 | + var $email = $(".contact-form #v-email").val(); |
| 59 | + var $comment = $(".contact-form #v-comment").val(); |
| 60 | + |
| 61 | + // For success/failure feedback |
| 62 | + var $firstName = $name; |
| 63 | + |
| 64 | + // Check for white space in name for success/fail feedback |
| 65 | + if ($firstName.indexOf(" ") >= 0) { |
| 66 | + $firstName = $name.split(" ").slice(0, -1).join(" "); |
| 67 | + } |
| 68 | + |
| 69 | + // Disable submit button until AJAX call is completed to prevent duplicate messages |
| 70 | + var $submitBtn = $(".btn-dark"); |
| 71 | + $submitBtn.prop("disabled", true).val("Sending..."); |
| 72 | + |
| 73 | + $.ajax({ |
| 74 | + url: "./php/contact.php", |
| 75 | + type: "POST", |
| 76 | + data: { |
| 77 | + name: $name, |
| 78 | + email: $email, |
| 79 | + comment: $comment |
| 80 | + |
| 81 | + }, |
| 82 | + cache: false, |
| 83 | + success: successFn, |
| 84 | + error: errorFn, |
| 85 | + complete: completeFn |
| 86 | + }); |
| 87 | + |
| 88 | + function successFn() { |
| 89 | + // Success feedback |
| 90 | + $("#feedback").html("<div class='alert alert-success'></div>"); |
| 91 | + $("#feedback > .alert-success").html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>"); |
| 92 | + $("#feedback > .alert-success").append($("<strong>").text("Hi " + $firstName + ", Your message has been sent.")) |
| 93 | + .append('</strong>'); |
| 94 | + } |
| 95 | + |
| 96 | + function errorFn() { |
| 97 | + // Fail feedback |
| 98 | + $("#feedback").html("<div class='alert alert-danger'></div>"); |
| 99 | + $("#feedback > .alert-danger").html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>"); |
| 100 | + $("#feedback > .alert-danger").append($("<strong>").text("Sorry " + $firstName + ", it seems that my mail server is not responding. Please try again later!")) |
| 101 | + .append('</strong>'); |
| 102 | + } |
| 103 | + |
| 104 | + function completeFn() { |
| 105 | + // Clear all fields |
| 106 | + $(".contact-form").trigger("reset"); |
| 107 | + |
| 108 | + // Re-enable submit button when AJAX call is completed |
| 109 | + setTimeout(function() { |
| 110 | + $submitBtn.prop("disabled", false).val("Submit Comment"); |
| 111 | + }, 1500); |
| 112 | + |
| 113 | + // Remove feedback, if focus, click events occur on the page after AJAX call is completed |
| 114 | + $("html * ").focus(function() { |
| 115 | + $("#feedback").html(""); |
| 116 | + }); |
| 117 | + |
| 118 | + $("html *").click(function() { |
| 119 | + $("#feedback").html(""); |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + } |
40 | 124 |
|
41 |
| - // Toggle site search icon |
42 |
| - var $searchIcon = $("#search-icon"); // variable for search link |
43 |
| - $searchIcon.toggleClass("fa-search"); |
44 |
| - $searchIcon.toggleClass("fa-times"); |
45 |
| - } |
| 125 | + }); |
46 | 126 |
|
47 |
| - // Tooltip activation |
48 |
| - $('[data-toggle="tooltip"]').tooltip(); |
| 127 | + // Tooltip activation |
| 128 | + $('[data-toggle="tooltip"]').tooltip(); |
49 | 129 |
|
50 |
| -}); |
| 130 | + }); |
51 | 131 | })();
|
0 commit comments