forked from staltz/frontmen-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathy.js
More file actions
223 lines (190 loc) · 6.87 KB
/
Copy pathy.js
File metadata and controls
223 lines (190 loc) · 6.87 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// https://github.com/jneidel/project-manager/blob/021ca104fd37ad05008203ea612a232782da4193/src/scripts/app.js
/* globals Vue url axios */
/* eslint-disable no-alert */
// Listening for item/title changes
const setListener = {
item( item ) {
let originalItem = item.value;
item.addEventListener( "keydown", async () => {
if ( event.which === 13 ) {
const parentNode = item.parentNode.parentNode.parentNode;
const titleNode = parentNode.children;
let cardSide;
if ( parentNode.className.match( /back/ ) === null ) {
cardSide = "front";
} else {
cardSide = "back";
}
if ( titleNode.length == 3 ) {
var title = titleNode[1].value;
var lastItem = parentNode
.children[2]
.children[parentNode.children[2].children.length - 1]
.children[1];
} else {
var title = titleNode[0].value;
var lastItem = titleNode[1]
.children[titleNode[1].children.length - 1]
.children[1];
}
if ( lastItem === item ) {
axios.post( "/api/add-new-item", { cardSide, title } );
if ( titleNode.length == 3 ) {
var newItemWrapper = parentNode.children[2].appendChild( document.createElement( "li" ) );
} else {
var newItemWrapper = parentNode.children[1].appendChild( document.createElement( "li" ) );
}
const span = newItemWrapper.appendChild( document.createElement( "span" ) );
const input = newItemWrapper.appendChild( document.createElement( "input" ) );
span.classes = "bullet";
span.innerHTML = "● ";
span.style = "font-size: 0.8rem;";
input.type = "text";
input.classes = "item";
setListener.item( input );
setListener.bullet( span );
}
await axios.post( "/api/update", {
updatedItem: item.value,
oldItem : originalItem,
cardSide,
title,
} );
originalItem = item.value;
}
} );
},
title( title ) {
let originalTitle = title.value;
title.addEventListener( "keydown", async () => {
if ( event.which === 13 ) {
if ( title.value.length >= 20 ) {
alert( `The title "${title.value}" will probably be cut off as its too long.
${title.value.length}` );
}
const parent = title.parentNode.parentNode.children;
if ( !title.parentNode.className.match( /back/ ) ) {
parent[1].children[1].value = title.value;
} else {
parent[0].children[0].value = title.value;
}
await axios.post( "/api/update", { updatedTitle: title.value, title: originalTitle } );
originalTitle = title.value;
}
} );
},
bullet( bullet ) {
async function removeItem() {
const item = bullet.parentNode.children[1].value;
const title = bullet.parentNode.parentNode.parentNode.children[0].value;
let side = bullet.parentNode.parentNode.parentNode.className;
if ( side.match( /front/ ) ) {
side = "front";
} else {
side = "back";
}
bullet.parentNode.remove();
const response = await axios.post( "/api/remove-item", { title, item, side } );
checkResponse( response.data, "app", true );
}
bullet.addEventListener( "mouseenter", () => {
bullet.style = "color: #333;";
bullet.addEventListener( "click", removeItem );
} );
bullet.addEventListener( "mouseleave", () => {
bullet.style = "color: #F5F7FA";
bullet.removeEventListener( "click", removeItem );
} );
},
};
function setEventListeners() {
const items = document.getElementsByClassName( "item" );
const titles = document.getElementsByClassName( "title" );
const bullets = document.getElementsByClassName( "bullet" );
for ( const item of items ) {
setListener.item( item );
}
for ( const title of titles ) {
setListener.title( title );
}
for ( const bullet of bullets ) {
setListener.bullet( bullet );
}
const cards = document.getElementsByClassName( "card" );
// flip cards
function flipCard( card ) {
let cardState = "front";
card.addEventListener( "dblclick", ( event ) => {
const el = event.target.tagName;
const permitted = [ "DIV", "P" ];
if ( ~permitted.indexOf( el ) ) {
if ( cardState === "front" ) {
card.style.transform = "rotateY( 180deg )";
cardState = "back";
} else {
card.style.transform = "rotateY( 0deg )";
cardState = "front";
}
}
} );
}
// Add new card
const cardListenerCallback = function cardListenerCallbackWrapper() {
setNewCardToInput( this, cardListenerCallback );
};
async function setNewCardToInput( cardToBeSet, callingFunction ) {
cardToBeSet.removeEventListener( "dblclick", callingFunction );
cardToBeSet.className = "card";
cardToBeSet.innerHTML = `
<div class="front inner">
<input class="title" type="text" placeholder="Add title">
<ul>
<li>
<span class="bullet">● </span>
<input class="item" type="text" placeholder="Add items">
</li>
</ul>
</div>
<div class="back inner">
<p class="future">Future</p>
<input class="title" type="text" placeholder="Add title">
<ul>
<li>
<span class="bullet">● </span>
<input class="item" type="text" placeholder="Add items">
</li>
</ul>
</div>
`;
for ( const item of cardToBeSet.children ) {
const side = item.children.length == 3 ? 2 : 1;
setListener.item( item.children[ side ].children[0].children[0] );
}
for ( const title of cardToBeSet.children ) {
const side = title.children.length == 3 ? 1 : 0;
setListener.title( title.children[ side ] );
}
for ( const bullet of cardToBeSet.children ) {
const side = bullet.children.length == 3 ? 2 : 1;
setListener.bullet( bullet.children[ side ].children[0].children[0] );
}
flipCard( cardToBeSet );
const content = document.getElementById( "inner" );
const newCard = content.appendChild( document.createElement( "span" ) );
newCard.innerHTML += `<img class="addCard" src="img/add.png">`;
newCard.className = "card addCardContainer";
newCard.addEventListener( "dblclick", cardListenerCallback );
const cardIdRequest = await axios.post( "/api/generate-cardId" );
const cardId = cardIdRequest.data._id;
axios.post( "/api/add-new-card", { _id: cardId } );
}
for ( const card of cards ) {
const classes = card.className;
if ( !classes.match( /.addCardContainer/ ) ) {
flipCard( card );
} else {
card.addEventListener( "dblclick", cardListenerCallback );
}
}
}
setEventListeners();