<commit_before>/**
 * Modal
 */
Garnish.Modal = Garnish.Base.extend({

	$container: null,
	$header: null,
	$body: null,
	$scrollpane: null,
	$footer: null,
	$footerBtns: null,
	$submitBtn: null,

	_headerHeight: null,
	_footerHeight: null,

	visible: false,

	dragger: null,

	init: function(container, settings)
	{
		// Param mapping
		if (!settings && Garnish.isObject(container))
		{
			// (settings)
			settings = container;
			items = null;
		}

		this.setSettings(settings, Garnish.Modal.defaults);

		if (container)
		{
			this.setContainer(container);
			this.show();
		}

		Garnish.Modal.instances.push(this);
	},

	setContainer: function(container)
	{
		this.$container = $(container);

		// Is this already a modal?
		if (this.$container.data('modal'))
		{
			Garnish.log('Double-instantiating a modal on an element');
			this.$container.data('modal').destroy();
		}

		this.$container.data('modal', this);

		this.$header = this.$container.find('.pane-head:first');
		this.$body = this.$container.find('.pane-body:first');
		this.$scrollpane = this.$body.children('.scrollpane:first');
		this.$footer = this.$container.find('.pane-foot:first');
		this.$footerBtns = this.$footer.find('.btn');
		this.$submitBtn = this.$footerBtns.filter('.submit:first');
		this.$closeBtn = this.$footerBtns.filter('.close:first');

		if (this.settings.draggable)
		{
			var $dragHandles = this.$header.add(this.$footer);
			if ($dragHandles.length)
			{
				this.dragger = new Garnish.DragMove(this.$container, {
					handle: this.$container
				});
			}
		}

		this.addListener(this.$container, 'click', function(ev) {
			ev.stopPropagation();
		});

		this.addListener(this.$container, 'keydown', 'onKeyDown');
		this.addListener(this.$closeBtn, 'click', 'hide');
	},

	show: function()
	{
		if (Garnish.Modal.visibleModal)
		{
			Garnish.Modal.visibleModal.hide();
		}

		if (this.$container)
		{
			this.$container.show();

			// Center it vertically
			var modalHeight = this.getHeight();
			this.$container.css('margin-top', -Math.round(modalHeight/2));

			// Make sure it's not too wide
			var windowWidth = Garnish.$win.width();
			if (this.$container.width() > windowWidth)
			{
				this.$container.css({
					width: windowWidth,
					marginLeft: -Math.round(windowWidth/2)
				});
			}

			this.$container.delay(50).fadeIn($.proxy(this, 'onFadeIn'));
		}

		this.visible = true;
		Garnish.Modal.visibleModal = this;
		Garnish.Modal.$shade.fadeIn(50);
		this.addListener(Garnish.Modal.$shade, 'click', 'hide');

		this.settings.onShow();
	},

	hide: function()
	hide: function(ev)
	{
		{
			ev.stopPropagation();
		}

		if (this.$container)
		{
			this.$container.fadeOut('fast');
			this.removeListener(Garnish.$win, 'resize');
		}

		this.visible = false;
		Garnish.Modal.visibleModal = null;
		Garnish.Modal.$shade.fadeOut('fast', $.proxy(this, 'onFadeOut'));
		this.removeListener(Garnish.Modal.$shade, 'click');

		this.settings.onHide();
	},

	onFadeIn: function()
	{
		this.settings.onFadeIn();
	},

	onFadeOut: function()
	{
		this.settings.onFadeOut();
	},

	getHeight: function()
	{
		if (!this.$container)
		{
			throw 'Attempted to get the height of a modal whose container has not been set.';
		}
<commit_msg>Close modals with ESC key<commit_after>/**
 * Modal
 */
Garnish.Modal = Garnish.Base.extend({

	$container: null,
	$header: null,
	$body: null,
	$scrollpane: null,
	$footer: null,
	$footerBtns: null,
	$submitBtn: null,

	_headerHeight: null,
	_footerHeight: null,

	visible: false,

	dragger: null,

	init: function(container, settings)
	{
		// Param mapping
		if (!settings && Garnish.isObject(container))
		{
			// (settings)
			settings = container;
			items = null;
		}

		this.setSettings(settings, Garnish.Modal.defaults);

		if (container)
		{
			this.setContainer(container);
			this.show();
		}

		Garnish.Modal.instances.push(this);
	},

	setContainer: function(container)
	{
		this.$container = $(container);

		// Is this already a modal?
		if (this.$container.data('modal'))
		{
			Garnish.log('Double-instantiating a modal on an element');
			this.$container.data('modal').destroy();
		}

		this.$container.data('modal', this);

		this.$header = this.$container.find('.pane-head:first');
		this.$body = this.$container.find('.pane-body:first');
		this.$scrollpane = this.$body.children('.scrollpane:first');
		this.$footer = this.$container.find('.pane-foot:first');
		this.$footerBtns = this.$footer.find('.btn');
		this.$submitBtn = this.$footerBtns.filter('.submit:first');
		this.$closeBtn = this.$footerBtns.filter('.close:first');

		if (this.settings.draggable)
		{
			var $dragHandles = this.$header.add(this.$footer);
			if ($dragHandles.length)
			{
				this.dragger = new Garnish.DragMove(this.$container, {
					handle: this.$container
				});
			}
		}

		this.addListener(this.$container, 'click', function(ev) {
			ev.stopPropagation();
		});

		this.addListener(this.$container, 'keydown', 'onKeyDown');
		this.addListener(this.$closeBtn, 'click', 'hide');
	},

	show: function()
	{
		if (Garnish.Modal.visibleModal)
		{
			Garnish.Modal.visibleModal.hide();
		}

		if (this.$container)
		{
			this.$container.show();

			// Center it vertically
			var modalHeight = this.getHeight();
			this.$container.css('margin-top', -Math.round(modalHeight/2));

			// Make sure it's not too wide
			var windowWidth = Garnish.$win.width();
			if (this.$container.width() > windowWidth)
			{
				this.$container.css({
					width: windowWidth,
					marginLeft: -Math.round(windowWidth/2)
				});
			}

			this.$container.delay(50).fadeIn($.proxy(this, 'onFadeIn'));
		}

		this.visible = true;
		Garnish.Modal.visibleModal = this;
		Garnish.Modal.$shade.fadeIn(50);

		this.addListener(Garnish.Modal.$shade, 'click', 'hide');
		this.addListener(Garnish.$bod, 'keyup', 'onKeypress');

		this.settings.onShow();
	},

	hide: function(ev)
	{
		if (ev)
		{
			ev.stopPropagation();
		}

		if (this.$container)
		{
			this.$container.fadeOut('fast');
			this.removeListener(Garnish.$win, 'resize');
		}

		this.visible = false;
		Garnish.Modal.visibleModal = null;
		Garnish.Modal.$shade.fadeOut('fast', $.proxy(this, 'onFadeOut'));
		this.removeListener(Garnish.Modal.$shade, 'click');
		this.removeListener(Garnish.$bod, 'keyup');

		this.settings.onHide();
	},

	onKeypress: function(ev)
	{
		if (ev.keyCode == Garnish.ESC_KEY)
		{
			this.hide();
		}
	},

	onFadeIn: function()
	{
		this.settings.onFadeIn();
	},

	onFadeOut: function()
	{
		this.settings.onFadeOut();
	},

	getHeight: function()
	{
		if (!this.$container)
		{
			throw 'Attempted to get the height of a modal whose container has not been set.';
		}

{'fraction_non_alphanumeric': 0.17720378435200482, 'fraction_numerical': 0.0018020723832407267, 'mean_word_length': 3.6736842105263157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '201405', 'n_tokens_mistral': 2714, 'n_tokens_neox': 2351, 'n_words': 397}