aboutsummaryrefslogtreecommitdiffstats
path: root/app/comp-fe/catalog.js
blob: dc7006e8067976012c9ff904b91987b1109f9e56 (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
setTimeout(function () {
    sbox = document.getElementById('searchbox');
    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 = "";

function catalogbyname(pattern) {
    xhrget(catalogprefix + "/byname?" + pattern,
        function (req) {
            var res = JSON.parse(req);
            var list = d3.select("#catalogitemlist");
            res.result.map(function (item) {
                var div = list.append("div");

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

                })();

            });
        });
}

function catalogitem(id, fn) {
    var nn = 0;
    xhrget(catalogprefix + "/itembyid?" + id, function (resp) {
        var x = JSON.parse(resp);
        if (x.models && x.models.length > 0) {
            x.models.map(function (w) {
                nn += w.nodes.length;
                w.nodes.map(function (n) {
                    if (n.id == 0) { //  dummy node special case
                        if (--nn == 0)
                            fn(x);
                    } else {
                        xhrget(catalogprefix + "/type?" + n.type.name, function (resp) {
                            var ti = JSON.parse(resp);
                            n.typeinfo = ti;
                            if (--nn == 0)
                                fn(x);
                        });
                    }
                });
            });
        } else {
            fn(x);
        }
    });
}

function catalogtype(typename, fn) {
    xhrget(catalogprefix + "/type?" + typename, fn);
}

/* e.g.
 catalogtype(n.type.name, function(resp) {
 var ti = JSON.parse(resp);
 n.typeinfo = ti;
 addnode(n);
 });
 */


function log(x, y) {
    var args = ["catalog:"].concat(Array.prototype.slice.call(arguments, 0));
    console.log.apply(console, args);
}