@clagentic/console 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (221) hide show
  1. package/CREDITS.md +3 -0
  2. package/LICENSE +21 -0
  3. package/README.md +250 -0
  4. package/bin/cli.js +2812 -0
  5. package/lib/agents-favorites.js +164 -0
  6. package/lib/agents.js +282 -0
  7. package/lib/ask-user-mcp-server.js +120 -0
  8. package/lib/browser-mcp-server.js +484 -0
  9. package/lib/build-user-env.js +63 -0
  10. package/lib/certs/fullchain.pem +47 -0
  11. package/lib/certs/privkey.pem +5 -0
  12. package/lib/cli-sessions.js +265 -0
  13. package/lib/codex-defaults.js +18 -0
  14. package/lib/config.js +332 -0
  15. package/lib/crisis-safety.js +60 -0
  16. package/lib/daemon-projects.js +164 -0
  17. package/lib/daemon.js +1602 -0
  18. package/lib/dm.js +128 -0
  19. package/lib/ipc.js +125 -0
  20. package/lib/ipv4-only.js +39 -0
  21. package/lib/mcp-local.js +385 -0
  22. package/lib/notes.js +141 -0
  23. package/lib/os-users.js +529 -0
  24. package/lib/pages.js +1325 -0
  25. package/lib/project-connection.js +302 -0
  26. package/lib/project-external-trigger.js +236 -0
  27. package/lib/project-file-watch.js +120 -0
  28. package/lib/project-filesystem.js +528 -0
  29. package/lib/project-http.js +753 -0
  30. package/lib/project-image.js +95 -0
  31. package/lib/project-knowledge.js +88 -0
  32. package/lib/project-loop.js +1304 -0
  33. package/lib/project-mcp.js +374 -0
  34. package/lib/project-memory.js +39 -0
  35. package/lib/project-notifications.js +262 -0
  36. package/lib/project-sessions.js +1581 -0
  37. package/lib/project-user-mention.js +193 -0
  38. package/lib/project-user-message.js +628 -0
  39. package/lib/project.js +1303 -0
  40. package/lib/public/app.js +1031 -0
  41. package/lib/public/apple-touch-icon.png +0 -0
  42. package/lib/public/claude-code-avatar.png +0 -0
  43. package/lib/public/codex-avatar.png +0 -0
  44. package/lib/public/console-icon.png +0 -0
  45. package/lib/public/css/admin.css +736 -0
  46. package/lib/public/css/agent-picker.css +119 -0
  47. package/lib/public/css/base.css +235 -0
  48. package/lib/public/css/channel-layout.css +441 -0
  49. package/lib/public/css/command-palette.css +559 -0
  50. package/lib/public/css/debate.css +1609 -0
  51. package/lib/public/css/diff.css +139 -0
  52. package/lib/public/css/filebrowser.css +1976 -0
  53. package/lib/public/css/highlight.css +144 -0
  54. package/lib/public/css/home-hub.css +545 -0
  55. package/lib/public/css/icon-strip.css +1856 -0
  56. package/lib/public/css/input.css +1349 -0
  57. package/lib/public/css/loop.css +1248 -0
  58. package/lib/public/css/mention.css +317 -0
  59. package/lib/public/css/menus.css +857 -0
  60. package/lib/public/css/messages.css +1833 -0
  61. package/lib/public/css/mobile-nav.css +1332 -0
  62. package/lib/public/css/notifications-center.css +296 -0
  63. package/lib/public/css/overlays.css +1273 -0
  64. package/lib/public/css/playbook.css +264 -0
  65. package/lib/public/css/profile.css +397 -0
  66. package/lib/public/css/rewind.css +671 -0
  67. package/lib/public/css/scheduler-modal.css +1495 -0
  68. package/lib/public/css/scheduler.css +1420 -0
  69. package/lib/public/css/server-settings.css +909 -0
  70. package/lib/public/css/session-search.css +159 -0
  71. package/lib/public/css/sidebar.css +2012 -0
  72. package/lib/public/css/skills.css +789 -0
  73. package/lib/public/css/sticky-notes.css +854 -0
  74. package/lib/public/css/stt.css +155 -0
  75. package/lib/public/css/title-bar.css +674 -0
  76. package/lib/public/css/tooltip.css +67 -0
  77. package/lib/public/css/user-settings.css +702 -0
  78. package/lib/public/emoji-data.json +1 -0
  79. package/lib/public/favicon-banded-32.png +0 -0
  80. package/lib/public/favicon-banded.png +0 -0
  81. package/lib/public/favicon-dark.svg +32 -0
  82. package/lib/public/favicon.svg +32 -0
  83. package/lib/public/icon-192.png +0 -0
  84. package/lib/public/icon-512.png +0 -0
  85. package/lib/public/icon-banded-76.png +0 -0
  86. package/lib/public/icon-banded-96.png +0 -0
  87. package/lib/public/icon-mono.svg +25 -0
  88. package/lib/public/icons/email/gmail.svg +7 -0
  89. package/lib/public/icons/email/outlook.svg +35 -0
  90. package/lib/public/icons/email/yahoo.svg +1 -0
  91. package/lib/public/index.html +1946 -0
  92. package/lib/public/manifest.json +27 -0
  93. package/lib/public/markdown-worker.js +43 -0
  94. package/lib/public/marked.min.js +6 -0
  95. package/lib/public/modules/admin.js +954 -0
  96. package/lib/public/modules/agent-picker.js +429 -0
  97. package/lib/public/modules/app-connection.js +216 -0
  98. package/lib/public/modules/app-cursors.js +492 -0
  99. package/lib/public/modules/app-dm.js +265 -0
  100. package/lib/public/modules/app-favicon.js +231 -0
  101. package/lib/public/modules/app-header.js +242 -0
  102. package/lib/public/modules/app-home-hub.js +432 -0
  103. package/lib/public/modules/app-loop-ui.js +691 -0
  104. package/lib/public/modules/app-loop-wizard.js +439 -0
  105. package/lib/public/modules/app-messages.js +1415 -0
  106. package/lib/public/modules/app-misc.js +463 -0
  107. package/lib/public/modules/app-notifications.js +651 -0
  108. package/lib/public/modules/app-panels.js +1061 -0
  109. package/lib/public/modules/app-projects.js +875 -0
  110. package/lib/public/modules/app-rate-limit.js +529 -0
  111. package/lib/public/modules/app-rendering.js +739 -0
  112. package/lib/public/modules/app-skills-install.js +285 -0
  113. package/lib/public/modules/avatar.js +36 -0
  114. package/lib/public/modules/command-palette.js +491 -0
  115. package/lib/public/modules/console-logo.js +162 -0
  116. package/lib/public/modules/context-sources.js +365 -0
  117. package/lib/public/modules/diff.js +412 -0
  118. package/lib/public/modules/dom-refs.js +21 -0
  119. package/lib/public/modules/emoji-picker.js +222 -0
  120. package/lib/public/modules/events.js +21 -0
  121. package/lib/public/modules/filebrowser.js +1966 -0
  122. package/lib/public/modules/fileicons.js +172 -0
  123. package/lib/public/modules/icons.js +80 -0
  124. package/lib/public/modules/input.js +1102 -0
  125. package/lib/public/modules/longpress.js +74 -0
  126. package/lib/public/modules/markdown.js +486 -0
  127. package/lib/public/modules/mcp-ui.js +295 -0
  128. package/lib/public/modules/mention.js +1093 -0
  129. package/lib/public/modules/notifications.js +613 -0
  130. package/lib/public/modules/playbook.js +578 -0
  131. package/lib/public/modules/profile.js +594 -0
  132. package/lib/public/modules/project-settings.js +849 -0
  133. package/lib/public/modules/project-switcher.js +375 -0
  134. package/lib/public/modules/qrcode.js +87 -0
  135. package/lib/public/modules/rewind.js +381 -0
  136. package/lib/public/modules/scheduler-config.js +1611 -0
  137. package/lib/public/modules/scheduler-history.js +131 -0
  138. package/lib/public/modules/scheduler.js +1689 -0
  139. package/lib/public/modules/server-settings.js +842 -0
  140. package/lib/public/modules/session-search.js +456 -0
  141. package/lib/public/modules/settings-defaults.js +253 -0
  142. package/lib/public/modules/sidebar-mobile.js +1425 -0
  143. package/lib/public/modules/sidebar-projects.js +2022 -0
  144. package/lib/public/modules/sidebar-sessions.js +1548 -0
  145. package/lib/public/modules/sidebar-users.js +449 -0
  146. package/lib/public/modules/sidebar.js +348 -0
  147. package/lib/public/modules/skills.js +793 -0
  148. package/lib/public/modules/state.js +3 -0
  149. package/lib/public/modules/sticky-notes.js +1221 -0
  150. package/lib/public/modules/store.js +34 -0
  151. package/lib/public/modules/stt.js +266 -0
  152. package/lib/public/modules/terminal.js +987 -0
  153. package/lib/public/modules/theme.js +812 -0
  154. package/lib/public/modules/tool-palette.js +562 -0
  155. package/lib/public/modules/tools.js +2285 -0
  156. package/lib/public/modules/tooltip.js +150 -0
  157. package/lib/public/modules/user-settings.js +544 -0
  158. package/lib/public/modules/utils.js +56 -0
  159. package/lib/public/modules/ws-ref.js +26 -0
  160. package/lib/public/style.css +34 -0
  161. package/lib/public/sw.js +159 -0
  162. package/lib/push.js +149 -0
  163. package/lib/scheduler.js +471 -0
  164. package/lib/sdk-bridge.js +1792 -0
  165. package/lib/sdk-message-processor.js +740 -0
  166. package/lib/sdk-message-queue.js +42 -0
  167. package/lib/sdk-skill-discovery.js +131 -0
  168. package/lib/sdk-worker.js +677 -0
  169. package/lib/server-admin.js +712 -0
  170. package/lib/server-auth.js +737 -0
  171. package/lib/server-dm.js +170 -0
  172. package/lib/server-palette.js +104 -0
  173. package/lib/server-settings.js +553 -0
  174. package/lib/server-skills.js +280 -0
  175. package/lib/server.js +1390 -0
  176. package/lib/session-search.js +693 -0
  177. package/lib/sessions.js +946 -0
  178. package/lib/smtp.js +221 -0
  179. package/lib/terminal-manager.js +219 -0
  180. package/lib/terminal.js +39 -0
  181. package/lib/themes/ayu-light.json +9 -0
  182. package/lib/themes/catppuccin-latte.json +9 -0
  183. package/lib/themes/catppuccin-mocha.json +9 -0
  184. package/lib/themes/clagentic-dark.json +10 -0
  185. package/lib/themes/clagentic-light.json +10 -0
  186. package/lib/themes/dracula.json +9 -0
  187. package/lib/themes/everforest-light.json +9 -0
  188. package/lib/themes/everforest.json +9 -0
  189. package/lib/themes/github-light.json +9 -0
  190. package/lib/themes/gruvbox-dark.json +9 -0
  191. package/lib/themes/gruvbox-light.json +9 -0
  192. package/lib/themes/monokai.json +9 -0
  193. package/lib/themes/nord-light.json +9 -0
  194. package/lib/themes/nord.json +9 -0
  195. package/lib/themes/one-dark.json +9 -0
  196. package/lib/themes/one-light.json +9 -0
  197. package/lib/themes/rose-pine-dawn.json +9 -0
  198. package/lib/themes/rose-pine.json +9 -0
  199. package/lib/themes/solarized-dark.json +9 -0
  200. package/lib/themes/solarized-light.json +9 -0
  201. package/lib/themes/tokyo-night-light.json +9 -0
  202. package/lib/themes/tokyo-night.json +9 -0
  203. package/lib/updater.js +124 -0
  204. package/lib/user-presence.js +92 -0
  205. package/lib/users-auth.js +146 -0
  206. package/lib/users-permissions.js +118 -0
  207. package/lib/users-preferences.js +254 -0
  208. package/lib/users.js +465 -0
  209. package/lib/utils.js +64 -0
  210. package/lib/worktree.js +134 -0
  211. package/lib/ws-schema.js +464 -0
  212. package/lib/yoke/adapters/claude-worker.js +759 -0
  213. package/lib/yoke/adapters/claude.js +1599 -0
  214. package/lib/yoke/adapters/codex.js +1559 -0
  215. package/lib/yoke/codex-app-server.js +352 -0
  216. package/lib/yoke/index.js +319 -0
  217. package/lib/yoke/instructions.js +61 -0
  218. package/lib/yoke/interface.js +98 -0
  219. package/lib/yoke/mcp-bridge-server.js +302 -0
  220. package/lib/yoke/package.json +7 -0
  221. package/package.json +72 -0
@@ -0,0 +1,1611 @@
1
+ /**
2
+ * Scheduler config module — Create/edit modal, delete dialog, cron builder, preview.
3
+ *
4
+ * Extracted from scheduler.js to keep module sizes manageable.
5
+ */
6
+
7
+ import { showToast } from './utils.js';
8
+
9
+ // Constants (duplicated from scheduler.js — small arrays)
10
+ var DAY_NAMES = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
11
+ var MONTH_NAMES = [
12
+ "January", "February", "March", "April", "May", "June",
13
+ "July", "August", "September", "October", "November", "December"
14
+ ];
15
+
16
+ // Module-private state
17
+ var configCtx = null;
18
+ var previewEl = null;
19
+ var createEditingRecId = null;
20
+ var createSelectedDate = null;
21
+ var createRecurrence = "none";
22
+ var createCustomConfirmed = false;
23
+ var createInterval = "none";
24
+ var createIntervalCustom = null;
25
+ var createIntervalEnd = "allday";
26
+ var createIntervalEndAfter = 5;
27
+ var createIntervalEndTime = "";
28
+ var createColor = "#ffb86c";
29
+ var createEndType = "never";
30
+ var createEndDate = null;
31
+ var createEndCalMonth = null;
32
+ var createEndAfter = 10;
33
+
34
+ // --- Init ---
35
+
36
+ export function initSchedulerConfig(_configCtx) {
37
+ configCtx = _configCtx;
38
+ }
39
+
40
+ // --- Create Popover (inline, Akiflow-style) ---
41
+
42
+ export function setupCreateModal() {
43
+ var createPopover = configCtx.getCreatePopover();
44
+ if (!createPopover) return;
45
+
46
+ // Close
47
+ document.getElementById("sched-create-cancel").addEventListener("click", function () { closeCreateModal(); });
48
+
49
+ // Color picker
50
+ var colorBtn = document.getElementById("sched-create-color-btn");
51
+ var colorPalette = document.getElementById("sched-create-color-palette");
52
+ if (colorBtn && colorPalette) {
53
+ colorBtn.addEventListener("click", function (e) {
54
+ e.stopPropagation();
55
+ colorPalette.classList.toggle("hidden");
56
+ });
57
+ var swatches = colorPalette.querySelectorAll(".sched-color-swatch");
58
+ for (var i = 0; i < swatches.length; i++) {
59
+ swatches[i].addEventListener("click", function (e) {
60
+ e.stopPropagation();
61
+ var c = this.dataset.color;
62
+ createColor = c;
63
+ var dot = document.getElementById("sched-create-color-dot");
64
+ if (dot) dot.style.background = c;
65
+ // update active state
66
+ var all = colorPalette.querySelectorAll(".sched-color-swatch");
67
+ for (var j = 0; j < all.length; j++) {
68
+ all[j].classList.toggle("active", all[j].dataset.color === c);
69
+ }
70
+ colorPalette.classList.add("hidden");
71
+ });
72
+ }
73
+ }
74
+
75
+ // Date picker change -> sync createSelectedDate and recurrence labels
76
+ var datePickerEl = document.getElementById("sched-create-date-picker");
77
+ if (datePickerEl) {
78
+ datePickerEl.addEventListener("change", function () {
79
+ var parts = this.value.split("-");
80
+ if (parts.length === 3) {
81
+ createSelectedDate = new Date(parseInt(parts[0], 10), parseInt(parts[1], 10) - 1, parseInt(parts[2], 10));
82
+ document.getElementById("sched-create-date").value = this.value;
83
+ updateRecurrenceLabels(createSelectedDate);
84
+ enforceMinTime();
85
+ }
86
+ });
87
+ }
88
+
89
+ // Time input change -- enforce min time when today is selected
90
+ var timeInputEl = document.getElementById("sched-create-time");
91
+ if (timeInputEl) {
92
+ timeInputEl.addEventListener("blur", function () {
93
+ enforceMinTime();
94
+ });
95
+ }
96
+
97
+ // Task dropdown
98
+ var taskBtn = document.getElementById("sched-create-task-btn");
99
+ var taskList = document.getElementById("sched-create-task-list");
100
+ if (taskBtn && taskList) {
101
+ taskBtn.addEventListener("click", function (e) {
102
+ e.stopPropagation();
103
+ taskList.classList.toggle("hidden");
104
+ });
105
+ }
106
+
107
+ // Close task dropdown on outside click
108
+ document.addEventListener("click", function (e) {
109
+ var tl = document.getElementById("sched-create-task-list");
110
+ if (tl && !tl.classList.contains("hidden")) {
111
+ if (!tl.contains(e.target) && !e.target.closest("#sched-create-task-btn")) {
112
+ tl.classList.add("hidden");
113
+ }
114
+ }
115
+ });
116
+
117
+ // Review checkbox toggle
118
+ var reviewCheck = document.getElementById("sched-review-check");
119
+ if (reviewCheck) {
120
+ reviewCheck.addEventListener("change", function () {
121
+ var countEl = document.getElementById("sched-review-count");
122
+ if (countEl) countEl.classList.toggle("hidden", !this.checked);
123
+ if (!this.checked) {
124
+ var iterInput = document.getElementById("sched-create-iterations");
125
+ if (iterInput) iterInput.value = "1";
126
+ }
127
+ updateTaskSubtitle();
128
+ });
129
+ }
130
+
131
+ // Iterations input change updates subtitle
132
+ var iterInputForSub = document.getElementById("sched-create-iterations");
133
+ if (iterInputForSub) {
134
+ iterInputForSub.addEventListener("change", function () { updateTaskSubtitle(); });
135
+ }
136
+
137
+ // updateTaskSubtitle is defined at module level (below)
138
+
139
+ // --- Repeat accordion toggle ---
140
+ document.getElementById("sched-accordion-repeat-header").addEventListener("click", function (e) {
141
+ e.stopPropagation();
142
+ var acc = document.getElementById("sched-accordion-repeat");
143
+ var body = document.getElementById("sched-accordion-repeat-body");
144
+ var isOpen = acc.classList.contains("open");
145
+ // Close interval accordion
146
+ document.getElementById("sched-accordion-interval").classList.remove("open");
147
+ document.getElementById("sched-accordion-interval-body").classList.add("hidden");
148
+ acc.classList.toggle("open", !isOpen);
149
+ body.classList.toggle("hidden", isOpen);
150
+ try { lucide.createIcons({ node: acc }); } catch (ex) {}
151
+ });
152
+
153
+ // --- Interval accordion toggle ---
154
+ document.getElementById("sched-accordion-interval-header").addEventListener("click", function (e) {
155
+ e.stopPropagation();
156
+ var acc = document.getElementById("sched-accordion-interval");
157
+ var body = document.getElementById("sched-accordion-interval-body");
158
+ var isOpen = acc.classList.contains("open");
159
+ // Close repeat accordion
160
+ document.getElementById("sched-accordion-repeat").classList.remove("open");
161
+ document.getElementById("sched-accordion-repeat-body").classList.add("hidden");
162
+ acc.classList.toggle("open", !isOpen);
163
+ body.classList.toggle("hidden", isOpen);
164
+ try { lucide.createIcons({ node: acc }); } catch (ex) {}
165
+ });
166
+
167
+ // --- Clear buttons ---
168
+ document.getElementById("sched-accordion-repeat-clear").addEventListener("click", function (e) {
169
+ e.stopPropagation();
170
+ createRecurrence = "none";
171
+ createCustomConfirmed = false;
172
+ // Reset controls
173
+ document.getElementById("sched-custom-interval").value = "1";
174
+ document.getElementById("sched-custom-unit").value = "week";
175
+ var dowSection = document.getElementById("sched-custom-dow-section");
176
+ if (dowSection) dowSection.style.display = "";
177
+ var dowBtns = document.querySelectorAll("#sched-custom-dow-row .sched-dow-btn");
178
+ for (var d = 0; d < dowBtns.length; d++) dowBtns[d].classList.remove("active");
179
+ document.getElementById("sched-custom-end").value = "never";
180
+ document.getElementById("sched-custom-end-label").textContent = "Never";
181
+ var dateBtn3 = document.getElementById("sched-custom-end-date-btn");
182
+ if (dateBtn3) dateBtn3.classList.add("hidden");
183
+ var afterWrap3 = document.getElementById("sched-custom-end-after-wrap");
184
+ if (afterWrap3) afterWrap3.classList.add("hidden");
185
+ var calPanel3 = document.getElementById("sched-custom-end-calendar");
186
+ if (calPanel3) calPanel3.classList.add("hidden");
187
+ // Collapse and update
188
+ document.getElementById("sched-accordion-repeat").classList.remove("open");
189
+ document.getElementById("sched-accordion-repeat-body").classList.add("hidden");
190
+ updateRecurrenceBtn();
191
+ });
192
+
193
+ document.getElementById("sched-accordion-interval-clear").addEventListener("click", function (e) {
194
+ e.stopPropagation();
195
+ createInterval = "none";
196
+ createIntervalCustom = null;
197
+ createIntervalEnd = "allday";
198
+ createIntervalEndAfter = 5;
199
+ createIntervalEndTime = "";
200
+ // Reset controls
201
+ document.getElementById("sched-interval-custom-value").value = "10";
202
+ var segs = document.querySelectorAll(".sched-interval-seg");
203
+ for (var s = 0; s < segs.length; s++) segs[s].classList.toggle("active", segs[s].dataset.unit === "minute");
204
+ var iendOpts2 = document.querySelectorAll(".sched-interval-end-opt");
205
+ for (var ie2 = 0; ie2 < iendOpts2.length; ie2++) iendOpts2[ie2].classList.toggle("active", iendOpts2[ie2].dataset.iend === "allday");
206
+ var iAfterRow = document.getElementById("sched-interval-end-after-row");
207
+ if (iAfterRow) iAfterRow.classList.add("hidden");
208
+ var iUntilRow = document.getElementById("sched-interval-end-until-row");
209
+ if (iUntilRow) iUntilRow.classList.add("hidden");
210
+ // Collapse and update
211
+ document.getElementById("sched-accordion-interval").classList.remove("open");
212
+ document.getElementById("sched-accordion-interval-body").classList.add("hidden");
213
+ updateIntervalBtn();
214
+ });
215
+
216
+ // Interval custom input
217
+ var intCustomValue = document.getElementById("sched-interval-custom-value");
218
+ var intUnitSegs = document.querySelectorAll(".sched-interval-seg");
219
+ function getIntervalUnit() {
220
+ for (var s = 0; s < intUnitSegs.length; s++) {
221
+ if (intUnitSegs[s].classList.contains("active")) return intUnitSegs[s].dataset.unit;
222
+ }
223
+ return "minute";
224
+ }
225
+ function applyInlineInterval() {
226
+ var v = parseInt(intCustomValue.value, 10) || 1;
227
+ var u = getIntervalUnit();
228
+ createInterval = "custom";
229
+ createIntervalCustom = { value: v, unit: u };
230
+ updateIntervalBtn();
231
+ }
232
+ intCustomValue.addEventListener("change", applyInlineInterval);
233
+ for (var si = 0; si < intUnitSegs.length; si++) {
234
+ (function (seg) {
235
+ seg.addEventListener("click", function (e) {
236
+ e.stopPropagation();
237
+ for (var s = 0; s < intUnitSegs.length; s++) {
238
+ intUnitSegs[s].classList.toggle("active", intUnitSegs[s] === seg);
239
+ }
240
+ applyInlineInterval();
241
+ });
242
+ })(intUnitSegs[si]);
243
+ }
244
+
245
+ // Interval end condition options
246
+ var iendOpts = document.querySelectorAll(".sched-interval-end-opt");
247
+ for (var ie = 0; ie < iendOpts.length; ie++) {
248
+ (function (opt) {
249
+ opt.addEventListener("click", function (e) {
250
+ e.stopPropagation();
251
+ var val = opt.dataset.iend;
252
+ for (var j = 0; j < iendOpts.length; j++) {
253
+ iendOpts[j].classList.toggle("active", iendOpts[j] === opt);
254
+ }
255
+ createIntervalEnd = val;
256
+ var afterRow = document.getElementById("sched-interval-end-after-row");
257
+ var untilRow = document.getElementById("sched-interval-end-until-row");
258
+ if (afterRow) afterRow.classList.toggle("hidden", val !== "after");
259
+ if (untilRow) untilRow.classList.toggle("hidden", val !== "until");
260
+ });
261
+ })(iendOpts[ie]);
262
+ }
263
+
264
+ var iendAfterInput = document.getElementById("sched-interval-end-after");
265
+ if (iendAfterInput) {
266
+ iendAfterInput.addEventListener("change", function () {
267
+ createIntervalEndAfter = parseInt(this.value, 10) || 5;
268
+ if (createIntervalEndAfter < 1) createIntervalEndAfter = 1;
269
+ });
270
+ }
271
+
272
+ var iendTimeInput = document.getElementById("sched-interval-end-time");
273
+ if (iendTimeInput) {
274
+ iendTimeInput.addEventListener("change", function () {
275
+ createIntervalEndTime = this.value;
276
+ });
277
+ }
278
+
279
+ // Custom repeat: unit change (applies immediately)
280
+ document.getElementById("sched-custom-unit").addEventListener("change", function () {
281
+ var dowSection = document.getElementById("sched-custom-dow-section");
282
+ if (dowSection) dowSection.style.display = this.value === "week" ? "" : "none";
283
+ createRecurrence = "custom";
284
+ createCustomConfirmed = true;
285
+ updateRecurrenceBtn();
286
+ });
287
+
288
+ // Custom repeat: interval input (applies immediately)
289
+ document.getElementById("sched-custom-interval").addEventListener("change", function () {
290
+ createRecurrence = "custom";
291
+ createCustomConfirmed = true;
292
+ updateRecurrenceBtn();
293
+ });
294
+
295
+ // Custom repeat: DOW toggle
296
+ var customDowBtns = document.querySelectorAll("#sched-custom-dow-row .sched-dow-btn");
297
+ for (var i = 0; i < customDowBtns.length; i++) {
298
+ (function (btn) {
299
+ btn.addEventListener("click", function (e) {
300
+ e.stopPropagation();
301
+ btn.classList.toggle("active");
302
+ createRecurrence = "custom";
303
+ createCustomConfirmed = true;
304
+ updateRecurrenceBtn();
305
+ });
306
+ })(customDowBtns[i]);
307
+ }
308
+
309
+ // Custom repeat: End type JS dropdown
310
+ var endBtn = document.getElementById("sched-custom-end-btn");
311
+ var endList = document.getElementById("sched-custom-end-list");
312
+
313
+ endBtn.addEventListener("click", function (e) {
314
+ e.stopPropagation();
315
+ if (endList.classList.contains("hidden")) {
316
+ var r = endBtn.getBoundingClientRect();
317
+ endList.style.left = r.left + "px";
318
+ endList.style.top = (r.bottom + 4) + "px";
319
+ // If it would overflow bottom, show above
320
+ endList.classList.remove("hidden");
321
+ var lr = endList.getBoundingClientRect();
322
+ if (lr.bottom > window.innerHeight - 8) {
323
+ endList.style.top = (r.top - lr.height - 4) + "px";
324
+ }
325
+ } else {
326
+ endList.classList.add("hidden");
327
+ }
328
+ });
329
+
330
+ var endItems = endList.querySelectorAll(".sched-custom-end-item");
331
+ for (var ei = 0; ei < endItems.length; ei++) {
332
+ (function (item) {
333
+ item.addEventListener("click", function (e) {
334
+ e.stopPropagation();
335
+ var val = item.dataset.value;
336
+ createEndType = val;
337
+ document.getElementById("sched-custom-end").value = val;
338
+ document.getElementById("sched-custom-end-label").textContent = item.textContent;
339
+
340
+ // Update active state
341
+ for (var j = 0; j < endItems.length; j++) {
342
+ endItems[j].classList.toggle("active", endItems[j] === item);
343
+ }
344
+ endList.classList.add("hidden");
345
+
346
+ // Toggle conditional inputs
347
+ var dateBtn2 = document.getElementById("sched-custom-end-date-btn");
348
+ var afterWrap = document.getElementById("sched-custom-end-after-wrap");
349
+ var calPanel = document.getElementById("sched-custom-end-calendar");
350
+
351
+ dateBtn2.classList.add("hidden");
352
+ afterWrap.classList.add("hidden");
353
+ calPanel.classList.add("hidden");
354
+
355
+ if (val === "until") {
356
+ dateBtn2.classList.remove("hidden");
357
+ if (!createEndDate) {
358
+ createEndDate = new Date(createSelectedDate || new Date());
359
+ createEndDate.setMonth(createEndDate.getMonth() + 1);
360
+ }
361
+ updateEndDateLabel();
362
+ } else if (val === "after") {
363
+ afterWrap.classList.remove("hidden");
364
+ document.getElementById("sched-custom-end-after").value = createEndAfter;
365
+ }
366
+ });
367
+ })(endItems[ei]);
368
+ }
369
+
370
+ // Close end dropdown on outside click
371
+ document.addEventListener("click", function (e) {
372
+ if (endList && !endList.classList.contains("hidden")) {
373
+ if (!endList.contains(e.target) && !endBtn.contains(e.target)) {
374
+ endList.classList.add("hidden");
375
+ }
376
+ }
377
+ });
378
+
379
+ // Custom repeat: End date button -> toggle inline calendar
380
+ document.getElementById("sched-custom-end-date-btn").addEventListener("click", function (e) {
381
+ e.stopPropagation();
382
+ var calPanel = document.getElementById("sched-custom-end-calendar");
383
+ if (calPanel.classList.contains("hidden")) {
384
+ createEndCalMonth = new Date(createEndDate.getFullYear(), createEndDate.getMonth(), 1);
385
+ renderEndCalendar();
386
+ calPanel.classList.remove("hidden");
387
+ try { lucide.createIcons({ node: calPanel }); } catch (ex) {}
388
+ } else {
389
+ calPanel.classList.add("hidden");
390
+ }
391
+ });
392
+
393
+ // Custom repeat: End calendar prev/next
394
+ document.getElementById("sched-cal-prev").addEventListener("click", function (e) {
395
+ e.stopPropagation();
396
+ createEndCalMonth.setMonth(createEndCalMonth.getMonth() - 1);
397
+ renderEndCalendar();
398
+ });
399
+ document.getElementById("sched-cal-next").addEventListener("click", function (e) {
400
+ e.stopPropagation();
401
+ createEndCalMonth.setMonth(createEndCalMonth.getMonth() + 1);
402
+ renderEndCalendar();
403
+ });
404
+
405
+ // Custom repeat: After occurrences input
406
+ document.getElementById("sched-custom-end-after").addEventListener("change", function () {
407
+ createEndAfter = parseInt(this.value, 10) || 10;
408
+ if (createEndAfter < 1) { createEndAfter = 1; this.value = 1; }
409
+ });
410
+
411
+ // Submit
412
+ document.getElementById("sched-create-submit").addEventListener("click", function () { submitCreateSchedule(); });
413
+
414
+ // Delete button -> close popover, then open dialog
415
+ var deleteBtn = document.getElementById("sched-create-delete");
416
+ var deleteDialog = document.getElementById("sched-delete-dialog");
417
+ if (deleteBtn) {
418
+ deleteBtn.addEventListener("click", function (e) {
419
+ e.stopPropagation();
420
+ if (!createEditingRecId) return;
421
+ var records = configCtx.getRecords();
422
+ var rec = null;
423
+ for (var j = 0; j < records.length; j++) {
424
+ if (records[j].id === createEditingRecId) { rec = records[j]; break; }
425
+ }
426
+ if (!rec) return;
427
+ // Save context before closing popover
428
+ var deleteRecId = createEditingRecId;
429
+ var deleteDate = createSelectedDate ? new Date(createSelectedDate) : null;
430
+ closeCreateModal();
431
+ openDeleteDialog(deleteRecId, deleteDate, !rec.cron);
432
+ });
433
+ }
434
+
435
+ // Delete dialog option handlers
436
+ if (deleteDialog) {
437
+ var deleteOptions = deleteDialog.querySelectorAll(".sched-delete-option");
438
+ for (var i = 0; i < deleteOptions.length; i++) {
439
+ (function (opt) {
440
+ opt.addEventListener("click", function (e) {
441
+ e.stopPropagation();
442
+ var action = opt.dataset.delete;
443
+ if (action === "cancel") {
444
+ closeDeleteDialog();
445
+ return;
446
+ }
447
+ var recId = deleteDialog.dataset.recId;
448
+ var dateStr = deleteDialog.dataset.eventDate;
449
+ if (!recId) return;
450
+ if (action === "this") {
451
+ if (dateStr) {
452
+ var dp = dateStr.split("-");
453
+ var next = new Date(parseInt(dp[0], 10), parseInt(dp[1], 10) - 1, parseInt(dp[2], 10));
454
+ next.setDate(next.getDate() + 1);
455
+ var newDate = next.getFullYear() + "-" + configCtx.pad(next.getMonth() + 1) + "-" + configCtx.pad(next.getDate());
456
+ configCtx.send({ type: "loop_registry_update", id: recId, data: { date: newDate } });
457
+ }
458
+ } else if (action === "following") {
459
+ if (dateStr) {
460
+ var dp2 = dateStr.split("-");
461
+ var prev = new Date(parseInt(dp2[0], 10), parseInt(dp2[1], 10) - 1, parseInt(dp2[2], 10));
462
+ prev.setDate(prev.getDate() - 1);
463
+ var endDate = prev.getFullYear() + "-" + configCtx.pad(prev.getMonth() + 1) + "-" + configCtx.pad(prev.getDate());
464
+ configCtx.send({ type: "loop_registry_update", id: recId, data: { recurrenceEnd: { type: "until", date: endDate } } });
465
+ }
466
+ } else if (action === "all") {
467
+ configCtx.send({ type: "loop_registry_remove", id: recId });
468
+ }
469
+ closeDeleteDialog();
470
+ });
471
+ })(deleteOptions[i]);
472
+ }
473
+ // Close on backdrop click
474
+ deleteDialog.addEventListener("click", function (e) {
475
+ if (e.target === deleteDialog) closeDeleteDialog();
476
+ });
477
+ }
478
+
479
+ // Close color palette on any click outside it
480
+ document.addEventListener("click", function (e) {
481
+ var pal = document.getElementById("sched-create-color-palette");
482
+ if (pal && !pal.classList.contains("hidden")) {
483
+ if (!pal.contains(e.target) && !e.target.closest("#sched-create-color-btn")) {
484
+ pal.classList.add("hidden");
485
+ }
486
+ }
487
+ });
488
+
489
+ // Close popover on outside click
490
+ document.addEventListener("click", function (e) {
491
+ var cp = configCtx.getCreatePopover();
492
+ if (!cp || cp.classList.contains("hidden")) return;
493
+ if (cp.contains(e.target)) return;
494
+ // Also ignore clicks on calendar cells (they open the popover)
495
+ if (e.target.closest(".scheduler-cell") || e.target.closest(".scheduler-week-slot")) return;
496
+ closeCreateModal();
497
+ });
498
+
499
+ // Escape key
500
+ document.addEventListener("keydown", function (e) {
501
+ var cp = configCtx.getCreatePopover();
502
+ if (e.key === "Escape" && cp && !cp.classList.contains("hidden")) {
503
+ // Close open accordions first
504
+ var openAcc = cp.querySelector(".sched-accordion.open");
505
+ if (openAcc) {
506
+ openAcc.classList.remove("open");
507
+ var openBody = openAcc.querySelector(".sched-accordion-body");
508
+ if (openBody) openBody.classList.add("hidden");
509
+ return;
510
+ }
511
+ closeCreateModal();
512
+ }
513
+ });
514
+ }
515
+
516
+ function updateTaskSubtitle() {
517
+ var sub = document.getElementById("sched-accordion-task-subtitle");
518
+ if (!sub) return;
519
+ var taskLabel = document.getElementById("sched-create-task-label");
520
+ var taskName = (taskLabel && taskLabel.textContent !== "Select a task") ? taskLabel.textContent : null;
521
+ var check = document.getElementById("sched-review-check");
522
+ var iterInput = document.getElementById("sched-create-iterations");
523
+ var iters = iterInput ? (parseInt(iterInput.value, 10) || 1) : 1;
524
+ if (!taskName) {
525
+ sub.textContent = "What to run";
526
+ } else if (check && check.checked && iters > 1) {
527
+ sub.textContent = taskName + " - review " + iters + "x";
528
+ } else {
529
+ sub.textContent = taskName + " - run once";
530
+ }
531
+ }
532
+
533
+ function updateRecurrenceBtn() {
534
+ var acc = document.getElementById("sched-accordion-repeat");
535
+ var hasVal = createRecurrence !== "none";
536
+ if (acc) {
537
+ acc.classList.toggle("has-value", hasVal);
538
+ }
539
+ var clearBtn = document.getElementById("sched-accordion-repeat-clear");
540
+ if (clearBtn) clearBtn.classList.toggle("hidden", !hasVal);
541
+ // Update subtitle with summary or default description
542
+ var sub = document.getElementById("sched-accordion-repeat-subtitle");
543
+ if (!sub) return;
544
+ if (createRecurrence === "none" || !createCustomConfirmed) {
545
+ sub.textContent = "Which days to run";
546
+ return;
547
+ }
548
+ var interval = document.getElementById("sched-custom-interval").value || "1";
549
+ var unit = document.getElementById("sched-custom-unit").value || "week";
550
+ var summary = "Every " + interval + " " + unit;
551
+ if (unit === "week") {
552
+ var activeDow = document.querySelectorAll("#sched-custom-dow-row .sched-dow-btn.active");
553
+ if (activeDow.length > 0) {
554
+ var days = [];
555
+ for (var di = 0; di < activeDow.length; di++) days.push(activeDow[di].textContent);
556
+ summary += " on " + days.join(", ");
557
+ }
558
+ }
559
+ sub.textContent = summary;
560
+ }
561
+
562
+ /**
563
+ * When today is selected, set min on the time input so past times appear disabled.
564
+ * If the current time value is before now, bump it to the next quarter-hour.
565
+ */
566
+ function enforceMinTime() {
567
+ var timeInput = document.getElementById("sched-create-time");
568
+ var datePicker = document.getElementById("sched-create-date-picker");
569
+ if (!timeInput || !datePicker) return;
570
+
571
+ var now = new Date();
572
+ var todayStr = now.getFullYear() + "-" + configCtx.pad(now.getMonth() + 1) + "-" + configCtx.pad(now.getDate());
573
+ var isToday = datePicker.value === todayStr;
574
+
575
+ if (isToday) {
576
+ // Round up to the next minute for min
577
+ var minMinutes = now.getHours() * 60 + now.getMinutes() + 1;
578
+ var minH = Math.floor(minMinutes / 60);
579
+ var minM = minMinutes % 60;
580
+ if (minH >= 24) { minH = 23; minM = 59; }
581
+ var minVal = configCtx.pad(minH) + ":" + configCtx.pad(minM);
582
+ timeInput.min = minVal;
583
+
584
+ // If current value is before min, bump it
585
+ if (timeInput.value < minVal) {
586
+ timeInput.value = minVal;
587
+ }
588
+ } else {
589
+ timeInput.removeAttribute("min");
590
+ }
591
+ }
592
+
593
+ function updateIntervalBtn() {
594
+ var acc = document.getElementById("sched-accordion-interval");
595
+ var hasVal = createInterval !== "none";
596
+ if (acc) {
597
+ acc.classList.toggle("has-value", hasVal);
598
+ }
599
+ var clearBtn = document.getElementById("sched-accordion-interval-clear");
600
+ if (clearBtn) clearBtn.classList.toggle("hidden", !hasVal);
601
+ // Show/hide interval end conditions section
602
+ var endSection = document.getElementById("sched-interval-end-section");
603
+ if (endSection) {
604
+ endSection.classList.toggle("hidden", createInterval === "none");
605
+ }
606
+ // Update subtitle
607
+ var sub = document.getElementById("sched-accordion-interval-subtitle");
608
+ if (sub) {
609
+ if (createInterval === "none" || !createIntervalCustom) {
610
+ sub.textContent = "How often within each day";
611
+ } else {
612
+ var v = createIntervalCustom.value || 10;
613
+ var u = createIntervalCustom.unit === "hour" ? "hrs" : "min";
614
+ sub.textContent = "Every " + v + " " + u;
615
+ }
616
+ }
617
+ updateRecurrenceBtn();
618
+ }
619
+
620
+ export function getPreviewEl() {
621
+ return previewEl;
622
+ }
623
+
624
+ export function removePreview() {
625
+ if (previewEl && previewEl.parentNode) {
626
+ previewEl.parentNode.removeChild(previewEl);
627
+ }
628
+ previewEl = null;
629
+ }
630
+
631
+ export function showPreviewOnCell(cell) {
632
+ removePreview();
633
+ var dragState = configCtx.getDragState();
634
+ var label = dragState.draggedTaskName || "(No title)";
635
+ var el = document.createElement("div");
636
+ el.className = "scheduler-event preview";
637
+ el.textContent = label;
638
+ cell.appendChild(el);
639
+ previewEl = el;
640
+ }
641
+
642
+ export function showPreviewOnSlot(slot) {
643
+ removePreview();
644
+ var dragState = configCtx.getDragState();
645
+ var label = dragState.draggedTaskName || "(No title)";
646
+ var hour = parseInt(slot.dataset.hour, 10);
647
+ var quarter = parseInt(slot.dataset.quarter || "0", 10);
648
+ var minute = quarter * 15;
649
+ var timeStr = configCtx.pad(hour) + ":" + configCtx.pad(minute);
650
+ var col = slot.closest(".scheduler-week-day-col");
651
+ if (!col) return;
652
+ var topPct = ((hour * 60 + minute) / 1440) * 100;
653
+ var el = document.createElement("div");
654
+ el.className = "scheduler-week-event preview";
655
+ el.style.cssText = "top:" + topPct + "%;height:calc(160vh / 48)";
656
+ el.textContent = timeStr + " " + label;
657
+ col.appendChild(el);
658
+ previewEl = el;
659
+ }
660
+
661
+ export function showPreviewForCreate(anchorEl, label) {
662
+ removePreview();
663
+ if (!anchorEl) return;
664
+ var text = label || "(No title)";
665
+ if (anchorEl.classList.contains("scheduler-week-slot")) {
666
+ var hour = parseInt(anchorEl.dataset.hour, 10);
667
+ var quarter = parseInt(anchorEl.dataset.quarter || "0", 10);
668
+ var minute = quarter * 15;
669
+ var timeStr = configCtx.pad(hour) + ":" + configCtx.pad(minute);
670
+ var col = anchorEl.closest(".scheduler-week-day-col");
671
+ if (!col) return;
672
+ var topPct = ((hour * 60 + minute) / 1440) * 100;
673
+ var el = document.createElement("div");
674
+ el.className = "scheduler-week-event preview";
675
+ el.style.cssText = "top:" + topPct + "%;height:calc(160vh / 48)";
676
+ el.textContent = timeStr + " " + text;
677
+ col.appendChild(el);
678
+ previewEl = el;
679
+ } else if (anchorEl.classList.contains("scheduler-cell")) {
680
+ var el = document.createElement("div");
681
+ el.className = "scheduler-event preview";
682
+ el.textContent = text;
683
+ anchorEl.appendChild(el);
684
+ previewEl = el;
685
+ }
686
+ }
687
+
688
+ export function applyDraggedTask() {
689
+ var dragState = configCtx.getDragState();
690
+ if (!dragState.draggedTaskId) return;
691
+ var taskHidden = document.getElementById("sched-create-task");
692
+ var taskLabel = document.getElementById("sched-create-task-label");
693
+ var taskBtn = document.getElementById("sched-create-task-btn");
694
+ if (taskHidden) taskHidden.value = dragState.draggedTaskId;
695
+ if (taskLabel) taskLabel.textContent = dragState.draggedTaskName || dragState.draggedTaskId;
696
+ if (taskBtn) { taskBtn.classList.add("has-value"); taskBtn.classList.remove("invalid"); }
697
+ updateTaskSubtitle();
698
+ // Mark the matching item as selected in the dropdown list
699
+ var taskListEl = document.getElementById("sched-create-task-list");
700
+ if (taskListEl) {
701
+ var items = taskListEl.querySelectorAll(".sched-create-task-item");
702
+ for (var k = 0; k < items.length; k++) {
703
+ items[k].classList.toggle("selected", items[k].dataset.taskId === dragState.draggedTaskId);
704
+ }
705
+ }
706
+ // Auto-generate title: "taskName - HH:MM"
707
+ var titleInput = document.getElementById("sched-create-title");
708
+ var timeInput = document.getElementById("sched-create-time");
709
+ if (titleInput && (dragState.draggedTaskName || dragState.draggedTaskId)) {
710
+ var name = dragState.draggedTaskName || dragState.draggedTaskId;
711
+ var time = timeInput ? timeInput.value : "";
712
+ titleInput.value = time ? name + " - " + time : name;
713
+ }
714
+ // Update preview text to match auto-title
715
+ if (previewEl && titleInput) {
716
+ var previewText = titleInput.value || "(No title)";
717
+ if (previewEl.classList.contains("scheduler-week-event") && timeInput) {
718
+ previewText = timeInput.value + " " + (titleInput.value || "(No title)");
719
+ }
720
+ previewEl.textContent = previewText;
721
+ }
722
+ configCtx.clearDragState();
723
+ }
724
+
725
+ export function openCreateModalWithRecord(rec, anchorEl) {
726
+ var createPopover = configCtx.getCreatePopover();
727
+ // Parse date/time from record
728
+ var date = null;
729
+ var hour = null;
730
+ if (rec.date) {
731
+ var dp = rec.date.split("-");
732
+ date = new Date(parseInt(dp[0], 10), parseInt(dp[1], 10) - 1, parseInt(dp[2], 10));
733
+ }
734
+ if (rec.time) {
735
+ var tp = rec.time.split(":");
736
+ hour = parseInt(tp[0], 10) || 0;
737
+ var mins = parseInt(tp[1], 10) || 0;
738
+ if (date) { date.setHours(hour, mins, 0); }
739
+ }
740
+ // Mark as editing existing record
741
+ createEditingRecId = rec.id;
742
+
743
+ // Open the create modal normally first
744
+ openCreateModal(date || new Date(), hour, anchorEl);
745
+
746
+ // Show delete button
747
+ var deleteBtn = document.getElementById("sched-create-delete");
748
+ if (deleteBtn) deleteBtn.classList.remove("hidden");
749
+
750
+ // Now override with record values
751
+ var titleInput = document.getElementById("sched-create-title");
752
+ if (titleInput) titleInput.value = rec.name || "";
753
+
754
+ // Show description row only if the record already has one
755
+ var descRow = document.getElementById("sched-create-desc-row");
756
+ var descInput = document.getElementById("sched-create-desc");
757
+ if (rec.description) {
758
+ if (descInput) descInput.value = rec.description;
759
+ if (descRow) descRow.classList.remove("hidden");
760
+ } else {
761
+ if (descInput) descInput.value = "";
762
+ if (descRow) descRow.classList.add("hidden");
763
+ }
764
+
765
+ // Set color
766
+ if (rec.color) {
767
+ createColor = rec.color;
768
+ var colorDot = document.getElementById("sched-create-color-dot");
769
+ if (colorDot) colorDot.style.background = createColor;
770
+ var swatches = createPopover.querySelectorAll(".sched-color-swatch");
771
+ for (var si = 0; si < swatches.length; si++) {
772
+ swatches[si].classList.toggle("active", swatches[si].dataset.color === createColor);
773
+ }
774
+ }
775
+
776
+ // Set skip-if-running
777
+ var skipRunningEl = document.getElementById("sched-skip-running");
778
+ if (skipRunningEl) skipRunningEl.checked = rec.skipIfRunning !== false;
779
+
780
+ // Set iterations + review checkbox
781
+ var hasReview = rec.maxIterations && rec.maxIterations > 1;
782
+ var iterInput = document.getElementById("sched-create-iterations");
783
+ if (iterInput) iterInput.value = hasReview ? rec.maxIterations : 3;
784
+ var reviewCheckEdit = document.getElementById("sched-review-check");
785
+ if (reviewCheckEdit) reviewCheckEdit.checked = hasReview;
786
+ var reviewCountEdit = document.getElementById("sched-review-count");
787
+ if (reviewCountEdit) reviewCountEdit.classList.toggle("hidden", !hasReview);
788
+
789
+ // Set linked task
790
+ if (rec.linkedTaskId) {
791
+ var taskHidden = document.getElementById("sched-create-task");
792
+ var taskLabel2 = document.getElementById("sched-create-task-label");
793
+ var taskBtn = document.getElementById("sched-create-task-btn");
794
+ var taskListEl = document.getElementById("sched-create-task-list");
795
+ var records = configCtx.getRecords();
796
+ if (taskHidden) taskHidden.value = rec.linkedTaskId;
797
+ // Find the task name
798
+ var taskName = rec.linkedTaskId;
799
+ for (var j = 0; j < records.length; j++) {
800
+ if (records[j].id === rec.linkedTaskId) { taskName = records[j].name || records[j].id; break; }
801
+ }
802
+ if (taskLabel2) taskLabel2.textContent = taskName;
803
+ if (taskBtn) { taskBtn.classList.add("has-value"); taskBtn.classList.remove("invalid"); }
804
+ // (no accordion to update)
805
+ if (taskListEl) {
806
+ var items = taskListEl.querySelectorAll(".sched-create-task-item");
807
+ for (var k = 0; k < items.length; k++) {
808
+ items[k].classList.toggle("selected", items[k].dataset.taskId === rec.linkedTaskId);
809
+ }
810
+ }
811
+ updateTaskSubtitle();
812
+ }
813
+
814
+ // Restore interval from cron
815
+ if (rec.cron) {
816
+ var cronParts = rec.cron.trim().split(/\s+/);
817
+ if (cronParts.length === 5) {
818
+ var detectedMinInterval = null;
819
+ var detectedHrInterval = null;
820
+ // Detect minute-level interval: e.g. "0,5,10,... * * * *" or "*/5 * * * *"
821
+ if (cronParts[1] === "*" && cronParts[2] === "*") {
822
+ detectedMinInterval = configCtx.detectInterval(cronParts[0], 60);
823
+ }
824
+ // Detect hour-level interval: e.g. "0 1,3,5,... * * *"
825
+ if (!detectedMinInterval && cronParts[2] === "*") {
826
+ detectedHrInterval = configCtx.detectInterval(cronParts[1], 24);
827
+ }
828
+
829
+ if (detectedMinInterval) {
830
+ createInterval = "custom";
831
+ createIntervalCustom = { value: detectedMinInterval, unit: "minute" };
832
+ var intValEl = document.getElementById("sched-interval-custom-value");
833
+ if (intValEl) intValEl.value = detectedMinInterval;
834
+ var intUnitBtns = document.querySelectorAll("#sched-interval-custom-unit .sched-interval-seg");
835
+ for (var iu = 0; iu < intUnitBtns.length; iu++) {
836
+ intUnitBtns[iu].classList.toggle("active", intUnitBtns[iu].dataset.unit === "minute");
837
+ }
838
+ updateIntervalBtn();
839
+ } else if (detectedHrInterval) {
840
+ createInterval = "custom";
841
+ createIntervalCustom = { value: detectedHrInterval, unit: "hour" };
842
+ var intValEl2 = document.getElementById("sched-interval-custom-value");
843
+ if (intValEl2) intValEl2.value = detectedHrInterval;
844
+ var intUnitBtns2 = document.querySelectorAll("#sched-interval-custom-unit .sched-interval-seg");
845
+ for (var iu2 = 0; iu2 < intUnitBtns2.length; iu2++) {
846
+ intUnitBtns2[iu2].classList.toggle("active", intUnitBtns2[iu2].dataset.unit === "hour");
847
+ }
848
+ updateIntervalBtn();
849
+ }
850
+
851
+ // Restore recurrence from cron (if no interval detected, or combined with interval)
852
+ if (!detectedMinInterval && !detectedHrInterval) {
853
+ var cronDow = cronParts[4];
854
+ var cronDom = cronParts[2];
855
+ var cronMonth = cronParts[3];
856
+ if (cronDow === "*" && cronDom === "*" && cronMonth === "*") {
857
+ createRecurrence = "daily";
858
+ } else if (cronDow === "1-5" && cronDom === "*") {
859
+ createRecurrence = "weekdays";
860
+ } else if (cronDom !== "*" && cronMonth !== "*") {
861
+ createRecurrence = "yearly";
862
+ } else if (cronDom !== "*" && cronDow === "*") {
863
+ createRecurrence = "monthly";
864
+ } else if (cronDow !== "*" && cronDom === "*") {
865
+ // Check if it matches a single day (weekly)
866
+ var dowVals = cronDow.split(",");
867
+ if (dowVals.length === 1) {
868
+ createRecurrence = "weekly";
869
+ } else if (dowVals.length === 7) {
870
+ createRecurrence = "daily";
871
+ } else {
872
+ createRecurrence = "custom";
873
+ createCustomConfirmed = true;
874
+ // Set custom panel values
875
+ document.getElementById("sched-custom-interval").value = "1";
876
+ document.getElementById("sched-custom-unit").value = "week";
877
+ var customDowBtns = document.querySelectorAll("#sched-custom-dow-row .sched-dow-btn");
878
+ for (var cd = 0; cd < customDowBtns.length; cd++) {
879
+ customDowBtns[cd].classList.toggle("active", dowVals.indexOf(customDowBtns[cd].dataset.dow) !== -1);
880
+ }
881
+ }
882
+ }
883
+ updateRecurrenceBtn();
884
+ }
885
+ }
886
+ }
887
+
888
+ // Restore interval end conditions
889
+ if (rec.intervalEnd) {
890
+ createIntervalEnd = rec.intervalEnd.type || "allday";
891
+ var editIendOpts = document.querySelectorAll(".sched-interval-end-opt");
892
+ for (var ei = 0; ei < editIendOpts.length; ei++) {
893
+ editIendOpts[ei].classList.toggle("active", editIendOpts[ei].dataset.iend === createIntervalEnd);
894
+ }
895
+ var editAfterRow = document.getElementById("sched-interval-end-after-row");
896
+ var editUntilRow = document.getElementById("sched-interval-end-until-row");
897
+ if (createIntervalEnd === "after") {
898
+ createIntervalEndAfter = rec.intervalEnd.count || 5;
899
+ if (editAfterRow) editAfterRow.classList.remove("hidden");
900
+ if (editUntilRow) editUntilRow.classList.add("hidden");
901
+ var editAfterInput = document.getElementById("sched-interval-end-after");
902
+ if (editAfterInput) editAfterInput.value = createIntervalEndAfter;
903
+ } else if (createIntervalEnd === "until") {
904
+ createIntervalEndTime = rec.intervalEnd.time || "18:00";
905
+ if (editAfterRow) editAfterRow.classList.add("hidden");
906
+ if (editUntilRow) editUntilRow.classList.remove("hidden");
907
+ var editTimeInput = document.getElementById("sched-interval-end-time");
908
+ if (editTimeInput) editTimeInput.value = createIntervalEndTime;
909
+ }
910
+ }
911
+
912
+ // Update preview to show record name
913
+ if (previewEl) {
914
+ var previewText = rec.name || "(No title)";
915
+ if (previewEl.classList.contains("scheduler-week-event") && rec.time) {
916
+ previewText = rec.time + " " + previewText;
917
+ }
918
+ previewEl.textContent = previewText;
919
+ }
920
+ }
921
+
922
+ export function openCreateModal(date, hour, anchorEl) {
923
+ var createPopover = configCtx.getCreatePopover();
924
+ if (!createPopover) return;
925
+ // Reset editing state (openCreateModalWithRecord sets this before calling us)
926
+ if (!createEditingRecId) {
927
+ var deleteBtn = document.getElementById("sched-create-delete");
928
+ if (deleteBtn) deleteBtn.classList.add("hidden");
929
+ }
930
+ createSelectedDate = date || new Date();
931
+ createRecurrence = "none";
932
+ createCustomConfirmed = false;
933
+ createInterval = "none";
934
+ createIntervalCustom = null;
935
+ createIntervalEnd = "allday";
936
+ createIntervalEndAfter = 5;
937
+ createIntervalEndTime = "";
938
+ createColor = "#ffb86c";
939
+
940
+ // Reset form
941
+ document.getElementById("sched-create-title").value = "";
942
+ document.getElementById("sched-create-desc").value = "";
943
+ var descRowReset = document.getElementById("sched-create-desc-row");
944
+ if (descRowReset) descRowReset.classList.add("hidden");
945
+ var iterReset = document.getElementById("sched-create-iterations");
946
+ if (iterReset) iterReset.value = "3";
947
+ var reviewCheck = document.getElementById("sched-review-check");
948
+ if (reviewCheck) reviewCheck.checked = false;
949
+ var reviewCount = document.getElementById("sched-review-count");
950
+ if (reviewCount) reviewCount.classList.add("hidden");
951
+ // (task is always visible, no accordion reset needed)
952
+
953
+ // Reset color
954
+ var colorDot = document.getElementById("sched-create-color-dot");
955
+ if (colorDot) colorDot.style.background = createColor;
956
+ var palette = document.getElementById("sched-create-color-palette");
957
+ if (palette) palette.classList.add("hidden");
958
+ var swatches = createPopover.querySelectorAll(".sched-color-swatch");
959
+ for (var si = 0; si < swatches.length; si++) {
960
+ swatches[si].classList.toggle("active", swatches[si].dataset.color === createColor);
961
+ }
962
+
963
+ // Populate task dropdown (only tasks -- exclude ralph and schedule)
964
+ var taskHidden = document.getElementById("sched-create-task");
965
+ var taskLabel = document.getElementById("sched-create-task-label");
966
+ var taskBtn = document.getElementById("sched-create-task-btn");
967
+ var taskListEl = document.getElementById("sched-create-task-list");
968
+ var records = configCtx.getRecords();
969
+ if (taskHidden) taskHidden.value = "";
970
+ if (taskLabel) taskLabel.textContent = "Select a task";
971
+ if (taskBtn) { taskBtn.classList.remove("has-value"); taskBtn.classList.remove("invalid"); }
972
+ if (taskListEl) {
973
+ taskListEl.classList.add("hidden");
974
+ var tasks = records.filter(function (r) { return r.source !== "ralph" && r.source !== "schedule"; });
975
+ var html = "";
976
+ if (tasks.length === 0) {
977
+ html = '<div class="sched-create-task-empty">No tasks available</div>';
978
+ } else {
979
+ for (var i = 0; i < tasks.length; i++) {
980
+ html += '<div class="sched-create-task-item" data-task-id="' + configCtx.esc(tasks[i].id) + '">' + configCtx.esc(tasks[i].name || tasks[i].id) + '</div>';
981
+ }
982
+ }
983
+ taskListEl.innerHTML = html;
984
+ // Bind click handlers
985
+ var items = taskListEl.querySelectorAll(".sched-create-task-item");
986
+ for (var j = 0; j < items.length; j++) {
987
+ (function (item) {
988
+ item.addEventListener("click", function (e) {
989
+ e.stopPropagation();
990
+ var id = item.dataset.taskId;
991
+ var name = item.textContent;
992
+ if (taskHidden) taskHidden.value = id;
993
+ if (taskLabel) taskLabel.textContent = name;
994
+ if (taskBtn) { taskBtn.classList.add("has-value"); taskBtn.classList.remove("invalid"); }
995
+ // (no accordion to update)
996
+ // Update selected state
997
+ var all = taskListEl.querySelectorAll(".sched-create-task-item");
998
+ for (var k = 0; k < all.length; k++) {
999
+ all[k].classList.toggle("selected", all[k] === item);
1000
+ }
1001
+ taskListEl.classList.add("hidden");
1002
+ updateTaskSubtitle();
1003
+ });
1004
+ })(items[j]);
1005
+ }
1006
+ }
1007
+
1008
+ // Set date picker
1009
+ var dateStr = createSelectedDate.getFullYear() + "-" + configCtx.pad(createSelectedDate.getMonth() + 1) + "-" + configCtx.pad(createSelectedDate.getDate());
1010
+ document.getElementById("sched-create-date").value = dateStr;
1011
+ var datePicker = document.getElementById("sched-create-date-picker");
1012
+ if (datePicker) {
1013
+ datePicker.value = dateStr;
1014
+ var todayNow = new Date();
1015
+ var todayMin = todayNow.getFullYear() + "-" + configCtx.pad(todayNow.getMonth() + 1) + "-" + configCtx.pad(todayNow.getDate());
1016
+ datePicker.min = todayMin;
1017
+ }
1018
+
1019
+ // Time (use minutes from createSelectedDate for 15-min snapping)
1020
+ if (hour !== null && hour !== undefined) {
1021
+ var mins = createSelectedDate.getMinutes ? createSelectedDate.getMinutes() : 0;
1022
+ document.getElementById("sched-create-time").value = configCtx.pad(hour) + ":" + configCtx.pad(mins);
1023
+ } else {
1024
+ // Default to current time (next quarter-hour)
1025
+ var nowT = new Date();
1026
+ var nowMins = nowT.getHours() * 60 + nowT.getMinutes();
1027
+ var nextQ = Math.ceil(nowMins / 15) * 15;
1028
+ var defH = Math.floor(nextQ / 60);
1029
+ var defM = nextQ % 60;
1030
+ if (defH >= 24) { defH = 23; defM = 45; }
1031
+ document.getElementById("sched-create-time").value = configCtx.pad(defH) + ":" + configCtx.pad(defM);
1032
+ }
1033
+
1034
+ // Update recurrence labels
1035
+ updateRecurrenceLabels(createSelectedDate);
1036
+
1037
+ // Enforce min time for today
1038
+ enforceMinTime();
1039
+
1040
+ // Reset recurrence
1041
+ createRecurrence = "none";
1042
+ createCustomConfirmed = false;
1043
+ updateRecurrenceBtn();
1044
+
1045
+ // Reset interval accordion
1046
+ document.getElementById("sched-accordion-interval").classList.remove("open");
1047
+ document.getElementById("sched-accordion-interval-body").classList.add("hidden");
1048
+ var timeInput = document.getElementById("sched-create-time");
1049
+ if (timeInput) timeInput.style.display = "";
1050
+
1051
+ // Reset interval end conditions
1052
+ var iendOpts = document.querySelectorAll(".sched-interval-end-opt");
1053
+ for (var ie = 0; ie < iendOpts.length; ie++) {
1054
+ iendOpts[ie].classList.toggle("active", iendOpts[ie].dataset.iend === "allday");
1055
+ }
1056
+ var iendAfterRow = document.getElementById("sched-interval-end-after-row");
1057
+ if (iendAfterRow) iendAfterRow.classList.add("hidden");
1058
+ var iendUntilRow = document.getElementById("sched-interval-end-until-row");
1059
+ if (iendUntilRow) iendUntilRow.classList.add("hidden");
1060
+ var iendAfterInput = document.getElementById("sched-interval-end-after");
1061
+ if (iendAfterInput) iendAfterInput.value = "5";
1062
+ var iendTimeInput = document.getElementById("sched-interval-end-time");
1063
+ if (iendTimeInput) iendTimeInput.value = "18:00";
1064
+
1065
+ updateIntervalBtn();
1066
+
1067
+ // Reset repeat accordion
1068
+ document.getElementById("sched-accordion-repeat").classList.remove("open");
1069
+ document.getElementById("sched-accordion-repeat-body").classList.add("hidden");
1070
+ document.getElementById("sched-custom-interval").value = "1";
1071
+ document.getElementById("sched-custom-unit").value = "week";
1072
+ document.getElementById("sched-custom-dow-section").style.display = "";
1073
+ var customDowBtns = document.querySelectorAll("#sched-custom-dow-row .sched-dow-btn");
1074
+ for (var i = 0; i < customDowBtns.length; i++) {
1075
+ customDowBtns[i].classList.toggle("active", parseInt(customDowBtns[i].dataset.dow) === createSelectedDate.getDay());
1076
+ }
1077
+ document.getElementById("sched-custom-end").value = "never";
1078
+ document.getElementById("sched-custom-end-label").textContent = "Never";
1079
+ var endItems = document.querySelectorAll(".sched-custom-end-item");
1080
+ for (var ei = 0; ei < endItems.length; ei++) {
1081
+ endItems[ei].classList.toggle("active", endItems[ei].dataset.value === "never");
1082
+ }
1083
+ document.getElementById("sched-custom-end-list").classList.add("hidden");
1084
+ createEndType = "never";
1085
+ createEndDate = null;
1086
+ createEndAfter = 10;
1087
+ document.getElementById("sched-custom-end-date-btn").classList.add("hidden");
1088
+ document.getElementById("sched-custom-end-after-wrap").classList.add("hidden");
1089
+ document.getElementById("sched-custom-end-calendar").classList.add("hidden");
1090
+
1091
+ // Show preview event on the calendar cell
1092
+ var dragState = configCtx.getDragState();
1093
+ showPreviewForCreate(anchorEl, dragState.draggedTaskName || null);
1094
+
1095
+ // Position near anchor cell
1096
+ createPopover.classList.remove("hidden");
1097
+ positionCreatePopover(anchorEl);
1098
+
1099
+ try { lucide.createIcons({ node: createPopover }); } catch (e) {}
1100
+ setTimeout(function () { document.getElementById("sched-create-title").focus(); }, 50);
1101
+ }
1102
+
1103
+ function positionCreatePopover(anchorEl) {
1104
+ var createPopover = configCtx.getCreatePopover();
1105
+ var contentCalEl = configCtx.getContentCalEl();
1106
+ if (!createPopover || !anchorEl) {
1107
+ // Fallback: center in scheduler content area
1108
+ if (createPopover && contentCalEl) {
1109
+ var cRect = contentCalEl.getBoundingClientRect();
1110
+ createPopover.style.left = (cRect.left + cRect.width / 2 - 180) + "px";
1111
+ createPopover.style.top = (cRect.top + 60) + "px";
1112
+ }
1113
+ return;
1114
+ }
1115
+
1116
+ var rect = anchorEl.getBoundingClientRect();
1117
+ var popW = 360;
1118
+ var popH = createPopover.offsetHeight || 300;
1119
+
1120
+ // Try to place to the right of the cell
1121
+ var left = rect.right + 8;
1122
+ var top = rect.top;
1123
+
1124
+ // If it overflows right, place to the left
1125
+ if (left + popW > window.innerWidth - 10) {
1126
+ left = rect.left - popW - 8;
1127
+ }
1128
+ // If it still overflows left, center horizontally on the cell
1129
+ if (left < 10) {
1130
+ left = Math.max(10, rect.left + rect.width / 2 - popW / 2);
1131
+ }
1132
+
1133
+ // Vertical: don't overflow bottom
1134
+ if (top + popH > window.innerHeight - 10) {
1135
+ top = window.innerHeight - popH - 10;
1136
+ }
1137
+ if (top < 10) top = 10;
1138
+
1139
+ createPopover.style.left = left + "px";
1140
+ createPopover.style.top = top + "px";
1141
+ }
1142
+
1143
+ function updateRecurrenceLabels(date) {
1144
+ var dow = date.getDay();
1145
+ var dayName = DAY_NAMES[dow];
1146
+ var dom = date.getDate();
1147
+ var monthName = MONTH_NAMES[date.getMonth()];
1148
+
1149
+ // Weekly on {day}
1150
+ var weeklyEl = document.getElementById("sched-recurrence-weekly");
1151
+ if (weeklyEl) weeklyEl.textContent = "Weekly on " + dayName;
1152
+
1153
+ // Every second {day} of the month
1154
+ var weekOfMonth = Math.ceil(dom / 7);
1155
+ var ordinals = ["", "first", "second", "third", "fourth", "fifth"];
1156
+ var biweeklyEl = document.getElementById("sched-recurrence-biweekly");
1157
+ if (biweeklyEl) {
1158
+ var ordStr = ordinals[weekOfMonth] || weekOfMonth + "th";
1159
+ biweeklyEl.textContent = "Every " + ordStr + " " + dayName + " of the mo...";
1160
+ }
1161
+
1162
+ // Every year on {month} {date}
1163
+ var yearlyEl = document.getElementById("sched-recurrence-yearly");
1164
+ if (yearlyEl) yearlyEl.textContent = "Every year on " + monthName + " " + dom;
1165
+
1166
+ // Every month on the {date}th
1167
+ var monthlyEl = document.getElementById("sched-recurrence-monthly");
1168
+ if (monthlyEl) {
1169
+ var suffix = "th";
1170
+ if (dom === 1 || dom === 21 || dom === 31) suffix = "st";
1171
+ else if (dom === 2 || dom === 22) suffix = "nd";
1172
+ else if (dom === 3 || dom === 23) suffix = "rd";
1173
+ monthlyEl.textContent = "Every month on the " + dom + suffix;
1174
+ }
1175
+ }
1176
+
1177
+ export function closeCreateModal() {
1178
+ var createPopover = configCtx.getCreatePopover();
1179
+ if (createPopover) createPopover.classList.add("hidden");
1180
+ // Collapse accordions
1181
+ var accRepeat = document.getElementById("sched-accordion-repeat");
1182
+ if (accRepeat) { accRepeat.classList.remove("open"); }
1183
+ var accRepeatBody = document.getElementById("sched-accordion-repeat-body");
1184
+ if (accRepeatBody) accRepeatBody.classList.add("hidden");
1185
+ var accInterval = document.getElementById("sched-accordion-interval");
1186
+ if (accInterval) { accInterval.classList.remove("open"); }
1187
+ var accIntervalBody = document.getElementById("sched-accordion-interval-body");
1188
+ if (accIntervalBody) accIntervalBody.classList.add("hidden");
1189
+ var pal = document.getElementById("sched-create-color-palette");
1190
+ if (pal) pal.classList.add("hidden");
1191
+ var tl = document.getElementById("sched-create-task-list");
1192
+ if (tl) tl.classList.add("hidden");
1193
+ removePreview();
1194
+ createSelectedDate = null;
1195
+ createEditingRecId = null;
1196
+ }
1197
+
1198
+ function openDeleteDialog(recId, eventDate, isOneOff) {
1199
+ var dialog = document.getElementById("sched-delete-dialog");
1200
+ if (!dialog) return;
1201
+ dialog.dataset.recId = recId;
1202
+ if (eventDate) {
1203
+ dialog.dataset.eventDate = eventDate.getFullYear() + "-" + configCtx.pad(eventDate.getMonth() + 1) + "-" + configCtx.pad(eventDate.getDate());
1204
+ } else {
1205
+ dialog.dataset.eventDate = "";
1206
+ }
1207
+ // Toggle between one-off and recurring UI
1208
+ var title = dialog.querySelector(".sched-delete-dialog-title");
1209
+ var body = dialog.querySelector(".sched-delete-dialog-body");
1210
+ var footer = dialog.querySelector(".sched-delete-dialog-footer");
1211
+ var cancelBtn = dialog.querySelector('[data-delete="cancel"]');
1212
+ dialog.dataset.oneOff = isOneOff ? "1" : "";
1213
+ if (isOneOff) {
1214
+ if (title) title.textContent = "Delete this event?";
1215
+ if (body) body.classList.add("hidden");
1216
+ if (cancelBtn) cancelBtn.textContent = "Cancel";
1217
+ // Add a "Delete" button next to cancel in footer
1218
+ var existingDel = footer ? footer.querySelector(".sched-delete-confirm-btn") : null;
1219
+ if (!existingDel && footer) {
1220
+ var delBtn = document.createElement("button");
1221
+ delBtn.className = "sched-delete-option danger sched-delete-confirm-btn";
1222
+ delBtn.dataset.delete = "all";
1223
+ delBtn.textContent = "Delete";
1224
+ footer.appendChild(delBtn);
1225
+ delBtn.addEventListener("click", function (e) {
1226
+ e.stopPropagation();
1227
+ var rid = dialog.dataset.recId;
1228
+ if (rid) configCtx.send({ type: "loop_registry_remove", id: rid });
1229
+ closeDeleteDialog();
1230
+ });
1231
+ }
1232
+ if (existingDel) existingDel.classList.remove("hidden");
1233
+ } else {
1234
+ if (title) title.textContent = "Delete recurring event";
1235
+ if (body) body.classList.remove("hidden");
1236
+ if (cancelBtn) cancelBtn.textContent = "Cancel";
1237
+ var existingDel = footer ? footer.querySelector(".sched-delete-confirm-btn") : null;
1238
+ if (existingDel) existingDel.classList.add("hidden");
1239
+ }
1240
+ dialog.classList.remove("hidden");
1241
+ }
1242
+
1243
+ function closeDeleteDialog() {
1244
+ var dialog = document.getElementById("sched-delete-dialog");
1245
+ if (dialog) {
1246
+ dialog.classList.add("hidden");
1247
+ dialog.dataset.recId = "";
1248
+ dialog.dataset.eventDate = "";
1249
+ }
1250
+ }
1251
+
1252
+ // Build an explicit list of values offset from a start value with a given step, wrapping at max
1253
+ function buildOffsetList(start, step, max) {
1254
+ var vals = [];
1255
+ var v = start % max;
1256
+ for (var i = 0; i < max; i += step) {
1257
+ vals.push(v);
1258
+ v = (v + step) % max;
1259
+ }
1260
+ vals.sort(function (a, b) { return a - b; });
1261
+ return vals.join(",");
1262
+ }
1263
+
1264
+ function buildCreateCron() {
1265
+ if (!createSelectedDate) return null;
1266
+
1267
+ var timeVal = document.getElementById("sched-create-time").value || "09:00";
1268
+ var timeParts = timeVal.split(":");
1269
+ var h = parseInt(timeParts[0], 10);
1270
+ var m = parseInt(timeParts[1], 10);
1271
+
1272
+ var dow = createSelectedDate.getDay();
1273
+ var dom = createSelectedDate.getDate();
1274
+ var month = createSelectedDate.getMonth() + 1;
1275
+
1276
+ // Determine interval minutes
1277
+ var intervalMins = 0;
1278
+ if (createInterval !== "none") {
1279
+ if (createInterval === "custom" && createIntervalCustom) {
1280
+ intervalMins = createIntervalCustom.unit === "hour"
1281
+ ? createIntervalCustom.value * 60
1282
+ : createIntervalCustom.value;
1283
+ } else {
1284
+ intervalMins = parseInt(createInterval, 10) || 0;
1285
+ }
1286
+ }
1287
+
1288
+ // Interval only (no recurrence) = interval every day
1289
+ if (intervalMins > 0 && createRecurrence === "none") {
1290
+ if (intervalMins < 60) return buildOffsetList(m, intervalMins, 60) + " * * * *";
1291
+ var intHrs = Math.floor(intervalMins / 60);
1292
+ return String(m) + " " + buildOffsetList(h, intHrs, 24) + " * * *";
1293
+ }
1294
+
1295
+ if (createRecurrence === "none" && intervalMins === 0) return null;
1296
+
1297
+ // Build minute/hour fields from interval or time
1298
+ var minField = String(m);
1299
+ var hourField = String(h);
1300
+ if (intervalMins > 0 && intervalMins < 60) {
1301
+ minField = buildOffsetList(m, intervalMins, 60);
1302
+ hourField = "*";
1303
+ } else if (intervalMins >= 60) {
1304
+ var intHrs2 = Math.floor(intervalMins / 60);
1305
+ minField = String(m);
1306
+ hourField = buildOffsetList(h, intHrs2, 24);
1307
+ }
1308
+
1309
+ if (createRecurrence === "daily") return minField + " " + hourField + " * * *";
1310
+ if (createRecurrence === "weekly") return minField + " " + hourField + " * * " + dow;
1311
+ if (createRecurrence === "biweekly") {
1312
+ var weekNum = Math.ceil(dom / 7);
1313
+ return minField + " " + hourField + " " + ((weekNum - 1) * 7 + 1) + "-" + (weekNum * 7) + " * " + dow;
1314
+ }
1315
+ if (createRecurrence === "yearly") return minField + " " + hourField + " " + dom + " " + month + " *";
1316
+ if (createRecurrence === "monthly") return minField + " " + hourField + " " + dom + " * *";
1317
+ if (createRecurrence === "weekdays") return minField + " " + hourField + " * * 1-5";
1318
+
1319
+ if (createRecurrence === "custom" && createCustomConfirmed) {
1320
+ return buildCustomCron(h, m);
1321
+ }
1322
+
1323
+ return null;
1324
+ }
1325
+
1326
+ function updateEndDateLabel() {
1327
+ var label = document.getElementById("sched-custom-end-date-label");
1328
+ if (!label || !createEndDate) return;
1329
+ var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
1330
+ var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
1331
+ label.textContent = days[createEndDate.getDay()] + ", " + months[createEndDate.getMonth()] + " " + createEndDate.getDate();
1332
+ }
1333
+
1334
+ function renderEndCalendar() {
1335
+ var grid = document.getElementById("sched-cal-grid");
1336
+ var titleEl = document.getElementById("sched-cal-title");
1337
+ if (!grid || !createEndCalMonth) return;
1338
+
1339
+ var year = createEndCalMonth.getFullYear();
1340
+ var month = createEndCalMonth.getMonth();
1341
+ var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
1342
+ titleEl.textContent = months[month] + " " + year;
1343
+
1344
+ var firstDay = new Date(year, month, 1).getDay();
1345
+ var daysInMonth = new Date(year, month + 1, 0).getDate();
1346
+ var prevDays = new Date(year, month, 0).getDate();
1347
+
1348
+ var today = new Date();
1349
+ today.setHours(0, 0, 0, 0);
1350
+
1351
+ grid.innerHTML = "";
1352
+
1353
+ // Previous month filler
1354
+ for (var p = firstDay - 1; p >= 0; p--) {
1355
+ var d = prevDays - p;
1356
+ var btn = document.createElement("button");
1357
+ btn.className = "sched-cal-day other-month";
1358
+ btn.textContent = d;
1359
+ btn.type = "button";
1360
+ var prevDate = new Date(year, month - 1, d);
1361
+ (function (dt) {
1362
+ btn.addEventListener("click", function (e) {
1363
+ e.stopPropagation();
1364
+ createEndDate = dt;
1365
+ updateEndDateLabel();
1366
+ renderEndCalendar();
1367
+ });
1368
+ })(prevDate);
1369
+ grid.appendChild(btn);
1370
+ }
1371
+
1372
+ // Current month
1373
+ for (var i = 1; i <= daysInMonth; i++) {
1374
+ var btn = document.createElement("button");
1375
+ btn.className = "sched-cal-day";
1376
+ btn.textContent = i;
1377
+ btn.type = "button";
1378
+ var cellDate = new Date(year, month, i);
1379
+ if (cellDate.getTime() === today.getTime()) btn.classList.add("today");
1380
+ if (createEndDate && cellDate.getFullYear() === createEndDate.getFullYear() && cellDate.getMonth() === createEndDate.getMonth() && cellDate.getDate() === createEndDate.getDate()) {
1381
+ btn.classList.add("selected");
1382
+ }
1383
+ (function (dt) {
1384
+ btn.addEventListener("click", function (e) {
1385
+ e.stopPropagation();
1386
+ createEndDate = dt;
1387
+ updateEndDateLabel();
1388
+ renderEndCalendar();
1389
+ });
1390
+ })(cellDate);
1391
+ grid.appendChild(btn);
1392
+ }
1393
+
1394
+ // Next month filler
1395
+ var totalCells = firstDay + daysInMonth;
1396
+ var remaining = (7 - (totalCells % 7)) % 7;
1397
+ for (var n = 1; n <= remaining; n++) {
1398
+ var btn = document.createElement("button");
1399
+ btn.className = "sched-cal-day other-month";
1400
+ btn.textContent = n;
1401
+ btn.type = "button";
1402
+ var nextDate = new Date(year, month + 1, n);
1403
+ (function (dt) {
1404
+ btn.addEventListener("click", function (e) {
1405
+ e.stopPropagation();
1406
+ createEndDate = dt;
1407
+ updateEndDateLabel();
1408
+ renderEndCalendar();
1409
+ });
1410
+ })(nextDate);
1411
+ grid.appendChild(btn);
1412
+ }
1413
+ }
1414
+
1415
+ function buildCustomCron(h, m) {
1416
+ var interval = parseInt(document.getElementById("sched-custom-interval").value, 10) || 1;
1417
+ var unit = document.getElementById("sched-custom-unit").value;
1418
+
1419
+ if (unit === "minute") {
1420
+ return interval === 1 ? "*/1 * * * *" : buildOffsetList(m, interval, 60) + " * * * *";
1421
+ }
1422
+ if (unit === "hour") {
1423
+ return interval === 1 ? m + " */1 * * *" : m + " " + buildOffsetList(h, interval, 24) + " * * *";
1424
+ }
1425
+ if (unit === "day") {
1426
+ if (interval === 1) return m + " " + h + " * * *";
1427
+ return m + " " + h + " */" + interval + " * *";
1428
+ }
1429
+
1430
+ if (unit === "week") {
1431
+ var days = [];
1432
+ var btns = document.querySelectorAll("#sched-custom-dow-row .sched-dow-btn.active");
1433
+ for (var i = 0; i < btns.length; i++) days.push(btns[i].dataset.dow);
1434
+ if (days.length === 0) days.push(String(createSelectedDate ? createSelectedDate.getDay() : 0));
1435
+ return m + " " + h + " * * " + days.sort().join(",");
1436
+ }
1437
+
1438
+ if (unit === "month") {
1439
+ var dom = createSelectedDate ? createSelectedDate.getDate() : 1;
1440
+ if (interval === 1) return m + " " + h + " " + dom + " * *";
1441
+ return m + " " + h + " " + dom + " */" + interval + " *";
1442
+ }
1443
+
1444
+ if (unit === "year") {
1445
+ var dom = createSelectedDate ? createSelectedDate.getDate() : 1;
1446
+ var month = createSelectedDate ? createSelectedDate.getMonth() + 1 : 1;
1447
+ return m + " " + h + " " + dom + " " + month + " *";
1448
+ }
1449
+
1450
+ return null;
1451
+ }
1452
+
1453
+ function submitCreateSchedule() {
1454
+ var name = document.getElementById("sched-create-title").value.trim();
1455
+ if (!name) { document.getElementById("sched-create-title").focus(); return; }
1456
+
1457
+ var taskId = document.getElementById("sched-create-task").value || null;
1458
+ if (!taskId) {
1459
+ var taskBtn = document.getElementById("sched-create-task-btn");
1460
+ if (taskBtn) taskBtn.classList.add("invalid");
1461
+ return;
1462
+ }
1463
+
1464
+ var ctx = configCtx.ctx;
1465
+ ctx.requireClayRalph(function () {
1466
+ var createPopover = configCtx.getCreatePopover();
1467
+ var descInput = document.getElementById("sched-create-desc");
1468
+ var description = descInput ? descInput.value.trim() : "";
1469
+ var datePicker = document.getElementById("sched-create-date-picker");
1470
+ var dateVal = datePicker ? datePicker.value : document.getElementById("sched-create-date").value;
1471
+ var timeVal = document.getElementById("sched-create-time").value || "09:00";
1472
+
1473
+ // Reject scheduling in the past
1474
+ if (dateVal && timeVal) {
1475
+ var dp = dateVal.split("-");
1476
+ var tp = timeVal.split(":");
1477
+ if (dp.length === 3 && tp.length >= 2) {
1478
+ var schedDate = new Date(
1479
+ parseInt(dp[0], 10), parseInt(dp[1], 10) - 1, parseInt(dp[2], 10),
1480
+ parseInt(tp[0], 10), parseInt(tp[1], 10), 0
1481
+ );
1482
+ if (schedDate < new Date()) {
1483
+ showToast("Cannot schedule a task in the past", "error");
1484
+ return;
1485
+ }
1486
+ }
1487
+ }
1488
+
1489
+ var cron = buildCreateCron();
1490
+
1491
+ // Build recurrence end info
1492
+ var recurrenceEnd = null;
1493
+ // Interval-only (no recurrence): limit to the scheduled date only
1494
+ if (cron && createRecurrence === "none" && createInterval !== "none" && dateVal) {
1495
+ recurrenceEnd = { type: "until", date: dateVal };
1496
+ }
1497
+ if (cron && createRecurrence === "custom" && createCustomConfirmed) {
1498
+ if (createEndType === "until" && createEndDate) {
1499
+ var ey = createEndDate.getFullYear();
1500
+ var em = String(createEndDate.getMonth() + 1).padStart(2, "0");
1501
+ var ed = String(createEndDate.getDate()).padStart(2, "0");
1502
+ recurrenceEnd = { type: "until", date: ey + "-" + em + "-" + ed };
1503
+ } else if (createEndType === "after" && createEndAfter > 0) {
1504
+ recurrenceEnd = { type: "after", count: createEndAfter };
1505
+ }
1506
+ }
1507
+
1508
+ // Build interval end info
1509
+ var intervalEnd = null;
1510
+ if (createInterval !== "none") {
1511
+ if (createIntervalEnd === "after" && createIntervalEndAfter > 0) {
1512
+ intervalEnd = { type: "after", count: createIntervalEndAfter };
1513
+ } else if (createIntervalEnd === "until" && createIntervalEndTime) {
1514
+ intervalEnd = { type: "until", time: createIntervalEndTime };
1515
+ }
1516
+ // "allday" = null (no limit)
1517
+ }
1518
+
1519
+ var skipRunningEl = document.getElementById("sched-skip-running");
1520
+ var skipIfRunning = skipRunningEl ? skipRunningEl.checked : true;
1521
+
1522
+ var reviewCheckSubmit = document.getElementById("sched-review-check");
1523
+ var maxIterations = 1;
1524
+ if (reviewCheckSubmit && reviewCheckSubmit.checked) {
1525
+ var iterInput = document.getElementById("sched-create-iterations");
1526
+ maxIterations = iterInput ? (parseInt(iterInput.value, 10) || 3) : 3;
1527
+ if (maxIterations < 2) maxIterations = 2;
1528
+ if (maxIterations > 100) maxIterations = 100;
1529
+ }
1530
+
1531
+ if (createEditingRecId) {
1532
+ configCtx.send({
1533
+ type: "loop_registry_update",
1534
+ id: createEditingRecId,
1535
+ data: {
1536
+ name: name,
1537
+ description: description,
1538
+ date: dateVal,
1539
+ time: timeVal,
1540
+ allDay: false,
1541
+ cron: cron,
1542
+ enabled: cron ? true : false,
1543
+ color: createColor,
1544
+ recurrenceEnd: recurrenceEnd,
1545
+ intervalEnd: intervalEnd,
1546
+ maxIterations: maxIterations,
1547
+ skipIfRunning: skipIfRunning,
1548
+ },
1549
+ });
1550
+ } else {
1551
+ configCtx.send({
1552
+ type: "schedule_create",
1553
+ data: {
1554
+ name: name,
1555
+ taskId: taskId,
1556
+ description: description,
1557
+ date: dateVal,
1558
+ time: timeVal,
1559
+ allDay: false,
1560
+ cron: cron,
1561
+ enabled: cron ? true : false,
1562
+ color: createColor,
1563
+ recurrenceEnd: recurrenceEnd,
1564
+ intervalEnd: intervalEnd,
1565
+ maxIterations: maxIterations,
1566
+ skipIfRunning: skipIfRunning,
1567
+ },
1568
+ });
1569
+ }
1570
+
1571
+ closeCreateModal();
1572
+ });
1573
+ }
1574
+
1575
+ // --- Cron parser (client-side) ---
1576
+
1577
+ export function parseCronSimple(expr) {
1578
+ if (!expr) return null;
1579
+ var fields = expr.trim().split(/\s+/);
1580
+ if (fields.length !== 5) return null;
1581
+ return {
1582
+ minutes: parseField(fields[0], 0, 59),
1583
+ hours: parseField(fields[1], 0, 23),
1584
+ daysOfMonth: parseField(fields[2], 1, 31),
1585
+ months: parseField(fields[3], 1, 12),
1586
+ daysOfWeek: parseField(fields[4], 0, 6),
1587
+ };
1588
+ }
1589
+
1590
+ function parseField(field, min, max) {
1591
+ var values = [];
1592
+ var parts = field.split(",");
1593
+ for (var i = 0; i < parts.length; i++) {
1594
+ var part = parts[i].trim();
1595
+ if (part.indexOf("/") !== -1) {
1596
+ var sp = part.split("/");
1597
+ var step = parseInt(sp[1], 10);
1598
+ var rMin = min, rMax = max;
1599
+ if (sp[0] !== "*") { var rp = sp[0].split("-"); rMin = parseInt(rp[0], 10); rMax = rp.length > 1 ? parseInt(rp[1], 10) : rMin; }
1600
+ for (var v = rMin; v <= rMax; v += step) values.push(v);
1601
+ } else if (part === "*") {
1602
+ for (var v = min; v <= max; v++) values.push(v);
1603
+ } else if (part.indexOf("-") !== -1) {
1604
+ var rp = part.split("-");
1605
+ for (var v = parseInt(rp[0], 10); v <= parseInt(rp[1], 10); v++) values.push(v);
1606
+ } else {
1607
+ values.push(parseInt(part, 10));
1608
+ }
1609
+ }
1610
+ return values;
1611
+ }