-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathspeaker_form.js
More file actions
446 lines (398 loc) · 17.2 KB
/
speaker_form.js
File metadata and controls
446 lines (398 loc) · 17.2 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
const fieldLength = () => {
let sum = 0;
Object.keys(document.getElementsByClassName('talk-field')).forEach((key) => {
if(document.getElementsByClassName('talk-field')[key].hidden == false){
sum += 1;
}
})
return sum;
}
// セッション追加ボタンのクリックハンドラー(重複登録を防ぐため名前付き関数として定義)
const handleAddTalkClick = (e) => {
e.preventDefault();
const time = new Date().getTime();
const regexp = new RegExp(e.target.dataset.id, 'g');
let div = document.createElement('div');
div.innerHTML = e.target.dataset.fields.replace(regexp, time);
document.getElementsByClassName('talk-fields')[0].append(div);
if (fieldLength() >= 3) {
document.getElementsByClassName('add-talk')[0].hidden = false;
}
addDeleteButtonListener(div.querySelector('.remove_talk_field'));
// 動的に追加されたフィールドにも文字数カウンター、カンファレンス選択機能、スタイル変更を適用
setTimeout(() => {
initializeCharCounter();
initializeConferenceFieldToggle();
initializeInputCardStyles();
}, 100);
return false;
};
const initializeAddTalkButton = () => {
const fields = Array.from(document.getElementsByClassName('add_talk_fields'))
if (fields.length === 0) {
return;
}
// 既存のイベントリスナーを削除してから新しいリスナーを追加(重複登録を防ぐ)
fields[0].removeEventListener('click', handleAddTalkClick);
fields[0].addEventListener('click', handleAddTalkClick);
}
const initializeRemoveTalkButton = () => {
Array.from(document.getElementsByClassName('remove_talk_field')).forEach((obj) => {addDeleteButtonListener(obj)});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
initializeAddTalkButton();
initializeRemoveTalkButton()
})
} else {
initializeAddTalkButton();
initializeRemoveTalkButton()
}
document.addEventListener('change', (e) => {
if (e.target.classList.contains('talk-categories')) {
const radio20min = e.target.parentElement.parentElement.querySelector('._20min');
const radio40min = e.target.parentElement.parentElement.querySelector('._40min');
if (e.target.selectedOptions[0].innerHTML == 'Keynote') {
radio20min.disabled = false;
radio40min.disabled = true;
radio20min.checked = true;
} else {
radio20min.checked = false;
radio20min.disabled = true;
radio40min.disabled = false;
radio40min.checked = true;
}
}
return false;
});
const addDeleteButtonListener = (obj) => {
obj.removeEventListener('click', buttonListener);
obj.addEventListener('click', buttonListener);
}
const buttonListener = (e) => {
e.preventDefault();
if (confirm("このセッションを削除しますか?")) {
// ボタン内のSVGやテキストがクリックされた場合も考慮して、closest()で削除ボタンを取得
const deleteButton = e.target.closest('.remove_talk_field');
const talkField = deleteButton.closest('.talk-field');
// destroy_flag_fieldを設定
const destroyFlag = talkField.querySelector('.destroy_flag_field');
if (destroyFlag) {
destroyFlag.value = 1;
}
// フィールドを非表示
talkField.hidden = true;
// バリデーション属性を削除
['input', 'textarea', 'select'].forEach((selector) => {
talkField.querySelectorAll(selector).forEach((elm) => {
['required', 'max', 'min', 'maxlength', 'pattern'].forEach((attr) => {
elm.removeAttribute(attr);
})
})
})
if (fieldLength() < 3) {
document.getElementsByClassName('add-talk')[0].hidden = false;
}
}
}
// 文字数制限を適用(タイトル: 60文字、概要: 500文字)
const MAX_TITLE_CHARS = 60;
const MAX_ABSTRACT_CHARS = 500;
// 文字数をカウント(全角・半角・絵文字関係なく、絵文字は1文字としてカウント)
const countChars = (str) => {
if (!str) return 0;
// Intl.Segmenterを使って絵文字を正しく1文字としてカウント
try {
const segmenter = new Intl.Segmenter('ja', { granularity: 'grapheme' });
return [...segmenter.segment(str)].length;
} catch (e) {
// フォールバック: スプレッド演算子を使用(古いブラウザ対応)
return [...str].length;
}
}
// テスト用にエクスポート(Node.js環境の場合のみ)
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
countChars,
MAX_TITLE_CHARS,
MAX_ABSTRACT_CHARS,
// テスト用に他の関数もエクスポート可能にする場合はここに追加
};
}
const initializeCharCounter = () => {
// タイトルと概要欄の入力フィールドを取得
const titleInputs = document.querySelectorAll('input[name*="[title]"]');
const abstractInputs = document.querySelectorAll('textarea[name*="[abstract]"]');
const setupCharCounter = (input, counterId, maxChars) => {
// カウンター要素が既に存在する場合はスキップ
if (document.getElementById(counterId)) {
return;
}
// カウンター要素を作成
const counter = document.createElement('span');
counter.id = counterId;
counter.className = 'char-counter';
counter.style.fontSize = '0.7em';
counter.style.color = '#666';
counter.style.marginLeft = '5px';
// 入力フィールドの親要素にカウンターを追加
const fieldDiv = input.closest('.field');
if (fieldDiv) {
const label = fieldDiv.querySelector('label');
if (label) {
label.appendChild(counter);
}
}
// 文字数カウントと表示を更新
const updateCounter = () => {
const text = input.value || '';
const charCount = countChars(text);
counter.textContent = `(${charCount}/${maxChars}文字)`;
if (charCount > maxChars) {
counter.style.color = '#dc3545';
input.style.borderColor = '#dc3545';
} else {
counter.style.color = '#666';
input.style.borderColor = '';
}
};
// 入力時に文字数をチェックし、制限を超えた場合は入力を制限
input.addEventListener('input', (e) => {
const text = e.target.value || '';
const charCount = countChars(text);
if (charCount > maxChars) {
// 制限を超えた場合、絵文字を考慮して正しく切り詰める
let truncated = '';
try {
const segmenter = new Intl.Segmenter('ja', { granularity: 'grapheme' });
const segments = [...segmenter.segment(text)];
truncated = segments.slice(0, maxChars).map(seg => seg.segment).join('');
} catch (err) {
// フォールバック: スプレッド演算子を使用
const chars = [...text];
truncated = chars.slice(0, maxChars).join('');
}
e.target.value = truncated;
}
updateCounter();
});
// 初期表示
updateCounter();
};
// タイトル欄にカウンターを設定(60文字制限)
titleInputs.forEach((input, index) => {
const formIndex = input.name.match(/\[(\d+)\]/)?.[1] || index;
setupCharCounter(input, `title-counter-${formIndex}`, MAX_TITLE_CHARS);
});
// 概要欄にカウンターを設定(500文字制限)
abstractInputs.forEach((input, index) => {
const formIndex = input.name.match(/\[(\d+)\]/)?.[1] || index;
setupCharCounter(input, `abstract-counter-${formIndex}`, MAX_ABSTRACT_CHARS);
});
}
// ページ読み込み時と動的に追加されたフィールドにも適用
const initializeCharCounters = () => {
initializeCharCounter();
// 動的に追加されたフィールドにも適用するため、MutationObserverを使用
const observer = new MutationObserver(() => {
initializeCharCounter();
});
const talkFieldsContainer = document.querySelector('.talk-fields');
if (talkFieldsContainer) {
observer.observe(talkFieldsContainer, {
childList: true,
subtree: true
});
}
}
// ===== 3カンファレンス選択機能 =====
/**
* カンファレンス選択に応じてカテゴリ・受講者フィールドを表示/非表示
*/
const toggleConferenceFields = (formIndex) => {
// HTMLで生成される実際のID(parameterize.underscoreの結果)に合わせる
const cndCheckbox = document.getElementById(`target_conference_cloud_native_${formIndex}`);
const pekCheckbox = document.getElementById(`target_conference_platform_engineering_${formIndex}`);
const srekCheckbox = document.getElementById(`target_conference_sre_${formIndex}`);
if (!cndCheckbox) return; // conference_id: 15 以外は何もしない
// CloudNative Days フィールド
const cndFields = document.querySelectorAll(`.cnd-fields[data-form-index="${formIndex}"]`);
cndFields.forEach(field => {
if (cndCheckbox.checked) {
field.style.display = 'block';
// required 属性を追加(selectタグ)
field.querySelectorAll('.cnd-category-select').forEach(select => {
select.setAttribute('required', 'required');
});
} else {
field.style.display = 'none';
// required 属性を削除し、選択をクリア(selectタグ)
field.querySelectorAll('.cnd-category-select').forEach(select => {
select.removeAttribute('required');
select.selectedIndex = 0; // 最初のオプション(「選択してください」)に戻す
});
field.querySelectorAll('.cnd-visitor-checkbox').forEach(checkbox => {
checkbox.checked = false;
});
}
});
// Platform Engineering フィールド
const pekFields = document.querySelectorAll(`.pek-fields[data-form-index="${formIndex}"]`);
pekFields.forEach(field => {
if (pekCheckbox.checked) {
field.style.display = 'block';
// required 属性を追加(selectタグ)
field.querySelectorAll('.pek-category-select').forEach(select => {
select.setAttribute('required', 'required');
});
} else {
field.style.display = 'none';
// required 属性を削除し、選択をクリア(selectタグ)
field.querySelectorAll('.pek-category-select').forEach(select => {
select.removeAttribute('required');
select.selectedIndex = 0; // 最初のオプション(「選択してください」)に戻す
});
field.querySelectorAll('.pek-visitor-checkbox').forEach(checkbox => {
checkbox.checked = false;
});
}
});
// SRE フィールド
const srekFields = document.querySelectorAll(`.srek-fields[data-form-index="${formIndex}"]`);
srekFields.forEach(field => {
if (srekCheckbox.checked) {
field.style.display = 'block';
// required 属性を追加(selectタグ)
field.querySelectorAll('.srek-category-select').forEach(select => {
select.setAttribute('required', 'required');
});
} else {
field.style.display = 'none';
// required 属性を削除し、選択をクリア(selectタグ)
field.querySelectorAll('.srek-category-select').forEach(select => {
select.removeAttribute('required');
select.selectedIndex = 0; // 最初のオプション(「選択してください」)に戻す
});
field.querySelectorAll('.srek-visitor-checkbox').forEach(checkbox => {
checkbox.checked = false;
});
}
});
};
/**
* カンファレンス選択チェックボックスにイベントリスナーを設定
*/
const initializeConferenceFieldToggle = () => {
document.querySelectorAll('.target-conference-checkbox').forEach(checkbox => {
const formIndex = checkbox.dataset.formIndex;
// 初期表示制御
toggleConferenceFields(formIndex);
// 既にイベントリスナーが登録されている場合はスキップ(重複登録を防ぐ)
if (!checkbox.dataset.listenerInitialized) {
checkbox.dataset.listenerInitialized = 'true';
// 変更イベント
checkbox.addEventListener('change', () => {
toggleConferenceFields(formIndex);
});
}
});
};
// ===== ラジオボタン・チェックボックスの選択状態スタイル =====
/**
* ラジオボタンの選択状態に応じてカードスタイルを更新
*/
const updateRadioCardStyles = (radioInput) => {
const name = radioInput.name;
// 同じname属性を持つすべてのラジオボタンを取得
const allRadios = document.querySelectorAll(`input[type="radio"][name="${name}"]`);
allRadios.forEach(radio => {
const card = radio.closest('.cfp-radio-card');
if (card) {
if (radio.checked) {
card.classList.add('border-primary', 'bg-primary', 'bg-opacity-10');
} else {
card.classList.remove('border-primary', 'bg-primary', 'bg-opacity-10');
}
}
});
};
/**
* チェックボックスの選択状態に応じてカードスタイルを更新
*/
const updateCheckboxCardStyles = (checkboxInput) => {
const card = checkboxInput.closest('.cfp-checkbox-card, .cfp-conference-card');
if (card) {
if (checkboxInput.checked) {
card.classList.add('border-primary', 'bg-primary', 'bg-opacity-10');
} else {
card.classList.remove('border-primary', 'bg-primary', 'bg-opacity-10');
}
}
};
/**
* ラジオボタンとチェックボックスのスタイル変更イベントを初期化
*/
const initializeInputCardStyles = () => {
// ラジオボタンの変更イベント
document.querySelectorAll('.cfp-radio-card input[type="radio"]').forEach(radio => {
if (!radio.dataset.styleListenerInitialized) {
radio.dataset.styleListenerInitialized = 'true';
radio.addEventListener('change', () => {
updateRadioCardStyles(radio);
});
}
});
// チェックボックスの変更イベント
document.querySelectorAll('.cfp-checkbox-card input[type="checkbox"], .cfp-conference-card input[type="checkbox"]').forEach(checkbox => {
if (!checkbox.dataset.styleListenerInitialized) {
checkbox.dataset.styleListenerInitialized = 'true';
checkbox.addEventListener('change', () => {
updateCheckboxCardStyles(checkbox);
});
}
});
// カード全体をクリックでラジオボタン/チェックボックスを選択
document.querySelectorAll('.cfp-radio-card, .cfp-checkbox-card').forEach(card => {
if (!card.dataset.cardClickInitialized) {
card.dataset.cardClickInitialized = 'true';
card.addEventListener('click', (e) => {
// すでにinputやlabelをクリックした場合は何もしない
if (e.target.tagName === 'INPUT' || e.target.tagName === 'LABEL') {
return;
}
const input = card.querySelector('input[type="radio"], input[type="checkbox"]');
if (input && !input.disabled) {
if (input.type === 'radio') {
input.checked = true;
input.dispatchEvent(new Event('change', { bubbles: true }));
} else {
input.checked = !input.checked;
input.dispatchEvent(new Event('change', { bubbles: true }));
}
}
});
}
});
};
// 既存の初期化処理に追加
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
initializeAddTalkButton();
initializeRemoveTalkButton();
initializeCharCounters();
initializeConferenceFieldToggle();
initializeInputCardStyles(); // 追加
})
} else {
initializeAddTalkButton();
initializeRemoveTalkButton();
initializeCharCounters();
initializeConferenceFieldToggle();
initializeInputCardStyles(); // 追加
}
// Turbo Frame 内でフォームが差し替えられた場合にも再初期化
document.addEventListener('turbo:frame-load', () => {
initializeCharCounters();
initializeConferenceFieldToggle();
initializeInputCardStyles();
});