<commit_before>(function(){
"use strict";

var DU = Kekule.DomUtils;
var EU = Kekule.HtmlElementUtils;
var CNS = Kekule.Widget.HtmlClassNames;

/** @ignore */
Kekule.Widget.HtmlClassNames = Object.extend(Kekule.Widget.HtmlClassNames, {
	LISTVIEW: 'K-ListView',
	LISTVIEW_ITEM_HOLDER: 'K-ListView-ItemHolder',
	LISTVIEW_ITEM: 'K-ListView-Item',
	LISTVIEW_ITEMCONTENT: 'K-ListView-ItemContent'
});

/**
 * List view widget, provide the ability to list/select a series of elements inside.
 * @class
 * @augments Kekule.Widget.NestedContainer
 *
 * @property {Array} items All child list items in list view, array of HTML elements.
 * @property {Array} selection All selected items in list view.
 * @property {Object} selectedItem Most recently selected item in tree view.
 * @property {Bool} enableSelect Whether item selecting is enabled.
 * @property {Bool} enableMultiSelect Whether only one item can be selected at same time.
 * @property {Bool} enableSelectInSelection Whether the most active one is allowed to be selected in selection.
 */
/**
 * Invoked when the selection is changed in the list view.
 *   event param of it has one fields: {selection: Array, selectedItem: HTMLElement}
 * @name Kekule.Widget.ListView#selectionChange
 * @event
 */
Kekule.Widget.ListView = Class.create(Kekule.Widget.BaseWidget,
/** @lends Kekule.Widget.ListView# */
{
	/** @private */
	CLASS_NAME: 'Kekule.Widget.ListView',
	/** @private */
	ITEM_DATA_FIELD: '__$itemData__',
	/** @construct */
	initialize: function(parentOrElementOrDocument)
	{
		this.setPropStoreFieldValue('enableSelect', true);
		this.setPropStoreFieldValue('enableMultiSelect', true);
		this.tryApplySuper('initialize', [parentOrElementOrDocument]);
	},
	/** @private */
	initProperties: function() {
		this.defineProp('items', {'dataType': DataType.ARRAY, 'serializable': false,
			'getter': function()
			{
				var result = [];
				var children = DU.getDirectChildElems(this.getChildrenHolderElement());
				for (var i = 0, l = children.length; i < l; ++i)
				{
					var child = children[i];
					if (this.isChildItemElem(child))
						result.push(child);
				}
				return result;
			}
		});
		this.defineProp('enableSelect', {
			'dataType': DataType.BOOL,
			'setter': function(value)
			{
				this.setPropStoreFieldValue('enableSelect', value);
				if (!value)
					this.clearSelection();
			}
		});
		this.defineProp('enableMultiSelect', {'dataType': DataType.BOOL,
			'setter': function(value)
			{
				this.setPropStoreFieldValue('enableMultiSelect', value);
				if (!value)
					this.clearSelection();
			}
		});
		this.defineProp('enableSelectInSelection', {'dataType': DataType.BOOL,
			'setter': function(value)
			{
				this.setPropStoreFieldValue('enableSelectInSelection', value);
				var curr = this.getSelectedItem();
				if (curr)
				{
					if (value)
					{
						EU.addClass(curr, CNS.STATE_CURRENT_SELECTED);
					}
					else
					{
						EU.removeClass(curr, CNS.STATE_CURRENT_SELECTED);
					}
				}
			}
		});
		this.defineProp('selection', {'dataType': DataType.ARRAY, 'serializable': false,
			'scope': Class.PropertyScope.PUBLIC,
			'getter': function()
			{
				var result = this.getPropStoreFieldValue('selection');
				if (!result)
				{
					result = [];
					this.setPropStoreFieldValue('selection', result);
				}
				return result;
			},
			'setter': function(value)
			{
				this.select(value);
			}
		});
		this.defineProp('currSelectedItem', {
			'dataType': DataType.OBJECT, 'serializable': false,
			'getter': function() { return this.getSelectedItem(); },
			'setter': function(value) { this.setSelectedItem(value); }
		});
		this.defineProp('selectedItem', {'dataType': DataType.OBJECT, 'serializable': false,
			'getter': function()
			{
				var selection = this.getSelection();
				return selection.length? selection[selection.length - 1]: null;
			},
			'setter': function(value)
			{
				var item = this.getBelongedChildItem(value);
				if (item)
				{
					if (this.getEnableSelectInSelection())
					{
						var selection = this.getSelection();
						var index = selection.indexOf(item);
						if (index >= 0)
						{
							this.prepareChangingSelection();
							selection.splice(index, 1);
							selection.push(item);   // put item into tail
							this.selectionChanged();
						}
						else
							this.select(item);
					}
					else
						this.select(item);
				}
				else
					this.clearSelection();
			}
		});
	},

	/** @ignore */
	doCreateSubElements: function(doc, docFragment)
	{
		var result = this.tryApplySuper('doCreateSubElements', []);
		this._holderElem = doc.createElement('ul');
		this._holderElem.className = CNS.LISTVIEW_ITEM_HOLDER;
		docFragment.appendChild(this._holderElem);
		result = result.concat(this._holderElem);
		return result;
	},
	/** @ignore */
	getChildrenHolderElement: function()
	{
		return this._holderElem;
	},
	/** @ignore */
	doGetWidgetClassName: function(/*$super*/)
	{
		return this.tryApplySuper('doGetWidgetClassName') + ' ' + CNS.LISTVIEW;
	},

	/**
	 * Returns the HTML class name for all child items.
	 * Descendants can override this method.
	 * @returns {string}
	 */
	getItemClassName: function()
	{
		return CNS.LISTVIEW_ITEM;
	},
<commit_msg>Fix an error preventing create ListView widget directly in js code<commit_after>(function(){
"use strict";

var DU = Kekule.DomUtils;
var EU = Kekule.HtmlElementUtils;
var CNS = Kekule.Widget.HtmlClassNames;

/** @ignore */
Kekule.Widget.HtmlClassNames = Object.extend(Kekule.Widget.HtmlClassNames, {
	LISTVIEW: 'K-ListView',
	LISTVIEW_ITEM_HOLDER: 'K-ListView-ItemHolder',
	LISTVIEW_ITEM: 'K-ListView-Item',
	LISTVIEW_ITEMCONTENT: 'K-ListView-ItemContent'
});

/**
 * List view widget, provide the ability to list/select a series of elements inside.
 * @class
 * @augments Kekule.Widget.NestedContainer
 *
 * @property {Array} items All child list items in list view, array of HTML elements.
 * @property {Array} selection All selected items in list view.
 * @property {Object} selectedItem Most recently selected item in tree view.
 * @property {Bool} enableSelect Whether item selecting is enabled.
 * @property {Bool} enableMultiSelect Whether only one item can be selected at same time.
 * @property {Bool} enableSelectInSelection Whether the most active one is allowed to be selected in selection.
 */
/**
 * Invoked when the selection is changed in the list view.
 *   event param of it has one fields: {selection: Array, selectedItem: HTMLElement}
 * @name Kekule.Widget.ListView#selectionChange
 * @event
 */
Kekule.Widget.ListView = Class.create(Kekule.Widget.BaseWidget,
/** @lends Kekule.Widget.ListView# */
{
	/** @private */
	CLASS_NAME: 'Kekule.Widget.ListView',
	/** @private */
	ITEM_DATA_FIELD: '__$itemData__',
	/** @construct */
	initialize: function(parentOrElementOrDocument)
	{
		this.setPropStoreFieldValue('enableSelect', true);
		this.setPropStoreFieldValue('enableMultiSelect', true);
		this.tryApplySuper('initialize', [parentOrElementOrDocument]);
	},
	/** @private */
	initProperties: function() {
		this.defineProp('items', {'dataType': DataType.ARRAY, 'serializable': false,
			'getter': function()
			{
				var result = [];
				var children = DU.getDirectChildElems(this.getChildrenHolderElement());
				for (var i = 0, l = children.length; i < l; ++i)
				{
					var child = children[i];
					if (this.isChildItemElem(child))
						result.push(child);
				}
				return result;
			}
		});
		this.defineProp('enableSelect', {
			'dataType': DataType.BOOL,
			'setter': function(value)
			{
				this.setPropStoreFieldValue('enableSelect', value);
				if (!value)
					this.clearSelection();
			}
		});
		this.defineProp('enableMultiSelect', {'dataType': DataType.BOOL,
			'setter': function(value)
			{
				this.setPropStoreFieldValue('enableMultiSelect', value);
				if (!value)
					this.clearSelection();
			}
		});
		this.defineProp('enableSelectInSelection', {'dataType': DataType.BOOL,
			'setter': function(value)
			{
				this.setPropStoreFieldValue('enableSelectInSelection', value);
				var curr = this.getSelectedItem();
				if (curr)
				{
					if (value)
					{
						EU.addClass(curr, CNS.STATE_CURRENT_SELECTED);
					}
					else
					{
						EU.removeClass(curr, CNS.STATE_CURRENT_SELECTED);
					}
				}
			}
		});
		this.defineProp('selection', {'dataType': DataType.ARRAY, 'serializable': false,
			'scope': Class.PropertyScope.PUBLIC,
			'getter': function()
			{
				var result = this.getPropStoreFieldValue('selection');
				if (!result)
				{
					result = [];
					this.setPropStoreFieldValue('selection', result);
				}
				return result;
			},
			'setter': function(value)
			{
				this.select(value);
			}
		});
		this.defineProp('currSelectedItem', {
			'dataType': DataType.OBJECT, 'serializable': false,
			'getter': function() { return this.getSelectedItem(); },
			'setter': function(value) { this.setSelectedItem(value); }
		});
		this.defineProp('selectedItem', {'dataType': DataType.OBJECT, 'serializable': false,
			'getter': function()
			{
				var selection = this.getSelection();
				return selection.length? selection[selection.length - 1]: null;
			},
			'setter': function(value)
			{
				var item = this.getBelongedChildItem(value);
				if (item)
				{
					if (this.getEnableSelectInSelection())
					{
						var selection = this.getSelection();
						var index = selection.indexOf(item);
						if (index >= 0)
						{
							this.prepareChangingSelection();
							selection.splice(index, 1);
							selection.push(item);   // put item into tail
							this.selectionChanged();
						}
						else
							this.select(item);
					}
					else
						this.select(item);
				}
				else
					this.clearSelection();
			}
		});
	},

	/** @ignore */
	doCreateSubElements: function(doc, docFragment)
	{
		var result = this.tryApplySuper('doCreateSubElements', []);
		this._holderElem = doc.createElement('ul');
		this._holderElem.className = CNS.LISTVIEW_ITEM_HOLDER;
		docFragment.appendChild(this._holderElem);
		result = result.concat(this._holderElem);
		return result;
	},
	/** @ignore */
	getChildrenHolderElement: function()
	{
		return this._holderElem;
	},
	/** @ignore */
	doGetWidgetClassName: function(/*$super*/)
	{
		return this.tryApplySuper('doGetWidgetClassName') + ' ' + CNS.LISTVIEW;
	},
	/** @ignore */
	doCreateRootElement: function(doc)
	{
		var result = doc.createElement('ul');
		return result;
	},

	/**
	 * Returns the HTML class name for all child items.
	 * Descendants can override this method.
	 * @returns {string}
	 */
	getItemClassName: function()
	{
		return CNS.LISTVIEW_ITEM;
	},

{'fraction_non_alphanumeric': 0.14028131785141132, 'fraction_numerical': 0.0007552157084867366, 'mean_word_length': 3.6772626931567327, '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': '3012642', 'n_tokens_mistral': 3973, 'n_tokens_neox': 3276, 'n_words': 720}