smartGeoinfoOriginal/src/main/webapp/WEB-INF/jsp/sgis/com/board/boardWrite.jsp

294 lines
9.9 KiB
Plaintext

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!-- 비쥬얼 & 페이지 제목 & 카테고리 시작 -->
<div class="page-content-visual">
<div class="page-content-visual-inner">
<div class="page-content-title-category">
<!-- 카테고리 시작 -->
<div class="category-wrapper">
<ul class="page-category">
<li class="category-item">홈 </li>
<%-- Controller에서 설정된 displayName을 사용하여 현재 게시판 카테고리 이름을 동적으로 표시 --%>
<li class="category-item">${displayName}</li>
</ul>
</div>
<!-- 카테고리 끝 -->
</div>
</div>
</div>
<!-- 비쥬얼 & 페이지 제목 & 카테고리 끝 -->
<%-- Controller에서 설정된 displayName을 사용하여 페이지 제목을 동적으로 표시 --%>
<h1 class="page-title"><span>${displayName}</span></h1>
<!-- 쓰기 시작 -->
<div id="wfrom" class="marT20">
<form id="frm">
<%-- boardCategoryId를 hidden 필드로 추가하여 글 작성 시 해당 카테고리에 글이 등록되도록 합니다. --%>
<input type="hidden" name="boardCategoryId" id="boardCategoryId" value="${boardCategoryId}"/>
<input type="hidden" name="userId" id="userId" value="${sUserId}"/>
<input type="hidden" name="sUserGrp" id="sUserGrp" value="${sUserGrp}"/>
<input type="hidden" name="userNm" id="userNm" value="${sUserNm}"/> <%-- ID 충돌 수정: userNm으로 변경 --%>
<%-- parentPostId를 hidden 필드로 추가하여 답변글 작성 시 부모 게시글 ID를 전달합니다. --%>
<input type="hidden" name="parentPostId" id="parentPostId" value="${parentPostId}"/>
<%-- 답변글인 경우, 상위글 정보 표시 --%>
<c:if test="${parentPostId != null && parentPostId != ''}">
<div class="table-scrollable marB20">
<h2>상위글</h2>
<table class="table table-bordered">
<caption>상위글 정보 테이블</caption>
<colgroup>
<col width="20%">
<col width="auto">
</colgroup>
<tbody>
<tr>
<th class="td-head" scope="row">제목</th>
<td><input type="text" id="parentTitle" class="form-control" readonly="readonly"/></td>
</tr>
<tr>
<th class="td-head" scope="row">내용</th>
<td><textarea rows="5" class="form-control" id="parentContent" readonly="readonly"></textarea></td>
</tr>
<tr>
<th class="td-head" scope="row">작성자</th>
<td><input type="text" id="parentWriter" class="form-control" readonly="readonly"/></td>
</tr>
</tbody>
</table>
</div>
<h2 class="marT20">답변 작성</h2>
</c:if>
<div class="table-scrollable">
<table class="table table-bordered">
<caption>커뮤니티 게시판 쓰기 테이블</caption>
<colgroup>
<col width="20%">
<col width="auto">
</colgroup>
<tbody>
<tr>
<th class="td-head" scope="row">제목</th>
<%-- parentPostId가 존재하는 경우 제목 입력 필드를 읽기 전용으로 설정 --%>
<td><input type="text" id="title" name="title" class="form-control"
<c:if test="${parentPostId != null && parentPostId != ''}">readonly="readonly"</c:if>/></td>
</tr>
<tr>
<th class="td-head" scope="row">파일첨부</th>
<!-- <td><input type="file" id="title" name="title" class="form-control" /></td> -->
<td>
<div id="dropZone" class="form-control p-4" style="cursor:pointer;">
<p class="text-muted">여기에 파일을 드래그 앤 드롭 하거나 클릭해서 파일 선택</p>
<input type="file" id="fileInput" multiple hidden />
<ul id="fileList" class="list-group"></ul>
</div>
</td>
</tr>
<tr>
<th class="td-head" scope="row">내용</th>
<td><textarea rows="7" class="form-control" id="boardContent" name="boardContent"></textarea></td>
</tr>
<tr>
<th class="td-head" scope="row">작성자</th>
<td><input type="text" id="writer" name="writer" class="form-control" value="${sUserNm}" readonly="readonly"/></td>
</tr>
</tbody>
</table>
</div>
<div class="contents-row t-center">
<button type="button" class="btn btn-ske-blue" onclick="goInsert()">등록</button>
<!-- <button type="reset" class="btn btn-dark-gray fclear">초기화</button> -->
<button type="reset" class="btn btn-dark-gray fclear" onclick="goList()">취소</button>
</div>
</form>
</div>
<!-- 쓰기 끝 -->
<script>
let fileList = []; // 파일첨부 - 첨부된 파일 목록 포시란
$(document).ready(function() {
var parentPostId = "${parentPostId}";
if (parentPostId !== null && parentPostId !== '' && parentPostId !== "null") {
// 부모글 정보 로드
$.ajax({
url: "/sgis/portal/board/" + parentPostId + ".do",
type: "get",
dataType: "json",
success: function(data) {
if (data) {
$("#parentTitle").val(data.title);
$("#parentContent").val(data.boardContent);
$("#parentWriter").val(data.writer);
// 답변글 제목 필드에 "RE: " 또는 "답변: " 자동 채우기
$("#title").val("RE: " + data.title);
} else {
alert("상위글 정보를 불러오는데 실패했습니다.");
}
},
error: function() {
alert("상위글 정보를 불러오는 중 오류가 발생했습니다.");
}
});
}
const $dropZone = $("#dropZone"); // 파일첨부 - 드래그앤드롭
const $fileInput = $("#fileInput"); // 파일첨부 -
const $fileListUI = $("#fileList"); // 첨부된 파일 목록 표시
// 파일 선택 창 열기
$dropZone.on("click", function () {
$fileInput.trigger("click");
});
// 드래그 오버 스타일
$dropZone.on("dragover", function (e) {
e.preventDefault();
$dropZone.addClass("dragover");
});
$dropZone.on("dragleave", function () {
$dropZone.removeClass("dragover");
});
$dropZone.on("drop", function (e) {
e.preventDefault();
$dropZone.removeClass("dragover");
const files = e.originalEvent.dataTransfer.files;
handleFiles(files);
});
$fileInput.on("change", function () {
handleFiles(this.files);
});
function handleFiles(files) {
$.each(files, function (i, file) {
// 중복 체크는 이름으로만 (간단히 처리)
if (!fileList.some(f => f.name === file.name && f.size === file.size)) {
fileList.push(file);
}
});
renderFileList();
}
function renderFileList() {
$fileListUI.empty();
$.each(fileList, function (index, file) {
const $li = $(`
<li class="list-group-item d-flex justify-content-between align-items-center">
<span>` + file.name + ` </span>
<b class="icon-trash remove-btn" title="삭제버튼" style="cursor:pointer;">삭제</b>
</li>
`);
$li.find(".remove-btn").on("click", function () {
removeFile(index);
});
$fileListUI.append($li);
});
}
function removeFile(index) {
fileList.splice(index, 1);
renderFileList();
}
});
function goList(){
var categoryId = "${boardCategoryId}" ? "${boardCategoryId}" : "${params.boardCategoryId}";
location.href = "/sgis/portal/board-list.do?boardCategoryId=" + categoryId;
}
function goInsert(){
var title = $("#title").val();
var content = $("#boardContent").val();
var boardCategoryId = $("#boardCategoryId").val();
var parentPostId = $("#parentPostId").val();
if(title == null || title == "" || title == 0){
alert("제목을 입력하세요");
return false;
} else if (content == null || content == "" || content == 0) {
alert("내용을 입력하세요");
return false;
}
var fData = $("#frm").serialize();
fData += "&boardCategoryId=" + boardCategoryId;
if (parentPostId !== null && parentPostId !== "" && parentPostId !== "null") {
fData += "&parentPostId=" + parentPostId;
}
$.ajax({
url : "/sgis/portal/board/new.do",
type : "post",
data : fData,
/* beforeSend: function(xhr){
xhr.setRequestHeader(csrfHeaderName, csrfTokenValue)
}, */
success : function(){
$("#grid").empty();
var categoryId = "${boardCategoryId}" ? "${boardCategoryId}" : "${params.boardCategoryId}";
location.href = "/sgis/portal/board-list.do?boardCategoryId=" + categoryId;
},
error : function() {
alert("error");
}
});
$(".fclear").trigger("click");
}
function addPost(){
var title = $("#title").val();
var content = $("#boardContent").val();
var boardCategoryId = $("#boardCategoryId").val();
var parentPostId = $("#parentPostId").val();
if(title == null || title == "" || title == 0){
alert("제목을 입력하세요");
return false;
} else if (content == null || content == "" || content == 0) {
alert("내용을 입력하세요");
return false;
}
const formData = new FormData();
formData.append("title", title);
formData.append("content", content);
formData.append("boardCategoryId", boardCategoryId);
if (parentPostId !== null && parentPostId !== "" && parentPostId !== "null") {
formData.append("parentPostId", parentPostId);
}
// $.each(fileList, function (i, file) {
// formData.append("files", file);
// });
$.ajax({
url : "/sgis/portal/board/new.do",
type : "post",
data: formData,
contentType: false,
processData: false,
success : function(){
$("#grid").empty();
var categoryId = "${boardCategoryId}" ? "${boardCategoryId}" : "${params.boardCategoryId}";
location.href = "/sgis/portal/board-list.do?boardCategoryId=" + categoryId;
},
error : function() {
alert("error");
}
});
$(".fclear").trigger("click");
}
</script>