This repository was archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathx-property-editor.html
177 lines (172 loc) · 5.2 KB
/
x-property-editor.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
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
<!--
Copyright 2013 The Polymer Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../x-editors/x-editors.html">
<polymer-element name="x-property-editor" attributes="editorTag">
<template>
<style>
:host {
display: block;
white-space: nowrap;
}
</style>
<content></content>
</template>
<script>
Polymer('x-property-editor', {
meta: null,
metaChanged: function(type) {
// create polymorphic editor
this.editor = document.createElement(this.meta.editorTag);
this.appendChild(this.editor);
// bind propertyValue to property
var p = this.meta.property;
this.bindProperty('propertyValue', new PathObserver(p.obj, p.name));
// bind this.editor.value to propertyValue
// (imperative form of <editor value="{{editorValue}}">)
this.editor.bindProperty('value', new PathObserver(this, 'propertyValue'));
// update editor meta from property
this.updateEditorMeta();
},
updateEditorMeta: function() {
if (this.editor) {
this.editor.meta = this.meta.property.meta || {};
this.editor.meta.obj = this.meta.property.obj;
}
}
});
</script>
</polymer-element>
<polymer-element name="x-binding-editor" extends="x-abstract-editor">
<template>
<style>
:host {
display: block;
white-space: nowrap;
}
</style>
<x-id-select-editor id="idSelect" style="width: 112px;" options="{{elements}}" value="{{target}}" on-editor-value-changed="{{editorValueChanged}}" ></x-id-select-editor>
<!-- <x-select-editor style="width: 145px;" options="{{properties}}"></x-select-editor> -->
<x-string-editor style="width: 161px;" value="{{workingValue}}" on-editor-value-changed="{{editorValueChanged}}" ></x-string-editor>
</template>
<script>
Polymer('x-binding-editor', {
target: null,
meta: null,
metaChanged: function() {
this.$.idSelect.meta = {obj: this.meta.property.obj };
this.value = this.meta.binding || '';
},
editorValueChanged: function(event) {
event.stopPropagation();
this.commit();
},
format: function(value) {
var path = [];
if (this.target == this.meta.property.obj.id) {
this.target = '';
}
if (this.target) {
path.push('$', this.target);
}
if (value) {
path.push(value);
}
path = path.join('.');
return path;
},
valueChanged: function() {
if (this.value[0] == '$') {
var values = this.value.splice ? this.value.slice(0) :
this.value.split('.');
values.shift(); // remove $
this.target = values.shift() // get object id
this.workingValue = values.join('.');
} else {
this.workingValue = this.value;
}
},
commit: function() {
this.super();
//console.log('binding-editor.bind-property: %s - %s', this.meta.property.name, this.value);
this.fire('bind-property', {
obj: this.meta.property.obj,
name: this.meta.property.name,
path: this.value
});
},
/* commit an empty binding value when detached */
unbind: function() {
this.fire('bind-property', {
obj: this.meta.property.obj,
name: this.meta.property.name,
path: ''
});
}
});
</script>
</polymer-element>
<polymer-element name="x-id-select-editor" extends="x-select-editor">
<template>
<shadow></shadow>
</template>
<script>
Polymer('x-id-select-editor', {
root: null,
idPrefix: '',
metaChanged: function() {
this.super();
this.root = this.findRoot(this.meta.obj);
},
rootChanged: function() {
this.buildOptions();
},
findRoot: function(node) {
while (node && node.id !== 'canvas') {
node = node.parentNode;
}
return node;
},
buildOptions: function() {
var o = [''];
if (this.root) {
var pre = this.idPrefix;
Array.prototype.forEach.call(this.root.querySelectorAll('[id]'),
function(n) {
o.push(pre + n.id);
});
}
this.options = o;
},
get selectedNode() {
return this.root && this.value
&& this.root.querySelector('#' + this.value);
}
});
</script>
</polymer-element>
<polymer-element name="x-target-select-editor" extends="x-id-select-editor">
<script>
Polymer('x-target-select-editor', {
idPrefix: '#'
});
</script>
</polymer-element>
<polymer-element name="x-action-editor" extends="x-abstract-editor">
<template>
<button touch-action="none" on-pointerup="{{buttonClick}}">{{meta.label}}</button>
</template>
<script>
Polymer('x-action-editor', {
buttonClick: function() {
var action = this.meta.action;
if (this.meta.obj[action]) {
this.meta.obj[action]();
}
}
});
</script>
</polymer-element>