aboutsummaryrefslogtreecommitdiffstats
path: root/app/comp-fe/asdc-catalog.js
blob: 334a6d7b2ab7a9d5b47e38bdb4a35f3d1523c75b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var log = log || function (x, y) {
    var args = ["asdc-catalog:"].concat(Array.prototype.slice.call(arguments, 0));
    console.log.apply(console, args);
}

setTimeout(function () {
    sbox = document.getElementById('searchbox');
    if (sbox) {
        sbox.onkeypress = function (e) {
            if (!e) e = window.event;
            var keyCode = e.keyCode || e.which;
            if (keyCode == '13') {
                dosearch(sbox.value);
                return false;
            }
        };
    }
}, 1000);

function dosearch() {
    input = document.getElementById("searchbox");
    catalogbyname(".*(" + input.value + ").*");
}

var catalogprefix = window.catalogPrefix;

function elements() {
    // log("elements");
    var url = "/catalog";
    xhrget(url,
        function (req) {
            d3.select("#elementquery").remove();
            var res = JSON.parse(req);
            var list = d3.select("#catalogitemlist");
            res.elements.map(function (item) {
                (function () {
                    var div = list.append("div");
                    var name = item.name;
                    div.classed("folder", true)
                        .html("<b data-tests-id=" + name + ">" + name + "</b>")
                        .append("br");
                    div.on("click", function () {
                        div.on("click", null); // once only
                        items(item, div);
                    });
                })();
            });
        });
}

function items(item, parent) {

    var items = item.items;
    items.map(function (item) {
        var _item = item;
        (function () {
            var div = parent.append("div");
            var name = _item.name;
            log("ITEM", name, _item);

            div.classed("item", true)
                .attr('data-tests-id', name)
                .html(name);

            parent.append("div").style("clear", "both").style("height", 0);
            div.on("click", function () {
                dropdata(_item);
            });

            div.attr("draggable", true)
                .classed("draggable", true);
            div.node().addEventListener('dragstart', function (e) {
                e.dataTransfer.effectAllowed = 'move';
                e.dataTransfer.setData('product', JSON.stringify(_item));
            });
        })();
    });

}

function catalogitem(id, fn) {
    var nn = 0;
    var url = "/" + id + "/model";
    log("catalogitem:", url);
    xhrget(url, function (resp) {
        var x = JSON.parse(resp);
        if (!_.isEmpty(x.error)) {
            log("catalogitem error", x.error);
            return;
        }
        if (_.isEmpty(x.data)) {
            log("catalogitem NO DATA");
            return;
        }

        x = x.data; // HACK

        var model = x.model;
        log("catalogitem", model.nodes);

        if (!x.models) {
            log("HACK models");
            x.models = [x.model];
        }

        nn += model.nodes.length;
        model.nodes.map(function (n) {
            catalogtype(id, n.type, function (resp) {
                var ti = JSON.parse(resp);
                n.typeinfo = ti;

                // patch things up (HACK)
                log("HACK typeinfo");
                n.typeinfo = n.typeinfo.data.type;
                if (!n.typeinfo.requirements)
                    n.typeinfo.requirements = [];
                if (!n.typeinfo.capabilities)
                    n.typeinfo.capabilities = [];

                if (--nn == 0)
                    fn(x);
            });
        });
    });
}

function catalogitem00(id, fn) {
    var nn = 0;
    var url = "/" + id + "/model";
    log("catalogitem:", url);
    xhrget(url, function (resp) {
        var x = JSON.parse(resp);
        var model = x.data.model;
        //model.id = id;
        model.name = "foo"; // HACK
        log("catalogitem", id, x, model);
        fn(model);
    });
}

function xhrget(url, callback) {
    var req = new XMLHttpRequest;
    console.log(url);
    // debugger;
    url = catalogprefix + url;
    // if (!(url.startsWith("https://") || url.startsWith("http://")))
    //     url = catalogprefix + url;
    // console.log(url);
    req.open("GET", url, true); // asynchronous request
    req.onreadystatechange = function (x) {
        if (req.readyState === 4)
            callback(req.responseText);
    };
    req.send(null);
}

function catalogtype(id, type, fn) {
    var url = "/" + id + "/type/" + type + "/";
    xhrget(url, function (resp) {
        //log("catalogtype", id, type, resp);
        fn(resp);
    });
}

setTimeout(elements, 500);