remove markup from js

so themes can use different styles
isekai-main
Barış Soner Uşaklı 2 years ago
parent c97977f846
commit f2c72bc7a3

@ -2,20 +2,27 @@
define('forum/chats/search', [ define('forum/chats/search', [
'components', 'api', 'alerts', 'helpers', 'components', 'api', 'alerts',
], function (components, api, alerts, helpers) { ], function (components, api, alerts) {
const search = {}; const search = {};
search.init = function () { search.init = function () {
components.get('chat/search').on('keyup', utils.debounce(doSearch, 250)); components.get('chat/search').on('keyup', utils.debounce(doSearch, 250));
const chatsListEl = $('[component="chat/search/list"]');
chatsListEl.on('click', '[data-uid]', function () {
onUserClick($(this).attr('data-uid'));
});
}; };
function doSearch() { function doSearch() {
const chatsListEl = $('[component="chat/search/list"]');
const username = components.get('chat/search').val(); const username = components.get('chat/search').val();
if (!username) { if (!username) {
return $('[component="chat/search/list"]').translateHtml('<li><a href="#" class="dropdown-item">[[admin/menu:search.start-typing]]</a></li>'); removeResults(chatsListEl);
chatsListEl.find('[component="chat/search/no-users"]').addClass('hidden');
return chatsListEl.find('[component="chat/search/start-typing"]').removeClass('hidden');
} }
chatsListEl.find('[component="chat/search/start-typing"]').addClass('hidden');
api.get('/api/users', { api.get('/api/users', {
query: username, query: username,
searchBy: 'username', searchBy: 'username',
@ -24,43 +31,31 @@ define('forum/chats/search', [
.catch(alerts.error); .catch(alerts.error);
} }
function displayResults(data) { function removeResults(chatsListEl) {
const chatsListEl = $('[component="chat/search/list"]'); chatsListEl.find('[data-uid]').remove();
chatsListEl.empty(); }
async function displayResults(data) {
const chatsListEl = $('[component="chat/search/list"]');
removeResults(chatsListEl);
data.users = data.users.filter(function (user) { data.users = data.users.filter(function (user) {
return parseInt(user.uid, 10) !== parseInt(app.user.uid, 10); return parseInt(user.uid, 10) !== parseInt(app.user.uid, 10);
}); });
if (!data.users.length) { if (!data.users.length) {
return chatsListEl.translateHtml('<li><a href="#" class="dropdown-item">[[users:no-users-found]]</a></li>'); return chatsListEl.find('[component="chat/search/no-users"]').removeClass('hidden');
} }
chatsListEl.find('[component="chat/search/no-users"]').addClass('hidden');
data.users.forEach(function (userObj) { const html = await app.parseAndTranslate('chats', 'searchUsers', { searchUsers: data.users });
const chatEl = displayUser(chatsListEl, userObj); chatsListEl.append(html);
onUserClick(chatEl, userObj); chatsListEl.parent().toggleClass('show', true);
});
chatsListEl.parent().toggleClass('open', true);
} }
function displayUser(chatsListEl, userObj) { function onUserClick(uid) {
function createUserImage() { if (!uid) {
const img = helpers.buildAvatar(userObj, '24px', true); return;
return `<a href="#" class="dropdown-item">${img} ${userObj.username}<a>`;
} }
socket.emit('modules.chats.hasPrivateChat', uid, function (err, roomId) {
const chatEl = $('<li component="chat/search/user"></li>')
.attr('data-uid', userObj.uid)
.appendTo(chatsListEl);
chatEl.append(createUserImage());
return chatEl;
}
function onUserClick(chatEl, userObj) {
chatEl.on('click', function () {
socket.emit('modules.chats.hasPrivateChat', userObj.uid, function (err, roomId) {
if (err) { if (err) {
return alerts.error(err); return alerts.error(err);
} }
@ -70,11 +65,10 @@ define('forum/chats/search', [
}); });
} else { } else {
require(['chat'], function (chat) { require(['chat'], function (chat) {
chat.newChat(userObj.uid); chat.newChat(uid);
}); });
} }
}); });
});
} }
return search; return search;

Loading…
Cancel
Save