Skip to content

Commit

Permalink
Fix / Refat: Avoid forum filters duplicate and refactoring discussion…
Browse files Browse the repository at this point in the history
…-controllers for readibility
  • Loading branch information
frclba committed Jun 30, 2020
1 parent bed289f commit 9a19ec1
Showing 1 changed file with 87 additions and 59 deletions.
146 changes: 87 additions & 59 deletions discussion/static/js/discussion-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,74 @@

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);
});
}
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);
});
Expand All @@ -48,7 +95,7 @@
}
);
}
$scope.user = CurrentUser;

// Pagination controls
$scope.forum_pages_max_number = 10;
$scope.forum_topics_page = 20;
Expand All @@ -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;
},
Expand All @@ -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({
Expand All @@ -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";
Expand Down Expand Up @@ -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 = []
Expand All @@ -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 );
Expand All @@ -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;
});
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
);
Expand Down

0 comments on commit 9a19ec1

Please sign in to comment.