-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathzen-utils.js
156 lines (135 loc) · 4.99 KB
/
zen-utils.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
/*
Zen Utils is a small library consisting of utility functions (reusable code)
written by Bruno Facca.
License: MIT
*/
"use strict";
// The IIFE creates a new scope so we don't pollute the global namespace.
(function zenUtils() {
class Library {
constructor() {
this.libraryName = "Zen Utils";
this.version = "0.1.1";
}
// Get the type of an object in a reliable way (typeof is NOT reliable)
classof(obj) {
return Object.prototype
.toString
.call(obj)
.replace(/^\[object\s(.*)\]$/, "$1");
}
// Test if array, object literal or string is empty.
// Deprecated: jQuery.isEmptyObject({}); obviates this.
isEmpty(obj) {
return !Object.keys(obj).length;
}
// Adds a leading zero if number < 10. Useful for formatting days,
// months, hours and minutes < 10. Only works with positive numbers.
padNumber(num) {
return (`0${num}`).slice(-2);
}
// Formats a date object as mm/dd/yyyy.
dateToMDY(date) {
const day = date.getDate();
// Month starts at zero (e.g., January is 0)
const month = date.getMonth() + 1;
const year = date.getFullYear();
return `${this.padNumber(month)}/${this.padNumber(day)}/${year}`;
}
/*
Display Bootstrap Alerts at the top of the page (formatted the same way
as Rail's flash messages.
Available categories: success, info, warning and danger.
Depends on:
a) jQuery;
b) Existence of a DIV with ID "flash-message".
*/
showAlert(msg, bootstrapContext, autoCloseTimeMs) {
let icon;
let bootstrapClass;
bootstrapClass = bootstrapContext;
switch (bootstrapContext) {
case "danger":
icon = "exclamation-sign";
break;
case "success":
icon = "ok";
break;
case "warning":
icon = "info-sign";
break;
default:
bootstrapClass = "info";
icon = "info-sign";
}
// Generate HTML for the alert and append to a div with ID
// "flash-messages".
const alertDiv = $(
`<div class="alert-dismissable alert alert-${bootstrapClass}">
<span class="glyphicon glyphicon-${icon}" aria-hidden="true"> </span>
<a class="close" data-dismiss="alert">×</a>${msg}</div>`
).appendTo("#flash-messages").hide().slideDown();
// Scroll to the top of the page to ensure the alert is visible
$("html, body").animate({ scrollTop: 0 }, "slow");
// If the "autoCloseTimeMs" optional argument is supplied, close
// the alert after the specified time (in milliseconds).
if (typeof autoCloseTimeMs !== "undefined") {
return alertDiv.delay(autoCloseTimeMs).slideUp(200);
}
return undefined;
}
handleAjaxError(jqXHR, textStatus, errorThrown) {
let msg;
let report = false;
if (jqXHR.status === 0) {
msg = "Could not connect to server. Please check your " +
"internet connection.";
} else if (jqXHR.status === 500) {
msg = "Internal Server Error. Please try again later.";
report = true;
} else if (jqXHR.status === 404) {
msg = "Requested page not found.";
report = true;
} else if (textStatus === "parsererror") {
msg = "JSON parse error.";
report = true;
} else if (textStatus === "timeout") {
msg = "No response from the server. Please check your " +
"internet connection.";
}
if (report) {
msg += " Please try again and contact us if the problem" +
" persists.";
}
const truncatedResponseText = (jqXHR.responseText !== undefined) ?
jqXHR.responseText.substring(0, 200) : null;
// Display Bootstrap alert
ZenUtils.showAlert(msg, "danger", 10000);
const errorText = `
jqXHR.readyState: ${jqXHR.readyState}
jqXHR.status: ${jqXHR.status}
jqXHR.statusText: ${jqXHR.statusText}
---------------------------------------------------------------
jqXHR.getAllResponseHeaders():
${jqXHR.getAllResponseHeaders()}
---------------------------------------------------------------
jqXHR.responseText (first 200 characters):
${truncatedResponseText}
---------------------------------------------------------------
The context (this) of the callback contains:
${JSON.stringify(this, null, 4)}
---------------------------------------------------------------
`;
// Remove leading whitespaces (due to indentation) from template literal.
console.error(errorText.replace(/^\s{8}/gm, ""));
}
} // end of the the Library class declaration
// Avoid name conflicts. Define "ZenUtils" in the global object for our
// library, unless that variable name is already taken.
if (typeof (ZenUtils) === "undefined") {
window.ZenUtils = new Library();
} else {
console.error(`Unable to initialize ${ZenUtils.libraryName}: a variable
called ZenUtils is already defined.`);
}
}());