diff --git a/discussion/static/js/discussion-controllers.js b/discussion/static/js/discussion-controllers.js index ed1d492..308b401 100644 --- a/discussion/static/js/discussion-controllers.js +++ b/discussion/static/js/discussion-controllers.js @@ -4,15 +4,62 @@ app.controller('ForumCtrl', ['$scope', '$routeParams', '$http', '$location', 'Category', 'Forum', 'Tag', 'Topic', 'TopicPage', 'CurrentUser', function ($scope, $routeParams, $http, $location, Category, Forum, Tag, Topic, TopicPage, CurrentUser) { + + const forum_id = $routeParams.forumId; + $scope.user = CurrentUser; + + if(forum_id) { + singleInit(); + } else { + normalInit(); + } + + function singleInit() { + Forum.get({id: forum_id}, (forum) => { + // console.log("singleInit") + $scope.filters = undefined; + $scope.forum_search = false; + $scope.forum_single = true; + $scope.forums = []; + $scope.forum = forum; + $scope.forum.current_page = 1; + $scope.forum.page = TopicPage.get({ + page: 1, + page_size: $scope.forum_topics_page, + forum: forum_id, + ordering: '-last_activity_at'}, + function(page){ + $scope.forum.topics = page.results; + $scope.forum_topics_total = page.count; + $scope.topics_loaded = true; + }, + function(err){ + console.log("Erro ao carregar os tópicos"); + } + ); + $scope.forums.push(forum); // to reuse template's ng-repeat + },function(err){ + normalInit(); + }); + } + function normalInit() { + // console.log("normalInit") $scope.filters = {}; $scope.forum_single = false; - if($routeParams['categories'] || $routeParams['tags']) { - if($routeParams['categories']) { - if(typeof $routeParams['categories'] === 'string' || typeof $routeParams['categories'] === 'number') - $scope.filters.categories = [$routeParams['categories']]; + const categoriesParams = $routeParams['categories']; + const tagParams = $routeParams['tags'] + + // console.log(categoriesParams) + // console.log(tagParams) + + if(categoriesParams || tagParams) { + if(categoriesParams) { + if(typeof categoriesParams === 'string' || typeof categoriesParams === 'number') + $scope.filters.categories = [categoriesParams]; else - $scope.filters.categories = $routeParams['categories']; + $scope.filters.categories = categoriesParams; + $scope.filters.categories = $scope.filters.categories.map(function(cat) { return angular.fromJson(cat); }); @@ -20,11 +67,11 @@ else { $scope.filters.categories = []; } - if($routeParams['tags']) { - if(typeof $routeParams['tags'] === 'string' || typeof $routeParams['tags'] === 'number') - $scope.filters.tags = [$routeParams['tags']]; + if(tagParams) { + if(typeof tagParams === 'string' || typeof tagParams === 'number') + $scope.filters.tags = [tagParams]; else - $scope.filters.tags = $routeParams['tags']; + $scope.filters.tags = tagParams; $scope.filters.tags = $scope.filters.tags.map(function(tag) { return angular.fromJson(tag); }); @@ -48,7 +95,7 @@ } ); } - $scope.user = CurrentUser; + // Pagination controls $scope.forum_pages_max_number = 10; $scope.forum_topics_page = 20; @@ -60,6 +107,10 @@ forum: forum_id, ordering: '-last_activity_at'}, function(page){ + // console.log(page) + // console.log($scope.forum_topics_total) + // console.log($scope.forum.current_page) + // console.log($scope.forum_topics_page) $scope.forum.topics = page.results; $scope.topics_loaded = true; }, @@ -69,42 +120,6 @@ ); }; - function singleInit() { - Forum.get({id: forum_id},function(forum){ - $scope.filters = undefined; - $scope.forum_search = false; - $scope.forum_single = true; - $scope.forums = []; - $scope.forum = forum; - $scope.forum.current_page = 1; - $scope.forum.page = TopicPage.get({ - page: 1, - page_size: $scope.forum_topics_page, - forum: forum_id, - ordering: '-last_activity_at'}, - function(page){ - $scope.forum.topics = page.results; - $scope.forum_topics_total = page.count; - $scope.topics_loaded = true; - }, - function(err){ - console.log("Erro ao carregar os tópicos"); - } - ); - $scope.forums.push(forum); // to reuse template's ng-repeat - },function(err){ - normalInit(); - }); - } - - var forum_id = $routeParams.forumId; - - if(forum_id) { - singleInit(); - } else { - normalInit(); - } - $scope.getResults = function(txt) { $scope.current_search = txt; TopicPage.get({ @@ -113,7 +128,11 @@ page_size: $scope.forum_topics_page, ordering: '-last_activity_at', ignoreLoadingBar: true}, - function(page){ + function(page){ + // console.log(page) + // console.log($scope.forum_topics_total) + // console.log($scope.forum.current_page) + // console.log($scope.forum_topics_page) $scope.forums = []; $scope.forum = {}; $scope.forum.title = "Resultados de busca"; @@ -160,7 +179,8 @@ window.location.hash = new_url; } - $scope.forumFilter = function(operation,type,filter_obj) { + $scope.forumFilter = function(operation, type, filter_obj) { + if(!$scope.filters) { $scope.filters = {}; $scope.filters.categories = [] @@ -171,23 +191,21 @@ clear_filters(); } - if(type == 'cat') { - if(operation == 'add') { - if($scope.filters.categories.indexOf(filter_obj) > -1) { - return; - } - $scope.filters.categories.push(filter_obj); + if(type === 'cat') { + if(operation === 'add') { + $scope.filters.categories.some(obj => obj.name === filter_obj.name) ? + console.log('already filtering by this category') : + $scope.filters.categories.push(filter_obj); } else { $scope.filters.categories.splice( $scope.filters.categories.indexOf(filter_obj), 1 ); } } else { - if(operation == 'add') { - if($scope.filters.tags.indexOf(filter_obj) > -1) { - return; - } - $scope.filters.tags.push(filter_obj); + if(operation === 'add') { + $scope.filters.tags.some(obj => obj.name === filter_obj.name) ? + console.log('already filtering by this tag') : + $scope.filters.tags.push(filter_obj); } else { $scope.filters.tags.splice( $scope.filters.tags.indexOf(filter_obj), 1 ); @@ -208,6 +226,14 @@ return el.id; }) //array with tag id's }, function(r) { + // r.forEach(forum => { + // $scope.forum.forum_topics_total = forum.topics.length; + // }) + // $scope.forum_topics_total = r.topics.length + // console.log(r) + // console.log($scope.forum_topics_total) + // console.log($scope.forum.current_page) + // console.log($scope.forum_topics_page) $scope.forum_search = true; }); } @@ -313,6 +339,7 @@ Forum, Category, Tag, Topic, TopicFile, TopicRead, Comment, TopicLike, CommentLike, CommentFile, CurrentUser, ContentFile) { $scope.topic = Topic.get({id: $routeParams.topicId}, function(topic){ + // console.log(topic) // Mark topic as read if (topic.categories.length > 0) $scope.category_id = $scope.topic.categories[0].id; @@ -324,6 +351,7 @@ //Filter the topics from Forum Forum.get({id:$scope.topic.forum}, function(t) { + // console.log(t) $scope.forum_categories = t.category; } );