MediaWiki:EveryCRT-SearchEngine.js: Difference between revisions
Created page with "→* * EveryCRT Shared Search Engine * Faceted search using Semantic MediaWiki API, shared across CRT Search, AV Device Search, etc. * * Usage from a page-specific JS file: * mw.loader.getScript( * mw.config.get('wgScript') + '?title=MediaWiki:EveryCRT-SearchEngine.js&action=raw&ctype=text/javascript' * ).then(function() { * window.EveryCRTSearch({ containerId: '...', category: '...', ... }); * });: window.EveryCRTSearch = function(config) {..." |
No edit summary |
||
| Line 423: | Line 423: | ||
if (filter.loadFrom === 'category') { | if (filter.loadFrom === 'category') { | ||
promises.push(loadCategoryOptions(filterKey, filter.category)); | promises.push(loadCategoryOptions(filterKey, filter.category)); | ||
} else if (filter.loadFrom === 'smw') { | |||
promises.push(loadSMWOptions(filterKey, filter.smwQuery, filter.smwProperty)); | |||
} else if (filter.loadFrom === 'static' && filter.values) { | } else if (filter.loadFrom === 'static' && filter.values) { | ||
filterOptions[filterKey] = filter.values.map(function(v) { | filterOptions[filterKey] = filter.values.map(function(v) { | ||
| Line 452: | Line 454: | ||
data.query.categorymembers.forEach(function(member) { | data.query.categorymembers.forEach(function(member) { | ||
filterOptions[filterKey].push({ value: member.title, label: member.title }); | filterOptions[filterKey].push({ value: member.title, label: member.title }); | ||
}); | |||
filterOptions[filterKey].sort(function(a, b) { return a.label.localeCompare(b.label); }); | |||
} | |||
resolve(); | |||
}).fail(function() { | |||
filterOptions[filterKey] = []; | |||
resolve(); | |||
}); | |||
}); | |||
} | |||
function loadSMWOptions(filterKey, smwQuery, smwProperty) { | |||
return new Promise(function(resolve) { | |||
var api = new mw.Api(); | |||
api.get({ | |||
action: 'ask', | |||
query: smwQuery + '|?' + smwProperty + '|limit=500|format=json', | |||
format: 'json' | |||
}).done(function(data) { | |||
filterOptions[filterKey] = []; | |||
if (data.query && data.query.results) { | |||
Object.values(data.query.results).forEach(function(result) { | |||
if (result.printouts && result.printouts[smwProperty]) { | |||
result.printouts[smwProperty].forEach(function(val) { | |||
var v = typeof val === 'object' ? (val.fulltext || val.value || '') : String(val); | |||
if (v) filterOptions[filterKey].push({ value: v, label: v }); | |||
}); | |||
} | |||
}); | }); | ||
filterOptions[filterKey].sort(function(a, b) { return a.label.localeCompare(b.label); }); | filterOptions[filterKey].sort(function(a, b) { return a.label.localeCompare(b.label); }); | ||
| Line 1,101: | Line 1,131: | ||
var optionExists = Array.from(select.options).some(function(opt) { return opt.value === value; }); | var optionExists = Array.from(select.options).some(function(opt) { return opt.value === value; }); | ||
if (optionExists) { | if (optionExists) { | ||
select.value = value; | |||
} else if (filterConfig.loadFrom === 'category' || filterConfig.loadFrom === 'smw') { | |||
// Dynamic filter — options may not be loaded yet; add temporarily | |||
var tempOption = document.createElement('option'); | |||
tempOption.value = value; | |||
tempOption.textContent = value; | |||
select.appendChild(tempOption); | |||
select.value = value; | select.value = value; | ||
} else { | } else { | ||