-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
307 lines (270 loc) · 10.2 KB
/
script.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
const feedScript = document.createElement('script');
feedScript.src = 'http://np-ec2-nytimes-com.s3.amazonaws.com/dev/test/nyregion.js';
document.body.appendChild(feedScript);
const NYTD = {
data : {
language: 'english',
stories: []
},
render_section_front : function (params){
this.content = params.page.content;
this.parseData();
},
parseData : function(){
const self = this;
const feed = document.getElementById('feed');
const feedContainer = this.createElement('div', 'feed-container container is-hidden');
this.feedList = this.content.filter(type => type.name.includes('Column'));
// Loop through feed and add stories to data array
this.feedList.forEach((pages, index) => {
pages.collections.forEach((storyList, index) => {
if(storyList.renderStyle !== 'HeadlineOnly' && storyList.renderStyle !== 'Ads') {
storyList.assets.forEach((story) => {
if(story.headline && story.summary && story.images !== undefined && story.images.length > 0) {
// Separate English and Martian text
story.languages = self.addLanguages(story);
self.data.stories.push(story);
}
});
}
});
window.addEventListener('load', function () {
self.setHeight(0);
// Show Hidden Feed
const hiddenContent = document.getElementsByClassName('is-hidden');
Array.from(hiddenContent).forEach((content) => {
content.classList.remove('is-hidden');
});
});
});
// Loop through stories and append to the DOM
let count = 1;
this.data.stories.forEach((story, index) => {
let feedClass = 'feed-page flex-container';
if(index === 0 || index % 5 === 0) {
if(index === 0) {
feedClass += ' is-shown';
}
if(self.page) {
// Append story to feed
feedContainer.appendChild(self.page);
count++;
}
self.page = self.createElement('div', feedClass, null, 'data-page', count);
}
self.formatData(story);
});
// Add feed/pagination to container
feed.appendChild(feedContainer);
this.addPagination(feed, count - 1);
},
formatData: function(story) {
const storyContainer = this.createElement('div', 'story');
const leftContainer = this.createElement('div', 'l-side');
// Create date container
const storyDate = this.createElement('div', 'story-date story-content');
const dateText = new Date(story.lastPublished.substring(0, story.lastPublished.indexOf('T'))).toDateString().split(' ').slice(1).join(' ');
const date = this.createElement('div', null, dateText);
storyDate.appendChild(date);
leftContainer.appendChild(storyDate);
// Create content container
const storyContent = this.createElement('div', 'story-info story-content');
const storyUrl = this.createElement('a', null, story.url);
const headline = this.createElement('h3', 'headline is-content-toggle', story.languages.english.headline);
const summary = this.createElement('p', 'summary is-content-toggle', story.languages.english.summary);
const byline = this.createElement('span', 'byline is-content-toggle', story.languages.english.byline);
storyUrl.appendChild(headline);
storyContent.appendChild(storyUrl);
storyContent.appendChild(summary);
storyContent.appendChild(byline);
leftContainer.appendChild(storyContent);
// Create Image container and only if images present
const self = this;
let imageContainer;
// Loop through images and use image that is atleast 320px;
story.images[0].types.some(function(val) {
const imgUrl = 'http://nytimes.com/' + val.content;
imageContainer = self.createElement('div', 'story-image story-content');
storyImage = self.createElement('img', null, imgUrl);
return val.width >= 320;
});
imageContainer.appendChild(storyImage);
// Append story to DOM
storyContainer.appendChild(leftContainer);
storyContainer.appendChild(imageContainer);
self.page.appendChild(storyContainer);
},
setHeight: function(index) {
const feedList = document.getElementsByClassName('feed-page');
Array.from(feedList).forEach((feed) => {
let height = 0;
const storyList = feed.getElementsByClassName('story');
Array.from(storyList).forEach((story) => {
height += story.offsetHeight;
});
feed.style.height = height + 'px';
});
document.getElementsByClassName('feed-container').item(0).style.height = document.getElementsByClassName('feed-page').item(index).offsetHeight + 'px';
},
addLanguages: function(story) {
let languages = {
'english': {
headline: story.headline,
summary: story.summary,
byline: story.byline
},
'martian': {}
}
// Loop through english content
for(let content in languages.english) {
if(languages.english[content]) {
let wordArray = languages.english[content].split(" ");
languages.martian[content] = [];
// Loop through words and replace characters over 3
wordArray.forEach((word, index) => {
if(word.length > 3 && !word.match('[$]')) {
if(!word.match('[.,:!?\\-]')) {
/^[A-Z]/.test(word) ? word = 'Boinga' : word = 'boinga';
} else {
// If words have punctuation it replaces the words around them
if(word.split('.').length > 2) {
const wordArray = word.split('.');
const lastElem = wordArray.pop();
if(wordArray.length <= 3) {
word = word;
}
} else {
let splitWord = word.split('');
if(word.match('[.,:!?]') && word.match('[.,:!?]').index + 1 === word.length) {
const wordArray = word.split('');
const lastElem = wordArray.pop();
/^[A-Z]/.test(word) ? word = 'Boinga' + lastElem : word = 'boinga' + lastElem;
} else {
const wordArray = word.split('-');
wordArray.forEach((word, index) => {
if(word.length > 3) {
wordArray[index] = /^[A-Z]/.test(word) ? 'Boinga' : 'boinga';
}
});
word = wordArray.join('-');
}
}
}
}
languages.martian[content].push(word);
});
// Add Martian to languages object
languages.martian[content] = languages.martian[content].join(' ');
}
};
return languages;
},
addPagination: function(container, count) {
const pagination = this.createElement('div', 'pagination is-hidden');
const pageContainer = this.createElement('div', 'container');
const paginationText = this.createElement('span', 'text-pgn', '<span id="current-pgn">1</span> of ' + count);
const leftArrow = this.createElement('button', 'btn-arrow btn-arrow-prev btn js-arrow', null, 'data-direction', '-');
const leftArrowImg = this.createElement('img', 'arrow-img', './img/left-arrow.png');
const rightArrow = this.createElement('button', 'btn-arrow btn-arrow-next btn js-arrow', null, 'data-direction', '+');
const rightArrowImg = this.createElement('img', 'arrow-img', './img/right-arrow.png');
pagination.appendChild(pageContainer);
pageContainer.appendChild(paginationText);
leftArrow.appendChild(leftArrowImg);
pageContainer.appendChild(leftArrow);
rightArrow.appendChild(rightArrowImg);
pageContainer.appendChild(rightArrow);
container.appendChild(pagination);
this.bindClick(count);
this.bindResize();
},
bindClick: function(count) {
const self = this;
window.addEventListener('load', function () {
const arrows = document.getElementsByClassName('js-arrow');
const languages = document.getElementsByClassName('js-toggle-language');
let current = 1;
// Bind arrow click
Array.from(arrows).forEach((arrow, index) => {
document.getElementsByClassName('js-arrow')[index].addEventListener('click', function(e) {
// If there is annother page hides the current and shows the next
if((this.getAttribute('data-direction') === '+' && current + 1 <= count) || (this.getAttribute('data-direction') === '-' && current - 1 >= 1)) {
document.getElementsByClassName('is-shown').item(0).classList.remove('is-shown');
if(this.getAttribute('data-direction') === '+'){
current += 1;
} else {
current -= 1;
};
setTimeout(() => {
self.setHeight(current - 1);
}, 500);
setTimeout(() => {
document.querySelector("[data-page='" + current + "']").classList.add('is-shown');
}, 600);
document.querySelector('#current-pgn').textContent = current;
}
});
});
// Bind language toggle click
Array.from(languages).forEach((languageBtn) => {
languageBtn.addEventListener('click', function() {
const language = this.getAttribute('data-language');
if(self.data.language !== language) {
self.data.language = language;
self.data.stories.forEach((story, index) => {
self.toggleLanguage(this, story.languages, language, index);
});
}
});
});
});
},
bindResize: function() {
const self = this;
window.addEventListener('resize', function() {
setTimeout(() => {
self.setHeight(document.getElementsByClassName('is-shown').item(0));
}, 10);
});
},
toggleLanguage: function(btn, languageObj, language, index) {
// Toggle active class on buttons
document.querySelector('.btn.is-active').classList.remove('is-active');
btn.classList.add('is-active');
if(document.getElementsByClassName('story').item(index)) {
const feedItem = document.getElementsByClassName('story').item(index);
// Loop through story list and update content to language clicked
for(let item in languageObj[language]) {
if(languageObj[language][item]) {
const el = feedItem.querySelector("." + item);
const text = languageObj[language][item];
el.classList.add('is-hidden', 'is-hidden-text');
setTimeout(function(){
el.textContent = text;
}, 630);
setTimeout(function(){
el.classList.remove('is-hidden-text');
}, 660);
}
}
}
},
createElement: function(type, className = null, text = null, attribute = null, attributeVal = null) {
const element = document.createElement(type);
if(className) {
element.className = className;
}
if(text) {
if(type === 'img') {
element.src = text;
} else if(type === 'a') {
element.href = text;
} else {
element.innerHTML = text;
}
}
if(attribute) {
element.setAttribute(attribute, attributeVal);
}
return element;
}
}