-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeTypeOptions.js
More file actions
153 lines (147 loc) · 4.74 KB
/
nodeTypeOptions.js
File metadata and controls
153 lines (147 loc) · 4.74 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
148
149
150
151
152
153
NodeTypeOptions = Class.create({
buttonsDefs : [
{
key: "M",
type: "person",
label: "Male",
tip : "Create a person of male gender",
symbol: "◻"
}, {
key: "F",
type: "person",
label: "Female",
tip : "Create a person of female gender",
symbol: "◯"
}, {
key: "U",
type: "person",
label: "Unknown",
tip : "Create a person of unknown gender",
symbol: "◇"
}, {
key: "T",
type: "person",
label: "Twins",
tip : "Create twins (expandable to triplets or more)",
symbol: "⋀"
}, {
type: "separator"
}, {
key: "m",
type: "person",
label: "Multiple",
tip : "Create a node representing multiple siblings",
symbol: "〈n〉"
}, {
type: "separator"
}, {
key: "n",
type: "marker",
label: "No children",
tip : "Mark as childless by choice",
symbol: "┴",
callback : "setNoChildren"
}, {
key: "i",
type: "marker",
label: "Infertile",
tip : "Mark as infertile",
symbol: "╧",
callback : "setInfertile"
}
],
initialize : function() {
this.element = new Element('div', {'class' : 'callout'});
this.element.insert(new Element('span', {'class' : 'callout-handle'}));
var container = new Element('div', {'class' : 'node-type-options'});
this.element.insert(container);
var _this = this;
this.buttonsDefs.each(function(def) {
container.insert(def.type == 'separator' ? _this._separator() : _this._createOption(def, 'createChild'));
});
this.element.hide();
editor.workspace.insert(this.element);
this._onClickOutside = this._onClickOutside.bindAsEventListener(this);
},
_createOption : function(data, callback) {
var i = 1;
if(!data) {
return;
}
var o = new Element('a', {
'class' : 'node-type-option ' + (data.type || '') + '-type-option node-type-' + data.key,
'title' : data.tip,
'href' : '#'
}).update(data.symbol); // TODO: eliminate symbol, do ".update(data.label)", add style (icons);
!callback && (callback = data.callback);
var _this = this;
o.observe('click', function(event) {
event.stop();
// TODO:
//_this._node && typeof (_this._node[callback]) == 'function' && _this._node[callback]();
alert(data.key + " (" + data.label + ")"); // TODO : remove me!
_this.hide();
});
return o;
},
_separator : function() {
return new Element('span', {'class' : 'separator'}).update(' | ');
},
_positionAt : function(x, y) {
y = Math.round(y);
if (y + this.element.getHeight() > editor.workspace.getHeight()) {
this.element.addClassName("upside");
y = Math.round(y - this.element.getHeight());
}
this.element.style.top = y + "px";
var dx = Math.round(this.element.getWidth()/2);
if (x - dx + this.element.getWidth() > editor.workspace.getWidth()) {
dx = Math.round(this.element.getWidth() - (editor.workspace.getWidth() - x));
} else if (dx > x) {
dx = Math.round(x);
}
this.element.down('.callout-handle').style.left = dx + "px";
this.element.style.left = Math.round(x - dx) + "px";
},
show : function(node, x, y) {
// Quick trick to prevent the bubble from immediately hiding on the click that is supposed to display it (see also the "_onClickOutside" function).
this._ignore = true;
this._node = node;
if (!this._node) return;
// TODO decide which options to display, depending on the source node's status
// E.g.:
// if (/* the node has actual (person) children */) {
// this.element.select('.marker-type-option').invoke('hide');
// } else if (/* the node cannot have children */) {
// this.element.select('.person-type-option').invoke('hide');
// }
this.element.show();
this._positionAt(x, y);
document.observe('click', this._onClickOutside);
},
hide : function() {
document.stopObserving('click', this._onClickOutside);
if (this._node) {
delete this._node;
// reset the state
this.element.select('.node-type-option').invoke('show');
this.element.removeClassName("upside");
}
this.element.hide();
},
_onClickOutside: function (event) {
if (editor.nodeTypeOptions && editor.nodeTypeOptions.isActive()) {
if (!event.element().up || !event.element().up('.callout')) {
// Quick trick to prevent the bubble from immediately hiding on the click that is supposed to display it (see also the "show" function)
if (this._ignore) {
this._ignore = false;
} else {
editor.nodeTypeOptions.hide();
}
}
}
},
isActive : function() {
return !!this._node;
}
});