-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvalue-transform.html
More file actions
98 lines (87 loc) · 3.14 KB
/
value-transform.html
File metadata and controls
98 lines (87 loc) · 3.14 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
<!--
See https://github.com/Polymer/polymer/issues/3563
This is designed for use within a dom-bind or other element. It allows two-way computed
binding via a pair of functions that encode and decode. It is helpful when you have
two elements that need to be bound together but they don't agree on a data format.
<paper-slider min="0" max="10" step="1" value="{{something_ugly}}"></paper-slider>
<value-transform raw_value="{{something_ugly}}" value="{{something_beautiful}}"
encode="_to_beautiful" decode="_to_ugly"></value-transform>
The current value is: [[something_beautiful]].
...
<script>
my_element._to_beautiful = function(the_int){
return the_int > 0 ? the_int : "off";
}
my_element._to_ugly = function(the_str){
return parseInt(the_str) || 0; // "off" is mapped to 0
}
</script>
Note that if the forward and reverse transformations do not match up then the
values will end up in an inconsistent state (recursion is prevent at the very
first level, so there is no oppurtunity for values to oscillate back and forth
before agreeing).
-->
<link rel="import" href="bower_components/polymer/polymer.html">
<dom-module id="value-transform">
<template></template>
<script>
"use strict";
Polymer({
is:'value-transform',
properties: {
value_a: {
notify: true
},
value_b: {
notify: true
},
a_to_b: {
type: String
},
b_to_a: {
type: String
}
}, observers: [
'_value_a_changed(value_a.*)',
'_value_b_changed(value_b.*)'
], attached: function(){
let host = this.domHost || this.dataHost; // TODO: is that right?
this._a_to_b_func = host ? host[this.a_to_b] : null;
this._b_to_a_func = host ? host[this.b_to_a] : null;
this._is_attached = true;
}, _value_a_changed: function(e){
if(this._changing || !this._is_attached){
return;
}
this._changing = true;
if(this._a_to_b_func){
this.set('value_b', this._a_to_b_func(this.value_a));
} else {
let sub_path = e.path.slice("value_a".length);
if(sub_path && !this.value_b){
this.set('value_b', this.value_a);
} else {
this.set('value_b' + sub_path, e.value);
}
}
this._changing = false;
}, _value_b_changed: function(e){
if(this._changing || !this._is_attached){
return;
}
this._changing = true;
if(this._b_to_a_func){
this.set('value_a', this._b_to_a_func(this.value_b));
} else {
let sub_path = e.path.slice("value_b".length);
if(sub_path && !this.value_a){
this.set('value_a', this.value_b);
} else {
this.set('value_a' + sub_path, e.value);
}
}
this._changing = false;
}
});
</script>
</dom-module>