Fix for historic and new undefined order issues

pull/1941/head
wmwragg 2016-08-10 11:39:10 +01:00
parent 1c812b340d
commit 699a58e0b3
1 changed files with 56 additions and 37 deletions

View File

@ -154,28 +154,21 @@ var RoomSubList = React.createClass({
},
// Generates the manual comparator using the given list
manualComparator: function(list) {
var self = this;
var roomList = list;
return function(roomA, roomB) {
if (!roomA.tags[self.props.tagName] || !roomB.tags[self.props.tagName]) return 0;
manualComparator: function(roomA, roomB) {
if (!roomA.tags[this.props.tagName] || !roomB.tags[this.props.tagName]) return 0;
// Make sure the room tag has an order element, if not set it to be the bottom
var a = roomA.tags[self.props.tagName].order;
var b = roomB.tags[self.props.tagName].order;
var a = roomA.tags[this.props.tagName].order;
var b = roomB.tags[this.props.tagName].order;
if (a === undefined) {
a = (self._getHighestOrder(roomList) + 1.0) / 2.0;
roomA.tags[self.props.tagName].order = a;
};
if (b === undefined) {
b = (self._getHighestOrder(roomList) + 1.0) / 2.0;
roomB.tags[self.props.tagName].order = b;
};
return a == b ? self.lexicographicalComparator(roomA, roomB) : ( a > b ? 1 : -1);
// Order undefined room tag orders to the bottom
if (a === undefined && b !== undefined) {
return 1;
} else if (a !== undefined && b === undefined) {
return -1;
}
return a == b ? this.lexicographicalComparator(roomA, roomB) : ( a > b ? 1 : -1);
},
sortList: function(list, order) {
@ -183,30 +176,16 @@ var RoomSubList = React.createClass({
if (order === undefined) order = this.props.order;
var comparator;
list = list || [];
if (order === "manual") comparator = this.manualComparator(list);
if (order === "manual") comparator = this.manualComparator;
if (order === "recent") comparator = this.recentsComparator;
// Fix undefined orders here, and make sure the backend gets updated as well
this._fixUndefinedOrder(list);
//if (debug) console.log("sorting list for sublist " + this.props.label + " with length " + list.length + ", this.props.list = " + this.props.list);
this.setState({ sortedList: list.sort(comparator) });
},
// Finds the highest (lowest position) order of a room in a manual ordered list
// room.tag[tagname].order
_getHighestOrder: function(list) {
var order = 0.0;
if (this.props.order === "manual") {
var self = this;
list.forEach(function(room) {
if (room.tags.hasOwnProperty(self.props.tagName)) {
if (order < room.tags[self.props.tagName].order) {
order = room.tags[self.props.tagName].order;
}
}
})
}
return order;
},
moveRoomTile: function(room, atIndex) {
if (debug) console.log("moveRoomTile: id " + room.roomId + ", atIndex " + atIndex);
//console.log("moveRoomTile before: " + JSON.stringify(this.state.rooms));
@ -351,6 +330,46 @@ var RoomSubList = React.createClass({
this.props.onShowMoreRooms();
},
// Fix any undefined order elements of a room in a manual ordered list
// room.tag[tagname].order
_fixUndefinedOrder: function(list) {
if (this.props.order === "manual") {
var order = 0.0;
var self = this;
// Find the highest (lowest position) order of a room in a manual ordered list
list.forEach(function(room) {
if (room.tags.hasOwnProperty(self.props.tagName)) {
if (order < room.tags[self.props.tagName].order) {
order = room.tags[self.props.tagName].order;
}
}
});
// Fix any undefined order elements of a room in a manual ordered list
// Do this one at a time, as each time a rooms tag data is updated the RoomList
// gets triggered and another list is passed in. Doing it one at a time means that
// we always correctly calculate the highest order for the list - stops multiple
// rooms getting the same order. This is only really relevant for the first time this
// is run with historical room tag data, after that there should only be undefined
// in the list at a time anyway.
for (let i = 0; i < list.length; i++) {
if (list[i].tags[self.props.tagName].order === undefined) {
MatrixClientPeg.get().setRoomTag(list[i].roomId, self.props.tagName, {order: (order + 1.0) / 2.0}).finally(function() {
// Do any final stuff here
}).fail(function(err) {
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
Modal.createDialog(ErrorDialog, {
title: "Failed to add tag " + self.props.tagName + " to room",
description: err.toString()
});
});
break;
};
};
}
},
render: function() {
var connectDropTarget = this.props.connectDropTarget;
var RoomDropTarget = sdk.getComponent('rooms.RoomDropTarget');