-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathduma.js
108 lines (92 loc) · 3.64 KB
/
duma.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
"use strict"
/*******************************************
* Resize the image to fit the user's screen
********************************************/
function placeImage(w, h) {
w = window.screen.availWidth;
h = window.screen.availHeight;
document.body.innerHTML += '<img src=' + '"https://source.unsplash.com/collection/347929/' + w + 'x' + h + '"' + ' id="bg" alt="">';
}
/**************************************
* Get the date and place it at the top
***************************************/
function getDate() {
var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var date = new Date();
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
var marker;
if (day == 1 || day == 21) {
marker = "st";
} else if (day == 2 || day == 22) {
marker = "nd";
} else if (day == 3 || day == 23) {
marker = "rd";
} else {
marker = "th";
}
document.getElementById('date').innerHTML = monthNames[monthIndex] + ' ' + day + marker + ' ' + year;
}
/*******************************************************************
* Use the Wordnik API to get the word of the day and its definition
********************************************************************/
function theWord(callback) {
var baseUrl = "https://api.wordnik.com/v4/words.json/wordOfTheDay?api_key=";
var apiKey = "1380d58b8b5c33325130c0e8f340be6bc6fba6f7bb65bfc6f";
var apiUrl = baseUrl + apiKey;
//A promise is needed here, as without it the second API call would return before pronounceIt() can execute the callback, and we would get, well, nothing, instead of the actual word.
return new Promise(function(resolve, reject) {
$.ajax({
type: "GET",
url: apiUrl,
dataType: "json",
success: function(data) {
$("#word").append(data.word);
$("#defin").append(data.definitions[0].text);
resolve();
}
});
});
}
/**********************************************************
* Use the Wordnik API to get the pronunciation of the word
***********************************************************/
function pronounceIt() {
var baseUrl = "https://api.wordnik.com/v4/word.json/"
var apiKey = "1380d58b8b5c33325130c0e8f340be6bc6fba6f7bb65bfc6f";
var word = $("#word").text();
//var word = "test";
var apiUrl = baseUrl + word + "/pronunciations?useCanonical=false&typeFormat=ahd&limit=50&api_key=" + apiKey;
$("#link").attr("href", "http://www.dictionary.com/browse/" + word + "?s=t"); //link to dictionary.com page
$.ajax({
type: "GET",
url: apiUrl,
dataType: "json",
success: function(data) {
if (data.length > 0) {
var input = data[0].raw;
var output = "[" + input.slice(1,-1) + "]";
$("#pronun").append(output);
console.log(input);
}
}
});
}
/****************************
* Fade in the image and text
*****************************/
$(document).ready(function() {
placeImage();
getDate();
theWord().then(pronounceIt);
$('img').css('opacity', 1);
$('body').css('opacity', 1);
});
$('#favorites').click(function() {
$('#fave-panel').addClass('hide');
})
//store items in favorites by clicking a heart icon
// use localStorage
//https://developer.mozilla.org/en/docs/Web/API/Window/localStorage
//http://codepen.io/CrocoDillon/pen/pIlKB?editors=0010