<commit_before>(function() {
    'use strict;'

    angular.module('app').controller('FindRoomController', ['Restangular', '$state', 'settings', '$timeout', '$q', '$modal', '$scope', 'statusService', 'logger', function(Restangular, $state, settings, $timeout, $q, $modal, $scope, statusService, logger) {
        if(!settings.defaultRoom) {
            $state.go('settings');
            return;
        }
        var self = this;
        self.isLoading = true;
        self.rooms = [];
        self.search = {
            minSize: 1,
            location: '',
            equipment: []
        };
        self.sizeChoices = [ 1, 2, 3, 4, 6, 8, 10, 12, 40 ];
        self.searchResult = [];
        self.locationChoices = [ { id: '', text: '<ANY>'} ];
        function selfMatch (item, room) {
            return _.some(room.Equipment, item);
        }
        self.equipmentChoices = [ 
            { text: 'Display', icon: 'custom-icon-display'},
            { text: 'Telephone', icon: 'custom-icon-phone', displayText: 'Phone' },
            { text: 'Whiteboard', icon: 'custom-icon-whiteboard' }
        ];
        self.equipmentSelected = function(e) {
            return _.some(self.search.equipment, function(i) { return i === e; });
        };
        self.toggleEquipment = function(e) {
            if(self.equipmentSelected(e)) {
                self.search.equipment = self.search.equipment.filter(function(i) { return i != e; }); 
            } else {
                self.search.equipment = self.search.equipment.concat([e]);
            }
        };
        self.floor = function(e) {
            return Math.floor(e);
        }
        
        Restangular.all('roomList').getList().then(function(roomLists) {
            var allRooms = [];
            return $q.all(roomLists.map(function(roomList) {
                return Restangular.one('roomList', roomList.Address).getList('rooms').then(function(rooms) {
                    return $q.all(rooms.map(function(room) {
                        return $q.all([
                            Restangular.one('room', room.Address).one('info').get({ securityKey: '' }),
                            Restangular.one('room', room.Address).one('status').get()
                        ]).then(function(calls){
                            var merged = angular.merge({ Address: room.Address }, calls[0], calls[1]);
                            allRooms.push(merged);
                        });
                    }));
                });
            })).then(function() {
                allRooms.sort(function(a, b) { return a.DisplayName < b.DisplayName ? -1 : 1; });
                
                allRooms.forEach(function(r) {
                    r.Location = !r.BuildingId ? null : r.Floor ? r.BuildingId + " - " + r.Floor + 'th floor' : r.BuildingId; 
                });
                self.locationChoices = _.unique(allRooms.map(function(r) { return r.Location; }).filter(function(l) { return l; })).map(function(l) { return { id: l, text: l }; }).concat([ { id: '', text: '<ANY>'} ]);
                
                var defaultRoom = _.findWhere(allRooms, { Address: settings.defaultRoom });
                if(defaultRoom) {
                    self.search.minSize = defaultRoom.Size || 0;
                    self.search.equipment = (defaultRoom.Equipment || []).map(function(i) {
                        return _.find(self.equipmentChoices, { text: i });
                    });
                    if(defaultRoom.BuildingId) {
                        // default room has a building, so let's filter our data by that
                        allRooms = _.where(allRooms, { BuildingId: defaultRoom.BuildingId });
                    }
                }

                self.rooms = allRooms;
            });
        }).then(function() {
            self.isLoading = false;
        }).catch(function(err) {
            logger.error(err);
            $state.go('settings');
        });
        
        self.showDetails = function showDetails(meeting) {
            $modal.open({
                templateUrl: 'meeting/meeting.html',
                controller: 'MeetingController',
                controllerAs: 'c',
                resolve: {
                    meeting: function() {
                        return meeting;
                    }
                }
            });
        };
        
        self.showRoomDetails = function showDetails(calendar) {
            var room = _.find(self.rooms, { DisplayName: calendar.DisplayName });
            $modal.open({
                templateUrl: 'roomDetail/roomDetail.html',
                controller: 'RoomDetailController',
                controllerAs: 'c',
                resolve: {
                    room: function() {
                        return room;
                    }
                }
            });
        };
        
        $scope.$watch('[ c.rooms, c.search ]', function() {
            self.searchResults = self.rooms.map(function(room) {
                var matchLocation = !self.search.location || room.Location == self.search.location;
                var matchSize = self.search.minSize <= (room.Size || 0);
                var matchEquipment = _.all(self.search.equipment, function(e) {
                    return _.some(room.Equipment, e.match);
                });
                
                room = angular.copy(room);
                room.match = matchLocation && matchSize && matchEquipment;
                room.status = statusService.status(room.CurrentMeeting, room.NearTermMeetings);
                room.status.freeFor = room.status.freeFor === 0 ? 0 : Math.floor(Math.min(120, room.status.freeFor || 120)); 
                room.group = room.status.freeAt === 0 ? 'Available Now' : room.status.freeAt ? ('Available at ' + moment(room.status.freeAt).format('h:mm a')) : null;
                return room;
            }).filter(function(r) {
                return r.match && r.group && r.status.freeFor > 0;
            });
            var roomCount = self.searchResults.length;
            self.searchResults = _.pairs(_.groupBy(self.searchResults, 'group')).map(function(g) {
                return {
                    key: g[0],
                    rooms: g[1],
                    freeAt: g[1][0].status.freeAt
                };
            });;
            self.searchResults.sort(function(a,b) {
                if(a.freeAt === 0) {
                    return -1;
                }
                if(b.freeAt === 0) {
                    return 1;
                }
                if(!a.freeAt) { 
                    return 1;
                }
                if(!b.freeAt) {
                    return -1;
                }
                if(a.freeAt != b.freeAt) {
                    return a.freeAt < b.freeAt ? -1 : 1;
                }
                return a.key < b.key ? -1 : 1;
            });
            self.searchResults.forEach(function (g) {
                g.rooms.sort(function(a,b) {
                    if(a.status.freeFor != b.status.freeFor) {
                        return a.status.freeFor > b.status.freeFor ? -1 : 1;
                    }
                    return a.DisplayText < b.DisplayText ? -1 : 1;
                });
            });
            var groupCount = self.searchResults.length;
            self.scrollWidth = 40 + roomCount * (330 + 40) + groupCount * 20; // width + padding
        }, true);

        // we have a default room we're supposed to be managing - time out and go there after 60 seconds<commit_msg>fix up the algorithm for scrollable area width<commit_after>(function() {
    'use strict;'

    angular.module('app').controller('FindRoomController', ['Restangular', '$state', 'settings', '$timeout', '$q', '$modal', '$scope', 'statusService', 'logger', function(Restangular, $state, settings, $timeout, $q, $modal, $scope, statusService, logger) {
        if(!settings.defaultRoom) {
            $state.go('settings');
            return;
        }
        var self = this;
        self.isLoading = true;
        self.rooms = [];
        self.search = {
            minSize: 1,
            location: '',
            equipment: []
        };
        self.sizeChoices = [ 1, 2, 3, 4, 6, 8, 10, 12, 40 ];
        self.searchResult = [];
        self.locationChoices = [ { id: '', text: '<ANY>'} ];
        function selfMatch (item, room) {
            return _.some(room.Equipment, item);
        }
        self.equipmentChoices = [ 
            { text: 'Display', icon: 'custom-icon-display'},
            { text: 'Telephone', icon: 'custom-icon-phone', displayText: 'Phone' },
            { text: 'Whiteboard', icon: 'custom-icon-whiteboard' }
        ];
        self.equipmentSelected = function(e) {
            return _.some(self.search.equipment, function(i) { return i === e; });
        };
        self.toggleEquipment = function(e) {
            if(self.equipmentSelected(e)) {
                self.search.equipment = self.search.equipment.filter(function(i) { return i != e; }); 
            } else {
                self.search.equipment = self.search.equipment.concat([e]);
            }
        };
        self.floor = function(e) {
            return Math.floor(e);
        }
        
        Restangular.all('roomList').getList().then(function(roomLists) {
            var allRooms = [];
            return $q.all(roomLists.map(function(roomList) {
                return Restangular.one('roomList', roomList.Address).getList('rooms').then(function(rooms) {
                    return $q.all(rooms.map(function(room) {
                        return $q.all([
                            Restangular.one('room', room.Address).one('info').get({ securityKey: '' }),
                            Restangular.one('room', room.Address).one('status').get()
                        ]).then(function(calls){
                            var merged = angular.merge({ Address: room.Address }, calls[0], calls[1]);
                            allRooms.push(merged);
                        });
                    }));
                });
            })).then(function() {
                allRooms.sort(function(a, b) { return a.DisplayName < b.DisplayName ? -1 : 1; });
                
                allRooms.forEach(function(r) {
                    r.Location = !r.BuildingId ? null : r.Floor ? r.BuildingId + " - " + r.Floor + 'th floor' : r.BuildingId; 
                });
                self.locationChoices = _.unique(allRooms.map(function(r) { return r.Location; }).filter(function(l) { return l; })).map(function(l) { return { id: l, text: l }; }).concat([ { id: '', text: '<ANY>'} ]);
                
                var defaultRoom = _.findWhere(allRooms, { Address: settings.defaultRoom });
                if(defaultRoom) {
                    self.search.minSize = defaultRoom.Size || 0;
                    self.search.equipment = (defaultRoom.Equipment || []).map(function(i) {
                        return _.find(self.equipmentChoices, { text: i });
                    });
                    if(defaultRoom.BuildingId) {
                        // default room has a building, so let's filter our data by that
                        allRooms = _.where(allRooms, { BuildingId: defaultRoom.BuildingId });
                    }
                }

                self.rooms = allRooms;
            });
        }).then(function() {
            self.isLoading = false;
        }).catch(function(err) {
            logger.error(err);
            $state.go('settings');
        });
        
        self.showDetails = function showDetails(meeting) {
            $modal.open({
                templateUrl: 'meeting/meeting.html',
                controller: 'MeetingController',
                controllerAs: 'c',
                resolve: {
                    meeting: function() {
                        return meeting;
                    }
                }
            });
        };
        
        self.showRoomDetails = function showDetails(calendar) {
            var room = _.find(self.rooms, { DisplayName: calendar.DisplayName });
            $modal.open({
                templateUrl: 'roomDetail/roomDetail.html',
                controller: 'RoomDetailController',
                controllerAs: 'c',
                resolve: {
                    room: function() {
                        return room;
                    }
                }
            });
        };
        
        $scope.$watch('[ c.rooms, c.search ]', function() {
            self.searchResults = self.rooms.map(function(room) {
                var matchLocation = !self.search.location || room.Location == self.search.location;
                var matchSize = self.search.minSize <= (room.Size || 0);
                var matchEquipment = _.all(self.search.equipment, function(e) {
                    return _.some(room.Equipment, e.match);
                });
                
                room = angular.copy(room);
                room.match = matchLocation && matchSize && matchEquipment;
                room.status = statusService.status(room.CurrentMeeting, room.NearTermMeetings);
                room.status.freeFor = room.status.freeFor === 0 ? 0 : Math.floor(Math.min(120, room.status.freeFor || 120)); 
                room.group = room.status.freeAt === 0 ? 'Available Now' : room.status.freeAt ? ('Available at ' + moment(room.status.freeAt).format('h:mm a')) : null;
                return room;
            }).filter(function(r) {
                return r.match && r.group && r.status.freeFor > 0;
            });
            var roomCount = self.searchResults.length;
            self.searchResults = _.pairs(_.groupBy(self.searchResults, 'group')).map(function(g) {
                return {
                    key: g[0],
                    rooms: g[1],
                    freeAt: g[1][0].status.freeAt
                };
            });;
            self.searchResults.sort(function(a,b) {
                if(a.freeAt === 0) {
                    return -1;
                }
                if(b.freeAt === 0) {
                    return 1;
                }
                if(!a.freeAt) { 
                    return 1;
                }
                if(!b.freeAt) {
                    return -1;
                }
                if(a.freeAt != b.freeAt) {
                    return a.freeAt < b.freeAt ? -1 : 1;
                }
                return a.key < b.key ? -1 : 1;
            });
            self.searchResults.forEach(function (g) {
                g.rooms.sort(function(a,b) {
                    if(a.status.freeFor != b.status.freeFor) {
                        return a.status.freeFor > b.status.freeFor ? -1 : 1;
                    }
                    return a.DisplayText < b.DisplayText ? -1 : 1;
                });
            });
            var groupCount = self.searchResults.length;
            self.scrollWidth = 30 + roomCount * (330 + 20*2) + groupCount * 60; // calculate the full width of the scrolling container based on the expected sizes of the various elements
        }, true);

        // we have a default room we're supposed to be managing - time out and go there after 60 seconds
{'fraction_non_alphanumeric': 0.1353571193902359, 'fraction_numerical': 0.00768775872264932, 'mean_word_length': 1.3729342064234487, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5218482', 'n_tokens_mistral': 4256, 'n_tokens_neox': 3932, 'n_words': 775}