Last active
December 19, 2015 03:19
-
-
Save federicobond/5889447 to your computer and use it in GitHub Desktop.
Client-side messages for Bootstrap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// messages.js - Client-side messages for Bootstrap | |
// (c) 2013 Federico Bond | |
// messages.js may be freely distributed under the MIT License. | |
!function ($) { | |
"use strict"; | |
var templateString = '<div class="alert alert-{{ cls }}">{{ msg }}' + | |
'<a class="close" data-dismiss="alert" href="#">×</a>' + | |
'</div>'; | |
var template, partial; | |
if (window._) { | |
var _ = window._; | |
template = _.template; | |
partial = _.partial; | |
} else { | |
template = function (string) { | |
return function (context) { | |
return string.replace(/{{\s*(\w+)\s*}}/, | |
function (match, variable) { | |
return context[variable]; | |
}); | |
}; | |
}; | |
partial = function (func) { | |
var slice = Array.prototype.slice, | |
args = slice.call(arguments, 1); | |
return function () { | |
return func.apply(this, args.concat(slice.call(arguments))); | |
}; | |
}; | |
} | |
var settings = { | |
container: $('body > .container'), | |
template: template, | |
templateString: templateString, | |
closeButton: false, | |
append: false | |
}; | |
var message = function (cls, msg) { | |
var container = settings.container, | |
template = settings.template; | |
templateString = settings.templateString; | |
// Render message template | |
var elem = $(template(templateString)({'cls': cls, 'msg': msg})); | |
if (!settings.closeButton) { | |
elem.find('.close').remove(); | |
} | |
if (!settings.append) { | |
// Remove previous messages | |
container.find('.alert').remove(); | |
} | |
// Add current message | |
elem.hide().prependTo(container).fadeIn(); | |
}; | |
var setup = function (obj) { | |
$.extend(settings, obj); | |
}; | |
var messages = { | |
setup: setup, | |
success: partial(message, 'success'), | |
warning: partial(message, 'warning'), | |
error: partial(message, 'error'), | |
info: partial(message, 'info') | |
}; | |
window.messages = messages; | |
}(window.jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO:
setSelectorEngine