From 1c812b340d2505d37bf48b034cd2f7806b053052 Mon Sep 17 00:00:00 2001 From: wmwragg Date: Tue, 9 Aug 2016 19:20:27 +0100 Subject: [PATCH] Initial pass at handling room tags that don't have an order element, but need one manual ordering --- src/components/structures/RoomSubList.js | 51 ++++++++++++++++--- .../views/context_menus/RoomTagContextMenu.js | 4 +- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/components/structures/RoomSubList.js b/src/components/structures/RoomSubList.js index a5a6fd21..94a46d75 100644 --- a/src/components/structures/RoomSubList.js +++ b/src/components/structures/RoomSubList.js @@ -149,11 +149,33 @@ var RoomSubList = React.createClass({ return this.tsOfNewestEvent(roomB) - this.tsOfNewestEvent(roomA); }, - manualComparator: function(roomA, roomB) { - if (!roomA.tags[this.props.tagName] || !roomB.tags[this.props.tagName]) return 0; - var a = roomA.tags[this.props.tagName].order; - var b = roomB.tags[this.props.tagName].order; - return a == b ? this.recentsComparator(roomA, roomB) : ( a > b ? 1 : -1); + lexicographicalComparator: function(roomA, roomB) { + return roomA.name > roomB.name ? 1 : -1; + }, + + // 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; + + // 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); + } }, sortList: function(list, order) { @@ -161,13 +183,30 @@ var RoomSubList = React.createClass({ if (order === undefined) order = this.props.order; var comparator; list = list || []; - if (order === "manual") comparator = this.manualComparator; + if (order === "manual") comparator = this.manualComparator(list); if (order === "recent") comparator = this.recentsComparator; //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)); diff --git a/src/components/views/context_menus/RoomTagContextMenu.js b/src/components/views/context_menus/RoomTagContextMenu.js index fd917c04..68da3648 100644 --- a/src/components/views/context_menus/RoomTagContextMenu.js +++ b/src/components/views/context_menus/RoomTagContextMenu.js @@ -39,8 +39,6 @@ module.exports = React.createClass({ }, _toggleTag: function(tagNameOn, tagNameOff) { - console.log("DEBUG: _toggleTag"); - console.log("tagNameOn: " + tagNameOn + " tagNameOff: " + tagNameOff); var self = this; const roomId = this.props.room.roomId; var cli = MatrixClientPeg.get(); @@ -62,6 +60,8 @@ module.exports = React.createClass({ } if (tagNameOn !== null && tagNameOn !== undefined) { + // If the tag ordering meta data is required, it is added by + // the RoomSubList when it sorts its rooms cli.setRoomTag(roomId, tagNameOn, {}).finally(function() { // Close the context menu if (self.props.onFinished) {