var base_url = jQuery(".brand-link").attr('href'); if (base_url.substring(base_url.length - 1, base_url.length) != "/") base_url = base_url + "/"; 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.(uppercase letters and numbers, : only) ex: 00:AB:12:EF:DD:AA'); }); 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, contentType: 'application/json' }); }; }); jQuery(function ($) { let newHostHtml = '
'; $('h1').parents(".row").append(newHostHtml); }); jQuery(function ($) { $('.btn-outline-success').click(function () { const $this = $(this); $.put(base_url + 'wake_on_lan_host/' + $this.data('mac-address'), function (result) { $this.parents('.info-box').find('.latest-used').text(prettyDateTime(result)); }); }); }); 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(base_url + 'wake_on_lan_host/' + macAddress); $('#' + macAddress).remove(); $modal_remove_wake_on_lan_host.modal('hide'); }); }); jQuery(function ($) { $('.latest-used').each(function () { const $this = $(this); const timeText = $this.text().trim(); try { if (timeText != "Unused") { $this.text(prettyDateTime(timeText)); } } catch (ex) { console.log(timeText); throw ex; } }); }); 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: base_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')); }); });