<commit_before>/**
 * Versatile AJAX request handling.
 * 
 * In case you want to issue JSONP requests, please use `AjaxJsonp` instead.
 * 
 * @author	<NAME>
 * @copyright	2001-2015 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @module	WoltLab/WCF/Ajax/Request
 */
define(['Core', 'Language', 'Dom/ChangeListener', 'Dom/Util', 'Ui/Dialog', 'WoltLab/WCF/Ajax/Status'], function(Core, Language, DomChangeListener, DomUtil, UiDialog, AjaxStatus) {
	"use strict";
	
	var _didInit = false;
	var _ignoreAllErrors = false;
	
	/**
	 * @constructor
	 */
	function AjaxRequest(options) {
		this._data = null;
		this._options = {};
		this._previousXhr = null;
		this._xhr = null;
		
		this._init(options);
	};
	AjaxRequest.prototype = {
		/**
		 * Initializes the request options.
		 * 
		 * @param	{object<string, *>}	options		request options
		 */
		_init: function(options) {
			this._options = Core.extend({
				// request data
				data: {},
				contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
				responseType: 'application/json',
				type: 'POST',
				url: '',
				
				// behavior
				autoAbort: false,
				ignoreError: false,
				pinData: false,
				silent: false,
				
				// callbacks
				failure: null,
				finalize: null,
				success: null,
				
				callbackObject: null
			}, options);
			
			if (typeof options.callbackObject === 'object') {
				this._options.callbackObject = options.callbackObject;
			}
			
			this._options.url = Core.convertLegacyUrl(this._options.url);
			
			if (this._options.pinData) {
				this._data = Core.extend({}, this._options.data);
			}
			
			if (this._options.callbackObject !== null) {
				if (typeof this._options.callbackObject._ajaxFailure === 'function') this._options.failure = this._options.callbackObject._ajaxFailure.bind(this._options.callbackObject);
				if (typeof this._options.callbackObject._ajaxFinalize === 'function') this._options.finalize = this._options.callbackObject._ajaxFinalize.bind(this._options.callbackObject);
				if (typeof this._options.callbackObject._ajaxSuccess === 'function') this._options.success = this._options.callbackObject._ajaxSuccess.bind(this._options.callbackObject);
			}
			
			if (_didInit === false) {
				_didInit = true;
				
				window.addEventListener('beforeunload', function() { _ignoreAllErrors = true; });
			}
		},
		
		/**
		 * Dispatches a request, optionally aborting a currently active request.
		 * 
		 * @param	{boolean}	abortPrevious	abort currently active request
		 */
		sendRequest: function(abortPrevious) {
			if (abortPrevious === true || this._options.autoAbort) {
				this.abortPrevious();
			}
			
			if (!this._options.silent) {
				AjaxStatus.show();
			}
			
			if (this._xhr instanceof XMLHttpRequest) {
				this._previousXhr = this._xhr;
			}
			
			this._xhr = new XMLHttpRequest();
			this._xhr.open(this._options.type, this._options.url, true);
			if (this._options.contentType) {
				this._xhr.setRequestHeader('Content-Type', this._options.contentType);
			}
			this._xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
			
			var self = this;
			var options = Core.clone(this._options);
			this._xhr.onload = function() {
				if (this.readyState === XMLHttpRequest.DONE) {
					if (this.status >= 200 && this.status < 300 || this.status === 304) {
						if (options.responseType && options.responseType !== this.getResponseHeader('Content-Type')) {
							// request succeeded but invalid response type
							self._failure(this, options);
						}
						else {
							self._success(this, options);
						}
					}
					else {
						self._failure(this, options);
					}
				}
			};
			this._xhr.onerror = function() {
				self._failure(this, options);
			};
			
			if (this._options.type === 'POST') {
				var data = this._options.data;<commit_msg>Add progress and upload progress callbacks for AjaxRequest<commit_after>/**
 * Versatile AJAX request handling.
 * 
 * In case you want to issue JSONP requests, please use `AjaxJsonp` instead.
 * 
 * @author	<NAME>
 * @copyright	2001-2015 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @module	WoltLab/WCF/Ajax/Request
 */
define(['Core', 'Language', 'Dom/ChangeListener', 'Dom/Util', 'Ui/Dialog', 'WoltLab/WCF/Ajax/Status'], function(Core, Language, DomChangeListener, DomUtil, UiDialog, AjaxStatus) {
	"use strict";
	
	var _didInit = false;
	var _ignoreAllErrors = false;
	
	/**
	 * @constructor
	 */
	function AjaxRequest(options) {
		this._data = null;
		this._options = {};
		this._previousXhr = null;
		this._xhr = null;
		
		this._init(options);
	};
	AjaxRequest.prototype = {
		/**
		 * Initializes the request options.
		 * 
		 * @param	{object<string, *>}	options		request options
		 */
		_init: function(options) {
			this._options = Core.extend({
				// request data
				data: {},
				contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
				responseType: 'application/json',
				type: 'POST',
				url: '',
				
				// behavior
				autoAbort: false,
				ignoreError: false,
				pinData: false,
				silent: false,
				
				// callbacks
				failure: null,
				finalize: null,
				success: null,
				progress: null,
				uploadProgress: null,
				
				callbackObject: null
			}, options);
			
			if (typeof options.callbackObject === 'object') {
				this._options.callbackObject = options.callbackObject;
			}
			
			this._options.url = Core.convertLegacyUrl(this._options.url);
			
			if (this._options.pinData) {
				this._data = Core.extend({}, this._options.data);
			}
			
			if (this._options.callbackObject !== null) {
				if (typeof this._options.callbackObject._ajaxFailure === 'function') this._options.failure = this._options.callbackObject._ajaxFailure.bind(this._options.callbackObject);
				if (typeof this._options.callbackObject._ajaxFinalize === 'function') this._options.finalize = this._options.callbackObject._ajaxFinalize.bind(this._options.callbackObject);
				if (typeof this._options.callbackObject._ajaxSuccess === 'function') this._options.success = this._options.callbackObject._ajaxSuccess.bind(this._options.callbackObject);
				if (typeof this._options.callbackObject._ajaxProgress === 'function') this._options.progress = this._options.callbackObject._ajaxProgress.bind(this._options.callbackObject);
				if (typeof this._options.callbackObject._ajaxUploadProgress === 'function') this._options.uploadProgress = this._options.callbackObject._ajaxUploadProgress.bind(this._options.callbackObject);
			}
			
			if (_didInit === false) {
				_didInit = true;
				
				window.addEventListener('beforeunload', function() { _ignoreAllErrors = true; });
			}
		},
		
		/**
		 * Dispatches a request, optionally aborting a currently active request.
		 * 
		 * @param	{boolean}	abortPrevious	abort currently active request
		 */
		sendRequest: function(abortPrevious) {
			if (abortPrevious === true || this._options.autoAbort) {
				this.abortPrevious();
			}
			
			if (!this._options.silent) {
				AjaxStatus.show();
			}
			
			if (this._xhr instanceof XMLHttpRequest) {
				this._previousXhr = this._xhr;
			}
			
			this._xhr = new XMLHttpRequest();
			this._xhr.open(this._options.type, this._options.url, true);
			if (this._options.contentType) {
				this._xhr.setRequestHeader('Content-Type', this._options.contentType);
			}
			this._xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
			
			var self = this;
			var options = Core.clone(this._options);
			this._xhr.onload = function() {
				if (this.readyState === XMLHttpRequest.DONE) {
					if (this.status >= 200 && this.status < 300 || this.status === 304) {
						if (options.responseType && options.responseType !== this.getResponseHeader('Content-Type')) {
							// request succeeded but invalid response type
							self._failure(this, options);
						}
						else {
							self._success(this, options);
						}
					}
					else {
						self._failure(this, options);
					}
				}
			};
			this._xhr.onerror = function() {
				self._failure(this, options);
			};
			
			if (this._options.progress) {
				this._xhr.onprogress = this._options.progress;
			}
			if (this._options.uploadProgress) {
				this._xhr.upload.onprogress = this._options.uploadProgress;
			}
			
			if (this._options.type === 'POST') {
				var data = this._options.data;
{'fraction_non_alphanumeric': 0.1369487485101311, 'fraction_numerical': 0.004290822407628129, 'mean_word_length': 3.2745797249108506, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, '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': '1688119', 'n_tokens_mistral': 3201, 'n_tokens_neox': 2535, 'n_words': 510}