diff --git a/js/client-mainmenu.js b/js/client-mainmenu.js index fc49ae804..cbcea3ece 100644 --- a/js/client-mainmenu.js +++ b/js/client-mainmenu.js @@ -1,6 +1,8 @@ (function($) { var MainMenuRoom = this.MainMenuRoom = this.Room.extend({ + minWidth: 480, + maxWidth: 480, events: { 'click .button': 'clickButton' }, @@ -13,6 +15,13 @@ buf += '
'; this.$el.html(buf); }, + updateRightMenu: function() { + if (app.sideRoom) { + this.$('.rightmenu').hide(); + } else { + this.$('.rightmenu').show(); + } + }, clickButton: function(e) { e.preventDefault(); var $target = $(e.currentTarget); diff --git a/js/client.js b/js/client.js index 8ddef3796..b89f80dfb 100644 --- a/js/client.js +++ b/js/client.js @@ -59,9 +59,13 @@ initializeRooms: function() { this.rooms = {}; + + $(window).on('resize', _.bind(this.updateLayout, this)); }, + // the currently active room curRoom: null, curSideRoom: null, + sideRoom: null, joinRoom: function(id, type) { if (this.rooms[id]) { this.focusRoom(id); @@ -93,17 +97,84 @@ if (!room) return false; if (this.curRoom === room || this.curSideRoom === room) return true; - if (this.curRoom) { - this.curRoom.hide(); - this.curRoom = null; + this.updateSideRoom(id); + this.updateLayout(); + if (this.curSideRoom !== room) { + if (this.curRoom) { + this.curRoom.hide(); + this.curRoom = null; + } + this.curRoom = window.room = room; + this.updateLayout(); + if (this.curRoom.id === id) this.navigate(id); } - this.curRoom = room; - this.curRoom.show(); - app.navigate(id); - this.topbar.updateTabbar(); return; }, + updateLayout: function() { + if (!this.curRoom) return; // can happen during initialization + if (!this.sideRoom) { + this.curRoom.show('full'); + this.topbar.updateTabbar(); + return; + } + var leftMin = (this.curRoom.minWidth || this.curRoom.bestWidth); + var rightMin = (this.sideRoom.minWidth || this.sideRoom.bestWidth); + var available = $('body').width(); + if (this.curRoom.isSideRoom) { + // we're trying to focus a side room + if (available >= this.rooms[''].minWidth + leftMin) { + // it fits to the right of the main menu, so do that + this.curSideRoom = this.sideRoom = this.curRoom; + this.curRoom = this.rooms['']; + leftMin = (this.curRoom.minWidth || this.curRoom.bestWidth); + rightMin = (this.sideRoom.minWidth || this.sideRoom.bestWidth); + } else if (this.sideRoom) { + // nooo + if (this.curSideRoom) { + this.curSideRoom.hide(); + this.curSideRoom = null; + } + this.curRoom.show('full'); + this.topbar.updateTabbar(); + return; + } + } + if (available < leftMin + rightMin) { + if (this.curSideRoom) { + this.curSideRoom.hide(); + this.curSideRoom = null; + } + this.curRoom.show('full'); + this.topbar.updateTabbar(); + return; + } + this.curSideRoom = this.sideRoom; + var leftMax = (this.curRoom.maxWidth || this.curRoom.bestWidth); + var rightMax = (this.sideRoom.maxWidth || this.sideRoom.bestWidth); + var rightWidth = rightMin; + if (leftMax + rightMax <= available) { + rightWidth = rightMax; + } else { + available -= leftMin + rightMin; + var wanted = leftMax - leftMin + rightMax - rightMin; + if (wanted) rightWidth = Math.floor(rightMin + (rightMax - rightMin) * available / wanted); + } + this.curRoom.show('left', rightWidth); + this.curSideRoom.show('right', rightWidth); + this.topbar.updateTabbar(); + }, + updateSideRoom: function(id) { + if (id && this.rooms[id].isSideRoom) { + this.sideRoom = this.rooms[id]; + if (this.curSideRoom && this.curSideRoom !== this.sideRoom) { + this.curSideRoom.hide(); + this.curSideRoom = this.sideRoom; + } + // updateLayout will null curSideRoom if there's + // no room for this room + } + }, leaveRoom: function(id) { var room = this.rooms[id]; if (!room) return false; @@ -116,7 +187,12 @@ if (room === this.curRoom) this.focusRoom(''); delete this.rooms[id]; room.destroy(); - this.topbar.updateTabbar(); + if (room === this.sideRoom) { + this.sideRoom = null; + this.curSideRoom = null; + this.updateSideRoom(); + } + this.updateLayout(); return true; } return false; @@ -135,24 +211,41 @@ }, '$tabbar': null, updateTabbar: function() { - var curId = ''; - if (app.curRoom) curId = app.curRoom.id; + var curId = (app.curRoom ? app.curRoom.id : ''); + var curSideId = (app.curSideRoom ? app.curSideRoom.id : ''); + var buf = ''; var atLeastOne = false; + var sideBuf = ''; for (var id in app.rooms) { if (!id) continue; var name = id; if (id === 'lobby') name = ' Lobby chat'; else if (id === 'teambuilder') name = ' Teambuilder'; else if (id === 'ladder') name = ' Ladder'; + if (app.rooms[id].isSideRoom) { + if (!sideBuf) sideBuf = '