//= require voltron Voltron.addModule('Flash', function(){ var _initialized = false; var _defaults = { class: '', bind: 'body', id: 'flashes', containerClass: '', addMethod: 'prepend', revealMethod: 'slideDown', revealTime: 200, concealMethod: 'slideUp', concealTime: 200, autoClose: false, autoCloseAfter: 5000, autoAdd: true }; return { initialize: function(){ if(!_initialized){ _initialized = true; Voltron('Dispatch/addEventWatcher', 'click'); this.addListener(); } }, onClickCloseFlash: function(o){ this.clear(o, $(o.element).data('flash-options') || {}); }, setConfig: function(options){ _defaults = $.extend(_defaults, options); this.afterConfigChange(); return this; }, addListener: function(){ var self = this; $(document).ajaxComplete(function(event, request){ var flashes = request.getResponseHeader(Voltron.getConfig('flash/header', 'X-Flash')); var flash = $.parseJSON(flashes); Voltron.dispatch('flash:received', { event: event, request: request, flash: self, flashes: flash }); if(_defaults.autoAdd && flash){ Voltron('Flash/new', flash); } }); }, new: function(flashes, options){ options = $.extend(_defaults, options); var flash; this.getContainer(options).addClass(options.containerClass); $.each(flashes, $.proxy(function(type, messages){ flash = this.addFlash(type, messages, options); flash.addClass(options.class); if(flash.find('.flash-message').length == 1){ // If this is the first flash message, reveal the whole container flash.find('.flash-message').show(); flash[options.revealMethod](options.revealTime); }else{ // Otherwise reveal just the newest message(s) flash.find('.flash-message')[options.revealMethod](options.revealTime); } if(options.autoClose){ setTimeout(function(){ V('Flash/clear', { element: flash }); }, options.autoCloseAfter); } }, this)); return this; }, addFlash: function(type, messages, options){ var flash = this.getFlash(type, options); flash.append($.map($.makeArray(messages), function(message){ return $('

', { class: 'flash-message' }).html(message).hide(); })); if(!Voltron.getConfig('flash/group') || !flash.find('.flash-close').length){ flash.append($('