Skip to content

Commit 67cd164

Browse files
Silic0nS0ldierJordan Mele
authored andcommitted
Fix for #809, light optimisations and style standardising for uf-alerts.js
1 parent 5f8fb91 commit 67cd164

1 file changed

Lines changed: 45 additions & 42 deletions

File tree

app/sprinkles/core/assets/userfrosting/js/uf-alerts.js

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
* ufAlerts can be initialized on any container element as follows:
99
*
10-
* $("#myDiv").ufAlerts(options);
10+
* $('#myDiv').ufAlerts(options);
1111
*
1212
* `options` is an object containing any of the following parameters:
1313
* @param {string} url The absolute URL from which to fetch flash alerts.
@@ -34,17 +34,17 @@
3434
* @author Alexander Weissman <https://alexanderweissman.com>
3535
*/
3636
;(function($, window, document, undefined) {
37-
"use strict";
37+
'use strict';
3838

3939
// Define plugin name and defaults.
40-
var pluginName = "ufAlerts",
40+
var pluginName = 'ufAlerts',
4141
defaults = {
42-
url : site.uri.public + "/alerts",
42+
url : site.uri.public + '/alerts',
4343
scrollToTop : true,
4444
scrollWhenVisible : false,
4545
agglomerate : false,
46-
alertMessageClass : "uf-alert-message",
47-
alertTemplateId : "uf-alert-template",
46+
alertMessageClass : 'uf-alert-message',
47+
alertTemplateId : 'uf-alert-template',
4848
DEBUG : false
4949
};
5050

@@ -62,18 +62,18 @@
6262
// Plugin variables
6363
this.alerts = [];
6464
this._newAlertsPromise = $.Deferred().resolve();
65-
this._alertTemplateHtml = $("#" + this.settings.alertTemplateId).html();
65+
this._alertTemplateHtml = $('#' + this.settings.alertTemplateId).html();
6666
this._alertTypePriorities = {
6767
danger : 3,
6868
warning: 2,
6969
success: 1,
7070
info : 0
7171
};
7272
this._alertTypeIcon = {
73-
danger : "fa-ban",
74-
warning: "fa-warning",
75-
success: "fa-check",
76-
info : "fa-info"
73+
danger : 'fa-ban',
74+
warning: 'fa-warning',
75+
success: 'fa-check',
76+
info : 'fa-info'
7777
};
7878

7979
return this;
@@ -89,11 +89,11 @@
8989
this.alerts.length = 0;
9090

9191
if (this.settings.agglomerate) {
92-
this.element.toggleClass("alert", false)
93-
.toggleClass("alert-info", false)
94-
.toggleClass("alert-success", false)
95-
.toggleClass("alert-warning", false)
96-
.toggleClass("alert-danger", false);
92+
this.element.toggleClass('alert', false)
93+
.toggleClass('alert-info', false)
94+
.toggleClass('alert-success', false)
95+
.toggleClass('alert-warning', false)
96+
.toggleClass('alert-danger', false);
9797
}
9898

9999
// Clear any alert HTML
@@ -106,9 +106,15 @@
106106
*/
107107
fetch: function() {
108108
// Set a promise, so that any chained calls after fetch can wait until the messages have been retrieved
109-
this._newAlertsPromise = $.getJSON(this.settings.url)
110-
.done($.proxy(this._fetchSuccess, this))
111-
.fail($.proxy(this._fetchFailure, this));
109+
this._newAlertsPromise = $.ajax({
110+
url: this.settings.url,
111+
cache: false
112+
}).then(
113+
// Success
114+
this._fetchSuccess.bind(this),
115+
// Failure
116+
this._fetchFailure.bind(this)
117+
);
112118

113119
return this.$element;
114120
},
@@ -117,19 +123,19 @@
117123
*/
118124
_fetchSuccess: function(alerts) {
119125
if (alerts != null) this.alerts = $.merge(this.alerts, alerts);
120-
this.$element.trigger("fetch." + this._name);
126+
this.$element.trigger('fetch.' + this._name);
121127
},
122128
/**
123129
* Failure callback for fetch
124130
*/
125131
_fetchFailure: function(response) {
126-
this.$element.trigger("error." + this._name);
127-
if ((typeof site !== "undefined") && site.debug.ajax && response.responseText) {
132+
this.$element.trigger('error.' + this._name);
133+
if ((typeof site !== 'undefined') && site.debug.ajax && response.responseText) {
128134
document.write(response.responseText);
129135
document.close();
130136
} else {
131137
if (this.settings.DEBUG) {
132-
console.warn("Error (" + response.status + "): " + response.responseText );
138+
console.warn('Error (' + response.status + '): ' + response.responseText );
133139
}
134140
}
135141
},
@@ -138,8 +144,8 @@
138144
*/
139145
push: function(options) {
140146
this.alerts.push({
141-
"type" : options[0],
142-
"message": options[1]
147+
type : options[0],
148+
message: options[1]
143149
});
144150

145151
return this.$element;
@@ -149,11 +155,11 @@
149155
*/
150156
render: function() {
151157
// Wait for promise completion, only if promise is unresolved.
152-
if (this._newAlertsPromise.state() == "resolved" || this._newAlertsPromise.state() == "rejected") {
158+
if (this._newAlertsPromise.state() == 'resolved' || this._newAlertsPromise.state() == 'rejected') {
153159
this._render();
154160
}
155161
else {
156-
$.when(this._newAlertsPromise).then($.proxy(this._render, this));
162+
$.when(this._newAlertsPromise).then(this._render.bind(this));
157163
}
158164

159165
return this.$element;
@@ -163,7 +169,7 @@
163169
*/
164170
_render: function() {
165171
// Holds generated HTML
166-
var alertHtml = "";
172+
var alertHtml = '';
167173
// Only compile alerts if there are alerts to display
168174
if (this.alerts.length > 0) {
169175
// Prepare template
@@ -172,23 +178,23 @@
172178
// If agglomeration is enabled, set the container to the highest priority alert type
173179
if (this.settings.agglomerate) {
174180
// Holds generated agglomerated alerts
175-
var alertMessage = "<ul>";
181+
var alertMessage = '<ul>';
176182

177183
// Determine overall alert priority
178-
var alertContainerType = "info";
184+
var alertContainerType = 'info';
179185
for (i = 0; i < this.alerts.length; i++) {
180186
if (this._alertTypePriorities[this.alerts[i].type] > this._alertTypePriorities[alertContainerType]) {
181187
alertContainerType = this.alerts[i].type;
182188
}
183189
}
184190

185191
// Compile each alert
186-
var aggTemplate = Handlebars.compile("<li class=" + this.settings.alertMessageClass + ">{{ message }}</li>");
192+
var aggTemplate = Handlebars.compile('<li class=' + this.settings.alertMessageClass + '>{{ message }}</li>');
187193
for (i = 0; i < this.alerts.length; i++) {
188194
alertMessage += aggTemplate(this.alerts[i]);
189195
}
190196

191-
alertMessage += "</ul>";
197+
alertMessage += '</ul>';
192198

193199
// Generate complete alert HTML
194200
alertHtml = alertTemplate({
@@ -200,7 +206,7 @@
200206
else {
201207
// Compile each alert.
202208
for (i = 0; i < this.alerts.length; i++) {
203-
alert = this.alerts[i];
209+
var alert = this.alerts[i];
204210

205211
// Inject icon
206212
alert.icon = this._alertTypeIcon[alert.type];
@@ -214,15 +220,15 @@
214220
this.$element.html(alertHtml);
215221

216222
// Scroll to top of alert location is new alerts output, and auto scrolling is enabled
217-
if (this.settings.scrollToTop && alertHtml !== "") {
223+
if (this.settings.scrollToTop && alertHtml !== '') {
218224
// Don't scroll if already visible, unless scrollWhenVisible is true
219225
if (!this._alertsVisible() || this.settings.scrollWhenVisible) {
220-
$("html, body").animate({ scrollTop: this.$element.offset().top }, "fast");
226+
$('html, body').animate({ scrollTop: this.$element.offset().top }, 'fast');
221227
}
222228
}
223229

224230
// Trigger render events
225-
this.$element.trigger("render." + this._name);
231+
this.$element.trigger('render.' + this._name);
226232
},
227233
/**
228234
* Returns true if alerts container is completely within the viewport.
@@ -243,13 +249,10 @@
243249
// Unbind any bound events
244250
this.$element.off('.' + this._name);
245251

246-
// Grab jQuery wrapped element before plugin destruction
247-
var $element = this.$element;
248-
249252
// Remove plugin from element
250253
this.$element.removeData(this._name);
251254

252-
return $element;
255+
return this.$element;
253256
}
254257
});
255258

@@ -272,11 +275,11 @@
272275
return instance[methodOrOptions]( Array.prototype.slice.call(arguments, 1));
273276
}
274277
else {
275-
console.warn( 'Method ' + methodOrOptions + ' is private!' );
278+
console.warn('Method ' + methodOrOptions + ' is private!');
276279
}
277280
}
278281
else {
279-
console.warn( 'Method ' + methodOrOptions + ' does not exist.' );
282+
console.warn('Method ' + methodOrOptions + ' does not exist.');
280283
}
281284
};
282285
})(jQuery, window, document);

0 commit comments

Comments
 (0)