Skip to content

Commit eec1f6d

Browse files
Silic0nS0ldieralexweissman
authored andcommitted
JS linting, reliability improvements, and refreshing of cached DOM element for ufAlerts, ufCollection, ufForm
1 parent 219d0d2 commit eec1f6d

3 files changed

Lines changed: 18 additions & 16 deletions

File tree

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
this._defaults = defaults;
5757
this._name = pluginName;
5858

59+
// Detect changes to element attributes
60+
this.$element.attrchange({ callback: function (event) { this.element = event.target; }.bind(this) });
61+
5962
// Plugin variables
6063
this.alerts = [];
6164
this._newAlertsPromise = $.Deferred().resolve();
@@ -165,23 +168,23 @@
165168
if (this.alerts.length > 0) {
166169
// Prepare template
167170
var alertTemplate = Handlebars.compile(this._alertTemplateHtml, {noEscape: true});
168-
171+
var i;
169172
// If agglomeration is enabled, set the container to the highest priority alert type
170173
if (this.settings.agglomerate) {
171174
// Holds generated agglomerated alerts
172175
var alertMessage = "<ul>";
173176

174177
// Determine overall alert priority
175178
var alertContainerType = "info";
176-
for (var i = 0; i < this.alerts.length; i++) {
179+
for (i = 0; i < this.alerts.length; i++) {
177180
if (this._alertTypePriorities[this.alerts[i].type] > this._alertTypePriorities[alertContainerType]) {
178181
alertContainerType = this.alerts[i].type;
179182
}
180183
}
181184

182185
// Compile each alert
183186
var aggTemplate = Handlebars.compile("<li class=" + this.settings.alertMessageClass + ">{{ message }}</li>");
184-
for (var i = 0; i < this.alerts.length; i++) {
187+
for (i = 0; i < this.alerts.length; i++) {
185188
alertMessage += aggTemplate(this.alerts[i]);
186189
}
187190

@@ -196,7 +199,7 @@
196199
}
197200
else {
198201
// Compile each alert.
199-
for (var i = 0; i < this.alerts.length; i++) {
202+
for (i = 0; i < this.alerts.length; i++) {
200203
alert = this.alerts[i];
201204

202205
// Inject icon
@@ -211,7 +214,7 @@
211214
this.$element.html(alertHtml);
212215

213216
// Scroll to top of alert location is new alerts output, and auto scrolling is enabled
214-
if (this.settings.scrollToTop && alertHtml != "") {
217+
if (this.settings.scrollToTop && alertHtml !== "") {
215218
// Don't scroll if already visible, unless scrollWhenVisible is true
216219
if (!this._alertsVisible() || this.settings.scrollWhenVisible) {
217220
$("html, body").animate({ scrollTop: this.$element.offset().top }, "fast");

app/sprinkles/core/assets/local/core/js/uf-collection.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@
8181
var lateDefaults = {
8282
dropdownControl: this.$element.find('.js-select-new'),
8383
rowContainer: this.$element.find('tbody').first()
84-
}
84+
};
8585
this.settings = $.extend(true, {}, defaults, lateDefaults, options);
8686
this._defaults = defaults;
8787
this._name = pluginName;
8888

89+
// Detect changes to element attributes
90+
this.$element.attrchange({ callback: function (event) { this.element = event.target; }.bind(this) });
8991

9092
// Internal counter for adding rows to the collection. Gets updated every time `addRow` is called.
9193
this._rownum = 0;

app/sprinkles/core/assets/local/core/js/uf-form.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
this._defaults = defaults;
6565
this._name = pluginName;
6666

67+
// Detect changes to element attributes
68+
this.$element.attrchange({ callback: function (event) { this.element = event.target; }.bind(this) });
69+
6770
// Expose settings for 'onkeyup' until a better more event driven apporch is adopted.
6871
var settings = this.settings;
6972

@@ -176,13 +179,6 @@
176179
}
177180
});
178181

179-
// Detect changes to element attributes
180-
this.$element.attrchange({
181-
callback: $.proxy(function (event) {
182-
this.element = event.target;
183-
}, this)
184-
});
185-
186182
return this;
187183
}
188184

@@ -193,8 +189,9 @@
193189
*/
194190
_urlencodeData: function(form) {
195191
// Serialize and post to the backend script in ajax mode
192+
var serializedData;
196193
if (this.settings.binaryCheckboxes) {
197-
var serializedData = form.find(':input').not(':checkbox').serialize();
194+
serializedData = form.find(':input').not(':checkbox').serialize();
198195
// Get unchecked checkbox values, set them to 0
199196
form.find('input[type=checkbox]:enabled').each(function() {
200197
if ($(this).is(':checked')) {
@@ -205,7 +202,7 @@
205202
});
206203
}
207204
else {
208-
var serializedData = form.find(':input').serialize();
205+
serializedData = form.find(':input').serialize();
209206
}
210207

211208
return serializedData;
@@ -223,7 +220,7 @@
223220
var checkboxes = form.find('input[type=checkbox]:enabled');
224221
// Feature detection. Several browsers don't support `set`
225222
if (typeof formData.set !== 'function') {
226-
this.settings.msgTarget.ufAlerts("push", "danger", "Your browser is missing a required feature. This form will still attempt to submit, but if it fails, you'll need to use Chrome for desktop or FireFox for desktop.")
223+
this.settings.msgTarget.ufAlerts("push", "danger", "Your browser is missing a required feature. This form will still attempt to submit, but if it fails, you'll need to use Chrome for desktop or FireFox for desktop.");
227224
}
228225
else {
229226
checkboxes.each(function() {

0 commit comments

Comments
 (0)