-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAnswer.js
More file actions
116 lines (93 loc) · 2.59 KB
/
Answer.js
File metadata and controls
116 lines (93 loc) · 2.59 KB
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
const Answer = {
sendQuestion: () => {
$('#newQuestionForm').submit();
$('#questionModal').modal('toggle');
$('#newQuestion').val('');
alert('Спасибо за Ваш вопрос / исправление!');
},
showAnswer: (data, question) => {
const questionObject = data.find(item => item.question === question);
const answerText = questionObject.answer || "";
if (answerText) {
$('#questionText').text(question);
$('#answerText').html(answerText);
const parts = location.pathname.split("/");
let link = "/";
if (parts.length === 3) {
link += parts[1] + "/";
}
link += createSlug(question);
$('#permalink').attr('href', link);
$('#answerModal').modal('toggle');
$('#question').val('');
if (gtag) {
gtag('event', 'page_view', {
page_title: question,
page_location: 'http://spain-answer.xyz' + link,
page_path: link,
send_to: 'G-RV7NR0PTD7'
});
}
}
}
};
const LETTERS = {
"а": "a", "б": "b", "в": "v", "г": "g",
"д": "d", "е": "e", "ё": "yo", "ж": "zh",
"з": "z", "и": "i", "й": "yi", "к": "k",
"л": "l", "м": "m", "н": "n", "о": "o",
"п": "p", "р": "r", "с": "s", "т": "t",
"у": "u", "ф": "f", "х": "j", "ц": "c",
"ч": "ch", "ш": "sh", "щ": "sch", "ъ": "",
"ы": "y", "ь": "", "э": "e", "ю": "yu",
"я": "ya", "?": "", " ": "-"
};
const createSlug = (input) => {
let output = "";
const lowercase = input.toLowerCase();
for (let k = 0; k < lowercase.length; k++) {
output += (LETTERS[lowercase[k]] || "");
}
return output;
};
$(document).ready(
() => {
$.getJSON(
"questions.json",
(data) => {
$("#question").autocomplete({
source: (search, response) => {
response(data.filter(
(questionObject) =>
questionObject.question.toLowerCase().indexOf(search.term.toLowerCase()) !== -1
).map(
(questionObject) => ({
label: questionObject.question.toLowerCase(), value: questionObject.question
})
));
},
minLength: 2,
select: (event, ui) => {
if (ui.item.value.length) {
Answer.showAnswer(data, ui.item.value);
}
}
});
const results = (/\?q=(.*)/g).exec(window.location.search);
const questionSlug = (results !== null) ? results[1] || 0 : false;
if (questionSlug) {
const questionObject = data.find((item) =>
createSlug(item.question) === questionSlug
);
Answer.showAnswer(data, questionObject.question);
}
}
);
$('#answerModal').on(
'hidden.bs.modal',
() => {
$('#question').val('');
}
);
}
);