diff --git a/.eslintrc.js b/.eslintrc.js index 1e253c7..6c552d4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -3,13 +3,13 @@ module.exports = { browser: true, es2021: true, }, - extends: ['eslint:recommended'], - plugins: ['prettier'], + extends: ["eslint:recommended", "prettier"], + plugins: ["prettier"], rules: { - 'prettier/prettier': 'error', - 'comma-dangle': ['error', 'only-multiline'], - 'linebreak-style': ['error', 'windows'], - 'no-param-reassign': [2, { props: false }], + "prettier/prettier": "error", + "comma-dangle": ["error", "only-multiline"], + "linebreak-style": ["error", "windows"], + "no-param-reassign": [2, { props: false }], }, - parser: '@babel/eslint-parser', + parser: "@babel/eslint-parser", }; diff --git a/.prettierrc b/.prettierrc index b0f8d70..8cc5540 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,6 +1,6 @@ { "semi": true, - "singleQuote": true, + "singleQuote": false, "tabWidth": 2, "endOfLine": "auto", "printWidth": 80 diff --git a/README.md b/README.md index 8c83d47..1beed86 100644 --- a/README.md +++ b/README.md @@ -60,16 +60,16 @@ npm run prod ``` - +## CDN HTML ```html - + ``` CSS ```html - + ``` -- OR -- @@ -156,7 +156,7 @@ CSS ### Only the ellipsis → [live example](https://tomik23.github.io/show-more#ellipsis): ```html -
+
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
@@ -244,9 +244,11 @@ document.addEventListener('DOMContentLoaded', function () { }); }); ``` -You can also mix, `global configuration` + `data-config`. In these cases, `data-config` takes precedence over `global configuration`. +You can also mix, `global configuration` + `data-config`. In such cases, the `global configuration` object will merge with` data-config`. For example, we have 10 texts to shorten, then we add the global configuration, but we also have a table that we want to shorten, in this case we add `data-config` to table - [see table example](#for-table--live-example) +You don't need to add all the variables in the `data-config`. For example, if we have more/less buttons in `global configuration` then in `data-config` we do not have to declare them, but if we add them, they will overwrite their settings in globalen configuration. + ## Configuration of the plugin | element | require | description | @@ -260,6 +262,7 @@ For example, we have 10 texts to shorten, then we add the global configuration, | `more/less` | | is the text and chars that appears after the text, list or table e.g. `> show more` and `< show less` | | `number` | | number of hidden items to show more/less e.g. `-> show more 3`, only works for list and table | | `ellipsis` | | By default, adding an ellipsis to shortened text can be turned off by setting 'ellipsis': false | +| `nobutton` | | Diable showing the read more/less button, by default 'nobutton': false | | `regex` | | adding your own regular expressions. It is an object with two parameters `match` and `replace`, see example below | | `btnClass` | | Button class name. Default: `show-more-btn` | | `btnClassAppend` | | Opportunity to add additional classes to the button | @@ -280,7 +283,7 @@ Configuration for IE: ### cdn -- https://cdn.jsdelivr.net/gh/tomik23/show-more@1.1.2/dist/js/showMore.min.js +- https://cdn.jsdelivr.net/gh/tomik23/show-more@1.1.3/dist/js/showMore.ie.min.js ### cdn polyfill from npm diff --git a/dist/js/showMore.esm.js b/dist/js/showMore.esm.js index 90422ad..6eec4a7 100644 --- a/dist/js/showMore.esm.js +++ b/dist/js/showMore.esm.js @@ -1,15 +1,15 @@ -function getNumber(_ref, type) { +const getNumber = (_ref, type) => { let { rows, children } = _ref; - const elementType = type === 'table' ? rows : children; - const numbersElementHidden = [].slice.call(elementType).filter(el => el.className === 'hidden').length; - return numbersElementHidden !== 0 ? " " + numbersElementHidden : ''; -} -function htmlSubstr(originalText, count) { - let div = document.createElement('div'); - div.innerHTML = originalText; + const elementType = type === "table" ? rows : children; + const numbersElementHidden = [].slice.call(elementType).filter(el => el.className === "hidden").length; + return numbersElementHidden !== 0 ? " " + numbersElementHidden : ""; +}; +const htmlSubstr = (originalText, count) => { + let div = createElement("div"); + div.insertAdjacentHTML("afterbegin", originalText); walk(div, track); function track(el) { if (count > 0) { @@ -19,7 +19,7 @@ function htmlSubstr(originalText, count) { el.data = el.substringData(0, el.data.length + count); } } else { - el.data = ''; + el.data = ""; } } function walk(el, fn) { @@ -33,33 +33,50 @@ function htmlSubstr(originalText, count) { } while (node = node.nextSibling); } return div.innerHTML; -} -function addRemoveClass(element, type) { +}; +const addRemoveClass = function (element, type) { if (type === void 0) { type = false; } - element.classList[type ? 'add' : 'remove']('hidden'); -} + return element.classList[type ? "add" : "remove"]("hidden"); +}; +const setAttributes = (el, object) => { + for (let key in object) { + el.setAttribute(key, object[key]); + } +}; +const createElement = type => document.createElement(type); const defaultRegex = { newLine: { match: /(\r\n|\n|\r)/gm, - replace: '' + replace: "" }, space: { match: /\s\s+/gm, - replace: ' ' + replace: " " }, br: { match: //gim, - replace: '' + replace: "" }, html: { match: /(<((?!b|\/b|!strong|\/strong)[^>]+)>)/gi, - replace: '' + replace: "" } }; +const defaultOptions = { + typeElement: "span", + more: false, + less: false, + number: false, + nobutton: false, + after: 0, + btnClass: "show-more-btn", + btnClassAppend: null +}; + class ShowMore { constructor(className, _temp) { let { @@ -67,194 +84,195 @@ class ShowMore { regex = {}, config } = _temp === void 0 ? {} : _temp; - const elements = document.querySelectorAll(className); - this.onMoreLess = onMoreLess; - this.regex = Object.assign(defaultRegex, regex); - for (let i = 0; i < elements.length; i++) { + this._initial = () => { const { - type, - limit, element, after, - more, - less, - number, ellipsis, + nobutton, + limit, + type + } = this._object; + setAttributes(element, { + "aria-expanded": "false" + }); + const limitCounts = limit + after; + const ellips = ellipsis === false ? "" : "..."; + console.log(nobutton); + if (type === "text") { + const originalText = element.innerHTML.trim(); + const elementText = element.textContent.trim(); + if (elementText.length > limitCounts) { + let orgTexReg = originalText; + for (let key in this._regex) { + const { + match, + replace + } = this._regex[key]; + if (key && match) orgTexReg = orgTexReg.replace(match, replace); + } + const truncatedText = htmlSubstr(orgTexReg, limit - 1).concat(ellips); + element.textContent = ""; + element.insertAdjacentHTML("beforeend", truncatedText); + this._clickEvent(element, { ...this._object, + originalText, + truncatedText + }); + if (nobutton) return; + this._addBtn(this._object); + } + } + if (type === "list" || type === "table") { + const items = this._getNumberCount(element, type); + if (items.length > limitCounts) { + for (let i = limit; i < items.length; i++) { + addRemoveClass(items[i], true); + } + this._clickEvent(type === "list" ? element : element.nextElementSibling, this._object); + if (nobutton) return; + this._addBtn(this._object); + } + } + }; + this._clickEvent = (element, object) => element.addEventListener("click", this._handleEvent.bind(this, object)); + this._createBtn = _ref => { + let { + element, + number, + less, + more, + type, btnClass, btnClassAppend - } = JSON.parse(elements[i].getAttribute('data-config')) || config; - this.object = { - index: i, - element: elements[i], + } = _ref; + const typeAria = this._checkExp ? less || "" : more || ""; + const label = this._checkExp ? "collapse" : "expand"; + const expanded = this._checkExp ? true : false; + const button = createElement("button"); + button.className = btnClassAppend == null ? btnClass : btnClass + " " + btnClassAppend; + setAttributes(button, { + "aria-expanded": expanded, + "aria-label": label, + tabindex: 0 + }); + button.insertAdjacentHTML("beforeend", number ? typeAria + getNumber(element, type) : typeAria); + return button; + }; + this._handleEvent = (object, _ref2) => { + let { + target + } = _ref2; + const { + element, type, limit, - classArray: elements[i].classList, - ellipsis, - typeElement: element || 'span', - more: more || false, - less: less || false, - number: number || false, - after: after || 0, - btnClass: btnClass || 'show-more-btn', - btnClassAppend: btnClassAppend || null - }; - this.initial(this.object); - } - } - initial(_ref) { - let { - element, - after, - ellipsis, - limit, - type - } = _ref; - element.setAttribute('aria-expanded', 'false'); - const limitCounts = limit + after; - const ellips = ellipsis === false ? '' : '...'; - if (type === 'text') { - let truncatedText = ''; - const originalText = element.innerHTML.trim(); - let elementText = element.textContent.trim(); - let orgTexReg = originalText; - for (let key in this.regex) { - const { - match, - replace - } = this.regex[key]; - if (key && match) orgTexReg = orgTexReg.replace(match, replace); - } - if (elementText.length > limitCounts) { - truncatedText = htmlSubstr(orgTexReg, limit).concat(ellips); - element.innerHTML = truncatedText; - this.addBtn(this.object); - this.clickEvent(element, { ...this.object, - originalText, - truncatedText - }); + less, + typeElement, + originalText, + truncatedText, + btnClass + } = object; + const checkContainsClass = target.classList.contains(btnClass); + if (!checkContainsClass) return; + const ariaExpanded = element.getAttribute("aria-expanded"); + this._checkExp = ariaExpanded === "false"; + if (type === "text" && checkContainsClass) { + element.textContent = ""; + element.insertAdjacentHTML("beforeend", this._checkExp ? originalText : truncatedText); + if (less) { + const el = createElement(typeElement); + el.classList.add("show-more-wrapper"); + el.insertAdjacentElement("beforeend", this._createBtn(object)); + element.appendChild(el); + } } - } - if (type === 'list' || type === 'table') { - const items = type === 'list' ? [].slice.call(element.children) : element.rows; - if (items.length > limitCounts) { - for (let i = limit; i < items.length; i++) { - addRemoveClass(items[i], true); + if (type === "list" || type === "table") { + const items = this._getNumberCount(element, type); + for (let i = 0; i < items.length; i++) { + const typeRemove = type === "list" ? i >= limit && i < items.length - 1 : i >= limit; + if (ariaExpanded === "false") { + addRemoveClass(items[i]); + } else if (typeRemove) { + addRemoveClass(items[i], true); + } } - this.addBtn(this.object); - this.clickEvent(type === 'list' ? element : element.nextElementSibling, this.object); } - } - } - clickEvent(element, object) { - element.addEventListener('click', this.handleEvent.bind(this, object)); - } - createBtn(_ref2) { - let { - element, - number, - less, - more, - type, - btnClass, - btnClassAppend - } = _ref2; - const typeAria = this.checkExp ? less || '' : more || ''; - const label = this.checkExp ? 'collapse' : 'expand'; - const expanded = this.checkExp ? true : false; - const btn = document.createElement('button'); - btn.className = btnClassAppend == null ? btnClass : btnClass + ' ' + btnClassAppend; - btn.setAttribute('aria-expanded', expanded); - btn.setAttribute('aria-label', label); - btn.setAttribute('tabindex', 0); - btn.innerHTML = number ? typeAria + getNumber(element, type) : typeAria; - return btn; - } - handleEvent(object, _ref3) { - let { - currentTarget, - target - } = _ref3; - const { - element, - type, - limit, - less, - typeElement, - originalText, - truncatedText, - btnClass - } = object; - const checkContainsClass = target.classList.contains(btnClass); - if (!checkContainsClass) return; - const ariaExpanded = element.getAttribute('aria-expanded'); - this.checkExp = ariaExpanded === 'false'; - if (type === 'text' && checkContainsClass) { - element.innerHTML = ''; - element.innerHTML = this.checkExp ? originalText : truncatedText; - if (less) { - const el = document.createElement(typeElement); - el.insertAdjacentElement('beforeend', this.createBtn(object)); - element.appendChild(el); + if (type) { + this._setExpand({ ...object, + target + }); } - } - if (type === 'list' || type === 'table') { - const items = type === 'list' ? [].slice.call(currentTarget.children) : element.rows; - for (let i = 0; i < items.length; i++) { - const typeRemove = type === 'list' ? i >= limit && i < items.length - 1 : i >= limit; - if (ariaExpanded === 'false') { - addRemoveClass(items[i]); - } else if (typeRemove) { - addRemoveClass(items[i], true); - } + }; + this._getNumberCount = (element, type) => { + return type === "list" ? [].slice.call(element.children) : element.rows; + }; + this._addBtn = object => { + const { + type, + element, + more, + typeElement + } = object; + if (!more) return; + if (type === "table") { + element.insertAdjacentElement("afterend", this._createBtn(object)); + } else { + const el = createElement(typeElement); + el.classList.add("show-more-wrapper"); + el.appendChild(this._createBtn(object)); + element.appendChild(el); } - } - if (type === 'table' || type === 'list' || type === 'text') { - this.setExpand({ ...object, + }; + this._setExpand = object => { + const { + element, + type, + less, + more, + number, target + } = object; + const typeAria = this._checkExp ? less : more; + const aria = this._checkExp ? "expand" : "collapse"; + const ariaText = type === "table" ? type : "the " + type; + const lastChild = element.lastElementChild; + setAttributes(element, { + "aria-expanded": this._checkExp }); - } - } - addBtn(object) { - const { - type, - element, - more, - typeElement - } = object; - if (!more) return; - if (type === 'table') { - element.insertAdjacentElement('afterend', this.createBtn(object)); - } else { - const el = document.createElement(typeElement); - el.appendChild(this.createBtn(object)); - element.appendChild(el); - } - } - setExpand(object) { - const { - element, - type, - less, - more, - number, - target - } = object; - const check = this.checkExp; - const typeAria = check ? less : more; - const aria = check ? 'expand' : 'collapse'; - const ariaText = type === 'table' ? type : "the " + type; - const lastChild = element.lastElementChild; - element.setAttribute('aria-expanded', check); - target.setAttribute('aria-expanded', check); - target.setAttribute('aria-label', aria + " " + ariaText); - this.onMoreLess(aria, object); - if (typeAria) { - target.innerHTML = number ? typeAria + getNumber(element, type) : typeAria; - } else if (type === 'table') { - target.parentNode.removeChild(target); - } else if (type === 'list') { - lastChild.parentNode.removeChild(lastChild); - } + setAttributes(target, { + "aria-expanded": this._checkExp, + "aria-label": aria + " " + ariaText + }); + this._onMoreLess(aria, object); + if (typeAria) { + target.innerHTML = number ? typeAria + getNumber(element, type) : typeAria; + } else if (type === "table") { + target.parentNode.removeChild(target); + } else if (type === "list") { + lastChild.parentNode.removeChild(lastChild); + } + }; + const elements = [].slice.call(document.querySelectorAll(className)); + this._onMoreLess = onMoreLess; + this._regex = { ...defaultRegex, + ...regex + }; + elements.map((item, index) => { + const configData = JSON.parse(item.getAttribute("data-config")); + const configGlobal = config; + const configDataAndGlobal = { ...configGlobal, + ...configData + }; + this._object = { + index, + classArray: item.classList, + ...defaultOptions, + ...configDataAndGlobal, + typeElement: configDataAndGlobal.element || "span", + element: item + }; + this._initial(); + }); } } diff --git a/dist/js/showMore.esm.js.map b/dist/js/showMore.esm.js.map index 2f8631c..f53fe7e 100644 --- a/dist/js/showMore.esm.js.map +++ b/dist/js/showMore.esm.js.map @@ -1 +1 @@ -{"version":3,"file":"showMore.esm.js","sources":["../../sources/utils/function.js","../../sources/utils/regex.js","../../sources/index.js"],"sourcesContent":["/**\r\n * Get hidden element\r\n *\r\n * @param {object} object\r\n * @param {String} type - type of element table | div\r\n * @returns\r\n */\r\nfunction getNumber({ rows, children }, type) {\r\n const elementType = type === 'table' ? rows : children;\r\n\r\n const numbersElementHidden = [].slice\r\n .call(elementType)\r\n .filter((el) => el.className === 'hidden').length;\r\n return numbersElementHidden !== 0 ? ` ${numbersElementHidden}` : '';\r\n}\r\n\r\n// https://stackoverflow.com/questions/6003271/substring-text-with-html-tags-in-javascript\r\n/**\r\n * Substring text width html tags\r\n *\r\n * @param {String} originalText - text with html tags\r\n * @param {String} count - limit of characters\r\n * @returns\r\n */\r\nfunction htmlSubstr(originalText, count) {\r\n let div = document.createElement('div');\r\n div.innerHTML = originalText;\r\n\r\n walk(div, track);\r\n\r\n function track(el) {\r\n if (count > 0) {\r\n let len = el.data.length;\r\n count -= len;\r\n if (count <= 0) {\r\n el.data = el.substringData(0, el.data.length + count);\r\n }\r\n } else {\r\n el.data = '';\r\n }\r\n }\r\n\r\n function walk(el, fn) {\r\n let node = el.firstChild;\r\n do {\r\n if (node.nodeType === 3) {\r\n fn(node);\r\n } else if (node.nodeType === 1 && node.childNodes && node.childNodes[0]) {\r\n walk(node, fn);\r\n }\r\n } while ((node = node.nextSibling));\r\n }\r\n return div.innerHTML;\r\n}\r\n\r\n/**\r\n * Add/remove class 'hidden' to element\r\n *\r\n * @param {HTMLElement} element\r\n * @param {String} type - type of element add or remove\r\n */\r\nfunction addRemoveClass(element, type = false) {\r\n element.classList[type ? 'add' : 'remove']('hidden');\r\n}\r\n\r\nexport { addRemoveClass, getNumber, htmlSubstr };\r\n","/**\r\n * Default regexes for validation\r\n */\r\nconst defaultRegex = {\r\n newLine: {\r\n match: /(\\r\\n|\\n|\\r)/gm,\r\n replace: '',\r\n },\r\n space: {\r\n match: /\\s\\s+/gm,\r\n replace: ' ',\r\n },\r\n br: {\r\n match: //gim,\r\n replace: '',\r\n },\r\n html: {\r\n match: /(<((?!b|\\/b|!strong|\\/strong)[^>]+)>)/gi,\r\n replace: '',\r\n },\r\n};\r\n\r\nexport default defaultRegex;\r\n","import { addRemoveClass, getNumber, htmlSubstr } from './utils/function';\r\nimport defaultRegex from './utils/regex';\r\n\r\n/**\r\n * @class ShowMore\r\n */\r\nexport default class ShowMore {\r\n /**\r\n * Constructor\r\n *\r\n * @param {HTMLElement} className\r\n * @param {Object} object\r\n */\r\n constructor(className, { onMoreLess = () => {}, regex = {}, config } = {}) {\r\n // all html elements\r\n const elements = document.querySelectorAll(className);\r\n\r\n // colback function\r\n this.onMoreLess = onMoreLess;\r\n\r\n // global regex\r\n this.regex = Object.assign(defaultRegex, regex);\r\n\r\n for (let i = 0; i < elements.length; i++) {\r\n const {\r\n type,\r\n limit,\r\n element,\r\n after,\r\n more,\r\n less,\r\n number,\r\n ellipsis,\r\n btnClass,\r\n btnClassAppend,\r\n } = JSON.parse(elements[i].getAttribute('data-config')) || config;\r\n\r\n // create global object\r\n this.object = {\r\n index: i,\r\n element: elements[i],\r\n type,\r\n limit,\r\n classArray: elements[i].classList,\r\n ellipsis,\r\n typeElement: element || 'span',\r\n more: more || false,\r\n less: less || false,\r\n number: number || false,\r\n after: after || 0,\r\n btnClass: btnClass || 'show-more-btn',\r\n btnClassAppend: btnClassAppend || null,\r\n };\r\n\r\n this.initial(this.object);\r\n }\r\n }\r\n\r\n /**\r\n * Initail function\r\n *\r\n * @param {Object} object\r\n */\r\n initial({ element, after, ellipsis, limit, type }) {\r\n // set default aria-expande to false\r\n element.setAttribute('aria-expanded', 'false');\r\n\r\n const limitCounts = limit + after;\r\n const ellips = ellipsis === false ? '' : '...';\r\n\r\n // text\r\n if (type === 'text') {\r\n let truncatedText = '';\r\n const originalText = element.innerHTML.trim();\r\n let elementText = element.textContent.trim();\r\n\r\n let orgTexReg = originalText;\r\n for (let key in this.regex) {\r\n const { match, replace } = this.regex[key];\r\n if (key && match) orgTexReg = orgTexReg.replace(match, replace);\r\n }\r\n\r\n if (elementText.length > limitCounts) {\r\n truncatedText = htmlSubstr(orgTexReg, limit).concat(ellips);\r\n\r\n element.innerHTML = truncatedText;\r\n\r\n this.addBtn(this.object);\r\n\r\n this.clickEvent(element, {\r\n ...this.object,\r\n originalText,\r\n truncatedText,\r\n });\r\n }\r\n }\r\n\r\n // list and table\r\n if (type === 'list' || type === 'table') {\r\n const items =\r\n type === 'list' ? [].slice.call(element.children) : element.rows;\r\n\r\n if (items.length > limitCounts) {\r\n for (let i = limit; i < items.length; i++) {\r\n addRemoveClass(items[i], true);\r\n }\r\n\r\n // add button to the list and table\r\n this.addBtn(this.object);\r\n\r\n // add event click\r\n this.clickEvent(\r\n type === 'list' ? element : element.nextElementSibling,\r\n this.object\r\n );\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Event click\r\n *\r\n * @param {HTMLElement} element\r\n * @param {Object} object\r\n */\r\n clickEvent(element, object) {\r\n element.addEventListener('click', this.handleEvent.bind(this, object));\r\n }\r\n\r\n /**\r\n * Create button\r\n *\r\n * @param {Object} object\r\n * @returns HTMLElement\r\n */\r\n createBtn({ element, number, less, more, type, btnClass, btnClassAppend }) {\r\n const typeAria = this.checkExp ? less || '' : more || '';\r\n const label = this.checkExp ? 'collapse' : 'expand';\r\n const expanded = this.checkExp ? true : false;\r\n\r\n const btn = document.createElement('button');\r\n btn.className =\r\n btnClassAppend == null ? btnClass : btnClass + ' ' + btnClassAppend;\r\n btn.setAttribute('aria-expanded', expanded);\r\n btn.setAttribute('aria-label', label);\r\n btn.setAttribute('tabindex', 0);\r\n btn.innerHTML = number ? typeAria + getNumber(element, type) : typeAria;\r\n return btn;\r\n }\r\n\r\n /**\r\n * Event handler\r\n *\r\n * @param {Object} object\r\n * @param {Event} event\r\n */\r\n handleEvent(object, { currentTarget, target }) {\r\n const {\r\n element,\r\n type,\r\n limit,\r\n less,\r\n typeElement,\r\n originalText,\r\n truncatedText,\r\n btnClass,\r\n } = object;\r\n\r\n // check if the button is clicked\r\n const checkContainsClass = target.classList.contains(btnClass);\r\n\r\n if (!checkContainsClass) return;\r\n\r\n const ariaExpanded = element.getAttribute('aria-expanded');\r\n this.checkExp = ariaExpanded === 'false';\r\n\r\n // --------------------------------------------------\r\n // text\r\n if (type === 'text' && checkContainsClass) {\r\n element.innerHTML = '';\r\n element.innerHTML = this.checkExp ? originalText : truncatedText;\r\n\r\n if (less) {\r\n const el = document.createElement(typeElement);\r\n el.insertAdjacentElement('beforeend', this.createBtn(object));\r\n element.appendChild(el);\r\n }\r\n }\r\n\r\n // --------------------------------------------------\r\n // list and table\r\n if (type === 'list' || type === 'table') {\r\n const items =\r\n type === 'list' ? [].slice.call(currentTarget.children) : element.rows;\r\n\r\n for (let i = 0; i < items.length; i++) {\r\n const typeRemove =\r\n type === 'list' ? i >= limit && i < items.length - 1 : i >= limit;\r\n\r\n if (ariaExpanded === 'false') {\r\n addRemoveClass(items[i]);\r\n } else if (typeRemove) {\r\n addRemoveClass(items[i], true);\r\n }\r\n }\r\n }\r\n\r\n // set aria-expanded\r\n if (type === 'table' || type === 'list' || type === 'text') {\r\n this.setExpand({ ...object, target });\r\n }\r\n }\r\n\r\n /**\r\n * Add button\r\n *\r\n * @param {Object} object\r\n */\r\n addBtn(object) {\r\n const { type, element, more, typeElement } = object;\r\n\r\n if (!more) return;\r\n\r\n if (type === 'table') {\r\n element.insertAdjacentElement('afterend', this.createBtn(object));\r\n } else {\r\n const el = document.createElement(typeElement);\r\n el.appendChild(this.createBtn(object));\r\n element.appendChild(el);\r\n }\r\n }\r\n\r\n /**\r\n * Set aria-expanded\r\n *\r\n * @param {Object} object\r\n */\r\n setExpand(object) {\r\n const { element, type, less, more, number, target } = object;\r\n\r\n const check = this.checkExp;\r\n\r\n const typeAria = check ? less : more;\r\n const aria = check ? 'expand' : 'collapse';\r\n const ariaText = type === 'table' ? type : `the ${type}`;\r\n const lastChild = element.lastElementChild;\r\n\r\n element.setAttribute('aria-expanded', check);\r\n target.setAttribute('aria-expanded', check);\r\n target.setAttribute('aria-label', `${aria} ${ariaText}`);\r\n\r\n // callback function on more/less\r\n this.onMoreLess(aria, object);\r\n\r\n if (typeAria) {\r\n target.innerHTML = number\r\n ? typeAria + getNumber(element, type)\r\n : typeAria;\r\n } else if (type === 'table') {\r\n target.parentNode.removeChild(target);\r\n } else if (type === 'list') {\r\n lastChild.parentNode.removeChild(lastChild);\r\n }\r\n }\r\n}\r\n"],"names":["getNumber","type","rows","children","elementType","numbersElementHidden","slice","call","filter","el","className","length","htmlSubstr","originalText","count","div","document","createElement","innerHTML","walk","track","len","data","substringData","fn","node","firstChild","nodeType","childNodes","nextSibling","addRemoveClass","element","classList","defaultRegex","newLine","match","replace","space","br","html","ShowMore","constructor","onMoreLess","regex","config","elements","querySelectorAll","Object","assign","i","limit","after","more","less","number","ellipsis","btnClass","btnClassAppend","JSON","parse","getAttribute","object","index","classArray","typeElement","initial","setAttribute","limitCounts","ellips","truncatedText","trim","elementText","textContent","orgTexReg","key","concat","addBtn","clickEvent","items","nextElementSibling","addEventListener","handleEvent","bind","createBtn","typeAria","checkExp","label","expanded","btn","currentTarget","target","checkContainsClass","contains","ariaExpanded","insertAdjacentElement","appendChild","typeRemove","setExpand","check","aria","ariaText","lastChild","lastElementChild","parentNode","removeChild"],"mappings":"AAOA,SAASA,SAAT,OAAuCC,IAAvC,EAA6C;AAAA,MAA1B;AAAEC,IAAAA,IAAF;AAAQC,IAAAA;AAAR,GAA0B;AAC3C,QAAMC,WAAW,GAAGH,IAAI,KAAK,OAAT,GAAmBC,IAAnB,GAA0BC,QAA9C;AAEA,QAAME,oBAAoB,GAAG,GAAGC,KAAH,CAC1BC,IAD0B,CACrBH,WADqB,EAE1BI,MAF0B,CAElBC,EAAD,IAAQA,EAAE,CAACC,SAAH,KAAiB,QAFN,EAEgBC,MAF7C;AAGA,SAAON,oBAAoB,KAAK,CAAzB,SAAiCA,oBAAjC,GAA0D,EAAjE;AACD;AAUD,SAASO,UAAT,CAAoBC,YAApB,EAAkCC,KAAlC,EAAyC;AACvC,MAAIC,GAAG,GAAGC,QAAQ,CAACC,aAAT,CAAuB,KAAvB,CAAV;AACAF,EAAAA,GAAG,CAACG,SAAJ,GAAgBL,YAAhB;AAEAM,EAAAA,IAAI,CAACJ,GAAD,EAAMK,KAAN,CAAJ;AAEA,WAASA,KAAT,CAAeX,EAAf,EAAmB;AACjB,QAAIK,KAAK,GAAG,CAAZ,EAAe;AACb,UAAIO,GAAG,GAAGZ,EAAE,CAACa,IAAH,CAAQX,MAAlB;AACAG,MAAAA,KAAK,IAAIO,GAAT;AACA,UAAIP,KAAK,IAAI,CAAb,EAAgB;AACdL,QAAAA,EAAE,CAACa,IAAH,GAAUb,EAAE,CAACc,aAAH,CAAiB,CAAjB,EAAoBd,EAAE,CAACa,IAAH,CAAQX,MAAR,GAAiBG,KAArC,CAAV;AACD;AACF,KAND,MAMO;AACLL,MAAAA,EAAE,CAACa,IAAH,GAAU,EAAV;AACD;AACF;AAED,WAASH,IAAT,CAAcV,EAAd,EAAkBe,EAAlB,EAAsB;AACpB,QAAIC,IAAI,GAAGhB,EAAE,CAACiB,UAAd;AACA,OAAG;AACD,UAAID,IAAI,CAACE,QAAL,KA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AqCiC,IAArC,SAA6CC,QAA7C,EAZgB;AAehB,SAAK1D,UAAL,CAAgByD,IAAhB,EAAsBtC,MAAtB;AAEA,QAAIuB,QAAJ,EAAc;AACZM,MAAAA,MAAM,CAACxE,SAAP,GAAmBoC,MAAM,GACrB8B,QAAQ,GAAGpF,SAAS,CAAC+B,OAAD,EAAU9B,IAAV,CADC,GAErBmF,QAFJ;AAGD,KAJD,MAIO,IAAInF,IAAI,KAAK,OAAb,EAAsB;AAC3ByF,MAAAA,MAAM,CAACa,UAAP,CAAkBC,WAAlB,CAA8Bd,MAA9B;AACD,KAFM,MAEA,IAAIzF,IAAI,KAAK,MAAb,EAAqB;AAC1BoG,MAAAA,SAAS,CAACE,UAAV,CAAqBC,WAArB,CAAiCH,SAAjC;AACD;AACF;AAjQ2B;;;;"} \ No newline at end of file +{"version":3,"file":"showMore.esm.js","sources":["../../sources/utils/function.js","../../sources/utils/regex.js","../../sources/utils/defaults.js","../../sources/index.js"],"sourcesContent":["/**\r\n * Get hidden element\r\n *\r\n * @param {object} object\r\n * @param {String} type - type of element table | div\r\n * @returns\r\n */\r\nconst getNumber = ({ rows, children }, type) => {\r\n const elementType = type === \"table\" ? rows : children;\r\n\r\n const numbersElementHidden = [].slice\r\n .call(elementType)\r\n .filter((el) => el.className === \"hidden\").length;\r\n return numbersElementHidden !== 0 ? ` ${numbersElementHidden}` : \"\";\r\n};\r\n\r\n// https://stackoverflow.com/questions/6003271/substring-text-with-html-tags-in-javascript\r\n/**\r\n * Substring text width html tags\r\n *\r\n * @param {String} originalText - text with html tags\r\n * @param {String} count - limit of characters\r\n * @returns\r\n */\r\nconst htmlSubstr = (originalText, count) => {\r\n let div = createElement(\"div\");\r\n div.insertAdjacentHTML(\"afterbegin\", originalText);\r\n\r\n walk(div, track);\r\n\r\n function track(el) {\r\n if (count > 0) {\r\n let len = el.data.length;\r\n count -= len;\r\n if (count <= 0) {\r\n el.data = el.substringData(0, el.data.length + count);\r\n }\r\n } else {\r\n el.data = \"\";\r\n }\r\n }\r\n\r\n function walk(el, fn) {\r\n let node = el.firstChild;\r\n do {\r\n if (node.nodeType === 3) {\r\n fn(node);\r\n } else if (node.nodeType === 1 && node.childNodes && node.childNodes[0]) {\r\n walk(node, fn);\r\n }\r\n } while ((node = node.nextSibling));\r\n }\r\n return div.innerHTML;\r\n};\r\n\r\n/**\r\n * Add/remove class 'hidden' to element\r\n *\r\n * @param {HTMLElement} element\r\n * @param {String} type - type of element add or remove\r\n */\r\nconst addRemoveClass = (element, type = false) => {\r\n return element.classList[type ? \"add\" : \"remove\"](\"hidden\");\r\n};\r\n\r\n/**\r\n * Set attributes to element\r\n *\r\n * @param {HTMLElement} el\r\n * @param {Object} object\r\n */\r\nconst setAttributes = (el, object) => {\r\n for (let key in object) {\r\n el.setAttribute(key, object[key]);\r\n }\r\n};\r\n\r\n/**\r\n * Create element\r\n *\r\n * @param {String} type - type of element\r\n * @returns {HTMLElement}\r\n */\r\nconst createElement = (type) => document.createElement(type);\r\n\r\nexport { addRemoveClass, createElement, getNumber, htmlSubstr, setAttributes };\r\n","/**\r\n * Default regexes for validation\r\n */\r\nconst defaultRegex = {\r\n newLine: {\r\n match: /(\\r\\n|\\n|\\r)/gm,\r\n replace: \"\",\r\n },\r\n space: {\r\n match: /\\s\\s+/gm,\r\n replace: \" \",\r\n },\r\n br: {\r\n match: //gim,\r\n replace: \"\",\r\n },\r\n html: {\r\n match: /(<((?!b|\\/b|!strong|\\/strong)[^>]+)>)/gi,\r\n replace: \"\",\r\n },\r\n};\r\n\r\nexport default defaultRegex;\r\n","const defaultOptions = {\r\n typeElement: \"span\",\r\n more: false,\r\n less: false,\r\n number: false,\r\n nobutton: false,\r\n after: 0,\r\n btnClass: \"show-more-btn\",\r\n btnClassAppend: null,\r\n};\r\n\r\nexport default defaultOptions;\r\n","import {\r\n addRemoveClass,\r\n createElement,\r\n getNumber,\r\n htmlSubstr,\r\n setAttributes,\r\n} from \"./utils/function\";\r\nimport defaultRegex from \"./utils/regex\";\r\nimport defaultOptions from \"./utils/defaults\";\r\n\r\n/**\r\n * @class ShowMore\r\n */\r\nexport default class ShowMore {\r\n /**\r\n * Constructor\r\n *\r\n * @param {HTMLElement} className\r\n * @param {Object} object\r\n */\r\n constructor(className, { onMoreLess = () => {}, regex = {}, config } = {}) {\r\n // all html elements\r\n const elements = [].slice.call(document.querySelectorAll(className));\r\n\r\n // colback function\r\n this._onMoreLess = onMoreLess;\r\n\r\n // global regex\r\n this._regex = { ...defaultRegex, ...regex };\r\n\r\n elements.map((item, index) => {\r\n const configData = JSON.parse(item.getAttribute(\"data-config\"));\r\n const configGlobal = config;\r\n\r\n const configDataAndGlobal = { ...configGlobal, ...configData };\r\n\r\n this._object = {\r\n index,\r\n classArray: item.classList,\r\n ...defaultOptions,\r\n ...configDataAndGlobal,\r\n typeElement: configDataAndGlobal.element || \"span\",\r\n element: item,\r\n };\r\n\r\n this._initial();\r\n });\r\n }\r\n\r\n /**\r\n * Initail function\r\n *\r\n * @param {Object} object\r\n */\r\n _initial = () => {\r\n const { element, after, ellipsis, nobutton, limit, type } = this._object;\r\n // set default aria-expande to false\r\n setAttributes(element, { \"aria-expanded\": \"false\" });\r\n\r\n const limitCounts = limit + after;\r\n const ellips = ellipsis === false ? \"\" : \"...\";\r\n\r\n console.log(nobutton);\r\n\r\n // text\r\n if (type === \"text\") {\r\n const originalText = element.innerHTML.trim();\r\n const elementText = element.textContent.trim();\r\n\r\n if (elementText.length > limitCounts) {\r\n let orgTexReg = originalText;\r\n\r\n for (let key in this._regex) {\r\n const { match, replace } = this._regex[key];\r\n if (key && match) orgTexReg = orgTexReg.replace(match, replace);\r\n }\r\n\r\n const truncatedText = htmlSubstr(orgTexReg, limit - 1).concat(ellips);\r\n\r\n element.textContent = \"\";\r\n element.insertAdjacentHTML(\"beforeend\", truncatedText);\r\n\r\n this._clickEvent(element, {\r\n ...this._object,\r\n originalText,\r\n truncatedText,\r\n });\r\n\r\n if (nobutton) return;\r\n this._addBtn(this._object);\r\n }\r\n }\r\n\r\n // list and table\r\n if (type === \"list\" || type === \"table\") {\r\n const items = this._getNumberCount(element, type);\r\n\r\n if (items.length > limitCounts) {\r\n for (let i = limit; i < items.length; i++) {\r\n addRemoveClass(items[i], true);\r\n }\r\n\r\n // add event click\r\n this._clickEvent(\r\n type === \"list\" ? element : element.nextElementSibling,\r\n this._object\r\n );\r\n\r\n if (nobutton) return;\r\n // add button to the list and table\r\n this._addBtn(this._object);\r\n }\r\n }\r\n };\r\n\r\n /**\r\n * Event click\r\n *\r\n * @param {HTMLElement} element\r\n * @param {Object} object\r\n */\r\n _clickEvent = (element, object) =>\r\n element.addEventListener(\"click\", this._handleEvent.bind(this, object));\r\n\r\n /**\r\n * Create button\r\n *\r\n * @param {Object} object\r\n * @returns HTMLElement\r\n */\r\n _createBtn = ({\r\n element,\r\n number,\r\n less,\r\n more,\r\n type,\r\n btnClass,\r\n btnClassAppend,\r\n }) => {\r\n const typeAria = this._checkExp ? less || \"\" : more || \"\";\r\n const label = this._checkExp ? \"collapse\" : \"expand\";\r\n const expanded = this._checkExp ? true : false;\r\n\r\n const button = createElement(\"button\");\r\n button.className =\r\n btnClassAppend == null ? btnClass : btnClass + \" \" + btnClassAppend;\r\n setAttributes(button, {\r\n \"aria-expanded\": expanded,\r\n \"aria-label\": label,\r\n tabindex: 0,\r\n });\r\n\r\n button.insertAdjacentHTML(\r\n \"beforeend\",\r\n number ? typeAria + getNumber(element, type) : typeAria\r\n );\r\n return button;\r\n };\r\n\r\n /**\r\n * Event handler\r\n *\r\n * @param {Object} object\r\n * @param {Event} event\r\n */\r\n _handleEvent = (object, { target }) => {\r\n const {\r\n element,\r\n type,\r\n limit,\r\n less,\r\n typeElement,\r\n originalText,\r\n truncatedText,\r\n btnClass,\r\n } = object;\r\n\r\n // check if the button is clicked\r\n const checkContainsClass = target.classList.contains(btnClass);\r\n\r\n if (!checkContainsClass) return;\r\n\r\n const ariaExpanded = element.getAttribute(\"aria-expanded\");\r\n this._checkExp = ariaExpanded === \"false\";\r\n\r\n // --------------------------------------------------\r\n // text\r\n if (type === \"text\" && checkContainsClass) {\r\n element.textContent = \"\";\r\n\r\n element.insertAdjacentHTML(\r\n \"beforeend\",\r\n this._checkExp ? originalText : truncatedText\r\n );\r\n\r\n if (less) {\r\n const el = createElement(typeElement);\r\n el.classList.add(\"show-more-wrapper\");\r\n el.insertAdjacentElement(\"beforeend\", this._createBtn(object));\r\n element.appendChild(el);\r\n }\r\n }\r\n\r\n // --------------------------------------------------\r\n // list and table\r\n if (type === \"list\" || type === \"table\") {\r\n const items = this._getNumberCount(element, type);\r\n\r\n for (let i = 0; i < items.length; i++) {\r\n const typeRemove =\r\n type === \"list\" ? i >= limit && i < items.length - 1 : i >= limit;\r\n\r\n if (ariaExpanded === \"false\") {\r\n addRemoveClass(items[i]);\r\n } else if (typeRemove) {\r\n addRemoveClass(items[i], true);\r\n }\r\n }\r\n }\r\n\r\n // set aria-expanded\r\n if (type) {\r\n this._setExpand({ ...object, target });\r\n }\r\n };\r\n\r\n _getNumberCount = (element, type) => {\r\n return type === \"list\" ? [].slice.call(element.children) : element.rows;\r\n };\r\n\r\n /**\r\n * Add button\r\n *\r\n * @param {Object} object\r\n */\r\n _addBtn = (object) => {\r\n const { type, element, more, typeElement } = object;\r\n\r\n if (!more) return;\r\n\r\n if (type === \"table\") {\r\n element.insertAdjacentElement(\"afterend\", this._createBtn(object));\r\n } else {\r\n const el = createElement(typeElement);\r\n el.classList.add(\"show-more-wrapper\");\r\n el.appendChild(this._createBtn(object));\r\n element.appendChild(el);\r\n }\r\n };\r\n\r\n /**\r\n * Set aria-expanded\r\n *\r\n * @param {Object} object\r\n */\r\n _setExpand = (object) => {\r\n const { element, type, less, more, number, target } = object;\r\n\r\n const typeAria = this._checkExp ? less : more;\r\n const aria = this._checkExp ? \"expand\" : \"collapse\";\r\n const ariaText = type === \"table\" ? type : `the ${type}`;\r\n const lastChild = element.lastElementChild;\r\n\r\n setAttributes(element, { \"aria-expanded\": this._checkExp });\r\n setAttributes(target, {\r\n \"aria-expanded\": this._checkExp,\r\n \"aria-label\": `${aria} ${ariaText}`,\r\n });\r\n\r\n // callback function on more/less\r\n this._onMoreLess(aria, object);\r\n\r\n if (typeAria) {\r\n target.innerHTML = number\r\n ? typeAria + getNumber(element, type)\r\n : typeAria;\r\n } else if (type === \"table\") {\r\n target.parentNode.removeChild(target);\r\n } else if (type === \"list\") {\r\n lastChild.parentNode.removeChild(lastChild);\r\n }\r\n };\r\n}\r\n"],"names":["getNumber","type","rows","children","elementType","numbersElementHidden","slice","call","filter","el","className","length","htmlSubstr","originalText","count","div","createElement","insertAdjacentHTML","walk","track","len","data","substringData","fn","node","firstChild","nodeType","childNodes","nextSibling","innerHTML","addRemoveClass","element","classList","setAttributes","object","key","setAttribute","document","defaultRegex","newLine","match","replace","space","br","html","defaultOptions","typeElement","more","less","number","nobutton","after","btnClass","btnClassAppend","ShowMore","constructor","onMoreLess","regex","config","_initial","ellipsis","limit","_object","limitCounts","ellips","console","log","trim","elementText","textContent","orgTexReg","_regex","truncatedText","concat","_clickEvent","_addBtn","items","_getNumberCount","i","nextElementSibling","addEventListener","_handleEvent","bind","_createBtn","typeAria","_checkExp","label","expanded","button","tabindex","target","checkContainsClass","contains","ariaExpanded","getAttribute","add","insertAdjacentElement","appendChild","typeRemove","_setExpand","aria","ariaText","lastChild","lastElementChild","_onMoreLess","parentNode","removeChild","elements","querySelectorAll","map","item","index","configData","JSON","parse","configGlobal","configDataAndGlobal","classArray"],"mappings":"AAOA,MAAMA,SAAS,GAAG,OAAqBC,IAArB,KAA8B;AAAA,MAA7B;AAAEC,IAAAA,IAAF;AAAQC,IAAAA;AAAR,GAA6B;AAC9C,QAAMC,WAAW,GAAGH,IAAI,KAAK,OAAT,GAAmBC,IAAnB,GAA0BC,QAA9C;AAEA,QAAME,oBAAoB,GAAG,GAAGC,KAAH,CAC1BC,IAD0B,CACrBH,WADqB,EAE1BI,MAF0B,CAElBC,EAAD,IAAQA,EAAE,C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mB;AAAtB,KAAd;AAEAkD,IAAAA,QAAQ,CAACE,GAAT,CAAa,CAACC,IAAD,EAAOC,KAAP,KAAiB;AAC5B,YAAMC,UAAU,GAAGC,IAAI,CAACC,KAAL,CAAWJ,IAAI,CAAChB,YAAL,CAAkB,aAAlB,CAAX,CAAnB;AACA,YAAMqB,YAAY,GAAGzD,MAArB;AAEA,YAAM0D,mBAAmB,GAAG,EAAE,GAAGD,YAAL;AAAmB,WAAGH;AAAtB,OAA5B;AAEA,WAAKlD,OAAL,GAAe;AACbiD,QAAAA,KADa;AAEbM,QAAAA,UAAU,EAAEP,IAAI,CAAC9E,SAFJ;AAGb,WAAGa,cAHU;AAIb,WAAGuE,mBAJU;AAKbtE,QAAAA,WAAW,EAAEsE,mBAAmB,CAACrF,OAApB,IAA+B,MAL/B;AAMbA,QAAAA,OAAO,EAAE+E;AANI,OAAf;AASA,WAAKnD,QAAL;AACD,KAhBD;AAiBD;AAlC2B;;;;"} \ No newline at end of file diff --git a/dist/js/showMore.esm.min.js b/dist/js/showMore.esm.min.js index e3d0a00..e1d8278 100644 --- a/dist/js/showMore.esm.min.js +++ b/dist/js/showMore.esm.min.js @@ -1 +1 @@ -function e(e,t){let{rows:n,children:s}=e;const i="table"===t?n:s,l=[].slice.call(i).filter(e=>"hidden"===e.className).length;return 0!==l?" "+l:""}function t(e,t){void 0===t&&(t=!1),e.classList[t?"add":"remove"]("hidden")}const n={newLine:{match:/(\r\n|\n|\r)/gm,replace:""},space:{match:/\s\s+/gm,replace:" "},br:{match://gim,replace:""},html:{match:/(<((?!b|\/b|!strong|\/strong)[^>]+)>)/gi,replace:""}};class s{constructor(e,t){let{onMoreLess:s=(()=>{}),regex:i={},config:l}=void 0===t?{}:t;const a=document.querySelectorAll(e);this.onMoreLess=s,this.regex=Object.assign(n,i);for(let e=0;er&&(e=function(e,t){let n=document.createElement("div");return n.innerHTML=e,function e(t,n){let s=t.firstChild;do{3===s.nodeType?n(s):1===s.nodeType&&s.childNodes&&s.childNodes[0]&&e(s,n)}while(s=s.nextSibling)}(n,(function(e){if(t>0){let n=e.data.length;(t-=n)<=0&&(e.data=e.substringData(0,e.data.length+t))}else e.data=""})),n.innerHTML}(i,l).concat(c),n.innerHTML=e,this.addBtn(this.object),this.clickEvent(n,{...this.object,originalText:t,truncatedText:e}))}if("list"===a||"table"===a){const e="list"===a?[].slice.call(n.children):n.rows;if(e.length>r){for(let n=l;n=r&&n=r;"false"===b?t(e[n]):s&&t(e[n],!0)}}"table"!==a&&"list"!==a&&"text"!==a||this.setExpand({...e,target:i})}addBtn(e){const{type:t,element:n,more:s,typeElement:i}=e;if(s)if("table"===t)n.insertAdjacentElement("afterend",this.createBtn(e));else{const t=document.createElement(i);t.appendChild(this.createBtn(e)),n.appendChild(t)}}setExpand(t){const{element:n,type:s,less:i,more:l,number:a,target:r}=t,c=this.checkExp,o=c?i:l,d=c?"expand":"collapse",h="table"===s?s:"the "+s,p=n.lastElementChild;n.setAttribute("aria-expanded",c),r.setAttribute("aria-expanded",c),r.setAttribute("aria-label",d+" "+h),this.onMoreLess(d,t),o?r.innerHTML=a?o+e(n,s):o:"table"===s?r.parentNode.removeChild(r):"list"===s&&p.parentNode.removeChild(p)}}export{s as default}; +const t=(t,e)=>{let{rows:s,children:n}=t;const i="table"===e?s:n,l=[].slice.call(i).filter(t=>"hidden"===t.className).length;return 0!==l?" "+l:""},e=function(t,e){return void 0===e&&(e=!1),t.classList[e?"add":"remove"]("hidden")},s=(t,e)=>{for(let s in e)t.setAttribute(s,e[s])},n=t=>document.createElement(t),i={newLine:{match:/(\r\n|\n|\r)/gm,replace:""},space:{match:/\s\s+/gm,replace:" "},br:{match://gim,replace:""},html:{match:/(<((?!b|\/b|!strong|\/strong)[^>]+)>)/gi,replace:""}},l={typeElement:"span",more:!1,less:!1,number:!1,nobutton:!1,after:0,btnClass:"show-more-btn",btnClassAppend:null};class r{constructor(r,a){let{onMoreLess:o=(()=>{}),regex:h={},config:c}=void 0===a?{}:a;this.t=()=>{const{element:t,after:i,ellipsis:l,nobutton:r,limit:a,type:o}=this.s;s(t,{"aria-expanded":"false"});const h=a+i,c=!1===l?"":"...";if("text"===o){const e=t.innerHTML.trim();if(t.textContent.trim().length>h){let s=e;for(let t in this.i){const{match:e,replace:n}=this.i[t];t&&e&&(s=s.replace(e,n))}const i=((t,e)=>{let s=n("div");return s.insertAdjacentHTML("afterbegin",t),function t(e,s){let n=e.firstChild;do{3===n.nodeType?s(n):1===n.nodeType&&n.childNodes&&n.childNodes[0]&&t(n,s)}while(n=n.nextSibling)}(s,(function(t){if(e>0){let s=t.data.length;(e-=s)<=0&&(t.data=t.substringData(0,t.data.length+e))}else t.data=""})),s.innerHTML})(s,a-1).concat(c);if(t.textContent="",t.insertAdjacentHTML("beforeend",i),this.l(t,{...this.s,originalText:e,truncatedText:i}),r)return;this.o(this.s)}}if("list"===o||"table"===o){const s=this.h(t,o);if(s.length>h){for(let t=a;tt.addEventListener("click",this.p.bind(this,e)),this.m=e=>{let{element:i,number:l,less:r,more:a,type:o,btnClass:h,btnClassAppend:c}=e;const d=this.u?r||"":a||"",p=this.u?"collapse":"expand",f=!!this.u,m=n("button");return m.className=null==c?h:h+" "+c,s(m,{"aria-expanded":f,"aria-label":p,tabindex:0}),m.insertAdjacentHTML("beforeend",l?d+t(i,o):d),m},this.p=(t,s)=>{let{target:i}=s;const{element:l,type:r,limit:a,less:o,typeElement:h,originalText:c,truncatedText:d,btnClass:p}=t,f=i.classList.contains(p);if(!f)return;const m=l.getAttribute("aria-expanded");if(this.u="false"===m,"text"===r&&f&&(l.textContent="",l.insertAdjacentHTML("beforeend",this.u?c:d),o)){const e=n(h);e.classList.add("show-more-wrapper"),e.insertAdjacentElement("beforeend",this.m(t)),l.appendChild(e)}if("list"===r||"table"===r){const t=this.h(l,r);for(let s=0;s=a&&s=a;"false"===m?e(t[s]):n&&e(t[s],!0)}}r&&this.g({...t,target:i})},this.h=(t,e)=>"list"===e?[].slice.call(t.children):t.rows,this.o=t=>{const{type:e,element:s,more:i,typeElement:l}=t;if(i)if("table"===e)s.insertAdjacentElement("afterend",this.m(t));else{const e=n(l);e.classList.add("show-more-wrapper"),e.appendChild(this.m(t)),s.appendChild(e)}},this.g=e=>{const{element:n,type:i,less:l,more:r,number:a,target:o}=e,h=this.u?l:r,c=this.u?"expand":"collapse",d="table"===i?i:"the "+i,p=n.lastElementChild;s(n,{"aria-expanded":this.u}),s(o,{"aria-expanded":this.u,"aria-label":c+" "+d}),this.C(c,e),h?o.innerHTML=a?h+t(n,i):h:"table"===i?o.parentNode.removeChild(o):"list"===i&&p.parentNode.removeChild(p)};const d=[].slice.call(document.querySelectorAll(r));this.C=o,this.i={...i,...h},d.map((t,e)=>{const s=JSON.parse(t.getAttribute("data-config")),n={...c,...s};this.s={index:e,classArray:t.classList,...l,...n,typeElement:n.element||"span",element:t},this.t()})}}export{r as default}; diff --git a/dist/js/showMore.ie.min.js b/dist/js/showMore.ie.min.js index 2b3bcbf..ae96879 100644 --- a/dist/js/showMore.ie.min.js +++ b/dist/js/showMore.ie.min.js @@ -1 +1 @@ -var ShowMore=function(){"use strict";function e(e,t){var n=e.rows,i=e.children,a="table"===t?n:i,r=[].slice.call(a).filter((function(e){return"hidden"===e.className})).length;return 0!==r?" "+r:""}function t(e,t){void 0===t&&(t=!1),e.classList[t?"add":"remove"]("hidden")}var n={newLine:{match:/(\r\n|\n|\r)/gm,replace:""},space:{match:/\s\s+/gm,replace:" "},br:{match://gim,replace:""},html:{match:/(<((?!b|\/b|!strong|\/strong)[^>]+)>)/gi,replace:""}};return function(){function i(e,t){var i=void 0===t?{}:t,a=i.onMoreLess,r=void 0===a?function(){}:a,s=i.regex,l=void 0===s?{}:s,c=i.config,d=document.querySelectorAll(e);this.onMoreLess=r,this.regex=Object.assign(n,l);for(var o=0;ol&&(d=function(e,t){var n=document.createElement("div");return n.innerHTML=e,function e(t,n){var i=t.firstChild;do{3===i.nodeType?n(i):1===i.nodeType&&i.childNodes&&i.childNodes[0]&&e(i,n)}while(i=i.nextSibling)}(n,(function(e){if(t>0){var n=e.data.length;(t-=n)<=0&&(e.data=e.substringData(0,e.data.length+t))}else e.data=""})),n.innerHTML}(p,r).concat(c),n.innerHTML=d,this.addBtn(this.object),this.clickEvent(n,Object.assign({},this.object,{originalText:o,truncatedText:d})))}if("list"===s||"table"===s){var v="list"===s?[].slice.call(n.children):n.rows;if(v.length>l){for(var g=r;g=l&&v=l;"false"===b?t(f[v]):g&&t(f[v],!0)}"table"!==s&&"list"!==s&&"text"!==s||this.setExpand(Object.assign({},e,{target:a}))}},a.addBtn=function(e){var t=e.type,n=e.element,i=e.more,a=e.typeElement;if(i)if("table"===t)n.insertAdjacentElement("afterend",this.createBtn(e));else{var r=document.createElement(a);r.appendChild(this.createBtn(e)),n.appendChild(r)}},a.setExpand=function(t){var n=t.element,i=t.type,a=t.less,r=t.more,s=t.number,l=t.target,c=this.checkExp,d=c?a:r,o=c?"expand":"collapse",h="table"===i?i:"the "+i,p=n.lastElementChild;n.setAttribute("aria-expanded",c),l.setAttribute("aria-expanded",c),l.setAttribute("aria-label",o+" "+h),this.onMoreLess(o,t),d?l.innerHTML=s?d+e(n,i):d:"table"===i?l.parentNode.removeChild(l):"list"===i&&p.parentNode.removeChild(p)},i}()}(); +var ShowMore=function(){"use strict";var e=function(e,t){var n=e.rows,r=e.children,a="table"===t?n:r,i=[].slice.call(a).filter((function(e){return"hidden"===e.className})).length;return 0!==i?" "+i:""},t=function(e,t){return void 0===t&&(t=!1),e.classList[t?"add":"remove"]("hidden")},n=function(e,t){for(var n in t)e.setAttribute(n,t[n])},r=function(e){return document.createElement(e)},a={newLine:{match:/(\r\n|\n|\r)/gm,replace:""},space:{match:/\s\s+/gm,replace:" "},br:{match://gim,replace:""},html:{match:/(<((?!b|\/b|!strong|\/strong)[^>]+)>)/gi,replace:""}},i={typeElement:"span",more:!1,less:!1,number:!1,nobutton:!1,after:0,btnClass:"show-more-btn",btnClassAppend:null};return function(o,f){var l=this,s=void 0===f?{}:f,c=s.onMoreLess,u=void 0===c?function(){}:c,d=s.regex,v=void 0===d?{}:d,b=s.config;this.t=function(){var e=l.i,a=e.element,i=e.after,o=e.ellipsis,f=e.nobutton,s=e.limit,c=e.type;n(a,{"aria-expanded":"false"});var u=s+i,d=!1===o?"":"...";if("text"===c){var v=a.innerHTML.trim();if(a.textContent.trim().length>u){var b=v;for(var p in l.o){var h=l.o[p],m=h.match,x=h.replace;p&&m&&(b=b.replace(m,x))}var g=function(e,t){var n=r("div");return n.insertAdjacentHTML("afterbegin",e),function e(t,n){var r=t.firstChild;do{3===r.nodeType?n(r):1===r.nodeType&&r.childNodes&&r.childNodes[0]&&e(r,n)}while(r=r.nextSibling)}(n,(function(e){if(t>0){var n=e.data.length;(t-=n)<=0&&(e.data=e.substringData(0,e.data.length+t))}else e.data=""})),n.innerHTML}(b,s-1).concat(d);if(a.textContent="",a.insertAdjacentHTML("beforeend",g),l.l(a,Object.assign({},l.i,{originalText:v,truncatedText:g})),f)return;l.s(l.i)}}if("list"===c||"table"===c){var w=l.u(a,c);if(w.length>u){for(var O=s;O=f&&x=f;"false"===p?t(m[x]):g&&t(m[x],!0)}o&&l.m(Object.assign({},e,{target:a}))}},this.u=function(e,t){return"list"===t?[].slice.call(e.children):e.rows},this.s=function(e){var t=e.type,n=e.element,a=e.more,i=e.typeElement;if(a)if("table"===t)n.insertAdjacentElement("afterend",l.p(e));else{var o=r(i);o.classList.add("show-more-wrapper"),o.appendChild(l.p(e)),n.appendChild(o)}},this.m=function(t){var r=t.element,a=t.type,i=t.less,o=t.more,f=t.number,s=t.target,c=l.h?i:o,u=l.h?"expand":"collapse",d="table"===a?a:"the "+a,v=r.lastElementChild;n(r,{"aria-expanded":l.h}),n(s,{"aria-expanded":l.h,"aria-label":u+" "+d}),l.g(u,t),c?s.innerHTML=f?c+e(r,a):c:"table"===a?s.parentNode.removeChild(s):"list"===a&&v.parentNode.removeChild(v)};var p=[].slice.call(document.querySelectorAll(o));this.g=u,this.o=Object.assign({},a,v),p.map((function(e,t){var n=JSON.parse(e.getAttribute("data-config")),r=b,a=Object.assign({},r,n);l.i=Object.assign({index:t,classArray:e.classList},i,a,{typeElement:a.element||"span",element:e}),l.t()}))}}(); diff --git a/dist/js/showMore.js b/dist/js/showMore.js index f20d305..dfe1b00 100644 --- a/dist/js/showMore.js +++ b/dist/js/showMore.js @@ -1,18 +1,18 @@ var ShowMore = (function () { 'use strict'; - function getNumber(_ref, type) { + const getNumber = (_ref, type) => { let { rows, children } = _ref; - const elementType = type === 'table' ? rows : children; - const numbersElementHidden = [].slice.call(elementType).filter(el => el.className === 'hidden').length; - return numbersElementHidden !== 0 ? " " + numbersElementHidden : ''; - } - function htmlSubstr(originalText, count) { - let div = document.createElement('div'); - div.innerHTML = originalText; + const elementType = type === "table" ? rows : children; + const numbersElementHidden = [].slice.call(elementType).filter(el => el.className === "hidden").length; + return numbersElementHidden !== 0 ? " " + numbersElementHidden : ""; + }; + const htmlSubstr = (originalText, count) => { + let div = createElement("div"); + div.insertAdjacentHTML("afterbegin", originalText); walk(div, track); function track(el) { if (count > 0) { @@ -22,7 +22,7 @@ var ShowMore = (function () { el.data = el.substringData(0, el.data.length + count); } } else { - el.data = ''; + el.data = ""; } } function walk(el, fn) { @@ -36,33 +36,50 @@ var ShowMore = (function () { } while (node = node.nextSibling); } return div.innerHTML; - } - function addRemoveClass(element, type) { + }; + const addRemoveClass = function (element, type) { if (type === void 0) { type = false; } - element.classList[type ? 'add' : 'remove']('hidden'); - } + return element.classList[type ? "add" : "remove"]("hidden"); + }; + const setAttributes = (el, object) => { + for (let key in object) { + el.setAttribute(key, object[key]); + } + }; + const createElement = type => document.createElement(type); const defaultRegex = { newLine: { match: /(\r\n|\n|\r)/gm, - replace: '' + replace: "" }, space: { match: /\s\s+/gm, - replace: ' ' + replace: " " }, br: { match: //gim, - replace: '' + replace: "" }, html: { match: /(<((?!b|\/b|!strong|\/strong)[^>]+)>)/gi, - replace: '' + replace: "" } }; + const defaultOptions = { + typeElement: "span", + more: false, + less: false, + number: false, + nobutton: false, + after: 0, + btnClass: "show-more-btn", + btnClassAppend: null + }; + class ShowMore { constructor(className, _temp) { let { @@ -70,194 +87,195 @@ var ShowMore = (function () { regex = {}, config } = _temp === void 0 ? {} : _temp; - const elements = document.querySelectorAll(className); - this.onMoreLess = onMoreLess; - this.regex = Object.assign(defaultRegex, regex); - for (let i = 0; i < elements.length; i++) { + this._initial = () => { const { - type, - limit, element, after, - more, - less, - number, ellipsis, + nobutton, + limit, + type + } = this._object; + setAttributes(element, { + "aria-expanded": "false" + }); + const limitCounts = limit + after; + const ellips = ellipsis === false ? "" : "..."; + console.log(nobutton); + if (type === "text") { + const originalText = element.innerHTML.trim(); + const elementText = element.textContent.trim(); + if (elementText.length > limitCounts) { + let orgTexReg = originalText; + for (let key in this._regex) { + const { + match, + replace + } = this._regex[key]; + if (key && match) orgTexReg = orgTexReg.replace(match, replace); + } + const truncatedText = htmlSubstr(orgTexReg, limit - 1).concat(ellips); + element.textContent = ""; + element.insertAdjacentHTML("beforeend", truncatedText); + this._clickEvent(element, { ...this._object, + originalText, + truncatedText + }); + if (nobutton) return; + this._addBtn(this._object); + } + } + if (type === "list" || type === "table") { + const items = this._getNumberCount(element, type); + if (items.length > limitCounts) { + for (let i = limit; i < items.length; i++) { + addRemoveClass(items[i], true); + } + this._clickEvent(type === "list" ? element : element.nextElementSibling, this._object); + if (nobutton) return; + this._addBtn(this._object); + } + } + }; + this._clickEvent = (element, object) => element.addEventListener("click", this._handleEvent.bind(this, object)); + this._createBtn = _ref => { + let { + element, + number, + less, + more, + type, btnClass, btnClassAppend - } = JSON.parse(elements[i].getAttribute('data-config')) || config; - this.object = { - index: i, - element: elements[i], + } = _ref; + const typeAria = this._checkExp ? less || "" : more || ""; + const label = this._checkExp ? "collapse" : "expand"; + const expanded = this._checkExp ? true : false; + const button = createElement("button"); + button.className = btnClassAppend == null ? btnClass : btnClass + " " + btnClassAppend; + setAttributes(button, { + "aria-expanded": expanded, + "aria-label": label, + tabindex: 0 + }); + button.insertAdjacentHTML("beforeend", number ? typeAria + getNumber(element, type) : typeAria); + return button; + }; + this._handleEvent = (object, _ref2) => { + let { + target + } = _ref2; + const { + element, type, limit, - classArray: elements[i].classList, - ellipsis, - typeElement: element || 'span', - more: more || false, - less: less || false, - number: number || false, - after: after || 0, - btnClass: btnClass || 'show-more-btn', - btnClassAppend: btnClassAppend || null - }; - this.initial(this.object); - } - } - initial(_ref) { - let { - element, - after, - ellipsis, - limit, - type - } = _ref; - element.setAttribute('aria-expanded', 'false'); - const limitCounts = limit + after; - const ellips = ellipsis === false ? '' : '...'; - if (type === 'text') { - let truncatedText = ''; - const originalText = element.innerHTML.trim(); - let elementText = element.textContent.trim(); - let orgTexReg = originalText; - for (let key in this.regex) { - const { - match, - replace - } = this.regex[key]; - if (key && match) orgTexReg = orgTexReg.replace(match, replace); - } - if (elementText.length > limitCounts) { - truncatedText = htmlSubstr(orgTexReg, limit).concat(ellips); - element.innerHTML = truncatedText; - this.addBtn(this.object); - this.clickEvent(element, { ...this.object, - originalText, - truncatedText - }); + less, + typeElement, + originalText, + truncatedText, + btnClass + } = object; + const checkContainsClass = target.classList.contains(btnClass); + if (!checkContainsClass) return; + const ariaExpanded = element.getAttribute("aria-expanded"); + this._checkExp = ariaExpanded === "false"; + if (type === "text" && checkContainsClass) { + element.textContent = ""; + element.insertAdjacentHTML("beforeend", this._checkExp ? originalText : truncatedText); + if (less) { + const el = createElement(typeElement); + el.classList.add("show-more-wrapper"); + el.insertAdjacentElement("beforeend", this._createBtn(object)); + element.appendChild(el); + } } - } - if (type === 'list' || type === 'table') { - const items = type === 'list' ? [].slice.call(element.children) : element.rows; - if (items.length > limitCounts) { - for (let i = limit; i < items.length; i++) { - addRemoveClass(items[i], true); + if (type === "list" || type === "table") { + const items = this._getNumberCount(element, type); + for (let i = 0; i < items.length; i++) { + const typeRemove = type === "list" ? i >= limit && i < items.length - 1 : i >= limit; + if (ariaExpanded === "false") { + addRemoveClass(items[i]); + } else if (typeRemove) { + addRemoveClass(items[i], true); + } } - this.addBtn(this.object); - this.clickEvent(type === 'list' ? element : element.nextElementSibling, this.object); } - } - } - clickEvent(element, object) { - element.addEventListener('click', this.handleEvent.bind(this, object)); - } - createBtn(_ref2) { - let { - element, - number, - less, - more, - type, - btnClass, - btnClassAppend - } = _ref2; - const typeAria = this.checkExp ? less || '' : more || ''; - const label = this.checkExp ? 'collapse' : 'expand'; - const expanded = this.checkExp ? true : false; - const btn = document.createElement('button'); - btn.className = btnClassAppend == null ? btnClass : btnClass + ' ' + btnClassAppend; - btn.setAttribute('aria-expanded', expanded); - btn.setAttribute('aria-label', label); - btn.setAttribute('tabindex', 0); - btn.innerHTML = number ? typeAria + getNumber(element, type) : typeAria; - return btn; - } - handleEvent(object, _ref3) { - let { - currentTarget, - target - } = _ref3; - const { - element, - type, - limit, - less, - typeElement, - originalText, - truncatedText, - btnClass - } = object; - const checkContainsClass = target.classList.contains(btnClass); - if (!checkContainsClass) return; - const ariaExpanded = element.getAttribute('aria-expanded'); - this.checkExp = ariaExpanded === 'false'; - if (type === 'text' && checkContainsClass) { - element.innerHTML = ''; - element.innerHTML = this.checkExp ? originalText : truncatedText; - if (less) { - const el = document.createElement(typeElement); - el.insertAdjacentElement('beforeend', this.createBtn(object)); - element.appendChild(el); + if (type) { + this._setExpand({ ...object, + target + }); } - } - if (type === 'list' || type === 'table') { - const items = type === 'list' ? [].slice.call(currentTarget.children) : element.rows; - for (let i = 0; i < items.length; i++) { - const typeRemove = type === 'list' ? i >= limit && i < items.length - 1 : i >= limit; - if (ariaExpanded === 'false') { - addRemoveClass(items[i]); - } else if (typeRemove) { - addRemoveClass(items[i], true); - } + }; + this._getNumberCount = (element, type) => { + return type === "list" ? [].slice.call(element.children) : element.rows; + }; + this._addBtn = object => { + const { + type, + element, + more, + typeElement + } = object; + if (!more) return; + if (type === "table") { + element.insertAdjacentElement("afterend", this._createBtn(object)); + } else { + const el = createElement(typeElement); + el.classList.add("show-more-wrapper"); + el.appendChild(this._createBtn(object)); + element.appendChild(el); } - } - if (type === 'table' || type === 'list' || type === 'text') { - this.setExpand({ ...object, + }; + this._setExpand = object => { + const { + element, + type, + less, + more, + number, target + } = object; + const typeAria = this._checkExp ? less : more; + const aria = this._checkExp ? "expand" : "collapse"; + const ariaText = type === "table" ? type : "the " + type; + const lastChild = element.lastElementChild; + setAttributes(element, { + "aria-expanded": this._checkExp }); - } - } - addBtn(object) { - const { - type, - element, - more, - typeElement - } = object; - if (!more) return; - if (type === 'table') { - element.insertAdjacentElement('afterend', this.createBtn(object)); - } else { - const el = document.createElement(typeElement); - el.appendChild(this.createBtn(object)); - element.appendChild(el); - } - } - setExpand(object) { - const { - element, - type, - less, - more, - number, - target - } = object; - const check = this.checkExp; - const typeAria = check ? less : more; - const aria = check ? 'expand' : 'collapse'; - const ariaText = type === 'table' ? type : "the " + type; - const lastChild = element.lastElementChild; - element.setAttribute('aria-expanded', check); - target.setAttribute('aria-expanded', check); - target.setAttribute('aria-label', aria + " " + ariaText); - this.onMoreLess(aria, object); - if (typeAria) { - target.innerHTML = number ? typeAria + getNumber(element, type) : typeAria; - } else if (type === 'table') { - target.parentNode.removeChild(target); - } else if (type === 'list') { - lastChild.parentNode.removeChild(lastChild); - } + setAttributes(target, { + "aria-expanded": this._checkExp, + "aria-label": aria + " " + ariaText + }); + this._onMoreLess(aria, object); + if (typeAria) { + target.innerHTML = number ? typeAria + getNumber(element, type) : typeAria; + } else if (type === "table") { + target.parentNode.removeChild(target); + } else if (type === "list") { + lastChild.parentNode.removeChild(lastChild); + } + }; + const elements = [].slice.call(document.querySelectorAll(className)); + this._onMoreLess = onMoreLess; + this._regex = { ...defaultRegex, + ...regex + }; + elements.map((item, index) => { + const configData = JSON.parse(item.getAttribute("data-config")); + const configGlobal = config; + const configDataAndGlobal = { ...configGlobal, + ...configData + }; + this._object = { + index, + classArray: item.classList, + ...defaultOptions, + ...configDataAndGlobal, + typeElement: configDataAndGlobal.element || "span", + element: item + }; + this._initial(); + }); } } diff --git a/dist/js/showMore.js.map b/dist/js/showMore.js.map index b740692..30a169a 100644 --- a/dist/js/showMore.js.map +++ b/dist/js/showMore.js.map @@ -1 +1 @@ -{"version":3,"file":"showMore.js","sources":["../../sources/utils/function.js","../../sources/utils/regex.js","../../sources/index.js"],"sourcesContent":["/**\r\n * Get hidden element\r\n *\r\n * @param {object} object\r\n * @param {String} type - type of element table | div\r\n * @returns\r\n */\r\nfunction getNumber({ rows, children }, type) {\r\n const elementType = type === 'table' ? rows : children;\r\n\r\n const numbersElementHidden = [].slice\r\n .call(elementType)\r\n .filter((el) => el.className === 'hidden').length;\r\n return numbersElementHidden !== 0 ? ` ${numbersElementHidden}` : '';\r\n}\r\n\r\n// https://stackoverflow.com/questions/6003271/substring-text-with-html-tags-in-javascript\r\n/**\r\n * Substring text width html tags\r\n *\r\n * @param {String} originalText - text with html tags\r\n * @param {String} count - limit of characters\r\n * @returns\r\n */\r\nfunction htmlSubstr(originalText, count) {\r\n let div = document.createElement('div');\r\n div.innerHTML = originalText;\r\n\r\n walk(div, track);\r\n\r\n function track(el) {\r\n if (count > 0) {\r\n let len = el.data.length;\r\n count -= len;\r\n if (count <= 0) {\r\n el.data = el.substringData(0, el.data.length + count);\r\n }\r\n } else {\r\n el.data = '';\r\n }\r\n }\r\n\r\n function walk(el, fn) {\r\n let node = el.firstChild;\r\n do {\r\n if (node.nodeType === 3) {\r\n fn(node);\r\n } else if (node.nodeType === 1 && node.childNodes && node.childNodes[0]) {\r\n walk(node, fn);\r\n }\r\n } while ((node = node.nextSibling));\r\n }\r\n return div.innerHTML;\r\n}\r\n\r\n/**\r\n * Add/remove class 'hidden' to element\r\n *\r\n * @param {HTMLElement} element\r\n * @param {String} type - type of element add or remove\r\n */\r\nfunction addRemoveClass(element, type = false) {\r\n element.classList[type ? 'add' : 'remove']('hidden');\r\n}\r\n\r\nexport { addRemoveClass, getNumber, htmlSubstr };\r\n","/**\r\n * Default regexes for validation\r\n */\r\nconst defaultRegex = {\r\n newLine: {\r\n match: /(\\r\\n|\\n|\\r)/gm,\r\n replace: '',\r\n },\r\n space: {\r\n match: /\\s\\s+/gm,\r\n replace: ' ',\r\n },\r\n br: {\r\n match: //gim,\r\n replace: '',\r\n },\r\n html: {\r\n match: /(<((?!b|\\/b|!strong|\\/strong)[^>]+)>)/gi,\r\n replace: '',\r\n },\r\n};\r\n\r\nexport default defaultRegex;\r\n","import { addRemoveClass, getNumber, htmlSubstr } from './utils/function';\r\nimport defaultRegex from './utils/regex';\r\n\r\n/**\r\n * @class ShowMore\r\n */\r\nexport default class ShowMore {\r\n /**\r\n * Constructor\r\n *\r\n * @param {HTMLElement} className\r\n * @param {Object} object\r\n */\r\n constructor(className, { onMoreLess = () => {}, regex = {}, config } = {}) {\r\n // all html elements\r\n const elements = document.querySelectorAll(className);\r\n\r\n // colback function\r\n this.onMoreLess = onMoreLess;\r\n\r\n // global regex\r\n this.regex = Object.assign(defaultRegex, regex);\r\n\r\n for (let i = 0; i < elements.length; i++) {\r\n const {\r\n type,\r\n limit,\r\n element,\r\n after,\r\n more,\r\n less,\r\n number,\r\n ellipsis,\r\n btnClass,\r\n btnClassAppend,\r\n } = JSON.parse(elements[i].getAttribute('data-config')) || config;\r\n\r\n // create global object\r\n this.object = {\r\n index: i,\r\n element: elements[i],\r\n type,\r\n limit,\r\n classArray: elements[i].classList,\r\n ellipsis,\r\n typeElement: element || 'span',\r\n more: more || false,\r\n less: less || false,\r\n number: number || false,\r\n after: after || 0,\r\n btnClass: btnClass || 'show-more-btn',\r\n btnClassAppend: btnClassAppend || null,\r\n };\r\n\r\n this.initial(this.object);\r\n }\r\n }\r\n\r\n /**\r\n * Initail function\r\n *\r\n * @param {Object} object\r\n */\r\n initial({ element, after, ellipsis, limit, type }) {\r\n // set default aria-expande to false\r\n element.setAttribute('aria-expanded', 'false');\r\n\r\n const limitCounts = limit + after;\r\n const ellips = ellipsis === false ? '' : '...';\r\n\r\n // text\r\n if (type === 'text') {\r\n let truncatedText = '';\r\n const originalText = element.innerHTML.trim();\r\n let elementText = element.textContent.trim();\r\n\r\n let orgTexReg = originalText;\r\n for (let key in this.regex) {\r\n const { match, replace } = this.regex[key];\r\n if (key && match) orgTexReg = orgTexReg.replace(match, replace);\r\n }\r\n\r\n if (elementText.length > limitCounts) {\r\n truncatedText = htmlSubstr(orgTexReg, limit).concat(ellips);\r\n\r\n element.innerHTML = truncatedText;\r\n\r\n this.addBtn(this.object);\r\n\r\n this.clickEvent(element, {\r\n ...this.object,\r\n originalText,\r\n truncatedText,\r\n });\r\n }\r\n }\r\n\r\n // list and table\r\n if (type === 'list' || type === 'table') {\r\n const items =\r\n type === 'list' ? [].slice.call(element.children) : element.rows;\r\n\r\n if (items.length > limitCounts) {\r\n for (let i = limit; i < items.length; i++) {\r\n addRemoveClass(items[i], true);\r\n }\r\n\r\n // add button to the list and table\r\n this.addBtn(this.object);\r\n\r\n // add event click\r\n this.clickEvent(\r\n type === 'list' ? element : element.nextElementSibling,\r\n this.object\r\n );\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Event click\r\n *\r\n * @param {HTMLElement} element\r\n * @param {Object} object\r\n */\r\n clickEvent(element, object) {\r\n element.addEventListener('click', this.handleEvent.bind(this, object));\r\n }\r\n\r\n /**\r\n * Create button\r\n *\r\n * @param {Object} object\r\n * @returns HTMLElement\r\n */\r\n createBtn({ element, number, less, more, type, btnClass, btnClassAppend }) {\r\n const typeAria = this.checkExp ? less || '' : more || '';\r\n const label = this.checkExp ? 'collapse' : 'expand';\r\n const expanded = this.checkExp ? true : false;\r\n\r\n const btn = document.createElement('button');\r\n btn.className =\r\n btnClassAppend == null ? btnClass : btnClass + ' ' + btnClassAppend;\r\n btn.setAttribute('aria-expanded', expanded);\r\n btn.setAttribute('aria-label', label);\r\n btn.setAttribute('tabindex', 0);\r\n btn.innerHTML = number ? typeAria + getNumber(element, type) : typeAria;\r\n return btn;\r\n }\r\n\r\n /**\r\n * Event handler\r\n *\r\n * @param {Object} object\r\n * @param {Event} event\r\n */\r\n handleEvent(object, { currentTarget, target }) {\r\n const {\r\n element,\r\n type,\r\n limit,\r\n less,\r\n typeElement,\r\n originalText,\r\n truncatedText,\r\n btnClass,\r\n } = object;\r\n\r\n // check if the button is clicked\r\n const checkContainsClass = target.classList.contains(btnClass);\r\n\r\n if (!checkContainsClass) return;\r\n\r\n const ariaExpanded = element.getAttribute('aria-expanded');\r\n this.checkExp = ariaExpanded === 'false';\r\n\r\n // --------------------------------------------------\r\n // text\r\n if (type === 'text' && checkContainsClass) {\r\n element.innerHTML = '';\r\n element.innerHTML = this.checkExp ? originalText : truncatedText;\r\n\r\n if (less) {\r\n const el = document.createElement(typeElement);\r\n el.insertAdjacentElement('beforeend', this.createBtn(object));\r\n element.appendChild(el);\r\n }\r\n }\r\n\r\n // --------------------------------------------------\r\n // list and table\r\n if (type === 'list' || type === 'table') {\r\n const items =\r\n type === 'list' ? [].slice.call(currentTarget.children) : element.rows;\r\n\r\n for (let i = 0; i < items.length; i++) {\r\n const typeRemove =\r\n type === 'list' ? i >= limit && i < items.length - 1 : i >= limit;\r\n\r\n if (ariaExpanded === 'false') {\r\n addRemoveClass(items[i]);\r\n } else if (typeRemove) {\r\n addRemoveClass(items[i], true);\r\n }\r\n }\r\n }\r\n\r\n // set aria-expanded\r\n if (type === 'table' || type === 'list' || type === 'text') {\r\n this.setExpand({ ...object, target });\r\n }\r\n }\r\n\r\n /**\r\n * Add button\r\n *\r\n * @param {Object} object\r\n */\r\n addBtn(object) {\r\n const { type, element, more, typeElement } = object;\r\n\r\n if (!more) return;\r\n\r\n if (type === 'table') {\r\n element.insertAdjacentElement('afterend', this.createBtn(object));\r\n } else {\r\n const el = document.createElement(typeElement);\r\n el.appendChild(this.createBtn(object));\r\n element.appendChild(el);\r\n }\r\n }\r\n\r\n /**\r\n * Set aria-expanded\r\n *\r\n * @param {Object} object\r\n */\r\n setExpand(object) {\r\n const { element, type, less, more, number, target } = object;\r\n\r\n const check = this.checkExp;\r\n\r\n const typeAria = check ? less : more;\r\n const aria = check ? 'expand' : 'collapse';\r\n const ariaText = type === 'table' ? type : `the ${type}`;\r\n const lastChild = element.lastElementChild;\r\n\r\n element.setAttribute('aria-expanded', check);\r\n target.setAttribute('aria-expanded', check);\r\n target.setAttribute('aria-label', `${aria} ${ariaText}`);\r\n\r\n // callback function on more/less\r\n this.onMoreLess(aria, object);\r\n\r\n if (typeAria) {\r\n target.innerHTML = number\r\n ? typeAria + getNumber(element, type)\r\n : typeAria;\r\n } else if (type === 'table') {\r\n target.parentNode.removeChild(target);\r\n } else if (type === 'list') {\r\n lastChild.parentNode.removeChild(lastChild);\r\n }\r\n }\r\n}\r\n"],"names":["getNumber","type","rows","children","elementType","numbersElementHidden","slice","call","filter","el","className","length","htmlSubstr","originalText","count","div","document","createElement","innerHTML","walk","track","len","data","substringData","fn","node","firstChild","nodeType","childNodes","nextSibling","addRemoveClass","element","classList","defaultRegex","newLine","match","replace","space","br","html","ShowMore","constructor","onMoreLess","regex","config","elements","querySelectorAll","Object","assign","i","limit","after","more","less","number","ellipsis","btnClass","btnClassAppend","JSON","parse","getAttribute","object","index","classArray","typeElement","initial","setAttribute","limitCounts","ellips","truncatedText","trim","elementText","textContent","orgTexReg","key","concat","addBtn","clickEvent","items","nextElementSibling","addEventListener","handleEvent","bind","createBtn","typeAria","checkExp","label","expanded","btn","currentTarget","target","checkContainsClass","contains","ariaExpanded","insertAdjacentElement","appendChild","typeRemove","setExpand","check","aria","ariaText","lastChild","lastElementChild","parentNode","removeChild"],"mappings":";;;EAOA,SAASA,SAAT,OAAuCC,IAAvC,EAA6C;EAAA,MAA1B;EAAEC,IAAAA,IAAF;EAAQC,IAAAA;EAAR,GAA0B;EAC3C,QAAMC,WAAW,GAAGH,IAAI,KAAK,OAAT,GAAmBC,IAAnB,GAA0BC,QAA9C;EAEA,QAAME,oBAAoB,GAAG,GAAGC,KAAH,CAC1BC,IAD0B,CACrBH,WADqB,EAE1BI,MAF0B,CAElBC,EAAD,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AkBC,WAAlB,CAA8Bd,MAA9B;EACD,KAFM,MAEA,IAAIzF,IAAI,KAAK,MAAb,EAAqB;EAC1BoG,MAAAA,SAAS,CAACE,UAAV,CAAqBC,WAArB,CAAiCH,SAAjC;EACD;EACF;EAjQ2B;;;;;;;;"} \ No newline at end of file +{"version":3,"file":"showMore.js","sources":["../../sources/utils/function.js","../../sources/utils/regex.js","../../sources/utils/defaults.js","../../sources/index.js"],"sourcesContent":["/**\r\n * Get hidden element\r\n *\r\n * @param {object} object\r\n * @param {String} type - type of element table | div\r\n * @returns\r\n */\r\nconst getNumber = ({ rows, children }, type) => {\r\n const elementType = type === \"table\" ? rows : children;\r\n\r\n const numbersElementHidden = [].slice\r\n .call(elementType)\r\n .filter((el) => el.className === \"hidden\").length;\r\n return numbersElementHidden !== 0 ? ` ${numbersElementHidden}` : \"\";\r\n};\r\n\r\n// https://stackoverflow.com/questions/6003271/substring-text-with-html-tags-in-javascript\r\n/**\r\n * Substring text width html tags\r\n *\r\n * @param {String} originalText - text with html tags\r\n * @param {String} count - limit of characters\r\n * @returns\r\n */\r\nconst htmlSubstr = (originalText, count) => {\r\n let div = createElement(\"div\");\r\n div.insertAdjacentHTML(\"afterbegin\", originalText);\r\n\r\n walk(div, track);\r\n\r\n function track(el) {\r\n if (count > 0) {\r\n let len = el.data.length;\r\n count -= len;\r\n if (count <= 0) {\r\n el.data = el.substringData(0, el.data.length + count);\r\n }\r\n } else {\r\n el.data = \"\";\r\n }\r\n }\r\n\r\n function walk(el, fn) {\r\n let node = el.firstChild;\r\n do {\r\n if (node.nodeType === 3) {\r\n fn(node);\r\n } else if (node.nodeType === 1 && node.childNodes && node.childNodes[0]) {\r\n walk(node, fn);\r\n }\r\n } while ((node = node.nextSibling));\r\n }\r\n return div.innerHTML;\r\n};\r\n\r\n/**\r\n * Add/remove class 'hidden' to element\r\n *\r\n * @param {HTMLElement} element\r\n * @param {String} type - type of element add or remove\r\n */\r\nconst addRemoveClass = (element, type = false) => {\r\n return element.classList[type ? \"add\" : \"remove\"](\"hidden\");\r\n};\r\n\r\n/**\r\n * Set attributes to element\r\n *\r\n * @param {HTMLElement} el\r\n * @param {Object} object\r\n */\r\nconst setAttributes = (el, object) => {\r\n for (let key in object) {\r\n el.setAttribute(key, object[key]);\r\n }\r\n};\r\n\r\n/**\r\n * Create element\r\n *\r\n * @param {String} type - type of element\r\n * @returns {HTMLElement}\r\n */\r\nconst createElement = (type) => document.createElement(type);\r\n\r\nexport { addRemoveClass, createElement, getNumber, htmlSubstr, setAttributes };\r\n","/**\r\n * Default regexes for validation\r\n */\r\nconst defaultRegex = {\r\n newLine: {\r\n match: /(\\r\\n|\\n|\\r)/gm,\r\n replace: \"\",\r\n },\r\n space: {\r\n match: /\\s\\s+/gm,\r\n replace: \" \",\r\n },\r\n br: {\r\n match: //gim,\r\n replace: \"\",\r\n },\r\n html: {\r\n match: /(<((?!b|\\/b|!strong|\\/strong)[^>]+)>)/gi,\r\n replace: \"\",\r\n },\r\n};\r\n\r\nexport default defaultRegex;\r\n","const defaultOptions = {\r\n typeElement: \"span\",\r\n more: false,\r\n less: false,\r\n number: false,\r\n nobutton: false,\r\n after: 0,\r\n btnClass: \"show-more-btn\",\r\n btnClassAppend: null,\r\n};\r\n\r\nexport default defaultOptions;\r\n","import {\r\n addRemoveClass,\r\n createElement,\r\n getNumber,\r\n htmlSubstr,\r\n setAttributes,\r\n} from \"./utils/function\";\r\nimport defaultRegex from \"./utils/regex\";\r\nimport defaultOptions from \"./utils/defaults\";\r\n\r\n/**\r\n * @class ShowMore\r\n */\r\nexport default class ShowMore {\r\n /**\r\n * Constructor\r\n *\r\n * @param {HTMLElement} className\r\n * @param {Object} object\r\n */\r\n constructor(className, { onMoreLess = () => {}, regex = {}, config } = {}) {\r\n // all html elements\r\n const elements = [].slice.call(document.querySelectorAll(className));\r\n\r\n // colback function\r\n this._onMoreLess = onMoreLess;\r\n\r\n // global regex\r\n this._regex = { ...defaultRegex, ...regex };\r\n\r\n elements.map((item, index) => {\r\n const configData = JSON.parse(item.getAttribute(\"data-config\"));\r\n const configGlobal = config;\r\n\r\n const configDataAndGlobal = { ...configGlobal, ...configData };\r\n\r\n this._object = {\r\n index,\r\n classArray: item.classList,\r\n ...defaultOptions,\r\n ...configDataAndGlobal,\r\n typeElement: configDataAndGlobal.element || \"span\",\r\n element: item,\r\n };\r\n\r\n this._initial();\r\n });\r\n }\r\n\r\n /**\r\n * Initail function\r\n *\r\n * @param {Object} object\r\n */\r\n _initial = () => {\r\n const { element, after, ellipsis, nobutton, limit, type } = this._object;\r\n // set default aria-expande to false\r\n setAttributes(element, { \"aria-expanded\": \"false\" });\r\n\r\n const limitCounts = limit + after;\r\n const ellips = ellipsis === false ? \"\" : \"...\";\r\n\r\n console.log(nobutton);\r\n\r\n // text\r\n if (type === \"text\") {\r\n const originalText = element.innerHTML.trim();\r\n const elementText = element.textContent.trim();\r\n\r\n if (elementText.length > limitCounts) {\r\n let orgTexReg = originalText;\r\n\r\n for (let key in this._regex) {\r\n const { match, replace } = this._regex[key];\r\n if (key && match) orgTexReg = orgTexReg.replace(match, replace);\r\n }\r\n\r\n const truncatedText = htmlSubstr(orgTexReg, limit - 1).concat(ellips);\r\n\r\n element.textContent = \"\";\r\n element.insertAdjacentHTML(\"beforeend\", truncatedText);\r\n\r\n this._clickEvent(element, {\r\n ...this._object,\r\n originalText,\r\n truncatedText,\r\n });\r\n\r\n if (nobutton) return;\r\n this._addBtn(this._object);\r\n }\r\n }\r\n\r\n // list and table\r\n if (type === \"list\" || type === \"table\") {\r\n const items = this._getNumberCount(element, type);\r\n\r\n if (items.length > limitCounts) {\r\n for (let i = limit; i < items.length; i++) {\r\n addRemoveClass(items[i], true);\r\n }\r\n\r\n // add event click\r\n this._clickEvent(\r\n type === \"list\" ? element : element.nextElementSibling,\r\n this._object\r\n );\r\n\r\n if (nobutton) return;\r\n // add button to the list and table\r\n this._addBtn(this._object);\r\n }\r\n }\r\n };\r\n\r\n /**\r\n * Event click\r\n *\r\n * @param {HTMLElement} element\r\n * @param {Object} object\r\n */\r\n _clickEvent = (element, object) =>\r\n element.addEventListener(\"click\", this._handleEvent.bind(this, object));\r\n\r\n /**\r\n * Create button\r\n *\r\n * @param {Object} object\r\n * @returns HTMLElement\r\n */\r\n _createBtn = ({\r\n element,\r\n number,\r\n less,\r\n more,\r\n type,\r\n btnClass,\r\n btnClassAppend,\r\n }) => {\r\n const typeAria = this._checkExp ? less || \"\" : more || \"\";\r\n const label = this._checkExp ? \"collapse\" : \"expand\";\r\n const expanded = this._checkExp ? true : false;\r\n\r\n const button = createElement(\"button\");\r\n button.className =\r\n btnClassAppend == null ? btnClass : btnClass + \" \" + btnClassAppend;\r\n setAttributes(button, {\r\n \"aria-expanded\": expanded,\r\n \"aria-label\": label,\r\n tabindex: 0,\r\n });\r\n\r\n button.insertAdjacentHTML(\r\n \"beforeend\",\r\n number ? typeAria + getNumber(element, type) : typeAria\r\n );\r\n return button;\r\n };\r\n\r\n /**\r\n * Event handler\r\n *\r\n * @param {Object} object\r\n * @param {Event} event\r\n */\r\n _handleEvent = (object, { target }) => {\r\n const {\r\n element,\r\n type,\r\n limit,\r\n less,\r\n typeElement,\r\n originalText,\r\n truncatedText,\r\n btnClass,\r\n } = object;\r\n\r\n // check if the button is clicked\r\n const checkContainsClass = target.classList.contains(btnClass);\r\n\r\n if (!checkContainsClass) return;\r\n\r\n const ariaExpanded = element.getAttribute(\"aria-expanded\");\r\n this._checkExp = ariaExpanded === \"false\";\r\n\r\n // --------------------------------------------------\r\n // text\r\n if (type === \"text\" && checkContainsClass) {\r\n element.textContent = \"\";\r\n\r\n element.insertAdjacentHTML(\r\n \"beforeend\",\r\n this._checkExp ? originalText : truncatedText\r\n );\r\n\r\n if (less) {\r\n const el = createElement(typeElement);\r\n el.classList.add(\"show-more-wrapper\");\r\n el.insertAdjacentElement(\"beforeend\", this._createBtn(object));\r\n element.appendChild(el);\r\n }\r\n }\r\n\r\n // --------------------------------------------------\r\n // list and table\r\n if (type === \"list\" || type === \"table\") {\r\n const items = this._getNumberCount(element, type);\r\n\r\n for (let i = 0; i < items.length; i++) {\r\n const typeRemove =\r\n type === \"list\" ? i >= limit && i < items.length - 1 : i >= limit;\r\n\r\n if (ariaExpanded === \"false\") {\r\n addRemoveClass(items[i]);\r\n } else if (typeRemove) {\r\n addRemoveClass(items[i], true);\r\n }\r\n }\r\n }\r\n\r\n // set aria-expanded\r\n if (type) {\r\n this._setExpand({ ...object, target });\r\n }\r\n };\r\n\r\n _getNumberCount = (element, type) => {\r\n return type === \"list\" ? [].slice.call(element.children) : element.rows;\r\n };\r\n\r\n /**\r\n * Add button\r\n *\r\n * @param {Object} object\r\n */\r\n _addBtn = (object) => {\r\n const { type, element, more, typeElement } = object;\r\n\r\n if (!more) return;\r\n\r\n if (type === \"table\") {\r\n element.insertAdjacentElement(\"afterend\", this._createBtn(object));\r\n } else {\r\n const el = createElement(typeElement);\r\n el.classList.add(\"show-more-wrapper\");\r\n el.appendChild(this._createBtn(object));\r\n element.appendChild(el);\r\n }\r\n };\r\n\r\n /**\r\n * Set aria-expanded\r\n *\r\n * @param {Object} object\r\n */\r\n _setExpand = (object) => {\r\n const { element, type, less, more, number, target } = object;\r\n\r\n const typeAria = this._checkExp ? less : more;\r\n const aria = this._checkExp ? \"expand\" : \"collapse\";\r\n const ariaText = type === \"table\" ? type : `the ${type}`;\r\n const lastChild = element.lastElementChild;\r\n\r\n setAttributes(element, { \"aria-expanded\": this._checkExp });\r\n setAttributes(target, {\r\n \"aria-expanded\": this._checkExp,\r\n \"aria-label\": `${aria} ${ariaText}`,\r\n });\r\n\r\n // callback function on more/less\r\n this._onMoreLess(aria, object);\r\n\r\n if (typeAria) {\r\n target.innerHTML = number\r\n ? typeAria + getNumber(element, type)\r\n : typeAria;\r\n } else if (type === \"table\") {\r\n target.parentNode.removeChild(target);\r\n } else if (type === \"list\") {\r\n lastChild.parentNode.removeChild(lastChild);\r\n }\r\n };\r\n}\r\n"],"names":["getNumber","type","rows","children","elementType","numbersElementHidden","slice","call","filter","el","className","length","htmlSubstr","originalText","count","div","createElement","insertAdjacentHTML","walk","track","len","data","substringData","fn","node","firstChild","nodeType","childNodes","nextSibling","innerHTML","addRemoveClass","element","classList","setAttributes","object","key","setAttribute","document","defaultRegex","newLine","match","replace","space","br","html","defaultOptions","typeElement","more","less","number","nobutton","after","btnClass","btnClassAppend","ShowMore","constructor","onMoreLess","regex","config","_initial","ellipsis","limit","_object","limitCounts","ellips","console","log","trim","elementText","textContent","orgTexReg","_regex","truncatedText","concat","_clickEvent","_addBtn","items","_getNumberCount","i","nextElementSibling","addEventListener","_handleEvent","bind","_createBtn","typeAria","_checkExp","label","expanded","button","tabindex","target","checkContainsClass","contains","ariaExpanded","getAttribute","add","insertAdjacentElement","appendChild","typeRemove","_setExpand","aria","ariaText","lastChild","lastElementChild","_onMoreLess","parentNode","removeChild","elements","querySelectorAll","map","item","index","configData","JSON","parse","configGlobal","configDataAndGlobal","classArray"],"mappings":";;;EAOA,MAAMA,SAAS,GAAG,OAAqBC,IAArB,KAA8B;EAAA,MAA7B;EAAEC,IAAAA,IAAF;EAAQC,IAAAA;EAAR,GAA6B;EAC9C,QAAMC,WAAW,GAAGH,IAAI,KAAK,OAAT,GAAmBC,IAAnB,GAA0BC,QAA9C;EAEA,QAAME,oBAAoB,GAAG,GAAGC,KAAH,CAC1BC,IAD0B,CACrBH,WADqB,EAE1BI,MAF0B,CAElBC,EAAD,IAAQA,EAAE,CAACC,SAAH,KAAiB,QAFN,EAEgBC,MAF7C;EAGA,SAAON,oBAAoB,KAAK,CAAzB,SAAiCA,oBAAjC,GAA0D,EAAjE;EACD,CAPD;EAiBA,MAAMO,UAAU,GAAG,CAACC,YAAD,EAAeC,KAAf,KAAyB;EAC1C,MAAIC,GAAG,GAAGC,aAAa,CAAC,KAAD,CAAvB;EACAD,EAAAA,GAAG,CAACE,kBAAJ,CAAuB,YAAvB,EAAqCJ,YAArC;EAEAK,EAAAA,IAAI,CAACH,GAAD,EAAM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EACbiD,QAAAA,KADa;EAEbM,QAAAA,UAAU,EAAEP,IAAI,CAAC9E,SAFJ;EAGb,WAAGa,cAHU;EAIb,WAAGuE,mBAJU;EAKbtE,QAAAA,WAAW,EAAEsE,mBAAmB,CAACrF,OAApB,IAA+B,MAL/B;EAMbA,QAAAA,OAAO,EAAE+E;EANI,OAAf;EASA,WAAKnD,QAAL;EACD,KAhBD;EAiBD;EAlC2B;;;;;;;;"} \ No newline at end of file diff --git a/dist/js/showMore.min.js b/dist/js/showMore.min.js index 5471eea..db9ae66 100644 --- a/dist/js/showMore.min.js +++ b/dist/js/showMore.min.js @@ -1 +1 @@ -var ShowMore=function(){"use strict";function e(e,t){let{rows:n,children:s}=e;const i="table"===t?n:s,l=[].slice.call(i).filter(e=>"hidden"===e.className).length;return 0!==l?" "+l:""}function t(e,t){void 0===t&&(t=!1),e.classList[t?"add":"remove"]("hidden")}const n={newLine:{match:/(\r\n|\n|\r)/gm,replace:""},space:{match:/\s\s+/gm,replace:" "},br:{match://gim,replace:""},html:{match:/(<((?!b|\/b|!strong|\/strong)[^>]+)>)/gi,replace:""}};return class{constructor(e,t){let{onMoreLess:s=(()=>{}),regex:i={},config:l}=void 0===t?{}:t;const a=document.querySelectorAll(e);this.onMoreLess=s,this.regex=Object.assign(n,i);for(let e=0;er&&(e=function(e,t){let n=document.createElement("div");return n.innerHTML=e,function e(t,n){let s=t.firstChild;do{3===s.nodeType?n(s):1===s.nodeType&&s.childNodes&&s.childNodes[0]&&e(s,n)}while(s=s.nextSibling)}(n,(function(e){if(t>0){let n=e.data.length;(t-=n)<=0&&(e.data=e.substringData(0,e.data.length+t))}else e.data=""})),n.innerHTML}(i,l).concat(c),n.innerHTML=e,this.addBtn(this.object),this.clickEvent(n,{...this.object,originalText:t,truncatedText:e}))}if("list"===a||"table"===a){const e="list"===a?[].slice.call(n.children):n.rows;if(e.length>r){for(let n=l;n=r&&n=r;"false"===b?t(e[n]):s&&t(e[n],!0)}}"table"!==a&&"list"!==a&&"text"!==a||this.setExpand({...e,target:i})}addBtn(e){const{type:t,element:n,more:s,typeElement:i}=e;if(s)if("table"===t)n.insertAdjacentElement("afterend",this.createBtn(e));else{const t=document.createElement(i);t.appendChild(this.createBtn(e)),n.appendChild(t)}}setExpand(t){const{element:n,type:s,less:i,more:l,number:a,target:r}=t,c=this.checkExp,o=c?i:l,d=c?"expand":"collapse",h="table"===s?s:"the "+s,p=n.lastElementChild;n.setAttribute("aria-expanded",c),r.setAttribute("aria-expanded",c),r.setAttribute("aria-label",d+" "+h),this.onMoreLess(d,t),o?r.innerHTML=a?o+e(n,s):o:"table"===s?r.parentNode.removeChild(r):"list"===s&&p.parentNode.removeChild(p)}}}(); +var ShowMore=function(){"use strict";const t=(t,e)=>{let{rows:s,children:n}=t;const i="table"===e?s:n,l=[].slice.call(i).filter(t=>"hidden"===t.className).length;return 0!==l?" "+l:""},e=function(t,e){return void 0===e&&(e=!1),t.classList[e?"add":"remove"]("hidden")},s=(t,e)=>{for(let s in e)t.setAttribute(s,e[s])},n=t=>document.createElement(t),i={newLine:{match:/(\r\n|\n|\r)/gm,replace:""},space:{match:/\s\s+/gm,replace:" "},br:{match://gim,replace:""},html:{match:/(<((?!b|\/b|!strong|\/strong)[^>]+)>)/gi,replace:""}},l={typeElement:"span",more:!1,less:!1,number:!1,nobutton:!1,after:0,btnClass:"show-more-btn",btnClassAppend:null};return class{constructor(r,a){let{onMoreLess:o=(()=>{}),regex:h={},config:c}=void 0===a?{}:a;this.t=()=>{const{element:t,after:i,ellipsis:l,nobutton:r,limit:a,type:o}=this.s;s(t,{"aria-expanded":"false"});const h=a+i,c=!1===l?"":"...";if(console.log(r),"text"===o){const e=t.innerHTML.trim();if(t.textContent.trim().length>h){let s=e;for(let t in this.i){const{match:e,replace:n}=this.i[t];t&&e&&(s=s.replace(e,n))}const i=((t,e)=>{let s=n("div");return s.insertAdjacentHTML("afterbegin",t),function t(e,s){let n=e.firstChild;do{3===n.nodeType?s(n):1===n.nodeType&&n.childNodes&&n.childNodes[0]&&t(n,s)}while(n=n.nextSibling)}(s,(function(t){if(e>0){let s=t.data.length;(e-=s)<=0&&(t.data=t.substringData(0,t.data.length+e))}else t.data=""})),s.innerHTML})(s,a-1).concat(c);if(t.textContent="",t.insertAdjacentHTML("beforeend",i),this.l(t,{...this.s,originalText:e,truncatedText:i}),r)return;this.o(this.s)}}if("list"===o||"table"===o){const s=this.h(t,o);if(s.length>h){for(let t=a;tt.addEventListener("click",this.p.bind(this,e)),this.m=e=>{let{element:i,number:l,less:r,more:a,type:o,btnClass:h,btnClassAppend:c}=e;const d=this.u?r||"":a||"",p=this.u?"collapse":"expand",f=!!this.u,m=n("button");return m.className=null==c?h:h+" "+c,s(m,{"aria-expanded":f,"aria-label":p,tabindex:0}),m.insertAdjacentHTML("beforeend",l?d+t(i,o):d),m},this.p=(t,s)=>{let{target:i}=s;const{element:l,type:r,limit:a,less:o,typeElement:h,originalText:c,truncatedText:d,btnClass:p}=t,f=i.classList.contains(p);if(!f)return;const m=l.getAttribute("aria-expanded");if(this.u="false"===m,"text"===r&&f&&(l.textContent="",l.insertAdjacentHTML("beforeend",this.u?c:d),o)){const e=n(h);e.classList.add("show-more-wrapper"),e.insertAdjacentElement("beforeend",this.m(t)),l.appendChild(e)}if("list"===r||"table"===r){const t=this.h(l,r);for(let s=0;s=a&&s=a;"false"===m?e(t[s]):n&&e(t[s],!0)}}r&&this.g({...t,target:i})},this.h=(t,e)=>"list"===e?[].slice.call(t.children):t.rows,this.o=t=>{const{type:e,element:s,more:i,typeElement:l}=t;if(i)if("table"===e)s.insertAdjacentElement("afterend",this.m(t));else{const e=n(l);e.classList.add("show-more-wrapper"),e.appendChild(this.m(t)),s.appendChild(e)}},this.g=e=>{const{element:n,type:i,less:l,more:r,number:a,target:o}=e,h=this.u?l:r,c=this.u?"expand":"collapse",d="table"===i?i:"the "+i,p=n.lastElementChild;s(n,{"aria-expanded":this.u}),s(o,{"aria-expanded":this.u,"aria-label":c+" "+d}),this.C(c,e),h?o.innerHTML=a?h+t(n,i):h:"table"===i?o.parentNode.removeChild(o):"list"===i&&p.parentNode.removeChild(p)};const d=[].slice.call(document.querySelectorAll(r));this.C=o,this.i={...i,...h},d.map((t,e)=>{const s=JSON.parse(t.getAttribute("data-config")),n={...c,...s};this.s={index:e,classArray:t.classList,...l,...n,typeElement:n.element||"span",element:t},this.t()})}}}(); diff --git a/dist/js/showMore.umd.js b/dist/js/showMore.umd.js index 409704e..1a2f83b 100644 --- a/dist/js/showMore.umd.js +++ b/dist/js/showMore.umd.js @@ -4,18 +4,18 @@ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.ShowMore = factory()); })(this, (function () { 'use strict'; - function getNumber(_ref, type) { + const getNumber = (_ref, type) => { let { rows, children } = _ref; - const elementType = type === 'table' ? rows : children; - const numbersElementHidden = [].slice.call(elementType).filter(el => el.className === 'hidden').length; - return numbersElementHidden !== 0 ? " " + numbersElementHidden : ''; - } - function htmlSubstr(originalText, count) { - let div = document.createElement('div'); - div.innerHTML = originalText; + const elementType = type === "table" ? rows : children; + const numbersElementHidden = [].slice.call(elementType).filter(el => el.className === "hidden").length; + return numbersElementHidden !== 0 ? " " + numbersElementHidden : ""; + }; + const htmlSubstr = (originalText, count) => { + let div = createElement("div"); + div.insertAdjacentHTML("afterbegin", originalText); walk(div, track); function track(el) { if (count > 0) { @@ -25,7 +25,7 @@ el.data = el.substringData(0, el.data.length + count); } } else { - el.data = ''; + el.data = ""; } } function walk(el, fn) { @@ -39,33 +39,50 @@ } while (node = node.nextSibling); } return div.innerHTML; - } - function addRemoveClass(element, type) { + }; + const addRemoveClass = function (element, type) { if (type === void 0) { type = false; } - element.classList[type ? 'add' : 'remove']('hidden'); - } + return element.classList[type ? "add" : "remove"]("hidden"); + }; + const setAttributes = (el, object) => { + for (let key in object) { + el.setAttribute(key, object[key]); + } + }; + const createElement = type => document.createElement(type); const defaultRegex = { newLine: { match: /(\r\n|\n|\r)/gm, - replace: '' + replace: "" }, space: { match: /\s\s+/gm, - replace: ' ' + replace: " " }, br: { match: //gim, - replace: '' + replace: "" }, html: { match: /(<((?!b|\/b|!strong|\/strong)[^>]+)>)/gi, - replace: '' + replace: "" } }; + const defaultOptions = { + typeElement: "span", + more: false, + less: false, + number: false, + nobutton: false, + after: 0, + btnClass: "show-more-btn", + btnClassAppend: null + }; + class ShowMore { constructor(className, _temp) { let { @@ -73,194 +90,195 @@ regex = {}, config } = _temp === void 0 ? {} : _temp; - const elements = document.querySelectorAll(className); - this.onMoreLess = onMoreLess; - this.regex = Object.assign(defaultRegex, regex); - for (let i = 0; i < elements.length; i++) { + this._initial = () => { const { - type, - limit, element, after, - more, - less, - number, ellipsis, + nobutton, + limit, + type + } = this._object; + setAttributes(element, { + "aria-expanded": "false" + }); + const limitCounts = limit + after; + const ellips = ellipsis === false ? "" : "..."; + console.log(nobutton); + if (type === "text") { + const originalText = element.innerHTML.trim(); + const elementText = element.textContent.trim(); + if (elementText.length > limitCounts) { + let orgTexReg = originalText; + for (let key in this._regex) { + const { + match, + replace + } = this._regex[key]; + if (key && match) orgTexReg = orgTexReg.replace(match, replace); + } + const truncatedText = htmlSubstr(orgTexReg, limit - 1).concat(ellips); + element.textContent = ""; + element.insertAdjacentHTML("beforeend", truncatedText); + this._clickEvent(element, { ...this._object, + originalText, + truncatedText + }); + if (nobutton) return; + this._addBtn(this._object); + } + } + if (type === "list" || type === "table") { + const items = this._getNumberCount(element, type); + if (items.length > limitCounts) { + for (let i = limit; i < items.length; i++) { + addRemoveClass(items[i], true); + } + this._clickEvent(type === "list" ? element : element.nextElementSibling, this._object); + if (nobutton) return; + this._addBtn(this._object); + } + } + }; + this._clickEvent = (element, object) => element.addEventListener("click", this._handleEvent.bind(this, object)); + this._createBtn = _ref => { + let { + element, + number, + less, + more, + type, btnClass, btnClassAppend - } = JSON.parse(elements[i].getAttribute('data-config')) || config; - this.object = { - index: i, - element: elements[i], + } = _ref; + const typeAria = this._checkExp ? less || "" : more || ""; + const label = this._checkExp ? "collapse" : "expand"; + const expanded = this._checkExp ? true : false; + const button = createElement("button"); + button.className = btnClassAppend == null ? btnClass : btnClass + " " + btnClassAppend; + setAttributes(button, { + "aria-expanded": expanded, + "aria-label": label, + tabindex: 0 + }); + button.insertAdjacentHTML("beforeend", number ? typeAria + getNumber(element, type) : typeAria); + return button; + }; + this._handleEvent = (object, _ref2) => { + let { + target + } = _ref2; + const { + element, type, limit, - classArray: elements[i].classList, - ellipsis, - typeElement: element || 'span', - more: more || false, - less: less || false, - number: number || false, - after: after || 0, - btnClass: btnClass || 'show-more-btn', - btnClassAppend: btnClassAppend || null - }; - this.initial(this.object); - } - } - initial(_ref) { - let { - element, - after, - ellipsis, - limit, - type - } = _ref; - element.setAttribute('aria-expanded', 'false'); - const limitCounts = limit + after; - const ellips = ellipsis === false ? '' : '...'; - if (type === 'text') { - let truncatedText = ''; - const originalText = element.innerHTML.trim(); - let elementText = element.textContent.trim(); - let orgTexReg = originalText; - for (let key in this.regex) { - const { - match, - replace - } = this.regex[key]; - if (key && match) orgTexReg = orgTexReg.replace(match, replace); - } - if (elementText.length > limitCounts) { - truncatedText = htmlSubstr(orgTexReg, limit).concat(ellips); - element.innerHTML = truncatedText; - this.addBtn(this.object); - this.clickEvent(element, { ...this.object, - originalText, - truncatedText - }); + less, + typeElement, + originalText, + truncatedText, + btnClass + } = object; + const checkContainsClass = target.classList.contains(btnClass); + if (!checkContainsClass) return; + const ariaExpanded = element.getAttribute("aria-expanded"); + this._checkExp = ariaExpanded === "false"; + if (type === "text" && checkContainsClass) { + element.textContent = ""; + element.insertAdjacentHTML("beforeend", this._checkExp ? originalText : truncatedText); + if (less) { + const el = createElement(typeElement); + el.classList.add("show-more-wrapper"); + el.insertAdjacentElement("beforeend", this._createBtn(object)); + element.appendChild(el); + } } - } - if (type === 'list' || type === 'table') { - const items = type === 'list' ? [].slice.call(element.children) : element.rows; - if (items.length > limitCounts) { - for (let i = limit; i < items.length; i++) { - addRemoveClass(items[i], true); + if (type === "list" || type === "table") { + const items = this._getNumberCount(element, type); + for (let i = 0; i < items.length; i++) { + const typeRemove = type === "list" ? i >= limit && i < items.length - 1 : i >= limit; + if (ariaExpanded === "false") { + addRemoveClass(items[i]); + } else if (typeRemove) { + addRemoveClass(items[i], true); + } } - this.addBtn(this.object); - this.clickEvent(type === 'list' ? element : element.nextElementSibling, this.object); } - } - } - clickEvent(element, object) { - element.addEventListener('click', this.handleEvent.bind(this, object)); - } - createBtn(_ref2) { - let { - element, - number, - less, - more, - type, - btnClass, - btnClassAppend - } = _ref2; - const typeAria = this.checkExp ? less || '' : more || ''; - const label = this.checkExp ? 'collapse' : 'expand'; - const expanded = this.checkExp ? true : false; - const btn = document.createElement('button'); - btn.className = btnClassAppend == null ? btnClass : btnClass + ' ' + btnClassAppend; - btn.setAttribute('aria-expanded', expanded); - btn.setAttribute('aria-label', label); - btn.setAttribute('tabindex', 0); - btn.innerHTML = number ? typeAria + getNumber(element, type) : typeAria; - return btn; - } - handleEvent(object, _ref3) { - let { - currentTarget, - target - } = _ref3; - const { - element, - type, - limit, - less, - typeElement, - originalText, - truncatedText, - btnClass - } = object; - const checkContainsClass = target.classList.contains(btnClass); - if (!checkContainsClass) return; - const ariaExpanded = element.getAttribute('aria-expanded'); - this.checkExp = ariaExpanded === 'false'; - if (type === 'text' && checkContainsClass) { - element.innerHTML = ''; - element.innerHTML = this.checkExp ? originalText : truncatedText; - if (less) { - const el = document.createElement(typeElement); - el.insertAdjacentElement('beforeend', this.createBtn(object)); - element.appendChild(el); + if (type) { + this._setExpand({ ...object, + target + }); } - } - if (type === 'list' || type === 'table') { - const items = type === 'list' ? [].slice.call(currentTarget.children) : element.rows; - for (let i = 0; i < items.length; i++) { - const typeRemove = type === 'list' ? i >= limit && i < items.length - 1 : i >= limit; - if (ariaExpanded === 'false') { - addRemoveClass(items[i]); - } else if (typeRemove) { - addRemoveClass(items[i], true); - } + }; + this._getNumberCount = (element, type) => { + return type === "list" ? [].slice.call(element.children) : element.rows; + }; + this._addBtn = object => { + const { + type, + element, + more, + typeElement + } = object; + if (!more) return; + if (type === "table") { + element.insertAdjacentElement("afterend", this._createBtn(object)); + } else { + const el = createElement(typeElement); + el.classList.add("show-more-wrapper"); + el.appendChild(this._createBtn(object)); + element.appendChild(el); } - } - if (type === 'table' || type === 'list' || type === 'text') { - this.setExpand({ ...object, + }; + this._setExpand = object => { + const { + element, + type, + less, + more, + number, target + } = object; + const typeAria = this._checkExp ? less : more; + const aria = this._checkExp ? "expand" : "collapse"; + const ariaText = type === "table" ? type : "the " + type; + const lastChild = element.lastElementChild; + setAttributes(element, { + "aria-expanded": this._checkExp }); - } - } - addBtn(object) { - const { - type, - element, - more, - typeElement - } = object; - if (!more) return; - if (type === 'table') { - element.insertAdjacentElement('afterend', this.createBtn(object)); - } else { - const el = document.createElement(typeElement); - el.appendChild(this.createBtn(object)); - element.appendChild(el); - } - } - setExpand(object) { - const { - element, - type, - less, - more, - number, - target - } = object; - const check = this.checkExp; - const typeAria = check ? less : more; - const aria = check ? 'expand' : 'collapse'; - const ariaText = type === 'table' ? type : "the " + type; - const lastChild = element.lastElementChild; - element.setAttribute('aria-expanded', check); - target.setAttribute('aria-expanded', check); - target.setAttribute('aria-label', aria + " " + ariaText); - this.onMoreLess(aria, object); - if (typeAria) { - target.innerHTML = number ? typeAria + getNumber(element, type) : typeAria; - } else if (type === 'table') { - target.parentNode.removeChild(target); - } else if (type === 'list') { - lastChild.parentNode.removeChild(lastChild); - } + setAttributes(target, { + "aria-expanded": this._checkExp, + "aria-label": aria + " " + ariaText + }); + this._onMoreLess(aria, object); + if (typeAria) { + target.innerHTML = number ? typeAria + getNumber(element, type) : typeAria; + } else if (type === "table") { + target.parentNode.removeChild(target); + } else if (type === "list") { + lastChild.parentNode.removeChild(lastChild); + } + }; + const elements = [].slice.call(document.querySelectorAll(className)); + this._onMoreLess = onMoreLess; + this._regex = { ...defaultRegex, + ...regex + }; + elements.map((item, index) => { + const configData = JSON.parse(item.getAttribute("data-config")); + const configGlobal = config; + const configDataAndGlobal = { ...configGlobal, + ...configData + }; + this._object = { + index, + classArray: item.classList, + ...defaultOptions, + ...configDataAndGlobal, + typeElement: configDataAndGlobal.element || "span", + element: item + }; + this._initial(); + }); } } diff --git a/dist/js/showMore.umd.js.map b/dist/js/showMore.umd.js.map index 9bb5d82..3443395 100644 --- a/dist/js/showMore.umd.js.map +++ b/dist/js/showMore.umd.js.map @@ -1 +1 @@ -{"version":3,"file":"showMore.umd.js","sources":["../../sources/utils/function.js","../../sources/utils/regex.js","../../sources/index.js"],"sourcesContent":["/**\r\n * Get hidden element\r\n *\r\n * @param {object} object\r\n * @param {String} type - type of element table | div\r\n * @returns\r\n */\r\nfunction getNumber({ rows, children }, type) {\r\n const elementType = type === 'table' ? rows : children;\r\n\r\n const numbersElementHidden = [].slice\r\n .call(elementType)\r\n .filter((el) => el.className === 'hidden').length;\r\n return numbersElementHidden !== 0 ? ` ${numbersElementHidden}` : '';\r\n}\r\n\r\n// https://stackoverflow.com/questions/6003271/substring-text-with-html-tags-in-javascript\r\n/**\r\n * Substring text width html tags\r\n *\r\n * @param {String} originalText - text with html tags\r\n * @param {String} count - limit of characters\r\n * @returns\r\n */\r\nfunction htmlSubstr(originalText, count) {\r\n let div = document.createElement('div');\r\n div.innerHTML = originalText;\r\n\r\n walk(div, track);\r\n\r\n function track(el) {\r\n if (count > 0) {\r\n let len = el.data.length;\r\n count -= len;\r\n if (count <= 0) {\r\n el.data = el.substringData(0, el.data.length + count);\r\n }\r\n } else {\r\n el.data = '';\r\n }\r\n }\r\n\r\n function walk(el, fn) {\r\n let node = el.firstChild;\r\n do {\r\n if (node.nodeType === 3) {\r\n fn(node);\r\n } else if (node.nodeType === 1 && node.childNodes && node.childNodes[0]) {\r\n walk(node, fn);\r\n }\r\n } while ((node = node.nextSibling));\r\n }\r\n return div.innerHTML;\r\n}\r\n\r\n/**\r\n * Add/remove class 'hidden' to element\r\n *\r\n * @param {HTMLElement} element\r\n * @param {String} type - type of element add or remove\r\n */\r\nfunction addRemoveClass(element, type = false) {\r\n element.classList[type ? 'add' : 'remove']('hidden');\r\n}\r\n\r\nexport { addRemoveClass, getNumber, htmlSubstr };\r\n","/**\r\n * Default regexes for validation\r\n */\r\nconst defaultRegex = {\r\n newLine: {\r\n match: /(\\r\\n|\\n|\\r)/gm,\r\n replace: '',\r\n },\r\n space: {\r\n match: /\\s\\s+/gm,\r\n replace: ' ',\r\n },\r\n br: {\r\n match: //gim,\r\n replace: '',\r\n },\r\n html: {\r\n match: /(<((?!b|\\/b|!strong|\\/strong)[^>]+)>)/gi,\r\n replace: '',\r\n },\r\n};\r\n\r\nexport default defaultRegex;\r\n","import { addRemoveClass, getNumber, htmlSubstr } from './utils/function';\r\nimport defaultRegex from './utils/regex';\r\n\r\n/**\r\n * @class ShowMore\r\n */\r\nexport default class ShowMore {\r\n /**\r\n * Constructor\r\n *\r\n * @param {HTMLElement} className\r\n * @param {Object} object\r\n */\r\n constructor(className, { onMoreLess = () => {}, regex = {}, config } = {}) {\r\n // all html elements\r\n const elements = document.querySelectorAll(className);\r\n\r\n // colback function\r\n this.onMoreLess = onMoreLess;\r\n\r\n // global regex\r\n this.regex = Object.assign(defaultRegex, regex);\r\n\r\n for (let i = 0; i < elements.length; i++) {\r\n const {\r\n type,\r\n limit,\r\n element,\r\n after,\r\n more,\r\n less,\r\n number,\r\n ellipsis,\r\n btnClass,\r\n btnClassAppend,\r\n } = JSON.parse(elements[i].getAttribute('data-config')) || config;\r\n\r\n // create global object\r\n this.object = {\r\n index: i,\r\n element: elements[i],\r\n type,\r\n limit,\r\n classArray: elements[i].classList,\r\n ellipsis,\r\n typeElement: element || 'span',\r\n more: more || false,\r\n less: less || false,\r\n number: number || false,\r\n after: after || 0,\r\n btnClass: btnClass || 'show-more-btn',\r\n btnClassAppend: btnClassAppend || null,\r\n };\r\n\r\n this.initial(this.object);\r\n }\r\n }\r\n\r\n /**\r\n * Initail function\r\n *\r\n * @param {Object} object\r\n */\r\n initial({ element, after, ellipsis, limit, type }) {\r\n // set default aria-expande to false\r\n element.setAttribute('aria-expanded', 'false');\r\n\r\n const limitCounts = limit + after;\r\n const ellips = ellipsis === false ? '' : '...';\r\n\r\n // text\r\n if (type === 'text') {\r\n let truncatedText = '';\r\n const originalText = element.innerHTML.trim();\r\n let elementText = element.textContent.trim();\r\n\r\n let orgTexReg = originalText;\r\n for (let key in this.regex) {\r\n const { match, replace } = this.regex[key];\r\n if (key && match) orgTexReg = orgTexReg.replace(match, replace);\r\n }\r\n\r\n if (elementText.length > limitCounts) {\r\n truncatedText = htmlSubstr(orgTexReg, limit).concat(ellips);\r\n\r\n element.innerHTML = truncatedText;\r\n\r\n this.addBtn(this.object);\r\n\r\n this.clickEvent(element, {\r\n ...this.object,\r\n originalText,\r\n truncatedText,\r\n });\r\n }\r\n }\r\n\r\n // list and table\r\n if (type === 'list' || type === 'table') {\r\n const items =\r\n type === 'list' ? [].slice.call(element.children) : element.rows;\r\n\r\n if (items.length > limitCounts) {\r\n for (let i = limit; i < items.length; i++) {\r\n addRemoveClass(items[i], true);\r\n }\r\n\r\n // add button to the list and table\r\n this.addBtn(this.object);\r\n\r\n // add event click\r\n this.clickEvent(\r\n type === 'list' ? element : element.nextElementSibling,\r\n this.object\r\n );\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Event click\r\n *\r\n * @param {HTMLElement} element\r\n * @param {Object} object\r\n */\r\n clickEvent(element, object) {\r\n element.addEventListener('click', this.handleEvent.bind(this, object));\r\n }\r\n\r\n /**\r\n * Create button\r\n *\r\n * @param {Object} object\r\n * @returns HTMLElement\r\n */\r\n createBtn({ element, number, less, more, type, btnClass, btnClassAppend }) {\r\n const typeAria = this.checkExp ? less || '' : more || '';\r\n const label = this.checkExp ? 'collapse' : 'expand';\r\n const expanded = this.checkExp ? true : false;\r\n\r\n const btn = document.createElement('button');\r\n btn.className =\r\n btnClassAppend == null ? btnClass : btnClass + ' ' + btnClassAppend;\r\n btn.setAttribute('aria-expanded', expanded);\r\n btn.setAttribute('aria-label', label);\r\n btn.setAttribute('tabindex', 0);\r\n btn.innerHTML = number ? typeAria + getNumber(element, type) : typeAria;\r\n return btn;\r\n }\r\n\r\n /**\r\n * Event handler\r\n *\r\n * @param {Object} object\r\n * @param {Event} event\r\n */\r\n handleEvent(object, { currentTarget, target }) {\r\n const {\r\n element,\r\n type,\r\n limit,\r\n less,\r\n typeElement,\r\n originalText,\r\n truncatedText,\r\n btnClass,\r\n } = object;\r\n\r\n // check if the button is clicked\r\n const checkContainsClass = target.classList.contains(btnClass);\r\n\r\n if (!checkContainsClass) return;\r\n\r\n const ariaExpanded = element.getAttribute('aria-expanded');\r\n this.checkExp = ariaExpanded === 'false';\r\n\r\n // --------------------------------------------------\r\n // text\r\n if (type === 'text' && checkContainsClass) {\r\n element.innerHTML = '';\r\n element.innerHTML = this.checkExp ? originalText : truncatedText;\r\n\r\n if (less) {\r\n const el = document.createElement(typeElement);\r\n el.insertAdjacentElement('beforeend', this.createBtn(object));\r\n element.appendChild(el);\r\n }\r\n }\r\n\r\n // --------------------------------------------------\r\n // list and table\r\n if (type === 'list' || type === 'table') {\r\n const items =\r\n type === 'list' ? [].slice.call(currentTarget.children) : element.rows;\r\n\r\n for (let i = 0; i < items.length; i++) {\r\n const typeRemove =\r\n type === 'list' ? i >= limit && i < items.length - 1 : i >= limit;\r\n\r\n if (ariaExpanded === 'false') {\r\n addRemoveClass(items[i]);\r\n } else if (typeRemove) {\r\n addRemoveClass(items[i], true);\r\n }\r\n }\r\n }\r\n\r\n // set aria-expanded\r\n if (type === 'table' || type === 'list' || type === 'text') {\r\n this.setExpand({ ...object, target });\r\n }\r\n }\r\n\r\n /**\r\n * Add button\r\n *\r\n * @param {Object} object\r\n */\r\n addBtn(object) {\r\n const { type, element, more, typeElement } = object;\r\n\r\n if (!more) return;\r\n\r\n if (type === 'table') {\r\n element.insertAdjacentElement('afterend', this.createBtn(object));\r\n } else {\r\n const el = document.createElement(typeElement);\r\n el.appendChild(this.createBtn(object));\r\n element.appendChild(el);\r\n }\r\n }\r\n\r\n /**\r\n * Set aria-expanded\r\n *\r\n * @param {Object} object\r\n */\r\n setExpand(object) {\r\n const { element, type, less, more, number, target } = object;\r\n\r\n const check = this.checkExp;\r\n\r\n const typeAria = check ? less : more;\r\n const aria = check ? 'expand' : 'collapse';\r\n const ariaText = type === 'table' ? type : `the ${type}`;\r\n const lastChild = element.lastElementChild;\r\n\r\n element.setAttribute('aria-expanded', check);\r\n target.setAttribute('aria-expanded', check);\r\n target.setAttribute('aria-label', `${aria} ${ariaText}`);\r\n\r\n // callback function on more/less\r\n this.onMoreLess(aria, object);\r\n\r\n if (typeAria) {\r\n target.innerHTML = number\r\n ? typeAria + getNumber(element, type)\r\n : typeAria;\r\n } else if (type === 'table') {\r\n target.parentNode.removeChild(target);\r\n } else if (type === 'list') {\r\n lastChild.parentNode.removeChild(lastChild);\r\n }\r\n }\r\n}\r\n"],"names":["getNumber","type","rows","children","elementType","numbersElementHidden","slice","call","filter","el","className","length","htmlSubstr","originalText","count","div","document","createElement","innerHTML","walk","track","len","data","substringData","fn","node","firstChild","nodeType","childNodes","nextSibling","addRemoveClass","element","classList","defaultRegex","newLine","match","replace","space","br","html","ShowMore","constructor","onMoreLess","regex","config","elements","querySelectorAll","Object","assign","i","limit","after","more","less","number","ellipsis","btnClass","btnClassAppend","JSON","parse","getAttribute","object","index","classArray","typeElement","initial","setAttribute","limitCounts","ellips","truncatedText","trim","elementText","textContent","orgTexReg","key","concat","addBtn","clickEvent","items","nextElementSibling","addEventListener","handleEvent","bind","createBtn","typeAria","checkExp","label","expanded","btn","currentTarget","target","checkContainsClass","contains","ariaExpanded","insertAdjacentElement","appendChild","typeRemove","setExpand","check","aria","ariaText","lastChild","lastElementChild","parentNode","removeChild"],"mappings":";;;;;;EAOA,SAASA,SAAT,OAAuCC,IAAvC,EAA6C;EAAA,MAA1B;EAAEC,IAAAA,IAAF;EAAQC,IAAAA;EAAR,GAA0B;EAC3C,QAAMC,WAAW,GAAGH,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pF,SAAS,CAAC+B,OAAD,EAAU9B,IAAV,CADC,GAErBmF,QAFJ;EAGD,KAJD,MAIO,IAAInF,IAAI,KAAK,OAAb,EAAsB;EAC3ByF,MAAAA,MAAM,CAACa,UAAP,CAAkBC,WAAlB,CAA8Bd,MAA9B;EACD,KAFM,MAEA,IAAIzF,IAAI,KAAK,MAAb,EAAqB;EAC1BoG,MAAAA,SAAS,CAACE,UAAV,CAAqBC,WAArB,CAAiCH,SAAjC;EACD;EACF;EAjQ2B;;;;;;;;"} \ No newline at end of file +{"version":3,"file":"showMore.umd.js","sources":["../../sources/utils/function.js","../../sources/utils/regex.js","../../sources/utils/defaults.js","../../sources/index.js"],"sourcesContent":["/**\r\n * Get hidden element\r\n *\r\n * @param {object} object\r\n * @param {String} type - type of element table | div\r\n * @returns\r\n */\r\nconst getNumber = ({ rows, children }, type) => {\r\n const elementType = type === \"table\" ? rows : children;\r\n\r\n const numbersElementHidden = [].slice\r\n .call(elementType)\r\n .filter((el) => el.className === \"hidden\").length;\r\n return numbersElementHidden !== 0 ? ` ${numbersElementHidden}` : \"\";\r\n};\r\n\r\n// https://stackoverflow.com/questions/6003271/substring-text-with-html-tags-in-javascript\r\n/**\r\n * Substring text width html tags\r\n *\r\n * @param {String} originalText - text with html tags\r\n * @param {String} count - limit of characters\r\n * @returns\r\n */\r\nconst htmlSubstr = (originalText, count) => {\r\n let div = createElement(\"div\");\r\n div.insertAdjacentHTML(\"afterbegin\", originalText);\r\n\r\n walk(div, track);\r\n\r\n function track(el) {\r\n if (count > 0) {\r\n let len = el.data.length;\r\n count -= len;\r\n if (count <= 0) {\r\n el.data = el.substringData(0, el.data.length + count);\r\n }\r\n } else {\r\n el.data = \"\";\r\n }\r\n }\r\n\r\n function walk(el, fn) {\r\n let node = el.firstChild;\r\n do {\r\n if (node.nodeType === 3) {\r\n fn(node);\r\n } else if (node.nodeType === 1 && node.childNodes && node.childNodes[0]) {\r\n walk(node, fn);\r\n }\r\n } while ((node = node.nextSibling));\r\n }\r\n return div.innerHTML;\r\n};\r\n\r\n/**\r\n * Add/remove class 'hidden' to element\r\n *\r\n * @param {HTMLElement} element\r\n * @param {String} type - type of element add or remove\r\n */\r\nconst addRemoveClass = (element, type = false) => {\r\n return element.classList[type ? \"add\" : \"remove\"](\"hidden\");\r\n};\r\n\r\n/**\r\n * Set attributes to element\r\n *\r\n * @param {HTMLElement} el\r\n * @param {Object} object\r\n */\r\nconst setAttributes = (el, object) => {\r\n for (let key in object) {\r\n el.setAttribute(key, object[key]);\r\n }\r\n};\r\n\r\n/**\r\n * Create element\r\n *\r\n * @param {String} type - type of element\r\n * @returns {HTMLElement}\r\n */\r\nconst createElement = (type) => document.createElement(type);\r\n\r\nexport { addRemoveClass, createElement, getNumber, htmlSubstr, setAttributes };\r\n","/**\r\n * Default regexes for validation\r\n */\r\nconst defaultRegex = {\r\n newLine: {\r\n match: /(\\r\\n|\\n|\\r)/gm,\r\n replace: \"\",\r\n },\r\n space: {\r\n match: /\\s\\s+/gm,\r\n replace: \" \",\r\n },\r\n br: {\r\n match: //gim,\r\n replace: \"\",\r\n },\r\n html: {\r\n match: /(<((?!b|\\/b|!strong|\\/strong)[^>]+)>)/gi,\r\n replace: \"\",\r\n },\r\n};\r\n\r\nexport default defaultRegex;\r\n","const defaultOptions = {\r\n typeElement: \"span\",\r\n more: false,\r\n less: false,\r\n number: false,\r\n nobutton: false,\r\n after: 0,\r\n btnClass: \"show-more-btn\",\r\n btnClassAppend: null,\r\n};\r\n\r\nexport default defaultOptions;\r\n","import {\r\n addRemoveClass,\r\n createElement,\r\n getNumber,\r\n htmlSubstr,\r\n setAttributes,\r\n} from \"./utils/function\";\r\nimport defaultRegex from \"./utils/regex\";\r\nimport defaultOptions from \"./utils/defaults\";\r\n\r\n/**\r\n * @class ShowMore\r\n */\r\nexport default class ShowMore {\r\n /**\r\n * Constructor\r\n *\r\n * @param {HTMLElement} className\r\n * @param {Object} object\r\n */\r\n constructor(className, { onMoreLess = () => {}, regex = {}, config } = {}) {\r\n // all html elements\r\n const elements = [].slice.call(document.querySelectorAll(className));\r\n\r\n // colback function\r\n this._onMoreLess = onMoreLess;\r\n\r\n // global regex\r\n this._regex = { ...defaultRegex, ...regex };\r\n\r\n elements.map((item, index) => {\r\n const configData = JSON.parse(item.getAttribute(\"data-config\"));\r\n const configGlobal = config;\r\n\r\n const configDataAndGlobal = { ...configGlobal, ...configData };\r\n\r\n this._object = {\r\n index,\r\n classArray: item.classList,\r\n ...defaultOptions,\r\n ...configDataAndGlobal,\r\n typeElement: configDataAndGlobal.element || \"span\",\r\n element: item,\r\n };\r\n\r\n this._initial();\r\n });\r\n }\r\n\r\n /**\r\n * Initail function\r\n *\r\n * @param {Object} object\r\n */\r\n _initial = () => {\r\n const { element, after, ellipsis, nobutton, limit, type } = this._object;\r\n // set default aria-expande to false\r\n setAttributes(element, { \"aria-expanded\": \"false\" });\r\n\r\n const limitCounts = limit + after;\r\n const ellips = ellipsis === false ? \"\" : \"...\";\r\n\r\n console.log(nobutton);\r\n\r\n // text\r\n if (type === \"text\") {\r\n const originalText = element.innerHTML.trim();\r\n const elementText = element.textContent.trim();\r\n\r\n if (elementText.length > limitCounts) {\r\n let orgTexReg = originalText;\r\n\r\n for (let key in this._regex) {\r\n const { match, replace } = this._regex[key];\r\n if (key && match) orgTexReg = orgTexReg.replace(match, replace);\r\n }\r\n\r\n const truncatedText = htmlSubstr(orgTexReg, limit - 1).concat(ellips);\r\n\r\n element.textContent = \"\";\r\n element.insertAdjacentHTML(\"beforeend\", truncatedText);\r\n\r\n this._clickEvent(element, {\r\n ...this._object,\r\n originalText,\r\n truncatedText,\r\n });\r\n\r\n if (nobutton) return;\r\n this._addBtn(this._object);\r\n }\r\n }\r\n\r\n // list and table\r\n if (type === \"list\" || type === \"table\") {\r\n const items = this._getNumberCount(element, type);\r\n\r\n if (items.length > limitCounts) {\r\n for (let i = limit; i < items.length; i++) {\r\n addRemoveClass(items[i], true);\r\n }\r\n\r\n // add event click\r\n this._clickEvent(\r\n type === \"list\" ? element : element.nextElementSibling,\r\n this._object\r\n );\r\n\r\n if (nobutton) return;\r\n // add button to the list and table\r\n this._addBtn(this._object);\r\n }\r\n }\r\n };\r\n\r\n /**\r\n * Event click\r\n *\r\n * @param {HTMLElement} element\r\n * @param {Object} object\r\n */\r\n _clickEvent = (element, object) =>\r\n element.addEventListener(\"click\", this._handleEvent.bind(this, object));\r\n\r\n /**\r\n * Create button\r\n *\r\n * @param {Object} object\r\n * @returns HTMLElement\r\n */\r\n _createBtn = ({\r\n element,\r\n number,\r\n less,\r\n more,\r\n type,\r\n btnClass,\r\n btnClassAppend,\r\n }) => {\r\n const typeAria = this._checkExp ? less || \"\" : more || \"\";\r\n const label = this._checkExp ? \"collapse\" : \"expand\";\r\n const expanded = this._checkExp ? true : false;\r\n\r\n const button = createElement(\"button\");\r\n button.className =\r\n btnClassAppend == null ? btnClass : btnClass + \" \" + btnClassAppend;\r\n setAttributes(button, {\r\n \"aria-expanded\": expanded,\r\n \"aria-label\": label,\r\n tabindex: 0,\r\n });\r\n\r\n button.insertAdjacentHTML(\r\n \"beforeend\",\r\n number ? typeAria + getNumber(element, type) : typeAria\r\n );\r\n return button;\r\n };\r\n\r\n /**\r\n * Event handler\r\n *\r\n * @param {Object} object\r\n * @param {Event} event\r\n */\r\n _handleEvent = (object, { target }) => {\r\n const {\r\n element,\r\n type,\r\n limit,\r\n less,\r\n typeElement,\r\n originalText,\r\n truncatedText,\r\n btnClass,\r\n } = object;\r\n\r\n // check if the button is clicked\r\n const checkContainsClass = target.classList.contains(btnClass);\r\n\r\n if (!checkContainsClass) return;\r\n\r\n const ariaExpanded = element.getAttribute(\"aria-expanded\");\r\n this._checkExp = ariaExpanded === \"false\";\r\n\r\n // --------------------------------------------------\r\n // text\r\n if (type === \"text\" && checkContainsClass) {\r\n element.textContent = \"\";\r\n\r\n element.insertAdjacentHTML(\r\n \"beforeend\",\r\n this._checkExp ? originalText : truncatedText\r\n );\r\n\r\n if (less) {\r\n const el = createElement(typeElement);\r\n el.classList.add(\"show-more-wrapper\");\r\n el.insertAdjacentElement(\"beforeend\", this._createBtn(object));\r\n element.appendChild(el);\r\n }\r\n }\r\n\r\n // --------------------------------------------------\r\n // list and table\r\n if (type === \"list\" || type === \"table\") {\r\n const items = this._getNumberCount(element, type);\r\n\r\n for (let i = 0; i < items.length; i++) {\r\n const typeRemove =\r\n type === \"list\" ? i >= limit && i < items.length - 1 : i >= limit;\r\n\r\n if (ariaExpanded === \"false\") {\r\n addRemoveClass(items[i]);\r\n } else if (typeRemove) {\r\n addRemoveClass(items[i], true);\r\n }\r\n }\r\n }\r\n\r\n // set aria-expanded\r\n if (type) {\r\n this._setExpand({ ...object, target });\r\n }\r\n };\r\n\r\n _getNumberCount = (element, type) => {\r\n return type === \"list\" ? [].slice.call(element.children) : element.rows;\r\n };\r\n\r\n /**\r\n * Add button\r\n *\r\n * @param {Object} object\r\n */\r\n _addBtn = (object) => {\r\n const { type, element, more, typeElement } = object;\r\n\r\n if (!more) return;\r\n\r\n if (type === \"table\") {\r\n element.insertAdjacentElement(\"afterend\", this._createBtn(object));\r\n } else {\r\n const el = createElement(typeElement);\r\n el.classList.add(\"show-more-wrapper\");\r\n el.appendChild(this._createBtn(object));\r\n element.appendChild(el);\r\n }\r\n };\r\n\r\n /**\r\n * Set aria-expanded\r\n *\r\n * @param {Object} object\r\n */\r\n _setExpand = (object) => {\r\n const { element, type, less, more, number, target } = object;\r\n\r\n const typeAria = this._checkExp ? less : more;\r\n const aria = this._checkExp ? \"expand\" : \"collapse\";\r\n const ariaText = type === \"table\" ? type : `the ${type}`;\r\n const lastChild = element.lastElementChild;\r\n\r\n setAttributes(element, { \"aria-expanded\": this._checkExp });\r\n setAttributes(target, {\r\n \"aria-expanded\": this._checkExp,\r\n \"aria-label\": `${aria} ${ariaText}`,\r\n });\r\n\r\n // callback function on more/less\r\n this._onMoreLess(aria, object);\r\n\r\n if (typeAria) {\r\n target.innerHTML = number\r\n ? typeAria + getNumber(element, type)\r\n : typeAria;\r\n } else if (type === \"table\") {\r\n target.parentNode.removeChild(target);\r\n } else if (type === \"list\") {\r\n lastChild.parentNode.removeChild(lastChild);\r\n }\r\n };\r\n}\r\n"],"names":["getNumber","type","rows","children","elementType","numbersElementHidden","slice","call","filter","el","className","length","htmlSubstr","originalText","count","div","createElement","insertAdjacentHTML","walk","track","len","data","substringData","fn","node","firstChild","nodeType","childNodes","nextSibling","innerHTML","addRemoveClass","element","classList","setAttributes","object","key","setAttribute","document","defaultRegex","newLine","match","replace","space","br","html","defaultOptions","typeElement","more","less","number","nobutton","after","btnClass","btnClassAppend","ShowMore","constructor","onMoreLess","regex","config","_initial","ellipsis","limit","_object","limitCounts","ellips","console","log","trim","elementText","textContent","orgTexReg","_regex","truncatedText","concat","_clickEvent","_addBtn","items","_getNumberCount","i","nextElementSibling","addEventListener","_handleEvent","bind","_createBtn","typeAria","_checkExp","label","expanded","button","tabindex","target","checkContainsClass","contains","ariaExpanded","getAttribute","add","insertAdjacentElement","appendChild","typeRemove","_setExpand","aria","ariaText","lastChild","lastElementChild","_onMoreLess","parentNode","removeChild","elements","querySelectorAll","map","item","index","configData","JSON","parse","configGlobal","configDataAndGlobal","classArray"],"mappings":";;;;;;EAOA,MAAMA,SAAS,GAAG,OAAqBC,IAArB,KAA8B;EAAA,MAA7B;EAAEC,IAAAA,IAAF;EAAQC,IAAAA;EAAR,GAA6B;EAC9C,QAAMC,WAAW,GAAGH,IAAI,KAAK,OAAT,GAAmBC,IAAnB,GAA0BC,QAA9C;EAEA,QAAME,oBAAoB,GAAG,GAAGC,KAAH,CAC1BC,IAD0B,CACrBH,WADqB,EAE1BI,MAF0B,CAElBC,EAAD,IAAQA,EAAE,CAACC,SAAH,KAAiB,QAFN,EAEgBC,MAF7C;EAGA,SAAON,oBAAoB,KAAK,CAAzB,SAAiCA,oBAAjC,GAA0D,EAAjE;EACD,CAPD;EAiBA,MAAMO,UAAU,GAAG,CAACC,YAAD,EAAeC,KAAf,KAAyB;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lB,CAAX,CAAnB;EACA,YAAMqB,YAAY,GAAGzD,MAArB;EAEA,YAAM0D,mBAAmB,GAAG,EAAE,GAAGD,YAAL;EAAmB,WAAGH;EAAtB,OAA5B;EAEA,WAAKlD,OAAL,GAAe;EACbiD,QAAAA,KADa;EAEbM,QAAAA,UAAU,EAAEP,IAAI,CAAC9E,SAFJ;EAGb,WAAGa,cAHU;EAIb,WAAGuE,mBAJU;EAKbtE,QAAAA,WAAW,EAAEsE,mBAAmB,CAACrF,OAApB,IAA+B,MAL/B;EAMbA,QAAAA,OAAO,EAAE+E;EANI,OAAf;EASA,WAAKnD,QAAL;EACD,KAhBD;EAiBD;EAlC2B;;;;;;;;"} \ No newline at end of file diff --git a/dist/js/showMore.umd.min.js b/dist/js/showMore.umd.min.js index a6d672c..499ba02 100644 --- a/dist/js/showMore.umd.min.js +++ b/dist/js/showMore.umd.min.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).ShowMore=t()}(this,(function(){"use strict";function e(e,t){let{rows:n,children:i}=e;const s="table"===t?n:i,l=[].slice.call(s).filter(e=>"hidden"===e.className).length;return 0!==l?" "+l:""}function t(e,t){void 0===t&&(t=!1),e.classList[t?"add":"remove"]("hidden")}const n={newLine:{match:/(\r\n|\n|\r)/gm,replace:""},space:{match:/\s\s+/gm,replace:" "},br:{match://gim,replace:""},html:{match:/(<((?!b|\/b|!strong|\/strong)[^>]+)>)/gi,replace:""}};return class{constructor(e,t){let{onMoreLess:i=(()=>{}),regex:s={},config:l}=void 0===t?{}:t;const a=document.querySelectorAll(e);this.onMoreLess=i,this.regex=Object.assign(n,s);for(let e=0;er&&(e=function(e,t){let n=document.createElement("div");return n.innerHTML=e,function e(t,n){let i=t.firstChild;do{3===i.nodeType?n(i):1===i.nodeType&&i.childNodes&&i.childNodes[0]&&e(i,n)}while(i=i.nextSibling)}(n,(function(e){if(t>0){let n=e.data.length;(t-=n)<=0&&(e.data=e.substringData(0,e.data.length+t))}else e.data=""})),n.innerHTML}(s,l).concat(c),n.innerHTML=e,this.addBtn(this.object),this.clickEvent(n,{...this.object,originalText:t,truncatedText:e}))}if("list"===a||"table"===a){const e="list"===a?[].slice.call(n.children):n.rows;if(e.length>r){for(let n=l;n=r&&n=r;"false"===b?t(e[n]):i&&t(e[n],!0)}}"table"!==a&&"list"!==a&&"text"!==a||this.setExpand({...e,target:s})}addBtn(e){const{type:t,element:n,more:i,typeElement:s}=e;if(i)if("table"===t)n.insertAdjacentElement("afterend",this.createBtn(e));else{const t=document.createElement(s);t.appendChild(this.createBtn(e)),n.appendChild(t)}}setExpand(t){const{element:n,type:i,less:s,more:l,number:a,target:r}=t,c=this.checkExp,o=c?s:l,d=c?"expand":"collapse",h="table"===i?i:"the "+i,p=n.lastElementChild;n.setAttribute("aria-expanded",c),r.setAttribute("aria-expanded",c),r.setAttribute("aria-label",d+" "+h),this.onMoreLess(d,t),o?r.innerHTML=a?o+e(n,i):o:"table"===i?r.parentNode.removeChild(r):"list"===i&&p.parentNode.removeChild(p)}}})); +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).ShowMore=t()}(this,(function(){"use strict";const e=(e,t)=>{let{rows:s,children:n}=e;const i="table"===t?s:n,l=[].slice.call(i).filter(e=>"hidden"===e.className).length;return 0!==l?" "+l:""},t=function(e,t){return void 0===t&&(t=!1),e.classList[t?"add":"remove"]("hidden")},s=(e,t)=>{for(let s in t)e.setAttribute(s,t[s])},n=e=>document.createElement(e),i={newLine:{match:/(\r\n|\n|\r)/gm,replace:""},space:{match:/\s\s+/gm,replace:" "},br:{match://gim,replace:""},html:{match:/(<((?!b|\/b|!strong|\/strong)[^>]+)>)/gi,replace:""}},l={typeElement:"span",more:!1,less:!1,number:!1,nobutton:!1,after:0,btnClass:"show-more-btn",btnClassAppend:null};return class{constructor(r,o){let{onMoreLess:a=(()=>{}),regex:h={},config:c}=void 0===o?{}:o;this.t=()=>{const{element:e,after:i,ellipsis:l,nobutton:r,limit:o,type:a}=this.s;s(e,{"aria-expanded":"false"});const h=o+i,c=!1===l?"":"...";if("text"===a){const t=e.innerHTML.trim();if(e.textContent.trim().length>h){let s=t;for(let e in this.i){const{match:t,replace:n}=this.i[e];e&&t&&(s=s.replace(t,n))}const i=((e,t)=>{let s=n("div");return s.insertAdjacentHTML("afterbegin",e),function e(t,s){let n=t.firstChild;do{3===n.nodeType?s(n):1===n.nodeType&&n.childNodes&&n.childNodes[0]&&e(n,s)}while(n=n.nextSibling)}(s,(function(e){if(t>0){let s=e.data.length;(t-=s)<=0&&(e.data=e.substringData(0,e.data.length+t))}else e.data=""})),s.innerHTML})(s,o-1).concat(c);if(e.textContent="",e.insertAdjacentHTML("beforeend",i),this.l(e,{...this.s,originalText:t,truncatedText:i}),r)return;this.o(this.s)}}if("list"===a||"table"===a){const s=this.h(e,a);if(s.length>h){for(let e=o;ee.addEventListener("click",this.p.bind(this,t)),this.m=t=>{let{element:i,number:l,less:r,more:o,type:a,btnClass:h,btnClassAppend:c}=t;const f=this.u?r||"":o||"",d=this.u?"collapse":"expand",p=!!this.u,m=n("button");return m.className=null==c?h:h+" "+c,s(m,{"aria-expanded":p,"aria-label":d,tabindex:0}),m.insertAdjacentHTML("beforeend",l?f+e(i,a):f),m},this.p=(e,s)=>{let{target:i}=s;const{element:l,type:r,limit:o,less:a,typeElement:h,originalText:c,truncatedText:f,btnClass:d}=e,p=i.classList.contains(d);if(!p)return;const m=l.getAttribute("aria-expanded");if(this.u="false"===m,"text"===r&&p&&(l.textContent="",l.insertAdjacentHTML("beforeend",this.u?c:f),a)){const t=n(h);t.classList.add("show-more-wrapper"),t.insertAdjacentElement("beforeend",this.m(e)),l.appendChild(t)}if("list"===r||"table"===r){const e=this.h(l,r);for(let s=0;s=o&&s=o;"false"===m?t(e[s]):n&&t(e[s],!0)}}r&&this.g({...e,target:i})},this.h=(e,t)=>"list"===t?[].slice.call(e.children):e.rows,this.o=e=>{const{type:t,element:s,more:i,typeElement:l}=e;if(i)if("table"===t)s.insertAdjacentElement("afterend",this.m(e));else{const t=n(l);t.classList.add("show-more-wrapper"),t.appendChild(this.m(e)),s.appendChild(t)}},this.g=t=>{const{element:n,type:i,less:l,more:r,number:o,target:a}=t,h=this.u?l:r,c=this.u?"expand":"collapse",f="table"===i?i:"the "+i,d=n.lastElementChild;s(n,{"aria-expanded":this.u}),s(a,{"aria-expanded":this.u,"aria-label":c+" "+f}),this.T(c,t),h?a.innerHTML=o?h+e(n,i):h:"table"===i?a.parentNode.removeChild(a):"list"===i&&d.parentNode.removeChild(d)};const f=[].slice.call(document.querySelectorAll(r));this.T=a,this.i={...i,...h},f.map((e,t)=>{const s=JSON.parse(e.getAttribute("data-config")),n={...c,...s};this.s={index:t,classArray:e.classList,...l,...n,typeElement:n.element||"span",element:e},this.t()})}}})); diff --git a/docs/github-corner.js b/docs/github-corner.js index 54c2ded..71539ea 100644 --- a/docs/github-corner.js +++ b/docs/github-corner.js @@ -2,41 +2,15 @@ const githubConrner = ` `; -document.body.insertAdjacentHTML('beforeend', githubConrner); +document.body.insertAdjacentHTML("beforeend", githubConrner); -// if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > -1) { -// var doc = document.documentElement; -// doc.setAttribute('data-useragent', 'IE'); -// } +const topButton = document.createElement("a"); +topButton.href = "#"; +topButton.className = "top-button"; +topButton.textContent = "top"; -// var examples = document.querySelectorAll('.example-html'); -// for (let i = 0; i < examples.length; i++) { -// examples[i].addEventListener('click', function (e) { -// e.target.parentNode.classList.toggle('open'); -// }); -// } - -const topButton = document.createElement('a'); -topButton.href = '#'; -topButton.className = 'top-button' -topButton.textContent = 'top'; - -const section = document.querySelectorAll('section'); +const section = document.querySelectorAll("section"); section.forEach((element, index) => { - if (index > 0) { - element.insertAdjacentElement("beforeend", topButton.cloneNode(true)); - } + element.insertAdjacentElement("beforeend", topButton.cloneNode(true)); }); - -// var sections = document.querySelectorAll('section'); -// for (var i = 0; i < sections.length; i++) { -// var element = sections[i].children; -// var htmlCode = element[1].cloneNode(true).outerHTML.replace(/^\s{1,8}/gm, ''); - -// var htmlConverter = htmlCode.replace(/"/gim, "'").replace(/[\u00A0-\u9999<>\\&]/gim, function (i) { -// return `&#${i.charCodeAt(0)};`; -// }); -// var htmlShow = element[2].lastElementChild.lastElementChild; -// htmlShow.insertAdjacentHTML('beforeend', htmlConverter); -// } \ No newline at end of file diff --git a/docs/global.css b/docs/global.css index dc929c1..5ff65cb 100644 --- a/docs/global.css +++ b/docs/global.css @@ -1,285 +1 @@ -@charset "UTF-8"; -.styled-table { - border-collapse: collapse; - margin: 25px 0; - font-size: 0.9em; - font-family: sans-serif; - box-shadow: 0 0 20px rgba(234, 0, 255, 0.15); -} -.styled-table thead tr { - background-color: #b152ff; - color: #fff; - text-align: left; -} -.styled-table th, -.styled-table td { - padding: 12px 15px; -} -.styled-table tbody tr { - border-bottom: 1px solid #ddd; -} -.styled-table tbody tr:nth-of-type(even) { - background-color: #f8f8f8; -} -.styled-table tbody tr:last-of-type { - border-bottom: 2px solid #b152ff; -} -.styled-table tbody tr:hover { - font-weight: bold; - background: #f6e5ff; - cursor: pointer; -} -.styled-table tfoot { - font-weight: 700; -} - -*, -*:after, -*:before { - box-sizing: border-box; -} - -html { - scroll-behavior: smooth; -} - -body, -html { - margin: 0; - padding: 0; -} - -body { - display: flex; - justify-content: center; - font-family: "Lato", sans-serif; - font-size: 18px; - line-height: 150%; -} - -h2, -h3 { - padding: 0; - margin: 0; -} - -a { - position: relative; - display: inline-block; -} - -ul { - padding: 0 0 0 22px; - margin: 0; -} - -section { - position: relative; - padding: 30px 0; - border-bottom: 1px solid #edd7ff; - margin-top: 20px; -} -section h2 { - position: relative; - color: #583131; - line-height: 1.2; - margin-bottom: 15px; -} -@media (max-width: 700px) { - section h2 { - font-size: 1.2em; - padding: 0; - } -} -section h3 { - margin-top: 20px; -} - -img { - width: 100%; - height: auto; -} - -table { - border-collapse: collapse; - width: 100%; -} - -thead th, -td { - padding: 10px; - border: 1px solid #c8b0ca; - width: 33.33%; - text-align: left; -} - -tbody tr:nth-child(odd) { - background: #eee; -} - -tfoot th { - font-weight: normal; - text-align: left; -} - -html[data-useragent*=IE] .example-html { - display: none; -} - -.container { - max-width: 600px; - width: 100%; - min-height: 100%; - padding: 0 20px; - margin-bottom: 50px; -} - -.display-inline { - display: flex; - flex-direction: row; - flex-wrap: wrap; - list-style: none; - padding: 0; -} -.display-inline li { - margin-right: 10px; -} - -.links-style a { - text-decoration: none; - margin-right: 5px; - color: #681bcc; -} -.links-style a:after { - content: ""; - display: block; - margin: auto; - height: 2px; - width: 0; - background-color: transparent; - transition: width 0.5s ease, background-color 0.5s ease; -} -.links-style a:hover:after { - width: 100%; - background-color: #681bcc; -} - -.example { - position: relative; - display: block; - margin: 10px auto; -} - -pre { - overflow: auto; - margin: 0px auto -50px; - padding: 0; -} -pre code { - line-height: 130%; - border-radius: 4px; -} - -h5 { - position: relative; - cursor: pointer; - padding: 20px 2px 2px 25px; - margin: 0; -} -h5:before { - position: absolute; - content: "➤"; - left: 0; - top: 19px; - color: #b152ff; - transition: transform 0.2s ease-in-out; -} - -.open:focus { - outline: 1px dashed red; -} -.open h5::before { - transform: rotate(90deg); -} -.open pre { - display: block; -} - -.top-button { - position: absolute; - bottom: -12px; - right: 0; - background: #fff; - text-decoration: none; - font-size: 0.8em; - padding: 0 10px 0 10px; -} -.top-button:after { - position: absolute; - top: -1px; - right: -1px; - content: "↑"; -} -.top-button:hover { - color: red; -} - -p { - margin: 5px 0 -15px; -} - -.hljs { - padding: 1em; -} - -.explanation { - font-size: 80%; - line-height: 1.2; - margin-bottom: 20px; -} -.explanation code { - color: #c7254e; - background: #f9f2f4; - padding: 2px 4px; - border-radius: 4px; -} - -.element table, -.element img { - margin: 20px 0; -} - -.hidden { - display: none; -} - -.extending { - margin-top: 5px; - padding: 7px 23px 7px; - outline: 0; - border: 1px solid #000; - cursor: pointer; - position: relative; - color: #000; - background-color: #fff; -} -.extending::after { - content: ""; - background-color: #ffe54c; - width: 100%; - z-index: -1; - position: absolute; - height: 100%; - top: 7px; - left: 7px; - transition: 0.2s; -} -.extending:hover::after { - top: 0px; - left: 0px; -} - -#example-list .element li:last-of-type { - list-style: none; - margin-left: -25px; -} +@charset "UTF-8";.styled-table{border-collapse:collapse;box-shadow:0 0 20px #ea00ff26;font-family:sans-serif;font-size:.9em;margin:25px 0}.styled-table thead tr{background-color:#b152ff;color:#fff;text-align:left}.styled-table td,.styled-table th{padding:12px 15px}.styled-table tbody tr{border-bottom:1px solid #ddd}.styled-table tbody tr:nth-of-type(2n){background-color:#f8f8f8}.styled-table tbody tr:last-of-type{border-bottom:2px solid #b152ff}.styled-table tbody tr:hover{background:#f6e5ff;cursor:pointer;font-weight:700}.styled-table tfoot{font-weight:700}*,:after,:before{box-sizing:border-box}html{scroll-behavior:smooth}body,html{margin:0;padding:0}body{display:flex;font-family:Lato,sans-serif;font-size:18px;justify-content:center;line-height:150%}h2,h3{margin:0;padding:0}a{display:inline-block;position:relative}ul{margin:0;padding:0 0 0 22px}section{border-bottom:1px solid #edd7ff;margin-top:20px;padding:30px 0;position:relative}section h2{color:#583131;line-height:1.2;margin-bottom:15px;position:relative}@media (max-width:700px){section h2{font-size:1.2em;padding:0}}section h3{margin-top:20px}img{height:auto}img,table{width:100%}table{border-collapse:collapse}td,thead th{border:1px solid #c8b0ca;padding:10px;text-align:left;width:33.33%}tbody tr:nth-child(odd){background:#eee}tfoot th{font-weight:400;text-align:left}html[data-useragent*=IE] .example-html{display:none}.container{margin-bottom:50px;max-width:600px;min-height:100%;padding:0 20px;width:100%}.display-inline{display:flex;flex-direction:row;flex-wrap:wrap;list-style:none;padding:0}.display-inline li{margin-right:10px}.links-style a{color:#681bcc;margin-right:5px;text-decoration:none}.links-style a:after{background-color:initial;content:"";display:block;height:2px;margin:auto;transition:width .5s ease,background-color .5s ease;width:0}.links-style a:hover:after{background-color:#681bcc;width:100%}.example{display:block;margin:10px auto;position:relative}pre{margin:0 auto -50px;overflow:auto;padding:0}pre code{border-radius:5px;line-height:130%}h5{cursor:pointer;margin:0;padding:20px 2px 2px 25px;position:relative}h5:before{color:#b152ff;content:"➤";left:0;position:absolute;top:19px;transition:transform .2s ease-in-out}.open:focus{outline:1px dashed red}.open h5:before{transform:rotate(90deg)}.open pre{display:block}.top-button{background:#fff;bottom:-12px;font-size:.8em;padding:0 10px;position:absolute;right:0;text-decoration:none}.top-button:after{content:"↑";position:absolute;right:-1px;top:-1px}.top-button:hover{color:red}p{margin:5px 0 -15px}.hljs{padding:1em}.explanation{font-size:80%;line-height:1.2;margin-bottom:20px}.explanation code{background:#f9f2f4;border-radius:4px;color:#c7254e;padding:2px 4px}.element img,.element table{margin:20px 0}.hidden{display:none}.extending{background-color:#fff;border:1px solid #000;color:#000;cursor:pointer;margin-top:5px;outline:0;padding:7px 23px;position:relative}.extending:after{background-color:#ffe54c;content:"";height:100%;left:7px;position:absolute;top:7px;transition:.2s;width:100%;z-index:-1}.extending:hover:after{left:0;top:0}#example-list .element li:last-of-type{list-style:none;margin-left:-25px} \ No newline at end of file diff --git a/docs/global.css.map b/docs/global.css.map new file mode 100644 index 0000000..3beaa52 --- /dev/null +++ b/docs/global.css.map @@ -0,0 +1 @@ +{"version":3,"sourceRoot":"","sources":["../sources/scss/modules/_table.scss","../sources/scss/global.scss"],"names":[],"mappings":";AAAA;EACE;EACA;EACA;EACA;EACA;;AAEA;EACE;EACA;EACA;;AAGF;AAAA;EAEE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;EACA;EACA;;AAGF;EACE;;;ACnCJ;AAAA;AAAA;EAGE;;;AAGF;EACE;;;AAGF;AAAA;EAEE;EACA;;;AAGF;EACE;EACA;EACA;EACA;EACA;;;AAGF;AAAA;EAEE;EACA;;;AAGF;EACE;EACA;;;AAGF;EACE;EACA;;;AAGF;EACE;EACA;EACA;EACA;;AAEA;EACE;EACA;EACA;EACA;;AAEA;EANF;IAOI;IACA;;;AAIJ;EACE;;;AAIJ;EACE;EACA;;;AAGF;EACE;EACA;;;AAGF;AAAA;EAEE;EACA;EACA;EACA;;;AAGF;EACE;;;AAGF;EACE;EACA;;;AAGF;EACE;;;AAGF;EACE;EACA;EACA;EACA;EACA;;;AAGF;EACE;EACA;EACA;EACA;EACA;;AAEA;EACE;;;AAKF;EACE;EACA;EACA;;AAEA;EACE;EACA;EACA;EACA;EACA;EACA;EACA;;AAGF;EACE;EACA;;;AAKN;EACE;EACA;EACA;;;AAGF;EACE;EACA;EACA;EACA;;AAEA;EACE;EACA;;;AAIJ;EACE;EACA;EACA;EACA;;AAEA;EACE;EACA;EACA;EACA;EACA;EACA;;;AAKF;EACE;;AAIA;EACE;;AAIJ;EACE;;;AAIJ;EACE;EACA;EACA;EACA;EACA;EACA;EACA;;AACA;EACE;EACA;EACA;EACA;;AAEF;EACE;;;AAIJ;EACE;;;AAGF;EACE;;;AAGF;EACE;EACA;EACA;;AAEA;EACE;EACA;EACA;EACA;;;AAKF;AAAA;EAEE;;;AAIJ;EACE;;;AAGF;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;AAEA;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;AAGF;EACE;EACA;;;AAIJ;EACE;EACA","file":"global.css"} \ No newline at end of file diff --git a/docs/index.html b/docs/index.html index 8c2c77b..63eb0a6 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1,21 +1,24 @@ - - - - + + + ShowMoreLess - - - - + + + +
-

Configuration

@@ -42,9 +45,10 @@

Configuration

} }); -

You can add your own regular expression

-
-
+        
+

You can add your own regular expression

+
+
             new ShowMore('.element', {
   regex: {
     image: {
@@ -54,71 +58,106 @@ 

Configuration

} });
+
+

Global config

+

+ If you have one type of items that you want to shorten, you can add + global configuration, you don't need to add + data-config to each item. Below is an example: +

+
+
+              new ShowMore('.element', {
+  config: {
+    type: "text",
+    limit: 120,
+    more: "→ read more",
+    less: "← read less"
+  }
+});
+
+
+

+
+ You can also mix, global configuration + + data-config. In such cases, the + global configuration object will merge with + data-config.
For example, we have 10 texts to + shorten, then we add the global configuration, but we also have a + table that we want to shorten, in this case we add 'data-config' to + table - see global configuration +
+
+
+ If you have global configuration, you don't need to + specify all the variables in the + data-config:

+
+
<div class="element" data-config='{ "limit": 20 }'>
+  Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quo, deleniti?
+</div>
+
-

Global config

-

If you have one type of items that you want to shorten, you can add global configuration, you don't need to - add data-config to each item. Below is an example:

               new ShowMore('.element', {
   config: {
     type: "text",
-    limit: 120,
+    limit: 200,
     more: "→ read more",
     less: "← read less"
   }
 });
 
-

-
- You can also mix, global configuration + data-config. In these cases, - 'data-config' takes - precedence - over 'global configuration'.
For example, we have 10 texts to shorten, then we add the global - configuration, but we also have a table - that we want to - shorten, in this case we add 'data-config' to table - see global configuration -
+
+

More text - without 'after', cut after 120 characters

-
- Lorem ipsum dolor sit amet, consectetur adipisicing elit. Officia illo temporibus voluptate veritatis porro, - dignissimos dolore debitis! +
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit. Officia illo + temporibus voluptate veritatis porro, dignissimos dolore debitis!
             {
   "type": "text",
-  "limit": 120, // show makx chars
+  "limit": 120, // show max chars
   "more": "→ show more",
   "less": "← less"
 }
           
- If the text is too short, then we have a problem, after should prevent this. See the next - example. + If the text is too short, then we have a problem, after should + prevent this. See the next example.
-

More text - from 'after': 50, prevent cutting after 120 characters

-
- Lorem ipsum dolor sit amet, consectetur adipisicing elit. Officia illo temporibus voluptate veritatis porro, - dignissimos dolore debitis! +

+ More text - from 'after': 50, prevent cutting after 120 characters +

+
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit. Officia illo + temporibus voluptate veritatis porro, dignissimos dolore debitis!
             {
   "type": "text",
   "limit": 120,
-  // prevent cutting after 120 characters
-  "after": 50,
+  "after": 50, // prevent cutting after 120 characters
   "more": "→ show more",
   "less": "← less"
 }
@@ -129,21 +168,25 @@ 

More text - from 'after': 50, prevent cutting after 120 characters

More text

-
- Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of - classical - Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at - Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a - Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the - undoubtable - source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum - et - Malorum" (The - Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of - ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", - comes from - a line in section 1.10.32. +
+ Contrary to popular belief, Lorem Ipsum is not simply random + text. It has roots in a piece of classical Latin literature from 45 + BC, making it over 2000 years old. + Richard McClintock, a Latin professor at Hampden-Sydney College in + Virginia, looked up one of the more obscure Latin words, consectetur, from a + Lorem Ipsum passage, and going through the cites of the word in + classical literature, discovered the undoubtable source. + Lorem Ipsum comes from sections 1.10.32 and 1.10.33 + of "de Finibus Bonorum et Malorum" (The Extremes of Good + and Evil) by Cicero, written in 45 BC. This book is a treatise on the + theory of ethics, very popular during the Renaissance. The first line + of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in + section 1.10.32.
@@ -161,26 +204,31 @@ 

More text

More text, "show more" new line

-
- Lorem Ipsum - це текст-"риба", що використовується в - друкарстві та дизайні. Lorem Ipsum є, фактично, стандартною "рибою" аж з XVI сторіччя, коли - невідомий - друкар - взяв шрифтову гранку та склав на ній підбірку - зразків шрифтів. "Риба" не тільки успішно пережила п'ять століть, але й прижилася в електронному верстуванні, - залишаючись по суті незмінною. Вона популяризувалась в 60-их роках минулого сторіччя завдяки - виданню зразків - шрифтів Letraset, які містили уривки з Lorem Ipsum, і вдруге - нещодавно завдяки програмам комп'ютерного - верстування на кшталт Aldus Pagemaker, які використовували різні версії Lorem Ipsum. +
+ Lorem Ipsum - це текст-"риба", що використовується в друкарстві + та дизайні. Lorem + Ipsum є, фактично, стандартною "рибою" аж з XVI сторіччя, коли + невідомий друкар + взяв шрифтову гранку та склав на ній підбірку зразків шрифтів. "Риба" + не тільки успішно пережила п'ять століть, але й прижилася в + електронному верстуванні, залишаючись по суті незмінною. + Вона популяризувалась в 60-их роках минулого сторіччя + завдяки виданню зразків шрифтів Letraset, які містили уривки з Lorem + Ipsum, і вдруге - нещодавно завдяки програмам комп'ютерного + верстування на кшталт Aldus Pagemaker, які використовували різні + версії Lorem Ipsum.
               {
   "type": "text",
   "limit": 90,
-  // adds a 'div' on a new line
-  "element": "div",
+  "element": "div", // adds a 'div' on a new line
   "after": 50,
   "more": "↓ show more",
   "less": "↑ less"
@@ -192,8 +240,10 @@ 

More text, "show more" new line

Element list

-
    +
    • Elektronika
    • Import win
    • Informacja o produktach, handlu i usługach
    • @@ -214,8 +264,7 @@

      Element list

      "element": "li", "more": "↓ show more", "less": "↑ less", - // adds the number of items to the button - "number": true + "number": true // adds the number of items to the button }
@@ -224,8 +273,10 @@

Element list

Element list display-inline

-
    +
    • Usługi murarskie i tynkarskie,
    • Wydobycie i sprzedaż soli,
    • Modemy i mobilny internet,
    • @@ -250,8 +301,10 @@

      Element list display-inline

      Element inline

      -

      Table more/less rows

      - +
      @@ -316,9 +371,9 @@

      Table more/less rows

      - - - + + +
      ONE
      = 35 = 40 = 45= 35= 40= 45
      @@ -338,7 +393,10 @@

      Table more/less rows

      Only "show more"

      -
@@ -364,9 +421,12 @@

Only "show more"

Only the ellipsis

-
- It is a long established fact that a reader will be distracted by the readable content of a page when looking - at its layout. +
+ It is a long established fact that a reader will be distracted by the + readable content of a page when looking at its layout.
@@ -383,15 +443,19 @@ 

Only the ellipsis

Individual button appearance more/less

-
Additional classes to control the appearance of the more/less button +
+ Additional classes to control the appearance of the more/less button btnClass and btnClassAppend
-
- Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate laborum eligendi ipsa quam accusantium sit - nisi. Numquam soluta molestias id, ullam officia atque. Architecto assumenda dicta voluptatum voluptatem, iste - distinctio. +
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate + laborum eligendi ipsa quam accusantium sit nisi. Numquam soluta + molestias id, ullam officia atque. Architecto assumenda dicta + voluptatum voluptatem, iste distinctio.
@@ -409,10 +473,12 @@ 

Individual button appearance more/less

Callback function

-
- With the callback function you can, for example, change the color of elements on the page. Click on 'show - more' to see what happens. +
+ With the callback function you can, for example, change the color of + elements on the page. Click on 'show more' to see what happens.
@@ -421,7 +487,7 @@ 

Callback function

const index = object.index; const h2 = object.element.parentElement.firstElementChild; - if (index == 10) { + if (index == 11) { if (type === 'expand') { h2.setAttribute('style', 'color: #eb00ff;'); console.log(object); @@ -436,21 +502,22 @@

Callback function

-

HTML tags

-
It should be remembered that html elements other than +
+ It should be remembered that html elements other than <strong></strong> and - <b></b> should - be - placed outside - the visible shortened element. -
-
- Lorem ipsum dolor sit, amet consectetur adipisicing elit. Libero deserunt dignissimos blanditiis animi - esse veritatis, quasi, ab, commodi itaque quisquam delectus inventore perspiciatis corrupti! + <b></b> should be placed outside the visible + shortened element. +
+
+ Lorem ipsum dolor sit, amet consectetur adipisicing elit. + Libero deserunt dignissimos blanditiis animi esse veritatis, quasi, + ab, commodi itaque quisquam delectus inventore perspiciatis corrupti! @@ -468,17 +535,20 @@

HTML tags

one f
- Sequi debitis suscipit - molestias, eligendi ab odit ullam. Vero eius debitis quis corporis, possimus veniam sit fugit aliquid. - Fuga, - libero eaque consequuntur ipsa esse omnis, ad eius laboriosam reprehenderit iste quaerat vitae quis corrupti - saepe veniam, ullam placeat voluptatum sint dolore sunt quo. Voluptate fugit quo architecto laboriosam - ipsam - pariatur delectus iusto consectetur provident odio amet tempora veniam velit at deleniti sint soluta - accusamus, praesentium necessitatibus maxime.

Aliquam necessitatibus porro dolores atque aliquid - itaque, ad - maiores! + Sequi debitis suscipit molestias, eligendi ab odit ullam. Vero eius + debitis quis corporis, possimus veniam sit fugit aliquid. + + Fuga, libero eaque consequuntur ipsa esse omnis, ad eius laboriosam + reprehenderit iste quaerat vitae quis corrupti saepe veniam, ullam + placeat voluptatum sint dolore sunt quo. Voluptate fugit quo + architecto laboriosam + ipsam pariatur delectus iusto consectetur provident odio amet + tempora veniam velit at deleniti sint soluta accusamus, praesentium + necessitatibus maxime.

+ Aliquam necessitatibus porro dolores atque aliquid itaque, ad maiores!
@@ -492,13 +562,12 @@ 

HTML tags

-

Global configuration

- Lorem ipsum dolor sit amet, consectetur adipisicing elit. Officia illo temporibus voluptate veritatis porro, - dignissimos dolore debitis! + Lorem ipsum dolor sit amet, consectetur adipisicing elit. Officia illo + temporibus voluptate veritatis porro, dignissimos dolore debitis!
@@ -513,36 +582,35 @@ 

Global configuration

-
- + - - \ No newline at end of file + diff --git a/docs/show-more.css b/docs/show-more.css index 8cb51a5..defdf5d 100644 --- a/docs/show-more.css +++ b/docs/show-more.css @@ -1,13 +1 @@ -.show-more-btn { - margin: auto; - cursor: pointer; - color: #0095e5; - white-space: nowrap; - transition: color 300ms ease-in-out; - border: 0; - font-size: 0.8em; - background: transparent; -} -.show-more-btn:hover { - color: red; -} +.show-more-btn{background:#0000;border:0;color:#0095e5;cursor:pointer;font-size:.8em;margin:auto;transition:color .3s ease-in-out;white-space:nowrap}.show-more-btn:hover{color:red} \ No newline at end of file diff --git a/docs/showMore.min.js b/docs/showMore.min.js index 394ba74..633b8f7 100644 --- a/docs/showMore.min.js +++ b/docs/showMore.min.js @@ -1,2 +1,2 @@ -var ShowMore=function(){"use strict";function e(e,t){let{rows:n,children:s}=e;const i="table"===t?n:s,l=[].slice.call(i).filter(e=>"hidden"===e.className).length;return 0!==l?" "+l:""}function t(e,t){void 0===t&&(t=!1),e.classList[t?"add":"remove"]("hidden")}const n={newLine:{match:/(\r\n|\n|\r)/gm,replace:""},space:{match:/\s\s+/gm,replace:" "},br:{match://gim,replace:""},html:{match:/(<((?!b|\/b|!strong|\/strong)[^>]+)>)/gi,replace:""}};return class{constructor(e,t){let{onMoreLess:s=(()=>{}),regex:i={},config:l}=void 0===t?{}:t;const a=document.querySelectorAll(e);this.onMoreLess=s,this.regex=Object.assign(n,i);for(let e=0;er&&(e=function(e,t){let n=document.createElement("div");return n.innerHTML=e,function e(t,n){let s=t.firstChild;do{3===s.nodeType?n(s):1===s.nodeType&&s.childNodes&&s.childNodes[0]&&e(s,n)}while(s=s.nextSibling)}(n,(function(e){if(t>0){let n=e.data.length;(t-=n)<=0&&(e.data=e.substringData(0,e.data.length+t))}else e.data=""})),n.innerHTML}(i,l).concat(c),n.innerHTML=e,this.addBtn(this.object),this.clickEvent(n,{...this.object,originalText:t,truncatedText:e}))}if("list"===a||"table"===a){const e="list"===a?[].slice.call(n.children):n.rows;if(e.length>r){for(let n=l;n=r&&n=r;"false"===b?t(e[n]):s&&t(e[n],!0)}}"table"!==a&&"list"!==a&&"text"!==a||this.setExpand({...e,target:i})}addBtn(e){const{type:t,element:n,more:s,typeElement:i}=e;if(s)if("table"===t)n.insertAdjacentElement("afterend",this.createBtn(e));else{const t=document.createElement(i);t.appendChild(this.createBtn(e)),n.appendChild(t)}}setExpand(t){const{element:n,type:s,less:i,more:l,number:a,target:r}=t,c=this.checkExp,o=c?i:l,d=c?"expand":"collapse",h="table"===s?s:"the "+s,p=n.lastElementChild;n.setAttribute("aria-expanded",c),r.setAttribute("aria-expanded",c),r.setAttribute("aria-label",d+" "+h),this.onMoreLess(d,t),o?r.innerHTML=a?o+e(n,s):o:"table"===s?r.parentNode.removeChild(r):"list"===s&&p.parentNode.removeChild(p)}}}(); +var ShowMore=function(){"use strict";const t=(t,e)=>{let{rows:s,children:n}=t;const i="table"===e?s:n,l=[].slice.call(i).filter(t=>"hidden"===t.className).length;return 0!==l?" "+l:""},e=function(t,e){return void 0===e&&(e=!1),t.classList[e?"add":"remove"]("hidden")},s=(t,e)=>{for(let s in e)t.setAttribute(s,e[s])},n=t=>document.createElement(t),i={newLine:{match:/(\r\n|\n|\r)/gm,replace:""},space:{match:/\s\s+/gm,replace:" "},br:{match://gim,replace:""},html:{match:/(<((?!b|\/b|!strong|\/strong)[^>]+)>)/gi,replace:""}},l={typeElement:"span",more:!1,less:!1,number:!1,nobutton:!1,after:0,btnClass:"show-more-btn",btnClassAppend:null};return class{constructor(r,a){let{onMoreLess:o=(()=>{}),regex:h={},config:c}=void 0===a?{}:a;this.t=()=>{const{element:t,after:i,ellipsis:l,nobutton:r,limit:a,type:o}=this.s;s(t,{"aria-expanded":"false"});const h=a+i,c=!1===l?"":"...";if(console.log(r),"text"===o){const e=t.innerHTML.trim();if(t.textContent.trim().length>h){let s=e;for(let t in this.i){const{match:e,replace:n}=this.i[t];t&&e&&(s=s.replace(e,n))}const i=((t,e)=>{let s=n("div");return s.insertAdjacentHTML("afterbegin",t),function t(e,s){let n=e.firstChild;do{3===n.nodeType?s(n):1===n.nodeType&&n.childNodes&&n.childNodes[0]&&t(n,s)}while(n=n.nextSibling)}(s,(function(t){if(e>0){let s=t.data.length;(e-=s)<=0&&(t.data=t.substringData(0,t.data.length+e))}else t.data=""})),s.innerHTML})(s,a-1).concat(c);if(t.textContent="",t.insertAdjacentHTML("beforeend",i),this.l(t,{...this.s,originalText:e,truncatedText:i}),r)return;this.o(this.s)}}if("list"===o||"table"===o){const s=this.h(t,o);if(s.length>h){for(let t=a;tt.addEventListener("click",this.p.bind(this,e)),this.m=e=>{let{element:i,number:l,less:r,more:a,type:o,btnClass:h,btnClassAppend:c}=e;const d=this.u?r||"":a||"",p=this.u?"collapse":"expand",f=!!this.u,m=n("button");return m.className=null==c?h:h+" "+c,s(m,{"aria-expanded":f,"aria-label":p,tabindex:0}),m.insertAdjacentHTML("beforeend",l?d+t(i,o):d),m},this.p=(t,s)=>{let{target:i}=s;const{element:l,type:r,limit:a,less:o,typeElement:h,originalText:c,truncatedText:d,btnClass:p}=t,f=i.classList.contains(p);if(!f)return;const m=l.getAttribute("aria-expanded");if(this.u="false"===m,"text"===r&&f&&(l.textContent="",l.insertAdjacentHTML("beforeend",this.u?c:d),o)){const e=n(h);e.classList.add("show-more-wrapper"),e.insertAdjacentElement("beforeend",this.m(t)),l.appendChild(e)}if("list"===r||"table"===r){const t=this.h(l,r);for(let s=0;s=a&&s=a;"false"===m?e(t[s]):n&&e(t[s],!0)}}r&&this.g({...t,target:i})},this.h=(t,e)=>"list"===e?[].slice.call(t.children):t.rows,this.o=t=>{const{type:e,element:s,more:i,typeElement:l}=t;if(i)if("table"===e)s.insertAdjacentElement("afterend",this.m(t));else{const e=n(l);e.classList.add("show-more-wrapper"),e.appendChild(this.m(t)),s.appendChild(e)}},this.g=e=>{const{element:n,type:i,less:l,more:r,number:a,target:o}=e,h=this.u?l:r,c=this.u?"expand":"collapse",d="table"===i?i:"the "+i,p=n.lastElementChild;s(n,{"aria-expanded":this.u}),s(o,{"aria-expanded":this.u,"aria-label":c+" "+d}),this.C(c,e),h?o.innerHTML=a?h+t(n,i):h:"table"===i?o.parentNode.removeChild(o):"list"===i&&p.parentNode.removeChild(p)};const d=[].slice.call(document.querySelectorAll(r));this.C=o,this.i={...i,...h},d.map((t,e)=>{const s=JSON.parse(t.getAttribute("data-config")),n={...c,...s};this.s={index:e,classArray:t.classList,...l,...n,typeElement:n.element||"span",element:t},this.t()})}}}(); //# sourceMappingURL=showMore.min.js.map diff --git a/docs/showMore.min.js.map b/docs/showMore.min.js.map index 34ed734..86a544b 100644 --- a/docs/showMore.min.js.map +++ b/docs/showMore.min.js.map @@ -1 +1 @@ -{"version":3,"file":"showMore.min.js","sources":["../sources/utils/function.js","../sources/utils/regex.js","../sources/index.js"],"sourcesContent":["/**\r\n * Get hidden element\r\n *\r\n * @param {object} object\r\n * @param {String} type - type of element table | div\r\n * @returns\r\n */\r\nfunction getNumber({ rows, children }, type) {\r\n const elementType = type === 'table' ? rows : children;\r\n\r\n const numbersElementHidden = [].slice\r\n .call(elementType)\r\n .filter((el) => el.className === 'hidden').length;\r\n return numbersElementHidden !== 0 ? ` ${numbersElementHidden}` : '';\r\n}\r\n\r\n// https://stackoverflow.com/questions/6003271/substring-text-with-html-tags-in-javascript\r\n/**\r\n * Substring text width html tags\r\n *\r\n * @param {String} originalText - text with html tags\r\n * @param {String} count - limit of characters\r\n * @returns\r\n */\r\nfunction htmlSubstr(originalText, count) {\r\n let div = document.createElement('div');\r\n div.innerHTML = originalText;\r\n\r\n walk(div, track);\r\n\r\n function track(el) {\r\n if (count > 0) {\r\n let len = el.data.length;\r\n count -= len;\r\n if (count <= 0) {\r\n el.data = el.substringData(0, el.data.length + count);\r\n }\r\n } else {\r\n el.data = '';\r\n }\r\n }\r\n\r\n function walk(el, fn) {\r\n let node = el.firstChild;\r\n do {\r\n if (node.nodeType === 3) {\r\n fn(node);\r\n } else if (node.nodeType === 1 && node.childNodes && node.childNodes[0]) {\r\n walk(node, fn);\r\n }\r\n } while ((node = node.nextSibling));\r\n }\r\n return div.innerHTML;\r\n}\r\n\r\n/**\r\n * Add/remove class 'hidden' to element\r\n *\r\n * @param {HTMLElement} element\r\n * @param {String} type - type of element add or remove\r\n */\r\nfunction addRemoveClass(element, type = false) {\r\n element.classList[type ? 'add' : 'remove']('hidden');\r\n}\r\n\r\nexport { addRemoveClass, getNumber, htmlSubstr };\r\n","/**\r\n * Default regexes for validation\r\n */\r\nconst defaultRegex = {\r\n newLine: {\r\n match: /(\\r\\n|\\n|\\r)/gm,\r\n replace: '',\r\n },\r\n space: {\r\n match: /\\s\\s+/gm,\r\n replace: ' ',\r\n },\r\n br: {\r\n match: //gim,\r\n replace: '',\r\n },\r\n html: {\r\n match: /(<((?!b|\\/b|!strong|\\/strong)[^>]+)>)/gi,\r\n replace: '',\r\n },\r\n};\r\n\r\nexport default defaultRegex;\r\n","import { addRemoveClass, getNumber, htmlSubstr } from './utils/function';\r\nimport defaultRegex from './utils/regex';\r\n\r\n/**\r\n * @class ShowMore\r\n */\r\nexport default class ShowMore {\r\n /**\r\n * Constructor\r\n *\r\n * @param {HTMLElement} className\r\n * @param {Object} object\r\n */\r\n constructor(className, { onMoreLess = () => {}, regex = {}, config } = {}) {\r\n // all html elements\r\n const elements = document.querySelectorAll(className);\r\n\r\n // colback function\r\n this.onMoreLess = onMoreLess;\r\n\r\n // global regex\r\n this.regex = Object.assign(defaultRegex, regex);\r\n\r\n for (let i = 0; i < elements.length; i++) {\r\n const {\r\n type,\r\n limit,\r\n element,\r\n after,\r\n more,\r\n less,\r\n number,\r\n ellipsis,\r\n btnClass,\r\n btnClassAppend,\r\n } = JSON.parse(elements[i].getAttribute('data-config')) || config;\r\n\r\n // create global object\r\n this.object = {\r\n index: i,\r\n element: elements[i],\r\n type,\r\n limit,\r\n classArray: elements[i].classList,\r\n ellipsis,\r\n typeElement: element || 'span',\r\n more: more || false,\r\n less: less || false,\r\n number: number || false,\r\n after: after || 0,\r\n btnClass: btnClass || 'show-more-btn',\r\n btnClassAppend: btnClassAppend || null,\r\n };\r\n\r\n this.initial(this.object);\r\n }\r\n }\r\n\r\n /**\r\n * Initail function\r\n *\r\n * @param {Object} object\r\n */\r\n initial({ element, after, ellipsis, limit, type }) {\r\n // set default aria-expande to false\r\n element.setAttribute('aria-expanded', 'false');\r\n\r\n const limitCounts = limit + after;\r\n const ellips = ellipsis === false ? '' : '...';\r\n\r\n // text\r\n if (type === 'text') {\r\n let truncatedText = '';\r\n const originalText = element.innerHTML.trim();\r\n let elementText = element.textContent.trim();\r\n\r\n let orgTexReg = originalText;\r\n for (let key in this.regex) {\r\n const { match, replace } = this.regex[key];\r\n if (key && match) orgTexReg = orgTexReg.replace(match, replace);\r\n }\r\n\r\n if (elementText.length > limitCounts) {\r\n truncatedText = htmlSubstr(orgTexReg, limit).concat(ellips);\r\n\r\n element.innerHTML = truncatedText;\r\n\r\n this.addBtn(this.object);\r\n\r\n this.clickEvent(element, {\r\n ...this.object,\r\n originalText,\r\n truncatedText,\r\n });\r\n }\r\n }\r\n\r\n // list and table\r\n if (type === 'list' || type === 'table') {\r\n const items =\r\n type === 'list' ? [].slice.call(element.children) : element.rows;\r\n\r\n if (items.length > limitCounts) {\r\n for (let i = limit; i < items.length; i++) {\r\n addRemoveClass(items[i], true);\r\n }\r\n\r\n // add button to the list and table\r\n this.addBtn(this.object);\r\n\r\n // add event click\r\n this.clickEvent(\r\n type === 'list' ? element : element.nextElementSibling,\r\n this.object\r\n );\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Event click\r\n *\r\n * @param {HTMLElement} element\r\n * @param {Object} object\r\n */\r\n clickEvent(element, object) {\r\n element.addEventListener('click', this.handleEvent.bind(this, object));\r\n }\r\n\r\n /**\r\n * Create button\r\n *\r\n * @param {Object} object\r\n * @returns HTMLElement\r\n */\r\n createBtn({ element, number, less, more, type, btnClass, btnClassAppend }) {\r\n const typeAria = this.checkExp ? less || '' : more || '';\r\n const label = this.checkExp ? 'collapse' : 'expand';\r\n const expanded = this.checkExp ? true : false;\r\n\r\n const btn = document.createElement('button');\r\n btn.className =\r\n btnClassAppend == null ? btnClass : btnClass + ' ' + btnClassAppend;\r\n btn.setAttribute('aria-expanded', expanded);\r\n btn.setAttribute('aria-label', label);\r\n btn.setAttribute('tabindex', 0);\r\n btn.innerHTML = number ? typeAria + getNumber(element, type) : typeAria;\r\n return btn;\r\n }\r\n\r\n /**\r\n * Event handler\r\n *\r\n * @param {Object} object\r\n * @param {Event} event\r\n */\r\n handleEvent(object, { currentTarget, target }) {\r\n const {\r\n element,\r\n type,\r\n limit,\r\n less,\r\n typeElement,\r\n originalText,\r\n truncatedText,\r\n btnClass,\r\n } = object;\r\n\r\n // check if the button is clicked\r\n const checkContainsClass = target.classList.contains(btnClass);\r\n\r\n if (!checkContainsClass) return;\r\n\r\n const ariaExpanded = element.getAttribute('aria-expanded');\r\n this.checkExp = ariaExpanded === 'false';\r\n\r\n // --------------------------------------------------\r\n // text\r\n if (type === 'text' && checkContainsClass) {\r\n element.innerHTML = '';\r\n element.innerHTML = this.checkExp ? originalText : truncatedText;\r\n\r\n if (less) {\r\n const el = document.createElement(typeElement);\r\n el.insertAdjacentElement('beforeend', this.createBtn(object));\r\n element.appendChild(el);\r\n }\r\n }\r\n\r\n // --------------------------------------------------\r\n // list and table\r\n if (type === 'list' || type === 'table') {\r\n const items =\r\n type === 'list' ? [].slice.call(currentTarget.children) : element.rows;\r\n\r\n for (let i = 0; i < items.length; i++) {\r\n const typeRemove =\r\n type === 'list' ? i >= limit && i < items.length - 1 : i >= limit;\r\n\r\n if (ariaExpanded === 'false') {\r\n addRemoveClass(items[i]);\r\n } else if (typeRemove) {\r\n addRemoveClass(items[i], true);\r\n }\r\n }\r\n }\r\n\r\n // set aria-expanded\r\n if (type === 'table' || type === 'list' || type === 'text') {\r\n this.setExpand({ ...object, target });\r\n }\r\n }\r\n\r\n /**\r\n * Add button\r\n *\r\n * @param {Object} object\r\n */\r\n addBtn(object) {\r\n const { type, element, more, typeElement } = object;\r\n\r\n if (!more) return;\r\n\r\n if (type === 'table') {\r\n element.insertAdjacentElement('afterend', this.createBtn(object));\r\n } else {\r\n const el = document.createElement(typeElement);\r\n el.appendChild(this.createBtn(object));\r\n element.appendChild(el);\r\n }\r\n }\r\n\r\n /**\r\n * Set aria-expanded\r\n *\r\n * @param {Object} object\r\n */\r\n setExpand(object) {\r\n const { element, type, less, more, number, target } = object;\r\n\r\n const check = this.checkExp;\r\n\r\n const typeAria = check ? less : more;\r\n const aria = check ? 'expand' : 'collapse';\r\n const ariaText = type === 'table' ? type : `the ${type}`;\r\n const lastChild = element.lastElementChild;\r\n\r\n element.setAttribute('aria-expanded', check);\r\n target.setAttribute('aria-expanded', check);\r\n target.setAttribute('aria-label', `${aria} ${ariaText}`);\r\n\r\n // callback function on more/less\r\n this.onMoreLess(aria, object);\r\n\r\n if (typeAria) {\r\n target.innerHTML = number\r\n ? typeAria + getNumber(element, type)\r\n : typeAria;\r\n } else if (type === 'table') {\r\n target.parentNode.removeChild(target);\r\n } else if (type === 'list') {\r\n lastChild.parentNode.removeChild(lastChild);\r\n }\r\n }\r\n}\r\n"],"names":["getNumber","type","rows","children","elementType","numbersElementHidden","slice","call","filter","el","className","length","addRemoveClass","element","classList","defaultRegex","newLine","match","replace","space","br","html","constructor","onMoreLess","regex","config","elements","document","querySelectorAll","Object","assign","i","limit","after","more","less","number","ellipsis","btnClass","btnClassAppend","JSON","parse","getAttribute","object","index","classArray","typeElement","initial","this","setAttribute","limitCounts","ellips","truncatedText","originalText","innerHTML","trim","elementText","textContent","orgTexReg","key","count","div","createElement","walk","fn","node","firstChild","nodeType","childNodes","nextSibling","len","data","substringData","htmlSubstr","concat","addBtn","clickEvent","items","nextElementSibling","addEventListener","handleEvent","bind","createBtn","typeAria","checkExp","label","expanded","btn","currentTarget","target","checkContainsClass","contains","ariaExpanded","insertAdjacentElement","appendChild","typeRemove","setExpand","check","aria","ariaText","lastChild","lastElementChild","parentNode","removeChild"],"mappings":"qCAOA,SAASA,IAA8BC,OAApBC,KAAEA,EAAFC,SAAQA,WACnBC,EAAuB,UAATH,EAAmBC,EAAOC,EAExCE,EAAuB,GAAGC,MAC7BC,KAAKH,GACLI,OAAQC,GAAwB,WAAjBA,EAAGC,WAAwBC,cACb,IAAzBN,MAAiCA,EAAyB,GAgDnE,SAASO,EAAeC,EAASZ,YAAAA,IAAAA,GAAO,GACtCY,EAAQC,UAAUb,EAAO,MAAQ,UAAU,UC3D7C,MAAMc,EAAe,CACnBC,QAAS,CACPC,MAAO,iBACPC,QAAS,IAEXC,MAAO,CACLF,MAAO,UACPC,QAAS,KAEXE,GAAI,CACFH,MAAO,gBACPC,QAAS,IAEXG,KAAM,CACJJ,MAAO,0CACPC,QAAS,YCZE,MAObI,YAAYZ,SAAWa,WAAEA,EAAa,SAAfC,MAAyBA,EAAQ,GAAjCC,OAAqCA,cAAW,WAE/DC,EAAWC,SAASC,iBAAiBlB,QAGtCa,WAAaA,OAGbC,MAAQK,OAAOC,OAAOf,EAAcS,OAEpC,IAAIO,EAAI,EAAGA,EAAIL,EAASf,OAAQoB,IAAK,OAClC9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KAAuBA,EAAvBE,OAA6BA,EAA7BqD,OAAqCA,GAAW9C,EAEhDsD,EAAQjD,KAAKoC,SAEbD,EAAWc,EAAQ9D,EAAOD,EAC1BgE,EAAOD,EAAQ,SAAW,WAC1BE,EAAoB,UAATlG,EAAmBA,SAAcA,EAC5CmG,EAAYvF,EAAQwF,iBAE1BxF,EAAQoC,aAAa,gBAAiBgD,GACtCR,EAAOxC,aAAa,gBAAiBgD,GACrCR,EAAOxC,aAAa,aAAiBiD,MAAQC,QAGxC5E,WAAW2E,EAAMvD,GAElBwC,EACFM,EAAOnC,UAAYlB,EACf+C,EAAWnF,EAAUa,EAASZ,GAC9BkF,EACc,UAATlF,EACTwF,EAAOa,WAAWC,YAAYd,GACZ,SAATxF,GACTmG,EAAUE,WAAWC,YAAYH"} \ No newline at end of file +{"version":3,"file":"showMore.min.js","sources":["../sources/utils/function.js","../sources/utils/regex.js","../sources/utils/defaults.js","../sources/index.js"],"sourcesContent":["/**\r\n * Get hidden element\r\n *\r\n * @param {object} object\r\n * @param {String} type - type of element table | div\r\n * @returns\r\n */\r\nconst getNumber = ({ rows, children }, type) => {\r\n const elementType = type === \"table\" ? rows : children;\r\n\r\n const numbersElementHidden = [].slice\r\n .call(elementType)\r\n .filter((el) => el.className === \"hidden\").length;\r\n return numbersElementHidden !== 0 ? ` ${numbersElementHidden}` : \"\";\r\n};\r\n\r\n// https://stackoverflow.com/questions/6003271/substring-text-with-html-tags-in-javascript\r\n/**\r\n * Substring text width html tags\r\n *\r\n * @param {String} originalText - text with html tags\r\n * @param {String} count - limit of characters\r\n * @returns\r\n */\r\nconst htmlSubstr = (originalText, count) => {\r\n let div = createElement(\"div\");\r\n div.insertAdjacentHTML(\"afterbegin\", originalText);\r\n\r\n walk(div, track);\r\n\r\n function track(el) {\r\n if (count > 0) {\r\n let len = el.data.length;\r\n count -= len;\r\n if (count <= 0) {\r\n el.data = el.substringData(0, el.data.length + count);\r\n }\r\n } else {\r\n el.data = \"\";\r\n }\r\n }\r\n\r\n function walk(el, fn) {\r\n let node = el.firstChild;\r\n do {\r\n if (node.nodeType === 3) {\r\n fn(node);\r\n } else if (node.nodeType === 1 && node.childNodes && node.childNodes[0]) {\r\n walk(node, fn);\r\n }\r\n } while ((node = node.nextSibling));\r\n }\r\n return div.innerHTML;\r\n};\r\n\r\n/**\r\n * Add/remove class 'hidden' to element\r\n *\r\n * @param {HTMLElement} element\r\n * @param {String} type - type of element add or remove\r\n */\r\nconst addRemoveClass = (element, type = false) => {\r\n return element.classList[type ? \"add\" : \"remove\"](\"hidden\");\r\n};\r\n\r\n/**\r\n * Set attributes to element\r\n *\r\n * @param {HTMLElement} el\r\n * @param {Object} object\r\n */\r\nconst setAttributes = (el, object) => {\r\n for (let key in object) {\r\n el.setAttribute(key, object[key]);\r\n }\r\n};\r\n\r\n/**\r\n * Create element\r\n *\r\n * @param {String} type - type of element\r\n * @returns {HTMLElement}\r\n */\r\nconst createElement = (type) => document.createElement(type);\r\n\r\nexport { addRemoveClass, createElement, getNumber, htmlSubstr, setAttributes };\r\n","/**\r\n * Default regexes for validation\r\n */\r\nconst defaultRegex = {\r\n newLine: {\r\n match: /(\\r\\n|\\n|\\r)/gm,\r\n replace: \"\",\r\n },\r\n space: {\r\n match: /\\s\\s+/gm,\r\n replace: \" \",\r\n },\r\n br: {\r\n match: //gim,\r\n replace: \"\",\r\n },\r\n html: {\r\n match: /(<((?!b|\\/b|!strong|\\/strong)[^>]+)>)/gi,\r\n replace: \"\",\r\n },\r\n};\r\n\r\nexport default defaultRegex;\r\n","const defaultOptions = {\r\n typeElement: \"span\",\r\n more: false,\r\n less: false,\r\n number: false,\r\n nobutton: false,\r\n after: 0,\r\n btnClass: \"show-more-btn\",\r\n btnClassAppend: null,\r\n};\r\n\r\nexport default defaultOptions;\r\n","import {\r\n addRemoveClass,\r\n createElement,\r\n getNumber,\r\n htmlSubstr,\r\n setAttributes,\r\n} from \"./utils/function\";\r\nimport defaultRegex from \"./utils/regex\";\r\nimport defaultOptions from \"./utils/defaults\";\r\n\r\n/**\r\n * @class ShowMore\r\n */\r\nexport default class ShowMore {\r\n /**\r\n * Constructor\r\n *\r\n * @param {HTMLElement} className\r\n * @param {Object} object\r\n */\r\n constructor(className, { onMoreLess = () => {}, regex = {}, config } = {}) {\r\n // all html elements\r\n const elements = [].slice.call(document.querySelectorAll(className));\r\n\r\n // colback function\r\n this._onMoreLess = onMoreLess;\r\n\r\n // global regex\r\n this._regex = { ...defaultRegex, ...regex };\r\n\r\n elements.map((item, index) => {\r\n const configData = JSON.parse(item.getAttribute(\"data-config\"));\r\n const configGlobal = config;\r\n\r\n const configDataAndGlobal = { ...configGlobal, ...configData };\r\n\r\n this._object = {\r\n index,\r\n classArray: item.classList,\r\n ...defaultOptions,\r\n ...configDataAndGlobal,\r\n typeElement: configDataAndGlobal.element || \"span\",\r\n element: item,\r\n };\r\n\r\n this._initial();\r\n });\r\n }\r\n\r\n /**\r\n * Initail function\r\n *\r\n * @param {Object} object\r\n */\r\n _initial = () => {\r\n const { element, after, ellipsis, nobutton, limit, type } = this._object;\r\n // set default aria-expande to false\r\n setAttributes(element, { \"aria-expanded\": \"false\" });\r\n\r\n const limitCounts = limit + after;\r\n const ellips = ellipsis === false ? \"\" : \"...\";\r\n\r\n console.log(nobutton);\r\n\r\n // text\r\n if (type === \"text\") {\r\n const originalText = element.innerHTML.trim();\r\n const elementText = element.textContent.trim();\r\n\r\n if (elementText.length > limitCounts) {\r\n let orgTexReg = originalText;\r\n\r\n for (let key in this._regex) {\r\n const { match, replace } = this._regex[key];\r\n if (key && match) orgTexReg = orgTexReg.replace(match, replace);\r\n }\r\n\r\n const truncatedText = htmlSubstr(orgTexReg, limit - 1).concat(ellips);\r\n\r\n element.textContent = \"\";\r\n element.insertAdjacentHTML(\"beforeend\", truncatedText);\r\n\r\n this._clickEvent(element, {\r\n ...this._object,\r\n originalText,\r\n truncatedText,\r\n });\r\n\r\n if (nobutton) return;\r\n this._addBtn(this._object);\r\n }\r\n }\r\n\r\n // list and table\r\n if (type === \"list\" || type === \"table\") {\r\n const items = this._getNumberCount(element, type);\r\n\r\n if (items.length > limitCounts) {\r\n for (let i = limit; i < items.length; i++) {\r\n addRemoveClass(items[i], true);\r\n }\r\n\r\n // add event click\r\n this._clickEvent(\r\n type === \"list\" ? element : element.nextElementSibling,\r\n this._object\r\n );\r\n\r\n if (nobutton) return;\r\n // add button to the list and table\r\n this._addBtn(this._object);\r\n }\r\n }\r\n };\r\n\r\n /**\r\n * Event click\r\n *\r\n * @param {HTMLElement} element\r\n * @param {Object} object\r\n */\r\n _clickEvent = (element, object) =>\r\n element.addEventListener(\"click\", this._handleEvent.bind(this, object));\r\n\r\n /**\r\n * Create button\r\n *\r\n * @param {Object} object\r\n * @returns HTMLElement\r\n */\r\n _createBtn = ({\r\n element,\r\n number,\r\n less,\r\n more,\r\n type,\r\n btnClass,\r\n btnClassAppend,\r\n }) => {\r\n const typeAria = this._checkExp ? less || \"\" : more || \"\";\r\n const label = this._checkExp ? \"collapse\" : \"expand\";\r\n const expanded = this._checkExp ? true : false;\r\n\r\n const button = createElement(\"button\");\r\n button.className =\r\n btnClassAppend == null ? btnClass : btnClass + \" \" + btnClassAppend;\r\n setAttributes(button, {\r\n \"aria-expanded\": expanded,\r\n \"aria-label\": label,\r\n tabindex: 0,\r\n });\r\n\r\n button.insertAdjacentHTML(\r\n \"beforeend\",\r\n number ? typeAria + getNumber(element, type) : typeAria\r\n );\r\n return button;\r\n };\r\n\r\n /**\r\n * Event handler\r\n *\r\n * @param {Object} object\r\n * @param {Event} event\r\n */\r\n _handleEvent = (object, { target }) => {\r\n const {\r\n element,\r\n type,\r\n limit,\r\n less,\r\n typeElement,\r\n originalText,\r\n truncatedText,\r\n btnClass,\r\n } = object;\r\n\r\n // check if the button is clicked\r\n const checkContainsClass = target.classList.contains(btnClass);\r\n\r\n if (!checkContainsClass) return;\r\n\r\n const ariaExpanded = element.getAttribute(\"aria-expanded\");\r\n this._checkExp = ariaExpanded === \"false\";\r\n\r\n // --------------------------------------------------\r\n // text\r\n if (type === \"text\" && checkContainsClass) {\r\n element.textContent = \"\";\r\n\r\n element.insertAdjacentHTML(\r\n \"beforeend\",\r\n this._checkExp ? originalText : truncatedText\r\n );\r\n\r\n if (less) {\r\n const el = createElement(typeElement);\r\n el.classList.add(\"show-more-wrapper\");\r\n el.insertAdjacentElement(\"beforeend\", this._createBtn(object));\r\n element.appendChild(el);\r\n }\r\n }\r\n\r\n // --------------------------------------------------\r\n // list and table\r\n if (type === \"list\" || type === \"table\") {\r\n const items = this._getNumberCount(element, type);\r\n\r\n for (let i = 0; i < items.length; i++) {\r\n const typeRemove =\r\n type === \"list\" ? i >= limit && i < items.length - 1 : i >= limit;\r\n\r\n if (ariaExpanded === \"false\") {\r\n addRemoveClass(items[i]);\r\n } else if (typeRemove) {\r\n addRemoveClass(items[i], true);\r\n }\r\n }\r\n }\r\n\r\n // set aria-expanded\r\n if (type) {\r\n this._setExpand({ ...object, target });\r\n }\r\n };\r\n\r\n _getNumberCount = (element, type) => {\r\n return type === \"list\" ? [].slice.call(element.children) : element.rows;\r\n };\r\n\r\n /**\r\n * Add button\r\n *\r\n * @param {Object} object\r\n */\r\n _addBtn = (object) => {\r\n const { type, element, more, typeElement } = object;\r\n\r\n if (!more) return;\r\n\r\n if (type === \"table\") {\r\n element.insertAdjacentElement(\"afterend\", this._createBtn(object));\r\n } else {\r\n const el = createElement(typeElement);\r\n el.classList.add(\"show-more-wrapper\");\r\n el.appendChild(this._createBtn(object));\r\n element.appendChild(el);\r\n }\r\n };\r\n\r\n /**\r\n * Set aria-expanded\r\n *\r\n * @param {Object} object\r\n */\r\n _setExpand = (object) => {\r\n const { element, type, less, more, number, target } = object;\r\n\r\n const typeAria = this._checkExp ? less : more;\r\n const aria = this._checkExp ? \"expand\" : \"collapse\";\r\n const ariaText = type === \"table\" ? type : `the ${type}`;\r\n const lastChild = element.lastElementChild;\r\n\r\n setAttributes(element, { \"aria-expanded\": this._checkExp });\r\n setAttributes(target, {\r\n \"aria-expanded\": this._checkExp,\r\n \"aria-label\": `${aria} ${ariaText}`,\r\n });\r\n\r\n // callback function on more/less\r\n this._onMoreLess(aria, object);\r\n\r\n if (typeAria) {\r\n target.innerHTML = number\r\n ? typeAria + getNumber(element, type)\r\n : typeAria;\r\n } else if (type === \"table\") {\r\n target.parentNode.removeChild(target);\r\n } else if (type === \"list\") {\r\n lastChild.parentNode.removeChild(lastChild);\r\n }\r\n };\r\n}\r\n"],"names":["getNumber","type","rows","children","elementType","numbersElementHidden","slice","call","filter","el","className","length","addRemoveClass","element","classList","setAttributes","object","key","setAttribute","createElement","document","defaultRegex","newLine","match","replace","space","br","html","defaultOptions","typeElement","more","less","number","nobutton","after","btnClass","btnClassAppend","constructor","onMoreLess","regex","config","_initial","ellipsis","limit","this","_object","limitCounts","ellips","console","log","originalText","innerHTML","trim","textContent","orgTexReg","_regex","truncatedText","count","div","insertAdjacentHTML","walk","fn","node","firstChild","nodeType","childNodes","nextSibling","len","data","substringData","htmlSubstr","concat","_clickEvent","_addBtn","items","_getNumberCount","i","nextElementSibling","addEventListener","_handleEvent","bind","_createBtn","_ref","typeAria","_checkExp","label","expanded","button","tabindex","target","checkContainsClass","contains","ariaExpanded","getAttribute","add","insertAdjacentElement","appendChild","typeRemove","_setExpand","aria","ariaText","lastChild","lastElementChild","_onMoreLess","parentNode","removeChild","elements","querySelectorAll","map","item","index","configData","JSON","parse","configDataAndGlobal","classArray"],"mappings":"qCAOA,MAAMA,EAAY,GAAqBC,SAApBC,KAAEA,EAAFC,SAAQA,WACnBC,EAAuB,UAATH,EAAmBC,EAAOC,EAExCE,EAAuB,GAAGC,MAC7BC,KAAKH,GACLI,OAAQC,GAAwB,WAAjBA,EAAGC,WAAwBC,cACb,IAAzBN,MAAiCA,EAAyB,IAgD7DO,EAAiB,SAACC,EAASZ,mBAAAA,IAAAA,GAAO,GAC/BY,EAAQC,UAAUb,EAAO,MAAQ,UAAU,WAS9Cc,EAAgB,CAACN,EAAIO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IAC/BH,EAAQmF,YAAYvF,UASxByF,EAAclF,UACNH,QAAEA,EAAFZ,KAAWA,EAAX8B,KAAiBA,EAAjBD,KAAuBA,EAAvBE,OAA6BA,EAA7ByD,OAAqCA,GAAWzE,EAEhDmE,EAAWvC,KAAKwC,EAAYrD,EAAOD,EACnCqE,EAAOvD,KAAKwC,EAAY,SAAW,WACnCgB,EAAoB,UAATnG,EAAmBA,SAAcA,EAC5CoG,EAAYxF,EAAQyF,iBAE1BvF,EAAcF,EAAS,iBAAmB+B,KAAKwC,IAC/CrE,EAAc0E,EAAQ,iBACH7C,KAAKwC,eACLe,MAAQC,SAItBG,EAAYJ,EAAMnF,GAEnBmE,EACFM,EAAOtC,UAAYnB,EACfmD,EAAWnF,EAAUa,EAASZ,GAC9BkF,EACc,UAATlF,EACTwF,EAAOe,WAAWC,YAAYhB,GACZ,SAATxF,GACToG,EAAUG,WAAWC,YAAYJ,UAjQ7BK,EAAW,GAAGpG,MAAMC,KAAKa,SAASuF,iBAAiBjG,SAGpD6F,EAAcjE,OAGdiB,EAAS,IAAKlC,KAAiBkB,GAEpCmE,EAASE,IAAI,CAACC,EAAMC,WACZC,EAAaC,KAAKC,MAAMJ,EAAKhB,aAAa,gBAG1CqB,EAAsB,IAFP1E,KAE6BuE,QAE7ClE,EAAU,CACbiE,MAAAA,EACAK,WAAYN,EAAK/F,aACdc,KACAsF,EACHrF,YAAaqF,EAAoBrG,SAAW,OAC5CA,QAASgG,QAGNpE"} \ No newline at end of file diff --git a/package.json b/package.json index 667eaed..bd69fff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "show-more-read", - "version": "1.1.2", + "version": "1.1.3", "main": "dist/js/showMore.js", "description": "JavaScript library that truncates text, list or table by chars, elements or rows", "author": "Grzegorz Tomicki", @@ -14,14 +14,15 @@ "url": "https://github.com/tomik23/show-more.git" }, "scripts": { - "dev": "concurrently \"yarn sass:dev\" \"yarn sass:prod\" \"yarn watch\"", - "prod": "yarn build && yarn sass:prod && yarn postcss && yarn postcss:prod", + "dev": "concurrently \"yarn sass:dev\" \"yarn sass:prod\" \"yarn watch\"", + "prod": "yarn build && yarn sass:prod && yarn postcss && yarn postcss:prod && yarn lib:version", "build": "rollup -c --environment PRODUCTION", "watch": "rollup -c -w", "sass:prod": "sass --no-source-map sources/scss:docs", "sass:dev": "sass --watch sources/scss:docs", "postcss": "postcss docs/**/*.css --dir docs", - "postcss:prod": "postcss --ext min.css docs/show-more.css --dir dist/css" + "postcss:prod": "postcss --ext min.css docs/show-more.css --dir dist/css", + "lib:version": "node ./scripts/version.js" }, "keywords": [ "show-more/less", diff --git a/rollup.config.js b/rollup.config.js index 5c62613..0a29d41 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,36 +1,36 @@ -import { babel } from '@rollup/plugin-babel'; -import { terser } from 'rollup-plugin-terser'; -import serve from 'rollup-plugin-serve'; -import livereload from 'rollup-plugin-livereload'; -import cleanup from 'rollup-plugin-cleanup'; +import { babel } from "@rollup/plugin-babel"; +import { terser } from "rollup-plugin-terser"; +import serve from "rollup-plugin-serve"; +import livereload from "rollup-plugin-livereload"; +import cleanup from "rollup-plugin-cleanup"; -import pkg from './package.json'; +import pkg from "./package.json"; const { PRODUCTION } = process.env; -const input = 'sources/index.js'; +const input = "sources/index.js"; const targets = { targets: { - browsers: ['defaults', 'not IE 11', 'maintained node versions'], + browsers: ["defaults", "not IE 11", "maintained node versions"], }, }; const targetsIE = { targets: { - browsers: ['>0.2%', 'not dead', 'not op_mini all'], + browsers: [">0.2%", "not dead", "not op_mini all"], }, }; const pluginsConfig = (target) => [ babel({ - babelHelpers: 'bundled', + babelHelpers: "bundled", presets: [ [ - '@babel/preset-env', + "@babel/preset-env", { // debug: true, // useBuiltIns: 'usage', - useBuiltIns: 'entry', + useBuiltIns: "entry", corejs: 3, loose: true, ...target, @@ -41,14 +41,22 @@ const pluginsConfig = (target) => [ cleanup(), ]; +const terserConfig = { + mangle: { + properties: { + regex: /^_/, + }, + }, +}; + export default [ { input, plugins: pluginsConfig(targets), watch: false, output: { - name: 'ShowMore', - format: 'iife', + name: "ShowMore", + format: "iife", file: pkg.main, sourcemap: true, }, @@ -58,28 +66,27 @@ export default [ plugins: pluginsConfig(targets), watch: false, output: { - name: 'ShowMore', - format: 'iife', + name: "ShowMore", + format: "iife", sourcemap: false, - file: 'dist/js/showMore.min.js', - plugins: [terser()], + file: "dist/js/showMore.min.js", + plugins: [terser({ ...terserConfig })], }, }, { input, plugins: pluginsConfig(targets), output: { - name: 'ShowMore', - format: 'iife', + name: "ShowMore", + format: "iife", sourcemap: true, - file: 'docs/showMore.min.js', + file: "docs/showMore.min.js", plugins: [ PRODUCTION && terser({ - mangle: true, - compress: { drop_console: true, drop_debugger: true }, + ...terserConfig, }), - !PRODUCTION && serve({ open: true, contentBase: ['docs'] }), + !PRODUCTION && serve({ open: true, contentBase: ["docs"] }), !PRODUCTION && livereload(), ], }, @@ -90,19 +97,19 @@ export default [ watch: false, output: [ { - name: 'ShowMore', - format: 'umd', + name: "ShowMore", + format: "umd", sourcemap: true, - file: 'dist/js/showMore.umd.js', + file: "dist/js/showMore.umd.js", }, { - name: 'ShowMore', - format: 'umd', + name: "ShowMore", + format: "umd", sourcemap: false, - file: 'dist/js/showMore.umd.min.js', + file: "dist/js/showMore.umd.min.js", plugins: [ terser({ - mangle: true, + ...terserConfig, compress: { drop_console: true, drop_debugger: true }, }), ], @@ -115,19 +122,19 @@ export default [ watch: false, output: [ { - name: 'ShowMore', - format: 'es', + name: "ShowMore", + format: "es", sourcemap: true, - file: 'dist/js/showMore.esm.js', + file: "dist/js/showMore.esm.js", }, { - name: 'ShowMore', - format: 'es', + name: "ShowMore", + format: "es", sourcemap: false, - file: 'dist/js/showMore.esm.min.js', + file: "dist/js/showMore.esm.min.js", plugins: [ terser({ - mangle: true, + ...terserConfig, compress: { drop_console: true, drop_debugger: true }, }), ], @@ -140,13 +147,13 @@ export default [ watch: false, output: [ { - name: 'ShowMore', - format: 'iife', + name: "ShowMore", + format: "iife", sourcemap: false, - file: 'dist/js/showMore.ie.min.js', + file: "dist/js/showMore.ie.min.js", plugins: [ terser({ - mangle: true, + ...terserConfig, compress: { drop_console: true, drop_debugger: true }, }), ], diff --git a/scripts/version.js b/scripts/version.js new file mode 100644 index 0000000..36f6e37 --- /dev/null +++ b/scripts/version.js @@ -0,0 +1,31 @@ +const fs = require("fs"); + +const pkg = require("../package.json"); + +const newVersion = pkg.version; + +function updateVersion(file, newVersion) { + fs.readFile(file, "utf8", function (err, data) { + if (err) { + return console.log(err); + } + + const matches = data.match(/\@(.*?)\/dist/i)[1]; + + const reg = new RegExp(matches.replace(/\./g, "\\."), "g"); + + const result = data.replace(reg, newVersion); + + fs.writeFile(file, result, "utf8", function (err) { + if (err) return console.log(err); + }); + }); +} + +// ------------------------------------------------------------ + +const someFiles = ["README.md"]; + +someFiles.forEach((file) => { + updateVersion(file, newVersion); +}); diff --git a/sources/index.js b/sources/index.js index 66bef89..cb93251 100644 --- a/sources/index.js +++ b/sources/index.js @@ -1,5 +1,12 @@ -import { addRemoveClass, getNumber, htmlSubstr } from './utils/function'; -import defaultRegex from './utils/regex'; +import { + addRemoveClass, + createElement, + getNumber, + htmlSubstr, + setAttributes, +} from "./utils/function"; +import defaultRegex from "./utils/regex"; +import defaultOptions from "./utils/defaults"; /** * @class ShowMore @@ -13,47 +20,31 @@ export default class ShowMore { */ constructor(className, { onMoreLess = () => {}, regex = {}, config } = {}) { // all html elements - const elements = document.querySelectorAll(className); + const elements = [].slice.call(document.querySelectorAll(className)); // colback function - this.onMoreLess = onMoreLess; + this._onMoreLess = onMoreLess; // global regex - this.regex = Object.assign(defaultRegex, regex); - - for (let i = 0; i < elements.length; i++) { - const { - type, - limit, - element, - after, - more, - less, - number, - ellipsis, - btnClass, - btnClassAppend, - } = JSON.parse(elements[i].getAttribute('data-config')) || config; - - // create global object - this.object = { - index: i, - element: elements[i], - type, - limit, - classArray: elements[i].classList, - ellipsis, - typeElement: element || 'span', - more: more || false, - less: less || false, - number: number || false, - after: after || 0, - btnClass: btnClass || 'show-more-btn', - btnClassAppend: btnClassAppend || null, + this._regex = { ...defaultRegex, ...regex }; + + elements.map((item, index) => { + const configData = JSON.parse(item.getAttribute("data-config")); + const configGlobal = config; + + const configDataAndGlobal = { ...configGlobal, ...configData }; + + this._object = { + index, + classArray: item.classList, + ...defaultOptions, + ...configDataAndGlobal, + typeElement: configDataAndGlobal.element || "span", + element: item, }; - this.initial(this.object); - } + this._initial(); + }); } /** @@ -61,61 +52,66 @@ export default class ShowMore { * * @param {Object} object */ - initial({ element, after, ellipsis, limit, type }) { + _initial = () => { + const { element, after, ellipsis, nobutton, limit, type } = this._object; // set default aria-expande to false - element.setAttribute('aria-expanded', 'false'); + setAttributes(element, { "aria-expanded": "false" }); const limitCounts = limit + after; - const ellips = ellipsis === false ? '' : '...'; + const ellips = ellipsis === false ? "" : "..."; + + console.log(nobutton); // text - if (type === 'text') { - let truncatedText = ''; + if (type === "text") { const originalText = element.innerHTML.trim(); - let elementText = element.textContent.trim(); - - let orgTexReg = originalText; - for (let key in this.regex) { - const { match, replace } = this.regex[key]; - if (key && match) orgTexReg = orgTexReg.replace(match, replace); - } + const elementText = element.textContent.trim(); if (elementText.length > limitCounts) { - truncatedText = htmlSubstr(orgTexReg, limit).concat(ellips); + let orgTexReg = originalText; + + for (let key in this._regex) { + const { match, replace } = this._regex[key]; + if (key && match) orgTexReg = orgTexReg.replace(match, replace); + } - element.innerHTML = truncatedText; + const truncatedText = htmlSubstr(orgTexReg, limit - 1).concat(ellips); - this.addBtn(this.object); + element.textContent = ""; + element.insertAdjacentHTML("beforeend", truncatedText); - this.clickEvent(element, { - ...this.object, + this._clickEvent(element, { + ...this._object, originalText, truncatedText, }); + + if (nobutton) return; + this._addBtn(this._object); } } // list and table - if (type === 'list' || type === 'table') { - const items = - type === 'list' ? [].slice.call(element.children) : element.rows; + if (type === "list" || type === "table") { + const items = this._getNumberCount(element, type); if (items.length > limitCounts) { for (let i = limit; i < items.length; i++) { addRemoveClass(items[i], true); } - // add button to the list and table - this.addBtn(this.object); - // add event click - this.clickEvent( - type === 'list' ? element : element.nextElementSibling, - this.object + this._clickEvent( + type === "list" ? element : element.nextElementSibling, + this._object ); + + if (nobutton) return; + // add button to the list and table + this._addBtn(this._object); } } - } + }; /** * Event click @@ -123,9 +119,8 @@ export default class ShowMore { * @param {HTMLElement} element * @param {Object} object */ - clickEvent(element, object) { - element.addEventListener('click', this.handleEvent.bind(this, object)); - } + _clickEvent = (element, object) => + element.addEventListener("click", this._handleEvent.bind(this, object)); /** * Create button @@ -133,20 +128,34 @@ export default class ShowMore { * @param {Object} object * @returns HTMLElement */ - createBtn({ element, number, less, more, type, btnClass, btnClassAppend }) { - const typeAria = this.checkExp ? less || '' : more || ''; - const label = this.checkExp ? 'collapse' : 'expand'; - const expanded = this.checkExp ? true : false; - - const btn = document.createElement('button'); - btn.className = - btnClassAppend == null ? btnClass : btnClass + ' ' + btnClassAppend; - btn.setAttribute('aria-expanded', expanded); - btn.setAttribute('aria-label', label); - btn.setAttribute('tabindex', 0); - btn.innerHTML = number ? typeAria + getNumber(element, type) : typeAria; - return btn; - } + _createBtn = ({ + element, + number, + less, + more, + type, + btnClass, + btnClassAppend, + }) => { + const typeAria = this._checkExp ? less || "" : more || ""; + const label = this._checkExp ? "collapse" : "expand"; + const expanded = this._checkExp ? true : false; + + const button = createElement("button"); + button.className = + btnClassAppend == null ? btnClass : btnClass + " " + btnClassAppend; + setAttributes(button, { + "aria-expanded": expanded, + "aria-label": label, + tabindex: 0, + }); + + button.insertAdjacentHTML( + "beforeend", + number ? typeAria + getNumber(element, type) : typeAria + ); + return button; + }; /** * Event handler @@ -154,7 +163,7 @@ export default class ShowMore { * @param {Object} object * @param {Event} event */ - handleEvent(object, { currentTarget, target }) { + _handleEvent = (object, { target }) => { const { element, type, @@ -171,33 +180,37 @@ export default class ShowMore { if (!checkContainsClass) return; - const ariaExpanded = element.getAttribute('aria-expanded'); - this.checkExp = ariaExpanded === 'false'; + const ariaExpanded = element.getAttribute("aria-expanded"); + this._checkExp = ariaExpanded === "false"; // -------------------------------------------------- // text - if (type === 'text' && checkContainsClass) { - element.innerHTML = ''; - element.innerHTML = this.checkExp ? originalText : truncatedText; + if (type === "text" && checkContainsClass) { + element.textContent = ""; + + element.insertAdjacentHTML( + "beforeend", + this._checkExp ? originalText : truncatedText + ); if (less) { - const el = document.createElement(typeElement); - el.insertAdjacentElement('beforeend', this.createBtn(object)); + const el = createElement(typeElement); + el.classList.add("show-more-wrapper"); + el.insertAdjacentElement("beforeend", this._createBtn(object)); element.appendChild(el); } } // -------------------------------------------------- // list and table - if (type === 'list' || type === 'table') { - const items = - type === 'list' ? [].slice.call(currentTarget.children) : element.rows; + if (type === "list" || type === "table") { + const items = this._getNumberCount(element, type); for (let i = 0; i < items.length; i++) { const typeRemove = - type === 'list' ? i >= limit && i < items.length - 1 : i >= limit; + type === "list" ? i >= limit && i < items.length - 1 : i >= limit; - if (ariaExpanded === 'false') { + if (ariaExpanded === "false") { addRemoveClass(items[i]); } else if (typeRemove) { addRemoveClass(items[i], true); @@ -206,60 +219,65 @@ export default class ShowMore { } // set aria-expanded - if (type === 'table' || type === 'list' || type === 'text') { - this.setExpand({ ...object, target }); + if (type) { + this._setExpand({ ...object, target }); } - } + }; + + _getNumberCount = (element, type) => { + return type === "list" ? [].slice.call(element.children) : element.rows; + }; /** * Add button * * @param {Object} object */ - addBtn(object) { + _addBtn = (object) => { const { type, element, more, typeElement } = object; if (!more) return; - if (type === 'table') { - element.insertAdjacentElement('afterend', this.createBtn(object)); + if (type === "table") { + element.insertAdjacentElement("afterend", this._createBtn(object)); } else { - const el = document.createElement(typeElement); - el.appendChild(this.createBtn(object)); + const el = createElement(typeElement); + el.classList.add("show-more-wrapper"); + el.appendChild(this._createBtn(object)); element.appendChild(el); } - } + }; /** * Set aria-expanded * * @param {Object} object */ - setExpand(object) { + _setExpand = (object) => { const { element, type, less, more, number, target } = object; - const check = this.checkExp; - - const typeAria = check ? less : more; - const aria = check ? 'expand' : 'collapse'; - const ariaText = type === 'table' ? type : `the ${type}`; + const typeAria = this._checkExp ? less : more; + const aria = this._checkExp ? "expand" : "collapse"; + const ariaText = type === "table" ? type : `the ${type}`; const lastChild = element.lastElementChild; - element.setAttribute('aria-expanded', check); - target.setAttribute('aria-expanded', check); - target.setAttribute('aria-label', `${aria} ${ariaText}`); + setAttributes(element, { "aria-expanded": this._checkExp }); + setAttributes(target, { + "aria-expanded": this._checkExp, + "aria-label": `${aria} ${ariaText}`, + }); // callback function on more/less - this.onMoreLess(aria, object); + this._onMoreLess(aria, object); if (typeAria) { target.innerHTML = number ? typeAria + getNumber(element, type) : typeAria; - } else if (type === 'table') { + } else if (type === "table") { target.parentNode.removeChild(target); - } else if (type === 'list') { + } else if (type === "list") { lastChild.parentNode.removeChild(lastChild); } - } + }; } diff --git a/sources/scss/global.scss b/sources/scss/global.scss index 18eb3c7..9d8b5cf 100644 --- a/sources/scss/global.scss +++ b/sources/scss/global.scss @@ -1,4 +1,4 @@ -@import './modules/table'; +@import "./modules/table"; *, *:after, @@ -19,7 +19,7 @@ html { body { display: flex; justify-content: center; - font-family: 'Lato', sans-serif; + font-family: "Lato", sans-serif; font-size: 18px; line-height: 150%; } @@ -90,7 +90,7 @@ tfoot th { text-align: left; } -html[data-useragent*='IE'] .example-html { +html[data-useragent*="IE"] .example-html { display: none; } @@ -121,7 +121,7 @@ html[data-useragent*='IE'] .example-html { color: #681bcc; &:after { - content: ''; + content: ""; display: block; margin: auto; height: 2px; @@ -147,11 +147,11 @@ pre { overflow: auto; margin: 0px auto -50px; padding: 0; + // font-size: 1rem; code { line-height: 130%; - border-radius: 4px; - // margin: 20px 0; + border-radius: 5px; } } @@ -163,7 +163,7 @@ h5 { &:before { position: absolute; - content: '\27A4'; + content: "\27A4"; left: 0; top: 19px; color: #b152ff; @@ -199,7 +199,7 @@ h5 { position: absolute; top: -1px; right: -1px; - content: '↑'; + content: "↑"; } &:hover { color: red; @@ -249,7 +249,7 @@ p { background-color: #fff; &::after { - content: ''; + content: ""; background-color: #ffe54c; width: 100%; z-index: -1; diff --git a/sources/utils/defaults.js b/sources/utils/defaults.js new file mode 100644 index 0000000..48bbeb2 --- /dev/null +++ b/sources/utils/defaults.js @@ -0,0 +1,12 @@ +const defaultOptions = { + typeElement: "span", + more: false, + less: false, + number: false, + nobutton: false, + after: 0, + btnClass: "show-more-btn", + btnClassAppend: null, +}; + +export default defaultOptions; diff --git a/sources/utils/function.js b/sources/utils/function.js index 3a5847a..88b45c0 100644 --- a/sources/utils/function.js +++ b/sources/utils/function.js @@ -5,14 +5,14 @@ * @param {String} type - type of element table | div * @returns */ -function getNumber({ rows, children }, type) { - const elementType = type === 'table' ? rows : children; +const getNumber = ({ rows, children }, type) => { + const elementType = type === "table" ? rows : children; const numbersElementHidden = [].slice .call(elementType) - .filter((el) => el.className === 'hidden').length; - return numbersElementHidden !== 0 ? ` ${numbersElementHidden}` : ''; -} + .filter((el) => el.className === "hidden").length; + return numbersElementHidden !== 0 ? ` ${numbersElementHidden}` : ""; +}; // https://stackoverflow.com/questions/6003271/substring-text-with-html-tags-in-javascript /** @@ -22,9 +22,9 @@ function getNumber({ rows, children }, type) { * @param {String} count - limit of characters * @returns */ -function htmlSubstr(originalText, count) { - let div = document.createElement('div'); - div.innerHTML = originalText; +const htmlSubstr = (originalText, count) => { + let div = createElement("div"); + div.insertAdjacentHTML("afterbegin", originalText); walk(div, track); @@ -36,7 +36,7 @@ function htmlSubstr(originalText, count) { el.data = el.substringData(0, el.data.length + count); } } else { - el.data = ''; + el.data = ""; } } @@ -51,7 +51,7 @@ function htmlSubstr(originalText, count) { } while ((node = node.nextSibling)); } return div.innerHTML; -} +}; /** * Add/remove class 'hidden' to element @@ -59,8 +59,28 @@ function htmlSubstr(originalText, count) { * @param {HTMLElement} element * @param {String} type - type of element add or remove */ -function addRemoveClass(element, type = false) { - element.classList[type ? 'add' : 'remove']('hidden'); -} +const addRemoveClass = (element, type = false) => { + return element.classList[type ? "add" : "remove"]("hidden"); +}; -export { addRemoveClass, getNumber, htmlSubstr }; +/** + * Set attributes to element + * + * @param {HTMLElement} el + * @param {Object} object + */ +const setAttributes = (el, object) => { + for (let key in object) { + el.setAttribute(key, object[key]); + } +}; + +/** + * Create element + * + * @param {String} type - type of element + * @returns {HTMLElement} + */ +const createElement = (type) => document.createElement(type); + +export { addRemoveClass, createElement, getNumber, htmlSubstr, setAttributes }; diff --git a/sources/utils/regex.js b/sources/utils/regex.js index 617c6a7..76f3d49 100644 --- a/sources/utils/regex.js +++ b/sources/utils/regex.js @@ -4,19 +4,19 @@ const defaultRegex = { newLine: { match: /(\r\n|\n|\r)/gm, - replace: '', + replace: "", }, space: { match: /\s\s+/gm, - replace: ' ', + replace: " ", }, br: { match: //gim, - replace: '', + replace: "", }, html: { match: /(<((?!b|\/b|!strong|\/strong)[^>]+)>)/gi, - replace: '', + replace: "", }, };