Gitlab expand all discussions

Add a new button to expand all discussions

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name        Gitlab expand all discussions
// @description Add a new button to expand all discussions
// @include     https://gitlab.*/*
// @require     https://code.jquery.com/jquery-3.3.1.slim.min.js
// @version     1
// @grant       none
// @namespace https://greatest.deepsurf.us/users/4947
// ==/UserScript==

// https://gitlab.com/gitlab-org/gitlab-ce/issues/19149#note_103194373

'use strict';

const intervalDelay = 1000;
const buttonDelay = 200;

const State = Object.freeze({
	EMPTY: 'EMPTY',
	SOME_CLOSE: 'SOME_CLOSE',
	NO_CLOSE: 'NO_CLOSE',
});

const textMap = Object.freeze({
	[State.SOME_CLOSE]: 'expand all discussions',
	[State.NO_CLOSE]: 'collapse all discussions',
});

(function ready() {
	const templateDiv = $('.line-resolve-all');

	if (!templateDiv.length)
	{
		setTimeout(ready, intervalDelay);
		return;
	}

	function getToggleState() {
		const all = $('.discussion-toggle-button');
		if (!all.length) return State.EMPTY;

		const closed = all.filter(':has(i.fa-chevron-down)');

		return closed.length ? State.SOME_CLOSE : State.NO_CLOSE;
	}

	function updateDiv(state) {
		if (!(state in State)) state = getToggleState();

		if (state === State.EMPTY) {
			newDiv.hide();
			return;
		}

		newDiv.find('.line-resolve-text > a').text(textMap[state]);
	}

	function updateDivDelay() {
		setTimeout(updateDiv, buttonDelay);
		setTimeout(updateDiv, buttonDelay * 2);
	}

	const newDiv = templateDiv.clone();

	newDiv.find('.is-disabled').remove();
	newDiv.find('.line-resolve-text').attr('type', 'button').text('').append('<a>')
	newDiv.click(() => {
		const state = getToggleState();
		let nextState;

		switch (state) {
			case State.SOME_CLOSE:
				$('.discussion-toggle-button:has(i.fa-chevron-down)').click();
				nextState = State.NO_CLOSE;
				break;
			case State.NO_CLOSE:
				$('.discussion-toggle-button').click();
				nextState = State.SOME_CLOSE;
				break;
		}

		updateDiv(nextState);
	});

	updateDiv();

	templateDiv.after(newDiv);

	$(document).on('click', 'button', updateDivDelay);

	// Should we add toggle button? seems not useful
})();