|
|
|
|
@@ -72,31 +72,6 @@ html, body {
|
|
|
|
|
outline-offset: 2px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Subtle safety plan button in banner — always visible */
|
|
|
|
|
#banner-safety-plan-btn {
|
|
|
|
|
background: none;
|
|
|
|
|
border: 1px solid #6e7681;
|
|
|
|
|
color: #8b949e;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
padding: 3px 8px;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
font-size: 0.75rem;
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
gap: 4px;
|
|
|
|
|
transition: background 0.2s, border-color 0.2s, color 0.2s;
|
|
|
|
|
flex-shrink: 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#banner-safety-plan-btn:hover,
|
|
|
|
|
#banner-safety-plan-btn:focus {
|
|
|
|
|
background: rgba(139, 148, 158, 0.15);
|
|
|
|
|
border-color: #8b949e;
|
|
|
|
|
color: #e6edf3;
|
|
|
|
|
outline: 2px solid #58a6ff;
|
|
|
|
|
outline-offset: 2px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#connection-status {
|
|
|
|
|
font-size: 0.7rem;
|
|
|
|
|
color: #6e7681;
|
|
|
|
|
@@ -525,6 +500,184 @@ html, body {
|
|
|
|
|
min-height: 60px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-status {
|
|
|
|
|
min-height: 20px;
|
|
|
|
|
margin: 4px 0 18px;
|
|
|
|
|
font-size: 0.85rem;
|
|
|
|
|
color: #8b949e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-status.success {
|
|
|
|
|
color: #3fb950;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-status.error {
|
|
|
|
|
color: #ff7b72;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-versioning {
|
|
|
|
|
margin-top: 24px;
|
|
|
|
|
padding-top: 20px;
|
|
|
|
|
border-top: 1px solid #30363d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-versioning-grid {
|
|
|
|
|
display: grid;
|
|
|
|
|
gap: 16px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-history-panel,
|
|
|
|
|
.safety-plan-diff-panel {
|
|
|
|
|
background: #0d1117;
|
|
|
|
|
border: 1px solid #30363d;
|
|
|
|
|
border-radius: 12px;
|
|
|
|
|
padding: 16px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-section-header h3 {
|
|
|
|
|
font-size: 0.95rem;
|
|
|
|
|
margin-bottom: 4px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-section-header p {
|
|
|
|
|
font-size: 0.8rem;
|
|
|
|
|
color: #8b949e;
|
|
|
|
|
margin-bottom: 12px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-history {
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
gap: 10px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-history-item {
|
|
|
|
|
border: 1px solid #30363d;
|
|
|
|
|
border-radius: 10px;
|
|
|
|
|
padding: 12px;
|
|
|
|
|
background: #161b22;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-history-item.active {
|
|
|
|
|
border-color: #58a6ff;
|
|
|
|
|
box-shadow: 0 0 0 1px rgba(88, 166, 255, 0.35);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-history-meta {
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
gap: 12px;
|
|
|
|
|
align-items: baseline;
|
|
|
|
|
margin-bottom: 8px;
|
|
|
|
|
flex-wrap: wrap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-history-title {
|
|
|
|
|
font-size: 0.88rem;
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
color: #e6edf3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-history-note {
|
|
|
|
|
font-size: 0.78rem;
|
|
|
|
|
color: #8b949e;
|
|
|
|
|
margin-bottom: 10px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-history-actions {
|
|
|
|
|
display: flex;
|
|
|
|
|
gap: 8px;
|
|
|
|
|
flex-wrap: wrap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-history-button,
|
|
|
|
|
.safety-plan-restore-button {
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
border: 1px solid #30363d;
|
|
|
|
|
background: transparent;
|
|
|
|
|
color: #e6edf3;
|
|
|
|
|
padding: 8px 12px;
|
|
|
|
|
font-size: 0.82rem;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-history-button:hover,
|
|
|
|
|
.safety-plan-restore-button:hover,
|
|
|
|
|
.safety-plan-history-button:focus,
|
|
|
|
|
.safety-plan-restore-button:focus {
|
|
|
|
|
border-color: #58a6ff;
|
|
|
|
|
color: #58a6ff;
|
|
|
|
|
outline: none;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-restore-button {
|
|
|
|
|
background: rgba(35, 134, 54, 0.14);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-diff {
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
gap: 12px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-diff-meta {
|
|
|
|
|
font-size: 0.78rem;
|
|
|
|
|
color: #8b949e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-diff-field {
|
|
|
|
|
border-top: 1px solid #21262d;
|
|
|
|
|
padding-top: 12px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-diff-field:first-child {
|
|
|
|
|
border-top: none;
|
|
|
|
|
padding-top: 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-diff-field h4 {
|
|
|
|
|
font-size: 0.84rem;
|
|
|
|
|
margin-bottom: 8px;
|
|
|
|
|
color: #c9d1d9;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-diff-block {
|
|
|
|
|
white-space: pre-wrap;
|
|
|
|
|
line-height: 1.5;
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
padding: 10px;
|
|
|
|
|
font-size: 0.88rem;
|
|
|
|
|
margin-bottom: 8px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.diff-unchanged {
|
|
|
|
|
background: #161b22;
|
|
|
|
|
color: #c9d1d9;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.diff-added {
|
|
|
|
|
background: rgba(46, 160, 67, 0.16);
|
|
|
|
|
border-left: 3px solid #2ea043;
|
|
|
|
|
color: #d2f4d3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.diff-removed {
|
|
|
|
|
background: rgba(248, 81, 73, 0.16);
|
|
|
|
|
border-left: 3px solid #f85149;
|
|
|
|
|
color: #ffd8d3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.safety-plan-empty {
|
|
|
|
|
color: #8b949e;
|
|
|
|
|
font-size: 0.85rem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@media (min-width: 840px) {
|
|
|
|
|
.safety-plan-versioning-grid {
|
|
|
|
|
grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.modal-footer {
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: flex-end;
|
|
|
|
|
@@ -650,10 +803,6 @@ html, body {
|
|
|
|
|
<a href="tel:988" aria-label="Call 988 Suicide and Crisis Lifeline">
|
|
|
|
|
988 Suicide & Crisis Lifeline — Call or text 988
|
|
|
|
|
</a>
|
|
|
|
|
<button id="banner-safety-plan-btn" aria-label="Open my safety plan" title="My Safety Plan">
|
|
|
|
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/></svg>
|
|
|
|
|
<span class="sr-only">My Safety Plan</span>
|
|
|
|
|
</button>
|
|
|
|
|
<div id="connection-status" aria-hidden="true">
|
|
|
|
|
<span class="status-dot"></span>
|
|
|
|
|
<span id="status-text">Online</span>
|
|
|
|
|
@@ -709,7 +858,7 @@ html, body {
|
|
|
|
|
|
|
|
|
|
<!-- Footer -->
|
|
|
|
|
<footer id="footer">
|
|
|
|
|
<a href="/about" aria-label="About The Door">about</a>
|
|
|
|
|
<a href="/about.html" aria-label="About The Door">about</a>
|
|
|
|
|
<button id="safety-plan-btn" aria-label="Open My Safety Plan">my safety plan</button>
|
|
|
|
|
<button id="clear-chat-btn" aria-label="Clear chat history">clear chat</button>
|
|
|
|
|
</footer>
|
|
|
|
|
@@ -765,6 +914,28 @@ html, body {
|
|
|
|
|
<label for="sp-environment">5. Making my environment safe</label>
|
|
|
|
|
<textarea id="sp-environment" placeholder="e.g., Giving my car keys to a friend, locking away meds..."></textarea>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div id="safety-plan-status" class="safety-plan-status" role="status" aria-live="polite"></div>
|
|
|
|
|
|
|
|
|
|
<section class="safety-plan-versioning" aria-labelledby="safety-plan-history-title">
|
|
|
|
|
<div class="safety-plan-versioning-grid">
|
|
|
|
|
<div class="safety-plan-history-panel">
|
|
|
|
|
<div class="safety-plan-section-header">
|
|
|
|
|
<h3 id="safety-plan-history-title">Version History</h3>
|
|
|
|
|
<p>Each save stays on this device so you can review changes and restore an earlier plan.</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div id="safety-plan-history" class="safety-plan-history"></div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="safety-plan-diff-panel">
|
|
|
|
|
<div class="safety-plan-section-header">
|
|
|
|
|
<h3>Diff View</h3>
|
|
|
|
|
<p>Compare the selected version against the version immediately before it.</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div id="safety-plan-diff" class="safety-plan-diff"></div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="modal-footer">
|
|
|
|
|
<button class="btn btn-secondary" id="cancel-safety-plan">Cancel</button>
|
|
|
|
|
@@ -837,24 +1008,38 @@ Sovereignty and service always.`;
|
|
|
|
|
var crisisPanel = document.getElementById('crisis-panel');
|
|
|
|
|
var crisisOverlay = document.getElementById('crisis-overlay');
|
|
|
|
|
var overlayDismissBtn = document.getElementById('overlay-dismiss-btn');
|
|
|
|
|
var overlayCallLink = document.querySelector('.overlay-call');
|
|
|
|
|
var statusDot = document.querySelector('.status-dot');
|
|
|
|
|
var statusText = document.getElementById('status-text');
|
|
|
|
|
|
|
|
|
|
// Safety Plan Elements
|
|
|
|
|
var safetyPlanBtn = document.getElementById('safety-plan-btn');
|
|
|
|
|
var crisisSafetyPlanBtn = document.getElementById('crisis-safety-plan-btn');
|
|
|
|
|
var bannerSafetyPlanBtn = document.getElementById('banner-safety-plan-btn');
|
|
|
|
|
var safetyPlanModal = document.getElementById('safety-plan-modal');
|
|
|
|
|
var closeSafetyPlan = document.getElementById('close-safety-plan');
|
|
|
|
|
var cancelSafetyPlan = document.getElementById('cancel-safety-plan');
|
|
|
|
|
var saveSafetyPlan = document.getElementById('save-safety-plan');
|
|
|
|
|
var clearChatBtn = document.getElementById('clear-chat-btn');
|
|
|
|
|
var safetyPlanHistory = document.getElementById('safety-plan-history');
|
|
|
|
|
var safetyPlanDiff = document.getElementById('safety-plan-diff');
|
|
|
|
|
var safetyPlanStatus = document.getElementById('safety-plan-status');
|
|
|
|
|
var safetyPlanFields = {
|
|
|
|
|
warningSigns: document.getElementById('sp-warning-signs'),
|
|
|
|
|
coping: document.getElementById('sp-coping'),
|
|
|
|
|
distraction: document.getElementById('sp-distraction'),
|
|
|
|
|
help: document.getElementById('sp-help'),
|
|
|
|
|
environment: document.getElementById('sp-environment')
|
|
|
|
|
};
|
|
|
|
|
var SAFETY_PLAN_STORAGE_KEY = 'timmy_safety_plan';
|
|
|
|
|
var SAFETY_PLAN_VERSIONS_KEY = 'timmy_safety_plan_versions';
|
|
|
|
|
var MAX_SAFETY_PLAN_VERSIONS = 20;
|
|
|
|
|
|
|
|
|
|
// ===== STATE =====
|
|
|
|
|
var messages = [];
|
|
|
|
|
var isStreaming = false;
|
|
|
|
|
var overlayTimer = null;
|
|
|
|
|
var crisisPanelShown = false;
|
|
|
|
|
var selectedSafetyPlanVersionId = null;
|
|
|
|
|
|
|
|
|
|
// ===== SERVICE WORKER =====
|
|
|
|
|
if ('serviceWorker' in navigator) {
|
|
|
|
|
@@ -1080,7 +1265,8 @@ Sovereignty and service always.`;
|
|
|
|
|
}
|
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
|
|
overlayDismissBtn.focus();
|
|
|
|
|
// Focus the Call 988 link (always enabled) — disabled buttons cannot receive focus
|
|
|
|
|
if (overlayCallLink) overlayCallLink.focus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register focus trap on document (always listening, gated by class check)
|
|
|
|
|
@@ -1199,18 +1385,347 @@ Sovereignty and service always.`;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ===== SAFETY PLAN LOGIC =====
|
|
|
|
|
function loadSafetyPlan() {
|
|
|
|
|
function emptySafetyPlan() {
|
|
|
|
|
return {
|
|
|
|
|
warningSigns: '',
|
|
|
|
|
coping: '',
|
|
|
|
|
distraction: '',
|
|
|
|
|
help: '',
|
|
|
|
|
environment: ''
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function cloneSafetyPlan(plan) {
|
|
|
|
|
var normalized = emptySafetyPlan();
|
|
|
|
|
var source = plan || {};
|
|
|
|
|
Object.keys(normalized).forEach(function(key) {
|
|
|
|
|
normalized[key] = typeof source[key] === 'string' ? source[key] : '';
|
|
|
|
|
});
|
|
|
|
|
return normalized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function applySafetyPlan(plan) {
|
|
|
|
|
var nextPlan = cloneSafetyPlan(plan);
|
|
|
|
|
Object.keys(safetyPlanFields).forEach(function(key) {
|
|
|
|
|
safetyPlanFields[key].value = nextPlan[key];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSafetyPlanFormData() {
|
|
|
|
|
return {
|
|
|
|
|
warningSigns: safetyPlanFields.warningSigns.value,
|
|
|
|
|
coping: safetyPlanFields.coping.value,
|
|
|
|
|
distraction: safetyPlanFields.distraction.value,
|
|
|
|
|
help: safetyPlanFields.help.value,
|
|
|
|
|
environment: safetyPlanFields.environment.value
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function escapeHtml(value) {
|
|
|
|
|
return String(value || '')
|
|
|
|
|
.replace(/&/g, '&')
|
|
|
|
|
.replace(/</g, '<')
|
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
|
.replace(/'/g, ''');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setSafetyPlanStatus(message, tone) {
|
|
|
|
|
if (!safetyPlanStatus) return;
|
|
|
|
|
safetyPlanStatus.textContent = message || '';
|
|
|
|
|
safetyPlanStatus.className = 'safety-plan-status' + (tone ? ' ' + tone : '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSafetyPlanVersions() {
|
|
|
|
|
try {
|
|
|
|
|
var saved = localStorage.getItem('timmy_safety_plan');
|
|
|
|
|
if (saved) {
|
|
|
|
|
var plan = JSON.parse(saved);
|
|
|
|
|
document.getElementById('sp-warning-signs').value = plan.warningSigns || '';
|
|
|
|
|
document.getElementById('sp-coping').value = plan.coping || '';
|
|
|
|
|
document.getElementById('sp-distraction').value = plan.distraction || '';
|
|
|
|
|
document.getElementById('sp-help').value = plan.help || '';
|
|
|
|
|
document.getElementById('sp-environment').value = plan.environment || '';
|
|
|
|
|
var saved = localStorage.getItem(SAFETY_PLAN_VERSIONS_KEY);
|
|
|
|
|
if (!saved) return [];
|
|
|
|
|
var parsed = JSON.parse(saved);
|
|
|
|
|
if (!Array.isArray(parsed)) return [];
|
|
|
|
|
return parsed.filter(function(version) {
|
|
|
|
|
return version && version.id && version.plan;
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setSafetyPlanVersions(versions) {
|
|
|
|
|
localStorage.setItem(
|
|
|
|
|
SAFETY_PLAN_VERSIONS_KEY,
|
|
|
|
|
JSON.stringify((versions || []).slice(0, MAX_SAFETY_PLAN_VERSIONS))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildSafetyPlanVersion(plan, meta) {
|
|
|
|
|
return {
|
|
|
|
|
id: 'spv_' + Date.now() + '_' + Math.random().toString(36).slice(2, 8),
|
|
|
|
|
savedAt: new Date().toISOString(),
|
|
|
|
|
source: (meta && meta.source) || 'save',
|
|
|
|
|
restoredFrom: meta && meta.restoredFrom ? meta.restoredFrom : null,
|
|
|
|
|
plan: cloneSafetyPlan(plan)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ensureSafetyPlanVersionHistory() {
|
|
|
|
|
var versions = getSafetyPlanVersions();
|
|
|
|
|
if (versions.length) {
|
|
|
|
|
return versions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
var saved = localStorage.getItem(SAFETY_PLAN_STORAGE_KEY);
|
|
|
|
|
if (!saved) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
var parsed = JSON.parse(saved);
|
|
|
|
|
var migrated = [buildSafetyPlanVersion(parsed, { source: 'legacy' })];
|
|
|
|
|
setSafetyPlanVersions(migrated);
|
|
|
|
|
return migrated;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatSafetyPlanTimestamp(iso) {
|
|
|
|
|
var date = new Date(iso || '');
|
|
|
|
|
if (isNaN(date.getTime())) {
|
|
|
|
|
return 'Saved just now';
|
|
|
|
|
}
|
|
|
|
|
return date.toLocaleString([], {
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
month: 'short',
|
|
|
|
|
day: 'numeric',
|
|
|
|
|
hour: 'numeric',
|
|
|
|
|
minute: '2-digit'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSafetyPlanVersionById(versionId) {
|
|
|
|
|
var versions = getSafetyPlanVersions();
|
|
|
|
|
for (var i = 0; i < versions.length; i++) {
|
|
|
|
|
if (versions[i].id === versionId) {
|
|
|
|
|
return { version: versions[i], index: i, versions: versions };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function calculateSafetyPlanDiff(previousText, currentText) {
|
|
|
|
|
var previous = String(previousText || '');
|
|
|
|
|
var current = String(currentText || '');
|
|
|
|
|
var start = 0;
|
|
|
|
|
while (start < previous.length && start < current.length && previous.charAt(start) === current.charAt(start)) {
|
|
|
|
|
start += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var previousEnd = previous.length - 1;
|
|
|
|
|
var currentEnd = current.length - 1;
|
|
|
|
|
while (previousEnd >= start && currentEnd >= start && previous.charAt(previousEnd) === current.charAt(currentEnd)) {
|
|
|
|
|
previousEnd -= 1;
|
|
|
|
|
currentEnd -= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
before: current.slice(0, start),
|
|
|
|
|
removed: previous.slice(start, previousEnd + 1),
|
|
|
|
|
added: current.slice(start, currentEnd + 1),
|
|
|
|
|
after: current.slice(currentEnd + 1)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderDiffSegmentHtml(previousText, currentText) {
|
|
|
|
|
var previous = String(previousText || '');
|
|
|
|
|
var current = String(currentText || '');
|
|
|
|
|
|
|
|
|
|
if (!previous && !current) {
|
|
|
|
|
return '<p class="safety-plan-empty">No content saved for this section yet.</p>';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (previous === current) {
|
|
|
|
|
return '<div class="safety-plan-diff-block diff-unchanged">' + escapeHtml(current || 'No changes in this version.') + '</div>';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var diff = calculateSafetyPlanDiff(previous, current);
|
|
|
|
|
var blocks = [];
|
|
|
|
|
if (diff.before) {
|
|
|
|
|
blocks.push('<div class="safety-plan-diff-block diff-unchanged">' + escapeHtml(diff.before) + '</div>');
|
|
|
|
|
}
|
|
|
|
|
if (diff.removed) {
|
|
|
|
|
blocks.push('<div class="safety-plan-diff-block diff-removed">Removed<br>' + escapeHtml(diff.removed) + '</div>');
|
|
|
|
|
}
|
|
|
|
|
if (diff.added) {
|
|
|
|
|
blocks.push('<div class="safety-plan-diff-block diff-added">Added<br>' + escapeHtml(diff.added) + '</div>');
|
|
|
|
|
}
|
|
|
|
|
if (diff.after) {
|
|
|
|
|
blocks.push('<div class="safety-plan-diff-block diff-unchanged">' + escapeHtml(diff.after) + '</div>');
|
|
|
|
|
}
|
|
|
|
|
return blocks.join('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderSafetyPlanDiff(versionId) {
|
|
|
|
|
if (!safetyPlanDiff) return;
|
|
|
|
|
|
|
|
|
|
var versions = getSafetyPlanVersions();
|
|
|
|
|
if (!versions.length) {
|
|
|
|
|
safetyPlanDiff.innerHTML = '<p class="safety-plan-empty">Save your plan to start tracking changes.</p>';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var selected = getSafetyPlanVersionById(versionId || selectedSafetyPlanVersionId || versions[0].id);
|
|
|
|
|
if (!selected) {
|
|
|
|
|
selected = { version: versions[0], index: 0, versions: versions };
|
|
|
|
|
selectedSafetyPlanVersionId = versions[0].id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var baseline = selected.versions[selected.index + 1]
|
|
|
|
|
? cloneSafetyPlan(selected.versions[selected.index + 1].plan)
|
|
|
|
|
: emptySafetyPlan();
|
|
|
|
|
var currentPlan = cloneSafetyPlan(selected.version.plan);
|
|
|
|
|
var baselineLabel = selected.versions[selected.index + 1]
|
|
|
|
|
? formatSafetyPlanTimestamp(selected.versions[selected.index + 1].savedAt)
|
|
|
|
|
: 'an empty plan';
|
|
|
|
|
|
|
|
|
|
var fields = [
|
|
|
|
|
['Warning signs', 'warningSigns'],
|
|
|
|
|
['Internal coping strategies', 'coping'],
|
|
|
|
|
['People/Places for distraction', 'distraction'],
|
|
|
|
|
['People I can ask for help', 'help'],
|
|
|
|
|
['Making my environment safe', 'environment']
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
var html = [
|
|
|
|
|
'<div class="safety-plan-diff-meta">Comparing ' +
|
|
|
|
|
escapeHtml(formatSafetyPlanTimestamp(selected.version.savedAt)) +
|
|
|
|
|
' against ' + escapeHtml(baselineLabel) + '.</div>'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
fields.forEach(function(field) {
|
|
|
|
|
html.push(
|
|
|
|
|
'<div class="safety-plan-diff-field">' +
|
|
|
|
|
'<h4>' + escapeHtml(field[0]) + '</h4>' +
|
|
|
|
|
renderDiffSegmentHtml(baseline[field[1]], currentPlan[field[1]]) +
|
|
|
|
|
'</div>'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
safetyPlanDiff.innerHTML = html.join('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderSafetyPlanVersionHistory() {
|
|
|
|
|
if (!safetyPlanHistory) return;
|
|
|
|
|
|
|
|
|
|
var versions = getSafetyPlanVersions();
|
|
|
|
|
if (!versions.length) {
|
|
|
|
|
safetyPlanHistory.innerHTML = '<p class="safety-plan-empty">No saved versions yet. Each save creates a new local version.</p>';
|
|
|
|
|
renderSafetyPlanDiff(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!selectedSafetyPlanVersionId || !getSafetyPlanVersionById(selectedSafetyPlanVersionId)) {
|
|
|
|
|
selectedSafetyPlanVersionId = versions[0].id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var html = [];
|
|
|
|
|
versions.forEach(function(version, index) {
|
|
|
|
|
var note = 'Saved locally';
|
|
|
|
|
if (version.source === 'restore') {
|
|
|
|
|
note = 'Restored from an earlier version';
|
|
|
|
|
} else if (version.source === 'legacy') {
|
|
|
|
|
note = 'Imported from your previous saved plan';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
html.push(
|
|
|
|
|
'<div class="safety-plan-history-item' + (selectedSafetyPlanVersionId === version.id ? ' active' : '') + '">' +
|
|
|
|
|
'<div class="safety-plan-history-meta">' +
|
|
|
|
|
'<span class="safety-plan-history-title">' + escapeHtml(index === 0 ? 'Current version' : 'Version ' + (versions.length - index)) + '</span>' +
|
|
|
|
|
'<span class="safety-plan-empty">' + escapeHtml(formatSafetyPlanTimestamp(version.savedAt)) + '</span>' +
|
|
|
|
|
'</div>' +
|
|
|
|
|
'<div class="safety-plan-history-note">' + escapeHtml(note) + '</div>' +
|
|
|
|
|
'<div class="safety-plan-history-actions">' +
|
|
|
|
|
'<button type="button" class="safety-plan-history-button" data-version-id="' + escapeHtml(version.id) + '">View diff</button>' +
|
|
|
|
|
'<button type="button" class="safety-plan-restore-button" data-restore-version-id="' + escapeHtml(version.id) + '">Restore this version</button>' +
|
|
|
|
|
'</div>' +
|
|
|
|
|
'</div>'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
safetyPlanHistory.innerHTML = html.join('');
|
|
|
|
|
renderSafetyPlanDiff(selectedSafetyPlanVersionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function saveSafetyPlanVersion(plan, meta) {
|
|
|
|
|
var snapshot = buildSafetyPlanVersion(plan, meta);
|
|
|
|
|
var versions = getSafetyPlanVersions();
|
|
|
|
|
versions.unshift(snapshot);
|
|
|
|
|
setSafetyPlanVersions(versions);
|
|
|
|
|
localStorage.setItem(SAFETY_PLAN_STORAGE_KEY, JSON.stringify(snapshot.plan));
|
|
|
|
|
selectedSafetyPlanVersionId = snapshot.id;
|
|
|
|
|
renderSafetyPlanVersionHistory();
|
|
|
|
|
return snapshot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function restoreSafetyPlanVersion(versionId) {
|
|
|
|
|
var selected = getSafetyPlanVersionById(versionId);
|
|
|
|
|
if (!selected) {
|
|
|
|
|
setSafetyPlanStatus('That version could not be restored.', 'error');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
applySafetyPlan(selected.version.plan);
|
|
|
|
|
var restored = saveSafetyPlanVersion(selected.version.plan, {
|
|
|
|
|
source: 'restore',
|
|
|
|
|
restoredFrom: selected.version.id
|
|
|
|
|
});
|
|
|
|
|
setSafetyPlanStatus(
|
|
|
|
|
'Restored version from ' + formatSafetyPlanTimestamp(selected.version.savedAt) + ' as the current plan.',
|
|
|
|
|
'success'
|
|
|
|
|
);
|
|
|
|
|
return restored;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function loadSafetyPlan() {
|
|
|
|
|
var versions = ensureSafetyPlanVersionHistory();
|
|
|
|
|
var latestPlan = versions.length ? versions[0].plan : null;
|
|
|
|
|
|
|
|
|
|
if (!latestPlan) {
|
|
|
|
|
try {
|
|
|
|
|
var saved = localStorage.getItem(SAFETY_PLAN_STORAGE_KEY);
|
|
|
|
|
latestPlan = saved ? JSON.parse(saved) : null;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
latestPlan = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
applySafetyPlan(latestPlan || emptySafetyPlan());
|
|
|
|
|
renderSafetyPlanVersionHistory();
|
|
|
|
|
if (versions.length) {
|
|
|
|
|
setSafetyPlanStatus('Version history stays on this device only.', '');
|
|
|
|
|
} else {
|
|
|
|
|
setSafetyPlanStatus('Every save creates a local version you can diff and restore.', '');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openSafetyPlanModal(triggerEl) {
|
|
|
|
|
loadSafetyPlan();
|
|
|
|
|
safetyPlanModal.classList.add('active');
|
|
|
|
|
_activateSafetyPlanFocusTrap(triggerEl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (safetyPlanHistory) {
|
|
|
|
|
safetyPlanHistory.addEventListener('click', function(event) {
|
|
|
|
|
var restoreButton = event.target.closest('[data-restore-version-id]');
|
|
|
|
|
if (restoreButton) {
|
|
|
|
|
restoreSafetyPlanVersion(restoreButton.getAttribute('data-restore-version-id'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var diffButton = event.target.closest('[data-version-id]');
|
|
|
|
|
if (diffButton) {
|
|
|
|
|
selectedSafetyPlanVersionId = diffButton.getAttribute('data-version-id');
|
|
|
|
|
renderSafetyPlanVersionHistory();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closeSafetyPlan.addEventListener('click', function() {
|
|
|
|
|
@@ -1224,41 +1739,23 @@ Sovereignty and service always.`;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
saveSafetyPlan.addEventListener('click', function() {
|
|
|
|
|
var plan = {
|
|
|
|
|
warningSigns: document.getElementById('sp-warning-signs').value,
|
|
|
|
|
coping: document.getElementById('sp-coping').value,
|
|
|
|
|
distraction: document.getElementById('sp-distraction').value,
|
|
|
|
|
help: document.getElementById('sp-help').value,
|
|
|
|
|
environment: document.getElementById('sp-environment').value
|
|
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
localStorage.setItem('timmy_safety_plan', JSON.stringify(plan));
|
|
|
|
|
safetyPlanModal.classList.remove('active');
|
|
|
|
|
_restoreSafetyPlanFocus();
|
|
|
|
|
alert('Safety plan saved locally.');
|
|
|
|
|
var snapshot = saveSafetyPlanVersion(getSafetyPlanFormData(), { source: 'save' });
|
|
|
|
|
setSafetyPlanStatus('Saved locally as a new version at ' + formatSafetyPlanTimestamp(snapshot.savedAt) + '.', 'success');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
alert('Error saving plan.');
|
|
|
|
|
setSafetyPlanStatus('Error saving plan.', 'error');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ===== SAFETY PLAN FOCUS TRAP (fix #65) =====
|
|
|
|
|
// Focusable elements inside the modal, in tab order
|
|
|
|
|
var _spFocusableIds = [
|
|
|
|
|
'close-safety-plan',
|
|
|
|
|
'sp-warning-signs',
|
|
|
|
|
'sp-coping',
|
|
|
|
|
'sp-distraction',
|
|
|
|
|
'sp-help',
|
|
|
|
|
'sp-environment',
|
|
|
|
|
'cancel-safety-plan',
|
|
|
|
|
'save-safety-plan'
|
|
|
|
|
];
|
|
|
|
|
var _spTriggerEl = null; // element that opened the modal
|
|
|
|
|
|
|
|
|
|
function _getSpFocusableEls() {
|
|
|
|
|
return _spFocusableIds
|
|
|
|
|
.map(function(id) { return document.getElementById(id); })
|
|
|
|
|
.filter(function(el) { return el && !el.disabled; });
|
|
|
|
|
return Array.prototype.slice.call(
|
|
|
|
|
safetyPlanModal.querySelectorAll('button:not([disabled]), textarea:not([disabled]), a[href], input:not([disabled]), select:not([disabled]), [tabindex]:not([tabindex="-1"])')
|
|
|
|
|
).filter(function(el) {
|
|
|
|
|
return !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _trapSafetyPlanFocus(e) {
|
|
|
|
|
@@ -1315,26 +1812,13 @@ Sovereignty and service always.`;
|
|
|
|
|
|
|
|
|
|
// Wire open buttons to activate focus trap
|
|
|
|
|
safetyPlanBtn.addEventListener('click', function() {
|
|
|
|
|
loadSafetyPlan();
|
|
|
|
|
safetyPlanModal.classList.add('active');
|
|
|
|
|
_activateSafetyPlanFocusTrap(safetyPlanBtn);
|
|
|
|
|
openSafetyPlanModal(safetyPlanBtn);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Crisis panel safety plan button (if crisis panel is visible)
|
|
|
|
|
if (crisisSafetyPlanBtn) {
|
|
|
|
|
crisisSafetyPlanBtn.addEventListener('click', function() {
|
|
|
|
|
loadSafetyPlan();
|
|
|
|
|
safetyPlanModal.classList.add('active');
|
|
|
|
|
_activateSafetyPlanFocusTrap(crisisSafetyPlanBtn);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Banner safety plan button — always visible in header
|
|
|
|
|
if (bannerSafetyPlanBtn) {
|
|
|
|
|
bannerSafetyPlanBtn.addEventListener('click', function() {
|
|
|
|
|
loadSafetyPlan();
|
|
|
|
|
safetyPlanModal.classList.add('active');
|
|
|
|
|
_activateSafetyPlanFocusTrap(bannerSafetyPlanBtn);
|
|
|
|
|
openSafetyPlanModal(crisisSafetyPlanBtn);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -1481,9 +1965,7 @@ Sovereignty and service always.`;
|
|
|
|
|
// Check for URL params (e.g., ?safetyplan=true for PWA shortcut)
|
|
|
|
|
var urlParams = new URLSearchParams(window.location.search);
|
|
|
|
|
if (urlParams.get('safetyplan') === 'true') {
|
|
|
|
|
loadSafetyPlan();
|
|
|
|
|
safetyPlanModal.classList.add('active');
|
|
|
|
|
_activateSafetyPlanFocusTrap(safetyPlanBtn);
|
|
|
|
|
openSafetyPlanModal(safetyPlanBtn);
|
|
|
|
|
// Clean up URL
|
|
|
|
|
window.history.replaceState({}, document.title, window.location.pathname);
|
|
|
|
|
}
|
|
|
|
|
|