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