summaryrefslogtreecommitdiff
path: root/src/client/view/search.js
blob: 41075c2816e2a6f0d7e23860c5b575118cb1cf9e (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
/*
    This file is part of rhizi, a collaborative knowledge graph editor.
    Copyright (C) 2014-2015  Rhizi

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

define(['consts', 'view/completer', 'textanalysis', 'rz_core', 'view/selection'],
function(consts,        completer,   textanalysis,   rz_core,        selection)
{
var search = $('#search'),
    search_btn = $('#btn_search'),
    search_completer_element = $('#search-suggestion'),
    search_completer;

var focus = function() {
    search.focus();
}

function init() {
    search_completer = completer(search, search_completer_element,
                                 {
                                    triggerStart:'|',
                                    triggerEnd:'|',
                                    matchStartOfString: true,
                                    appendOnCompletion:'|',
                                 });

    search_completer.options.plug(textanalysis.suggestions_options.map('.nodes'));
    search.asEventStream('keydown').filter(
            function(e) {
            if (e.which === consts.VK_ENTER && search_completer.handleEnter()) {
                e.preventDefault();
            }
            return (e.which === consts.VK_ENTER);
        })
        .merge($('#btn_search').asEventStream('click'))
        .merge(search_completer.completionsBus)
        .map(function () { return search[0].value.trim(); })
        .skipDuplicates()
        .onValue(search_on_submit);

    function attribute_match(obj, regexp) {
        var v, k;

        for (k in obj) {
            if (obj.hasOwnProperty(k)) {
                v = obj[k];
                if ("string" === typeof(v) && v.match(regexp)) {
                    return true;
                }
            }
        }
        return false;
    }

    function search_on_submit(text) {
        var r,
            selector = function (obj) { return attribute_match(obj, r); };
            text_processed = text[text.length - 1] == '|' ? text.slice(0, text.length - 1) : text;

        try {
            r = new RegExp(text_processed, 'i');
        } catch (e) {
            return; // don't clear selection either
        }
        if (text_processed.length > 0) {
            selection.byVisitors(selector, selector);
        } else {
            selection.clear();
        }
        rz_core.update_view__graph(false);
    };

}

function clear() {
    search.val('');
}

return {
    focus: focus,
    init: init,
    clear: clear,
};
}
);