This repository was archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
147 lines (128 loc) · 3.18 KB
/
api.js
File metadata and controls
147 lines (128 loc) · 3.18 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
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
'use strict';
import { Mixin } from 'mara';
let empty = function(value) { return value; };
let fileId = 0;
let types = {};
export let addType = function(name, toData, fromData) {
types[name] = {
toData: toData,
fromData: fromData || empty
};
};
export let adapterFor = function(type) {
return types[type];
};
addType('number', parseInt);
addType('range', parseInt);
addType('boolean', function(value) { return "true" == value; });
function traverse(root, onInput) {
const children = root.children;
for(let i=0, n=children.length; i<n; i++) {
const child = children[i];
if(FormInput.isInstance(child) || FormSection.isInstance(child)) {
onInput(child, true);
continue;
} else if(child instanceof HTMLElement) {
switch(child.nodeName) {
case 'INPUT':
case 'TEXTAREA':
case 'SELECT':
{
// First check if this input is ignored
const ignore = child.getAttribute('mara-ignore') || child.getAttribute('data-mara-ignore');
if(ignore !== 'true') {
onInput(child, false);
}
}
}
}
if(child instanceof Element) {
// Descend into all elements
traverse(child, onInput);
}
}
}
export const FormSection = Mixin(superclass => class extends superclass {
toData() {
const result = {};
traverse(this.sectionInputRoot || this, (input, custom) => {
if(custom) {
result[input.name] = input.toData();
} else {
const type = input.getAttribute('mara-type') || input.getAttribute('data-mara-type') || input.type;
const adapter = adapterFor(type);
let value = input.value;
switch(input.type) {
case 'checkbox':
if(! input.checked) {
value = null;
}
break;
case 'file':
{
let files = [];
for(let i=0; i<input.files.length; i++) {
let file = input.files[i];
files.push({
id: 'file-' + (fileId++),
name: file.name,
size: file.size,
type: file.type,
data: file
});
}
if(input.multiple) {
value = files;
} else {
value = files[0];
}
break;
}
}
if(adapter) {
value = adapter.toData(value);
}
result[input.name] = value;
}
});
return result;
}
fromData(data) {
traverse(this.sectionInputRoot || this, (input, custom) => {
const name = input.name;
if(custom) {
input.fromData(data[name]);
} else {
const type = input.getAttribute('mara-type') || input.getAttribute('data-mara-type') || input.type;
const adapter = adapterFor(type);
const value = adapter ? adapter.fromData(data) : data;
switch(input.type) {
case 'checkbox':
if(value) {
input.checked = true;
}
break;
default:
input.value = value;
}
}
});
}
});
let markAsChanged = function() {
this.classList.add('mara-changed');
};
export const FormInput = Mixin(superclass => class extends superclass {
createdCallback() {
super.createdCallback();
this.addEventListener('blur', markAsChanged);
this.addEventListener('keypress', markAsChanged);
this.addEventListener('change', markAsChanged);
}
markAsChanged() {
this.classList.add('mara-changed');
}
markAsUnchanged() {
this.classList.remove('mara-changed');
}
});