-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathexpounder.js
125 lines (89 loc) · 3.01 KB
/
expounder.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
;(function (global) {
'use strict';
function Expounder() {
var appearClass = 'expounded-appear',
disappearClass = 'expounded-disappear';
this.getScriptParams = function() {
var scripts = document.getElementsByTagName('script');
var scriptName = scripts[scripts.length - 1]; // always the current script
return {
defaultCollapse : scriptName.hasAttribute('data-default-collapse')
? (scriptName.getAttribute('data-default-collapse') == "true")
: false
};
};
this.getElements = function() {
return document.querySelectorAll('[data-expounder], [data-expounder-c]');
};
this.getExpoundedItems = function(id) {
if (typeof id === 'undefined') {
throw new Error("Missing Expounder id");
}
return document.querySelectorAll('[data-expounded="' + id + '"]');
};
this.animationEndListener = function(event) {
if (event.target.className == disappearClass) {
event.target.className = '';
}
};
this.apply = function() {
var expounder = this,
elements = this.getElements();
for (var index = 0; index < elements.length; index++) {
var element = elements[index];
if ((typeof element.dataset.expounderLoaded != 'undefined')) {
continue;
} else {
element.dataset.expounderLoaded = true;
}
elements[index].addEventListener('mousedown', function(event) {
event.preventDefault();
// Only allow left and middle mouse clicks
if (event.button != 0 && event.button != 1)
return;
var defaultCollapse = expounder.getScriptParams().defaultCollapse,
shouldContract = defaultCollapse || (typeof this.dataset.expounderC != 'undefined'),
expoundId = this.dataset.expounder || this.dataset.expounderC,
expoundedItems = expounder.getExpoundedItems(expoundId);
for (var i = 0; i < expoundedItems.length; ++i) {
var expounded = expoundedItems[i];
if (shouldContract) {
if (expounded.className == appearClass) {
expounded.addEventListener('animationend', expounder.animationEndListener);
expounded.className = disappearClass;
} else {
expounded.removeEventListener('animationend', expounder.animationEndListener);
expounded.className = appearClass;
}
} else {
expounded.className = appearClass;
}
}
if (!shouldContract) {
var parentNode = this.parentNode;
var textNode = document.createTextNode(this.textContent);
parentNode.replaceChild(textNode, this);
}
}, false);
}
};
}
// Export
var expounder = new Expounder();
// automatically apply it on load
document.addEventListener('DOMContentLoaded', function() {
expounder.apply();
});
// AMD
if (typeof define === 'function' && define.amd) {
define(function () {
return expounder;
});
// Node and other CommonJS-like environments that support module.exports
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = expounder;
// Browser
} else {
global.Expounder = expounder;
}
})(this);