define('notice', ['mailpoet', 'jquery'], function(MailPoet, jQuery) { "use strict"; /*================================================================================================== MailPoet Notice: description: Handles notices version: 1.0 author: Jonathan Labreuille company: Wysija dependencies: jQuery Usage: // success message (static: false) MailPoet.Notice.success('Yatta!'); // error message (static: false) MailPoet.Notice.error('Boo!'); // system message (static: true) MailPoet.Notice.system('You need to updated ASAP!'); ==================================================================================================*/ MailPoet.Notice = { version: 1.0, // default options defaults: { type: 'success', message: '', static: false, hideClose: false, id: null, positionAfter: false, scroll: false, timeout: 5000, onOpen: null, onClose: null }, options: {}, init: function(options) { // set options this.options = jQuery.extend({}, this.defaults, options); return this; }, createNotice: function() { // clone element this.element = jQuery('#mailpoet_notice_'+this.options.type).clone(); // add data-id to the element if (this.options.id) { this.element.attr( 'data-id', this.options.id ); } // remove id from clone this.element.removeAttr('id'); // insert notice after its parent var positionAfter; if (typeof this.options.positionAfter === 'object') { positionAfter = this.options.positionAfter; } else if (typeof this.options.positionAfter === 'string') { positionAfter = jQuery(this.options.positionAfter); } else { positionAfter = jQuery('#mailpoet_notice_'+this.options.type); } positionAfter.after(this.element); // setup onClose callback var onClose = null; if (this.options.onClose !== null) { onClose = this.options.onClose; } // listen to remove event jQuery(this.element).on('close', function() { jQuery(this).fadeOut(200, function() { // on close callback if (onClose !== null) { onClose(); } // remove notice jQuery(this).remove(); }); }.bind(this.element)); // listen to message event jQuery(this.element).on('setMessage', function(e, message) { MailPoet.Notice.setMessage(message); }.bind(this.element)); return this; }, updateNotice: function() { // update notice's message jQuery('[data-id="'+this.options.id+'"').first().trigger( 'setMessage', this.options.message ); }, isHTML: function(str) { var a = document.createElement('div'); a.innerHTML = str; for (var c = a.childNodes, i = c.length; i--;) { if (c[i].nodeType == 1) return true; } return false; }, setMessage: function(message) { message = this.formatMessage(message); // if it's not an html message // let's sugar coat the message with a fancy

if (this.isHTML(message) === false) { message = '

'+message+'

'; } // set message return this.element.html(message); }, formatMessage: function(message) { if (Array.isArray(message)) { return message.join('
'); } else { return message; } }, show: function(options) { // initialize this.init(options); if ( this.options.id !== null && jQuery('[data-id="'+this.options.id+'"').length > 0 ) { this.updateNotice(); } else { this.createNotice(); } this.showNotice(); }, showNotice: function() { // set message this.setMessage(this.options.message); // position notice this.element.insertAfter(jQuery('h2.title')); // set class name switch (this.options.type) { case 'success': this.element.addClass('updated'); break; case 'system': this.element.addClass('update-nag'); break; case 'error': this.element.addClass('error'); break; } // make the notice appear this.element.fadeIn(200); // if scroll option is enabled, scroll to the notice if (this.options.scroll === true) { this.element.get(0).scrollIntoView(false); } // if the notice is not static, it has to disappear after a timeout if (this.options.static === false) { this.element.delay(this.options.timeout).trigger('close'); } else if (this.options.hideClose === false) { this.element.append(''); this.element.find('.mailpoet_notice_close').on('click', function() { jQuery(this).trigger('close'); }); } // call onOpen callback if (this.options.onOpen !== null) { this.options.onOpen(this.element); } }, hide: function(all) { if (all !== undefined && all === true) { // all notices jQuery('.mailpoet_notice:not([id])').trigger('close'); } else if (all !== undefined && jQuery.isArray(all)) { // array of ids for (var id in all) { jQuery('[data-id="' + all[id] + '"]').trigger('close'); } } if (all !== undefined) { // single id jQuery('[data-id="' + all + '"]').trigger('close'); } else { jQuery('.mailpoet_notice.updated:not([id]), .mailpoet_notice.error:not([id])') .trigger('close'); } }, error: function(message, options) { this.show(jQuery.extend({}, { type: 'error', message: message }, options)); }, success: function(message, options) { this.show(jQuery.extend({}, { type: 'success', message: message }, options)); }, system: function(message, options) { this.show(jQuery.extend({}, { type: 'system', static: true, message: message }, options)); } }; });