thelounge/client/js/socket-events/sync_sort.js

47 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-05-18 20:08:54 +00:00
"use strict";
const $ = require("jquery");
const socket = require("../socket");
const options = require("../options");
socket.on("sync_sort", function(data) {
// Syncs the order of channels or networks when they are reordered
if (options.ignoreSortSync) {
options.ignoreSortSync = false;
return; // Ignore syncing because we 'caused' it
}
const type = data.type;
const order = data.order;
const container = $(".networks");
const network = container.find(`.network[data-uuid="${data.target}"]`);
2017-05-18 20:08:54 +00:00
if (type === "networks") {
$.each(order, function(index, value) {
const position = $(container.children(".network")[index]);
2017-05-18 20:08:54 +00:00
if (position.data("id") === value) { // Network in correct place
return true; // No point in continuing
}
network.insertBefore(position);
2017-05-18 20:08:54 +00:00
});
} else if (type === "channels") {
$.each(order, function(index, value) {
if (index === 0) { // Shouldn't attempt to move lobby
return true; // same as `continue` -> skip to next item
}
const position = $(network.children(".chan")[index]); // Target channel at position
2017-05-18 20:08:54 +00:00
if (position.data("id") === value) { // Channel in correct place
return true; // No point in continuing
}
const channel = network.find(".chan[data-id=" + value + "]"); // Channel at position
channel.insertBefore(position);
2017-05-18 20:08:54 +00:00
});
}
});