define('modules/knowledge-base/views/dashlets/kb-search', ['views/dashlets/abstract/base'], function (Dep) {
return Dep.extend({
name: 'KbSearch',
templateContent: `
`,
events: {
'click [data-action="submit"]': function () {
this.runSearch();
},
'keypress input[data-name="query"]': function (e) {
if (e.which === 13) this.runSearch();
},
},
runSearch: function () {
const query = (this.$el.find('input[data-name="query"]').val() || '').trim();
if (!query) return;
const $r = this.$el.find('.kb-dashlet-results');
$r.html('מחפש…
');
Espo.Ajax.postRequest('KnowledgeBase/action/search', {
query: query,
kind: 'any',
topK: 5,
}).then(res => this.renderHits(res.hits || []))
.catch(err => {
$r.html('' + ((err && err.statusText) || 'Error') + '
');
});
},
renderHits: function (hits) {
const $r = this.$el.find('.kb-dashlet-results');
if (!hits.length) {
$r.html('לא נמצאו תוצאות.
');
return;
}
const esc = s => String(s || '').replace(/&/g, '&').replace(//g, '>');
const kindHe = {law: 'חוק', regulation: 'תקנה', circular: 'חוזר'};
const rows = hits.map(h => {
const kind = kindHe[h.kind] || h.kind;
const ident = h.identifier ? ' ' + esc(h.identifier) : '';
const ref = h.section_ref || h.heading_path || '';
const preview = esc((h.content || '').slice(0, 200)) + ((h.content || '').length > 200 ? '…' : '');
return `
[${kind}${ident}] ${esc(h.title || '')}
${ref ? ' — ' + esc(ref) + '' : ''}
${preview}
`;
}).join('');
$r.html(rows);
},
});
});