Facebook
From ZipHttpd.com, 3 Years ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 121
  1. /**
  2.  * Bootstrap5 accordion support
  3.  *
  4.  * 【ライセンス】
  5.  *      The MIT License
  6.  *      Copyright (c) <2021> <copyright ZipHttpd.com>
  7.  *      以下に定める条件に従い、本ソフトウェアおよび関連文書のファイル(以下「ソフトウェア」)の複製を取得するすべての人に対し、ソフトウェアを無制限に扱うことを無償で許可します。
  8.  *      これには、ソフトウェアの複製を使用、複写、変更、結合、掲載、頒布、サブライセンス、および/または販売する権利、およびソフトウェアを提供する相手に同じことを許可する権利も無制限に含まれます。
  9.  *      上記の著作権表示および本許諾表示を、ソフトウェアのすべての複製または重要な部分に記載するものとします。
  10.  *      ソフトウェアは「現状のまま」で、明示であるか暗黙であるかを問わず、何らの保証もなく提供されます。
  11.  *      ここでいう保証とは、商品性、特定の目的への適合性、および権利非侵害についての保証も含みますが、それに限定されるものではありません。
  12.  *      作者または著作権者は、契約行為、不法行為、またはそれ以外であろうと、ソフトウェアに起因または関連し、あるいはソフトウェアの使用またはその他の扱いによって生じる一切の請求、損害、その他の義務について何らの責任も負わないものとします。
  13.  */
  14. "use strict";
  15.  
  16. function Accordion(id, issort) {
  17.         this.holder = document.querySelector("#" + id);
  18.         this.holder.classList.add("accordion");
  19.         this.issort = issort;
  20.         this.itemIds = [];
  21. };
  22. // ホルダーID
  23. Accordion.prototype.HolderId = function() {
  24.         return this.holder.id;
  25. }
  26. // アイテムID
  27. Accordion.prototype.ItemId = function(id) {
  28.         const holderId = this.HolderId();
  29.         const itemId = `accordion-item-${holderId}_${id}`;
  30.         return itemId;
  31. }
  32. Accordion.prototype.AddItem = function(id, title, body, data, beforeItem, expand) {
  33.         const itemId = this.ItemId(id);
  34.         const index = this.itemIds.findIndex(elem => elem.id > id);
  35.         if (this.issort && index>=0) {
  36.                 beforeItem = this.Item(this.itemIds[index].id);
  37.                 this.itemIds.splice(index, 0, {id:id, data:data});
  38.         } else {
  39.                 this.itemIds.push({id:id, data:data});
  40.         }
  41.         const item = createAccordionItem(this.holder, id, title, body, beforeItem, expand);
  42.         return item;
  43. }
  44. Accordion.prototype.RemoveItem = function(id) {
  45.         const index = this.itemIds.findIndex(elem => elem.id > id);
  46.         if (index<0) {
  47.                 return null;
  48.         }
  49.         this.itemIds.splice(index, 1);
  50.         return this.holder.removeChild(this.Item(id));
  51. }
  52. Accordion.prototype.Item = function(id) {
  53.         const itemId = this.ItemId(id);
  54.         return document.querySelector("#" + itemId);
  55. }
  56. Accordion.prototype.ItemData = function(id) {
  57.         const elem = this.itemIds.find(elem => elem.id = id);
  58.         return elem.data;
  59. }
  60. Accordion.prototype.Body = function(id) {
  61.         const holderId = this.HolderId();
  62.         const bodyId = `accordion-body-${holderId}_${id}`;
  63.         return document.querySelector("#" + bodyId);
  64. }
  65.  
  66. function createAccordionItem(holder, id, title, body, beforeItem, expand) {
  67.         const holderId = holder.id;
  68.  
  69.         const itemId = `accordion-item-${holderId}_${id}`;
  70.         const item_attr = {
  71.                 id: itemId
  72.                 , class: "accordion-item"
  73.         };
  74.         const item = createElement("div", item_attr);
  75.  
  76.         // ヘッダ部
  77.         const headerId = `accordion-heading-${holderId}_${id}`;
  78.         const h_attr = {class:"accordion-header", id: headerId};
  79.         const header = createElement("h2", h_attr);
  80.         item.appendChild(header);
  81.  
  82.         // 開閉ボタン
  83.         const collapseId = `accordion-collapse-${holderId}_${id}`;
  84.         const button_attr = {
  85.                 class: "accordion-button" + (expand ? "" : " collapsed")
  86.                 , type: "button"
  87.                 , "data-bs-toggle": "collapse"
  88.                 , "data-bs-target": "#" + collapseId
  89.                 , "aria-expanded": expand ? true : false
  90.                 , "aria-controls": collapseId
  91.         };
  92.         const accordionbutton = createElement("button", button_attr);
  93.         if ("string"==typeof(title)) {
  94.                 accordionbutton.appendChild(document.createTextNode(title));
  95.         } else {
  96.                 accordionbutton.appendChild(title);
  97.         }
  98.         header.appendChild(accordionbutton);
  99.  
  100.         // 開閉部
  101.         const collapse_attr = {
  102.                 id: collapseId
  103.                 , class: "accordion-collapse collapse" + (expand ? " show" : "")
  104.                 , "aria-labelledby": headerId
  105.                 , "data-bs-parent": "#" + holderId
  106.         };
  107.         const collapse = createElement("div", collapse_attr);
  108.  
  109.         // 開閉部コンテンツ
  110.         const bodyId = `accordion-body-${holderId}_${id}`;
  111.         const body_attr = {
  112.                 id: bodyId
  113.                 , class: "accordion-body"
  114.         };
  115.         const accordionbody = createElement("div", body_attr);
  116.         if (body) {
  117.                 accordionbody.appendChild(body);
  118.         }
  119.         collapse.appendChild(accordionbody);
  120.         item.appendChild(collapse);
  121.  
  122.         // アコーディオンに追加
  123.         holder.insertBefore(item, beforeItem);
  124.  
  125.         return item;
  126. }
  127.  

Replies to accordion.js rss

Title Name Language When
Re: accordion.js Innocent Cassowary javascript 3 Years ago.