-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudyCard.js
194 lines (165 loc) · 5.38 KB
/
studyCard.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { LitElement, html, svg, css, createRef, ref } from 'https://cdn.jsdelivr.net/gh/lit/dist@3/all/lit-all.min.js';
import { StudyMD } from './studyMD.js';
export class StudyCard extends LitElement {
static styles = css`
:host{
display: flex;
flex-direction: row;
flex-wrap: wrap;
background: #1e1e1e;
background: #fff;
flex: 1 1 auto;
align-items: center;
justify-content: center;
padding: 5%;
}
article {
background: #fff;
border: solid 1px #ccc;
padding: 5%;
box-sizing: border-box;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
transition: 0.3s;
border-radius: 5px;
flex: 1 1 auto;
white-space-collapse: preserve-breaks;
position:relative;
}
article:hover {
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
}
article .right, article .left{
visibility:hidden;
}
article:hover .right, article:hover .left{
visibility: visible;
border-radius: 3px 0 0 3px;
z-index: 1;
cursor: pointer;
position: absolute;
width: auto;
padding: 12px;
color: white;
background-color: rgba(0,0,0,0.8);
font-weight: bold;
font-size: 18px;
transition: background-color 0.9s ease;
top: 50%;
margin-top: -22px;
}
article .left{
left:0;
}
article .right{
right: 0;
}
`;
static properties = {
data: { type: String },
articles: { type: Array },
index: { type: Number },
articleRef: {}
};
constructor() {
super();
this.data = "";
this.articles = [];
this.index = 0;
this.articleRef = createRef();
}
render() {
//return this.renderAllView();
return html`<article ${ref(this.articleRef)}>${this.renderArticle()}
<span class="left" @click="${() => this.swipeDetected(null, 'left')}">❮</span>
<span class="right" @click="${() => this.swipeDetected(null, 'right')}">❯</span>
</article>`;
}
renderAllView() {
if (this.data) {
return html`<study-md markdown="${this.data}"></study-md>`;
}
}
renderOneView() {
return html`<article ${ref(this.articleRef)}>${this.renderArticle()}
<span class="left" @click="${() => this.swipeDetected(null, 'left')}">❮</span>
<span class="right" @click="${() => this.swipeDetected(null, 'right')}">❯</span>
</article>`;
}
renderArticle() {
if (this.data) {
var article = this.articles[this.index];
return html`<study-md markdown="${article}"></study-md>`;
}
}
async connectedCallback() {
super.connectedCallback()
var response = await this.getData();
this.data = await response.text();
this.makeArticles();
}
makeArticles() {
this.articles = this.data.split(/(?=#\s)/g);
}
async getData() {
const response = await fetch('./religion.md');
return response;
}
firstUpdated() {
super.firstUpdated();
const articleElement = this.renderRoot.querySelector("article");
this.detectSwipe(articleElement, this.swipeDetected.bind(this));
console.log(articleElement);
console.log(this.articleRef.value);
}
swipeDetected(e, dir) {
if (dir === 'left') this.index++;
else if (dir === 'right') this.index--;
this.index = (this.articles.length + this.index) % this.articles.length;
console.log(dir);
}
detectSwipe(element, f) {
var detect = {
startX: 0,
startY: 0,
endX: 0,
endY: 0,
minX: 30, // min X swipe for horizontal swipe
maxX: 30, // max X difference for vertical swipe
minY: 50, // min Y swipe for vertial swipe
maxY: 60 // max Y difference for horizontal swipe
},
direction = null
element.addEventListener('touchstart', function (event) {
var touch = event.touches[0];
detect.startX = touch.screenX;
detect.startY = touch.screenY;
event.preventDefault();
});
element.addEventListener('touchmove', function (event) {
event.preventDefault();
var touch = event.touches[0];
detect.endX = touch.screenX;
detect.endY = touch.screenY;
});
element.addEventListener('touchend', function (event) {
event.preventDefault();
if (
// Horizontal move.
(Math.abs(detect.endX - detect.startX) > detect.minX)
&& (Math.abs(detect.endY - detect.startY) < detect.maxY)
) {
direction = (detect.endX > detect.startX) ? 'right' : 'left';
} else if (
// Vertical move.
(Math.abs(detect.endY - detect.startY) > detect.minY)
&& (Math.abs(detect.endX - detect.startX) < detect.maxX)
) {
direction = (detect.endY > detect.startY) ? 'down' : 'up';
}
if ((direction !== null) && (typeof f === 'function')) {
f(element, direction);
}
});
}
}
customElements.define('study-card', StudyCard);