-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
273 lines (209 loc) · 6.87 KB
/
script.js
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
(function ($, document, window) {
var table = $('.table');
var tmp_storage = [];
// table.delegate('td', 'dblclick', function () {
// var _self = $(this);
// console.log('dbl click');
// table.find('td').attr('contenteditable', 'false');
// _self.attr('contenteditable', 'true');
// });
$('.table').bind('click', function (e) {
e.stopPropagation();
});
$(document).bind('click', function () {
// console.log('do something');
cancelSelected('모든 선택 해제!');
});
/**
* 테이블의 셀선택 이벤트 관련
*/
table.delegate('td', 'click', function () {
var _self = $(this);
if(_self.hasClass('selected')){
_self.removeClass('selected');
// TODO 동일한 것을 다시 선택할 경우 해당 배열을 제거해야 한다.
cancelSelected('시작영역을 다시 선택하세요.');
return;
}else{
_self.addClass('selected');
}
var row = _self.parent().index();
var column = _self.index();
console.log('row : ' + row + ' / column : ' + column);
if(tmp_storage.length === 0){
console.log('first push data');
if(_self.attr('rowspan') > 1){
console.log('병합된 셀을 첫번째로 선택하는 경우');
var i=row, size=parseInt(_self.attr('rowspan')) + parseInt(row);
for(;i<size;i++){
tmp_storage.push({
c: column,
r: i
});
}
}else{
tmp_storage.push({
c: column,
r: row
});
}
return;
}
if(tmp_storage[tmp_storage.length-1].c !== column){
cancelSelected('동일한 칼럼에서만 지정할 수 있습니다.');
return;
}
// 병합된 셀을 선택하게 될 경우 실제 그 병합된 셀을 모두 선택한 것처럼 배열에 해당 값을 넣을 수 있도록 한다.
if(_self.attr('rowspan') > 1){
console.log('병합된 셀이 : ' + _self.attr('rowspan'));
// 상단의 코드와 중복되는 지점
console.log('두번째로 선택된 셀이 병합된 셀일 경우');
console.log('row ' + row);
var i=row, size=parseInt(_self.attr('rowspan')) + parseInt(row);
for(;i<size;i++){
tmp_storage.push({
c: column,
r: i
});
}
}else{
console.log('하나의 셀');
tmp_storage.push({
c: column,
r: row
});
}
if(tmp_storage.length > 1){
console.log(tmp_storage[tmp_storage.length-1].r)
console.log('current : ' + row);
console.log( Math.abs(tmp_storage[tmp_storage.length-1].r - row) );
}
var tmp_single_row = [];
for(var i=0,size=tmp_storage.length;i<size;i++){
tmp_single_row.push(tmp_storage[i].r);
}
tmp_single_row = tmp_single_row.sort();
console.log('총 선택된 영역이 중간이 빠진 것이 없는지 확인하기 위해서');
console.log(tmp_single_row);
var tmp_selected_value = null;
for(var i=0,size=tmp_single_row.length;i<size;i++){
if(tmp_selected_value == null){
tmp_selected_value = tmp_single_row[i];
}
if(Math.abs(tmp_single_row[i] - tmp_selected_value) > 1){
console.log('순차적이지 않으므로 모두 해제!!');
cancelSelected('선택된 영역이 잘못되었습니다.');
return;
}
tmp_selected_value = tmp_single_row[i];
}
console.log(column + ' / ' + row);
});
/**
* 선택된 영역을 해제
* @param {*} msg
*/
function cancelSelected(msg){
table.find('td').removeClass('selected');
tmp_storage = [];
console.log(msg);
}
/**
* 기본에 이미 병합된 셀을 다시 분리
* @param {*} msg
*/
function restoreCell(msg){
// 배열에 저장된 위치의 각 셀에서 rowspan과 blind class를 모두 제거한다.
for(var i=0,size=tmp_storage.length;i<size;i++){
console.log('c : ' + tmp_storage[i].c + ' / r : ' + tmp_storage[i].r);
table.find('tr').eq(tmp_storage[i].r).children('td').eq(tmp_storage[i].c).attr({'rowspan' : '1'}).removeClass('blind');
}
cancelSelected('병합된 셀을 해제 후 선택 해제');
console.log(msg);
}
/**
* 선택된 셀을 병합
* @param {*} msg
*/
function mergeSelected(msg){
if(tmp_storage.length <= 1){
alert('병합할 영역을 하나 이상 선택하세요.');
return;
}
var tmp_min = null, arr_pos = null;
for(var i=0,size=tmp_storage.length;i<size;i++){
if(tmp_min == null){
tmp_min = tmp_storage[i].r;
}
if(tmp_storage[i].r <= tmp_min){
tmp_min = tmp_storage[i].r;
arr_pos = i;
}
}
console.log('min row ' + tmp_min);
console.log('arr pos ' + arr_pos);
for(var i=0,size=tmp_storage.length;i<size;i++){
if(i === arr_pos){
console.log( 'start column ' + tmp_storage[i].c );
console.log( 'start row ' + tmp_storage[i].r );
table.find('tr').eq(tmp_storage[i].r).children('td').eq(tmp_storage[i].c).attr('rowspan', tmp_storage.length);
}else{
console.log( 'blind column ' + tmp_storage[i].c );
console.log( 'blind row ' + tmp_storage[i].r );
table.find('tr').eq(tmp_storage[i].r).children('td').eq(tmp_storage[i].c).addClass('blind');
}
}
table.find('td').removeClass('selected');
tmp_storage = [];
console.log(msg);
}
/**
* 선택된 영역에 메모를 추가한다.
* @param {*} msg
*/
function insertMemo(msg){
// 하나의 선택영역에서만 메모가 가능하다
// 복수의 선택영역이 있을 경우 선택 영역을 모두 해제한다.
// 선택된 영역을 contenteditable="true"로 변환한다.
// blur 이벤트가 일어나거나 메모종료 버튼을 누를 경우 수정 모드가 종료되는 것으로 설정할 수 있겠다.
table.find('td').attr('contenteditable', 'false');
table.find('.selected').attr('contenteditable', 'true').focus();
table.find('.selected').blur(function () {
console.log('blur');
table.find('td').attr('contenteditable', 'false');
table.find('td').removeClass('selected');
});
console.log(msg);
}
$('.js-btn-merge').bind('click', function () {
mergeSelected('병합되었습니다.');
});
$('.js-btn-cancel').bind('click', function () {
cancelSelected('선택영역이 취소되었습니다.');
});
$('.js-btn-redo-merge').bind('click', function () {
// 선택영역이 있는지 확인하고
// 두개 이상 선택된 셀이 있다면 모두 선택 해제 한다.
if(table.find('.selected').length>1){
cancelSelected('병합취소는 하나의 선택영역에서만 가능합니다.');
return;
}
if(!table.find('.selected').attr('rowspan')){
cancelSelected('선택한 영역은 병합된 셀이 아닙니다.');
return;
}
if(table.find('.selected').attr('rowspan') <= 1){
cancelSelected('선택한 영역은 병합된 셀이 아닙니다.');
return;
}
console.log( table.find('.selected').attr('rowspan') );
restoreCell('병합된 셀이 분리되었습니다.');
});
$('.js-btn-write').bind('click', function () {
if(table.find('.selected').length<1){
console.log('메모할 셀을 선택해주세요.');
return;
}
insertMemo('쓰기 모드');
});
}(jQuery, document, window));