|
| 1 | +export default { |
| 2 | + attachAttributes (el, binding) { |
| 3 | + let item = document.getElementById(binding.arg) |
| 4 | + item.style.overflow = 'hidden' |
| 5 | + item.classList.add('collapse') |
| 6 | + if (!binding.modifiers.show && !binding.value) { |
| 7 | + el.classList.add('collapsed') |
| 8 | + el.setAttribute('aria-expanded', 'false') |
| 9 | + } else { |
| 10 | + el.setAttribute('aria-expanded', 'true') |
| 11 | + item.classList.add('show') |
| 12 | + } |
| 13 | + }, |
| 14 | + inserted (el, binding) { |
| 15 | + binding.def.attachAttributes(el, binding) |
| 16 | + if(binding.value == undefined) |
| 17 | + el.addEventListener('click', () => binding.def.toggle(el, binding)) |
| 18 | + }, |
| 19 | + update (el, binding) { |
| 20 | + if(binding.oldValue !== binding.value) |
| 21 | + binding.def.toggle(el, binding) |
| 22 | + }, |
| 23 | + toggle (el, binding) { |
| 24 | + let item = document.getElementById(binding.arg) |
| 25 | + let duration = el.dataset.duration ? Number(el.dataset.duration) : 400 |
| 26 | + if(item.classList.contains('show')) |
| 27 | + binding.def.hide(item, el, duration) |
| 28 | + else |
| 29 | + binding.def.show(item, el, duration) |
| 30 | + }, |
| 31 | + hide(item, el, duration){ |
| 32 | + item.style.transition = `all ${duration}ms ease-in-out` |
| 33 | + item.style.height = item.scrollHeight + 'px' |
| 34 | + setTimeout(function () { |
| 35 | + item.style.paddingTop = 0 |
| 36 | + item.style.paddingBottom = 0 |
| 37 | + item.style.height = 0 |
| 38 | + }, 1) |
| 39 | + setTimeout(function () { |
| 40 | + item.classList.remove('show'); |
| 41 | + item.style.transition = '' |
| 42 | + item.style.paddingTop = '' |
| 43 | + item.style.paddingBottom = '' |
| 44 | + el.setAttribute('aria-expanded', 'false') |
| 45 | + el.classList.add('collapsed') |
| 46 | + item.style.height = '' |
| 47 | + }, duration) |
| 48 | + }, |
| 49 | + show (item, el, duration) { |
| 50 | + var getHeight = function () { |
| 51 | + item.style.display = 'block' |
| 52 | + let style = [] |
| 53 | + style['height'] = item.scrollHeight + 'px' |
| 54 | + style['pt'] = window.getComputedStyle(item).paddingTop |
| 55 | + style['pb'] = window.getComputedStyle(item).paddingBottom |
| 56 | + item.style.display = '' |
| 57 | + return style |
| 58 | + } |
| 59 | + var style = getHeight() |
| 60 | + item.style.height = 0 |
| 61 | + item.style.paddingTop = 0 |
| 62 | + item.style.paddingBottom = 0 |
| 63 | + item.classList.add('show') |
| 64 | + |
| 65 | + setTimeout(function () { |
| 66 | + item.style.transition = `all ${duration}ms ease-in-out` |
| 67 | + item.style.height = style['height'] |
| 68 | + item.style.paddingTop = style['pt'] |
| 69 | + item.style.paddingBottom = style['pb'] |
| 70 | + }, 1); |
| 71 | + setTimeout(function () { |
| 72 | + item.style.height = '' |
| 73 | + item.style.transition = '' |
| 74 | + el.setAttribute('aria-expanded', 'true') |
| 75 | + el.classList.remove('collapsed') |
| 76 | + }, duration) |
| 77 | + } |
| 78 | +} |
0 commit comments