From 8ca6cc32b2c789a3149861159ad258f2cb9491e3 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:39:39 +0200 Subject: Adding upstream version 2.11.4. Signed-off-by: Daniel Baumann --- public/js/helpers.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 public/js/helpers.js (limited to 'public/js/helpers.js') diff --git a/public/js/helpers.js b/public/js/helpers.js new file mode 100644 index 0000000..bcb2736 --- /dev/null +++ b/public/js/helpers.js @@ -0,0 +1,91 @@ +/*! Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */ + +/* jQuery Plugins */ +(function ($) { + + 'use strict'; + + /* 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; + }; + + /* Get class list */ + $.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; + }; + + /* 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; + }; + + $.fn.offsetTopRelativeTo = function($ancestor) { + if (typeof $ancestor === 'undefined') { + return false; + } + + var el = this[0]; + var offset = el.offsetTop; + var $parent = $(el.offsetParent); + + if ($parent.is('body') || $parent.is($ancestor)) { + return offset; + } + + if (el.tagName === 'TR') { + // TODO: Didn't found a better way, this will probably break sooner or later + return $parent.offsetTopRelativeTo($ancestor); + } + + return offset + $parent.offsetTopRelativeTo($ancestor); + }; + +})(jQuery); -- cgit v1.2.3