/** * The UGC search (/ugc_search): players' creations by name, id or LOT and where each one is (/api/ugc_links/search). * ?q= fills the search in; the address keeps the last search so it can be shared. */ (function () { var input = document.getElementById('ugcSearchInput'); var rows = document.getElementById('ugcSearchRows'); var status = document.getElementById('ugcSearchStatus'); var latest = 0, timer = null; var INVENTORIES = { 5: 'Models', 12: 'Vault models', 14: 'Brick-building models' }; function inventoryName(type) { return (window.Labels && Labels.name('inventories', type)) || INVENTORIES[type] || 'Inventory ' + type; } function where(item) { if (!item.where.length) { return 'Not found'; } return item.where.map(function (w) { if (w.type === 'property') { // A model the player didn't name has the client's placeholder (Objects__name) var given = w.modelName && !/^Objects_\d+_name$/.test(w.modelName); var name = given ? ' as ' + esc(w.modelName) + '' : ''; return '
On ' + fmt.link('/properties/' + w.propertyId, w.propertyName || 'Property ' + w.propertyId) + name + ' (' + fmt.character(w.ownerId, w.ownerName) + ', ' + esc(w.zoneId) + ')' + ' 3D
'; } if (w.type === 'mail') return '
In the mail to ' + fmt.character(w.characterId, w.characterName) + '
'; return '
In ' + fmt.character(w.characterId, item.characterName) + '\'s ' + esc(inventoryName(w.inventory)) + '
'; }).join(''); } function render(items) { rows.innerHTML = items.length ? items.map(function (item) { var lot = item.kind === 'modular' ? 6416 : 6662; var label = item.kind === 'modular' ? 'Car or rocket' : 'Model'; var title = item.link ? '' + label + ' ' + esc(item.ugcId) + '' : label + ' ' + esc(item.ugcId); var detail = item.detail ? '
' + (item.kind === 'modular' ? 'Modules ' + esc(item.detail) + '' : esc(item.detail)) + '
' : ''; var account = item.accountName ? '
' + (document.getElementById('ugcSearchPage').dataset.canAccounts ? fmt.link('/accounts/' + item.accountId, item.accountName) : esc(item.accountName)) + '
' : ''; return '' + UgcLinks.icon(item, lot, 48) + '' + title + detail + '' + '' + fmt.character(item.characterId, item.characterName) + account + '' + '' + where(item) + '' + UgcLinks.state(item) + ''; }).join('') : 'Nothing matches.'; } function search() { var q = input.value.trim(); var request = ++latest; var url = new URL(window.location.href); if (q) url.searchParams.set('q', q); else url.searchParams.delete('q'); history.replaceState(null, '', url); if (!q) { rows.innerHTML = 'Type something to search.'; status.textContent = ''; return; } status.textContent = 'Searching…'; api.get('/api/ugc_links/search?q=' + encodeURIComponent(q)).then(function (d) { if (request !== latest) return; var items = (d && d.items) || []; status.textContent = items.length + ' found' + (items.length >= 50 ? ' (the newest 50 of each kind; narrow the search for more)' : ''); render(items); }).catch(function () { if (request === latest) status.textContent = 'The search failed.'; }); } input.addEventListener('input', function () { clearTimeout(timer); timer = setTimeout(search, 300); }); document.getElementById('ugcSearchForm').addEventListener('submit', function (e) { e.preventDefault(); clearTimeout(timer); search(); }); input.value = document.getElementById('ugcSearchPage').dataset.query || ''; if (input.value) search(); input.focus(); })();