5
5
$ ( "#table-wrapper" ) . handsontable ( {
6
6
colHeaders : false ,
7
7
contextMenu : true ,
8
- afterChange : genPTT
8
+ afterChange : genPTT ,
9
+ afterSetCellMeta : genPTT
9
10
} ) ;
10
11
}
11
12
@@ -58,52 +59,64 @@ function genPTT(){
58
59
bottom_right : '┘'
59
60
}
60
61
}
62
+ var spacePadding = document . getElementById ( "spacePadding" ) . checked
61
63
62
- var arr = extractData ( ) ;
63
- var widths = getWidths ( arr ) ;
64
+ var data = extractData ( spacePadding ) ;
65
+ var widths = getWidths ( data , spacePadding ) ;
66
+ var heights = getHeights ( data , spacePadding ) ;
64
67
var str = "" ;
65
- var i , j , k , m , entry , row , pseudoRows , plen ;
68
+ var i , j , k , m , entry , item , offsets , end ;
66
69
var typeOption = document . getElementById ( 'type' ) . value ;
67
70
68
71
// top
69
72
str += generateSeparationLine ( widths , style , 'top_left' , 'top_center' , 'top_right' ) ;
70
73
71
74
// rows
72
- for ( k = 0 ; k < arr . length ; k ++ ) {
73
-
74
- row = arr [ k ] ;
75
- pseudoRows = [ ] ;
76
-
77
- for ( i = 0 ; i < widths . length ; i ++ ) {
78
- entry = arr [ k ] [ i ] ;
79
- if ( ! entry ) continue ;
80
- entry = entry . split ( '\n' ) ;
81
- plen = pseudoRows . length ;
82
- for ( j = 0 ; j < entry . length - plen ; j ++ ) {
83
- pseudoRows . push ( [ ] ) ;
84
- }
85
- for ( j = 0 ; j < entry . length ; j ++ ) {
86
- pseudoRows [ j ] [ i ] = entry [ j ] ;
75
+ for ( i = 0 ; i < heights . length ; i ++ ) {
76
+ offsets = [ ] ;
77
+ for ( j = 0 ; j < widths . length ; j ++ ) {
78
+ if ( 'bottom' == data [ 'arr' ] [ i ] [ j ] [ 'vAlign' ] ) {
79
+ offsets [ j ] = data [ 'arr' ] [ i ] [ j ] [ 'pseudoRows' ] . length - heights [ i ] ;
80
+ } else if ( 'middle' == data [ 'arr' ] [ i ] [ j ] [ 'vAlign' ] ) {
81
+ offsets [ j ] = Math . ceil ( ( data [ 'arr' ] [ i ] [ j ] [ 'pseudoRows' ] . length - heights [ i ] ) / 2 ) ;
82
+ } else {
83
+ offsets [ j ] = 0 ;
87
84
}
88
85
}
89
86
90
- for ( m = 0 ; m < pseudoRows . length ; m ++ ) {
87
+ for ( m = 0 ; m < heights [ i ] ; m ++ ) {
91
88
str += style [ 'vertical' ] ;
92
- for ( i = 0 ; i < widths . length ; i ++ ) {
93
- entry = pseudoRows [ m ] [ i ] || '' ;
89
+ for ( j = 0 ; j < widths . length ; j ++ ) {
90
+ item = data [ 'arr' ] [ i ] [ j ] ;
91
+ if ( item [ 'empty' ] ) {
92
+ entry = '' ;
93
+ } else {
94
+ entry = item [ 'pseudoRows' ] [ m + offsets [ j ] ] || '' ;
95
+ }
96
+ if ( 'right' == data [ 'arr' ] [ i ] [ j ] [ 'hAlign' ] ) {
97
+ end = widths [ j ] - entry . length ;
98
+ } else if ( 'center' == data [ 'arr' ] [ i ] [ j ] [ 'hAlign' ] ) {
99
+ end = Math . floor ( ( widths [ j ] - entry . length ) / 2 ) ;
100
+ } else {
101
+ end = 0 ;
102
+ }
103
+ for ( k = 0 ; k < end ; k ++ ) {
104
+ str += ' ' ;
105
+ }
94
106
str += entry ;
95
- for ( j = entry . length ; j < widths [ i ] ; j ++ ) {
107
+ end = widths [ j ] - entry . length - end ;
108
+ for ( k = 0 ; k < end ; k ++ ) {
96
109
str += ' ' ;
97
110
}
98
- if ( i < widths . length - 1 ) {
111
+ if ( j < widths . length - 1 ) {
99
112
str += style [ 'vertical' ] ;
100
113
}
101
114
}
102
115
str += style [ 'vertical' ] ;
103
116
str += '\n' ;
104
117
}
105
118
106
- if ( ( 'grid' == typeOption && k < arr . length - 1 ) || ( 'header' == typeOption && k == 0 && k < arr . length - 1 ) ) {
119
+ if ( ( 'grid' == typeOption && i < heights . length - 1 ) || ( 'header' == typeOption && i == 0 && i < heights . length - 1 ) ) {
107
120
str += generateSeparationLine ( widths , style , 'middle_left' , 'middle_center' , 'middle_right' ) ;
108
121
}
109
122
}
@@ -113,31 +126,109 @@ function genPTT(){
113
126
$ ( '#ptt-wrapper' ) . text ( str ) ;
114
127
}
115
128
116
- function extractData ( ) {
117
- return $ ( '#table-wrapper' ) . handsontable ( 'getData' ) ;
129
+ function extractData ( spacePadding ) {
130
+ var i , j , k , item , lines , w , meta , vAlign , hAlign ;
131
+ var result = [ ] ;
132
+ var table = $ ( '#table-wrapper' ) ;
133
+ var arr = table . handsontable ( 'getData' ) ;
134
+ for ( i = 0 ; i < arr . length ; i ++ ) {
135
+ result . push ( [ ] ) ;
136
+ for ( j = 0 ; j < arr [ i ] . length ; j ++ ) {
137
+ item = arr [ i ] [ j ] ;
138
+ if ( ! item ) {
139
+ result [ i ] [ j ] = { empty : true } ;
140
+ } else {
141
+ w = 0 ;
142
+ lines = item . split ( '\n' ) ;
143
+ for ( k = 0 ; k < lines . length ; k ++ ) {
144
+ if ( spacePadding ) {
145
+ if ( lines [ k ] . indexOf ( ' ' , 0 ) !== 0 ) {
146
+ lines [ k ] = ' ' + lines [ k ] ;
147
+ }
148
+ if ( lines [ k ] . indexOf ( ' ' , lines [ k ] . length - 1 ) === - 1 ) {
149
+ lines [ k ] = lines [ k ] + ' ' ;
150
+ }
151
+ }
152
+ if ( lines [ k ] . length > w ) {
153
+ w = lines [ k ] . length ;
154
+ }
155
+ }
156
+ meta = table . handsontable ( 'getCellMeta' , i , j ) ;
157
+ hAlign = 'left' ;
158
+ vAlign = 'top' ;
159
+ if ( meta [ 'className' ] ) {
160
+ if ( meta [ 'className' ] . indexOf ( 'htCenter' ) > - 1 ) {
161
+ hAlign = 'center' ;
162
+ } else if ( meta [ 'className' ] . indexOf ( 'htRight' ) > - 1 ) {
163
+ hAlign = 'right' ;
164
+ } else if ( meta [ 'className' ] . indexOf ( 'htJustify' ) > - 1 ) {
165
+ hAlign = 'justify' ;
166
+ }
167
+ if ( meta [ 'className' ] . indexOf ( 'htMiddle' ) > - 1 ) {
168
+ vAlign = 'middle' ;
169
+ } else if ( meta [ 'className' ] . indexOf ( 'htBottom' ) > - 1 ) {
170
+ vAlign = 'bottom' ;
171
+ }
172
+ }
173
+ result [ i ] [ j ] = { empty : false , pseudoRows : lines , maxWidth : w , vAlign : vAlign , hAlign : hAlign } ;
174
+ }
175
+ }
176
+ }
177
+ return { arr : result , vLen : i , hLen : j } ;
118
178
}
119
179
120
- function getWidths ( arr ) {
180
+ function getWidths ( data , spacePadding ) {
121
181
var widths = [ ] ;
122
- var i , j , k , w , item , lines ;
182
+ var i , j , w , item ;
183
+ var hasContent = false ;
123
184
124
- for ( i = 0 ; i < arr . length ; i ++ ) {
125
- for ( j = 0 ; j < arr [ i ] . length ; j ++ ) {
126
- w = widths [ j ] || 0 ;
127
- item = arr [ i ] [ j ] ;
128
- if ( ! item ) continue ;
129
- lines = item . split ( '\n' ) ;
130
- for ( k = 0 ; k < lines . length ; k ++ ) {
131
- if ( lines [ k ] . length > w ) {
132
- w = lines [ k ] . length ;
185
+ for ( j = data [ 'hLen' ] - 1 ; j >= 0 ; j -- ) {
186
+ w = 0 ;
187
+ for ( i = 0 ; i < data [ 'vLen' ] ; i ++ ) {
188
+ item = data [ 'arr' ] [ i ] [ j ] ;
189
+ if ( ! item [ 'empty' ] ) {
190
+ if ( item [ 'maxWidth' ] > w ) {
191
+ w = item [ 'maxWidth' ] ;
133
192
}
134
193
}
194
+ }
195
+ if ( hasContent || w > 0 ) {
196
+ if ( spacePadding && w == 0 ) {
197
+ w = 1 ;
198
+ }
135
199
widths [ j ] = w ;
200
+ hasContent = true ;
136
201
}
137
202
}
138
203
return widths ;
139
204
}
140
205
206
+ function getHeights ( data , spacePadding ) {
207
+ var heights = [ ] ;
208
+ var i , j , h , item ;
209
+ var hasContent = false ;
210
+
211
+ for ( i = data [ 'vLen' ] - 1 ; i >= 0 ; i -- ) {
212
+ h = 0 ;
213
+ for ( j = 0 ; j < data [ 'hLen' ] ; j ++ ) {
214
+ item = data [ 'arr' ] [ i ] [ j ] ;
215
+ if ( ! item [ 'empty' ] ) {
216
+ if ( item [ 'pseudoRows' ] . length > h ) {
217
+ h = item [ 'pseudoRows' ] . length ;
218
+ }
219
+ }
220
+ }
221
+ if ( hasContent || h > 0 ) {
222
+ if ( spacePadding && h == 0 ) {
223
+ h = 1 ;
224
+ }
225
+ heights [ i ] = h ;
226
+ hasContent = true ;
227
+ }
228
+ }
229
+ return heights ;
230
+ }
231
+
141
232
function generateSeparationLine ( widths , style , leftKey , centerKey , rightKey ) {
142
233
var str = "" ;
143
234
str += style [ leftKey ] ;
0 commit comments