<commit_before>/**
 * Provides API to easily create a dialog form created by form builder.
 * 
 * @author	<NAME>
 * @copyright	2001-2019 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @module	WoltLabSuite/Core/Form/Builder/Dialog
 * @since	5.2
 */
define(['Ajax', 'Core', './Manager', 'Ui/Dialog'], function(Ajax, Core, FormBuilderManager, UiDialog) {
	"use strict";
	
	/**
	 * @constructor
	 */
	function FormBuilderDialog(dialogId, className, actionName, options) {
		this.init(dialogId, className, actionName, options);
	};
	FormBuilderDialog.prototype = {
		/**
		 * Initializes the dialog.
		 * 
		 * @param	{string}	dialogId
		 * @param	{string}	className
		 * @param	{string}	actionName
		 * @param	{{actionParameters: object, destoryOnClose: boolean, dialog: object}}	options
		 */
		init: function(dialogId, className, actionName, options) {
			this._dialogId = dialogId;
			this._className = className;
			this._actionName = actionName;
			this._options = Core.extend({
				actionParameters: {},
				destroyOnClose: false,
				usesDboAction: this._className.match(/\w+\\data\\/)
			}, options);
			this._options.dialog = Core.extend(this._options.dialog || {}, {
				onClose: this._dialogOnClose.bind(this)
			});
			
			this._formId = '';
			this._dialogContent = '';
		},
		
		/**
		 * Returns the data for Ajax to setup the Ajax/Request object.
		 * 
		 * @return	{object}	setup data for Ajax/Request object
		 */
		_ajaxSetup: function() {
			var options = {
				data: {
					actionName: this._actionName,
					className: this._className,
					parameters: this._options.actionParameters
				}
			};
			
			// by default, `AJAXProxyAction` is used which relies on an `IDatabaseObjectAction`
			// object; if no such object is used but an `IAJAXInvokeAction` object,
			// `AJAXInvokeAction` has to be used
			if (!this._options.usesDboAction) {
				options.url = 'index.php?ajax-invoke/&t=' + SECURITY_TOKEN;
				options.withCredentials = true;
			}
			
			return options;
		},
		
		/**
		 * Handles successful Ajax requests.
		 *
		 * @param	{object}	data	response data
		 */
		_ajaxSuccess: function(data) {
			switch (data.actionName) {
				case this._actionName:
					if (data.returnValues === undefined) {
						throw new Error("Missing return data.");
					}
					else if (data.returnValues.dialog === undefined) {
						throw new Error("Missing dialog template in return data.");
					}
					else if (data.returnValues.formId === undefined) {
						throw new Error("Missing form id in return data.");
					}
					
					this._openDialogContent(data.returnValues.formId, data.returnValues.dialog);
					
					break;
					
				case this._options.submitActionName:
					// if the validation failed, the dialog is shown again
					if (data.returnValues && data.returnValues.formId && data.returnValues.dialog) {
						if (data.returnValues.formId !== this._formId) {
							throw new Error("Mismatch between form ids: expected '" + this._formId + "' but got '" + data.returnValues.formId + "'.");
						}
						
						this._openDialogContent(data.returnValues.formId, data.returnValues.dialog);
					}
					else {
						this.destroy();
						
						if (typeof this._options.successCallback === 'function') {
							this._options.successCallback(data.returnValues || {});
						}
					}
					
					break;
					
				default:
					throw new Error("Cannot handle action '" + data.actionName + "'.");
			}
		},
		
		/**
		 * Is called when clicking on the dialog form's close button.
		 */
		_closeDialog: function() {
			UiDialog.close(this);
		},
		
		/**
		 * Is called by the dialog API when the dialog is closed.
		 */
		_dialogOnClose: function() {
			if (this._options.destroyOnClose) {
				this.destroy();
			}
		},
		
		/**<commit_msg>Add close callback to form builder dialog<commit_after>/**
 * Provides API to easily create a dialog form created by form builder.
 * 
 * @author	<NAME>
 * @copyright	2001-2019 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @module	WoltLabSuite/Core/Form/Builder/Dialog
 * @since	5.2
 */
define(['Ajax', 'Core', './Manager', 'Ui/Dialog'], function(Ajax, Core, FormBuilderManager, UiDialog) {
	"use strict";
	
	/**
	 * @constructor
	 */
	function FormBuilderDialog(dialogId, className, actionName, options) {
		this.init(dialogId, className, actionName, options);
	};
	FormBuilderDialog.prototype = {
		/**
		 * Initializes the dialog.
		 * 
		 * @param	{string}	dialogId
		 * @param	{string}	className
		 * @param	{string}	actionName
		 * @param	{{actionParameters: object, destoryOnClose: boolean, dialog: object}}	options
		 */
		init: function(dialogId, className, actionName, options) {
			this._dialogId = dialogId;
			this._className = className;
			this._actionName = actionName;
			this._options = Core.extend({
				actionParameters: {},
				destroyOnClose: false,
				usesDboAction: this._className.match(/\w+\\data\\/)
			}, options);
			this._options.dialog = Core.extend(this._options.dialog || {}, {
				onClose: this._dialogOnClose.bind(this)
			});
			
			this._formId = '';
			this._dialogContent = '';
		},
		
		/**
		 * Returns the data for Ajax to setup the Ajax/Request object.
		 * 
		 * @return	{object}	setup data for Ajax/Request object
		 */
		_ajaxSetup: function() {
			var options = {
				data: {
					actionName: this._actionName,
					className: this._className,
					parameters: this._options.actionParameters
				}
			};
			
			// by default, `AJAXProxyAction` is used which relies on an `IDatabaseObjectAction`
			// object; if no such object is used but an `IAJAXInvokeAction` object,
			// `AJAXInvokeAction` has to be used
			if (!this._options.usesDboAction) {
				options.url = 'index.php?ajax-invoke/&t=' + SECURITY_TOKEN;
				options.withCredentials = true;
			}
			
			return options;
		},
		
		/**
		 * Handles successful Ajax requests.
		 *
		 * @param	{object}	data	response data
		 */
		_ajaxSuccess: function(data) {
			switch (data.actionName) {
				case this._actionName:
					if (data.returnValues === undefined) {
						throw new Error("Missing return data.");
					}
					else if (data.returnValues.dialog === undefined) {
						throw new Error("Missing dialog template in return data.");
					}
					else if (data.returnValues.formId === undefined) {
						throw new Error("Missing form id in return data.");
					}
					
					this._openDialogContent(data.returnValues.formId, data.returnValues.dialog);
					
					break;
					
				case this._options.submitActionName:
					// if the validation failed, the dialog is shown again
					if (data.returnValues && data.returnValues.formId && data.returnValues.dialog) {
						if (data.returnValues.formId !== this._formId) {
							throw new Error("Mismatch between form ids: expected '" + this._formId + "' but got '" + data.returnValues.formId + "'.");
						}
						
						this._openDialogContent(data.returnValues.formId, data.returnValues.dialog);
					}
					else {
						this.destroy();
						
						if (typeof this._options.successCallback === 'function') {
							this._options.successCallback(data.returnValues || {});
						}
					}
					
					break;
					
				default:
					throw new Error("Cannot handle action '" + data.actionName + "'.");
			}
		},
		
		/**
		 * Is called when clicking on the dialog form's close button.
		 */
		_closeDialog: function() {
			UiDialog.close(this);
			
			if (typeof this._options.closeCallback === 'function') {
				this._options.closeCallback();
			}
		},
		
		/**
		 * Is called by the dialog API when the dialog is closed.
		 */
		_dialogOnClose: function() {
			if (this._options.destroyOnClose) {
				this.destroy();
			}
		},
		
		/**
{'fraction_non_alphanumeric': 0.13075933075933077, 'fraction_numerical': 0.002574002574002574, 'mean_word_length': 2.8719481813652217, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1463614', 'n_tokens_mistral': 2984, 'n_tokens_neox': 2279, 'n_words': 579}