1. 1 : /**
  2. 2 : * Show contents in the results area. Will try to intelligently handle most types of content.
  3. 3 : * @memberof Spyral.Util
  4. 4 : * @method show
  5. 5 : * @static
  6. 6 : * @param {*} contents The contents to show
  7. 7 : * @param {Number} [len] A maximum length to trim the contents to
  8. 8 : * @param {String} [mode=info] A CSS class to apply to the shown contents
  9. 9 : */
  10. 10 : function show(contents, config = {}) {
  11. 11 : if (contents === undefined) {
  12. 12 : return;
  13. 13 : }
  14. 14 :
  15. 15 : if (document.querySelector('.spyral-dv-container') !== null) {
  16. 16 : document.querySelector('.spyral-dv-container').remove(); // get rid of dataviewer if it exists
  17. 17 : }
  18. 18 :
  19. 19 : if (contents.constructor === Object || Array.isArray(contents)) {
  20. 20 : return contents; // it's JSON so use the dataviewer
  21. 21 : }
  22. 22 :
  23. 23 : if (contents instanceof Node) {
  24. 24 : if (contents instanceof Element) {
  25. 25 : contents = contents.outerHTML;
  26. 26 : } else if ((contents instanceof Document || contents instanceof DocumentFragment) && contents.firstElementChild !== null) {
  27. 27 : if (contents.body) {
  28. 28 : contents = contents.body;
  29. 29 : }
  30. 30 : contents = contents.firstElementChild.outerHTML;
  31. 31 : }
  32. 32 : }
  33. 33 : if (contents.then) { // check if we currently have a promise
  34. 34 : return contents.then(function(text) {show(text, config)})
  35. 35 : }
  36. 36 : if (contents.toHtml) {contents=contents.toHtml()}
  37. 37 : else if (contents.getString) {contents=contents.getString()}
  38. 38 : else if (contents.toString) {contents=contents.toString()}
  39. 39 :
  40. 40 : if (contents.then) { // check again to see if we have a promise (like from toString())
  41. 41 : contents.then(function(text) {show(text, config)})
  42. 42 : } else {
  43. 43 : if (config['class'] === undefined) config['class'] = "info";
  44. 44 : var html = "<div";
  45. 45 : Object.keys(config).forEach(key => {
  46. 46 : var value = config[key];
  47. 47 : html += ` ${key}="${value}"`;
  48. 48 : })
  49. 49 : html += `>${contents}</div>`;
  50. 50 : document.body.insertAdjacentHTML('beforeend', html);
  51. 51 : }
  52. 52 : }
  53. 53 :
  54. 54 : /**
  55. 55 : * Show an error in the results area.
  56. 56 : * @memberof Spyral.Util
  57. 57 : * @method showError
  58. 58 : * @static
  59. 59 : * @param {*} error An Error to display
  60. 60 : * @param {*} [more] Additional Error details
  61. 61 : */
  62. 62 : function showError(error, more) {
  63. 63 : if (error !== undefined && error instanceof Error) {
  64. 64 : if (error.stack && more === undefined) {
  65. 65 : more = error.stack;
  66. 66 : }
  67. 67 : // trim excess error stack (it's likely already in "more")
  68. 68 : error = error.toString().split(/(\r\n|\r|\n)/).shift();
  69. 69 : }
  70. 70 :
  71. 71 : if (more && typeof more !== 'string' && more instanceof String === false) {
  72. 72 : more = more.toString();
  73. 73 : }
  74. 74 :
  75. 75 : if (console) {console.error(error)}
  76. 76 : if (more) {
  77. 77 : var encodedMore = more.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&apos;')
  78. 78 : error='<strong>'+error.toString()+'</strong><pre><span style="cursor:pointer;text-decoration:underline;" onclick="this.nextElementSibling.style.display=\'block\';this.style.display=\'none\';">Details</span><span style="display:none;">'+encodedMore+'</span></pre>';
  79. 79 : }
  80. 80 : show(error, {class: 'error'});
  81. 81 : }
  82. 82 :
  83. 83 : export {show, showError};