(function () { "use strict"; let tagsReset = false; let chrono = false; let albums = []; let year = 0; angular.module('streamApp').controller("streamController", StreamController); StreamController.$inject = ['$scope', 'streamService', 'userService', 'timeService', '$location', '$routeParams']; function StreamController($scope, streamService, userService, timeService, $location, $routeParams) { userService.getUser().then(function (data) { $scope.userName = data.userName; }); const date = new Date(); let first = true; $scope.filters = { albums: albums, people: [], allPeople: [], peopleIds: [], me: false, month: date.getMonth() + 1, day: date.getDate(), useDay: false, dayOfMonth: [], chronological: chrono }; $scope.clearTags = function () { $scope.filters.albums = []; streamService.clearTags().then(x => { $scope.updatePeopleAndTags(); } ); }; $scope.messageTime = ["If you select a day of the year, you can see memories from around that day " + "in previous years.", "This is a good way to re-live a time of year like going back to school or Christmas." ]; $scope.messageDate = ["View memories ordered by when they happened."]; const days = getDaysOfMonth(); $scope.uploading = []; $scope.months = { 1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December' }; function getDays() { $scope.filters.dayOfMonth = []; for (var day = 1; day < days[$scope.filters.month - 1] + 1; day++) { $scope.filters.dayOfMonth.push(day); } $scope.filters.day = Math.min($scope.filters.day, $scope.filters.dayOfMonth.length); }; $scope.updateDays = function () { getDays(); $scope.updateDay(); }; $scope.updateDay = function () { if ($scope.filters.useDay) { $scope.reload(); } }; getDays(); $scope.dropUploading = streamService.isUploading(finishedUpload); if ($scope.dropUploading) { $scope.uploading.push(true); }; function finishedUpload() { $scope.uploading.pop(); let indexOf = $scope.stream.findIndex(s => s.drop = $scope.dropUploading); if (indexOf >= 0) { streamService.getMemory($scope.dropUploading).then(memory => $scope.stream[indexOf] = memory); } }; $scope.useDay = function () { $scope.filters.useDay = !$scope.filters.useDay; $scope.reload(); }; $scope.setYear = function () { year = $scope.filters.year; if ($scope.filters.chronological) { $scope.reload(); } }; $scope.chronological = function () { $scope.filters.chronological = chrono = !$scope.filters.chronological; if (!$scope.filters.chronological) { $scope.filters.useDay = false; // if we are not chronological - turn off day filter } $scope.reload(); }; $scope.newTag = false; $scope.newDrop = false; $scope.dropEdit = false; $scope.change = false; $scope.crud = { hour: '', minute: '', content: '', drop: '', date: '', images: [] }; $scope.editTags = false; /*This should be renamed - it is used to clear filters*/ $scope.tagEditor = function () { $scope.editTags = !$scope.editTags; $scope.newTag = false; }; $scope.$watch('editTags', setHeight); $scope.zoom = function (img) { $scope.bigImage = img; }; $scope.hideImage = function () { $scope.bigImage = false; }; $scope.updatePeopleAndTags = function () { streamService.getFilter().then(function (data) { $scope.filters.albums = albums = data.data.albums; $scope.filters.me = data.data.me; $scope.filters.allPeople = data.data.allPeople; $scope.filters.people = $scope.filters.allPeople.filter(s => s.selected); $scope.filters.years = data.data.years; $scope.filters.year = year || data.data.years[0]; if ($routeParams.albumId && first) { let album = $scope.filters.albums.find(x => x.albumId === parseInt($routeParams.albumId)); if (album) { album.selected = true; } } first = false; $scope.reload(); }); }; $scope.addMe = function () { $scope.filters.me = !$scope.filters.me; if ($scope.filters.tags && $scope.filters.tags.length) { $scope.removeTag($scope.filters.tags[0]); } else { $scope.reload(); } }; $scope.removeMe = function () { $scope.filters.me = false; $scope.reload(); }; $scope.reload = function () { if (once) { streamUp = []; streamDown = []; $scope.isFiltered = $scope.filters.me || $scope.filters.useDay || $scope.filters.chronological || $scope.filters.albums.filter(x => x.selected).length || $scope.filters.people.length; once = false; streamService.getStream( $scope.filters.albums.filter(x => x.selected).map(x => x.albumId), $scope.filters.people.map(x => x.Id), [], true, $scope.filters.me, $scope.filters.day, $scope.filters.month, $scope.filters.useDay, $scope.filters.chronological, $scope.filters.year, ).then(function (data) { $scope.stream = data; setHeight(); once = true; }).catch(() => once = true); sortTagItems(); $('.waiting').hide(); } }; $scope.addHashTag = function (tag) { var foundTag = $scope.filters.allTags.find(x => x.tagId === tag.tagId); if (foundTag) $scope.addTag(foundTag); }; $scope.addPerson = function (person) { if (person.selected) return $scope.removePerson(person); $scope.filters.people = $scope.filters.allPeople.filter(p => p.selected); $scope.filters.peopleIds = $scope.filters.people.map(p => p.Id); streamService.addPerson(person.Id).then(() => $scope.reload() ); }; $scope.addAlbum = function (album) { $scope.reload(); }; $scope.removePerson = function (person) { $scope.filters.people = $scope.filters.allPeople.filter(p => p.selected); $scope.filters.peopleIds = $scope.filters.people.map(p => p.Id); streamService.removePerson(person.Id).then(() => $scope.reload() ); }; $scope.addNewDrop = function (prompt) { $location.url('/memory/add?questionId=' + prompt.promptId); }; var once = true; var streamUp = []; var streamDown = []; $scope.showLess = function () { if ($scope.stream && $scope.stream.length > 20) { let tempStream = $scope.stream.splice(10); streamDown = tempStream.concat(streamDown); } if (streamUp.length) { let tempStream = streamUp.splice(-10).concat($scope.stream); $scope.stream = tempStream; } }; $scope.showMore = function () { if ($scope.stream.length > 7) { if (once && !streamDown.length) { once = false; streamService.getStream( $scope.filters.albums.filter(x => x.selected).map(x => x.albumId), $scope.filters.people.map(x => x.Id), $scope.stream, false, $scope.filters.me, $scope.filters.day, $scope.filters.month, $scope.filters.useDay, $scope.filters.chronological, $scope.filters.year ).then(function (data) { $scope.stream = data; setHeight(); removeFromUp(); once = true; }); } else { removeFromUp(); let tempStream = streamDown.splice(0, 10); $scope.stream = $scope.stream.concat(tempStream); } } }; function removeFromUp() { if ($scope.stream && $scope.stream.length > 20) { streamUp = streamUp.concat($scope.stream.splice(0, 10)); } } if (!tagsReset) { $scope.clearTags(); tagsReset = true; } else { $scope.updatePeopleAndTags(); } function sort(items) { items = items || []; return sortName(items); } function sortTagItems() { $scope.filters.tags = sort($scope.filters.tags); $scope.filters.people = sort($scope.filters.people); $scope.filters.allTags = sort($scope.filters.allTags); $scope.filters.allPeople = sort($scope.filters.allPeople); } function setHeight() { /* var headerBottom = $('.site-title').position().top + $('.site-title').outerHeight(true) + 75; var windowHeight = $(window).height(); var height = windowHeight - headerBottom; height = height > 1024 ? height : windowHeight; $('.reset').height(height); */ } } })();