Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 5eea34f0

Přidáno uživatelem Pavel Fidranský před více než 6 roky(ů)

created shorter and more powerful static methods of DOM class, older ones kept for BC

Zobrazit rozdíly:

sources/src/main/webapp/js/utils/dom.js
66 66
	static createTextElement(text) {
67 67
		return document.createTextNode(text);
68 68
	}
69

  
70
	/**
71
	 * Creates a new HTML DOM element. If the tagName passed as a parameter is not a valid HTML tag, exception is thrown.
72
	 *
73
	 * @param {string} tagName Type of the newly created element (div, span, ...).
74
	 * @param {object} attributes Attributes of the element.
75
	 * @param {array<HTMLElement>} children Elements to be append to the newly created element.
76
	 * @param {object} options Options to be used while creating the element.
77
	 * @returns {HTMLElement} HTML DOM element.
78
	 * @throws {InvalidArgumentError} Thrown when tagName is not a valid HTML tag.
79
	 */
80
	static h(tagName, attributes = {}, children = [], options = null) {
81
		if (DOM.validHTMLTags.indexOf(tagName) === -1) {
82
			throw new InvalidArgumentError(tagName + 'is not a valid HTML element');
83
		}
84

  
85
		const element = document.createElement(tagName, options);
86

  
87
		for (let key in attributes) {
88
			let value = attributes[key];
89

  
90
			if (key.startsWith('on') && typeof value === 'function') {
91
				element.addEventListener(key.substring(2).toLowerCase(), value);
92
			} else if (key === 'innerText') {
93
				element.innerText = value;
94
			} else if (key === 'innerHTML') {
95
				element.innerHTML = value;
96
			} else {
97
				element.setAttribute(key, value);
98
			}
99
		}
100

  
101
		for (let child of children) {
102
			element.appendChild(child);
103
		}
104

  
105
		return element;
106
	}
107
	
108
	/**
109
	 * Creates a new SVG DOM element. If the tagName passed as a parameter is not a valid SVG tag, exception is thrown.
110
	 *
111
	 * @param {string} tagName Type of the element (circle, rect, ...).
112
	 * @param {object} attributes Attributes of the element.
113
	 * @param {array<SVGElement>} children Elements to be append to the newly created element.
114
	 * @param {object} options Options to be used while creating the element.
115
	 * @returns {SVGElement} SVG DOM element.
116
	 * @throws {InvalidArgumentError} Thrown when tagName is not a valid SVG tag.
117
	 */
118
	static s(tagName, attributes = {}, children = [], options = null) {
119
		if (DOM.validSVGTags.indexOf(tagName) === -1) {
120
			throw new InvalidArgumentError(tagName + 'is not a valid SVG element');
121
		}
122

  
123
		const element = document.createElementNS('http://www.w3.org/2000/svg', tagName, options);
124

  
125
		for (let key in attributes) {
126
			let value = attributes[key];
127

  
128
			if (key.startsWith('on') && typeof value === 'function') {
129
				element.addEventListener(key.substring(2).toLowerCase(), value);
130
			} else if (key === 'innerText') {
131
				element.innerText = value;
132
			} else if (key === 'innerHTML') {
133
				element.innerHTML = value;
134
			} else {
135
				element.setAttribute(key, value);
136
			}
137
		}
138

  
139
		for (let child of children) {
140
			element.appendChild(child);
141
		}
142

  
143
		return element;
144
	}
145

  
146
	/**
147
	 * Creates a new DOM node containing the text.
148
	 * 
149
	 * @param {string} text Text to create the element for.
150
	 */
151
	static t(text) {
152
		return document.createTextNode(text);
153
	}
69 154
}
70 155

  
71 156
DOM.validHTMLTags = JSON.parse(document.getElementById('htmlTags').textContent);

Také k dispozici: Unified diff