{"size": 6148, "ext": "js", "max_stars_repo_path": "js/dialog.js", "max_stars_repo_name": "jhuckaby/orchestra-theme", "max_stars_repo_stars_event_min_datetime": "2020-06-10T03:13:20.000Z", "max_stars_repo_stars_event_max_datetime": "2020-06-10T03:13:20.000Z", "max_issues_repo_path": "js/dialog.js", "max_issues_repo_name": "jhuckaby/orchestra-theme", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "js/dialog.js", "max_forks_repo_name": "jhuckaby/orchestra-theme", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.1374407583, "max_line_length": 136, "alphanum_fraction": 0.6174365647}
// Dialog Tools
// Author: Joseph Huckaby

var Dialog = {
	
	active: false,
	clickBlock: false,
	progress: null,
	onHide: null,
	
	show: function(html, click_block) {
		// show dialog, auto-size and center
		this.clickBlock = click_block || false;
		
		var temp = $('<div/>').addClass('dialog').css({
			position: 'absolute',
			visibility: 'hidden'
		}).html(html).appendTo('body');
		
		// var width = temp.width();
		// var height = temp.height();
		var width = temp[0].offsetWidth;
		var height = temp[0].offsetHeight;
		temp.remove();
		
		var size = get_inner_window_size();
		var x = Math.floor( (size.width / 2) - ((width + 0) / 2) );
		var y = Math.floor( ((size.height / 2) - (height / 2)) * 0.75 );
		
		if ($('#dialog_overlay').length) {
			$('#dialog_overlay').stop().remove();
		}
		
		var $overlay = $('<div id="dialog_overlay"></div>').css('opacity', 0);
		$('body').append($overlay);
		
		$overlay.fadeTo( 500, 0.75 ).on('mouseup', function() {
			if (!Dialog.clickBlock) {
				if (Dialog.active == 'confirmation') Dialog.confirm_click(false);
				else Dialog.hide();
			}
		});
		
		if ($('#dialog').length) {
			$('#dialog').stop().remove();
		}
		
		var $dialog = $('<div id="dialog"></div>').css({
			opacity: 0,
			left: '' + x + 'px',
			top: '' + y + 'px'
		}).html( html );;
		
		$('body').append($dialog);
		$dialog.fadeTo( 250, 1.0 );
		
		this.active = true;
		unscroll();
	},
	
	autoResize: function() {
		// automatically resize dialog to match changed content size
		if (!this.active) return;
		
		var $dialog = $('#dialog');
		// var width = $dialog.width();
		// var height = $dialog.height();
		var width = $dialog[0].offsetWidth;
		var height = $dialog[0].offsetHeight;
		
		var size = get_inner_window_size();
		var x = Math.floor( (size.width / 2) - ((width + 0) / 2) );
		var y = Math.floor( ((size.height / 2) - (height / 2)) * 0.75 );
		
		$dialog.css({
			left: '' + x + 'px',
			top: '' + y + 'px'
		});
	},
	
	hide: function() {
		// hide dialog
		if (this.active) {
			$('#dialog').stop().fadeOut( 250, function() { $(this).remove(); } );
			$('#dialog_overlay').stop().fadeOut( 300, function() { $(this).remove(); } );
			this.active = false;
			unscroll.reset();
			
			if (this.onHide) {
				// one time hook for hide
				var callback = this.onHide;
				this.onHide = null;
				callback();
			}
		}
	},
	
	hideProgress: function() {
		// hide progress dialog
		this.hide();
		delete this.progress;
	},
	
	showProgress: function(counter, title) {
		// show or update progress bar
		if (!$('#d_progress_bar').length) {
			// no progress dialog is active, so set it up
			if (!counter) counter = 0;
			if (counter < 0) counter = 0;
			if (counter > 1) counter = 1;
			var cx = Math.floor( counter * 196 );
			
			var html = '';
			html += '<div style="padding:15px;">';
			html += '<div id="d_progress_title" class="dialog_title" style="text-align:center; margin-bottom:15px;">' + title + '</div>';
			
			var extra_classes = '';
			if (counter == 1.0) extra_classes = 'indeterminate';
			
			html += '<div id="d_progress_bar_cont" class="progress_bar_container '+extra_classes+'" style="width:196px; margin:0 auto 0 auto;">';
				html += '<div id="d_progress_bar" class="progress_bar_inner" style="width:'+cx+'px;"></div>';
			html += '</div>';
			
			html += '</div>';
			app.hideMessage();
			this.show( html, true );
			
			this.progress = {
				start_counter: counter,
				counter: counter,
				counter_max: 1,
				start_time: hires_time_now(),
				last_update: hires_time_now(),
				title: title
			};
		}
		else if (this.progress) {
			// progress dialog is active, so update existing elements
			var now = hires_time_now();
			var cx = Math.floor( counter * 196 );
			$('#d_progress_bar').css( 'width', '' + cx + 'px' );
			
			var prog_cont = $('#d_progress_bar_cont');
			if ((counter == 1.0) && !prog_cont.hasClass('indeterminate')) prog_cont.addClass('indeterminate');
			else if ((counter < 1.0) && prog_cont.hasClass('indeterminate')) prog_cont.removeClass('indeterminate');
			
			if (title) this.progress.title = title;
			$('#d_progress_title').html( this.progress.title );
			
			this.progress.last_update = now;
			this.progress.counter = counter;
		}
	},
	
	showSimpleDialog: function(title, inner_html, buttons_html) {
		// show simple dialog with title, content and buttons
		var html = '';
		html += '<div class="dialog_title">' + title + '</div>';
		html += '<div class="dialog_content">' + inner_html + '</div>';
		html += '<div class="dialog_buttons">' + buttons_html + '</div>';
		Dialog.show( html );
	},
	
	confirm: function(title, html, ok_btn_label, callback) {
		// show simple OK / Cancel dialog with custom text
		// fires callback with true (OK) or false (Cancel)
		if (!ok_btn_label) ok_btn_label = "OK";
		this.confirm_callback = callback;
		
		var inner_html = "";
		if (html.match(/^</)) inner_html = html;
		else inner_html += '<div class="confirm_container">'+html+'</div>';
		
		var buttons_html = "";
		buttons_html += '<div class="button" onMouseUp="Dialog.confirm_click(false)">Cancel</div>';
		buttons_html += '<div class="button primary" onMouseUp="Dialog.confirm_click(true)">'+ok_btn_label+'</div>';
		
		this.showSimpleDialog( title, inner_html, buttons_html );
		
		// special mode for key capture
		Dialog.active = 'confirmation';
	},
	
	confirm_click: function(result) {
		// user clicked OK or Cancel in confirmation dialog, fire callback
		// caller MUST deal with Dialog.hide() if result is true
		if (this.confirm_callback) {
			this.confirm_callback(result);
			if (!result) this.hide();
		}
	},
	
	confirm_key: function(event) {
		// handle keydown with active confirmation dialog
		if (this.active !== 'confirmation') return;
		if ((event.keyCode != 13) && (event.keyCode != 27)) return;
		
		// skip enter check if textarea is active
		if (document.activeElement && (event.keyCode == 13)) {
			if ($(document.activeElement).prop('type') == 'textarea') return;
		}
		
		event.stopPropagation();
		event.preventDefault();
		
		if (event.keyCode == 13) this.confirm_click(true);
		else if (event.keyCode == 27) this.confirm_click(false);
	}
	
};
