summaryrefslogtreecommitdiff
path: root/src/client/textanalysis.ui.js
blob: 13c861d0bd4728124f6500c09fa87f596a926d3d (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"use strict"

define(['jquery', 'Bacon', 'consts', 'rz_bus', 'rz_core', 'textanalysis', 'util', 'view/textanalyser_input', 'model/types'],
function($,        Bacon ,  consts ,  rz_bus ,  rz_core ,  textanalysis ,  util,        textanalyser_input,   model_types) {

var element_name = '#textanalyser',
    input = textanalyser_input({
        'element_name': element_name,
        'completer_name': '#input-suggestion',
        }),
    plus_button = $('#btn_add'),
    description = consts.description,
    input_bus = new Bacon.Bus(),
    plus_button_initial_offset = plus_button.offset();

function get_svg__body_position(node_id)
{
    var item = document.getElementById(node_id),
        matrix = item.getScreenCTM();
    return {x: matrix.e, y: matrix.f};
}

var typeselection = function TypeSelectionDialog() {
    var fo = document.createElementNS('http://www.w3.org/2000/svg', 'foreignObject'),
        e = $('.type_selection'),
        e_intro = e.find('#type_selection__intro'),
        e_label = e.find('#type_selection__chosen_type_label'),
        e_name = e.find('#type_selection__chosen_type_name'),
        e_desc = e.find('#type_selection__chosen_type_desc'),
        typeselection = {};

    fo.setAttribute('width', 400);
    fo.setAttribute('height', 200);

    fo.appendChild(e[0]);
    typeselection.analysisNodeStart = function(node_id) {
        typeselection.show(node_id);
    }
    function attach_to(node_id) {
        var node = document.getElementById(node_id);

        if (node === undefined) {
            console.log('cannot show type selection since node does not exist: ' + node_id);
            return;
        }
        node.appendChild(fo);
    }
    typeselection.show = function(node_id) {
        if (document.getElementById(node_id) === null) {
            return;
        }
        attach_to(node_id);
        e_intro.show();
        e_label.show();
        e_desc.show();
        e_name.html(model_types.node_titles[textanalysis.selected_type()]);
        e.show();
    }
    typeselection.hide = function() {
        e.hide();
    }
    typeselection.showChosenType = function(node_id, nodetype) {
        var desc = description[nodetype];

        if (document.getElementById(node_id) === null) {
            return;
        }
        attach_to(node_id);
        e_intro.hide();
        e_label.show();
        e_name.html(model_types.node_titles[nodetype]);
        if (desc) {
            e_desc.html(description[nodetype]);
            e_desc.show();
        } else {
            e_desc.hide();
        }
    }
    return typeselection;
}();

function analyzeSentence(spec)
{
    util.assert(spec.sentence !== undefined &&
                spec.finalize !== undefined,
                "bad input");

    var sentence = spec.sentence,
        finalize = spec.finalize,
        ret = textanalysis.textAnalyser({'sentence': sentence, 'finalize': finalize}),
        lastnode;

    ret.applyToGraph({
        main_graph: rz_core.main_graph,
        edit_graph: rz_core.edit_graph,
        backend_commit: rz_config.backend_enabled,
    });

    switch (ret.state) {
    case textanalysis.ANALYSIS_NODE_START:
        lastnode = textanalysis.lastnode(rz_core.edit_graph, input.selectionStart());
        if (lastnode !== null) {
            typeselection.analysisNodeStart(lastnode.id);
        }
        break;
    case textanalysis.ANALYSIS_LINK:
        typeselection.hide();
        break;
    }

    if (finalize || sentence.length == 0) {
        typeselection.hide();
    } else {
        rz_core.main_graph_view.nodes__user_visible(ret.existing_nodes(rz_core.main_graph));
    }
    input_bus.push({where: consts.INPUT_WHERE_TEXTANALYSIS, input: sentence});
}

function changeType(up)
{
    var cursor = input.selectionStart(),
        lastnode = textanalysis.lastnode(rz_core.edit_graph, cursor),
        nodetype,
        id,
        name;

    if (!lastnode) {
        name = id = consts.NEW_NODE_NAME;
    } else {
        id = lastnode.id;
        name = lastnode.name;
    }
    nodetype = (up ? textanalysis.selected_type_next() : textanalysis.selected_type_prev());

    rz_core.edit_graph.editType(id, nodetype);
    typeselection.showChosenType(id, nodetype);
    textanalysis.set_type(name, nodetype);
}

var main = function ()
{
    input.on_sentence.onValue(submitNewSentence);
    input.on_analysis__input.onValue(function (sentence) {
            input.on_analysis__output.push(analyzeSentence({
                sentence: sentence,
                finalize: false,
            }));
        });
    input.on_cursor.onValue(function (cursor) {
        var lastnode = textanalysis.lastnode(rz_core.edit_graph, cursor);

        if (lastnode === null) {
            return;
        }
        textanalysis.selected_type_set(lastnode.type);
        typeselection.showChosenType(lastnode.id, lastnode.type);
    });
    input.on_type.onValue(function (up) {
            if (textanalysis.lastnode(rz_core.edit_graph, input.selectionStart())) {
                changeType(up);
                ret = false;
            }
        });

    function submitNewSentence(text) {
        analyzeSentence(
            {
                sentence: text,
                finalize: true,
            });
    }

    // Click is required to prevent the default action - this is a form so that's a post,
    // and away we go.
    // The mousedown is required because CSS3 transitions eat some events sometimes. This is
    // the closest I've come to an explanation:
    //   http://stackoverflow.com/questions/15786891/browser-sometimes-ignores-a-jquery-click-event-during-a-css3-transform
    plus_button.bind("click mousedown", function(e) {
        console.dir(e);
        submitNewSentence(input.value());
        e.preventDefault();
    });

    input.on_resize.onValue(function () {
        plus_button.offset({'left': input.element.offset().left + input.element.width() - 18});
    });

    rz_bus.ui_input.plug(input_bus);
};

return {
        analyzeSentence: analyzeSentence,
        main: main
       };
}); // define