mxLog.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. var mxLog =
  6. {
  7. /**
  8. * Class: mxLog
  9. *
  10. * A singleton class that implements a simple console.
  11. *
  12. * Variable: consoleName
  13. *
  14. * Specifies the name of the console window. Default is 'Console'.
  15. */
  16. consoleName: 'Console',
  17. /**
  18. * Variable: TRACE
  19. *
  20. * Specified if the output for <enter> and <leave> should be visible in the
  21. * console. Default is false.
  22. */
  23. TRACE: false,
  24. /**
  25. * Variable: DEBUG
  26. *
  27. * Specifies if the output for <debug> should be visible in the console.
  28. * Default is true.
  29. */
  30. DEBUG: true,
  31. /**
  32. * Variable: WARN
  33. *
  34. * Specifies if the output for <warn> should be visible in the console.
  35. * Default is true.
  36. */
  37. WARN: true,
  38. /**
  39. * Variable: buffer
  40. *
  41. * Buffer for pre-initialized content.
  42. */
  43. buffer: '',
  44. /**
  45. * Function: init
  46. *
  47. * Initializes the DOM node for the console. This requires document.body to
  48. * point to a non-null value. This is called from within <setVisible> if the
  49. * log has not yet been initialized.
  50. */
  51. init: function()
  52. {
  53. if (mxLog.window == null && document.body != null)
  54. {
  55. var title = mxLog.consoleName + ' - mxGraph ' + mxClient.VERSION;
  56. // Creates a table that maintains the layout
  57. var table = document.createElement('table');
  58. table.setAttribute('width', '100%');
  59. table.setAttribute('height', '100%');
  60. var tbody = document.createElement('tbody');
  61. var tr = document.createElement('tr');
  62. var td = document.createElement('td');
  63. td.style.verticalAlign = 'top';
  64. // Adds the actual console as a textarea
  65. mxLog.textarea = document.createElement('textarea');
  66. mxLog.textarea.setAttribute('wrap', 'off');
  67. mxLog.textarea.setAttribute('readOnly', 'true');
  68. mxLog.textarea.style.height = '100%';
  69. mxLog.textarea.style.resize = 'none';
  70. mxLog.textarea.value = mxLog.buffer;
  71. // Workaround for wrong width in standards mode
  72. if (mxClient.IS_NS && document.compatMode != 'BackCompat')
  73. {
  74. mxLog.textarea.style.width = '99%';
  75. }
  76. else
  77. {
  78. mxLog.textarea.style.width = '100%';
  79. }
  80. td.appendChild(mxLog.textarea);
  81. tr.appendChild(td);
  82. tbody.appendChild(tr);
  83. // Creates the container div
  84. tr = document.createElement('tr');
  85. mxLog.td = document.createElement('td');
  86. mxLog.td.style.verticalAlign = 'top';
  87. mxLog.td.setAttribute('height', '30px');
  88. tr.appendChild(mxLog.td);
  89. tbody.appendChild(tr);
  90. table.appendChild(tbody);
  91. // Adds various debugging buttons
  92. mxLog.addButton('Info', function (evt)
  93. {
  94. mxLog.info();
  95. });
  96. mxLog.addButton('DOM', function (evt)
  97. {
  98. var content = mxUtils.getInnerHtml(document.body);
  99. mxLog.debug(content);
  100. });
  101. mxLog.addButton('Trace', function (evt)
  102. {
  103. mxLog.TRACE = !mxLog.TRACE;
  104. if (mxLog.TRACE)
  105. {
  106. mxLog.debug('Tracing enabled');
  107. }
  108. else
  109. {
  110. mxLog.debug('Tracing disabled');
  111. }
  112. });
  113. mxLog.addButton('Copy', function (evt)
  114. {
  115. try
  116. {
  117. mxUtils.copy(mxLog.textarea.value);
  118. }
  119. catch (err)
  120. {
  121. mxUtils.alert(err);
  122. }
  123. });
  124. mxLog.addButton('Show', function (evt)
  125. {
  126. try
  127. {
  128. mxUtils.popup(mxLog.textarea.value);
  129. }
  130. catch (err)
  131. {
  132. mxUtils.alert(err);
  133. }
  134. });
  135. mxLog.addButton('Clear', function (evt)
  136. {
  137. mxLog.textarea.value = '';
  138. });
  139. // Cross-browser code to get window size
  140. var h = 0;
  141. var w = 0;
  142. if (typeof(window.innerWidth) === 'number')
  143. {
  144. h = window.innerHeight;
  145. w = window.innerWidth;
  146. }
  147. else
  148. {
  149. h = (document.documentElement.clientHeight || document.body.clientHeight);
  150. w = document.body.clientWidth;
  151. }
  152. mxLog.window = new mxWindow(title, table, Math.max(0, w - 320), Math.max(0, h - 210), 300, 160);
  153. mxLog.window.setMaximizable(true);
  154. mxLog.window.setScrollable(false);
  155. mxLog.window.setResizable(true);
  156. mxLog.window.setClosable(true);
  157. mxLog.window.destroyOnClose = false;
  158. // Workaround for ignored textarea height in various setups
  159. if (((mxClient.IS_NS || mxClient.IS_IE) && !mxClient.IS_GC &&
  160. !mxClient.IS_SF && document.compatMode != 'BackCompat') ||
  161. document.documentMode == 11)
  162. {
  163. var elt = mxLog.window.getElement();
  164. var resizeHandler = function(sender, evt)
  165. {
  166. mxLog.textarea.style.height = Math.max(0, elt.offsetHeight - 70) + 'px';
  167. };
  168. mxLog.window.addListener(mxEvent.RESIZE_END, resizeHandler);
  169. mxLog.window.addListener(mxEvent.MAXIMIZE, resizeHandler);
  170. mxLog.window.addListener(mxEvent.NORMALIZE, resizeHandler);
  171. mxLog.textarea.style.height = '92px';
  172. }
  173. }
  174. },
  175. /**
  176. * Function: info
  177. *
  178. * Writes the current navigator information to the console.
  179. */
  180. info: function()
  181. {
  182. mxLog.writeln(mxUtils.toString(navigator));
  183. },
  184. /**
  185. * Function: addButton
  186. *
  187. * Adds a button to the console using the given label and function.
  188. */
  189. addButton: function(lab, funct)
  190. {
  191. var button = document.createElement('button');
  192. mxUtils.write(button, lab);
  193. mxEvent.addListener(button, 'click', funct);
  194. mxLog.td.appendChild(button);
  195. },
  196. /**
  197. * Function: isVisible
  198. *
  199. * Returns true if the console is visible.
  200. */
  201. isVisible: function()
  202. {
  203. if (mxLog.window != null)
  204. {
  205. return mxLog.window.isVisible();
  206. }
  207. return false;
  208. },
  209. /**
  210. * Function: show
  211. *
  212. * Shows the console.
  213. */
  214. show: function()
  215. {
  216. mxLog.setVisible(true);
  217. },
  218. /**
  219. * Function: setVisible
  220. *
  221. * Shows or hides the console.
  222. */
  223. setVisible: function(visible)
  224. {
  225. if (mxLog.window == null)
  226. {
  227. mxLog.init();
  228. }
  229. if (mxLog.window != null)
  230. {
  231. mxLog.window.setVisible(visible);
  232. }
  233. },
  234. /**
  235. * Function: enter
  236. *
  237. * Writes the specified string to the console
  238. * if <TRACE> is true and returns the current
  239. * time in milliseconds.
  240. *
  241. * Example:
  242. *
  243. * (code)
  244. * mxLog.show();
  245. * var t0 = mxLog.enter('Hello');
  246. * // Do something
  247. * mxLog.leave('World!', t0);
  248. * (end)
  249. */
  250. enter: function(string)
  251. {
  252. if (mxLog.TRACE)
  253. {
  254. mxLog.writeln('Entering '+string);
  255. return new Date().getTime();
  256. }
  257. },
  258. /**
  259. * Function: leave
  260. *
  261. * Writes the specified string to the console
  262. * if <TRACE> is true and computes the difference
  263. * between the current time and t0 in milliseconds.
  264. * See <enter> for an example.
  265. */
  266. leave: function(string, t0)
  267. {
  268. if (mxLog.TRACE)
  269. {
  270. var dt = (t0 != 0) ? ' ('+(new Date().getTime() - t0)+' ms)' : '';
  271. mxLog.writeln('Leaving '+string+dt);
  272. }
  273. },
  274. /**
  275. * Function: debug
  276. *
  277. * Adds all arguments to the console if <DEBUG> is enabled.
  278. *
  279. * Example:
  280. *
  281. * (code)
  282. * mxLog.show();
  283. * mxLog.debug('Hello, World!');
  284. * (end)
  285. */
  286. debug: function()
  287. {
  288. if (mxLog.DEBUG)
  289. {
  290. mxLog.writeln.apply(this, arguments);
  291. }
  292. },
  293. /**
  294. * Function: warn
  295. *
  296. * Adds all arguments to the console if <WARN> is enabled.
  297. *
  298. * Example:
  299. *
  300. * (code)
  301. * mxLog.show();
  302. * mxLog.warn('Hello, World!');
  303. * (end)
  304. */
  305. warn: function()
  306. {
  307. if (mxLog.WARN)
  308. {
  309. mxLog.writeln.apply(this, arguments);
  310. }
  311. },
  312. /**
  313. * Function: write
  314. *
  315. * Adds the specified strings to the console.
  316. */
  317. write: function()
  318. {
  319. var string = '';
  320. for (var i = 0; i < arguments.length; i++)
  321. {
  322. string += arguments[i];
  323. if (i < arguments.length - 1)
  324. {
  325. string += ' ';
  326. }
  327. }
  328. if (mxLog.textarea != null)
  329. {
  330. mxLog.textarea.value = mxLog.textarea.value + string;
  331. // Workaround for no update in Presto 2.5.22 (Opera 10.5)
  332. if (navigator.userAgent != null &&
  333. navigator.userAgent.indexOf('Presto/2.5') >= 0)
  334. {
  335. mxLog.textarea.style.visibility = 'hidden';
  336. mxLog.textarea.style.visibility = 'visible';
  337. }
  338. mxLog.textarea.scrollTop = mxLog.textarea.scrollHeight;
  339. }
  340. else
  341. {
  342. mxLog.buffer += string;
  343. }
  344. },
  345. /**
  346. * Function: writeln
  347. *
  348. * Adds the specified strings to the console, appending a linefeed at the
  349. * end of each string.
  350. */
  351. writeln: function()
  352. {
  353. var string = '';
  354. for (var i = 0; i < arguments.length; i++)
  355. {
  356. string += arguments[i];
  357. if (i < arguments.length - 1)
  358. {
  359. string += ' ';
  360. }
  361. }
  362. mxLog.write(string + '\n');
  363. }
  364. };
  365. __mxOutput.mxLog = typeof mxLog !== 'undefined' ? mxLog : undefined;