summaryrefslogtreecommitdiff
path: root/web/viewsjs/search.js.erb
blob: aa717b5fde8600947adbed705ab1453c20b3c496 (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
<%
    osm = @trans.t.osm
    misc = @trans.t.misc
    search = @trans.t.pages.search
 %>
function link_to_key_with_highlight(key, highlight) {
    return link(
        url_for_key(key),
        key.replace(new RegExp('(' + highlight + ')', 'gi'), "<b>$1</b>")
    );
}

function link_to_value_with_highlight(key, value, highlight) {
    return link(
        url_for_tag(key, value), 
        value.replace(new RegExp('(' + highlight + ')', 'gi'), "<b>$1</b>")
    );
}

var create_flexigrid_for = {
    keys: function(query) {
        create_flexigrid('grid-keys', {
            url: '/api/4/keys/all?query=' + encodeURIComponent(query),
            colModel: [
                { display: '<%= misc.count %>', name: 'count_all', width: 80, sortable: true, align: 'right' },
                { display: '<%= osm.key %>', name: 'key', width: 500, sortable: true }
            ],
            sortname: 'count_all',
            sortorder: 'desc',
            preProcess: function(data) {
                data.rows = jQuery.map(data.data, function(row, i) {
                    return { 'cell': [
                        print_with_ts(row.count_all),
                        link_to_key_with_highlight(row.key, query)
                    ] };
                });
                return data;
            }
        });
    },
    values: function(query) {
        create_flexigrid('grid-values', {
            url: '/api/4/search/by_value?query=' + encodeURIComponent(query),
            colModel: [
                { display: '<%= misc.count %>', name: 'count_all', width: 80, sortable: true, align: 'right' },
                { display: '<%= osm.key %>', name: 'key', width: 250, sortable: true },
                { display: '<%= osm.value %>', name: 'value', width: 500, sortable: true }
            ],
            sortname: 'count_all',
            sortorder: 'desc',
            preProcess: function(data) {
                data.rows = jQuery.map(data.data, function(row, i) {
                    return { 'cell': [
                        print_with_ts(row.count_all),
                        link_to_key(row.key),
                        link_to_value_with_highlight(row.key, row.value, query)
                    ] };
                });
                return data;
            }
        });
    },
    tags: function(query) {
        var q = query.split('=', 2);
        create_flexigrid('grid-tags', {
            url: '/api/4/search/by_key_and_value?query=' + encodeURIComponent(query),
            colModel: [
                { display: '<%= misc.count %>', name: 'count_all', width: 80, sortable: true, align: 'right' },
                { display: '<%= osm.key %>', name: 'key', width: 300, sortable: true },
                { display: '<%= osm.value %>', name: 'value', width: 500, sortable: true }
            ],
            sortname: 'count_all',
            sortorder: 'desc',
            preProcess: function(data) {
                data.rows = jQuery.map(data.data, function(row, i) {
                    return { 'cell': [
                        print_with_ts(row.count_all),
                        link_to_key_with_highlight(row.key, q[0]),
                        link_to_value_with_highlight(row.key, row.value, q[1])
                    ] };
                });
                return data;
            }
        });
    },
    fulltext: function(query) {
        create_flexigrid('grid-fulltext', {
            url: '/api/4/search/by_keyword?query=' + encodeURIComponent(query),
            colModel: [
                { display: '<%= osm.key %>', name: 'key', width: 300, sortable: true },
                { display: '<%= osm.value %>', name: 'value', width: 500, sortable: true }
            ],
            sortname: 'key',
            sortorder: 'asc',
            preProcess: function(data) {
                data.rows = jQuery.map(data.data, function(row, i) {
                    return { 'cell': [
                        link_to_key(row.key),
                        row.value ? link_to_value(row.key, row.value) : ''
                    ] };
                });
                return data;
            }
        });
    }
};

function page_init() {
    page_init2();
}