From 699a58e0b37ca50c9e20c630ba20b1ef97fba445 Mon Sep 17 00:00:00 2001 From: wmwragg Date: Wed, 10 Aug 2016 11:39:10 +0100 Subject: [PATCH] Fix for historic and new undefined order issues --- src/components/structures/RoomSubList.js | 93 ++++++++++++++---------- 1 file changed, 56 insertions(+), 37 deletions(-) diff --git a/src/components/structures/RoomSubList.js b/src/components/structures/RoomSubList.js index 94a46d75..74ddb879 100644 --- a/src/components/structures/RoomSubList.js +++ b/src/components/structures/RoomSubList.js @@ -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; + manualComparator: function(roomA, roomB) { + if (!roomA.tags[this.props.tagName] || !roomB.tags[this.props.tagName]) return 0; - return function(roomA, roomB) { - if (!roomA.tags[self.props.tagName] || !roomB.tags[self.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[this.props.tagName].order; + var b = roomB.tags[this.props.tagName].order; - // 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; - - 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');