-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathperson.js
76 lines (57 loc) · 3.13 KB
/
person.js
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
import {Attribute, LABEL, VALUE} from "../presentationModel/presentationModel.js";
import {formProjector, listItemProjector, selectListItemForModel, removeListItemForModel, pageCss} from "./instantUpdateProjector.js";
export { MasterView, DetailView, Person, selectionMold, reset, ALL_ATTRIBUTE_NAMES }
// page-style change, only executed once
const style = document.createElement("STYLE");
style.innerHTML = pageCss;
document.head.appendChild(style);
const ALL_ATTRIBUTE_NAMES = ['firstname', 'lastname'];
let idCounter = 0;
const nextId = () => idCounter++;
const Person = () => { // facade
const id = nextId();
const firstnameAttr = Attribute("Monika", `Person.${id}.firstname`);
firstnameAttr.getObs(LABEL).setValue("First Name");
const lastnameAttr = Attribute("Mustermann", `Person.${id}.lastname`);
lastnameAttr.getObs(LABEL).setValue("Last Name");
lastnameAttr.setConverter( input => input.toUpperCase() ); // enable for playing around
lastnameAttr.setValidator( input => input.length >= 3 );
return {
firstname: firstnameAttr,
lastname: lastnameAttr,
toString: () => firstnameAttr.getObs(VALUE).getValue() + " " + lastnameAttr.getObs(VALUE).getValue(),
}
};
// View-specific parts
const MasterView = (listController, selectionController, rootElement) => {
const render = person =>
listItemProjector(listController, selectionController, rootElement, person, ALL_ATTRIBUTE_NAMES);
// binding
listController.onModelAdd(render);
listController.onModelRemove( (removedModel, removeMe) => {
removeListItemForModel(ALL_ATTRIBUTE_NAMES)(removedModel);
removedModel.firstname.setQualifier(undefined); // remove model attributes from model world
removedModel.lastname.setQualifier(undefined); // this could become more convenient
selectionController.clearSelection();
});
selectionController.onModelSelected(selectListItemForModel(ALL_ATTRIBUTE_NAMES));
};
const reset = person => {
person.firstname.setQualifier(undefined); // todo: make generic, unset all qualifiers
person.lastname.setQualifier(undefined);
person.firstname.setConvertedValue("");
person.lastname.setConvertedValue("");
return person;
};
const selectionMold = reset(Person());
const DetailView = (selectionController, rootElement) => {
formProjector(selectionController, rootElement, selectionMold, ALL_ATTRIBUTE_NAMES); // only once, view is stable, binding is stable
selectionController.onModelSelected( selectedPersonModel => { // todo: make this generic
// set the qualifiers to connect detailModel with current selection
// todo: set the values for _all_ observables
selectionMold.lastname.setQualifier(selectedPersonModel.lastname.getQualifier());
// selectionMold.lastname.getObs(VALUE).setValue(selectedPersonModel.lastname.getObs(VALUE).getValue());
selectionMold.firstname.setQualifier(selectedPersonModel.firstname.getQualifier());
// selectionMold.firstname.getObs(VALUE).setValue(selectedPersonModel.firstname.getObs(VALUE).getValue());
});
};