forked from LRNWebComponents/hax-body
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hax-input-mixer.html
285 lines (279 loc) · 8.94 KB
/
hax-input-mixer.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<link rel="import" href="../app-layout/app-toolbar/app-toolbar.html">
<link rel="import" href="../paper-input/paper-textarea.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../paper-checkbox/paper-checkbox.html">
<link rel="import" href="../paper-swatch-picker/paper-swatch-picker.html">
<link rel="import" href="../paper-slider/paper-slider.html">
<link rel="import" href="../paper-tooltip/paper-tooltip.html">
<link rel="import" href="hax-context-item-menu.html">
<link rel="import" href="hax-context-item.html">
<!--
`hax-input-mixer`
A context menu that provides common custom-element based authoring options. While
trying to call for haxProperties which can automatically generate the buttons
required for populating input.
@demo demo/index.html
@microcopy - the mental model for this element
- context menu - this is a menu of custom-element based buttons and events for use in a larger solution.
-->
<dom-module id="hax-input-mixer">
<template>
<style>
:host {
display: block;
}
app-toolbar {
background-color: white;
padding: 0;
height: 40px;
}
hax-context-item {
margin: 0;
width: 40px;
height: 40px;
}
#elementoptions {
height: inherit;
}
paper-textarea,
paper-input {
--paper-input-container: {
margin: 0 0 8px 0;
padding: 2px;
min-width: 250px;
};
}
</style>
<app-toolbar>
<template is="dom-if" if="[[__inputselect]]">
<span>[[label]]</span>
<hax-context-item-menu
selected="{{value}}"
icon="[[icon]]"
id="input">
<slot></slot>
</hax-context-item-menu>
</template>
<template is="dom-if" if="[[__inputtextarea]]">
<paper-textarea id="input" label="[[label]]" value="{{value}}" auto-validate pattern="[[validation]]" required="[[required]]"></paper-textarea>
</template>
<template is="dom-if" if="[[__inputtextfield]]">
<paper-input id="input" type="[[validationType]]" label="[[label]]" value="{{value}}" auto-validate pattern="[[validation]]" required="[[required]]"></paper-input>
</template>
<template is="dom-if" if="[[__inputboolean]]">
<paper-checkbox id="input" checked="{{value}}">[[label]]</paper-checkbox>
</template>
<template is="dom-if" if="[[__inputflipboolean]]">
<paper-checkbox id="input" checked="{{value}}">[[label]]</paper-checkbox>
</template>
<template is="dom-if" if="[[__inputcolorpicker]]">
<span>[[label]]</span>
<paper-swatch-picker id="input" color="{{value}}"></paper-swatch-picker>
</template>
<paper-tooltip
for="input"
position="top"
offset="14">
[[description]]
</paper-tooltip>
<hax-context-item
id="updatebutton"
icon="subdirectory-arrow-right"
label$="Update [[label]]"
event-name="hax-update-tap"></hax-context-item>
</app-toolbar>
</template>
<script>
Polymer({
is: 'hax-input-mixer',
listeners: {
'hax-context-item-selected': '_haxContextOperation',
},
properties: {
/**
* value, where the magic happens.
*/
value: {
type: String,
value: null,
},
/**
* Label for the input
*/
label: {
type: String,
reflectToAttribute: true,
},
/**
* Optional regex Validation for input and textarea fields
*/
validation: {
type: String,
reflectToAttribute: true,
},
/**
* Optional input type validation; use on input field
*/
validationType: {
type: String,
reflectToAttribute: true,
},
/**
* Required; used on input and textarea fields
*/
required: {
type: Boolean,
reflectToAttribute: true,
},
/**
* Options for the input if it's a select of some form
*/
options: {
type: Object,
value: {},
reflectToAttribute: true,
},
/**
* Optional icon that represents the item mixing.
*/
icon: {
type: String,
value: 'android',
reflectToAttribute: true,
},
/**
* longer description for the input
*/
description: {
type: String,
reflectToAttribute: true,
},
/**
* longer description for the input
*/
inputMethod: {
type: String,
value: null,
reflectToAttribute: true,
observer: '_inputMethodChanged',
},
/**
* longer description for the input
*/
propertyToBind: {
type: String,
reflectToAttribute: true,
},
/**
* slot to bind input back to
*/
slotToBind: {
type: String,
reflectToAttribute: true,
},
},
/**
* Ensure our weird data binding for templates is set initially.
*/
ready: function() {
// prime methods even though invisible most likely
this._resetInputMethods();
},
/**
* Input method changes, allow our templates to rebind correctly.
*/
_inputMethodChanged: function(newValue, oldValue) {
if (newValue != null && typeof oldValue !== typeof undefined) {
let method = newValue;
let methods = this.validInputMethods();
// ensure this is a valid method of input
if (methods.includes(method)) {
// set everything false to force a correct rebind of template tags
this._resetInputMethods();
// this is weird looking, I know...
this['__input' + method] = true;
// hide the menu if it was open previously
// need to paint into the slot so clean it out and repaint
let slot = Polymer.dom(this);
while (slot.firstChild !== null) {
slot.removeChild(slot.firstChild);
}
// select needs to inject settings into the slot
if (method === 'select' && typeof this.options !== typeof undefined) {
// this hits the key => value relationship correctly
for (val in this.options) {
item = document.createElement('paper-item');
item.attributes.value = val;
item.innerHTML = this.options[val];
slot.appendChild(item);
}
}
// try and force cursor to focus on this element
setTimeout( () => {
if (typeof this.$$('#input').hideMenu === 'function') {
this.$$('#input').hideMenu();
}
this.$$('#input').focus();
}, 200);
}
}
},
/**
* Validate input method.
*/
validInputMethods: function() {
var methods = ['flipboolean', 'boolean', 'select', 'confirm', 'textfield', 'textarea', 'datepicker', 'colorpicker', 'number'];
return methods;
},
/**
* Reset all our meta attributes.
*/
_resetInputMethods: function() {
let methods = this.validInputMethods();
for (var i=0; i<methods.length; i++) {
this['__input' + methods[i]] = false;
}
},
/**
* Respond to simple modifications.
*/
_haxContextOperation: function(e) {
let detail = e.detail;
// support a simple insert event to bubble up or everything else
switch(detail.eventName) {
case 'hax-update-tap':
// minor dataType conversion for boolean
if (this.inputMethod == 'boolean') {
this.value = this.value;
}
// opposite value for a flip-boolean
else if (this.inputMethod == 'flipboolean') {
this.value = !this.value;
}
else if (this.inputMethod == 'select') {
var count = 0;
// convert value into key value
for (val in this.options) {
if (count == this.value) {
this.value = val;
continue;
}
count++;
}
}
let mixer = {
'value': this.value,
'propertyToBind': this.propertyToBind,
'slotToBind': this.slotToBind,
};
// retarget event with all the guts of this item
// this way we can do whatever we want in hax-body which is
// basically notice that we got asked to do some data binding
// and then actually do it and hide this item!!!!
this.fire('hax-input-mixer-update', {inputMixer: mixer});
break;
}
},
});
</script>
</dom-module>