Newer
Older
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Fancytree - Example: Events</title>
<script src="//code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<link href="../src/skin-win8/ui.fancytree.css" rel="stylesheet">
<script src="../src/jquery.fancytree.js"></script>
<!-- Start_Exclude: This block is not part of the sample code -->
<link href="../lib/prettify.css" rel="stylesheet">
<script src="../lib/prettify.js"></script>
<link href="sample.css" rel="stylesheet">
<script src="sample.js"></script>
<!-- End_Exclude -->
<script type="text/javascript">
$.ui.fancytree.debugLevel = 1; // silence debug output
function logEvent(event, data, msg){
// var args = $.isArray(args) ? args.join(", ") :
msg = msg ? ": " + msg : "";
$.ui.fancytree.info("Event('" + event.type + "', node=" + data.node + ")" + msg);
}
$(function(){
$("#tree").fancytree({
checkbox: true,
// --- Tree events -------------------------------------------------
blurTree: function(event, data) {
logEvent(event, data);
},
create: function(event, data) {
logEvent(event, data);
},
init: function(event, data, flag) {
logEvent(event, data, "flag=" + flag);
},
focusTree: function(event, data) {
logEvent(event, data);
},
restore: function(event, data) {
logEvent(event, data);
},
// --- Node events -------------------------------------------------
activate: function(event, data) {
logEvent(event, data);
var node = data.node;
// acces node attributes
$("#echoActive").text(node.title);
if( !$.isEmptyObject(node.data) ){
// alert("custom node data: " + JSON.stringify(node.data));
}
},
beforeActivate: function(event, data) {
logEvent(event, data, "current state=" + data.node.isActive());
// return false to prevent default behavior (i.e. activation)
// return false;
},
beforeExpand: function(event, data) {
logEvent(event, data, "current state=" + data.node.isExpanded());
// return false to prevent default behavior (i.e. expanding or collapsing)
// return false;
},
beforeSelect: function(event, data) {
// console.log("select", event.originalEvent);
logEvent(event, data, "current state=" + data.node.isSelected());
// return false to prevent default behavior (i.e. selecting or deselecting)
// if( data.node.isFolder() ){
// return false;
// }
},
blur: function(event, data) {
logEvent(event, data);
$("#echoFocused").text("-");
},
click: function(event, data) {
logEvent(event, data, ", targetType=" + data.targetType);
// return false to prevent default behavior (i.e. activation, ...)
//return false;
},
collapse: function(event, data) {
logEvent(event, data);
},
createNode: function(event, data) {
// Optionally tweak data.node.span or bind handlers here
logEvent(event, data);
},
dblclick: function(event, data) {
logEvent(event, data);
// data.node.toggleSelect();
},
deactivate: function(event, data) {
logEvent(event, data);
$("#echoActive").text("-");
},
expand: function(event, data) {
logEvent(event, data);
},
enhanceTitle: function(event, data) {
logEvent(event, data);
},
focus: function(event, data) {
logEvent(event, data);
$("#echoFocused").text(data.node.title);
},
keydown: function(event, data) {
logEvent(event, data);
switch( event.which ) {
case 32: // [space]
data.node.toggleSelected();
return false;
}
},
keypress: function(event, data) {
// currently unused
logEvent(event, data);
},
lazyLoad: function(event, data) {
logEvent(event, data);
// return children or any other node source
data.result = {url: "ajax-sub2.json"};
// data.result = [
// {title: "A Lazy node", lazy: true},
// {title: "Another node", selected: true}
// ];
},
loadChildren: function(event, data) {
logEvent(event, data);
},
loadError: function(event, data) {
logEvent(event, data);
},
modifyChild: function(event, data) {
logEvent(event, data, "operation=" + data.operation +
", child=" + data.childNode);
},
postProcess: function(event, data) {
logEvent(event, data);
// either modify the ajax response directly
data.response[0].title += " - hello from postProcess";
// or setup and return a new response object
// data.result = [{title: "set by postProcess"}];
},
renderNode: function(event, data) {
// Optionally tweak data.node.span
// $(data.node.span).text(">>" + data.node.title);
logEvent(event, data);
},
renderTitle: function(event, data) {
// NOTE: may be removed!
// When defined, must return a HTML string for the node title
logEvent(event, data);
// return "new title";
},
select: function(event, data) {
logEvent(event, data, "current state=" + data.node.isSelected());
var s = data.tree.getSelectedNodes().join(", ");
$("#echoSelected").text(s);
}
}).bind("fancytreeactivate", function(event, data){
// alternative way to bind to 'activate' event
// logEvent(event, data);
}).on("mouseenter mouseleave", ".fancytree-title", function(event){
// Add a hover handler to all node titles (using event delegation)
var node = $.ui.fancytree.getNode(event);
node.info(event.type);
});
});
</script>
<!-- Start_Exclude: This block is not part of the sample code -->
<script>
$(function(){
addSampleButton({
label: "(De)Select active node",
newline: false,
code: function(){
var node = $("#tree").fancytree("getActiveNode");
node.setSelected( !node.isSelected() );
}
});
addSampleButton({
label: "Remove active node",
newline: false,
code: function(){
var node = $("#tree").fancytree("getActiveNode");
node.remove();
}
});
});
</script>
<!-- End_Exclude -->
</head>
<body class="example">
<h1>Example: Events</h1>
<div class="description">
Implements all callbacks: Open your browser's console to see the event log.
<br>
The 'links' folders contain nodes with a custom data.url option.
This is used to open the URL in the onActivate event.
<br>
See the <a href="https://github.com/mar10/fancytree/wiki/TutorialEvents"
target="_blank" class="external">Events Tutorial</a>
for details.
</div>
<div>
<label for="skinswitcher">Skin:</label> <select id="skinswitcher"></select>
</div>
<p id="sampleButtons">
</p>
<div id="tree">
<ul>
<li class="folder">jQuery links
<ul>
<li data-foo="bar">jQuery home
<li data-json='{"url": "http://docs.jquery.com"}'>jQuery docs
</ul>
<li class="folder">Other links
<ul>
<li data-url="http://code.google.com">Google Code
</ul>
<li class="folder">Lazy loading
<ul>
<li id="k123" class="lazy">This is a lazy loading document with key k123.
<li id="k234" class="lazy folder">This is a lazy loading folder with key k234.
</ul>
</ul>
</div>
<div>Active node: <span id="echoActive">-</span></div>
<div>Selected node list: <span id="echoSelected">-</span></div>
<div>Focused node: <span id="echoFocused">-</span></div>
<!-- Start_Exclude: This block is not part of the sample code -->
<hr>
<p class="sample-links no_code">
<a class="hideInsideFS" href="https://github.com/mar10/fancytree/">Fancytree project home</a>
<a class="hideOutsideFS" href="#">Link to this page</a>
<a class="hideInsideFS" href="index.html">Example Browser</a>
<a href="#" id="codeExample">View source code</a>
</p>
<pre id="sourceCode" class="prettyprint" style="display:none"></pre>
<!-- End_Exclude -->
</body>
</html>