aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/frontend/elements.js
blob: 8d7a5100acea50845f8ee1edf866dd4956488ea7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Collection of convenient element functions (elements.js)
HashOverConstructor.prototype.elements = {
	cache: {},

	// Shorthand for Document.getElementById ()
	get: function (id, force, prefix)
	{
		id = (prefix !== false) ? 'hashover-' + id : id;

		if (force === true || !this.cache[id]) {
			this.cache[id] = document.getElementById (id);
		}

		return this.cache[id];
	},

	// Execute callback function if element isn't false
	exists: function (element, callback, prefix)
	{
		if (element = this.get (element, true, prefix)) {
			return callback (element);
		}

		return false;
	},

	// Adds properties to an element
	addProperties: function (element, properties)
	{
		element = element || document.createElement ('span');
		properties = properties || {};

		// Add each property to element
		for (var property in properties) {
			if (properties.hasOwnProperty (property) === false) {
				continue;
			}

			// Property value
			var value = properties[property];

			// If the property is an object add each item to existing property
			if (!!value && value.constructor === Object) {
				this.addProperties (element[property], value);
				continue;
			}

			element[property] = value;
		}

		return element;
	},

	// Creates an element with attributes
	create: function (tagName, attributes)
	{
		tagName = tagName || 'span';
		attributes = attributes || {};

		// Create element
		var element = document.createElement (tagName);

		// Add properties to element
		element = this.addProperties (element, attributes);

		return element;
	},

	// Adds duplicate event listeners to an element
	duplicateProperties: function (element, names, value)
	{
		var properties = {};

		// Construct a properties object with duplicate values
		for (var i = 0, il = names.length; i < il; i++) {
			properties[(names[i])] = value;
		}

		// Add the properties to the object
		return this.addProperties (element, properties);
	}
};