|
| 1 | +/*! |
| 2 | + * @moyu/pointcss |
| 3 | + * @version v2.0.0-alpha.13 |
| 4 | + * @link https://github.com/moyus/pointcss |
| 5 | + * @author moyu <moyuboy@gmail.com> (https://moyu.io) |
| 6 | + * @license MIT |
| 7 | + */ |
| 8 | +!(function () { |
| 9 | + /*----------------------------------------------------------------------------*/ |
| 10 | + /* Helpers |
| 11 | + /*----------------------------------------------------------------------------*/ |
| 12 | + var forEach = function (items, fn) { |
| 13 | + var len = items.length |
| 14 | + for (var i = 0; i < len; i++) { |
| 15 | + fn(items[i], i, items) |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + /*----------------------------------------------------------------------------*/ |
| 20 | + /* Toggle |
| 21 | + /*----------------------------------------------------------------------------*/ |
| 22 | + function Toggle(elementOrSelector) { |
| 23 | + if (typeof elementOrSelector === 'string') { |
| 24 | + this.trigger = document.querySelector(elementOrSelector) |
| 25 | + } else if (elementOrSelector instanceof Element) { |
| 26 | + this.trigger = elementOrSelector |
| 27 | + } else { |
| 28 | + throw Error('Invalid trigger selector or element!') |
| 29 | + } |
| 30 | + |
| 31 | + if (!this.trigger) { |
| 32 | + throw Error('Trigger element does not exist!') |
| 33 | + } |
| 34 | + |
| 35 | + var targetId = this.trigger.dataset.target |
| 36 | + var toggleServiceName = this.trigger.dataset.toggle |
| 37 | + var enterServiceName = this.trigger.dataset.enter |
| 38 | + var leaveServiceName = this.trigger.dataset.leave |
| 39 | + |
| 40 | + if ( |
| 41 | + typeof toggleServiceName !== 'undefined' || |
| 42 | + typeof enterServiceName !== 'undefined' || |
| 43 | + typeof leaveServiceName !== 'undefined' |
| 44 | + ) { |
| 45 | + if (toggleServiceName) { |
| 46 | + this.type = 'toggle' |
| 47 | + this.service = Toggle.services[toggleServiceName] |
| 48 | + } else if (leaveServiceName) { |
| 49 | + this.type = 'leave' |
| 50 | + this.service = Toggle.services[leaveServiceName] |
| 51 | + } else if (enterServiceName) { |
| 52 | + this.type = 'enter' |
| 53 | + this.service = Toggle.services[enterServiceName] |
| 54 | + } |
| 55 | + } else { |
| 56 | + this.type = 'toggle' |
| 57 | + this.service = Toggle.services.default |
| 58 | + } |
| 59 | + |
| 60 | + if (typeof targetId !== 'undefined') { |
| 61 | + this.target = document.getElementById(targetId) |
| 62 | + } else { |
| 63 | + this.target = this.trigger |
| 64 | + } |
| 65 | + |
| 66 | + if (!this.service || !this.target || this.trigger.__toggler__) return |
| 67 | + |
| 68 | + var self = this; |
| 69 | + function listener(e) { |
| 70 | + if (self.type === 'toggle') { |
| 71 | + self.toggle(e) |
| 72 | + } else if (self.type === 'enter') { |
| 73 | + self.enter(e) |
| 74 | + } else if (self.type === 'leave') { |
| 75 | + self.leave(e) |
| 76 | + } |
| 77 | + } |
| 78 | + this.trigger.addEventListener('click', listener) |
| 79 | + this.listener = listener |
| 80 | + this.trigger.__toggler__ = this |
| 81 | + if (this.service.init) { |
| 82 | + this.service.init.call(this); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + Toggle.prototype.destroy = function () { |
| 87 | + if (this.service.destroy) { |
| 88 | + this.service.destroy.call(this); |
| 89 | + } |
| 90 | + this.trigger.removeEventListener('click', this.listener) |
| 91 | + this.trigger.__toggler__ = null |
| 92 | + this.trigger = null |
| 93 | + this.target = null |
| 94 | + this.listener = null |
| 95 | + } |
| 96 | + |
| 97 | + Toggle.prototype.toggle = function (e) { |
| 98 | + this.service.render.call(this, 'toggle', e) |
| 99 | + } |
| 100 | + |
| 101 | + Toggle.prototype.enter = function (e) { |
| 102 | + this.service.render.call(this, 'enter', e) |
| 103 | + } |
| 104 | + |
| 105 | + Toggle.prototype.leave = function (e) { |
| 106 | + this.service.render.call(this, 'leave', e) |
| 107 | + } |
| 108 | + |
| 109 | + Toggle.services = { |
| 110 | + default: { |
| 111 | + render: function (action) { |
| 112 | + if (action === 'leave') { |
| 113 | + this.target.classList.remove('is-active') |
| 114 | + } else if (action === 'enter') { |
| 115 | + this.target.classList.add('is-active') |
| 116 | + } else { |
| 117 | + this.target.classList.toggle('is-active') |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + Toggle.init = function init(elementOrSelector) { |
| 124 | + if (!elementOrSelector) { |
| 125 | + forEach(document.querySelectorAll('[data-toggle]'), function (trigger) { |
| 126 | + new Toggle(trigger) |
| 127 | + }) |
| 128 | + forEach(document.querySelectorAll('[data-enter]'), function (trigger) { |
| 129 | + new Toggle(trigger) |
| 130 | + }) |
| 131 | + forEach(document.querySelectorAll('[data-leave]'), function (trigger) { |
| 132 | + new Toggle(trigger) |
| 133 | + }) |
| 134 | + } else { |
| 135 | + new Toggle(elementOrSelector) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + Toggle.register = function register(option) { |
| 140 | + Toggle.services[option.name] = { |
| 141 | + init: option.init, |
| 142 | + render: option.render |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /*----------------------------------------------------------------------------*/ |
| 147 | + /* Modal |
| 148 | + /*----------------------------------------------------------------------------*/ |
| 149 | + Toggle.register({ |
| 150 | + name: 'modal', |
| 151 | + render: function(action) { |
| 152 | + if (action === 'leave') { |
| 153 | + this.target.classList.add('hidden') |
| 154 | + } else if (action === 'enter') { |
| 155 | + this.target.classList.remove('hidden') |
| 156 | + } else { |
| 157 | + this.target.classList.toggle('hidden') |
| 158 | + } |
| 159 | + } |
| 160 | + }) |
| 161 | + |
| 162 | + /*----------------------------------------------------------------------------*/ |
| 163 | + /* Dropdown |
| 164 | + /*----------------------------------------------------------------------------*/ |
| 165 | + Toggle.register({ |
| 166 | + name: 'dropdown', |
| 167 | + init: function() { |
| 168 | + var self = this; |
| 169 | + if (this.target !== this.trigger) return |
| 170 | + var sibling = this.trigger.parentNode.firstElementChild |
| 171 | + while(sibling) { |
| 172 | + if (sibling !== this.trigger && sibling.classList.contains('dropdown__content')) { |
| 173 | + this.target = sibling; |
| 174 | + break; |
| 175 | + } |
| 176 | + sibling = sibling.nextElementSibling |
| 177 | + } |
| 178 | + |
| 179 | + function clickoutHandler(e) { |
| 180 | + if (!self.target.contains(e.target) && !self.trigger.contains(e.target)) { |
| 181 | + self.leave() |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + document.addEventListener('click', clickoutHandler) |
| 186 | + this.clickoutHandler = clickoutHandler |
| 187 | + }, |
| 188 | + destroy: function() { |
| 189 | + document.removeEventListener('click', this.clickoutHandler) |
| 190 | + }, |
| 191 | + render: function(action) { |
| 192 | + if (action === 'leave') { |
| 193 | + this.target.classList.add('hidden') |
| 194 | + } else if (action === 'enter') { |
| 195 | + this.target.classList.remove('hidden') |
| 196 | + } else { |
| 197 | + this.target.classList.toggle('hidden') |
| 198 | + } |
| 199 | + } |
| 200 | + }) |
| 201 | + |
| 202 | + /*----------------------------------------------------------------------------*/ |
| 203 | + /* Menu |
| 204 | + /*----------------------------------------------------------------------------*/ |
| 205 | + Toggle.register({ |
| 206 | + name: 'menu', |
| 207 | + render: function(action, e) { |
| 208 | + if (!e) return |
| 209 | + |
| 210 | + var el = e.target |
| 211 | + while (el && el !== e.currentTarget) { |
| 212 | + if (el.classList.contains('menu__link')) { |
| 213 | + if ( |
| 214 | + el.nextElementSibling && |
| 215 | + el.nextElementSibling.classList.contains('menu__list') |
| 216 | + ) { |
| 217 | + if (action === 'leave') { |
| 218 | + el.parentNode.classList.remove('is-active') |
| 219 | + } else if (action === 'enter') { |
| 220 | + el.parentNode.classList.add('is-active') |
| 221 | + } else { |
| 222 | + el.parentNode.classList.toggle('is-active') |
| 223 | + } |
| 224 | + } |
| 225 | + break; |
| 226 | + } |
| 227 | + |
| 228 | + el = el.parentNode |
| 229 | + } |
| 230 | + } |
| 231 | + }) |
| 232 | + |
| 233 | + Toggle.init() |
| 234 | + window.Toggle = Toggle |
| 235 | + |
| 236 | +})(); |
0 commit comments