<commit_before>/*
---

name: Element.defineCustomEvent

description: Allows to create custom events based on other custom events.

authors: <NAME> (@cpojer)

license: MIT-style license.

requires: [Core/Element.Event]

provides: Element.defineCustomEvent

...
*/

(function(){

var events = Element.Events;

var eventsOf = function(element, event){
	var events = element.retrieve('events');
	return (events && events[event] && events[event].keys.length) || 0;
};

var wrap = function(custom, method, extended, name, amount){
	method = custom[method];
	extended = custom[extended];

	return function(fn, customName){
		if (!customName) customName = name;
		
		if (extended && eventsOf(this, customName) == amount) extended.call(this, fn, customName);
		if (method) method.call(this, fn, customName);
	};
};

var inherit = function(custom, base, method, name){
	return function(fn, customName){
		base[method].call(this, fn, customName || name);
		custom[method].call(this, fn, customName || name);
	};
};

Element.defineCustomEvent = function(name, custom){

	var base = events[custom.base];

	custom.onAdd = wrap(custom, 'onAdd', 'onSetup', name, 1);
	custom.onRemove = wrap(custom, 'onRemove', 'onTeardown', name, 0);

	if (!base){
		events[name] = custom;
		return;
	}

	events[name] = {

		base: base.base,

		condition: function(event, customName){
			if (base.condition && !base.condition.call(this, event, customName || name)) return false;
			if (custom.condition && !custom.condition.call(this, event, customName || name)) return false;

			return true;
		},

		onAdd: inherit(custom, base, 'onAdd', name),
		onRemove: inherit(custom, base, 'onRemove', name)

	};

};

})();<commit_msg>* Changing events check to values because the internal property gets pushed after the call to onAdd, simplifies some code<commit_after>/*
---

name: Element.defineCustomEvent

description: Allows to create custom events based on other custom events.

authors: <NAME> (@cpojer)

license: MIT-style license.

requires: [Core/Element.Event]

provides: Element.defineCustomEvent

...
*/

(function(){

[Element, Window, Document].invoke('implement', {hasEvent: function(event){
	var events = this.retrieve('events');
	return (events && events[event] && events[event].values.length);
}});

var events = Element.Events;

var wrap = function(custom, method, extended, name){
	method = custom[method];
	extended = custom[extended];

	return function(fn, customName){
		if (!customName) customName = name;
		
		if (extended && !this.hasEvent(customName)) extended.call(this, fn, customName);
		if (method) method.call(this, fn, customName);
	};
};

var inherit = function(custom, base, method, name){
	return function(fn, customName){
		base[method].call(this, fn, customName || name);
		custom[method].call(this, fn, customName || name);
	};
};

Element.defineCustomEvent = function(name, custom){

	var base = events[custom.base];

	custom.onAdd = wrap(custom, 'onAdd', 'onSetup', name);
	custom.onRemove = wrap(custom, 'onRemove', 'onTeardown', name);

	if (!base){
		events[name] = custom;
		return;
	}

	events[name] = {

		base: base.base,

		condition: function(event, customName){
			if (base.condition && !base.condition.call(this, event, customName || name)) return false;
			if (custom.condition && !custom.condition.call(this, event, customName || name)) return false;

			return true;
		},

		onAdd: inherit(custom, base, 'onAdd', name),
		onRemove: inherit(custom, base, 'onRemove', name)

	};

};

})();
{'fraction_non_alphanumeric': 0.1584356266057665, 'fraction_numerical': 0.0008564087924636027, 'mean_word_length': 4.84, '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': '3812403', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1106, 'n_words': 304}