initMainComments()

in static/src/javascripts/projects/common/modules/discussion/loader.js [296:405]


	initMainComments() {
		const commentId = this.getCommentIdFromHash();
		const order =
			userPrefs.get('discussion.order') ||
			(this.getDiscussionClosed() ? 'oldest' : 'newest');
		const threading = userPrefs.get('discussion.threading') || 'collapsed';

		const defaultPagesize = isBreakpoint({ min: 'tablet' }) ? 25 : 10;

		this.comments = new Comments({
			discussionId: this.getDiscussionId(),
			order,
			pagesize: defaultPagesize,
			threading,
		});

		if (this.comments) {
			this.comments.attachTo(qwery('.js-discussion-main-comments')[0]);
		}

		if (this.comments) {
			this.comments.on('untruncate-thread', () =>
				this.removeTruncation(),
			);
		}

		this.on('click,', '.js-discussion-author-link', () =>
			this.removeTruncation(),
		);

		this.on(
			'click',
			'.js-discussion-change-page, .js-discussion-show-button',
			() => {
				mediator.emit('discussion:comments:get-more-replies');
				this.removeTruncation();
			},
		);

		this.on('click', '.js-discussion-show-button', () => {
			document.dispatchEvent(new CustomEvent('comments-loaded'));
		});

		if (this.comments) {
			this.comments.on('rendered', (paginationHtml) => {
				const newPagination = bonzo.create(paginationHtml);
				const toolbarEl = qwery('.js-discussion-toolbar', this.elem)[0];
				const container = $(
					'.js-discussion-pagination',
					toolbarEl,
				).empty();

				// When the pagesize is 'All', do not show any pagination.
				if (this.comments && !this.comments.isAllPageSizeActive()) {
					container.html(newPagination);
				}
			});
		}

		this.setState('loading');

		this.on('user:loaded', () => {
			this.initState();
			this.renderCommentBar();

			if (this.user) {
				if (this.comments) {
					this.comments.addUser(this.user);
				}

				const userPageSize = userPrefs.get('discussion.pagesize');
				let pageSize = defaultPagesize;

				if (typeof userPageSize === 'number') {
					pageSize = userPageSize;
				} else if (userPageSize === 'All') {
					pageSize = config.get('switches.discussionAllPageSize')
						? 'All'
						: 100;
				}

				this.initPageSizeDropdown(pageSize);

				if (
					this.comments &&
					config.get('switches.discussionPageSize') &&
					isBreakpoint({ min: 'tablet' })
				) {
					this.comments.options.pagesize = pageSize;
				}

				if (this.user && this.user.isStaff) {
					this.removeState('not-staff');
					this.setState('is-staff');
				}
			}

			// Only truncate the loaded comments on this initial fetch,
			// and when no comment ID or #comments location is present.
			const shouldTruncate =
				!commentId && window.location.hash !== '#comments';

			this.loadComments({
				comment: commentId,
				shouldTruncate,
			}).catch(() => this.logError('Comments'));
		});

		this.getUser();
	}