<commit_before>/**
 * PHPR Post
 * 
 * This object is used for communicating with AJAX handlers in PHPR.
 * 
 * Usage:
 * 
 * $.phpr.post('user:on_login', { data: {username:'', password:''}  });
 * 
 * $.phpr.post('user:on_login').send({ data: {username:'', password:''}  });
 * 
 * $.phpr.post('user:on_login').data({username:'', password:''}).send();
 * 
 * 
 * Usage with form:
 * 
 * $('#myform').phpr().post('user:on_login', { data: {username:'', password:''}  });
 * 
 * $.phpr.post('user:on_login').form('#myform').data({username:'', password:''}).send();
 * 
 * 
 * Usage with PHPR.validate:
 * 
 * $('#myform').phpr().form()
 *   .defineFields(function(){ 
 *     this.defineField('username').required('What is your username?');
 *     this.defineField('password').required('<PASSWORD>!');
 *   })
 *   .validate()
 *   .post('user:on_signup', { success: function(){ alert('You are now logged in!'); }  }); 
 * 
 */

(function($) {

	PHPR.postDefaults = {
		action: 'core:on_null',
		data: {},
		update: {},
		success: null, // On Success
		error: null, // On Failure
		complete: null, // On Complete
		afterUpdate: null, // On After Update
		beforeSend: null, // On Before Send
		selectorMode: true,
		lock: true,
		alert: null,
		confirm: null,
		evalScripts: true,
		execScriptsOnFail: true,
		loadIndicator: { show: true },
		customIndicator: null,
		animation: function(element, html) { element.html(html); }
	};

	PHPR.post = function(handler, context) {

		var o = {},
			_deferred = $.Deferred(),
			_handler = handler,
			_context = context || {},
			_data = {},
			_update = {},
			_form = null,
			_events = { 
				beforeSend: [],
				complete: [],
				success: [],
				error: [],
				afterUpdate: []
			};

		o.requestObj = null;
		o.indicatorObj = null;

		//
		// Services
		// 

		o.form = function(element) {
			o.setFormElement(element);
			return this;
		}

		o.handler = o.action = function(value) {
			_handler = value;
			return this;
		}

		o.confirm = function(value) {
			_context.confirm = value;
			return this;
		}

		o.alert = function(value) {
			_context.alert = value;
			return this;
		}

		o.success = function(func) {
			if ($.isArray(_events.success))
				_events.success.push(func);
			else
				_events.success = func;

			return this;
		}

		o.error = function(func) {
			if ($.isArray(_events.error))
				_events.error.push(func);
			else
				_events.error = func;

			return this;
		}

		o.complete = function(func) {
			if ($.isArray(_events.complete))
				_events.complete.push(func);
			else
				_events.complete = func;

			return this;
		}

		o.beforeSend = function(func) {
			if ($.isArray(_events.beforeSend))
				_events.beforeSend.push(func);
			else
				_events.beforeSend = func;

			return this;
		}

		o.afterUpdate = function(func) {
			if ($.isArray(_events.afterUpdate))
				_events.afterUpdate.push(func);
			else
				_events.afterUpdate = func;

			return this;
		}

		o.update = function(element, partial) {
			if (partial) {
				if(element instanceof jQuery) { // this key is a jQuery object so let's give it a random ID so we can update it after AJAX
					var rand_id = Math.random().toString(36).substring(7);

					element.addClass('phpr_element_id_' + rand_id);

					_update['.phpr_element_id_' + rand_id] = partial;
				}
				else {
					_update[element] = partial;
				}
			}
			else
				_update = element;

			return this;
		}

		o.data = function(field, value) {
			if (typeof field == "object")
				_data = field;
			else if (typeof value != "undefined")
				_data[field] = value;
			return this;
		}

		o.queue = function(value) {
			_context.lock = !value;
			return this;
		}<commit_msg>Direct jQuery object can be used as a CMS update element.<commit_after>/**
 * PHPR Post
 * 
 * This object is used for communicating with AJAX handlers in PHPR.
 * 
 * Usage:
 * 
 * $.phpr.post('user:on_login', { data: {username:'', password:''}  });
 * 
 * $.phpr.post('user:on_login').send({ data: {username:'', password:''}  });
 * 
 * $.phpr.post('user:on_login').data({username:'', password:''}).send();
 * 
 * 
 * Usage with form:
 * 
 * $('#myform').phpr().post('user:on_login', { data: {username:'', password:''}  });
 * 
 * $.phpr.post('user:on_login').form('#myform').data({username:'', password:''}).send();
 * 
 * 
 * Usage with PHPR.validate:
 * 
 * $('#myform').phpr().form()
 *   .defineFields(function(){ 
 *     this.defineField('username').required('What is your username?');
 *     this.defineField('password').required('<PASSWORD>!');
 *   })
 *   .validate()
 *   .post('user:on_signup', { success: function(){ alert('You are now logged in!'); }  }); 
 * 
 */

(function($) {

	PHPR.postDefaults = {
		action: 'core:on_null',
		data: {},
		update: {},
		success: null, // On Success
		error: null, // On Failure
		complete: null, // On Complete
		afterUpdate: null, // On After Update
		beforeSend: null, // On Before Send
		selectorMode: true,
		lock: true,
		alert: null,
		confirm: null,
		evalScripts: true,
		execScriptsOnFail: true,
		loadIndicator: { show: true },
		customIndicator: null,
		animation: function(element, html) { element.html(html); }
	};

	PHPR.post = function(handler, context) {

		var o = {},
			_deferred = $.Deferred(),
			_handler = handler,
			_context = context || {},
			_data = {},
			_update = {},
			_form = null,
			_events = { 
				beforeSend: [],
				complete: [],
				success: [],
				error: [],
				afterUpdate: []
			};

		o.requestObj = null;
		o.indicatorObj = null;

		//
		// Services
		// 

		o.form = function(element) {
			o.setFormElement(element);
			return this;
		}

		o.handler = o.action = function(value) {
			_handler = value;
			return this;
		}

		o.confirm = function(value) {
			_context.confirm = value;
			return this;
		}

		o.alert = function(value) {
			_context.alert = value;
			return this;
		}

		o.success = function(func) {
			if ($.isArray(_events.success))
				_events.success.push(func);
			else
				_events.success = func;

			return this;
		}

		o.error = function(func) {
			if ($.isArray(_events.error))
				_events.error.push(func);
			else
				_events.error = func;

			return this;
		}

		o.complete = function(func) {
			if ($.isArray(_events.complete))
				_events.complete.push(func);
			else
				_events.complete = func;

			return this;
		}

		o.beforeSend = function(func) {
			if ($.isArray(_events.beforeSend))
				_events.beforeSend.push(func);
			else
				_events.beforeSend = func;

			return this;
		}

		o.afterUpdate = function(func) {
			if ($.isArray(_events.afterUpdate))
				_events.afterUpdate.push(func);
			else
				_events.afterUpdate = func;

			return this;
		}

		o.update = function(element, partial) {
			if (partial) {

				// jQuery object ensure it has an ID so we can update it
				if (element instanceof jQuery) { 
					
					if (!element.attr('id')) {
						var randId = Math.random().toString(36).substring(7);
						element.attr('id', 'phpr_element_id_' + randId);
					}

					element = '#' + element.attr('id');
				}

				_update[element] = partial;
			}
			else
				_update = element;

			return this;
		}

		o.data = function(field, value) {
			if (typeof field == "object")
				_data = field;
			else if (typeof value != "undefined")
				_data[field] = value;
			return this;
		}

		o.queue = function(value) {
			_context.lock = !value;
			return this;
		}
{'fraction_non_alphanumeric': 0.17123473541383988, 'fraction_numerical': 0.0008141112618724559, 'mean_word_length': 2.90206458443621, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6620814', 'n_tokens_mistral': 3013, 'n_tokens_neox': 2494, 'n_words': 544}