<commit_before>/* editableList.js
 *
 * An MVC and ajax-friendly list controler with user-editable items.
 *
 * editableList.list - Transforms an element into an editable list.
 *
 *  Once "listified", the element will ensure that there is exactly
 *  one child element for each item in its model.
 *
 *  Any html element that can contain children can become an editable
 *  list by calling editableList.list() on it directly.
 *
 *  An alternative way to create editable lists is with the
 *  <editable-list> tag. This module registers a page onload handler
 *  which will automagically call editableList.list() on the
 *  element. You may wish to include editableList.css to apply a basic
 *  default style.
 *
 * editableList.item - A factory function to create editable list
 *  items.
 *
 *  The default implementation presents a plain list item with no
 *  additional controls. It can be customized if different behavior is
 *  desired.
 *
 *  When the "edit mode" of the parent list is truthy, list items are
 *  made directly editable. The default behavior is to update the list
 *  model when an item loses focus. The current implementation also
 *  requires that the browser support the contentEditable attribute
 *  correctly.
 *
 *  List Item Interface:
 *
 *  + onEnterKey(): called when the enter key is pressed on the
 *   element. The default behavior is to blur the item, thereby
 *   commiting changes to the model.
 *
 *  + getContent(): returns the current content of the item.
 *
 *  + setContent(): updates the current content of the item.
 *
 *  + selectAll():  selects the contents of the item
 *
 *  + setEditable(editable): sets the edit state of the item. should be
 *    boolean true or false
 *
 *
 * Requires: util.js
 */


var editableList = (function () {
"use strict";

function readyHandler() {
    var i;
    var elements;

    elements = document.getElementsByTagName("editable-list");
    for (i = 0; i < elements.length; i++) {
	console.log("Converting to editable list: ", elements[i]);
	editableList(elements[i]);
    }
};

whenReady(readyHandler);

function editableListItem(list, model, items) {
    var item = el("div");
    var content;

    item.className = "listItem";

    item.onkeypress = function (evt) {
	switch (evt.keyCode) {
	case 13:
	    item.onEnterKey();
	    evt.preventDefault();
	    break;
	};
    };

    item.onfocus = function (evt) {
	list.setSelected(item);
	content = item.getContent();
	setTimeout(item.selectAll, 1);
    };

    item.onblur = function (evt) {
	if (item.innerHTML === content) {
	    return;
	}
	item.setEditable(false);
	model.update(items.indexOf(item), {content: item.getContent()});
    };

    item.onEnterKey = function (content) {
	item.blur();
    };

    item.getContent = function (content) {
	return item.innerHTML;
    };

    item.selectAll = function () {
	document.getSelection().selectAllChildren(item);
    };

    item.setContent = function (content) {
	item.innerHTML = content;
    };

    item.setEditable = function (editable) {
	item.setAttribute("contentEditable", editable);
    };

    return item;
}


function editableList(ret, model, listItem) {
    var items = [];
    var selected;
    var editable;
    var handlers;

    model = model
	|| eval(ret.getAttribute("model"))
	|| listModel("New Item");

    console.log(model);

    listItem = listItem
	|| eval(ret.getAttribute("listItem"))
	|| editableListItem;

    function itemAdded(index, attrs) {
	var item = listItem(ret, model, items);
	var attr;

	console.log("item added " + attrs.content);

	ret.insertBefore(item, items[index]);
	items.splice(index, 0, item);

	for (attr in attrs) {
	    itemChanged(index, attr, attrs[attr]);
	}

	item.setEditable(ret.getEditMode());
	item.focus();
    }

    function itemChanged(index, attr, value) {
	var item = items[index];

	if (!item) {
	    return;<commit_msg>scroll to position of newly added items<commit_after>/* editableList.js
 *
 * An MVC and ajax-friendly list controler with user-editable items.
 *
 * editableList.list - Transforms an element into an editable list.
 *
 *  Once "listified", the element will ensure that there is exactly
 *  one child element for each item in its model.
 *
 *  Any html element that can contain children can become an editable
 *  list by calling editableList.list() on it directly.
 *
 *  An alternative way to create editable lists is with the
 *  <editable-list> tag. This module registers a page onload handler
 *  which will automagically call editableList.list() on the
 *  element. You may wish to include editableList.css to apply a basic
 *  default style.
 *
 * editableList.item - A factory function to create editable list
 *  items.
 *
 *  The default implementation presents a plain list item with no
 *  additional controls. It can be customized if different behavior is
 *  desired.
 *
 *  When the "edit mode" of the parent list is truthy, list items are
 *  made directly editable. The default behavior is to update the list
 *  model when an item loses focus. The current implementation also
 *  requires that the browser support the contentEditable attribute
 *  correctly.
 *
 *  List Item Interface:
 *
 *  + onEnterKey(): called when the enter key is pressed on the
 *   element. The default behavior is to blur the item, thereby
 *   commiting changes to the model.
 *
 *  + getContent(): returns the current content of the item.
 *
 *  + setContent(): updates the current content of the item.
 *
 *  + selectAll():  selects the contents of the item
 *
 *  + setEditable(editable): sets the edit state of the item. should be
 *    boolean true or false
 *
 *
 * Requires: util.js
 */


var editableList = (function () {
"use strict";

function readyHandler() {
    var i;
    var elements;

    elements = document.getElementsByTagName("editable-list");
    for (i = 0; i < elements.length; i++) {
	console.log("Converting to editable list: ", elements[i]);
	editableList(elements[i]);
    }
};

whenReady(readyHandler);

function editableListItem(list, model, items) {
    var item = el("div");
    var content;

    item.className = "listItem";

    item.onkeypress = function (evt) {
	switch (evt.keyCode) {
	case 13:
	    item.onEnterKey();
	    evt.preventDefault();
	    break;
	};
    };

    item.onfocus = function (evt) {
	list.setSelected(item);
	content = item.getContent();
	setTimeout(item.selectAll, 1);
    };

    item.onblur = function (evt) {
	if (item.innerHTML === content) {
	    return;
	}
	item.setEditable(false);
	model.update(items.indexOf(item), {content: item.getContent()});
    };

    item.onEnterKey = function (content) {
	item.blur();
    };

    item.getContent = function (content) {
	return item.innerHTML;
    };

    item.selectAll = function () {
	document.getSelection().selectAllChildren(item);
    };

    item.setContent = function (content) {
	item.innerHTML = content;
    };

    item.setEditable = function (editable) {
	item.setAttribute("contentEditable", editable);
    };

    return item;
}


function editableList(ret, model, listItem) {
    var items = [];
    var selected;
    var editable;
    var handlers;
    var scrollOnInsert;
    var scrollOffset;

    model = model
	|| eval(ret.getAttribute("model"))
	|| listModel("New Item");

    console.log(model);

    listItem = listItem
	|| eval(ret.getAttribute("listItem"))
	|| editableListItem;

    scrollOnInsert = eval(ret.getAttribute("scrollOnInsert"))
	|| false;

    scrollOffset = eval(ret.getAttribute("scrollOffset"))
	|| 0;

    function itemAdded(index, attrs) {
	var item = listItem(ret, model, items);
	var attr;
	var rect;
	var before = items[index];

	console.log("item added " + attrs.content);

	ret.insertBefore(item, before);
	items.splice(index, 0, item);

	for (attr in attrs) {
	    itemChanged(index, attr, attrs[attr]);
	}

	item.setEditable(ret.getEditMode());
	if (scrollOnInsert) {
	    window.scrollTo(0, item.offsetTop + scrollOffset);
	}
	item.focus();
    }

    function itemChanged(index, attr, value) {
	var item = items[index];

	if (!item) {
	    return;
{'fraction_non_alphanumeric': 0.11373470144640871, 'fraction_numerical': 0.0014834961058227223, 'mean_word_length': 3.3967391304347827, 'pattern_counts': {'":': 0, '<': 7, '<?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': '713741', 'n_tokens_mistral': 2503, 'n_tokens_neox': 2445, 'n_words': 793}