Skip to content

Commit

Permalink
Merge pull request #23 from hacklabr/develop
Browse files Browse the repository at this point in the history
Develop into master
  • Loading branch information
frclba authored Jun 30, 2020
2 parents e46225c + b388eec commit 35a2d0a
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 72 deletions.
185 changes: 114 additions & 71 deletions discussion/static/js/discussion-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,80 @@
'use strict';
var app = angular.module('discussion.controllers', ['ngSanitize']);

app.controller('ForumCtrl', ['$scope', '$routeParams', '$http', '$location', 'Category', 'Forum', 'Tag', 'Topic', 'TopicPage', 'CurrentUser',
function ($scope, $routeParams, $http, $location, Category, Forum, Tag, Topic, TopicPage, CurrentUser) {
app.controller('ForumCtrl', ['$scope', '$routeParams', '$http', '$location', 'Category', 'Forum', 'ForumPage', 'Tag', 'Topic', 'TopicPage', 'CurrentUser',
function ($scope, $routeParams, $http, $location, Category, Forum, ForumPage, Tag, Topic, TopicPage, CurrentUser) {

const forum_id = $routeParams.forumId;
$scope.user = CurrentUser;

$scope.forum = {}
$scope.topics = {}
$scope.search = {txt:""}
// Pagination Params
$scope.forum_pages_max_number = 10;
$scope.forum_topics_page = 20;
$scope.forum.page_size = 10;
$scope.forum.current_page = 1

if(forum_id) {
singleInit();
} else {
normalInit();
}

function singleInit() {
Forum.get({id: forum_id}, (forum) => {
$scope.filters = undefined;
$scope.forum_search = false;
$scope.forum_single = true;
$scope.forums = [];
$scope.forum = forum;
$scope.topics.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() {
$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']

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 @@ -39,7 +90,18 @@
$scope.filters.tags = [];
$scope.forum_search = false;
}
$scope.forums = Forum.query({});

$scope.forums = ForumPage.get({
search: $scope.current_search, // if there is a search in progress, keep it
page: $scope.forum.current_page,
page_size: $scope.forum.page_size,
}, (response) => {
$scope.forums = response.results
$scope.forum.total_forum_items = response.count
$scope.forum.max_size = response.length
$scope.forum_page_loaded = response.$resolved;
$scope.forum.has_next_page = (response.next !== null || response.previous !== null)
});
$scope.latest_topics = Topic.query({
limit: 6,
ordering: '-last_activity_at',
Expand All @@ -48,18 +110,17 @@
}
);
}
$scope.user = CurrentUser;


// Pagination controls
$scope.forum_pages_max_number = 10;
$scope.forum_topics_page = 20;
$scope.pageChanged = function(){
$scope.topicPageChanged = function(){
$scope.forum.page = TopicPage.get({
search: $scope.current_search, // if there is a search in progress, keep it
page: $scope.forum.current_page,
page: $scope.topics.current_page,
page_size: $scope.forum_topics_page,
forum: forum_id,
ordering: '-last_activity_at'},
function(page){
function(page){
$scope.forum.topics = page.results;
$scope.topics_loaded = true;
},
Expand All @@ -68,42 +129,18 @@
}
);
};

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();
$scope.forumPageChanged = () => {
$scope.forums = ForumPage.get({
search: $scope.current_search, // if there is a search in progress, keep it
page: $scope.forum.current_page,
page_size: $scope.forum.page_size
}, (response) => {
$scope.forums = response.results
$scope.forum.total_forum_items = response.count
$scope.forum.max_size = response.length
$scope.forum_page_loaded = response.$resolved;
});
}

var forum_id = $routeParams.forumId;

if(forum_id) {
singleInit();
} else {
normalInit();
}
};

$scope.getResults = function(txt) {
$scope.current_search = txt;
Expand All @@ -113,11 +150,10 @@
page_size: $scope.forum_topics_page,
ordering: '-last_activity_at',
ignoreLoadingBar: true},
function(page){
function(page){
$scope.forums = [];
$scope.forum = {};
$scope.forum.title = "Resultados de busca";
$scope.forum.current_page = 1;
$scope.topics.current_page = 1;
$scope.forum.topics = page.results;
$scope.forum_topics_total = page.count;
$scope.topics_loaded = true;
Expand All @@ -127,7 +163,7 @@
$scope.forum_single = false;
$scope.forums.push($scope.forum); // to reuse template's ng-repeat

},function(err){
}, function(err){
normalInit();
});
}
Expand All @@ -138,6 +174,15 @@
$scope.filters.tags = [];
}

$scope.clear_search = () => {
$scope.forum_search = false;
$scope.current_search = "";
$scope.forums = {}
$scope.topics_loaded = false;
$scope.search = {txt:""}
normalInit()
}

function set_route() {
var new_url = '#!/';
if (forum_id)
Expand All @@ -160,7 +205,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 +217,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 @@ -199,7 +243,7 @@
}

set_route();

$scope.forums = Forum.query({ // TODO: when single forum, filter only within it
categories : $scope.filters.categories.map(function(el) {
return el.id;
Expand Down Expand Up @@ -228,7 +272,6 @@

$scope.save_topic = function() {
$scope.sending = true;
// $scope.new_topic.forum = 1;
$scope.new_topic.categories = [$scope.category];
var topic_files = $scope.new_topic.files;
$scope.new_topic.$save(function(topic){
Expand Down
7 changes: 7 additions & 0 deletions discussion/static/js/discussion-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
{'id' : '@id'},
{'update': {'method': 'PUT'} });
}]);

app.factory('ForumPage', ['$resource', function($resource){
return $resource('/discussion/api/forum_page/:id',
{'id' : '@id'},
{'update': {'method': 'PUT'} });
}]);


app.factory('Topic', ['$resource', function($resource){
return $resource('/discussion/api/topic/:id',
Expand Down
3 changes: 2 additions & 1 deletion discussion/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
ContentFileViewSet, ForumCreateView, ForumDeleteView, ForumFileViewSet,
ForumListView, ForumSearchViewSet, ForumUpdateView, ForumView, ForumViewSet,
TagViewSet, TopicFileViewSet, TopicLikeViewSet, TopicNotificationViewSet,
TopicPageViewSet, TopicReadViewSet, TopicTypeaheadViewSet, TopicViewSet
TopicPageViewSet, TopicReadViewSet, TopicTypeaheadViewSet, TopicViewSet, ForumPageViewSet
)

from rest_framework import routers
Expand All @@ -19,6 +19,7 @@
router = routers.SimpleRouter(trailing_slash=False)
router.register(r'category', CategoryViewSet)
router.register(r'forum', ForumViewSet)
router.register(r'forum_page', ForumPageViewSet)
router.register(r'topic', TopicViewSet)
router.register(r'topic_page', TopicPageViewSet)
router.register(r'comment', CommentViewSet)
Expand Down
8 changes: 8 additions & 0 deletions discussion/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ def perform_create(self, serializer):
def perform_update(self, serializer):
serializer.save(author=self.request.user)

class ForumPagination(PageNumberPagination):
page_size = 10
page_size_query_param = 'page_size'
max_page_size = 50

class ForumPageViewSet(ForumViewSet):
pagination_class = ForumPagination


class ForumSearchViewSet(viewsets.ReadOnlyModelViewSet):
"""
Expand Down

0 comments on commit 35a2d0a

Please sign in to comment.