Last active
August 28, 2015 07:39
-
-
Save rdev5/11abf514e73c8d87c9ff to your computer and use it in GitHub Desktop.
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
// For supplying defaultOptions and providing optional validation | |
var validateOptions = function(options, defaultOptions, validOptions) | |
{ | |
if (options === undefined) | |
return defaultOptions; | |
var validatedOptions = defaultOptions; | |
for (var k in options) | |
{ | |
if (validOptions === undefined) | |
{ | |
validatedOptions = options[k]; | |
continue; | |
} | |
if (k in validOptions) | |
{ | |
validatedOptions = options[k]; | |
// Handle validOptions list | |
if (Array.isArray(validOptions[k])) | |
{ | |
if (validOptions[k].indexOf(options[k]) == -1) | |
{ | |
delete validatedOptions[k]; | |
} | |
} | |
} | |
} | |
return validatedOptions; | |
} | |
// createAlert Example (http://getbootstrap.com/components/#alerts-dismissible): | |
var createAlert = function(message, options) | |
{ | |
var defaultOptions = { | |
alertClass: 'info', | |
dismissable: true, | |
}; | |
options = validateOptions(options, defaultOptions, { | |
alertClass: ['success', 'info', 'warning', 'danger'], | |
dismissable: [true, false], | |
message: null | |
}); | |
var alertDiv = $('<div />') | |
.addClass('alert') | |
.addClass('alert-' + options.alertClass) | |
.attr('role', 'alert'); | |
if (options.dismissable) | |
{ | |
var btn = $('<button />') | |
.attr('type', 'button') | |
.addClass('close') | |
.attr('data-dismiss', 'alert') | |
.attr('aria-label', 'Close') | |
.appendTo(alertDiv) | |
$('<span />') | |
.attr('aria-hidden', 'true') | |
.html('×') | |
.appendTo(btn); | |
alertDiv.addClass('alert-dismissible'); | |
} | |
alertDiv.append(message); | |
return alertDiv; | |
} | |
// Invoke | |
var defaultOptions = { | |
alertClass: 'info', | |
dismissable: true, | |
}; | |
options = validateOptions(options, defaultOptions, { | |
alertClass: ['success', 'info', 'warning', 'danger'], | |
dismissable: [true, false], | |
message: null | |
}); | |
var newAlert = createAlert('Info alert message'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment