Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 7b03cdcc

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

reworked utils to ES6 classes with static methods

Zobrazit rozdíly:

sources/src/main/webapp/js/app.js
13 13
	this.loader = new Loader;
14 14
	/** @prop {Zoom} zoom */
15 15
	this.zoom = new Zoom(0.8);
16
	/** @prop {Utils} utils */
17
	this.utils = new Utils;
18
	/** @prop {DOM} dom */
19
	this.dom = new DOM;
20
	/** @prop {Cookies} cookies */
21
	this.cookies = new Cookies;
22 16
	/** @prop {MarkSymbol} markSymbol */
23 17
	this.markSymbol = new MarkSymbol;
24 18

  
sources/src/main/webapp/js/utils/cookies.js
1 1
/**
2 2
 * Class containing cookie utility functions.
3
 * @constructor
4 3
 */
5
function Cookies() {
4
class Cookies {
6 5
	/**
7 6
	 * Gets the value of a cookie.
8 7
	 * 
9 8
	 * @param {string} name Name of the cookie.
10 9
	 */
11
	this.get = function(name) {
12
		var cookies = document.cookie.split('; ');
10
	static get(name) {
11
		const cookies = document.cookie.split('; ');
13 12

  
14
		var cookie = cookies.find(function(cookie) {
13
		const cookie = cookies.find(cookie => {
15 14
			return cookie.startsWith(name + '=');
16 15
		});
17 16

  
18 17
		if (typeof cookie === 'undefined') return null;
19 18

  
20 19
		return cookie.split('=')[1];
21
	};
20
	}
22 21

  
23 22
	/**
24 23
	 * Sets a new cookie.
......
26 25
	 * @param {string} name Name of the cookie.
27 26
	 * @param {string} value Value of the cookie.
28 27
	 */
29
	this.set = function(name, value) {
30
		var date = new Date();
28
	static set(name, value) {
29
		const date = new Date();
31 30

  
32 31
		document.cookie = name + "=" + value + "; path=/imiger";
33
	};
32
	}
34 33
}
sources/src/main/webapp/js/utils/dom.js
1 1
/**
2 2
 * Class containing Document Object Model utility functions.
3
 * @constructor
4 3
 */
5
function DOM() {
6
	var htmlTags = JSON.parse(document.getElementById('htmlTags').textContent);
7
	var svgTags = JSON.parse(document.getElementById('svgTags').textContent);
8

  
4
class DOM {
9 5
	/**
10 6
	 * Creates a new HTML DOM element from HTML string. Since only the first HTML element in the set is returned, the input 
11 7
	 * string should contain a wrapping element that would hold all inner elements.
......
13 9
	 * @param {string} html HTML string.
14 10
	 * @returns {Element} HTML element.
15 11
	 */
16
	this.htmlStringToElement = function(html) {
17
		var template = document.createElement('template');
12
	static htmlStringToElement(html) {
13
		const template = document.createElement('template');
18 14
		template.innerHTML = html;
19 15

  
20 16
		return template.content.firstChild;
21
	};
17
	}
22 18

  
23 19
	/**
24 20
	 * Creates a new HTML DOM element. If the tagName passed as a parameter is not a valid HTML tag, exception is thrown.
......
28 24
	 * @returns {Element} HTML DOM element.
29 25
	 * @throws {InvalidArgumentError} Thrown when tagName is not a valid HTML tag.
30 26
	 */
31
	this.createHtmlElement = function(tagName, attributes) {
32
		if (htmlTags.indexOf(tagName) === -1) {
27
	static createHtmlElement(tagName, attributes = {}) {
28
		if (DOM.validHTMLTags.indexOf(tagName) === -1) {
33 29
			throw new InvalidArgumentError(tagName + 'is not a valid HTML element');
34 30
		}
35 31

  
36
		var element = document.createElement(tagName);
32
		const element = document.createElement(tagName);
37 33
		for (let key in attributes) {
38 34
			element.setAttribute(key, attributes[key]);
39 35
		}
40 36

  
41 37
		return element;
42
	};
38
	}
43 39
	
44 40
	/**
45 41
	 * Creates a new SVG DOM element. If the tagName passed as a parameter is not a valid SVG tag, exception is thrown.
......
49 45
	 * @returns {Element} SVG DOM element.
50 46
	 * @throws {InvalidArgumentError} Thrown when tagName is not a valid SVG tag.
51 47
	 */
52
	this.createSvgElement = function(tagName, attributes) {
53
		if (svgTags.indexOf(tagName) === -1) {
48
	static createSvgElement(tagName, attributes = {}) {
49
		if (DOM.validSVGTags.indexOf(tagName) === -1) {
54 50
			throw new InvalidArgumentError(tagName + 'is not a valid SVG element');
55 51
		}
56 52

  
57
		var element = document.createElementNS('http://www.w3.org/2000/svg', tagName);
53
		const element = document.createElementNS('http://www.w3.org/2000/svg', tagName);
58 54
		for (let key in attributes) {
59 55
			element.setAttribute(key, attributes[key]);
60 56
		}
61 57

  
62 58
		return element;
63
	};
59
	}
64 60

  
65 61
	/**
66 62
	 * Creates a new DOM node containing the text.
67 63
	 * 
68 64
	 * @param {string} text Text to create the element for.
69 65
	 */
70
	this.createTextElement = function(text) {
66
	static createTextElement(text) {
71 67
		return document.createTextNode(text);
72
	};
68
	}
73 69
}
70

  
71
DOM.validHTMLTags = JSON.parse(document.getElementById('htmlTags').textContent);
72
DOM.validSVGTags = JSON.parse(document.getElementById('svgTags').textContent);
sources/src/main/webapp/js/utils/utils.js
1 1
/**
2 2
 * Class containing common utility functions.
3
 * @constructor
4 3
 */
5
function Utils() {
4
class Utils {
6 5
	/**
7 6
	 * No operation function.
8 7
	 */
9
	this.noop = function() {};
8
	static noop() {}
10 9

  
11 10
	/**
12 11
	 * Stops propagation of the mouse interaction to parental elements.
13 12
	 * @param {MouseEvent} e Click/double-click event.
14 13
	 */
15
	this.stopPropagation = function(e) {
14
	static stopPropagation(e) {
16 15
		e.stopPropagation();
17
	};
16
	}
18 17

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

  
29 28
	/**
30 29
	 * Checks whether the variable passed as parameter is not defined.
......
32 31
	 * @param variable Variable to be checked.
33 32
	 * @return {boolean} true if the variable is NOT defined, otherwise false
34 33
	 */
35
	this.isUndefined = function(variable) {
34
	static isUndefined(variable) {
36 35
		return typeof variable === 'undefined';
37
	};
38

  
39
	// TODO: move to DOM class
40
	this.createHtmlElement = function(tagName, attributes) {
41
		return app.dom.createHtmlElement(tagName, attributes);
42
	};
43

  
44
	// TODO: move to DOM class
45
	this.createSvgElement = function(tagName, attributes) {
46
		return app.dom.createSvgElement(tagName, attributes);
47
	};
48
	
49
	// TODO: move to DOM class
50
	this.createTextElement = function(text) {
51
		return app.dom.createTextElement(text);
52
	};
36
	}
53 37

  
54 38
	/**
55 39
	 * Returns a new promise that is resolved at the moment when all promises passed as function parameter are resolved.
......
58 42
	 * @param promises Array of promises to wait for.
59 43
	 * @return New promise.
60 44
	 */
61
	this.promiseAll = function(promises) {
45
	static promiseAll(promises) {
62 46
		if (!Array.isArray(promises)) {
63 47
			throw new TypeError('Parameter must be an array.');
64 48
		}
......
71 55
				return Array.prototype.slice.call(arguments, 0);
72 56
			}
73 57
		})
74
	};
58
	}
75 59

  
76 60
	/**
77 61
	 * Extracts value of a query parameter from the current URL.
......
80 64
	 * @param {string} variable 
81 65
	 * @returns value of the query parameter or false if the parameter does not exist
82 66
	 */
83
	this.getQueryVariable = function(variable) {
67
	static getQueryVariable(variable) {
84 68
		var query = window.location.search.substring(1);
85 69
		var vars = query.split('&');
86 70

  
......
93 77
		}
94 78

  
95 79
		return false;
96
	};
80
	}
97 81

  
98 82
    /**
99 83
     * @param {(Vertex|Group)} node Graph node.
100 84
     * @returns {string} Unique identifier of a graph node (group or vertex).
101 85
     */
102
    this.getUniqueId = function(node) {
103
    	if(this.isUndefined(node)) return '';
86
    static getUniqueId(node) {
87
    	if (Utils.isUndefined(node)) return '';
104 88

  
105 89
    	var prefix;
106 90
        if (node instanceof Vertex) {
......
112 96
        }
113 97

  
114 98
        return prefix + node.id;
115
    };
99
    }
116 100
}

Také k dispozici: Unified diff