var CheckboxListv2 = (function () { 'use strict'; return new Exports(); function New(target, title, items, updateFn, wide) { if (!!updateFn) { if (typeof updateFn !== "function") { console.error("invalid updateFn", updateFn) return null; } } return new CheckboxList(target, title, items, updateFn, wide); } function CheckboxList(target, title, items, updateFn, wide) { var element = document.createElement("checkbox-list"), search = null, list = []; if (wide) element.classList.add('wide'); init(); this.insert = insert; this.getActive = getActive; this.exit = exit; function init() { var hdr = newHeader(title); search = new SearchContainer(hdr, { "data": items.map(titlesMap) }, searchAction); element.appendChild(hdr); items.forEach(insert); target.appendChild(element); } function searchAction(results) { list.forEach(function (checkbox) { if (results.some(function (title) { return checkbox.getTitle() === title; })) { checkbox.show(); } else { checkbox.hide(); } }); } function insert(item) { if (!isItem(item)) { console.error("invalid checkbox item: expected CheckboxListv2.Item, received", item); return; } list.push(new Checkbox(element, item.title, item.value, item.active, updateFn)); } function getActive() { return list.filter(activeFilter); } function closeCheckbox(checkbox) { checkbox.exit(); } function exit() { list.forEach(closeCheckbox); element.innerHTML = ""; element = list = null; } } function Checkbox(target, title, value, active, updateFn) { var element = document.createElement("checkbox-container"), checkbox = document.createElement("checkbox"), label = document.createElement("label"), state = false; init(); this.getTitle = getTitle; this.getValue = getValue; this.getState = getState; this.toggle = toggle; this.setActive = setActive; this.unsetActive = unsetActive; this.show = show; this.hide = hide; this.exit = exit; function init() { label.textContent = title; checkbox.addEventListener("click", clickAction); element.appendChild(checkbox); element.appendChild(label); target.appendChild(element); if (active === true) { silentSet(true); } } function getTitle() { return title; } function getValue() { return value; } function getState() { return state; } function toggle() { if (!state) { setActive(); } else { unsetActive(); } } function silentSet(active) { state = active === true; if (state) { element.classList.add("active"); } else { element.classList.remove("active"); } } function setActive() { state = true; element.classList.add("active"); if (!!updateFn) { updateFn(); } } function unsetActive() { state = false; element.classList.remove("active"); if (!!updateFn) { updateFn(); } } function show() { element.style.display = ""; } function hide() { element.style.display = "none"; } function exit() { checkbox.removeEventListener("click", clickAction); removeChild(target, element); element.innerHTML = ""; element = checkbox = label = null; state = false; } function clickAction(e) { e.preventDefault(); toggle(); } } function Item(title, value, active) { this.title = title || ""; this.value = value || null; this.active = active === true; } function Exports() { this.New = New; this.Item = Item; } function activeFilter(checkbox) { return checkbox.getState(); } function titlesMap(item) { return { "title" : item.title, "value" : item }; } function isItem(item) { return !!item && !!item.constructor && item.constructor === Item; } function newHeader(title) { var hdr = document.createElement("header"), h3 = document.createElement("h3"); h3.textContent = title; hdr.appendChild(h3); return hdr; } function removeChild(parent, child) { if (!parent || !child) { return false; } if (!child.parentNode || child.parentNode !== parent) { return false; } parent.removeChild(child); return true; } })();