소스 파일 : /ex_run/embed_edit.js (2020-10-23)     소스 설명 : (구현항목) 삽입 메뉴얼 편집
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
// (2020.10.23, 차재복, Cha Jae Bok, http://www.ktword.co.kr)

/*
// (편집자용) 메뉴얼 업데이트용 버튼 생성
function editor_update_btn(id, no, type, sub_type) {
    // id : reform 항목 번호
    // no : 코드,메뉴얼 내 항목 번호
    // type : 메뉴얼(2) 구분
    // sub_type

		// 구분
		let ch;
		if (type=='1') ch = 'code_update';
		else if (type=='2') ch = 'man_update';

        let sub_ch;
        if (sub_type) sub_ch = sub_type;

		// button
		let btn = document.createElement('button');

        btn.id = id;
		btn.name = 'code';  //

		let btn_text = document.createTextNode('Update');
		btn.appendChild(btn_text);

        // btn 요소에 ajax 호출 정보 알리기
        btn.title = 'embed_edit.js/editor_update_btn() '+(type==1 ? 'code' : 'manual')+'.id:'+id+'.no:'+no;

        btn.addEventListener('click', function(e) {

			let title = document.getElementById('input_'+e.target.id+'_'+no).value;
			let contentElem = document.getElementById('area_'+e.target.id+'_'+no);
            let contentValue = contentElem.value

            // 코드 디버깅용
			if (!confirm('ch='+ch+', sub_ch='+sub_ch+', id='+id+', no='+no+', title='+title+
                            ', \n content='+contentValue)) return;

			// ajax
			let xhr = new XMLHttpRequest();
			xhr.open('POST','../reform/reform_update.php');
			xhr.onreadystatechange = function() {
			    if (xhr.readyState === 4 && xhr.status === 200) {
                    // 코드 디버깅용
					console.log(xhr.responseText);

					let data = JSON.parse(xhr.responseText);

                    // 결과 보이기
					console.log(data);
                    if(data.notice) alert('[수신알림]\n'+data.notice);
                    if(data.name) alert(data.name+' 항목 업데이트됨');
                    if(data.err_msg) alert(data.err_msg);

                    // content textarea 크기 조절 
                    let rows = contentElem.value.match(/\n/g); // 줄 카운트
                    if(rows != null) contentElem.setAttribute('rows', rows.length+1);

				}
			}
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhr.send('id='+id+'&no='+no+'&msg='+encodeURIComponent(title)+'&text='+encodeURIComponent(contentValue)+'&ch='+ch+'&sub_ch='+sub_ch);

		},false);

		return btn;
}
*/

// (편집자용) 편집 전용 버튼 생성
function editor_btn_make(btn_info) {
    // btn_name : 버튼 이름
    // type : 'code_update','man_update','code_delete','man_delete' 등 선택
    // id : code 또는 manual 테이블 내 항목 분류 번호
    // no : code 또는 manual 테이블 내 항목 번호
    // tr_id : table 내 tr id

	// button 생성
	let btn = document.createElement('button');

    // 생성된 button에 정보 기록
	const btn_name = document.createTextNode(btn_info.btn_name);

	btn.appendChild(btn_name);              // button name

    btn.dataset.type = btn_info.type;       // 'man_update','man_delete' 등 선택
    btn.dataset.id = btn_info.id;           // manual 테이블 내 항목 분류 번호
    btn.dataset.no = btn_info.no;           // manual 테이블 내 항목 번호

    btn.dataset.tr_id = btn_info.tr_id;     // table 내 tr id
    btn.dataset.tr_seq = btn_info.tr_seq;   // table 내 tr no

	return btn;

}


// (편집자용 manual table 버튼) (메뉴얼) 버튼 ajax 실행
function editor_btn_ajax_action(btn) {
    
    btn.addEventListener('click', function(e) {

        const url = '../reform/reform_update.php';
        const method = 'post';

        const id = btn.dataset.id;          // manual 테이블 내 항목 분류 번호
        const no = btn.dataset.no;          // manual 테이블 내 항목 번호
        const ch = btn.dataset.type;        // 'man_update','man_delete','man_insert' 등 선택
        const tr_id = btn.dataset.tr_id;    // table 내 tr id
        const tr_seq = btn.dataset.tr_seq;    // table 내 tr seq

        const td1Title = document.getElementById('input_'+btn.dataset.id+'_'+no).value; // 왼쪽 td 타이틀
		const td2AreaElem = document.getElementById('area_'+btn.dataset.id+'_'+no);     // 오른쪽 td textarea 요소
        const td2AreaValue = td2AreaElem.value;                                         // 오른쪽 td textarea 요소 내 텍스트

        const currentTr = document.getElementById(tr_id);   // 현재 tr 노드
        const currentTable = currentTr.parentNode;          // 현재 table 노드

        const liNode = e.target.closest('li');   // 해당 li 노드
        const currentParentObject = JSON.parse(sessionStorage.getItem('sid_' + liNode.dataset.parent));    // 현재 부모 sessionStorage

        // 삭제 경고
        if (ch=='man_delete' || ch=='code_delete')
            if(!confirm('삭제 여부 확인 ('+ch+')')) return;

        // 코드 디버깅용
        if (!confirm('ch='+ch+'\n id='+id+'\n no='+no+'\n title='+td1Title+'\n content='+td2AreaValue+'\n tr_id='+tr_id)) return;

           
        // ajax 실행
        let xhr = new XMLHttpRequest();
        xhr.open(method, url);
        xhr.onreadystatechange = function() {
			    if (xhr.readyState === 4 && xhr.status === 200) {
                    // 서버 응답 디버깅용
//					console.log(xhr.responseText);

					let data = JSON.parse(xhr.responseText);

                    // 서버 처리 결과 에러 검토
					if(data.err_msg) { alert('[에러발생]\n'+data.err_msg); console.log(data); return; }
                    if(data.notice) alert('[경고알림]\n'+data.notice);

                    // 현재 메뉴얼 tr 삭제
                    if (ch=='man_delete') {
                        //현재 tr 삭제
                        currentTable.removeChild(currentTr);

                        // 만일, 현재 메뉴얼 tr 하나도 없으면, 
                        if(!currentTable.hasChildNodes()) {
                            // currentTable 삭제
                            liNode.removeChild(currentTable);
                            // li line 요소에서,
                            const target = liNode.querySelector("span[name='line']");
                                // border bottom, mouse pointer 제거
                                target.style.borderBottom = '';
                                target.style.cursor = '';
                                // li line 요소에서 이벤트리스너 삭제
                                target.removeEventListener('click',moreShow);
                            // parent 노드의 sessionStorage 수정
                            currentParentObject[liNode.dataset.subseq]['more_type'] = null;
                            currentParentObject[liNode.dataset.subseq]['more_ptr'] = null;
                            const currentParentData = JSON.stringify(currentParentObject);
                            sessionStorage.setItem('sid_' + liNode.dataset.parent, currentParentData);
                        }

                    }

                    // 현재 메뉴얼 항목 직후 빈 tr 삽입
                    if (ch=='man_insert') {
                        // 나머지 tr들에 대해 seq 번호 1씩 증가
                        const restChilds = currentTable.childNodes;
                        for (let i=currentTr.seq+1; i<restChilds.length; i++) {
                            restChilds[i].dataset.seq = seq+1;
                        }
                        // 서버로부터 받은 데이터
                        const newObject = {'no':data.no,'type':data.name,'code':'','date':data.date,'seq':data.seq};
                        // 새로운 tr 생성
                        const newTr = table_tr_make(id, newObject, '', data.seq+1);
                        // 현재 tr 직후 newTr 삽입
                        insertAfter(currentTr, newTr);
                    }

                    // 현재 메뉴얼 항목 업데이트
                    if (ch=='man_update') {
                        if(data.name) alert(data.name+' 항목 업데이트됨');

                        // td2AreaElem의 textarea 크기 조절 
                        let rows = data.code.match(/\n/g).length; // 줄 카운트
                        if(rows > 0) td2AreaElem.setAttribute('rows', rows+1);

                        // alert('이전줄수:'+td2AreaValue.match(/\n/g).length+', 서버갱신후줄수:'+data.code.match(/\n/g).length);
                        // td2AreaElem의 텍스트 교체
//                        td2AreaElem.innerContent = data.code;
                        td2AreaElem.value = data.code;
                    }
				}
        }
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhr.send('ch='+ch+'&id='+id+'&no='+no+'&msg='+encodeURIComponent(td1Title)+'&text='+encodeURIComponent(td2AreaValue));

    },false);

}



Copyrightⓒ written by 차재복 (Cha Jae Bok)
"본 웹사이트 내 모든 저작물은 원출처를 밝히는 한 자유롭게 사용(상업화포함) 가능합니다"