-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubject-modals.js
More file actions
225 lines (205 loc) · 8.56 KB
/
Copy pathsubject-modals.js
File metadata and controls
225 lines (205 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// ══════════════════════════════════════════════════
// ADD / EDIT MODALS
// ══════════════════════════════════════════════════
let modalCtx = {};
function openAddSubjectModal(stage, semId) {
modalCtx = { mode:'add', stage, semId };
const title = stage === 'sslc' ? 'Add SSLC Subject' : `Add Subject — ${stage.charAt(0).toUpperCase()+stage.slice(1)}`;
openModal(title, buildSubjectForm(stage, semId, null));
setModalFooter([
{ label:'Cancel', cls:'btn-ghost', fn:'closeModal()' },
{ label:'Add Subject', cls:'btn-solid', fn:'saveSubject()' }
]);
}
function openEditSubjectModal(stage, semId, id) {
const subj = state[stage].find(s=>s.id===id);
if (!subj) return;
modalCtx = { mode:'edit', stage, semId, id };
openModal('Edit Subject', buildSubjectForm(stage, semId, subj));
setModalFooter([
{ label:'Cancel', cls:'btn-ghost', fn:'closeModal()' },
{ label:'Save Changes', cls:'btn-solid', fn:'saveSubject()' }
]);
}
function buildSubjectForm(stage, semId, subj) {
const sems = state.semesters[stage] || [];
let semSelect = '';
if (stage !== 'sslc') {
const opts = sems.map(s => `<option value="${s.id}" ${subj?.sem===s.id||semId===s.id?'selected':''}>${s.label}</option>`).join('');
semSelect = `
<div class="form-row single">
<div class="form-field">
<label class="form-label">Semester</label>
<select class="form-input" id="f-sem">${opts}</select>
</div>
</div>`;
}
let examTypeField = '';
if (stage === 'engineering') {
const types = ['regular','arrear','lab'];
examTypeField = `
<div class="form-row single" style="margin-bottom:0.75rem;">
<div class="form-field">
<label class="form-label">Exam Type</label>
<select class="form-input" id="f-examtype">
${types.map(t=>`<option value="${t}" ${subj?.examType===t?'selected':''}>${t.charAt(0).toUpperCase()+t.slice(1)}</option>`).join('')}
</select>
</div>
</div>`;
}
if (stage === 'sslc') {
const comps = subj?.components || [];
const extS = subj?.external?.scored ?? '';
const extM = subj?.external?.max ?? '';
const compRows = comps.map((c,i) => buildCompRow(i, c.name, c.scored, c.max)).join('');
return `
<div class="form-row single">
<div class="form-field">
<label class="form-label">Subject Name</label>
<input class="form-input" id="f-name" placeholder="e.g. Mathematics" value="${escHtml(subj?.name||'')}">
</div>
</div>
<div class="components-section">
<div class="components-title">
<span>Internal Components</span>
<button class="btn btn-ghost btn-sm" type="button" onclick="addCompRow()">+ Add Component</button>
</div>
<div id="comp-list">${compRows}</div>
</div>
<div class="components-section">
<div class="components-title"><span>External / End Exam</span></div>
<div class="form-row">
<div class="form-field">
<label class="form-label">Scored</label>
<input class="form-input" type="number" id="f-ext-scored" placeholder="0" value="${extS}">
</div>
<div class="form-field">
<label class="form-label">Max Marks</label>
<input class="form-input" type="number" id="f-ext-max" placeholder="100" value="${extM}">
</div>
</div>
</div>`;
}
// Diploma / Engineering
const comps = subj?.components || [];
const extS = subj?.external?.scored ?? '';
const extM = subj?.external?.max ?? 60;
const compRows = comps.map((c,i) => buildCompRow(i, c.name, c.scored, c.max)).join('');
return `
${semSelect}
<div class="form-row single">
<div class="form-field">
<label class="form-label">Subject Name</label>
<input class="form-input" id="f-name" placeholder="e.g. Engineering Mathematics" value="${escHtml(subj?.name||'')}">
</div>
</div>
${examTypeField}
<div class="components-section">
<div class="components-title">
<span>Internal Components</span>
<button class="btn btn-ghost btn-sm" type="button" onclick="addCompRow()">+ Add Component</button>
</div>
<div id="comp-list">${compRows}</div>
</div>
<div class="components-section">
<div class="components-title"><span>External / End-Sem Exam</span></div>
<div class="form-row">
<div class="form-field">
<label class="form-label">Scored</label>
<input class="form-input" type="number" id="f-ext-scored" placeholder="0" value="${extS}">
</div>
<div class="form-field">
<label class="form-label">Max Marks</label>
<input class="form-input" type="number" id="f-ext-max" placeholder="60" value="${extM}">
</div>
</div>
</div>`;
}
let compIdx = 0;
function buildCompRow(i, name='', scored='', max='') {
compIdx = Math.max(compIdx, i+1);
return `<div class="component-row" id="comp-row-${i}">
<input class="form-input" placeholder="Component name (e.g. IA1)" value="${escHtml(name)}" data-comp-name="${i}">
<input class="form-input" type="number" placeholder="0/${max||20}" value="${scored}" data-comp-scored="${i}" style="text-align:center;">
<input class="form-input" type="number" placeholder="Max" value="${max}" data-comp-max="${i}" style="text-align:center;">
<button class="remove-comp" onclick="removeCompRow(${i})">✕</button>
</div>`;
}
function addCompRow() {
const list = document.getElementById('comp-list');
const div = document.createElement('div');
div.innerHTML = buildCompRow(compIdx++);
list.appendChild(div.firstElementChild);
}
function removeCompRow(i) {
const el = document.getElementById('comp-row-' + i);
if (el) el.remove();
}
async function saveSubject() {
const { mode, stage, semId, id } = modalCtx;
const name = document.getElementById('f-name')?.value?.trim();
if (!name) { toast('Subject name is required'); return; }
if (stage === 'sslc') {
// Gather components
const compListEl = document.getElementById('comp-list');
const components = [];
if (compListEl) {
compListEl.querySelectorAll('.component-row').forEach(row => {
const nameInp = row.querySelector('[data-comp-name]');
const scoredInp = row.querySelector('[data-comp-scored]');
const maxInp = row.querySelector('[data-comp-max]');
if (nameInp && nameInp.value.trim()) {
components.push({ name: nameInp.value.trim(), scored: +scoredInp.value||0, max: +maxInp.value||0 });
}
});
}
const extScored = +document.getElementById('f-ext-scored')?.value || 0;
const extMax = +document.getElementById('f-ext-max')?.value || 0;
const external = { scored: extScored, max: extMax };
const subj = { id: mode==='edit'?id:uid(), name, components, external };
// Legacy compat: keep scored/max as totals for dashboard
const intTotal = components.reduce((a,c)=>a+(+c.scored||0),0);
const intMax = components.reduce((a,c)=>a+(+c.max||0),0);
subj.scored = intTotal + extScored;
subj.max = intMax + extMax;
if (mode === 'edit') {
const idx = state.sslc.findIndex(s=>s.id===id);
state.sslc[idx] = subj;
} else {
state.sslc.push(subj);
}
await dbPut('sslc', subj);
toast(mode==='edit'?'Subject updated':'Subject added');
closeModal();
renderSSLC();
return;
}
const fSem = document.getElementById('f-sem')?.value || semId;
const examType = document.getElementById('f-examtype')?.value || 'regular';
// Gather components
const compList = document.getElementById('comp-list');
const components = [];
compList.querySelectorAll('.component-row').forEach(row => {
const nameInp = row.querySelector('[data-comp-name]');
const scoredInp = row.querySelector('[data-comp-scored]');
const maxInp = row.querySelector('[data-comp-max]');
if (nameInp && nameInp.value.trim()) {
components.push({ name: nameInp.value.trim(), scored: +scoredInp.value||0, max: +maxInp.value||0 });
}
});
const external = {
scored: +document.getElementById('f-ext-scored').value || 0,
max: +document.getElementById('f-ext-max').value || 60,
};
const subj = { id: mode==='edit'?id:uid(), sem: fSem, name, examType, components, external };
if (mode === 'edit') {
const idx = state[stage].findIndex(s=>s.id===id);
state[stage][idx] = subj;
} else {
state[stage].push(subj);
}
await dbPut(stage, subj);
toast(mode==='edit'?'Subject updated':'Subject added');
closeModal();
renderStage(stage);
}