forked from fluxio/io-value
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_io-base-editable.html
109 lines (107 loc) · 3.18 KB
/
_io-base-editable.html
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
<!--
Base for io-value input elements
@element io-base-editable
@extends io-base
@homepage https://github.com/arodic/io-value
@demo http://akirodic.com/components/io-value/demo.html
@blurb Base for generic io-value input elements
@status alpha
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="_io-base.html">
<polymer-element name="io-base-editable" extends="io-base">
<script>
Polymer({
publish: {
/**
* Immediate value to be updated on each key stroke.
*
* @attribute immediateValue
* @type any
* @default null
*/
immediateValue: null,
/**
* Determines if value is updated from immediateValue on each key stroke
*
* @attribute updateImmediate
* @type boolean
* @default false
*/
updateImmediate: false,
/**
* Determines if the input can be clicked.
* If enabled, the input requires double-click to focus.
*
* @attribute clickmasked
* @type boolean
* @default false
*/
clickmasked: {value: false, reflect: true},
/**
* Specifies a short hint that describes the expected value of an input field
*
* @attribute placeholder
* @type string
* @default ''
*/
placeholder: '',
},
focused: false,
eventDelegates: {
'focus': 'focusAction',
'blur': 'blurAction',
'dblclick': 'dblclickAction',
'contextmenu': 'dblclickAction',
'mousedown': 'mousedownAction'
},
mousedownAction: function(event) {
// prevent focus but let currently focused blur
if (this.clickmasked && !this.focused) {
event.preventDefault();
document.activeElement.blur();
}
},
focusAction: function(event) {
event.stopPropagation();
if (event.path[0] == this.$.value) {
this.focused = true;
}
},
blurAction: function(event) {
event.stopPropagation();
var selection = window.getSelection();
selection.removeAllRanges();
this.focused = false;
if (this.clickmasked) this.disabledChanged();
if (event.path[0] == this.$.value) {
if (this.value !== this.immediateValue) {
this.setValue(this.immediateValue);
}
// If content of the value is overflowing the container,
// make sure the scroll resets at blur.
this.$.value.scrollLeft = 0
}
this.immediateValueChanged();
},
dblclickAction: function(event) {
if (this.clickmasked) {
event.preventDefault();
this.$.value.setAttribute('tabindex', 0);
this.$.value.setAttribute('contenteditable', '');
this.$.value.focus();
}
},
placeholderChanged: function() {
if (this.placeholder) {
this.setAttribute('aria-label', this.placeholder);
} else {
this.removeAttribute('aria-label');
}
},
clickmaskedChanged: function() {
this.disabledChanged();
}
});
</script>
</polymer-element>