1
0
Fork 0
frontend/bundle.js

180 lines
4.7 KiB
JavaScript
Raw Normal View History

2023-03-25 12:39:09 -04:00
window.leqsikoni = {
maybe_text: function(obj) {
if (obj) {
return String(obj).trim() || null
} else {
return null
}
},
2023-03-25 13:25:56 -04:00
app: function() {
if ($('#app').length === 0) return;
var path = String(window.location.hash).substring(1)
2023-03-25 12:39:09 -04:00
2023-03-25 13:25:56 -04:00
var match = null
if ((match = /^\/words\/(\d+)$/.exec(path))) {
window.leqsikoni.words_xxxxx(Number(match[1]))
}
},
words_xxxxx: function(word_id) {
2023-03-25 13:38:21 -04:00
$('#app').html(window.leqsikoni.words_xxxxx_template())
2023-03-24 20:27:30 -04:00
2023-02-04 22:04:28 -05:00
$.getJSON(
2023-03-24 20:27:30 -04:00
'http://localhost:4567/words/' + word_id,
function(data) {
document.title = window.leqsikoni.maybe_text(data.primary_form) || ''
2023-03-25 13:25:56 -04:00
$('#app').html(window.leqsikoni.words_xxxxx_template(data))
},
2023-03-25 12:39:09 -04:00
)
},
2023-02-18 09:24:07 -05:00
2023-03-25 12:39:09 -04:00
words_xxxxx_template: function(options) {
2023-03-25 13:38:21 -04:00
if (!options || typeof(options) !== 'object') {
2023-03-25 12:39:09 -04:00
options = {}
}
2023-03-24 20:27:30 -04:00
2023-03-25 12:39:09 -04:00
var primary_form = window.leqsikoni.maybe_text(options.primary_form)
var part_of_speech = window.leqsikoni.maybe_text(options.part_of_speech)
var commentary = window.leqsikoni.maybe_text(options.commentary)
2023-03-24 20:27:30 -04:00
2023-03-25 12:39:09 -04:00
var translations =
window.leqsikoni.words_xxxxx_translations_template(options.translations)
var inflections =
window.leqsikoni.words_xxxxx_inflections_template(options.inflections)
var examples =
window.leqsikoni.words_xxxxx_examples_template(options.examples)
2023-02-19 00:34:15 -05:00
2023-03-25 13:12:17 -04:00
var html = ''
2023-03-24 20:27:30 -04:00
2023-03-25 13:12:17 -04:00
if (primary_form) {
html += '<h1>' + primary_form + '</h1>'
}
if (part_of_speech) {
html += '<p>' + part_of_speech + '</p>'
2023-03-25 12:39:09 -04:00
}
if (translations) {
2023-03-25 13:12:17 -04:00
html += '<div><h2>Значения</h2><div>' + translations + '</div></div>'
}
if (commentary) {
html += '<div><h2>Комментарий</h2><p>' + commentary + '</p></div>'
2023-03-25 12:39:09 -04:00
}
if (inflections) {
2023-03-25 13:39:01 -04:00
html += '<div><h2>Inflections</h2><div>' + inflections + '</div></div>'
2023-03-25 12:39:09 -04:00
}
if (examples) {
2023-03-25 13:12:17 -04:00
html +=
'<div><h2>Примеры употребления</h2><div>' + examples + '</div></div>'
2023-03-25 12:39:09 -04:00
}
2023-03-25 13:12:17 -04:00
return html
2023-03-25 12:39:09 -04:00
},
words_xxxxx_translations_template: function(translations) {
if (translations === null || typeof(translations) !== 'object') {
translations = []
} else {
translations = Object.values(translations)
}
var items_html = []
translations.forEach(function(translation) {
var commentary = ''
if (translation.commentary) {
commentary = ' (' + translation.commentary + ')'
}
items_html.push('<li>' + translation.translation + commentary + '</li>')
})
if (items_html.length === 0) {
return null
} else {
return '<ol>' + items_html.join('') + '</ol>'
}
},
words_xxxxx_inflections_template: function(inflections) {
if (inflections === null || typeof(inflections) !== 'object') {
inflections = []
} else {
inflections = Object.values(inflections)
}
var items_html = []
inflections.forEach(function(inflection) {
var descr = inflection['descr']
var value = inflection['value'] || ''
var other_word_id = inflection['other_word_id']
if (other_word_id !== null) {
value =
2023-03-25 13:32:53 -04:00
'<a href="#/words/' + other_word_id + '">' + value + '</a>'
2023-03-25 12:39:09 -04:00
}
items_html.push(
'<li><b>' + descr + '</b>: ' + value + '</li>'
)
})
if (items_html.length === 0) {
return null
} else {
return '<ul>' + items_html.join('') + '</ul>'
}
},
words_xxxxx_examples_template: function(examples) {
if (examples === null || typeof(examples) !== 'object') {
examples = []
} else {
examples = Object.values(examples)
}
var items_html = []
examples.forEach(function(example) {
var left = example['left']
var right = example['right']
var highlights = example['highlights']
var highlighted = []
if (highlights.length == 0) {
highlighted.push(left)
} else {
var last_pos = 0
highlights.forEach(function(highlight) {
var pos = highlight['pos']
var len = highlight['len']
if (pos > 0) { highlighted.push(left.substring(last_pos, pos)) }
last_pos = pos + len
highlighted.push(
'<span class="highlight">' +
left.substring(pos, pos + len) +
'</span>'
)
})
}
items_html.push(
'<li>' + highlighted.join('') + ' &mdash; ' + right + '</li>'
)
})
if (items_html.length === 0) {
return null
} else {
return '<ul>' + items_html.join('') + '</ul>'
}
},
}
$(function() {
2023-03-25 13:25:56 -04:00
window.addEventListener('hashchange', window.leqsikoni.app, false)
window.leqsikoni.app()
2023-02-04 22:04:28 -05:00
})