summaryrefslogtreecommitdiff
path: root/src/model/core.js
blob: 6111954b2f96dc4e58516894c9826a814a5dac7e (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
"use strict"

/**
 * core model module - currently unused
 */
define([], function() {

    /**
     * return a random id
     */
    var random_id = function() {
        return Math.random().toString(36).substring(2);
    }

    function random_node_name() {
        return Math.random().toString(36).substring(2, 10);
    }

    function Node() {
    }

    function Link() {
    }

    /**
     * the most flexible way to create a node: - perform spec field validation -
     * fill-in missing spec fields
     */
    function crete_node_from_spec(node_spec) {
        var ret = new Node();

        // name
        if (undefined == node_spec.name) {
            console.debug('crete_node_from_spec: undefined name, falling back to \"\"');
            node_spec.name = "";
        }
        ret.name = node_spec.name;

        // type
        if (undefined == node_spec.type) {
            console.debug('crete_node_from_spec: undefined type, falling back to \'empty\'');
            node_spec.type = 'empty';
        }
        ret.type = node_spec.type;

        // status
        if (undefined == node_spec.status) {
            node_spec.type = 'unknown';
        }
        ret.status = node_spec.status;

        // visual
        ret.x = node_spec.x;
        ret.y = node_spec.y;

        // other
        ret.state = node_spec.state;
        ret.url = node_spec.url;
        ret.start = node_spec.start;
        ret.end = node_spec.end;
        ret.x = node_spec.x;
        ret.y = node_spec.y;
        console.log(ret);

        return ret;
    }

    /**
     *
     */
    function create_node__set_random_id(node_spec) {
        if (undefined == node_spec) {
            node_spec = {};
        }

        var ret = crete_node_from_spec(node_spec);
        Object.defineProperty(ret, "id", {
            value: random_id(),
            writable: false
        });

        return ret;
    }

    function create_link__set_random_id(src, dst, link_spec) {
        var ret = crete_link_from_spec(src, dst, link_spec);
        Object.defineProperty(ret, "id", {
            value: random_id(),
            writable: false
        });
    }

    /**
     * determine if nodes are equal by name
     *
     * @param other_node
     * @returns {Boolean}
     */
    Node.prototype.equal_by_name = function(other) {
        ret = this.name.toLowerCase() == other.name.toLowerCase();
        if (false == ret) {
            console.debug(this.id + ' != ' + other.id);
        }
        return ret;
    }

    function Link() {

    }

    function crete_link_from_spec(src, dst, link_spec) {
        if (undefined == src) {
            console.error('crete_link_from_spec: undefined: src');
            return null;
        }
        if (undefined == dst) {
            console.error('crete_link_from_spec: undefined: dst');
            return null;
        }

        var ret = new Link();
        ret.__src = src;
        ret.__dst = dst;
        ret.__type = 'textual_link';

        if (undefined == link_spec.name){
            console.warn('crete_link_from_spec: name: ' + link_spec.name);
            link_spec.name = "";
        }
        ret.name = link_spec.name.trim();

        ret.state = link_spec.state;
        return ret;
    }

    /**
     * determine if links are equal by ID
     *
     * @param other_node
     * @returns {Boolean}
     */
    Link.prototype.equal_by_id = function(other) {
        return this.id.toLowerCase() == other.id.toLowerCase();
    }

    return {
        random_node_name : random_node_name,
        crete_node_from_spec : crete_node_from_spec,
        crete_link_from_spec : crete_link_from_spec,
        create_node__set_random_id : create_node__set_random_id,
        create_link__set_random_id : create_link__set_random_id,
    };
});