summaryrefslogtreecommitdiff
path: root/web/viewsjs/search.js.erb
blob: 0dd3fdb3ffc9bf30849c66d60f3b04c5372af76d (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
<%
    osm = @trans.t.osm
    misc = @trans.t.misc
    search = @trans.t.pages.search
 %>

function highlight(str, query) {
    return html_escape(str).replace(new RegExp('(' + html_escape(query) + ')', 'gi'), "<b>$1</b>");
}

function link_to_key_with_highlight(key, query) {
    return link(
        url_for_key(key),
        highlight(key, query)
    );
}

function link_to_value_with_highlight(key, value, query) {
    return link(
        url_for_tag(key, value),
        highlight(value, query)
    );
}

function link_to_rtype_with_highlight(rtype, query) {
    return link(
        url_for_rtype(rtype),
        highlight(rtype, query)
    );
}

var create_flexigrid_for = {
    keys: function(query) {
        create_flexigrid('grid-keys', {
            url: '/api/4/keys/all?query=' + encodeURIComponent(query),
            colModel: [
                { display: '<%= h(misc.count) %>', name: 'count_all', width: 80, sortable: true, align: 'right' },
                { display: '<%= h(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': [
                        fmt_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: '<%= h(misc.count) %>', name: 'count_all', width: 80, sortable: true, align: 'right' },
                { display: '<%= h(osm.key) %>', name: 'key', width: 250, sortable: true },
                { display: '<%= h(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': [
                        fmt_with_ts(row.count_all),
                        link_to_key(row.key),
                        link_to_value_with_highlight(row.key, row.value, query)
                    ] };
                });
                return data;
            }
        });
    },
    relations: function(query) {
        create_flexigrid('grid-relations', {
            url: '/api/4/relations/all?query=' + encodeURIComponent(query),
            colModel: [
                { display: '<%= h(misc.count) %>', name: 'count', width: 80, sortable: true, align: 'right' },
                { display: '<%= h(osm.relation_type) %>', name: 'rtype', width: 500, sortable: true }
            ],
            sortname: 'count',
            sortorder: 'desc',
            preProcess: function(data) {
                data.rows = jQuery.map(data.data, function(row, i) {
                    return { 'cell': [
                        fmt_with_ts(row.count),
                        link_to_rtype_with_highlight(row.rtype, query)
                    ] };
                });
                return data;
            }
        });
    },
    roles: function(query) {
        create_flexigrid('grid-roles', {
            url: '/api/4/search/by_role?query=' + encodeURIComponent(query),
            colModel: [
                { display: '<%= h(misc.count) %>', name: 'count_all', width: 80, sortable: true, align: 'right' },
                { display: '<%= h(osm.relation_types) %>', name: 'rtype', width: 250, sortable: true },
                { display: '<%= h(osm.relation_member_roles) %>', name: 'role', width: 500, sortable: true }
            ],
            sortname: 'count_all',
            sortorder: 'desc',
            preProcess: function(data) {
                data.rows = jQuery.map(data.data, function(row, i) {
                    return { 'cell': [
                        fmt_with_ts(row.count_all),
                        link_to_rtype(row.rtype),
                        highlight(row.role, 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: '<%= h(misc.count) %>', name: 'count_all', width: 80, sortable: true, align: 'right' },
                { display: '<%= h(osm.key) %>', name: 'key', width: 300, sortable: true },
                { display: '<%= h(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': [
                        fmt_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: '<%= h(osm.key) %>', name: 'key', width: 300, sortable: true },
                { display: '<%= h(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();
}