forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeTabsBox.js
More file actions
executable file
·97 lines (71 loc) · 2.65 KB
/
codeTabsBox.js
File metadata and controls
executable file
·97 lines (71 loc) · 2.65 KB
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
let delegate = require('client/delegate');
let addLineNumbers = require('./addLineNumbers');
function CodeTabsBox(elem) {
if (window.ebookType) {
return;
}
this.elem = elem;
this.translateX = 0;
this.switchesElem = elem.querySelector('[data-code-tabs-switches]');
this.switchesElemItems = this.switchesElem.firstElementChild;
this.arrowLeft = elem.querySelector('[data-code-tabs-left]');
this.arrowRight = elem.querySelector('[data-code-tabs-right]');
this.arrowLeft.onclick = function(e) {
e.preventDefault();
this.translateX = Math.max(0, this.translateX - this.switchesElem.offsetWidth);
this.renderTranslate();
}.bind(this);
this.arrowRight.onclick = function(e) {
e.preventDefault();
this.translateX = Math.min(this.translateX +this.switchesElem.offsetWidth, this.switchesElemItems.offsetWidth - this.switchesElem.offsetWidth);
this.renderTranslate();
}.bind(this);
this.delegate('.code-tabs__switch', 'click', this.onSwitchClick);
}
CodeTabsBox.prototype.onSwitchClick = function(e) {
e.preventDefault();
let siblings = e.delegateTarget.parentNode.children;
let tabs = this.elem.querySelector('[data-code-tabs-content]').children;
let selectedIndex;
for(let i=0; i<siblings.length; i++) {
let switchElem = siblings[i];
let tabElem = tabs[i];
if (switchElem == e.delegateTarget) {
selectedIndex = i;
tabElem.classList.add('code-tabs__section_current');
switchElem.classList.add('code-tabs__switch_current');
} else {
tabElem.classList.remove('code-tabs__section_current');
switchElem.classList.remove('code-tabs__switch_current');
}
}
if (selectedIndex === 0) {
this.elem.classList.add('code-tabs_result_on');
} else {
this.elem.classList.remove('code-tabs_result_on');
this.highlightTab(tabs[selectedIndex]);
}
};
CodeTabsBox.prototype.highlightTab = function(tab) {
if (tab.highlighted) return;
let preElem = tab.querySelector('pre');
let codeElem = preElem.querySelector('code');
Prism.highlightElement(codeElem);
addLineNumbers(preElem);
tab.highlighted = true;
};
CodeTabsBox.prototype.renderTranslate = function() {
this.switchesElemItems.style.transform = 'translateX(-' + this.translateX + 'px)';
if (this.translateX === 0) {
this.arrowLeft.setAttribute('disabled', '');
} else {
this.arrowLeft.removeAttribute('disabled');
}
if (this.translateX === this.switchesElemItems.offsetWidth - this.switchesElem.offsetWidth) {
this.arrowRight.setAttribute('disabled', '');
} else {
this.arrowRight.removeAttribute('disabled');
}
};
delegate.delegateMixin(CodeTabsBox.prototype);
module.exports = CodeTabsBox;