All files / src class-toggler.js

85.96% Statements 98/114
77.78% Branches 28/36
81.82% Functions 27/33
85.98% Lines 92/107

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293                                          1x 1x 1x 1x 1x   1x                 1x                 1x   1x 1x 1x 1x   1x                   14x 14x   14x                   14x       14x         6x 6x   6x 7x 7x         22x 22x   22x 24x 17x 52x 52x     7x 7x           9x 9x   9x 4x 4x   20x 3x 15x 15x 15x     1x 1x 1x       5x 6x 3x 3x 3x   3x 3x 3x             3x 3x       3x           37x       37x       37x       88x             88x       24x 120x       24x 120x       24x 87x                   14x           14x           14x       15x 15x   15x 14x     15x 15x       15x                                 1x 15x 15x 15x   15x       15x 2x     15x 4x     15x 6x     15x 3x       1x                 1x                      
/**
 * --------------------------------------------------------------------------
 * CoreUI (v3.3.0): class-toggler.js
 * Licensed under MIT (https://coreui.io/license)
 * --------------------------------------------------------------------------
 */
 
import {
  getjQuery,
  typeCheckConfig
} from './util/index'
import Data from './dom/data'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
 
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */
 
const NAME = 'class-toggler'
const VERSION = '3.2.2'
const DATA_KEY = 'coreui.class-toggler'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
 
const DefaultType = {
  addClass: '(null|array|string)',
  breakpoints: '(null|array|string)',
  removeClass: '(null|array|string)',
  responsive: '(null|boolean)',
  target: '(null|string)',
  toggleClass: '(null|array|string)'
}
 
const Default = {
  addClass: null,
  breakpoints: ['', 'sm', 'md', 'lg', 'xl'],
  removeClass: null,
  responsive: false,
  target: 'body',
  toggleClass: null
}
 
const CLASS_NAME_CLASS_TOGGLER = 'c-class-toggler'
 
const EVENT_CLASS_ADDED = 'classadded'
const EVENT_CLASS_REMOVED = 'classremoved'
const EVENT_CLASS_TOGGLE = 'classtoggle'
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
 
const SELECTOR_CLASS_TOGGLER = '.c-class-toggler'
 
/**
 * ------------------------------------------------------------------------
 * Class Definition
 * ------------------------------------------------------------------------
 */
 
class ClassToggler {
  constructor(element, config) {
    this._element = element
    this._config = this._getConfig(config)
 
    Data.setData(element, DATA_KEY, this)
  }
 
  // Getters
 
  static get VERSION() {
    return VERSION
  }
 
  static get Default() {
    return Default
  }
 
  static get DefaultType() {
    return DefaultType
  }
 
  // Public
  add() {
    const target = this._target()
    const classNames = this._config.addClass.replace(/\s/g, '').split(',')
 
    classNames.forEach(className => {
      target.classList.add(className)
      this._customEvent(EVENT_CLASS_ADDED, target, true, className)
    })
  }
 
  remove() {
    const target = this._target()
    const classNames = this._config.removeClass.replace(/\s/g, '').split(',')
 
    classNames.forEach(className => {
      if (this._config.responsive) {
        this._updateResponsiveClassNames(className).forEach(className => {
          target.classList.remove(className)
          this._customEvent(EVENT_CLASS_REMOVED, target, false, className)
        })
      } else {
        target.classList.remove(className)
        this._customEvent(EVENT_CLASS_REMOVED, target, false, className)
      }
    })
  }
 
  toggle() {
    const target = this._target()
    const classNames = this._config.toggleClass.replace(/\s/g, '').split(',')
 
    if (this._config.responsive) {
      classNames.forEach(className => {
        const responsiveClassNames = this._updateResponsiveClassNames(className)
 
        if (responsiveClassNames.filter(className => target.classList.contains(className)).length) {
          this._updateResponsiveClassNames(className).forEach(className => {
            this._config.removeClass = className
            this.remove()
            this._customEvent(EVENT_CLASS_TOGGLE, target, false, className)
          })
        } else {
          this._config.addClass = className
          this.add()
          this._customEvent(EVENT_CLASS_TOGGLE, target, true, className)
        }
      })
    } else {
      classNames.forEach(className => {
        if (target.classList.contains(className)) {
          this._config.removeClass = className
          this.remove()
          this._customEvent(EVENT_CLASS_TOGGLE, target, false, className)
        } else {
          this._config.addClass = className
          this.add()
          this._customEvent(EVENT_CLASS_TOGGLE, target, true, className)
        }
      })
    }
  }
 
  class() {
    this._config.toggleClass = this._config.class
    Iif (this._element.getAttribute('responsive')) {
      this._config.responsive = this._element.getAttribute('responsive')
    }
 
    this.toggle()
  }
 
  // Private
 
  _target() {
    Iif (this._config.target === 'body') {
      return document.querySelector(this._config.target)
    }
 
    Iif (this._config.target === '_parent') {
      return this._element.parentNode
    }
 
    return document.querySelector(this._config.target)
  }
 
  _customEvent(eventName, target, add, className) {
    const event = new CustomEvent(eventName, {
      detail: {
        target,
        add,
        className
      }
    })
    target.dispatchEvent(event)
  }
 
  _breakpoint(className) {
    const { breakpoints } = this._config
    return breakpoints.filter(breakpoint => breakpoint.length > 0).filter(breakpoint => className.includes(breakpoint))[0]
  }
 
  _breakpoints(className) {
    const { breakpoints } = this._config
    return breakpoints.slice(0, breakpoints.indexOf(breakpoints.filter(breakpoint => breakpoint.length > 0).filter(breakpoint => className.includes(breakpoint))[0]) + 1)
  }
 
  _updateResponsiveClassNames(className) {
    const bp = this._breakpoint(className)
    return this._breakpoints(className).map(breakpoint => breakpoint.length > 0 ? className.replace(bp, breakpoint) : className.replace(`-${bp}`, breakpoint))
  }
 
  _includesResponsiveClass(className) {
    return this._updateResponsiveClassNames(className).filter(className => this._config.target.contains(className))
  }
 
  // Static
 
  _getConfig(config) {
    config = {
      ...this.constructor.Default,
      ...Manipulator.getDataAttributes(this._element),
      ...config
    }
 
    typeCheckConfig(
      NAME,
      config,
      this.constructor.DefaultType
    )
 
    return config
  }
 
  static classTogglerInterface(element, config) {
    let data = Data.getData(element, DATA_KEY)
    const _config = typeof config === 'object' && config
 
    if (!data) {
      data = new ClassToggler(element, _config)
    }
 
    Eif (typeof config === 'string') {
      Iif (typeof data[config] === 'undefined') {
        throw new TypeError(`No method named "${config}"`)
      }
 
      data[config]()
    }
  }
 
  static jQueryInterface(config) {
    return this.each(function () {
      ClassToggler.classTogglerInterface(this, config)
    })
  }
}
 
/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
 
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_CLASS_TOGGLER, event => {
  event.preventDefault()
  event.stopPropagation()
  let toggler = event.target
 
  Iif (!toggler.classList.contains(CLASS_NAME_CLASS_TOGGLER)) {
    toggler = toggler.closest(SELECTOR_CLASS_TOGGLER)
  }
 
  if (typeof toggler.dataset.addClass !== 'undefined') {
    ClassToggler.classTogglerInterface(toggler, 'add')
  }
 
  if (typeof toggler.dataset.removeClass !== 'undefined') {
    ClassToggler.classTogglerInterface(toggler, 'remove')
  }
 
  if (typeof toggler.dataset.toggleClass !== 'undefined') {
    ClassToggler.classTogglerInterface(toggler, 'toggle')
  }
 
  if (typeof toggler.dataset.class !== 'undefined') {
    ClassToggler.classTogglerInterface(toggler, 'class')
  }
})
 
const $ = getjQuery()
 
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
 * add .c-class-toggler to jQuery only if jQuery is present
 */
 
Iif ($) {
  const JQUERY_NO_CONFLICT = $.fn[NAME]
  $.fn[NAME] = ClassToggler.jQueryInterface
  $.fn[NAME].Constructor = ClassToggler
  $.fn[NAME].noConflict = () => {
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return ClassToggler.jQueryInterface
  }
}
 
export default ClassToggler