forum.js 2.99 KB
import './common/services/subscription';
import './common/controllers/subscription';
import './common/directives/frMarkdown';
import './poll/frPoll';
import './poll/frPollEditorModal';
import './common/jquery.litebox';
import './comment/frCommenter';

angular.module('footyroom')

.controller('forumCommentController', ['$scope', '$compile', '$element', function ($scope, $compile, $element) {
    var editorEl;
    var isEditing;

    var commentId = $element.attr('commentID');

    $scope.edit = function () {
        if (!isEditing) {
            isEditing = true;

            editorEl = $compile(
                '<forum-commenter class="edit-mode" comment-id=' + commentId + ' on-cancel="cancelEdit"></forum-commenter>'
            )($scope);

            $element.after(editorEl).hide();
        }
    };

    $scope.cancelEdit = function () {
        editorEl.remove();

        $element.show();

        isEditing = false;
    };
}])

.controller('NewPostController', ['$scope', '$http', '$window', function ($scope, $http, $window) {
    var isPosting;

    var wizardEl = angular.element('#newpost-wizard');

    $scope.step = 1;

    $scope.showCategories = function () {
        wizardEl.litebox();
    };

    $scope.selectCategory = function (id, title) {
        $scope.categoryId = id;
        $scope.categoryTitle = title;
        $scope.step = 2;
    };

    $scope.title = window.localStorage.getItem('postTitle');

    $scope.$watch('title', function (newValue, oldValue) {
        window.localStorage.setItem('postTitle', $scope.title || '');
    });

    $scope.publish = function () {
        if (isPosting) {
            return;
        }

        isPosting = true;

        wizardEl.find('.post-title').litelay();
        wizardEl.find('markdown-editor').litelay({ spinner: true });

        var data = {
            category: $scope.categoryId,
            title:	$scope.title,
            content:	$scope.content,
            honeypot: $scope.nameEmailUrl,
        };

        $http.post('/forum/discussions', $.param(data), { headers: {'Content-Type': 'application/x-www-form-urlencoded'} })

        .then(function (response) {
            window.localStorage.removeItem('postTitle');
            window.localStorage.removeItem('markdownContent');
            $window.location = response.data.url;
        })

        .catch(function (response) {
            $scope.errors = response.data.errors || "Something went wrong, but we don't know what. Try again later or report this to us.";

            isPosting = false;

            wizardEl.find('.post-title').litelay();
            wizardEl.find('markdown-editor').litelay();
        });
    };
}]);

//! !!!!!!
var Forum;

$(function () {
    var filter = $.QueryString.filter;

    if (filter) {
        $('#posts .filter [data-tab="' + filter + '"]').addClass('active');
    } else {
        $('#posts .filter [data-tab="allposts"]').addClass('active');
    }
});