{"size": 2370, "ext": "js", "max_stars_repo_path": "assets/js/test-results-by-task-type/sort.js", "max_stars_repo_name": "majacQ/web.client.simplytestable.com", "max_stars_repo_stars_event_min_datetime": "2017-12-20T20:04:49.000Z", "max_stars_repo_stars_event_max_datetime": "2017-12-20T20:04:49.000Z", "max_issues_repo_path": "assets/js/test-results-by-task-type/sort.js", "max_issues_repo_name": "webignition/web.client.simplytestable.com", "max_issues_repo_issues_event_min_datetime": "2019-01-04T10:56:06.000Z", "max_issues_repo_issues_event_max_datetime": "2019-04-22T16:29:11.000Z", "max_forks_repo_path": "assets/js/test-results-by-task-type/sort.js", "max_forks_repo_name": "majacQ/web.client.simplytestable.com", "max_forks_repo_forks_event_min_datetime": "2021-05-31T01:02:57.000Z", "max_forks_repo_forks_event_max_datetime": "2021-05-31T01:02:57.000Z"}
{"max_stars_count": 1, "max_issues_count": 352, "max_forks_count": 1, "avg_line_length": 31.1842105263, "max_line_length": 134, "alphanum_fraction": 0.6523206751}
let SortControl = require('../model/element/sort-control');
let SortableItem = require('../model/element/sortable-item');
let SortableItemList = require('../model/sortable-item-list');
let SortControlCollection = require('../model/sort-control-collection');

class Sort {
    /**
     * @param {Element} element
     * @param {NodeList} sortableItemsNodeList
     */
    constructor (element, sortableItemsNodeList) {
        this.element = element;
        this.sortControlCollection = this._createSortableControlCollection();
        this.sortableItemsNodeList = sortableItemsNodeList;
        this.sortableItemsList = this._createSortableItemList();
    };

    init () {
        this.element.classList.remove('invisible');
        this.sortControlCollection.controls.forEach((control) => {
            control.init();
            control.element.addEventListener(SortControl.getSortRequestedEventName(), this._sortControlClickEventListener.bind(this));
        });
    };

    /**
     * @returns {SortableItemList}
     * @private
     */
    _createSortableItemList () {
        let sortableItems = [];

        [].forEach.call(this.sortableItemsNodeList, (sortableItemElement) => {
            sortableItems.push(new SortableItem(sortableItemElement));
        });

        return new SortableItemList(sortableItems);
    }

    /**
     * @returns {SortControlCollection}
     * @private
     */
    _createSortableControlCollection () {
        let controls = [];

        [].forEach.call(this.element.querySelectorAll('.sort-control'), (sortControlElement) => {
            controls.push(new SortControl(sortControlElement));
        });

        return new SortControlCollection(controls);
    };

    /**
     * @param {CustomEvent} event
     * @private
     */
    _sortControlClickEventListener (event) {
        let parent = this.sortableItemsNodeList.item(0).parentElement;

        [].forEach.call(this.sortableItemsNodeList, (sortableItemElement) => {
            sortableItemElement.parentElement.removeChild(sortableItemElement);
        });

        let sortedItems = this.sortableItemsList.sort(event.detail.keys);

        sortedItems.forEach((sortableItem) => {
            parent.insertAdjacentElement('beforeend', sortableItem.element);
        });

        this.sortControlCollection.setSorted(event.target);
    };
}

module.exports = Sort;
