Major style/template overhaul
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / wcf.globalHelper.js
CommitLineData
d0023381
AE
1/**
2 * Collection of global short hand functions.
3 *
4 * @author Alexander Ebert
5 * @copyright 2001-2015 WoltLab GmbH
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
7 */
8(function(window, document) {
9 /**
10 * Shorthand function to retrieve or set an attribute.
11 *
12 * @param {Element} element target element
13 * @param {string} attribute attribute name
14 * @param {mixed=} value attribute value, omit if attribute should be read
15 * @return {(string|undefined)} attribute value, empty string if attribute is not set or undefined if `value` was omitted
16 */
17 window.elAttr = function(element, attribute, value) {
18 if (value === undefined) {
19 return element.getAttribute(attribute) || '';
20 }
21
22 element.setAttribute(attribute, value);
23 };
24
25 /**
26 * Shorthand function to find elements by class name.
27 *
28 * @param {string} className CSS class name
29 * @param {Element=} context target element, assuming `document` if omitted
30 * @return {NodeList} matching elements
31 */
32 window.elByClass = function(className, context) {
33 return (context || document).getElementsByClassName(className);
34 };
35
36 /**
37 * Shorthand function to retrieve an element by id.
38 *
39 * @param {string} id element id
40 * @return {(Element|null)} matching element or null if not found
41 */
42 window.elById = function(id) {
43 return document.getElementById(id);
44 };
45
46 /**
47 * Shorthand function to find an element by CSS selector.
48 *
49 * @param {string} selector CSS selector
50 * @param {Element=} context target element, assuming `document` if omitted
51 * @return {(Element|null)} matching element or null if no match
52 */
53 window.elBySel = function(selector, context) {
54 return (context || document).querySelector(selector);
55 };
56
57 /**
58 * Shorthand function to find elements by CSS selector.
59 *
60 * @param {string} selector CSS selector
61 * @param {Element=} context target element, assuming `document` if omitted
62 * @return {NodeList} matching elements
63 */
64 window.elBySelAll = function(selector, context) {
65 return (context || document).querySelectorAll(selector);
66 };
67
68 /**
69 * Shorthand function to find elements by tag name.
70 *
71 * @param {string} tagName element tag name
72 * @param {Element=} context target element, assuming `document` if omitted
73 * @return {NodeList} matching elements
74 */
75 window.elByTag = function(tagName, context) {
76 return (context || document).getElementsByTagName(tagName);
77 };
78
79 /**
80 * Shorthand function to create a DOM element.
81 *
82 * @param {string} tagName element tag name
83 * @return {Element} new DOM element
84 */
85 window.elCreate = function(tagName) {
86 return document.createElement(tagName);
87 };
88
89 /**
90 * Shorthand function to check if an object has a property while ignoring the chain.
91 *
92 * @param {object} obj target object
93 * @param {string} property property name
94 * @return {boolean} false if property does not exist or belongs to the chain
95 */
96 window.objOwns = function(obj, property) {
97 return obj.hasOwnProperty(property);
98 };
b8eab696
AE
99
100 /**
101 * Shorthand function to hide an element by setting its 'display' value to 'none'.
102 *
103 * @param {Element} element DOM element
104 */
105 window.elHide = function(element) {
106 element.style.setProperty('display', 'none');
107 };
108
109 /**
110 * Shorthand function to show an element previously hidden by using `elHide()`.
111 *
112 * @param {Element} element DOM element
113 */
114 window.elShow = function(element) {
115 element.style.removeProperty('display');
116 };
d0023381 117})(window, document);