forked from betagouv/api.gouv.fr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
170 lines (159 loc) · 4.73 KB
/
index.html
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
---
layout: default
additional_css: homepage
searchExamples:
- quotient familial
- revenu fiscal de référence
- cadastre
---
<section id="mission-statement">
<div class="header-with-image">
<img class="ui small image" src="/img/Apigouv_visual.svg"
alt="Une API connecte des services à la puissance d'internet" />
<div>
<h1 class="ui header">
{{ site.description }}
</h1>
<div class="ui search">
<div class="ui fluid left icon input">
<input class="prompt"
placeholder="{{ page.searchExamples | join: ', '}}…"
type="search">
<i class="search icon"></i>
</div>
<div class="results transition hidden">
</div>
</div>
</div>
</div>
</section>
<section id="apis">
<div class="ui container">
<div class="ui three stackable cards">
{% for api in site.api %}
{% include api-card.html description=api %}
{% endfor %}
</div>
</div>
</section>
<script>
function spanHighlight(val) {
return '<span class="result-highlight">' + val + '</span>';
}
function highlight(value, term) {
var lowerCasedTerm = term.unidecode().toLowerCase();
var valueUnidecoded = value.unidecode().toLowerCase();
var pasOccurrences = [];
var pos = valueUnidecoded.indexOf(lowerCasedTerm);
while (pos !== -1) {
pasOccurrences.push(pos);
pos = valueUnidecoded.indexOf(lowerCasedTerm, pos + 1);
}
$.each(pasOccurrences.reverse(), function () {
value = value.substring(0, this)
+ spanHighlight(value.substr(this, lowerCasedTerm.length))
+ value.substring(this + lowerCasedTerm.length);
});
return value;
}
function getHighLightFromArray(array, label, searchTerm) {
var termHighlighted = [];
$.each(array, function () {
var val = this.innerHTML;
if (val.search(searchTerm) !== -1) {
termHighlighted.push(spanHighlight(val));
}
});
if (termHighlighted.length > 0) {
return '<br>' + label + ' : ' + termHighlighted.slice(0, 3).join(', ');
} else {
return ''
}
}
function onSearchQuery(searchTerm) {
//this part higlights results
var module = $(this);
$.each(module.find('.content'), function () {
var content = $(this);
$.each(content.find('.description'), function () {
var description = $(this);
description.html(highlight($(this).html(), searchTerm));
$.each([['keywords', 'tags'],
['partners', 'partenaires'],
['clients', 'clients']], function () {
var HighLightFromArray = getHighLightFromArray(
content.find('.' + this[0] + ' ul li'),
this[1],
searchTerm
);
if (HighLightFromArray) {
description.append(HighLightFromArray);
return false;
}
});
});
});
}
function customTemplate(response, fields) {
var html = '';
$.each(response[fields.results], function (index, value) {
html += '<a class="result" href="' + value.url + '">'
+ '<div class="image"><img src="/img/' + value.image + '" /></div>'
+ '<div class="content">'
+ '<div class="title">' + value.title + '</div>'
+ '<div class="description">' + value.description + '</div>';
$.each(['keywords', 'partners'], function (i, key) {
html += '<div class="' + key + '" hidden="true"><ul>';
$.each(value[key], function (i, v) {
html += '<li>' + v + '</li>';
});
html += '</ul></div>';
});
html += '</div>';
html += '</a>';
});
return html;
}
// from https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript#answer-901144
function getFilterInQueryParams() {
var regex = new RegExp('[?&]filter(=([^&#]*)|&|#|$)');
var results = regex.exec(window.location.href);
if (!results) return null;
if (!results[2]) return null;
return results[2];
}
$(document).ready(function () {
$.getJSON('/api/v1/api.json', function (toSearch) {
var searchFields = ['title', 'description', 'keywords', 'tags', 'partners'];
for (var i = 0; i < toSearch.length; i++) {
toSearch[i]['unidecoded'] = searchFields.map(
function (v) {
return toSearch[i][v];
})
.join()
.unidecode()
;
}
searchFields.push('unidecoded');
$('.ui.search').search({
type: 'custom',
source: toSearch,
searchFields: searchFields,
searchFullText: false,
showNoResults: true,
regExp: {
escape: /[\-\[\]\/{}()*+?.\\^$|]/g,
beginsWith: ''
},
onSearchQuery: onSearchQuery,
templates: {
custom: customTemplate
}
});
});
var filter = getFilterInQueryParams();
if (filter) {
$('.api-card').not(":has(.api-tag." + filter + ")").css('display', 'none');
}
});
</script>