Wiki source code of 新しい学校ページを作成
Last modified by Super Admin on 2026/04/09 10:54
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{velocity}} | ||
| 2 | ## 新しい学校ページ作成フォーム(学校マスターデータ検索方式) | ||
| 3 | |||
| 4 | #set($currentUser = $xcontext.user) | ||
| 5 | #if($currentUser == 'XWiki.XWikiGuest') | ||
| 6 | {{error}}学校ページの作成にはログインが必要です。[[ログイン>>path:/bin/login/XWiki/XWikiLogin]]してください。{{/error}} | ||
| 7 | #stop | ||
| 8 | #end | ||
| 9 | |||
| 10 | #if($request.action && $request.action == 'create' && $services.csrf.isTokenValid($request.form_token)) | ||
| 11 | ## フォーム送信後の処理 | ||
| 12 | #set($schoolCode = $request.schoolCode) | ||
| 13 | #set($schoolName = $request.schoolName) | ||
| 14 | |||
| 15 | ## バリデーション: 学校コードが13文字の文科省形式であること(例: D112310000377) | ||
| 16 | ## 形式: 学校種(C1/C2/D1/D2) + 都道府県(2桁) + 設置区分(1桁) + 学校番号(7桁) + 検査数字(1桁) | ||
| 17 | #if(!$schoolCode || $schoolCode.length() != 13 || !$schoolCode.matches('^[CDX][12]\d{11}$')) | ||
| 18 | {{error}}学校コードが不正です。学校名検索から選択してください。{{/error}} | ||
| 19 | #stop | ||
| 20 | #end | ||
| 21 | |||
| 22 | #set($targetPage = "Schools.${schoolCode}.WebHome") | ||
| 23 | |||
| 24 | #if($xwiki.exists($targetPage)) | ||
| 25 | {{error}}この学校のページは既に存在します。[[既存のページを開く>>$targetPage]]{{/error}} | ||
| 26 | #else | ||
| 27 | ## 関連校として登録されているかチェック | ||
| 28 | #set($affQuery = $services.query.hql("select doc.fullName, nameObj.value from XWikiDocument doc, BaseObject bobj, StringProperty obj, StringProperty nameObj where bobj.name = doc.fullName and bobj.className = 'SeitokaiCode.SchoolClass' and obj.id.id = bobj.id and obj.id.name = 'affiliatedSchoolCode' and obj.value = :code and nameObj.id.id = bobj.id and nameObj.id.name = 'affiliatedSchoolName'").bindValue('code', $schoolCode)) | ||
| 29 | #set($affResults = $affQuery.execute()) | ||
| 30 | #if($affResults && $affResults.size() > 0) | ||
| 31 | #set($affRow = $affResults.get(0)) | ||
| 32 | #set($affPageRef = $affRow.get(0)) | ||
| 33 | #set($affPageName = $affRow.get(1)) | ||
| 34 | {{html clean="false"}} | ||
| 35 | <div class="form-message form-message-warning" style="margin: var(--sp-3) 0;"> | ||
| 36 | <svg class="ico" viewBox="0 0 24 24"><path d="M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg> | ||
| 37 | <div> | ||
| 38 | <strong>この学校は関連校(中高一貫校)として既に登録されています</strong> | ||
| 39 | <p style="margin:4px 0 0">$!escapetool.xml($affPageName) のページに統合されています。<a href="$xwiki.getURL($affPageRef, 'view')">そちらのページへ移動する →</a></p> | ||
| 40 | <p style="margin:4px 0 0;font-size:0.85em;color:var(--text-light)">別のページとして作成することもできますが、情報が分散する可能性があります。</p> | ||
| 41 | </div> | ||
| 42 | </div> | ||
| 43 | {{/html}} | ||
| 44 | #end | ||
| 45 | ## ページをプログラム的に作成 | ||
| 46 | #set($newDoc = $xwiki.getDocument($targetPage)) | ||
| 47 | ## SchoolTemplateのコンテンツを参照として設定(テンプレートの include) | ||
| 48 | ## author="target" により、SchoolTemplateの作者(superadmin)の権限でVelocityを実行 | ||
| 49 | ## これがないと学校ページの作者(一般ユーザー)のscript権限が必要になりエラーになる | ||
| 50 | $newDoc.setContent('{{include reference="SeitokaiCode.SchoolTemplate" author="target" /}}') | ||
| 51 | $newDoc.setTitle($schoolName) | ||
| 52 | $newDoc.setParent('Schools.WebHome') | ||
| 53 | ## SchoolClassオブジェクトを追加して初期値を設定 | ||
| 54 | #set($objNum = $newDoc.createNewObject('SeitokaiCode.SchoolClass')) | ||
| 55 | #set($newObj = $newDoc.getObject('SeitokaiCode.SchoolClass', $objNum)) | ||
| 56 | $newObj.set('schoolCode', $schoolCode) | ||
| 57 | $newObj.set('schoolName', $schoolName) | ||
| 58 | $newDoc.saveWithProgrammingRights('学校ページを新規作成') | ||
| 59 | ## 作成後にビューにリダイレクト | ||
| 60 | $response.sendRedirect($xwiki.getURL($targetPage, 'view')) | ||
| 61 | #end | ||
| 62 | #else | ||
| 63 | |||
| 64 | = 新しい学校ページを作成 = | ||
| 65 | |||
| 66 | {{info}} | ||
| 67 | 学校名で検索してページを作成します。1校で複数のページを持つことはできません。文科省の学校コード毎に学校ページを作成することができます。 | ||
| 68 | {{/info}} | ||
| 69 | |||
| 70 | {{html clean="false"}} | ||
| 71 | <div class="school-create-form"> | ||
| 72 | <form method="post" id="createSchoolForm"> | ||
| 73 | <input type="hidden" name="action" value="create" /> | ||
| 74 | <input type="hidden" name="form_token" value="$services.csrf.getToken()" /> | ||
| 75 | <input type="hidden" name="schoolCode" id="schoolCode" /> | ||
| 76 | <input type="hidden" name="schoolName" id="schoolName" /> | ||
| 77 | |||
| 78 | <div class="form-group"> | ||
| 79 | <label class="form-label">学校を検索 <span class="required-mark">*</span></label> | ||
| 80 | <div class="search-input-wrap"> | ||
| 81 | <input type="text" id="schoolSearchInput" class="form-input search-input-padded" | ||
| 82 | placeholder="学校名で検索(例: 日比谷、渋谷、北野...)" | ||
| 83 | autocomplete="off" /> | ||
| 84 | <span class="search-input-icon"><svg class="ico" viewBox="0 0 24 24"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg></span> | ||
| 85 | <ul id="schoolSearchResults" class="search-results-list"></ul> | ||
| 86 | </div> | ||
| 87 | <div class="form-hint">文科省の学校マスターデータから候補を検索します。</div> | ||
| 88 | <div id="manualSchoolToggle" style="margin-top:8px;"> | ||
| 89 | <a href="#" id="manualSchoolLink" style="font-size:0.85em;color:var(--accent);text-decoration:underline;">一覧にない学校の方はこちら(専修学校の高等課程等)</a> | ||
| 90 | </div> | ||
| 91 | </div> | ||
| 92 | |||
| 93 | <!-- 手動入力フォーム(初期非表示) --> | ||
| 94 | <div id="manualSchoolForm" class="form-group" style="display:none;"> | ||
| 95 | <div style="padding:16px;background:var(--bg-soft,#f8fafc);border:1px solid var(--border-color,#e2e8f0);border-radius:8px;"> | ||
| 96 | <div style="font-size:0.85em;color:var(--text-light);margin-bottom:12px;line-height:1.5;"> | ||
| 97 | 専修学校(高等課程)など、学校マスターデータに含まれない学校のページを作成できます。 | ||
| 98 | </div> | ||
| 99 | <div style="margin-bottom:10px;"> | ||
| 100 | <label class="form-label">学校名 <span class="required-mark">*</span></label> | ||
| 101 | <input type="text" id="manualSchoolName" class="form-input" placeholder="例: ○○専修学校高等課程" autocomplete="off" /> | ||
| 102 | </div> | ||
| 103 | <div style="margin-bottom:10px;"> | ||
| 104 | <label class="form-label">都道府県 <span class="required-mark">*</span></label> | ||
| 105 | <select id="manualSchoolPref" class="form-input"> | ||
| 106 | <option value="">選択してください</option> | ||
| 107 | <option value="01">北海道</option><option value="02">青森県</option> | ||
| 108 | <option value="03">岩手県</option><option value="04">宮城県</option> | ||
| 109 | <option value="05">秋田県</option><option value="06">山形県</option> | ||
| 110 | <option value="07">福島県</option><option value="08">茨城県</option> | ||
| 111 | <option value="09">栃木県</option><option value="10">群馬県</option> | ||
| 112 | <option value="11">埼玉県</option><option value="12">千葉県</option> | ||
| 113 | <option value="13">東京都</option><option value="14">神奈川県</option> | ||
| 114 | <option value="15">新潟県</option><option value="16">富山県</option> | ||
| 115 | <option value="17">石川県</option><option value="18">福井県</option> | ||
| 116 | <option value="19">山梨県</option><option value="20">長野県</option> | ||
| 117 | <option value="21">岐阜県</option><option value="22">静岡県</option> | ||
| 118 | <option value="23">愛知県</option><option value="24">三重県</option> | ||
| 119 | <option value="25">滋賀県</option><option value="26">京都府</option> | ||
| 120 | <option value="27">大阪府</option><option value="28">兵庫県</option> | ||
| 121 | <option value="29">奈良県</option><option value="30">和歌山県</option> | ||
| 122 | <option value="31">鳥取県</option><option value="32">島根県</option> | ||
| 123 | <option value="33">岡山県</option><option value="34">広島県</option> | ||
| 124 | <option value="35">山口県</option><option value="36">徳島県</option> | ||
| 125 | <option value="37">香川県</option><option value="38">愛媛県</option> | ||
| 126 | <option value="39">高知県</option><option value="40">福岡県</option> | ||
| 127 | <option value="41">佐賀県</option><option value="42">長崎県</option> | ||
| 128 | <option value="43">熊本県</option><option value="44">大分県</option> | ||
| 129 | <option value="45">宮崎県</option><option value="46">鹿児島県</option> | ||
| 130 | <option value="47">沖縄県</option><option value="00">その他(海外等)</option> | ||
| 131 | </select> | ||
| 132 | </div> | ||
| 133 | <div id="manualSimilarWarning" style="display:none;padding:10px 12px;background:#fffbeb;border-left:3px solid #f59e0b;border-radius:4px;margin-bottom:10px;font-size:0.85em;line-height:1.5;color:#92400e;"> | ||
| 134 | </div> | ||
| 135 | <div id="manualSchoolError" style="display:none;font-size:0.82em;color:#dc2626;margin-bottom:8px;"></div> | ||
| 136 | <div style="display:flex;gap:8px;"> | ||
| 137 | <button type="button" id="manualSchoolConfirm" class="btn-save" style="font-size:0.9em;padding:8px 20px;">この学校で作成</button> | ||
| 138 | <button type="button" id="manualSchoolCancel" class="btn-secondary" style="font-size:0.9em;padding:8px 16px;">一覧から選択に戻る</button> | ||
| 139 | </div> | ||
| 140 | </div> | ||
| 141 | </div> | ||
| 142 | |||
| 143 | <!-- 選択結果 --> | ||
| 144 | <div id="selectedSchoolArea" style="display:none;" class="form-group"> | ||
| 145 | <div class="selected-school-card"> | ||
| 146 | <div class="selected-school-layout"> | ||
| 147 | <div> | ||
| 148 | <div class="selected-school-label">選択された学校</div> | ||
| 149 | <div id="selectedSchoolName" class="selected-school-name"></div> | ||
| 150 | <div id="selectedSchoolInfo" class="selected-school-info"></div> | ||
| 151 | <div id="selectedSchoolCode" class="selected-school-code"></div> | ||
| 152 | </div> | ||
| 153 | <button type="button" onclick="clearSelection()" class="btn-change-school">選択解除</button> | ||
| 154 | </div> | ||
| 155 | </div> | ||
| 156 | </div> | ||
| 157 | |||
| 158 | <!-- 重複チェック結果 --> | ||
| 159 | <div id="duplicateWarning" class="duplicate-warning" style="display:none;"> | ||
| 160 | <strong><svg class="ico" viewBox="0 0 24 24"><path d="M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg> この学校のページは既に存在します</strong> | ||
| 161 | <div style="margin-top:6px;"><a id="duplicateLink" href="#">既存のページを開く →</a></div> | ||
| 162 | </div> | ||
| 163 | |||
| 164 | <!-- 関連校チェック結果 --> | ||
| 165 | <div id="affiliatedNotice" class="form-message form-message-info" style="display:none;"> | ||
| 166 | <svg class="ico" viewBox="0 0 24 24"><path d="M2 20h20"/><path d="M5 20V10l7-5 7 5v10"/><path d="M9 20v-5h6v5"/></svg> | ||
| 167 | <div> | ||
| 168 | <strong>この学校は関連校(中高一貫校)として登録されています</strong> | ||
| 169 | <p style="margin:4px 0 0" id="affiliatedNoticeText"></p> | ||
| 170 | <p style="margin:4px 0 0;font-size:0.85em;color:var(--text-light)">別のページとして作成することもできますが、情報が分散する可能性があります。</p> | ||
| 171 | </div> | ||
| 172 | </div> | ||
| 173 | |||
| 174 | <!-- 作成ボタン --> | ||
| 175 | <div id="submitArea" style="display:none;"> | ||
| 176 | <div class="create-ready-box"> | ||
| 177 | <svg class="ico" viewBox="0 0 24 24" stroke-width="2.5"><path d="M20 6L9 17l-5-5"/></svg> この学校のページはまだ作成されていません。 | ||
| 178 | </div> | ||
| 179 | <button type="submit" class="btn-save btn-full-width">ページを作成</button> | ||
| 180 | </div> | ||
| 181 | </form> | ||
| 182 | |||
| 183 | <!-- 未選択時ヒント --> | ||
| 184 | <div id="searchHint" class="search-hint-box"> | ||
| 185 | <div class="search-hint-icon"><svg class="ico" viewBox="0 0 24 24"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg></div> | ||
| 186 | <div class="search-hint-text">上の検索欄から学校名を入力してください</div> | ||
| 187 | </div> | ||
| 188 | </div> | ||
| 189 | |||
| 190 | <script> | ||
| 191 | (function() { | ||
| 192 | // XSSエスケープ関数 | ||
| 193 | function escapeHtml(s) { | ||
| 194 | var div = document.createElement('div'); | ||
| 195 | div.appendChild(document.createTextNode(s)); | ||
| 196 | return div.innerHTML; | ||
| 197 | } | ||
| 198 | |||
| 199 | // 学校マスターデータをJSON APIから取得 | ||
| 200 | var schools = []; | ||
| 201 | var schoolsLoaded = false; | ||
| 202 | |||
| 203 | // schools.json をロード(XWiki添付ファイルとして配置) | ||
| 204 | // 配置先: SeitokaiCode.SchoolMasterData の添付ファイル | ||
| 205 | var restBase = '$request.contextPath/rest/wikis/xwiki'; | ||
| 206 | fetch(restBase + '/spaces/SeitokaiCode/pages/SchoolMasterData/attachments/schools.json') | ||
| 207 | .then(function(r) { return r.json(); }) | ||
| 208 | .then(function(data) { schools = data; schoolsLoaded = true; }) | ||
| 209 | .catch(function() { | ||
| 210 | console.warn('学校マスターデータの読み込みに失敗しました。SeitokaiCode.SchoolMasterData に schools.json が添付されているか確認してください。'); | ||
| 211 | }); | ||
| 212 | |||
| 213 | var input = document.getElementById('schoolSearchInput'); | ||
| 214 | var resultsList = document.getElementById('schoolSearchResults'); | ||
| 215 | var debounceTimer = null; | ||
| 216 | |||
| 217 | input.addEventListener('input', function() { | ||
| 218 | clearTimeout(debounceTimer); | ||
| 219 | debounceTimer = setTimeout(function() { searchSchools(input.value); }, 200); | ||
| 220 | }); | ||
| 221 | |||
| 222 | function searchSchools(query) { | ||
| 223 | if (!query || query.length < 1 || !schoolsLoaded) { | ||
| 224 | resultsList.style.display = 'none'; | ||
| 225 | return; | ||
| 226 | } | ||
| 227 | var q = query.toLowerCase(); | ||
| 228 | var results = schools.filter(function(s) { | ||
| 229 | return s.name.toLowerCase().indexOf(q) >= 0 || | ||
| 230 | s.pref.indexOf(q) >= 0 || | ||
| 231 | s.city.indexOf(q) >= 0 || | ||
| 232 | s.code.indexOf(q) >= 0; | ||
| 233 | }).slice(0, 10); | ||
| 234 | |||
| 235 | resultsList.innerHTML = ''; | ||
| 236 | if (results.length === 0) { | ||
| 237 | resultsList.innerHTML = '<li class="search-result-empty">該当する学校が見つかりません</li>'; | ||
| 238 | } else { | ||
| 239 | results.forEach(function(s) { | ||
| 240 | var li = document.createElement('li'); | ||
| 241 | li.className = 'search-result-item'; | ||
| 242 | li.innerHTML = | ||
| 243 | '<div class="search-result-name">' + escapeHtml(s.name) + '</div>' + | ||
| 244 | '<div class="search-result-info">' + | ||
| 245 | escapeHtml(s.pref) + ' ' + escapeHtml(s.city) + ' ・ ' + escapeHtml(s.type) + '(' + escapeHtml(s.est) + ')' + | ||
| 246 | '</div>' + | ||
| 247 | '<div class="search-result-code">' + escapeHtml(s.code) + '</div>'; | ||
| 248 | li.onclick = function() { selectSchool(s); }; | ||
| 249 | resultsList.appendChild(li); | ||
| 250 | }); | ||
| 251 | } | ||
| 252 | resultsList.style.display = 'block'; | ||
| 253 | } | ||
| 254 | |||
| 255 | window.selectSchool = function(school) { | ||
| 256 | // hidden フィールドにセット | ||
| 257 | document.getElementById('schoolCode').value = school.code; | ||
| 258 | document.getElementById('schoolName').value = school.name; | ||
| 259 | |||
| 260 | // UI更新 | ||
| 261 | input.style.display = 'none'; | ||
| 262 | resultsList.style.display = 'none'; | ||
| 263 | document.getElementById('searchHint').style.display = 'none'; | ||
| 264 | |||
| 265 | document.getElementById('selectedSchoolName').textContent = school.name; | ||
| 266 | document.getElementById('selectedSchoolInfo').textContent = | ||
| 267 | school.pref + ' ' + school.city + ' ・ ' + school.type + '(' + school.est + ')'; | ||
| 268 | document.getElementById('selectedSchoolCode').textContent = '学校コード: ' + school.code; | ||
| 269 | document.getElementById('selectedSchoolArea').style.display = 'block'; | ||
| 270 | |||
| 271 | // 重複チェック(XWikiにページが存在するか確認) | ||
| 272 | var targetPage = 'Schools.' + school.code + '.WebHome'; | ||
| 273 | var duplicateCheck = fetch(restBase + '/spaces/Schools/spaces/' + school.code + '/pages/WebHome') | ||
| 274 | .then(function(r) { return r.ok; }) | ||
| 275 | .catch(function() { return false; }); | ||
| 276 | |||
| 277 | // 関連校チェック(他校の関連校として登録されているか確認) | ||
| 278 | var affiliatedCheck = fetch('$request.contextPath/bin/view/SeitokaiCode/CheckAffiliated?code=' + encodeURIComponent(school.code) + '&outputSyntax=plain') | ||
| 279 | .then(function(r) { return r.ok ? r.text() : ''; }) | ||
| 280 | .then(function(t) { | ||
| 281 | try { | ||
| 282 | var jsonEnd = t.indexOf('}'); | ||
| 283 | if (jsonEnd >= 0) return JSON.parse(t.substring(0, jsonEnd + 1)); | ||
| 284 | } catch(e) {} | ||
| 285 | return { found: false }; | ||
| 286 | }) | ||
| 287 | .catch(function() { return { found: false }; }); | ||
| 288 | |||
| 289 | Promise.all([duplicateCheck, affiliatedCheck]).then(function(results) { | ||
| 290 | var isDuplicate = results[0]; | ||
| 291 | var affData = results[1]; | ||
| 292 | var affNotice = document.getElementById('affiliatedNotice'); | ||
| 293 | |||
| 294 | if (isDuplicate) { | ||
| 295 | document.getElementById('duplicateWarning').style.display = 'block'; | ||
| 296 | document.getElementById('duplicateLink').href = | ||
| 297 | '$request.contextPath/bin/Schools/' + school.code + '/'; | ||
| 298 | document.getElementById('submitArea').style.display = 'none'; | ||
| 299 | } else { | ||
| 300 | document.getElementById('duplicateWarning').style.display = 'none'; | ||
| 301 | document.getElementById('submitArea').style.display = 'block'; | ||
| 302 | } | ||
| 303 | |||
| 304 | // 関連校として登録されている場合 | ||
| 305 | if (affData && affData.found) { | ||
| 306 | var linkHref = '$request.contextPath/bin/Schools/' + affData.mainCode + '/'; | ||
| 307 | document.getElementById('affiliatedNoticeText').innerHTML = | ||
| 308 | '<a href="' + escapeHtml(linkHref) + '">' + escapeHtml(affData.mainName) + '</a> に中高一貫校の関連校として統合されています。' + | ||
| 309 | '<a href="' + escapeHtml(linkHref) + '">統合先のページへ移動する →</a>'; | ||
| 310 | affNotice.style.display = 'flex'; | ||
| 311 | } else { | ||
| 312 | affNotice.style.display = 'none'; | ||
| 313 | } | ||
| 314 | }); | ||
| 315 | }; | ||
| 316 | |||
| 317 | window.clearSelection = function() { | ||
| 318 | document.getElementById('schoolCode').value = ''; | ||
| 319 | document.getElementById('schoolName').value = ''; | ||
| 320 | document.getElementById('selectedSchoolArea').style.display = 'none'; | ||
| 321 | document.getElementById('duplicateWarning').style.display = 'none'; | ||
| 322 | document.getElementById('affiliatedNotice').style.display = 'none'; | ||
| 323 | document.getElementById('submitArea').style.display = 'none'; | ||
| 324 | document.getElementById('searchHint').style.display = ''; | ||
| 325 | // 手動入力UIをリセットして一覧検索モードに戻す | ||
| 326 | var mf = document.getElementById('manualSchoolForm'); | ||
| 327 | if (mf) mf.style.display = 'none'; | ||
| 328 | var mt = document.getElementById('manualSchoolToggle'); | ||
| 329 | if (mt) mt.style.display = ''; | ||
| 330 | var fh = document.querySelector('.form-hint'); | ||
| 331 | if (fh) fh.style.display = ''; | ||
| 332 | input.parentElement.style.display = ''; | ||
| 333 | input.style.display = ''; | ||
| 334 | input.value = ''; | ||
| 335 | input.focus(); | ||
| 336 | }; | ||
| 337 | |||
| 338 | // キーボードナビゲーション | ||
| 339 | var selectedIdx = -1; | ||
| 340 | input.addEventListener('keydown', function(e) { | ||
| 341 | var items = resultsList.querySelectorAll('li'); | ||
| 342 | if (e.key === 'ArrowDown') { | ||
| 343 | e.preventDefault(); | ||
| 344 | selectedIdx = Math.min(selectedIdx + 1, items.length - 1); | ||
| 345 | items.forEach(function(li, i) { li.classList.toggle('active', i === selectedIdx); }); | ||
| 346 | } else if (e.key === 'ArrowUp') { | ||
| 347 | e.preventDefault(); | ||
| 348 | selectedIdx = Math.max(selectedIdx - 1, 0); | ||
| 349 | items.forEach(function(li, i) { li.classList.toggle('active', i === selectedIdx); }); | ||
| 350 | } else if (e.key === 'Enter' && selectedIdx >= 0) { | ||
| 351 | e.preventDefault(); | ||
| 352 | items[selectedIdx].click(); | ||
| 353 | selectedIdx = -1; | ||
| 354 | } else if (e.key === 'Escape') { | ||
| 355 | resultsList.style.display = 'none'; | ||
| 356 | selectedIdx = -1; | ||
| 357 | } | ||
| 358 | }); | ||
| 359 | |||
| 360 | // クリック外で閉じる | ||
| 361 | document.addEventListener('click', function(e) { | ||
| 362 | if (!e.target.closest('.school-create-form')) { | ||
| 363 | resultsList.style.display = 'none'; | ||
| 364 | selectedIdx = -1; | ||
| 365 | } | ||
| 366 | }); | ||
| 367 | |||
| 368 | // ========================================================================= | ||
| 369 | // X1 手動入力モード | ||
| 370 | // ========================================================================= | ||
| 371 | var manualToggle = document.getElementById('manualSchoolLink'); | ||
| 372 | var manualForm = document.getElementById('manualSchoolForm'); | ||
| 373 | var manualConfirm = document.getElementById('manualSchoolConfirm'); | ||
| 374 | var manualCancel = document.getElementById('manualSchoolCancel'); | ||
| 375 | var manualNameInput = document.getElementById('manualSchoolName'); | ||
| 376 | var manualPrefSelect = document.getElementById('manualSchoolPref'); | ||
| 377 | var manualError = document.getElementById('manualSchoolError'); | ||
| 378 | var manualSimilarWarn = document.getElementById('manualSimilarWarning'); | ||
| 379 | |||
| 380 | if (manualToggle) { | ||
| 381 | manualToggle.addEventListener('click', function(e) { | ||
| 382 | e.preventDefault(); | ||
| 383 | // 手動入力モードを表示 | ||
| 384 | manualForm.style.display = 'block'; | ||
| 385 | input.parentElement.style.display = 'none'; | ||
| 386 | document.querySelector('.form-hint').style.display = 'none'; | ||
| 387 | document.getElementById('manualSchoolToggle').style.display = 'none'; | ||
| 388 | document.getElementById('searchHint').style.display = 'none'; | ||
| 389 | resultsList.style.display = 'none'; | ||
| 390 | }); | ||
| 391 | } | ||
| 392 | |||
| 393 | if (manualCancel) { | ||
| 394 | manualCancel.addEventListener('click', function() { | ||
| 395 | // 一覧選択モードに戻る | ||
| 396 | manualForm.style.display = 'none'; | ||
| 397 | input.parentElement.style.display = ''; | ||
| 398 | document.querySelector('.form-hint').style.display = ''; | ||
| 399 | document.getElementById('manualSchoolToggle').style.display = ''; | ||
| 400 | document.getElementById('searchHint').style.display = ''; | ||
| 401 | manualNameInput.value = ''; | ||
| 402 | manualPrefSelect.selectedIndex = 0; | ||
| 403 | manualError.style.display = 'none'; | ||
| 404 | manualSimilarWarn.style.display = 'none'; | ||
| 405 | }); | ||
| 406 | } | ||
| 407 | |||
| 408 | // 類似名検索(手動入力時に既存の学校と名前が似ていないか警告) | ||
| 409 | function checkSimilarSchools(name) { | ||
| 410 | if (!name || name.length < 2 || !schoolsLoaded) { | ||
| 411 | manualSimilarWarn.style.display = 'none'; | ||
| 412 | return; | ||
| 413 | } | ||
| 414 | var q = name.toLowerCase(); | ||
| 415 | var similar = schools.filter(function(s) { | ||
| 416 | return s.name.toLowerCase().indexOf(q) >= 0 || q.indexOf(s.name.toLowerCase()) >= 0; | ||
| 417 | }).slice(0, 3); | ||
| 418 | |||
| 419 | if (similar.length > 0) { | ||
| 420 | var html = '<strong>類似する学校が一覧にあります:</strong><br>'; | ||
| 421 | similar.forEach(function(s) { | ||
| 422 | html += escapeHtml(s.name) + '(' + escapeHtml(s.pref) + ' ' + escapeHtml(s.code) + ')<br>'; | ||
| 423 | }); | ||
| 424 | html += '<span style="font-size:0.9em;">もし上記のいずれかが該当する場合は、「一覧から選択に戻る」を押して検索してください。</span>'; | ||
| 425 | manualSimilarWarn.innerHTML = html; | ||
| 426 | manualSimilarWarn.style.display = 'block'; | ||
| 427 | } else { | ||
| 428 | manualSimilarWarn.style.display = 'none'; | ||
| 429 | } | ||
| 430 | } | ||
| 431 | |||
| 432 | var manualDebounce = null; | ||
| 433 | if (manualNameInput) { | ||
| 434 | manualNameInput.addEventListener('input', function() { | ||
| 435 | clearTimeout(manualDebounce); | ||
| 436 | manualDebounce = setTimeout(function() { | ||
| 437 | checkSimilarSchools(manualNameInput.value.trim()); | ||
| 438 | }, 300); | ||
| 439 | }); | ||
| 440 | } | ||
| 441 | |||
| 442 | // SHA-256 ハッシュでX1コードを生成 | ||
| 443 | function computeX1Code(schoolName, prefCode) { | ||
| 444 | var str = schoolName.trim() + ':' + prefCode; | ||
| 445 | var encoder = new TextEncoder(); | ||
| 446 | var data = encoder.encode(str); | ||
| 447 | return crypto.subtle.digest('SHA-256', data).then(function(buf) { | ||
| 448 | var arr = new Uint8Array(buf); | ||
| 449 | var hex = ''; | ||
| 450 | for (var i = 0; i < arr.length; i++) { | ||
| 451 | hex += ('0' + arr[i].toString(16)).slice(-2); | ||
| 452 | } | ||
| 453 | var hashDigits = ''; | ||
| 454 | for (var j = 0; j < hex.length && hashDigits.length < 7; j++) { | ||
| 455 | hashDigits += (parseInt(hex[j], 16) % 10).toString(); | ||
| 456 | } | ||
| 457 | return 'X1' + prefCode + '0' + hashDigits + '0'; | ||
| 458 | }); | ||
| 459 | } | ||
| 460 | |||
| 461 | // フォールバック(SubtleCrypto 非対応環境) | ||
| 462 | function computeX1CodeFallback(schoolName, prefCode) { | ||
| 463 | var str = schoolName.trim() + ':' + prefCode; | ||
| 464 | var hash = 0; | ||
| 465 | for (var i = 0; i < str.length; i++) { | ||
| 466 | hash = ((hash << 5) - hash + str.charCodeAt(i)) | 0; | ||
| 467 | } | ||
| 468 | var digits = (Math.abs(hash) % 10000000).toString(); | ||
| 469 | while (digits.length < 7) digits = '0' + digits; | ||
| 470 | return 'X1' + prefCode + '0' + digits + '0'; | ||
| 471 | } | ||
| 472 | |||
| 473 | if (manualConfirm) { | ||
| 474 | manualConfirm.addEventListener('click', function() { | ||
| 475 | var name = manualNameInput.value.trim(); | ||
| 476 | var prefCode = manualPrefSelect.value; | ||
| 477 | |||
| 478 | // バリデーション | ||
| 479 | if (!name) { | ||
| 480 | manualError.textContent = '学校名を入力してください。'; | ||
| 481 | manualError.style.display = 'block'; | ||
| 482 | manualNameInput.focus(); | ||
| 483 | return; | ||
| 484 | } | ||
| 485 | if (name.length < 2) { | ||
| 486 | manualError.textContent = '学校名は2文字以上で入力してください。'; | ||
| 487 | manualError.style.display = 'block'; | ||
| 488 | manualNameInput.focus(); | ||
| 489 | return; | ||
| 490 | } | ||
| 491 | if (!prefCode) { | ||
| 492 | manualError.textContent = '都道府県を選択してください。'; | ||
| 493 | manualError.style.display = 'block'; | ||
| 494 | manualPrefSelect.focus(); | ||
| 495 | return; | ||
| 496 | } | ||
| 497 | |||
| 498 | manualError.style.display = 'none'; | ||
| 499 | manualConfirm.disabled = true; | ||
| 500 | manualConfirm.textContent = '処理中...'; | ||
| 501 | |||
| 502 | var prefText = manualPrefSelect.options[manualPrefSelect.selectedIndex].text; | ||
| 503 | |||
| 504 | // X1コード生成 → 選択状態にする | ||
| 505 | var codePromise; | ||
| 506 | try { | ||
| 507 | codePromise = computeX1Code(name, prefCode); | ||
| 508 | } catch(e) { | ||
| 509 | codePromise = Promise.resolve(computeX1CodeFallback(name, prefCode)); | ||
| 510 | } | ||
| 511 | |||
| 512 | codePromise.then(function(code) { | ||
| 513 | // selectSchool と同等の処理を実行 | ||
| 514 | document.getElementById('schoolCode').value = code; | ||
| 515 | document.getElementById('schoolName').value = name; | ||
| 516 | |||
| 517 | manualForm.style.display = 'none'; | ||
| 518 | document.getElementById('selectedSchoolName').textContent = name; | ||
| 519 | document.getElementById('selectedSchoolInfo').textContent = prefText + ' ・ その他の学校'; | ||
| 520 | document.getElementById('selectedSchoolCode').textContent = '学校コード: ' + code; | ||
| 521 | document.getElementById('selectedSchoolArea').style.display = 'block'; | ||
| 522 | |||
| 523 | // 重複チェック | ||
| 524 | var targetPage = 'Schools.' + code + '.WebHome'; | ||
| 525 | fetch(restBase + '/spaces/Schools/spaces/' + code + '/pages/WebHome') | ||
| 526 | .then(function(r) { return r.ok; }) | ||
| 527 | .catch(function() { return false; }) | ||
| 528 | .then(function(isDuplicate) { | ||
| 529 | if (isDuplicate) { | ||
| 530 | document.getElementById('duplicateWarning').style.display = 'block'; | ||
| 531 | document.getElementById('duplicateLink').href = | ||
| 532 | '$request.contextPath/bin/Schools/' + code + '/'; | ||
| 533 | document.getElementById('submitArea').style.display = 'none'; | ||
| 534 | } else { | ||
| 535 | document.getElementById('duplicateWarning').style.display = 'none'; | ||
| 536 | document.getElementById('submitArea').style.display = 'block'; | ||
| 537 | } | ||
| 538 | }); | ||
| 539 | |||
| 540 | manualConfirm.disabled = false; | ||
| 541 | manualConfirm.textContent = 'この学校で作成'; | ||
| 542 | }).catch(function() { | ||
| 543 | manualError.textContent = 'コードの生成に失敗しました。ブラウザを更新して再度お試しください。'; | ||
| 544 | manualError.style.display = 'block'; | ||
| 545 | manualConfirm.disabled = false; | ||
| 546 | manualConfirm.textContent = 'この学校で作成'; | ||
| 547 | }); | ||
| 548 | }); | ||
| 549 | } | ||
| 550 | |||
| 551 | // 送信時ローディング | ||
| 552 | var csForm = document.getElementById('createSchoolForm'); | ||
| 553 | if (csForm) { | ||
| 554 | csForm.addEventListener('submit', function() { | ||
| 555 | var btn = csForm.querySelector('button[type="submit"]'); | ||
| 556 | if (btn) { btn.disabled = true; btn.innerHTML = '<span class="btn-spinner"></span> 作成中...'; } | ||
| 557 | }); | ||
| 558 | } | ||
| 559 | })(); | ||
| 560 | </script> | ||
| 561 | {{/html}} | ||
| 562 | |||
| 563 | #end | ||
| 564 | {{/velocity}} |