Skip to content

Commit 6164164

Browse files
committed
Work on ext-logger
1 parent 96b9566 commit 6164164

File tree

4 files changed

+74
-45
lines changed

4 files changed

+74
-45
lines changed

demo/sample-ext-logger.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,20 @@
2828
// Attach the fancytree widget to an existing <div id="tree"> element
2929
// and pass the tree options as an argument to the fancytree() function:
3030
$("#tree").fancytree({
31+
// extensions: ["logger"],
3132
extensions: ["persist", "logger"],
3233
checkbox: true,
3334
selectMode: 3,
3435
source: {url: "ajax-tree-products.json"},
36+
ajax: {debugDelay: 1000},
3537
logger: {
36-
},
37-
ajax: {
38-
// debugDelay: [200, 1000] // don't use this in production code
38+
// traceEvents: true,
39+
traceUnhandledEvents: true,
40+
// // traceHooks: true, // ["treeCreate", "treeInit"],
41+
// timings: true
3942
},
4043
init: function(event, data) {
44+
data.tree.info("Got init");
4145
},
4246
lazyLoad: function(event, data) {
4347
data.result = {url: "ajax-sub2.json"};

src/jquery.fancytree.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5681,7 +5681,7 @@ $.extend($.ui.fancytree,
56815681
this.warn("keyEventToString() is deprecated: use eventToString()");
56825682
return this.eventToString(event);
56835683
},
5684-
/** Return a wrapped handler method, that provides `this.super`.
5684+
/** Return a wrapped handler method, that provides `this._super`.
56855685
*
56865686
* @example
56875687
// Implement `opts.createNode` event to add the 'draggable' attribute
@@ -5695,20 +5695,21 @@ $.extend($.ui.fancytree,
56955695
* @param {object} instance
56965696
* @param {string} methodName
56975697
* @param {function} handler
5698+
* @param {object} [self] optional context
56985699
*/
5699-
overrideMethod: function(instance, methodName, handler){
5700+
overrideMethod: function(instance, methodName, handler, self){
57005701
var prevSuper,
57015702
_super = instance[methodName] || $.noop;
57025703

5703-
// context = context || this;
5704+
self = self || this;
57045705

57055706
instance[methodName] = function() {
57065707
try {
5707-
prevSuper = this._super;
5708-
this._super = _super;
5709-
return handler.apply(this, arguments);
5708+
prevSuper = self._super;
5709+
self._super = _super;
5710+
return handler.apply(self, arguments);
57105711
} finally {
5711-
this._super = prevSuper;
5712+
self._super = prevSuper;
57125713
}
57135714
};
57145715
},

src/jquery.fancytree.logger.js

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
*/
3636
var i,
3737
FT = $.ui.fancytree,
38+
PREFIX = "ft-logger: ",
39+
logLine = window.console.log,
3840
HOOK_NAMES = "nodeClick nodeCollapseSiblings".split(" "),
39-
TREE_EVENT_NAMES = "blurTree create init focusTree restore".split(" "),
41+
TREE_EVENT_NAMES = "beforeRestore blurTree create init focusTree restore".split(" "),
4042
NODE_EVENT_NAMES = "activate beforeActivate beforeExpand beforeSelect blur click collapse createNode dblclick deactivate expand enhanceTitle focus keydown keypress lazyLoad loadChildren loadError modifyChild postProcess renderNode renderTitle select".split(" "),
4143
EVENT_NAMES = TREE_EVENT_NAMES.concat(NODE_EVENT_NAMES),
4244
HOOK_NAME_MAP = {},
@@ -64,49 +66,59 @@ function getBrowserInfo() {
6466

6567
function logEvent(event, data) {
6668
var res,
67-
logName = "event." + event.type,
69+
/* jshint validthis: true */
70+
self = this,
71+
// logName = PREFIX + "event." + event.type,
6872
opts = data.options.logger,
69-
obj = data.node || data.tree;
73+
tree = data.tree,
74+
// widget = data.widget,
75+
obj = data.node || tree,
76+
logName = PREFIX + "event." + event.type + " (" + obj + ")";
7077

7178
if( !opts.traceEvents || (opts.traceEvents !== true && $.inArray(name, opts.traceEvents) < 0) ) {
72-
return;
79+
return self._super.apply(self, arguments);
7380
}
74-
if( opts.timings === true || (opts.timings && $.inArray(name, opts.timings) >= 0 ) ) {
81+
if( self._super && opts.timings === true || (opts.timings && $.inArray(name, opts.timings) >= 0 ) ) {
7582
// if( name === "nodeRender" ) { logName += obj; } // allow timing for recursive calls
76-
logName += " (" + obj + ")";
83+
// logName += " (" + obj + ")";
7784
window.console.time(logName);
78-
res = self._super.apply(this, arguments);
85+
res = self._super.apply(self, arguments);
7986
window.console.timeEnd(logName);
8087
} else {
81-
obj.info(logName, data);
88+
// obj.info(logName, data);
89+
logLine(logName, event, data);
90+
res = self._super.apply(self, arguments);
8291
}
92+
return res;
8393
}
8494

8595
function logHook(name, self, args, extra) {
8696
var res,
87-
logName = "hook." + name,
8897
ctx = args[0],
8998
opts = ctx.options.logger,
90-
obj = ctx.node || ctx.tree;
99+
obj = ctx.node || ctx.tree,
100+
logName = PREFIX + "hook." + name + " (" + obj + ")";
91101

92102
if( !opts.traceHooks || (opts.traceHooks !== true && $.inArray(name, opts.traceHooks) < 0) ) {
93103
return self._superApply.call(self, args);
94104
}
95105
if( opts.timings === true || (opts.timings && $.inArray(name, opts.timings) >= 0 ) ) {
96106
// if( name === "nodeRender" ) { logName += obj; } // allow timing for recursive calls
97-
logName += " (" + obj + ")";
107+
// logName += " (" + obj + ")";
98108
window.console.time(logName);
99109
res = self._superApply.call(self, args);
100110
window.console.timeEnd(logName);
101-
return res;
102111
} else {
103112
if( extra ) {
104-
obj.info(logName, extra, ctx);
113+
// obj.info(logName, extra, ctx);
114+
logLine(logName, extra, ctx);
105115
} else {
106-
obj.info(logName, ctx);
116+
// obj.info(logName, ctx);
117+
logLine(logName, ctx);
107118
}
108-
return self._superApply.call(self, args);
119+
res = self._superApply.call(self, args);
109120
}
121+
return res;
110122
}
111123

112124

@@ -120,35 +132,40 @@ $.ui.fancytree.registerExtension({
120132
options: {
121133
logTarget: null, // optional redirect logging to this <div> tag
122134
traceEvents: true, // `true`or list of hook names
135+
traceUnhandledEvents: false,
123136
traceHooks: false, // `true`or list of event names
124137
timings: false // `true`or list of event names
125138
},
126139
// Overide virtual methods for this extension.
127140
// `this` : is this Fancytree object
128141
// `this._super`: the virtual function that was overridden (member of prev. extension or Fancytree)
129-
treeInit: function(ctx) {
142+
treeCreate: function(ctx) {
143+
var tree = ctx.tree,
144+
opts = ctx.options;
145+
130146
if( this.options.extensions[this.options.extensions.length-1] !== "logger" ) {
131147
throw "Fancytree 'logger' extension must be listed as last entry.";
132148
}
133-
ctx.tree.warn("Fancytree logger extension is enabled (this may be slow).", ctx.options.logger);
149+
tree.warn("Fancytree logger extension is enabled (this may be slow).", opts.logger);
134150

135-
console.info("Fancytree v" + $.ui.fancytree.version + ", buildType='" + $.ui.fancytree.buildType + "'");
136-
console.info("jQuery UI " + jQuery.ui.version + " (uiBackCompat=" + $.uiBackCompat + ")");
137-
console.info("jQuery " + jQuery.fn.jquery);
138-
console.info("Browser: " + getBrowserInfo());
151+
tree.debug("Fancytree v" + $.ui.fancytree.version + ", buildType='" + $.ui.fancytree.buildType + "'");
152+
tree.debug("jQuery UI " + jQuery.ui.version + " (uiBackCompat=" + $.uiBackCompat + ")");
153+
tree.debug("jQuery " + jQuery.fn.jquery);
154+
tree.debug("Browser: " + getBrowserInfo());
139155

156+
function _log(event, data) {
157+
logLine(PREFIX + "event." + event.type + " (unhandled)", event, data);
158+
}
140159
$.each(EVENT_NAMES, function(i, name){
141-
// ctx.tree.overrideMethod(ctx.tree, name, logEvent);
142-
$.ui.fancytree.overrideMethod(ctx.options, name, logEvent);
143-
// $.ui.fancytree.overrideMethod(ctx.options, "createNode", function(event, data) {
144-
// // Default processing if any
145-
// this._super.apply(this, arguments);
146-
// // Add 'draggable' attribute
147-
// data.node.span.draggable = true;
148-
// });
160+
if( typeof opts[name] === "function" ) {
161+
// tree.info(PREFIX + "override '" + name + "' event");
162+
$.ui.fancytree.overrideMethod(opts, name, logEvent, ctx.widget);
163+
} else if ( opts.logger.traceUnhandledEvents ) {
164+
opts[name] = _log;
165+
}
149166
});
150167

151-
return logHook("treeInit", this, arguments);
168+
return logHook("treeCreate", this, arguments);
152169
},
153170
nodeClick: function(ctx) {
154171
return logHook("nodeClick", this, arguments, FT.eventToString(ctx.originalEvent));
@@ -204,12 +221,15 @@ $.ui.fancytree.registerExtension({
204221
treeClear: function(ctx) {
205222
return logHook("treeClear", this, arguments);
206223
},
207-
treeCreate: function(ctx) {
208-
return logHook("treeCreate", this, arguments);
209-
},
224+
// treeCreate: function(ctx) {
225+
// return logHook("treeCreate", this, arguments);
226+
// },
210227
treeDestroy: function(ctx) {
211228
return logHook("treeDestroy", this, arguments);
212229
},
230+
treeInit: function(ctx) {
231+
return logHook("treeInit", this, arguments);
232+
},
213233
treeLoad: function(ctx, source) {
214234
return logHook("treeLoad", this, arguments);
215235
},

src/jsdoc-globals.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,10 @@ var FancytreeOptions = {};
262262
* @property {function} click `data.node` was clicked. `data.targetType` contains the region ("checkbox", "expander", "icon", "prefix", "title"). Return `false` to prevent default processing, i.e. activating, expanding, selecting, etc.
263263
* @property {function} clickPaging `data.node` is a 'paging' status node and was activated. Use data.node.replaceWith() to load additional nodes.
264264
* @property {function} collapse `data.node` was collapsed
265-
* @property {function} create Widget was created (called only once, even if re-initialized).
265+
* @property {function} create Widget was created.<br>
266+
* Source data may *not* be loaded or rendered yet:
267+
* see also the `init` event, which is fired later.<br>
268+
* Note: called only once, but not when re-initialized.<br>
266269
* @property {function} createNode Allow tweaking and binding, after node was created for the first time (NOTE: this event is only available as callback, but not for bind())
267270
* @property {function} dblclick `data.node` was double-clicked. `data.targetType` contains the region ("checkbox", "expander", "icon", "prefix", "title"). Return `false` to prevent default processing, i.e. expanding, etc.
268271
* @property {function} deactivate `data.node` was deactivated
@@ -275,7 +278,8 @@ var FancytreeOptions = {};
275278
* @property {function} focusTree `data.tree` received keyboard focus
276279
* @property {function} <del>iconClass</del> @deprecated use tree option `icon` instead.
277280
* @property {function} init Widget was (re-)initialized.<br>
278-
* Note: if ext-persist is used, see also the `restore` event.
281+
* The tree widget was initialized, source data was loaded, and visible nodes are rendered.<br>
282+
* Note: if ext-persist is used, see also the `restore` event, which is fired later.
279283
* @property {function} keydown `data.node` received key. `event.which` contains the key. Return `false` to prevent default processing, i.e. navigation. Call `data.result = "preventNav";` to prevent navigation but still allow default handling inside embedded input controls.
280284
* @property {function} keypress (currently unused)
281285
* @property {function} lazyLoad `data.node` is a lazy node that is expanded for the first time. The new child data must be returned in the `data.result` property (see `source` option for available formats).

0 commit comments

Comments
 (0)