|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Jenkins Contributors |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +Behaviour.specify(".snooze-button", "snooze-button", 0, function (button) { |
| 26 | + button.onclick = function () { |
| 27 | + openSnoozeDialog(button); |
| 28 | + }; |
| 29 | +}); |
| 30 | + |
| 31 | +function addSnoozeFormHandling(form) { |
| 32 | + var durationSelect = form.querySelector('select[name="durationPreset"]'); |
| 33 | + var customDateInput = form.querySelector('input[name="snoozeUntil"]'); |
| 34 | + |
| 35 | + durationSelect.addEventListener("change", function () { |
| 36 | + if (durationSelect.value === "custom") { |
| 37 | + customDateInput.classList.remove("jenkins-hidden"); |
| 38 | + var submitButton = form.querySelector('button[data-id="ok"]'); |
| 39 | + if (customDateInput.value !== "") { |
| 40 | + submitButton.disabled = false; |
| 41 | + } else { |
| 42 | + submitButton.disabled = true; |
| 43 | + } |
| 44 | + } else { |
| 45 | + var submitButton = form.querySelector('button[data-id="ok"]'); |
| 46 | + submitButton.disabled = false; |
| 47 | + customDateInput.classList.add("jenkins-hidden"); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + customDateInput.addEventListener("change", function () { |
| 52 | + var submitButton = form.querySelector('button[data-id="ok"]'); |
| 53 | + if (customDateInput.value !== "") { |
| 54 | + submitButton.disabled = false; |
| 55 | + } else { |
| 56 | + submitButton.disabled = true; |
| 57 | + } |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +function openSnoozeDialog(button) { |
| 62 | + var snoozeUrl = button.dataset.snoozeUrl; |
| 63 | + var templateId = button.dataset.templateId; |
| 64 | + var dialogTitle = button.dataset.title; |
| 65 | + var snoozeText = button.dataset.buttonSnooze; |
| 66 | + var cancelText = button.dataset.buttonCancel; |
| 67 | + |
| 68 | + var formTemplate = document |
| 69 | + .getElementById(templateId) |
| 70 | + .firstElementChild.cloneNode(true); |
| 71 | + var form = document.createElement("form"); |
| 72 | + |
| 73 | + // Set date constraints |
| 74 | + var dateInput = formTemplate.querySelector('input[name="snoozeUntil"]'); |
| 75 | + var now = new Date(); |
| 76 | + var tomorrow = new Date(); |
| 77 | + var nextYear = new Date(); |
| 78 | + var presetDate = new Date(); |
| 79 | + tomorrow.setDate(now.getDate() + 1); |
| 80 | + presetDate.setDate(now.getDate() + 1); |
| 81 | + nextYear.setDate(now.getDate() + 365); |
| 82 | + dateInput.min = tomorrow.toISOString().split("T")[0]; |
| 83 | + dateInput.max = nextYear.toISOString().split("T")[0]; |
| 84 | + dateInput.value = presetDate.toISOString().split("T")[0]; |
| 85 | + |
| 86 | + form.appendChild(formTemplate); |
| 87 | + addSnoozeFormHandling(form); |
| 88 | + |
| 89 | + dialog |
| 90 | + .form(form, { |
| 91 | + title: dialogTitle, |
| 92 | + okText: snoozeText, |
| 93 | + cancelText: cancelText, |
| 94 | + submitButton: false, |
| 95 | + maxWidth: "450px", |
| 96 | + minWidth: "350px", |
| 97 | + }) |
| 98 | + .then( |
| 99 | + function (formData) { |
| 100 | + // Get crumb directly from document head |
| 101 | + var crumbHeaderName = document.head.getAttribute("data-crumb-header"); |
| 102 | + var crumbValue = document.head.getAttribute("data-crumb-value"); |
| 103 | + |
| 104 | + var params = { |
| 105 | + durationPreset: formData.get("durationPreset"), |
| 106 | + snoozeUntil: formData.get("snoozeUntil"), |
| 107 | + }; |
| 108 | + // Add crumb to body |
| 109 | + if (crumbHeaderName && crumbValue) { |
| 110 | + params[crumbHeaderName] = crumbValue; |
| 111 | + } |
| 112 | + |
| 113 | + var headers = { |
| 114 | + "Content-Type": "application/x-www-form-urlencoded", |
| 115 | + }; |
| 116 | + // Add crumb to headers |
| 117 | + if (crumbHeaderName && crumbValue) { |
| 118 | + headers[crumbHeaderName] = crumbValue; |
| 119 | + } |
| 120 | + |
| 121 | + fetch(snoozeUrl, { |
| 122 | + body: new URLSearchParams(params), |
| 123 | + method: "post", |
| 124 | + headers: headers, |
| 125 | + }).then(function (rsp) { |
| 126 | + if (rsp.ok) { |
| 127 | + // Find and hide the monitor's parent element |
| 128 | + var monitorElement = button.closest(".jenkins-alert"); |
| 129 | + if (monitorElement) { |
| 130 | + monitorElement.style.display = "none"; |
| 131 | + } else { |
| 132 | + // Fallback: reload the page |
| 133 | + window.location.reload(); |
| 134 | + } |
| 135 | + } else { |
| 136 | + rsp.text().then(function (msg) { |
| 137 | + dialog.alert("Error", { |
| 138 | + message: msg || "Failed to snooze monitor", |
| 139 | + type: "destructive", |
| 140 | + }); |
| 141 | + }); |
| 142 | + } |
| 143 | + }); |
| 144 | + }, |
| 145 | + function () { |
| 146 | + // Dialog cancelled - do nothing |
| 147 | + }, |
| 148 | + ); |
| 149 | +} |
0 commit comments