You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

310 lines
8.6 KiB

  1. /*!
  2. * jquery.fancytree.logger.js
  3. *
  4. * Miscellaneous debug extensions.
  5. * (Extension module for jquery.fancytree.js: https://github.com/mar10/fancytree/)
  6. *
  7. * Copyright (c) 2008-2023, Martin Wendt (https://wwWendt.de)
  8. *
  9. * Released under the MIT license
  10. * https://github.com/mar10/fancytree/wiki/LicenseInfo
  11. *
  12. * @version 2.38.3
  13. * @date 2023-02-01T20:52:50Z
  14. */
  15. (function (factory) {
  16. if (typeof define === "function" && define.amd) {
  17. // AMD. Register as an anonymous module.
  18. define(["jquery", "./jquery.fancytree"], factory);
  19. } else if (typeof module === "object" && module.exports) {
  20. // Node/CommonJS
  21. require("./jquery.fancytree");
  22. module.exports = factory(require("jquery"));
  23. } else {
  24. // Browser globals
  25. factory(jQuery);
  26. }
  27. })(function ($) {
  28. "use strict";
  29. /******************************************************************************
  30. * Private functions and variables
  31. */
  32. var i,
  33. FT = $.ui.fancytree,
  34. PREFIX = "ft-logger: ",
  35. logLine = window.console.log,
  36. // HOOK_NAMES = "nodeClick nodeCollapseSiblings".split(" "),
  37. TREE_EVENT_NAMES =
  38. "beforeRestore beforeUpdateViewport blurTree create init focusTree preInit restore updateViewport".split(
  39. " "
  40. ),
  41. NODE_EVENT_NAMES =
  42. "activate activateCell beforeActivate beforeExpand beforeSelect blur click collapse createNode dblclick deactivate defaultGridAction expand enhanceTitle focus keydown keypress lazyLoad loadChildren loadError modifyChild postProcess renderNode renderTitle select".split(
  43. " "
  44. ),
  45. EVENT_NAMES = TREE_EVENT_NAMES.concat(NODE_EVENT_NAMES),
  46. // HOOK_NAME_MAP = {},
  47. EVENT_NAME_MAP = {};
  48. /*
  49. */
  50. // for (i = 0; i < HOOK_NAMES.length; i++) {
  51. // HOOK_NAME_MAP[HOOK_NAMES[i]] = true;
  52. // }
  53. for (i = 0; i < EVENT_NAMES.length; i++) {
  54. EVENT_NAME_MAP[EVENT_NAMES[i]] = true;
  55. }
  56. function getBrowserInfo() {
  57. var n = navigator.appName,
  58. ua = navigator.userAgent,
  59. tem,
  60. m = ua.match(
  61. /(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i
  62. );
  63. if (m && (tem = ua.match(/version\/([.\d]+)/i)) !== null) {
  64. m[2] = tem[1];
  65. }
  66. m = m ? [m[1], m[2]] : [n, navigator.appVersion, "-?"];
  67. return m.join(", ");
  68. }
  69. function logEvent(event, data) {
  70. var res,
  71. self = this,
  72. // logName = PREFIX + "event." + event.type,
  73. opts = data.options.logger,
  74. tree = data.tree,
  75. // widget = data.widget,
  76. obj = data.node || tree,
  77. logName = PREFIX + "event." + event.type + " (" + obj + ")";
  78. if (
  79. !opts.traceEvents ||
  80. (opts.traceEvents !== true && $.inArray(name, opts.traceEvents) < 0)
  81. ) {
  82. return self._super.apply(self, arguments);
  83. }
  84. if (
  85. (self._super && opts.timings === true) ||
  86. (opts.timings && $.inArray(name, opts.timings) >= 0)
  87. ) {
  88. // if( name === "nodeRender" ) { logName += obj; } // allow timing for recursive calls
  89. // logName += " (" + obj + ")";
  90. window.console.time(logName);
  91. res = self._super.apply(self, arguments);
  92. window.console.timeEnd(logName);
  93. } else {
  94. // obj.info(logName, data);
  95. logLine(logName, event, data);
  96. res = self._super.apply(self, arguments);
  97. }
  98. return res;
  99. }
  100. function logHook(name, this_, args, extra) {
  101. var res,
  102. ctx = args[0],
  103. opts = ctx.options.logger,
  104. obj = ctx.node || ctx.tree,
  105. logName = PREFIX + "hook." + name + " (" + obj + ")";
  106. if (
  107. !opts.traceHooks ||
  108. (opts.traceHooks !== true && $.inArray(name, opts.traceHooks) < 0)
  109. ) {
  110. return this_._superApply.call(this_, args);
  111. }
  112. if (
  113. opts.timings === true ||
  114. (opts.timings && $.inArray(name, opts.timings) >= 0)
  115. ) {
  116. // if( name === "nodeRender" ) { logName += obj; } // allow timing for recursive calls
  117. // logName += " (" + obj + ")";
  118. window.console.time(logName);
  119. res = this_._superApply.call(this_, args);
  120. window.console.timeEnd(logName);
  121. } else {
  122. if (extra) {
  123. // obj.info(logName, extra, ctx);
  124. logLine(logName, extra, ctx);
  125. } else {
  126. // obj.info(logName, ctx);
  127. logLine(logName, ctx);
  128. }
  129. res = this_._superApply.call(this_, args);
  130. }
  131. return res;
  132. }
  133. /******************************************************************************
  134. * Extension code
  135. */
  136. $.ui.fancytree.registerExtension({
  137. name: "logger",
  138. version: "2.38.3",
  139. // Default options for this extension.
  140. options: {
  141. logTarget: null, // optional redirect logging to this <div> tag
  142. traceEvents: true, // `true`or list of hook names
  143. traceUnhandledEvents: false,
  144. traceHooks: false, // `true`or list of event names
  145. timings: false, // `true`or list of event names
  146. },
  147. // Overide virtual methods for this extension.
  148. // `this` : is this Fancytree object
  149. // `this._super`: the virtual function that was overridden (member of prev. extension or Fancytree)
  150. treeCreate: function (ctx) {
  151. var tree = ctx.tree,
  152. opts = ctx.options;
  153. if (
  154. this.options.extensions[this.options.extensions.length - 1] !==
  155. "logger"
  156. ) {
  157. throw Error(
  158. "Fancytree 'logger' extension must be listed as last entry."
  159. );
  160. }
  161. tree.warn(
  162. "Fancytree logger extension is enabled (this may be slow).",
  163. opts.logger
  164. );
  165. tree.debug(
  166. "Fancytree v" +
  167. $.ui.fancytree.version +
  168. ", buildType='" +
  169. $.ui.fancytree.buildType +
  170. "'"
  171. );
  172. tree.debug(
  173. "jQuery UI " +
  174. jQuery.ui.version +
  175. " (uiBackCompat=" +
  176. $.uiBackCompat +
  177. ")"
  178. );
  179. tree.debug("jQuery " + jQuery.fn.jquery);
  180. tree.debug("Browser: " + getBrowserInfo());
  181. function _log(event, data) {
  182. logLine(
  183. PREFIX + "event." + event.type + " (unhandled)",
  184. event,
  185. data
  186. );
  187. }
  188. $.each(EVENT_NAMES, function (i, name) {
  189. if (typeof opts[name] === "function") {
  190. // tree.info(PREFIX + "override '" + name + "' event");
  191. $.ui.fancytree.overrideMethod(
  192. opts,
  193. name,
  194. logEvent,
  195. ctx.widget
  196. );
  197. } else if (opts.logger.traceUnhandledEvents) {
  198. opts[name] = _log;
  199. }
  200. });
  201. return logHook("treeCreate", this, arguments);
  202. },
  203. nodeClick: function (ctx) {
  204. return logHook(
  205. "nodeClick",
  206. this,
  207. arguments,
  208. FT.eventToString(ctx.originalEvent)
  209. );
  210. },
  211. nodeCollapseSiblings: function (ctx) {
  212. return logHook("nodeCollapseSiblings", this, arguments);
  213. },
  214. nodeDblclick: function (ctx) {
  215. return logHook("nodeDblclick", this, arguments);
  216. },
  217. nodeKeydown: function (ctx) {
  218. return logHook(
  219. "nodeKeydown",
  220. this,
  221. arguments,
  222. FT.eventToString(ctx.originalEvent)
  223. );
  224. },
  225. nodeLoadChildren: function (ctx, source) {
  226. return logHook("nodeLoadChildren", this, arguments);
  227. },
  228. nodeRemoveChildMarkup: function (ctx) {
  229. return logHook("nodeRemoveChildMarkup", this, arguments);
  230. },
  231. nodeRemoveMarkup: function (ctx) {
  232. return logHook("nodeRemoveMarkup", this, arguments);
  233. },
  234. nodeRender: function (ctx, force, deep, collapsed, _recursive) {
  235. return logHook("nodeRender", this, arguments);
  236. },
  237. nodeRenderStatus: function (ctx) {
  238. return logHook("nodeRenderStatus", this, arguments);
  239. },
  240. nodeRenderTitle: function (ctx, title) {
  241. return logHook("nodeRenderTitle", this, arguments);
  242. },
  243. nodeSetActive: function (ctx, flag, callOpts) {
  244. return logHook("nodeSetActive", this, arguments);
  245. },
  246. nodeSetExpanded: function (ctx, flag, callOpts) {
  247. return logHook("nodeSetExpanded", this, arguments);
  248. },
  249. nodeSetFocus: function (ctx) {
  250. return logHook("nodeSetFocus", this, arguments);
  251. },
  252. nodeSetSelected: function (ctx, flag, callOpts) {
  253. return logHook("nodeSetSelected", this, arguments);
  254. },
  255. nodeSetStatus: function (ctx, status, message, details) {
  256. return logHook("nodeSetStatus", this, arguments);
  257. },
  258. nodeToggleExpanded: function (ctx) {
  259. return logHook("nodeToggleExpanded", this, arguments);
  260. },
  261. nodeToggleSelected: function (ctx) {
  262. return logHook("nodeToggleSelected", this, arguments);
  263. },
  264. treeClear: function (ctx) {
  265. return logHook("treeClear", this, arguments);
  266. },
  267. // treeCreate: function(ctx) {
  268. // return logHook("treeCreate", this, arguments);
  269. // },
  270. treeDestroy: function (ctx) {
  271. return logHook("treeDestroy", this, arguments);
  272. },
  273. treeInit: function (ctx) {
  274. return logHook("treeInit", this, arguments);
  275. },
  276. treeLoad: function (ctx, source) {
  277. return logHook("treeLoad", this, arguments);
  278. },
  279. treeRegisterNode: function (ctx, add, node) {
  280. return logHook("treeRegisterNode", this, arguments);
  281. },
  282. treeSetFocus: function (ctx, flag, callOpts) {
  283. return logHook("treeSetFocus", this, arguments);
  284. },
  285. treeSetOption: function (ctx, key, value) {
  286. return logHook("treeSetOption", this, arguments);
  287. },
  288. treeStructureChanged: function (ctx, type) {
  289. return logHook("treeStructureChanged", this, arguments);
  290. },
  291. });
  292. // Value returned by `require('jquery.fancytree..')`
  293. return $.ui.fancytree;
  294. }); // End of closure