|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 5 | + <style> |
| 6 | + body { |
| 7 | + font-family: 'Verdana', sans-serif; |
| 8 | + color: #333; |
| 9 | + background-color: #f4f4f4; |
| 10 | + padding: 20px; |
| 11 | + font-size: 16px; |
| 12 | + width: 42em; |
| 13 | + } |
| 14 | + input[type="submit"] { |
| 15 | + font-size: 1.2rem; |
| 16 | + border-radius: 5px; |
| 17 | + border: 1px solid black; |
| 18 | + margin-top: 1ex; |
| 19 | + padding: 10px; |
| 20 | + } |
| 21 | + input[type="text"] { |
| 22 | + width: 100%; |
| 23 | + font-family: monospace; |
| 24 | + padding: 5px; |
| 25 | + } |
| 26 | + input[type="datetime-local"] { |
| 27 | + font-family: monospace; |
| 28 | + padding: 5px; |
| 29 | + } |
| 30 | + form > div { |
| 31 | + margin-bottom: 10px; |
| 32 | + } |
| 33 | + </style> |
| 34 | + <script type="module"> |
| 35 | + import ICAL from "https://unpkg.com/ical.js"; |
| 36 | + |
| 37 | + document.addEventListener('DOMContentLoaded', function() { |
| 38 | + const now = new Date(); |
| 39 | + now.setMinutes(0); |
| 40 | + now.setSeconds(0); |
| 41 | + now.setMilliseconds(0); |
| 42 | + const oneMonthLater = new Date(now.getFullYear(), now.getMonth() + 1, now.getDate()); |
| 43 | + |
| 44 | + // Format dates for input fields |
| 45 | + const dtstartValue = now.toISOString().slice(0, 16); // Formats to YYYY-MM-DDTHH:MM |
| 46 | + const rangeEndValue = oneMonthLater.toISOString().slice(0, 16); // Formats to YYYY-MM-DDTHH:MM |
| 47 | + |
| 48 | + // Set default values |
| 49 | + document.getElementById('dtstart').value = dtstartValue; |
| 50 | + document.getElementById('rangeStart').value = dtstartValue; |
| 51 | + document.getElementById('rangeEnd').value = rangeEndValue; |
| 52 | + }); |
| 53 | + |
| 54 | + document.getElementById("form").addEventListener("submit", (event) => { |
| 55 | + event.preventDefault(); |
| 56 | + |
| 57 | + let dtstart = new Date(document.getElementById('dtstart').value); |
| 58 | + let rruleString = document.getElementById('rrule').value; |
| 59 | + let rangeStart = new Date(document.getElementById('rangeStart').value); |
| 60 | + let rangeEnd = new Date(document.getElementById('rangeEnd').value); |
| 61 | + |
| 62 | + if (isNaN(dtstart.getTime()) || !rruleString || isNaN(rangeStart.getTime()) || isNaN(rangeEnd.getTime())) { |
| 63 | + alert('Please fill all fields correctly.'); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + let icDtStart = ICAL.Time.fromJSDate(dtstart); |
| 68 | + let icRangeEnd = ICAL.Time.fromJSDate(rangeEnd); |
| 69 | + let icRangeStart = ICAL.Time.fromJSDate(rangeStart); |
| 70 | + |
| 71 | + if (rruleString.startsWith("RRULE;")) { |
| 72 | + rruleString = rruleString.substring(6); |
| 73 | + } |
| 74 | + |
| 75 | + let rrule = ICAL.Recur.fromString(`RRULE;${rruleString}`); |
| 76 | + let vevent = new ICAL.Component('vevent'); |
| 77 | + vevent.addPropertyWithValue('dtstart', icDtStart); |
| 78 | + vevent.addPropertyWithValue('rrule', rrule); |
| 79 | + |
| 80 | + let iter = rrule.iterator(icDtStart); |
| 81 | + |
| 82 | + let occurrences = []; |
| 83 | + let next; |
| 84 | + while ((next = iter.next())) { |
| 85 | + if (next.compare(icRangeStart) < 0) { |
| 86 | + continue; |
| 87 | + } else if (next.compare(icRangeEnd) > 0) { |
| 88 | + break; |
| 89 | + } |
| 90 | + |
| 91 | + occurrences.push(next.toString()); |
| 92 | + } |
| 93 | + |
| 94 | + console.log(dtstart, rangeStart); |
| 95 | + |
| 96 | + document.getElementById("testcase").textContent = |
| 97 | + `// <describe testcase here>\n` + |
| 98 | + `testRRULE('${rruleString}', {\n` + |
| 99 | + (dtstart.getTime() == rangeStart.getTime() ? "" |
| 100 | + : ` dtStart: '${icRangeStart.toString()}',\n` |
| 101 | + ) + |
| 102 | + " dates: [\n" + |
| 103 | + ` '${occurrences.join("',\n '")}'\n` + |
| 104 | + " ]\n" + |
| 105 | + "});"; |
| 106 | + |
| 107 | + document.getElementById('occurrences').textContent = occurrences.join('\n'); |
| 108 | + }); |
| 109 | + </script> |
| 110 | + </head> |
| 111 | + <body> |
| 112 | + <form id="form" method="post" action="#"> |
| 113 | + <h1>Recurrence Rule Tester</h1> |
| 114 | + <p id="descr"> |
| 115 | + This tool allows you to calculate occurrences for RRULEs and prepare testcases for them. It |
| 116 | + will use ICAL.js from https://unpkg.com/ical.js. <b>Be sure to manually validate the |
| 117 | + occurrences, as otherwise it wouldn't be a good test</b>. |
| 118 | + </p> |
| 119 | + <div> |
| 120 | + <label for="dtstart">DTSTART:</label> |
| 121 | + <input type="datetime-local" id="dtstart"> |
| 122 | + </div> |
| 123 | + <div> |
| 124 | + <label for="rrule">RRULE:</label> |
| 125 | + <input type="text" id="rrule"> |
| 126 | + </div> |
| 127 | + <div> |
| 128 | + <label for="rangeStart">Expand range:</label> |
| 129 | + <input type="datetime-local" id="rangeStart"> |
| 130 | + <label for="rangeEnd">–</label> |
| 131 | + <input type="datetime-local" id="rangeEnd"> |
| 132 | + </div> |
| 133 | + <input type="submit" id="calculate" value="Calculate Occurrences"/><br/> |
| 134 | + <hr> |
| 135 | + <pre id="occurrences"></pre> |
| 136 | + <hr> |
| 137 | + <pre id="testcase"></pre> |
| 138 | + </form> |
| 139 | + </body> |
| 140 | +</html> |
0 commit comments