<commit_before>// Copyright 2016 Documize Inc. <<EMAIL>>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <<EMAIL>>.
//
// https://documize.com

import $ from 'jquery';
import { computed } from '@ember/object';
import { schedule } from '@ember/runloop';
import { inject as service } from '@ember/service';
import Component from '@ember/component';
import tocUtil from '../../utils/toc';
import TooltipMixin from '../../mixins/tooltip';

export default Component.extend(TooltipMixin, {
	documentService: service('document'),
	isDesktop: false,
	emptyState: computed('pages', function () {
		return this.get('pages.length') === 0;
	}),
	canEdit: computed('permssions', 'document', function() {
		let constants = this.get('constants');
		let permissions = this.get('permissions');
		let authenticated = this.get('session.authenticated');
		let notEmpty = this.get('pages.length') > 0;

		if (notEmpty && authenticated && permissions.get('documentEdit') && this.get('document.protection') === constants.ProtectionType.None) return true;
		if (notEmpty && authenticated && permissions.get('documentApprove') && this.get('document.protection') === constants.ProtectionType.Review) return true;

		return false;
	}),

	init() {
		this._super(...arguments);
		this.state = {
			actionablePage: false,
			upDisabled: true,
			downDisabled: true,
			indentDisabled: true,
			outdentDisabled: true,
			pageId: ''
		};
	},

	didReceiveAttrs() {
		this._super(...arguments);
		let cp = this.get('currentPageId');
		this.setState(is.empty(cp) ? '' : cp);
	},

	didInsertElement() {
		this._super(...arguments);
		this.eventBus.subscribe('documentPageAdded', this, 'onDocumentPageAdded');
		this.eventBus.subscribe('resized', this, 'setSize');

		this.setSize();
		this.renderTooltips();
	},

	willDestroyElement() {
		this._super(...arguments);
		this.eventBus.unsubscribe('documentPageAdded');
		this.eventBus.unsubscribe('resized');

		let t = '#doc-toc';
		if (interact.isSet(t)) interact(t).unset();
		this.removeTooltips();
	},

	onDocumentPageAdded(pageId) { // eslint-disable-line no-unused-vars
		this.setSize();
	},

	setSize() {
		schedule('afterRender', () => {
			let isDesktop = $(window).width() >= 1800;
			this.set('isDesktop', isDesktop);

			if (isDesktop) {
				let h = $(window).height() - $("#nav-bar").height() - 140;
				$("#doc-toc").css('max-height', h);

				let i = $("#doc-view").offset();

				if (is.not.undefined(i)) {
					let l = i.left - 100;
					if (l > 350) l = 350;
					$("#doc-toc").width(l);

					$("#doc-toc").css({<commit_msg>Ensure document TOC listens to section add event<commit_after>// Copyright 2016 Documize Inc. <<EMAIL>>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <<EMAIL>>.
//
// https://documize.com

import $ from 'jquery';
import { computed } from '@ember/object';
import { schedule } from '@ember/runloop';
import { inject as service } from '@ember/service';
import Component from '@ember/component';
import tocUtil from '../../utils/toc';
import TooltipMixin from '../../mixins/tooltip';

export default Component.extend(TooltipMixin, {
	documentService: service('document'),
	isDesktop: false,
	emptyState: computed('pages', function () {
		return this.get('pages.length') === 0;
	}),
	canEdit: computed('permissions', 'document', 'pages', function() {
		let constants = this.get('constants');
		let permissions = this.get('permissions');
		let authenticated = this.get('session.authenticated');
		let notEmpty = this.get('pages.length') > 0;

		if (notEmpty && authenticated && permissions.get('documentEdit') && this.get('document.protection') === constants.ProtectionType.None) return true;
		if (notEmpty && authenticated && permissions.get('documentApprove') && this.get('document.protection') === constants.ProtectionType.Review) return true;

		return false;
	}),

	init() {
		this._super(...arguments);
		this.state = {
			actionablePage: false,
			upDisabled: true,
			downDisabled: true,
			indentDisabled: true,
			outdentDisabled: true,
			pageId: ''
		};
	},

	didReceiveAttrs() {
		this._super(...arguments);
		let cp = this.get('currentPageId');
		this.setState(is.empty(cp) ? '' : cp);
	},

	didInsertElement() {
		this._super(...arguments);
		this.eventBus.subscribe('documentPageAdded', this, 'onDocumentPageAdded');
		this.eventBus.subscribe('resized', this, 'setSize');

		this.setSize();
		this.renderTooltips();
	},

	willDestroyElement() {
		this._super(...arguments);
		this.eventBus.unsubscribe('documentPageAdded');
		this.eventBus.unsubscribe('resized');

		let t = '#doc-toc';
		if (interact.isSet(t)) interact(t).unset();
		this.removeTooltips();
	},

	onDocumentPageAdded(pageId) { // eslint-disable-line no-unused-vars
		this.setSize();
		schedule('afterRender', () => {
			this.setState(pageId);
		});
	},

	setSize() {
		schedule('afterRender', () => {
			let isDesktop = $(window).width() >= 1800;
			this.set('isDesktop', isDesktop);

			if (isDesktop) {
				let h = $(window).height() - $("#nav-bar").height() - 140;
				$("#doc-toc").css('max-height', h);

				let i = $("#doc-view").offset();

				if (is.not.undefined(i)) {
					let l = i.left - 100;
					if (l > 350) l = 350;
					$("#doc-toc").width(l);

					$("#doc-toc").css({
{'fraction_non_alphanumeric': 0.18196119559517568, 'fraction_numerical': 0.008739730816290859, 'mean_word_length': 4.059239610963749, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '966585', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1796, 'n_words': 405}