Projekt

Obecné

Profil

Stáhnout (1.84 KB) Statistiky
| Větev: | Tag: | Revize:
1
/**
2
 * Class containing common utility functions.
3
 * @constructor
4
 */
5
function Utils() {
6
	/**
7
	 * No operation function.
8
	 */
9
	this.noop = function() {};
10

    
11
	/**
12
	 * Checks whether the variable passed as parameter is defined.
13
	 * 
14
	 * @param {boolean} variable Variable to be checked.
15
	 * @return true if the variable is defined, otherwise false
16
	 */
17
	this.isDefined = function(variable) {
18
		return typeof variable !== 'undefined';
19
	};
20

    
21
	/**
22
	 * Checks whether the variable passed as parameter is not defined.
23
	 * 
24
	 * @param {boolean} variable Variable to be checked.
25
	 * @return true if the variable is NOT defined, otherwise false
26
	 */
27
	this.isUndefined = function(variable) {
28
		return typeof variable === 'undefined';
29
	};
30

    
31
	// TODO: move to DOM class
32
	this.createHtmlElement = function(tagName, attributes) {
33
		return app.dom.createHtmlElement(tagName, attributes);
34
	};
35

    
36
	// TODO: move to DOM class
37
	this.createSvgElement = function(tagName, attributes) {
38
		return app.dom.createSvgElement(tagName, attributes);
39
	};
40
	
41
	// TODO: move to DOM class
42
	this.createTextElement = function(text) {
43
		return app.dom.createTextElement(text);
44
	};
45

    
46
	/**
47
	 * Returns a new promise that is resolved at the moment when all promises passed as function parameter are resolved.
48
	 * https://stackoverflow.com/a/35825493
49
	 * 
50
	 * @param promises Array of promises to wait for.
51
	 * @return New promise.
52
	 */
53
	this.promiseAll = function(promises) {
54
		if (!Array.isArray(promises)) {
55
			throw new TypeError('Parameter must be an array.');
56
		}
57

    
58
		return $.when.apply($, promises).then(function () {
59
			// if single argument was expanded into multiple arguments, then put it back into an array for consistency
60
			if (promises.length === 1 && arguments.length > 1) {
61
				return [ Array.prototype.slice.call(arguments, 0) ];
62
			} else {
63
				return Array.prototype.slice.call(arguments, 0);
64
			}
65
		})
66
	};
67

    
68
}
(3-3/3)