From 5da6a54d218c6ff5760aff9cb14165bda1c0abc6 Mon Sep 17 00:00:00 2001 From: Adam Rice Date: Wed, 27 Jul 2016 10:57:03 +1000 Subject: [PATCH] Javascript styling, consistency, and legibility Ensure Javascript all conforms to standards. Remove unnecessary whitespace but add whitespace where it makes long strings easier to read. --- .../rails_admin/ra.filtering-select.js | 112 +++++++++++------- 1 file changed, 68 insertions(+), 44 deletions(-) diff --git a/app/assets/javascripts/rails_admin/ra.filtering-select.js b/app/assets/javascripts/rails_admin/ra.filtering-select.js index 4f824c27cc..0b9333007f 100644 --- a/app/assets/javascripts/rails_admin/ra.filtering-select.js +++ b/app/assets/javascripts/rails_admin/ra.filtering-select.js @@ -53,7 +53,7 @@ this._setOptionsSource(); this._initAutocomplete(); - this._initKeyEvent() + this._initKeyEvent(); this._overloadRenderItem(); this._autocompleteDropdownEvent(this.button); @@ -64,43 +64,54 @@ _getResultSet: function(request, data, xhr) { var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), 'i'); + + var spannedContent = function(content) { + return $('').text(content).html(); + }; + var highlighter = function(label, word) { - if(word.length > 0) { - return $.map(label.split(word), function(el, i){ - return $('').text(el).html(); - }).join($('').text(word)[0].outerHTML); - }else{ - return $('').text(label).html(); + if(word.length) { + return $.map( + label.split(word), + function(el) { + return spannedContent(el); + }) + .join($('') + .text(word)[0] + .outerHTML + ); + } else { + return spannedContent(label); } }; - return $.map(data, function(el, i) { - // match regexp only for local requests, remote ones are already filtered, and label may not contain filtered term. - if ((el.id || el.value) && (xhr || matcher.test(el.label))) { - return { - html: highlighter(el.label || el.id, request.term), - value: el.label || el.id, - id: el.id || el.value - }; - } + return $.map( + data, + function(el) { + var id = el.id || el.value; + var value = el.label || el.id; + // match regexp only for local requests, remote ones are already + // filtered, and label may not contain filtered term. + if (id && (xhr || matcher.test(el.label))) { + return { + html: highlighter(value, request.term), + value: value, + id: id + }; + } }); }, _getSourceFunction: function(source) { - - var self = this, - requestIndex = 0; + var self = this; + var requestIndex = 0; if ($.isArray(source)) { - return function(request, response) { response(self._getResultSet(request, source, false)); }; - } else if (typeof source === 'string') { - return function(request, response) { - if (this.xhr) { this.xhr.abort(); } @@ -122,9 +133,7 @@ } }); }; - } else { - return source; } }, @@ -188,7 +197,7 @@ return $('
') .addClass('input-group filtering-select col-sm-2') .attr('data-input-for', inputFor) - .css('float', 'left') + .css('float', 'left'); }, _initAutocomplete: function() { @@ -199,33 +208,46 @@ minLength: this.options.minLength, source: this._getSourceFunction(this.options.source), select: function(event, ui) { - var option = $('').attr('value', ui.item.id).attr('selected', 'selected').text(ui.item.value); - self.element.html(option); - self.element.trigger('change', ui.item.id); + var option = $('')); self.input.data('ui-autocomplete').term = ''; - $(self.element.parents('.controls')[0]).find('.update').addClass('disabled'); + $(self.element.parents('.controls')[0]) + .find('.update') + .addClass('disabled'); return false; } }); @@ -235,7 +257,9 @@ var self = this; return this.input.keyup(function() { - if ($(this).val().length) return; + if ($(this).val().length) { + return; + } /* Clear select options and trigger change if selected item is deleted */ return self.element @@ -245,7 +269,7 @@ }, _overloadRenderItem: function() { - return this.input.data('ui-autocomplete')._renderItem = function(ul, item) { + this.input.data('ui-autocomplete')._renderItem = function(ul, item) { return $('
  • ') .data('ui-autocomplete-item', item) .append($('')