2015-05-28 20:33:49 +10:00
|
|
|
/*
|
2015-10-17 10:51:40 +11:00
|
|
|
* The flash message.
|
2014-05-14 23:24:20 +10:00
|
|
|
*
|
2015-10-17 10:51:40 +11:00
|
|
|
* Documentation: ../docs/flashmessage.md
|
2014-05-14 23:24:20 +10:00
|
|
|
*
|
2015-10-17 10:51:40 +11:00
|
|
|
* Require:
|
|
|
|
* - bootstrap/transition
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
+function ($) { "use strict";
|
|
|
|
|
|
|
|
var FlashMessage = function (options, el) {
|
2014-07-14 18:25:40 +10:00
|
|
|
var
|
2014-05-14 23:24:20 +10:00
|
|
|
options = $.extend({}, FlashMessage.DEFAULTS, options),
|
2014-07-14 18:25:40 +10:00
|
|
|
$element = $(el)
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
$('body > p.flash-message').remove()
|
|
|
|
|
2016-04-27 04:44:02 +10:00
|
|
|
if ($element.length == 0) {
|
2016-11-18 07:41:02 +11:00
|
|
|
$element = $('<p />').addClass(options.class).html(options.text)
|
2016-04-27 04:44:02 +10:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-07-14 18:25:40 +10:00
|
|
|
$element.addClass('flash-message fade')
|
2014-05-14 23:24:20 +10:00
|
|
|
$element.attr('data-control', null)
|
2014-07-14 18:25:40 +10:00
|
|
|
$element.append('<button type="button" class="close" aria-hidden="true">×</button>')
|
2014-05-14 23:24:20 +10:00
|
|
|
$element.on('click', 'button', remove)
|
2014-07-14 18:25:40 +10:00
|
|
|
$element.on('click', remove)
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-07-14 18:25:40 +10:00
|
|
|
$(document.body).append($element)
|
|
|
|
|
2016-11-06 17:14:12 +01:00
|
|
|
setTimeout(function() {
|
2014-07-14 18:25:40 +10:00
|
|
|
$element.addClass('in')
|
2016-11-18 07:41:02 +11:00
|
|
|
}, 100)
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2016-11-18 07:41:02 +11:00
|
|
|
var timer = window.setTimeout(remove, options.interval * 1000)
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-07-14 18:25:40 +10:00
|
|
|
function removeElement() {
|
|
|
|
$element.remove()
|
|
|
|
}
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
function remove() {
|
|
|
|
window.clearInterval(timer)
|
2014-07-14 18:25:40 +10:00
|
|
|
|
|
|
|
$element.removeClass('in')
|
2016-11-18 07:41:02 +11:00
|
|
|
$.support.transition && $element.hasClass('fade')
|
|
|
|
? $element
|
2014-07-14 18:25:40 +10:00
|
|
|
.one($.support.transition.end, removeElement)
|
2016-11-18 07:41:02 +11:00
|
|
|
.emulateTransitionEnd(500)
|
|
|
|
: removeElement()
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FlashMessage.DEFAULTS = {
|
|
|
|
class: 'success',
|
|
|
|
text: 'Default text',
|
2016-11-28 20:48:05 +11:00
|
|
|
interval: 5
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// FLASH MESSAGE PLUGIN DEFINITION
|
|
|
|
// ============================
|
|
|
|
|
|
|
|
if ($.oc === undefined)
|
|
|
|
$.oc = {}
|
2014-07-14 18:25:40 +10:00
|
|
|
|
|
|
|
$.oc.flashMsg = FlashMessage
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
// FLASH MESSAGE DATA-API
|
|
|
|
// ===============
|
|
|
|
|
|
|
|
$(document).render(function(){
|
|
|
|
$('[data-control=flash-message]').each(function(){
|
|
|
|
$.oc.flashMsg($(this).data(), this)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-18 07:41:02 +11:00
|
|
|
}(window.jQuery);
|