This repository was archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheditable-table-editor-cell.html
More file actions
256 lines (236 loc) · 7 KB
/
editable-table-editor-cell.html
File metadata and controls
256 lines (236 loc) · 7 KB
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../web-animations-js/web-animations-next-lite.min.html">
<link rel="import" href="../iron-autogrow-textarea/iron-autogrow-textarea.html">
<link rel="import" href="../iron-a11y-keys/iron-a11y-keys.html">
<link rel="import" href="editable-table-behaviors.html">
<!--
`editable-table-editor-cell`
An editable cell in the editable-table-editor
(editable-table-editor.html) interface.
@demo demo/index.html
@microcopy - the mental model for this element
<editable-table-editor-cell
row="3" //The index of the cell's row
column="2" //The index of the cell's column
value=""> //The editable contents of the cell
<iron-icon class="sortable-icon"icon="editable-table:sortable" aria-hidden="true"></iron-icon>
<iron-icon class="filter-icon"icon="editable-table:filter-off"></iron-icon>
</editable-table-editor-cell>
-->
<dom-module id="editable-table-editor-cell">
<template>
<style is="custom-style">
:host {
padding: 0;
margin: 0;
width: 100%;
min-width: unset;
display: inline-flex;
justify-content: space-between;
align-items:center;
align-content: stretch;
}
:host iron-autogrow-textarea {
width: 100%;
padding: 0;
border: none;
font-weight: unset;
resize: none;
-webkit-appearance: none;
-mozilla-appearance: none;
flex-grow: 1;
--iron-autogrow-textarea: {
padding: 0;
font-weight: unset;
border: none;
resize: none;
flex-direction: column;
-webkit-flex-direction: column;
-webkit-appearance: none;
-mozilla-appearance: none;
}
}
:host iron-autogrow-textarea > * {
padding: 0;
font-weight: unset;
border: none;
resize: none;
flex-direction: column;
-webkit-flex-direction: column;
-webkit-appearance: none;
-mozilla-appearance: none;
}
</style>
<iron-autogrow-textarea
autofocus
id="cell"
label$="[[label]]"
value$="{{value}}">
</iron-autogrow-textarea>
<div id="icons"><slot></slot></div>
<iron-a11y-keys
id="down"
keys="down"
target$="[[cell]]"
on-keys-pressed="_onCellBelow">
</iron-a11y-keys>
<iron-a11y-keys
id="up"
keys="up"
target$="[[cell]]"
on-keys-pressed="_onCellAbove">
</iron-a11y-keys>
<iron-a11y-keys
id="left"
keys="left"
target$="[[cell]]"
on-keys-pressed="_onCellLeft">
</iron-a11y-keys>
<iron-a11y-keys
id="right"
keys="right"
target$="[[cell]]"
on-keys-pressed="_onCellRight">
</iron-a11y-keys>
</template>
<script>
Polymer({
is: 'editable-table-editor-cell',
listeners: { 'bind-value-changed': '_onValueChanged' },
behaviors: [ editableTableBehaviors.cellBehaviors ],
properties: {
/**
* cell row
*/
row: {
type: Number,
value: null
},
/**
* cell column
*/
column: {
type: Number,
value: null
},
/**
* cell label
*/
label: {
type: String,
computed: '_getCellLabel(column,row)'
},
/**
* cell contents
*/
value: {
type: String,
value: false,
reflectToAttribute: true
},
},
/**
* set iron-a11y-keys target to this
*/
ready: function(){
this.cell = this.$.cell;
},
/**
* focus the on text area
*/
focus: function(){
this.cell.textarea.focus();
},
/**
* if clicking in td but outside textarea, focus the text area
*/
_getCellLabel: function(column,row){
return 'Cell '+this._getLabel(column,"Column")+this._getLabel(row,"Row");
},
/**
* if clicking in td but outside textarea, focus the text area
*/
_onValueChanged: function(e){
let root = this;
root.fire('cell-value-changed',{"row": root.row, "column": root.column, "value": e.detail.value });
},
/**
* FROM: https://stackoverflow.com/questions/2897155/get-cursor-position-in-characters-within-a-text-input-field
* Returns the caret (cursor) position of the specified text field.
* Return value range is 0-oField.value.length.
*/
getCaretPosition: function(){
var caret = 0;
// IE Support
if (document.selection) {
// Set focus on the element
this.$.cell.focus();
// To get cursor position, get empty selection range
var sel = document.selection.createRange();
// Move selection start to 0 position
sel.moveStart('character', - this.$.cell.value.length);
// The caret position is selection length
caret = sel.text.length;
} else if (this.$.cell.$$('textarea').selectionStart || this.$.cell.$$('textarea').selectionStart == '0') {
caret = this.$.cell.$$('textarea').selectionStart;
}
return caret;
},
/**
* make sure caret is in the correct position
*/
setCaretPosition: function(start,end){
let textarea = this.$.cell.$$('textarea');
textarea.focus();
if (textarea.createTextRange) {
let range = textarea.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', start);
range.select();
} else if (textarea.setSelectionRange) {
textarea.setSelectionRange(start,end);
textarea.selectionStart = start;
textarea.selectionEnd = end;
}
},
/**
* set focus to textarea
*/
setFocus: function(start,end){
this.$.cell.$$('textarea').focus();
if (start !== undefined && end !== undefined) {
this.setCaretPosition(start,end);
} else if (start !== undefined) {
this.setCaretPosition(start,start);
} else {
this.setCaretPosition(0,0);
}
},
/**
* handle left
*/
_onCellLeft: function(e){
this.fire('cell-move',{"cell": this.parentNode, "direction": "left"});
},
/**
* handle right
*/
_onCellRight: function(e){
this.fire('cell-move',{"cell": this.parentNode, "direction": "right"});
},
/**
* handle up
*/
_onCellAbove: function(e){
this.fire('cell-move',{"cell": this.parentNode, "direction": "up"});
},
/**
* handle down
*/
_onCellBelow: function(e){
this.fire('cell-move',{"cell": this.parentNode, "direction": "down"});
},
});
</script>
</dom-module>