forked from tatthien-zz/Mailinator-Instant-Box
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
163 lines (154 loc) · 4.3 KB
/
popup.js
File metadata and controls
163 lines (154 loc) · 4.3 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
(function($) {
/* Focus on search field when open popup */
$('#email-form input').focus();
const HOUR_IN_SECONDS = 3600;
const DAY_IN_SECONDS = 86400;
const WEEK_IN_SECONDS = 604800;
const MONTH_IN_SECONDS = 2678400;
const YEAR_IN_SECONDS = 31536000;
var numberEnding = function(number) {
return (number > 1) ? 's' : '';
}
var humanTimeDiff = function(time) {
if(time < 60) {
time = "a moment";
} else if(time >= 60 && time < HOUR_IN_SECONDS) {
time = Math.round(time / 60);
time = time + " minute" + numberEnding(time);
} else if(time >= HOUR_IN_SECONDS && time < DAY_IN_SECONDS) {
time = Math.round(time / HOUR_IN_SECONDS);
time = time + " hour" + numberEnding(time);
} else if(time >= DAY_IN_SECONDS && time < WEEK_IN_SECONDS) {
time = Math.round(time / DAY_IN_SECONDS);
time = time + " day" + numberEnding(time);
} else if(time >= WEEK_IN_SECONDS && time < MONTH_IN_SECONDS) {
time = Math.round(time / WEEK_IN_SECONDS);
time = time + " week" + numberEnding(time);
} else if(time >= MONTH_IN_SECONDS && time < YEAR_IN_SECONDS) {
time = Math.round(time / MONTH_IN_SECONDS);
time = time + " month" + numberEnding(time);
} else if(time >= YEAR_IN_SECONDS) {
time = Math.round(time / YEAR_IN_SECONDS);
time = time + " year" + numberEnding(time);
}
return time;
}
/******************
| COMPONENTS
******************/
var InboxItem = Vue.extend({
template: '#inbox-item-template',
props: ['model'],
data: function() {
return {
detail: '',
showDetail: false,
showButton: false,
showLoader: false,
itemTransitionName: 'item-fade',
buttonTransitionName: "scale",
detailTransitionName: "fade"
}
},
computed: {
humanTimeDiff: function() {
return humanTimeDiff(this.model.seconds_ago);
}
},
methods: {
/* Fetch mail by id */
fetchMail: function(msgid, event) {
event.preventDefault();
var self = this;
var target = $(event.currentTarget);
$.ajax({
method: 'GET',
dataType: 'json',
url: 'https://www.mailinator.com/fetch_email',
data: {
msgid: msgid,
zone: 'public'
},
beforeSend: function() {
// Show loader
self.showLoader = true;
},
success: function(response) {
// Binding data
self.detail = response.data;
// Show detail and close button
self.showDetail = true;
self.showButton = true;
// Hide loader
self.showLoader = false;
},
complete: function() {
}
});
},
/* Close detail */
closeDetail: function() {
var self = this;
self.showButton = false;
self.showDetail = false;
}
}
});
var ListInbox = Vue.extend({
template: '#list-inbox-template',
props: ['collection'],
components: {
'inbox-item': InboxItem
}
});
/* App container */
var MailinatorBox = new Vue({
el: '#app',
components: {
'list-inbox': ListInbox
},
data: {
listInbox: '',
mail: '',
isFetching: false
},
ready: function() {
$(this.el).find('#email-form input').focus();
},
computed: {
isEmpty: function() {
return (this.listInbox.length == 0) ? true : false;
}
},
methods: {
fetchInbox: function(event) {
console.log(self);
var self = this;
if(self.mail != '') {
var target = $(event.currentTarget);
$.ajax({
method: 'GET',
dataType: 'json',
url: 'https://www.mailinator.com/fetch_inbox',
data: {
x: 0,
to: self.mail,
zone: 'public'
},
beforeSend: function() {
target.blur();
self.isFetching = true;
},
success: function(response) {
self.listInbox = response.messages.reverse();
},
complete: function() {
target.focus();
self.isFetching = false;
}
});
}
}
}
});
})(jQuery);