xref: /plugin/combo/resources/webcode/webcode-console.js (revision 37748cd8654635afbeca80942126742f0f4cc346)
1/*
2 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved.
3 *
4 * This source code is licensed under the GPL license found in the
5 * COPYING  file in the root directory of this source tree.
6 *
7 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
8 * @author   ComboStrap <support@combostrap.com>
9 *
10 */
11
12/**
13 * Created by NicolasGERARD on 11/18/2015.
14 */
15let WEBCODE = {
16    appendLine: function (text) {
17        let webConsoleLine = document.createElement("p");
18        webConsoleLine.className = "webCodeConsoleLine";
19        webConsoleLine.innerHTML = text;
20        WEBCODE.appendChild(webConsoleLine);
21    },
22    appendChild: function (element) {
23        document.querySelector("#webCodeConsole").appendChild(element);
24    },
25    print: function (v) {
26        if (typeof v === 'undefined') {
27            return "(Undefined)"; // Undefined == null, therefore it must be in first position
28        } else if (Array.isArray(v)) {
29            if (v.length === 0) {
30                return "(Empty Array)";
31            } else {
32                return v;
33            }
34        } else if (typeof v === 'string') {
35            if (v.length === 0) {
36                return "(Empty String)"
37            } else {
38                return v;
39            }
40        } else if (v === null) {
41            return "(null)";
42        } else {
43            return v;
44        }
45    },
46    htmlEntities: function(str) {
47        // from https://css-tricks.com/snippets/javascript/htmlentities-for-javascript/
48        return String(str)
49            .replace(/&/g, '&amp;')
50            .replace(/</g, '&lt;')
51            .replace(/>/g, '&gt;')
52            .replace(/"/g, '&quot;')
53            .replace(/ /g, '&nbsp;');
54    }
55};
56
57
58window.console.log = function (input) {
59    let s = "";
60    if (typeof input === "object") {
61        s = "{\n";
62        let keys = Object.keys(input);
63        for (let i = 0; i < keys.length; i++) {
64            s += "  " + keys[i] + " : " + input[keys[i]] + ";\n";
65        }
66        s += "}\n";
67    } else {
68        s = String(input);
69    }
70    s = WEBCODE.htmlEntities(s);
71    // the BR replacement must be after the htmlEntities function ...
72    s = s.replace(/\n/g, '<BR>')
73    WEBCODE.appendLine(s);
74};
75
76// Console table implementation
77// https://developer.mozilla.org/en-US/docs/Web/API/Console/table
78window.console.table = function (input) {
79    if (Array.isArray(input) !== true) {
80
81        WEBCODE.appendLine("The variable of the function console.table must be an array.");
82
83    } else {
84        if (input.length <= 0) {
85
86            WEBCODE.appendLine("The variable of the console.table has no elements.");
87
88        } else {
89            // HTML Headers
90            let tableElement = document.createElement("table");
91            let theadElement = document.createElement("thead");
92            let tbodyElement = document.createElement("tbody");
93            let trHeadElement = document.createElement("tr");
94
95            tableElement.appendChild(theadElement);
96            tableElement.appendChild(tbodyElement);
97            theadElement.appendChild(trHeadElement);
98
99
100            for (let i = 0; i < input.length; i++) {
101
102                let element = input[i];
103
104                // First iteration, we pick the headers
105                if (i === 0) {
106
107                    if (typeof element === 'object') {
108                        for (let prop in element) {
109                            let thElement = document.createElement("th");
110                            thElement.innerHTML = WEBCODE.print(prop);
111                            trHeadElement.appendChild(thElement);
112                        }
113                    } else {
114                        // Header
115                        let thElement = document.createElement("th");
116                        thElement.innerHTML = "Values";
117                        trHeadElement.appendChild(thElement);
118                    }
119
120                }
121
122                let trBodyElement = document.createElement("tr");
123                tbodyElement.appendChild(trBodyElement);
124
125                if (typeof input[0] === 'object') {
126                    for (let prop in element) {
127                        let tdElement = document.createElement("td");
128                        tdElement.innerHTML = WEBCODE.print(element[prop]);
129                        trBodyElement.appendChild(tdElement);
130                    }
131                } else {
132                    let tdElement = document.createElement("td");
133                    tdElement.innerHTML = WEBCODE.print(element);
134                    let trElement = document.createElement("tr");
135                    trElement.appendChild(tdElement);
136                }
137
138            }
139            WEBCODE.appendChild(tableElement);
140
141        }
142    }
143};
144
145