<commit_before>// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
// MIT License. See license.txt

frappe.ui.FilterList = Class.extend({
	init: function(opts) {
		$.extend(this, opts);
		this.filters = [];
		this.wrapper = this.parent;
		this.stats = [];
		this.make();
		this.set_events();
	},
	make: function() {
		this.wrapper.find('.show_filters, .filter_area').remove();
		this.wrapper.append(`
			<div class="show_filters">
				<div class="set-filters">
					<button
						style="margin-right: 10px;"
						class="btn btn-default btn-xs new-filter text-muted">
						${__("Add Filter")}</button>
				</div>
			</div>
			<div class="filter_area"></div>`);
	},
	set_events: function() {
		var me = this;
		// show filters
		this.wrapper.find('.new-filter').bind('click', function() {
			me.add_filter();
		});

		this.wrapper.find('.clear-filters').bind('click', function() {
			me.clear_filters();
			$('.date-range-picker').val('')
			me.base_list.run();
			$(this).addClass("hide");
		});
	},

	show_filters: function() {
		this.wrapper.find('.show_filters').toggle();
		if(!this.filters.length) {
			this.add_filter(this.doctype, 'name');
			this.filters[0].wrapper.find(".filter_field input").focus();
		}
	},

	clear_filters: function() {
		$.each(this.filters, function(i, f) { f.remove(true); });
		if(this.base_list.page.fields_dict) {
			$.each(this.base_list.page.fields_dict, (key, value) => {
				value.set_input('');
			});
		}
		this.filters = [];
	},

	add_filter: function(doctype, fieldname, condition, value, hidden) {
		// adds a new filter, returns true if filter has been added

		// allow equal to be used as like
		let base_filter = this.base_list.page.fields_dict[fieldname];
		if (base_filter
			&& (base_filter.df.condition==condition
				|| (condition==='=' && base_filter.df.condition==='like'))) {
			// if filter exists in base_list, then exit
			this.base_list.page.fields_dict[fieldname].set_input(value);

			return true;
		}

		if(doctype && fieldname
			&& !frappe.meta.has_field(doctype, fieldname)
			&& !in_list(frappe.model.std_fields_list, fieldname)) {
			frappe.msgprint({
				message: __('Filter {0} missing', [fieldname.bold()]),
				title: 'Invalid Filter',
				indicator: 'red'
			});
			return false;
		}

		this.wrapper.find('.show_filters').toggle(true);
		var is_new_filter = arguments.length===0;

		if (is_new_filter && this.wrapper.find(".is-new-filter:visible").length) {
			// only allow 1 new filter at a time!
			return false;
		}

		var filter = this.push_new_filter(doctype, fieldname, condition, value);
		if (!filter) return;

		if(this.wrapper.find('.clear-filters').hasClass("hide")) {
			this.wrapper.find('.clear-filters').removeClass("hide");
		}

		if (filter && is_new_filter) {
			filter.wrapper.addClass("is-new-filter");
		} else {
			filter.freeze();
		}

		if (hidden) {
			filter.$btn_group.addClass("hide");
		}

		return true;
	},
	push_new_filter: function(doctype, fieldname, condition, value) {
		if(this.filter_exists(doctype, fieldname, condition, value)) {
			return;
		}

		// if standard filter exists, then clear it.
		if(this.base_list.page.fields_dict[fieldname]) {
			this.base_list.page.fields_dict[fieldname].set_input('');
		}

		var filter = new frappe.ui.Filter({
			flist: this,
			_doctype: doctype,
			fieldname: fieldname,
			condition: condition,
			value: value
		});

		this.filters.push(filter);

		return filter;
	},

	remove: function(filter) {
		// remove `filter` from flist
		for (var i in this.filters) {
			if (this.filters[i] === filter) {
				break;
			}
		}
		if (i!==undefined) {
			// remove index
			this.splice(i, 1);
		}
	},

	filter_exists: function(doctype, fieldname, condition, value) {
		var flag = false;
		for(var i in this.filters) {
			if(this.filters[i].field) {
				var f = this.filters[i].get_value();

				if(f[0]==doctype && f[1]==fieldname && f[2]==condition && f[3]==value) {
					flag = true;
				} else if($.isArray(value) && frappe.utils.arrays_equal(value, f[3])) {
					flag = true;
				}
			}
		}<commit_msg>splice error on console (#4669)<commit_after>// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
// MIT License. See license.txt

frappe.ui.FilterList = Class.extend({
	init: function(opts) {
		$.extend(this, opts);
		this.filters = [];
		this.wrapper = this.parent;
		this.stats = [];
		this.make();
		this.set_events();
	},
	make: function() {
		this.wrapper.find('.show_filters, .filter_area').remove();
		this.wrapper.append(`
			<div class="show_filters">
				<div class="set-filters">
					<button
						style="margin-right: 10px;"
						class="btn btn-default btn-xs new-filter text-muted">
						${__("Add Filter")}</button>
				</div>
			</div>
			<div class="filter_area"></div>`);
	},
	set_events: function() {
		var me = this;
		// show filters
		this.wrapper.find('.new-filter').bind('click', function() {
			me.add_filter();
		});

		this.wrapper.find('.clear-filters').bind('click', function() {
			me.clear_filters();
			$('.date-range-picker').val('')
			me.base_list.run();
			$(this).addClass("hide");
		});
	},

	show_filters: function() {
		this.wrapper.find('.show_filters').toggle();
		if(!this.filters.length) {
			this.add_filter(this.doctype, 'name');
			this.filters[0].wrapper.find(".filter_field input").focus();
		}
	},

	clear_filters: function() {
		$.each(this.filters, function(i, f) { f.remove(true); });
		if(this.base_list.page.fields_dict) {
			$.each(this.base_list.page.fields_dict, (key, value) => {
				value.set_input('');
			});
		}
		this.filters = [];
	},

	add_filter: function(doctype, fieldname, condition, value, hidden) {
		// adds a new filter, returns true if filter has been added

		// allow equal to be used as like
		let base_filter = this.base_list.page.fields_dict[fieldname];
		if (base_filter
			&& (base_filter.df.condition==condition
				|| (condition==='=' && base_filter.df.condition==='like'))) {
			// if filter exists in base_list, then exit
			this.base_list.page.fields_dict[fieldname].set_input(value);

			return true;
		}

		if(doctype && fieldname
			&& !frappe.meta.has_field(doctype, fieldname)
			&& !in_list(frappe.model.std_fields_list, fieldname)) {
			frappe.msgprint({
				message: __('Filter {0} missing', [fieldname.bold()]),
				title: 'Invalid Filter',
				indicator: 'red'
			});
			return false;
		}

		this.wrapper.find('.show_filters').toggle(true);
		var is_new_filter = arguments.length===0;

		if (is_new_filter && this.wrapper.find(".is-new-filter:visible").length) {
			// only allow 1 new filter at a time!
			return false;
		}

		var filter = this.push_new_filter(doctype, fieldname, condition, value);
		if (!filter) return;

		if(this.wrapper.find('.clear-filters').hasClass("hide")) {
			this.wrapper.find('.clear-filters').removeClass("hide");
		}

		if (filter && is_new_filter) {
			filter.wrapper.addClass("is-new-filter");
		} else {
			filter.freeze();
		}

		if (hidden) {
			filter.$btn_group.addClass("hide");
		}

		return true;
	},
	push_new_filter: function(doctype, fieldname, condition, value) {
		if(this.filter_exists(doctype, fieldname, condition, value)) {
			return;
		}

		// if standard filter exists, then clear it.
		if(this.base_list.page.fields_dict[fieldname]) {
			this.base_list.page.fields_dict[fieldname].set_input('');
		}

		var filter = new frappe.ui.Filter({
			flist: this,
			_doctype: doctype,
			fieldname: fieldname,
			condition: condition,
			value: value
		});

		this.filters.push(filter);

		return filter;
	},

	remove: function(filter) {
		// remove `filter` from flist
		for (var i in this.filters) {
			if (this.filters[i] === filter) {
				break;
			}
		}
		if (i!==undefined) {
			// remove index
			this.filters.splice(i, 1);
		}
	},

	filter_exists: function(doctype, fieldname, condition, value) {
		var flag = false;
		for(var i in this.filters) {
			if(this.filters[i].field) {
				var f = this.filters[i].get_value();

				if(f[0]==doctype && f[1]==fieldname && f[2]==condition && f[3]==value) {
					flag = true;
				} else if($.isArray(value) && frappe.utils.arrays_equal(value, f[3])) {
					flag = true;
				}
			}
		}
{'fraction_non_alphanumeric': 0.1792799412196914, 'fraction_numerical': 0.00440852314474651, 'mean_word_length': 3.547327394209354, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, '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': '4759863', 'n_tokens_mistral': 3330, 'n_tokens_neox': 2852, 'n_words': 544}