avvo-styleguide
Version:
Avvo styleguide
105 lines (79 loc) • 2.36 kB
JavaScript
/* ========================================================================
* Avvo UI - alert.js
* ========================================================================
* Forked from Bootstrap alert.js v3.2.0
* http://getbootstrap.com/javascript/#alerts
* ========================================================================
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
const $ = global.jQuery
// ALERT CLASS DEFINITION
// ======================
const dismiss = '[data-dismiss="alert"]'
const Alert = function (el) {
$(el).on('click', dismiss, this.close)
}
Alert.VERSION = '4.0.0'
Alert.prototype.close = function (e) {
const $this = $(this)
let selector = $this.attr('data-target')
if (!selector) {
selector = $this.attr('href')
}
let $parent = $(selector)
if (e) {
e.preventDefault()
}
if (!$parent.length) {
$parent = $this.hasClass('alert') ? $this : $this.closest('.alert')
}
const closeEvent = $.Event('close.ui.alert')
$parent.trigger(closeEvent)
if (closeEvent.isDefaultPrevented()) {
return
}
$parent.removeClass('in')
function removeElement() {
// detach from parent, fire event then clean up data
$parent.detach().trigger('closed.ui.alert').remove()
}
if (!$.support.transition) {
removeElement()
} else if ($parent.hasClass('fade')) {
$parent
.one('uiTransitionEnd', removeElement)
.emulateTransitionEnd(150)
} else {
$parent.slideUp(200, removeElement)
}
}
// ALERT PLUGIN DEFINITION
// =======================
function Plugin(option) {
return this.each(function () {
const $this = $(this)
let data = $this.data('ui.alert')
if (!data) {
data = new Alert(this)
$this.data('ui.alert', data)
}
if (typeof option === 'string') {
data[option].call($this)
}
})
}
export function init() {
const old = $.fn.alert
$.fn.alert = Plugin
$.fn.alert.Constructor = Alert
// ALERT NO CONFLICT
// =================
$.fn.alert.noConflict = function () {
$.fn.alert = old
return this
}
// ALERT DATA-API
// ==============
$(document).on('click.ui.alert.data-api', dismiss, Alert.prototype.close)
}