icingaweb2/public/js/helpers.js

71 lines
1.8 KiB
JavaScript
Raw Normal View History

/*! Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
2014-03-26 08:40:41 +01:00
/* jQuery Plugins */
2014-03-04 14:07:28 +01:00
(function ($) {
'use strict';
2019-07-29 15:58:41 +02:00
/* Get data value or default */
$.fn.getData = function (name, fallback) {
var value = this.data(name);
if (typeof value !== 'undefined') {
return value;
}
return fallback;
};
/* Whether a HTML tag has a specific attribute */
$.fn.hasAttr = function(name) {
// We have inconsistent behaviour across browsers (false VS undef)
var val = this.attr(name);
return typeof val !== 'undefined' && val !== false;
};
2014-03-26 08:40:41 +01:00
/* Get class list */
2014-03-04 14:07:28 +01:00
$.fn.classes = function (callback) {
var classes = [];
$.each(this, function (i, el) {
var c = $(el).attr('class');
if (typeof c === 'string') {
$.each(c.split(/\s+/), function(i, p) {
if (classes.indexOf(p) === -1) {
classes.push(p);
}
});
}
});
if (typeof callback === 'function') {
for (var i in classes) {
if (classes.hasOwnProperty(i)) {
callback(classes[i]);
}
}
}
return classes;
};
2014-03-26 08:40:41 +01:00
/* Serialize form elements to an object */
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
2014-03-04 14:07:28 +01:00
})(jQuery);