-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxenon-field.html
66 lines (64 loc) · 2.56 KB
/
xenon-field.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
<link rel="import" href="../polymer/polymer.html">
<dom-module id="xenon-field">
<template>
<style>
label { font-size:12px; color:grey; font-family: Roboto, Noto, sans-serif; font-weight:400 }
#wrap { margin: 12px 0px 13px 0px }
#empty { color:gainsboro }
#content, #empty { margin-top: 4px; height:1.2em; }
#content.xwrap { height: 1.2em; overflow: hidden; }
</style>
<div id="wrap">
<label>{{label}}</label>
<div id="empty" hidden$="{{!isEmpty}}">
none
</div>
<div id="content" hidden$="{{isEmpty}}">
<content></content>
</div>
</div>
</template>
<script>
Polymer({
is:"xenon-field",
properties: {
/* text label that floats above the content */
label: String,
/* allow the content body to span more than one line, by default content is constrained */
multiline: { type:Boolean, value:false, observer:"_multilineChange" },
/* updated as children are observed to show the none placeholder */
isEmpty: { type:Boolean, value: true }
},
/* set up an observer to watch my child tree for content */
ready: function () {
var self = this;
this.observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
var val = mutation.target.textContent;
var pat = /[a-zA-Z0-9]/;
self.isEmpty = !pat.test(val);
});
});
this.config = { attributes: true, childList: true, characterData: true };
var nodes = this.getEffectiveChildNodes();
for (var i = 0; i < nodes.length; i++) {
this._observe(nodes[i], this.config);
if (nodes[i].textContent != "") this.isEmpty = false;
}
},
/* recurse through children */
_observe: function(node) {
this.observer.observe(node, this.config);
var nodes = node.childNodes;
for (var i = 0; i < nodes.length; i++) {
this._observe(nodes[i], this.config);
if (nodes[i].textContent != "") this.isEmpty = false;
}
},
/* update the class to allow text wrapped content */
_multilineChange: function (multiline) {
this.toggleClass("xwrap", !multiline, this.$.content);
}
});
</script>
</dom-module>