UNPKG

gd-bs

Version:

Bootstrap JavaScript, TypeScript and Web Components library.

80 lines (79 loc) 3.32 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Accordion = void 0; var base_1 = require("../base"); var templates_1 = require("./templates"); var item_1 = require("./item"); /** * Accordion */ var _Accordion = /** @class */ (function (_super) { __extends(_Accordion, _super); // Constructor function _Accordion(props, template, itemTemplate) { if (template === void 0) { template = templates_1.HTML; } if (itemTemplate === void 0) { itemTemplate = templates_1.HTMLItem; } var _this = _super.call(this, template, props) || this; _this._items = null; // Ensure the id is set _this.el.id = _this.el.id || props.id || "accordion"; // Render the items _this.renderItems(itemTemplate); // Configure the parent _this.configureParent(); return _this; } // Configure the item event _Accordion.prototype.configureEvent = function (item) { var _this = this; // Set the click event if (item.elHeader) { item.elHeader.addEventListener("click", function (ev) { // Parse the items for (var i = 0; i < _this._items.length; i++) { var item_2 = _this._items[i]; // Toggle the item if it's active if (item_2.isExpanded) { item_2.toggle(); } } // Toggle this item item.toggle(); }); } }; // Renders the items _Accordion.prototype.renderItems = function (itemTemplate) { // Clear the items this._items = []; // Set the flag var autoCollapse = typeof (this.props.autoCollapse) === "boolean" ? this.props.autoCollapse : true; // Parse the items var items = this.props.items || []; for (var i = 0; i < items.length; i++) { // Create the item and append it to the accordion var item = new item_1.AccordionItem(this.el.id, this.el.id + i, items[i], itemTemplate, autoCollapse); this._items.push(item); autoCollapse ? this.configureEvent(item) : null; this.el.appendChild(item.el); } }; return _Accordion; }(base_1.Base)); var Accordion = function (props, template, itemTemplate) { return new _Accordion(props, template, itemTemplate); }; exports.Accordion = Accordion;