diff --git a/www/calendar/inner.js b/www/calendar/inner.js index 80b5606f2..2789371ca 100644 --- a/www/calendar/inner.js +++ b/www/calendar/inner.js @@ -134,8 +134,9 @@ define([ }; - var getCalendars = function () { + var LOOKUP = {}; + var TEAMS = {}; return Object.keys(APP.calendars).map(function (id) { var c = APP.calendars[id]; if (c.hidden || c.restricted || c.loading) { return; } @@ -149,7 +150,23 @@ define([ dragBgColor: md.color, borderColor: md.color, }; - }).filter(Boolean); + }).filter(Boolean).map(function (obj) { + var id = obj.id; + var cal = APP.calendars[id]; + var team = cal.teams.sort()[0] || cal.roTeams.sort()[0]; + var title = Util.find(cal, ['content', 'metadata', 'title']) || ''; + LOOKUP[id] = title; + TEAMS[id] = team; + return obj; + }).sort(function (a, b) { + var team1 = TEAMS[a.id]; + var team2 = TEAMS[b.id]; + var t1 = LOOKUP[a.id]; + var t2 = LOOKUP[b.id]; + return team1 > team2 ? 1 : + (team1 < team2 ? -1 : ( + t1 > t2 ? 1 : (t1 < t2 ? -1 : 0))); + }); }; var getSchedules = function () { var s = []; @@ -660,10 +677,21 @@ define([ $calendars.empty(); var privateData = metadataMgr.getPrivateData(); var filter = function (teamId) { + var LOOKUP = {}; return Object.keys(APP.calendars || {}).filter(function (id) { var cal = APP.calendars[id] || {}; var teams = (cal.teams || []).map(function (tId) { return Number(tId); }); return teams.indexOf(typeof(teamId) !== "undefined" ? Number(teamId) : 1) !== -1; + }).map(function (k) { + // nearly constant-time pre-sort + var cal = APP.calendars[k] || {}; + var title = Util.find(cal, ['content', 'metadata', 'title']) || ''; + LOOKUP[k] = title; + return k; + }).sort(function (a, b) { + var t1 = LOOKUP[a]; + var t2 = LOOKUP[b]; + return t1 > t2 ? 1 : (t1 === t2 ? 0 : -1); }); }; var tempCalendars = filter(0); @@ -719,7 +747,7 @@ define([ editCalendar(); }).appendTo($newContainer); - Object.keys(privateData.teams).forEach(function (teamId) { + Object.keys(privateData.teams).sort().forEach(function (teamId) { var calendars = filter(teamId); if (!calendars.length) { return; } var team = privateData.teams[teamId];