forked from makersacademy/news-summary-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle.js
300 lines (295 loc) · 12.5 KB
/
bundle.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
"use strict";
(() => {
// build/articlesModel.js
var ArticlesModel = class {
constructor() {
this.setArticles = (data) => {
this.articles = data;
};
this.getArticles = () => {
return this.articles;
};
this.getSearchArticles = (searchInput) => {
if (searchInput == null)
searchInput = "";
const filteredArticles = this.articles.filter((article) => {
const titleAndAbstract = article.title + " " + article.abstract;
return titleAndAbstract.toLowerCase().includes(searchInput.toLowerCase());
});
return filteredArticles;
};
this.articles = [];
}
};
// build/articlesView.js
var __classPrivateFieldGet = function(receiver, state, kind, f) {
if (kind === "a" && !f)
throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _ArticlesView_clearArticles;
var _ArticlesView_getArticleColumnDiv;
var _ArticlesView_getBodyEl;
var _ArticlesView_getTitleEl;
var _ArticlesView_getAbstractEl;
var _ArticlesView_getSectionEl;
var _ArticlesView_getDateEl;
var _ArticlesView_formatDate;
var _ArticlesView_getBylineEl;
var _ArticlesView_getLinkEl;
var _ArticlesView_getImageEl;
var ArticlesView = class {
constructor() {
this.displayArticles = (articles) => {
__classPrivateFieldGet(this, _ArticlesView_clearArticles, "f").call(this);
for (let i = 0; i < articles.length; i += 2) {
const rowEl = document.createElement("div");
rowEl.className = "row";
rowEl.append(__classPrivateFieldGet(this, _ArticlesView_getArticleColumnDiv, "f").call(this, articles[i], i));
if (articles[i + 1]) {
rowEl.append(__classPrivateFieldGet(this, _ArticlesView_getArticleColumnDiv, "f").call(this, articles[i + 1], i + 1));
}
this.articlesContainerEl.append(rowEl);
}
};
this.addSearchEventHandler = (callback) => {
this.searchButton.addEventListener("click", () => {
callback(this.searchInput.value);
});
};
this.addResetEventHandler = (callback) => {
this.resetButton.addEventListener("click", () => {
this.searchInput.value = "";
callback();
});
};
_ArticlesView_clearArticles.set(this, () => {
var first = this.articlesContainerEl.firstElementChild;
while (first) {
first.remove();
first = this.articlesContainerEl.firstElementChild;
}
});
_ArticlesView_getArticleColumnDiv.set(this, (article, index) => {
const columnEl = document.createElement("div");
columnEl.className = "col-sm-6";
const cardEl = document.createElement("div");
cardEl.className = "card mb-3";
if (article.multimedia) {
cardEl.append(__classPrivateFieldGet(this, _ArticlesView_getImageEl, "f").call(this, article.multimedia[0]));
}
cardEl.append(__classPrivateFieldGet(this, _ArticlesView_getBodyEl, "f").call(this, article, index));
columnEl.append(cardEl);
return columnEl;
});
_ArticlesView_getBodyEl.set(this, (article, index) => {
const bodyEl = document.createElement("div");
bodyEl.className = "card-body";
bodyEl.id = "article-" + (index + 1);
bodyEl.append(__classPrivateFieldGet(this, _ArticlesView_getTitleEl, "f").call(this, article.title));
bodyEl.append(__classPrivateFieldGet(this, _ArticlesView_getSectionEl, "f").call(this, article.section));
bodyEl.append(__classPrivateFieldGet(this, _ArticlesView_getDateEl, "f").call(this, article.published_date));
bodyEl.append(__classPrivateFieldGet(this, _ArticlesView_getBylineEl, "f").call(this, article.byline));
bodyEl.append(__classPrivateFieldGet(this, _ArticlesView_getLinkEl, "f").call(this, article.url));
bodyEl.append(__classPrivateFieldGet(this, _ArticlesView_getAbstractEl, "f").call(this, article.abstract));
return bodyEl;
});
_ArticlesView_getTitleEl.set(this, (title) => {
const titleEl = document.createElement("h6");
titleEl.className = "card-title";
titleEl.textContent = title;
return titleEl;
});
_ArticlesView_getAbstractEl.set(this, (abstract) => {
const abstractEl = document.createElement("p");
abstractEl.className = "card-text";
abstractEl.textContent = abstract;
return abstractEl;
});
_ArticlesView_getSectionEl.set(this, (section) => {
const sectionEl = document.createElement("p");
sectionEl.className = "card-text mb-0";
const sectionSmall = document.createElement("small");
sectionSmall.className = "text-muted";
sectionSmall.textContent = section.toUpperCase();
sectionEl.append(sectionSmall);
return sectionEl;
});
_ArticlesView_getDateEl.set(this, (date) => {
const dateEl = document.createElement("p");
dateEl.className = "card-text mb-0";
const dateSmall = document.createElement("small");
dateSmall.className = "text-muted";
dateSmall.textContent = __classPrivateFieldGet(this, _ArticlesView_formatDate, "f").call(this, date);
dateEl.append(dateSmall);
return dateEl;
});
_ArticlesView_formatDate.set(this, (date) => {
const day = date.split("T")[0];
const time = date.split("T")[1].split("-")[0].slice(0, 5);
return day + " " + time;
});
_ArticlesView_getBylineEl.set(this, (byline) => {
const bylineEl = document.createElement("p");
bylineEl.className = "card-text mb-0";
const bylineSmall = document.createElement("small");
bylineSmall.className = "text-muted";
bylineSmall.textContent = byline;
bylineEl.append(bylineSmall);
return bylineEl;
});
_ArticlesView_getLinkEl.set(this, (url) => {
const wrapperEl = document.createElement("p");
wrapperEl.className = "card-text";
const smallEl = document.createElement("small");
const linkEl = document.createElement("a");
linkEl.href = url;
linkEl.textContent = "LINK";
smallEl.append(linkEl);
wrapperEl.append(smallEl);
return wrapperEl;
});
_ArticlesView_getImageEl.set(this, (imageObj) => {
const imageEl = document.createElement("img");
imageEl.className = "card-img-top mt-4 mx-auto";
imageEl.src = imageObj.url;
imageEl.alt = imageObj.caption;
imageEl.style.width = "200px";
return imageEl;
});
this.articlesContainerEl = document.querySelector("#articles-container");
this.resetButton = document.querySelector("#reset-button");
this.searchButton = document.querySelector("#search-button");
this.searchInput = document.querySelector("#search-query");
}
};
_ArticlesView_clearArticles = /* @__PURE__ */ new WeakMap(), _ArticlesView_getArticleColumnDiv = /* @__PURE__ */ new WeakMap(), _ArticlesView_getBodyEl = /* @__PURE__ */ new WeakMap(), _ArticlesView_getTitleEl = /* @__PURE__ */ new WeakMap(), _ArticlesView_getAbstractEl = /* @__PURE__ */ new WeakMap(), _ArticlesView_getSectionEl = /* @__PURE__ */ new WeakMap(), _ArticlesView_getDateEl = /* @__PURE__ */ new WeakMap(), _ArticlesView_formatDate = /* @__PURE__ */ new WeakMap(), _ArticlesView_getBylineEl = /* @__PURE__ */ new WeakMap(), _ArticlesView_getLinkEl = /* @__PURE__ */ new WeakMap(), _ArticlesView_getImageEl = /* @__PURE__ */ new WeakMap();
// build/apiKey.js
var apiKey = "gDVZVg6YSTXr6z3KCiVstB3XgJuOSx2G";
// build/newYorkTimesApi.js
var __awaiter = function(thisArg, _arguments, P, generator) {
function adopt(value) {
return value instanceof P ? value : new P(function(resolve) {
resolve(value);
});
}
return new (P || (P = Promise))(function(resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}
function rejected(value) {
try {
step(generator["throw"](value));
} catch (e) {
reject(e);
}
}
function step(result) {
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
}
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var NewYorkTimesApi = class {
constructor() {
this.getArticles = () => __awaiter(this, void 0, void 0, function* () {
const articles = yield this.getArticlesHome();
return articles;
});
this.getArticlesHome = () => __awaiter(this, void 0, void 0, function* () {
const path = "home.json";
const url = this.url + path + this.apiKey;
const articles = yield fetch(url).then((res) => res.json());
return articles.results;
});
this.url = "https://api.nytimes.com/svc/topstories/v2/";
this.apiKey = `?api-key=${apiKey}`;
}
};
// build/articlesController.js
var __awaiter2 = function(thisArg, _arguments, P, generator) {
function adopt(value) {
return value instanceof P ? value : new P(function(resolve) {
resolve(value);
});
}
return new (P || (P = Promise))(function(resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}
function rejected(value) {
try {
step(generator["throw"](value));
} catch (e) {
reject(e);
}
}
function step(result) {
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
}
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __classPrivateFieldGet2 = function(receiver, state, kind, f) {
if (kind === "a" && !f)
throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _ArticlesController_addSearchEventHandler;
var _ArticlesController_loadSearchArticles;
var _ArticlesController_addResetEventHandler;
var ArticlesController = class {
constructor(model2, view2, api2) {
this.loadArticles = () => __awaiter2(this, void 0, void 0, function* () {
try {
const data = yield this.api.getArticles();
this.model.setArticles(data);
const articles = this.model.getArticles();
this.view.displayArticles(articles);
} catch (error) {
console.log(error);
}
});
_ArticlesController_addSearchEventHandler.set(this, () => {
this.view.addSearchEventHandler(__classPrivateFieldGet2(this, _ArticlesController_loadSearchArticles, "f"));
});
_ArticlesController_loadSearchArticles.set(this, (searchInput) => __awaiter2(this, void 0, void 0, function* () {
try {
const data = yield this.api.getArticles();
this.model.setArticles(data);
const articles = this.model.getSearchArticles(searchInput);
this.view.displayArticles(articles);
} catch (error) {
console.log(error);
}
}));
_ArticlesController_addResetEventHandler.set(this, () => {
this.view.addResetEventHandler(this.loadArticles);
});
this.model = model2;
this.view = view2;
this.api = api2;
__classPrivateFieldGet2(this, _ArticlesController_addSearchEventHandler, "f").call(this);
__classPrivateFieldGet2(this, _ArticlesController_addResetEventHandler, "f").call(this);
}
};
_ArticlesController_addSearchEventHandler = /* @__PURE__ */ new WeakMap(), _ArticlesController_loadSearchArticles = /* @__PURE__ */ new WeakMap(), _ArticlesController_addResetEventHandler = /* @__PURE__ */ new WeakMap();
// build/index.js
var api = new NewYorkTimesApi();
var view = new ArticlesView();
var model = new ArticlesModel();
var controller = new ArticlesController(model, view, api);
controller.loadArticles();
})();