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
126
127
128
129
130
131
132
133
134
135
136
137
138
|
'use strict';
const events = require('../events.js');
const views = require('../util/views.js');
const iosCorrectedInnerHeight = require('ios-inner-height');
const PostContentControl = require('../controls/post_content_control.js');
const template = views.getTemplate('metric-sorter');
const sideTemplate = views.getTemplate('metric-sorter-side');
//TODO: find a way to export these constants once
const LEFT = 'left';
const RIGHT = 'right';
class MetricSorterView extends events.EventTarget {
constructor(ctx) {
super();
this._ctx = ctx;
this._hostNode = document.getElementById('content-holder');
views.replaceContent(this._hostNode, template(ctx));
this._formNode.addEventListener('submit', e => this._evtFormSubmit(e));
this._skipButtonNode.addEventListener('click', e => this._evtSkipClick(e));
this._compareLessBtnNode.addEventListener('click', e => this._evtCompareClick(e));
this._compareGreaterBtnNode.addEventListener('click', e => this._evtCompareClick(e));
this._refreshCompareButton();
}
installLeftPost(post) {
this._leftPostControl = this._installPostControl(post, this._leftSideNode);
}
installRightPost(post) {
this._rightPostControl = this._installPostControl(post, this._rightSideNode);
}
_installPostControl(post, sideNode) {
views.replaceContent(
sideNode,
sideTemplate(Object.assign({}, this._ctx, {
post: post,
})));
let containerNode = this._getSidePostContainerNode(sideNode);
return new PostContentControl(
containerNode,
post,
() => {
// TODO: come up with a more reliable resizing mechanism
return window.innerWidth < 1000 ?
[
window.innerWidth,
iosCorrectedInnerHeight() / 2
] : [
containerNode.getBoundingClientRect().width,
window.innerHeight - containerNode.getBoundingClientRect().top -
this._buttonsNode.getBoundingClientRect().height * 2
];
});
}
clearMessages() {
views.clearMessages(this._hostNode);
}
enableForm() {
views.enableForm(this._formNode);
}
disableForm() {
views.disableForm(this._formNode);
}
showSuccess(message) {
views.showSuccess(this._hostNode, message);
}
showError(message) {
views.showError(this._hostNode, message);
}
get _formNode() {
return this._hostNode.querySelector('form');
}
get _leftSideNode() {
return this._hostNode.querySelector('.left-post-container');
}
get _rightSideNode() {
return this._hostNode.querySelector('.right-post-container');
}
get _compareGreaterBtnNode() {
return this._hostNode.querySelector('.left-gt-right')
}
get _compareLessBtnNode() {
return this._hostNode.querySelector('.left-lt-right')
}
get _buttonsNode() {
return this._hostNode.querySelector('.buttons');
}
get _skipButtonNode() {
return this._hostNode.querySelector('.skip-btn');
}
_getSidePostContainerNode(sideNode) {
return sideNode.querySelector('.post-container');
}
_evtSkipClick(e) {
e.preventDefault();
this.dispatchEvent(new CustomEvent('skip'));
}
_evtFormSubmit(e) {
e.preventDefault();
this.dispatchEvent(new CustomEvent('submit', {
detail: {
greaterPost: this._ctx.greaterPost,
}}));
}
_evtCompareClick(e) {
e.preventDefault();
this._ctx.greaterPost = this._ctx.greaterPost === LEFT ? RIGHT : LEFT;
this._refreshCompareButton();
}
_refreshCompareButton() {
this._compareGreaterBtnNode.hidden = this._ctx.greaterPost === RIGHT;
this._compareLessBtnNode.hidden = this._ctx.greaterPost === LEFT;
}
}
module.exports = MetricSorterView;
|