diff --git a/custom/js/wake_on_lan_hosts.js b/custom/js/wake_on_lan_hosts.js new file mode 100644 index 0000000..2317459 --- /dev/null +++ b/custom/js/wake_on_lan_hosts.js @@ -0,0 +1,186 @@ +const wake_on_lan_new_template = '
\n' + + '\t
\n' + + '\t\t
\n' + + '\t\t\t
\n' + + '\t\t\t\t\n' + + '\t\t\t\t\n' + + '\t\t\t\t\n' + + '\t\t\t
\n' + + '\t\t\t
\n' + + '\t\t\t {{ .Name }}\n' + + '\t\t\t {{ .MacAddress }}\n' + + '\t\t\t Unused\n' + + '\t\t
\n' + + '\t
\n' + + '
'; + +jQuery(function ($) { + $.validator.addMethod('mac', function (value, element) { + return this.optional(element) || /^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$/.test(value); + }, 'Please enter a valid MAC Address. ex: 00:00:00:00:00:00'); +}); + +jQuery.each(["put", "delete"], function (i, method) { + jQuery[method] = function (url, data, callback, type) { + if (jQuery.isFunction(data)) { + type = type || callback; + callback = data; + data = undefined; + } + + return jQuery.ajax({ + url: url, + type: method, + dataType: type, + data: data, + success: callback + }); + }; +}); + +jQuery(function ($) { + let newHostHtml = '
'; + $('h1').parents(".row").append(newHostHtml); +}); + +jQuery(function ($) { + $('.btn-outline-success').click(function () { + $.put('/wake_on_lan_host/' + $(this).data('mac-address')); + }); +}); + +jQuery(function ($) { + let $modal_remove_wake_on_lan_host = $('#modal_remove_wake_on_lan_host'); + let $remove_client_confirm = $('#remove_wake_on_host_confirm'); + + $modal_remove_wake_on_lan_host.on('show.bs.modal', function (event) { + const $btn = $(event.relatedTarget); + const $modal = $(this); + + const $editBtn = $btn.parents('.btn-group').find('.btn_modify_wake_on_lan_host'); + $modal.find('.modal-body').text("You are about to remove Wake On Lan Host " + $editBtn.data('name')); + $remove_client_confirm.val($editBtn.data('mac-address')); + }) + + $remove_client_confirm.click(function () { + const macAddress = $remove_client_confirm.val().replaceAll(":", "-"); + $.delete('/wake_on_lan_host/' + macAddress); + $('#' + macAddress).remove(); + + $modal_remove_wake_on_lan_host.modal('hide'); + }); +}); + +jQuery(function ($) { + let $modal_wake_on_lan_host = $("#modal_wake_on_lan_host"); + let $name = $('#frm_wake_on_lan_host_name'); + let $macAddress = $('#frm_wake_on_lan_host_mac_address'); + let $oldMacAddress = $('#frm_wake_on_lan_host_old_mac_address'); + let $contentRow = $('.content .row'); + let $frm_wake_on_lan_host = $("#frm_wake_on_lan_host"); + + // https://jqueryvalidation.org/ + let validator = $frm_wake_on_lan_host.validate({ + submitHandler: function () { + let data = { + name: $name.val(), + mac_address: $macAddress.val().toUpperCase(), + old_mac_address: $oldMacAddress.val().toUpperCase() + }; + $.ajax({ + cache: false, + method: 'POST', + url: '/wake_on_lan_host', + dataType: 'json', + contentType: "application/json", + data: JSON.stringify(data), + success: function (response) { + /** @type {string} */ + let oldMacAddress = $oldMacAddress.val().toUpperCase(); + + if (oldMacAddress != '') { + let macAddress = response.MacAddress; + let name = response.Name; + + let $container = $('#' + oldMacAddress.replaceAll(":", "-")); + if (macAddress != oldMacAddress) { + $container.attr('id', macAddress.replaceAll(":", "-")); + $container.find('.mac-address').text(macAddress); + $container.find('[data-mac-address]').data('mac-address', macAddress); + } + + $container.find('.name').text(name); + $container.find('[data-name]').data('name', name); + } else { + const $template = $( + wake_on_lan_new_template + .replace(/{{ .Id }}/g, response.MacAddress.replaceAll(":", "-").toUpperCase()) + .replace(/{{ .MacAddress }}/g, response.MacAddress.toUpperCase()) + .replace(/{{ .Name }}/g, response.Name) + ); + + $contentRow.append($template); + } + $modal_wake_on_lan_host.modal('hide'); + toastr.success('Wake on Lan Host Save successfully'); + }, + error: function (jqXHR, exception) { + const responseJson = jQuery.parseJSON(jqXHR.responseText); + toastr.error(responseJson['message']); + + if (typeof (console) != 'undefined') + console.log(exception); + } + }); + + return false; + }, + rules: { + name: { + required: true, + }, + mac_address: { + required: true, + mac: true, + } + }, + messages: { + name: { + required: "Please enter a name" + }, + mac_address: { + required: "Please enter a Mac Address" + } + }, + errorElement: 'span', + errorPlacement: function (error, element) { + error.addClass('invalid-feedback'); + element.closest('.form-group').append(error); + }, + highlight: function (element) { + $(element).addClass('is-invalid'); + }, + unhighlight: function (element) { + $(element).removeClass('is-invalid'); + } + }); + + $modal_wake_on_lan_host.on('show.bs.modal', function (e) { + const $btn = $(e.relatedTarget); + validator.resetForm(); + $macAddress.removeClass('is-invalid'); + + $name.val($btn.data('name')); + $macAddress.val($btn.data('mac-address')); + $oldMacAddress.val($btn.data('mac-address')); + }); +});