Reduce local variables usage in CoffeeScript

Local variables are one of the things CoffeeScript doesn't compile to
modern JavaScript automatically: it uses `var` instead of `const` or
`let`.

Besides, using `$this = $(this)` is usually done to reference the
current object in another function where the current object is a
different one. Here we were using it with no clear purpose.
This commit is contained in:
Javi Martín
2019-07-01 17:11:54 +02:00
parent ba325526ae
commit cc7e0d586b
16 changed files with 33 additions and 63 deletions

View File

@@ -4,9 +4,8 @@ App.CheckboxToggle =
initialize: ->
$("[data-checkbox-toggle]").on "change", ->
$this = $(this)
$target = $($this.data("checkbox-toggle"))
if $this.is(":checked")
$target = $($(this).data("checkbox-toggle"))
if $(this).is(":checked")
$target.show()
else
$target.hide()

View File

@@ -41,12 +41,10 @@ App.ColumnsSelector =
$(".column-selector-item input").prop("checked", false)
toggleColumn: (event) ->
column = $(event.target).data("column")
App.ColumnsSelector.displayColumn(column)
App.ColumnsSelector.displayColumn($(event.target).data("column"))
displayColumn: (column) ->
item = $("#column_selector_" + column)
if item.prop("checked")
if $("#column_selector_" + column).prop("checked")
$("[data-field=" + column + "]").removeClass("hidden")
else
$("[data-field=" + column + "]").addClass("hidden")

View File

@@ -42,12 +42,9 @@ App.Comments =
initialize: ->
$("body .js-add-comment-link").each ->
$this = $(this)
unless $this.data("initialized") is "yes"
$this.on("click", ->
id = $(this).data().id
App.Comments.toggle_form(id)
unless $(this).data("initialized") is "yes"
$(this).on("click", ->
App.Comments.toggle_form($(this).data().id)
false
).data "initialized", "yes"

View File

@@ -3,9 +3,7 @@
App.Documentable =
initialize: ->
inputFiles = $(".js-document-attachment")
inputFiles.each ->
$(".js-document-attachment").each ->
App.Documentable.initializeDirectUploadInput(this)
$("#nested-documents").on "cocoon:after-remove", ->

View File

@@ -27,9 +27,7 @@ App.Forms =
synchronizeInputs: ->
progress_bar = "[name='progress_bar[percentage]']"
process_background = "[name='legislation_process[background_color]']"
process_font = ", [name='legislation_process[font_color]']"
processes = process_background + process_font
processes = "[name='legislation_process[background_color]'], [name='legislation_process[font_color]']"
banners = "[name='banner[background_color]'], [name='banner[font_color]']"
inputs = $("#{progress_bar}, #{processes}, #{banners}")

View File

@@ -89,14 +89,12 @@ App.Globalize =
App.Globalize.update_description()
$(".js-select-language").on "change", ->
locale = $(this).val()
App.Globalize.display_translations(locale)
App.Globalize.display_translations($(this).val())
$(".js-delete-language").on "click", (e) ->
e.preventDefault()
locale = $(this).data("locale")
App.Globalize.remove_language($(this).data("locale"))
$(this).hide()
App.Globalize.remove_language(locale)
$(".js-add-fields-container").on "cocoon:after-insert", ->
App.Globalize.enabled_locales().forEach (locale) ->

View File

@@ -3,8 +3,7 @@
App.Imageable =
initialize: ->
inputFiles = $(".js-image-attachment")
inputFiles.each ->
$(".js-image-attachment").each ->
App.Imageable.initializeDirectUploadInput(this)
$("#nested-image").on "cocoon:after-remove", ->

View File

@@ -20,7 +20,7 @@ App.LegislationAnnotatable =
try
if !document.execCommand("BackColor", false, colour)
App.LegislationAnnotatable.makeEditableAndHighlight colour
catch ex
catch
App.LegislationAnnotatable.makeEditableAndHighlight colour
return
@@ -182,9 +182,8 @@ App.LegislationAnnotatable =
current_user_id = $("html").data("current-user-id")
$(".legislation-annotatable").each ->
$this = $(this)
ann_id = $this.data("legislation-draft-version-id")
base_url = $this.data("legislation-annotatable-base-url")
ann_id = $(this).data("legislation-draft-version-id")
base_url = $(this).data("legislation-annotatable-base-url")
App.LegislationAnnotatable.app = new annotator.App()
.include ->
@@ -203,7 +202,4 @@ App.LegislationAnnotatable =
App.LegislationAnnotatable.app.start().then ->
App.LegislationAnnotatable.app.ident.identity = current_user_id
options = {}
options["legislation_draft_version_id"] = ann_id
App.LegislationAnnotatable.app.annotations.load(options)
App.LegislationAnnotatable.app.annotations.load(legislation_draft_version_id: ann_id)

View File

@@ -16,8 +16,7 @@ App.Managers =
initialize: ->
$(".generate-random-value").on "click", ->
password = App.Managers.generatePassword()
$("#user_password").val(password)
$("#user_password").val(App.Managers.generatePassword())
$(".show-password").on "click", ->
if $("#user_password").is("input[type='password']")

View File

@@ -3,9 +3,7 @@
App.Map =
initialize: ->
maps = $("*[data-map]")
maps.each ->
$("*[data-map]").each ->
App.Map.initializeMap(this)
$(".js-toggle-map").on
@@ -83,8 +81,7 @@ App.Map =
e.target.bindPopup(getPopupContent(data)).openPopup()
getPopupContent = (data) ->
content = "<a href='/budgets/#{data["budget_id"]}/investments/#{data["investment_id"]}'>#{data["investment_title"]}</a>"
return content
"<a href='/budgets/#{data["budget_id"]}/investments/#{data["investment_id"]}'>#{data["investment_title"]}</a>"
mapCenterLatLng = new (L.LatLng)(mapCenterLatitude, mapCenterLongitude)
map = L.map(element.id).setView(mapCenterLatLng, zoom)

View File

@@ -34,8 +34,8 @@ App.MarkdownEditor =
editor.toggleClass("fullscreen")
$(".fullscreen-container").toggleClass("medium-8", "medium-12")
span = $(this).find("span")
current_html = span.html()
if(current_html == span.data("open-text"))
if(span.html() == span.data("open-text"))
span.html(span.data("closed-text"))
else
span.html(span.data("open-text"))

View File

@@ -18,8 +18,7 @@ App.Polls =
App.Polls.replaceToken(token)
$(".zoom-link").on "click", (event) ->
element = event.target
answer = $(element).closest("div.answer")
answer = $(event.target).closest("div.answer")
if $(answer).hasClass("medium-6")
$(answer).removeClass("medium-6")

View File

@@ -4,6 +4,4 @@ App.SocialShare =
initialize: ->
$(".social-share-button a").each ->
element = $(this)
site = element.data("site")
element.append("<span class='show-for-sr'>#{site}</span>")
$(this).append("<span class='show-for-sr'>#{$(this).data("site")}</span>")

View File

@@ -6,10 +6,8 @@ App.Tags =
$tag_input = $("input.js-tag-list")
$("body .js-add-tag-link").each ->
$this = $(this)
unless $this.data("initialized") is "yes"
$this.on("click", ->
unless $(this).data("initialized") is "yes"
$(this).on("click", ->
name = "\"#{$(this).text()}\""
current_tags = $tag_input.val().split(",").filter(Boolean)

View File

@@ -2,9 +2,7 @@
App.TreeNavigator =
setNodes: (nodes) ->
children = nodes.children("ul")
children.each ->
nodes.children("ul").each ->
link = $(this).prev("a")
$('<span class="open"></span>').insertBefore(link)
App.TreeNavigator.setNodes($(this).children())
@@ -20,13 +18,12 @@ App.TreeNavigator =
$("[data-tree-navigator] span").on
click: ->
elem = $(this)
if(elem.hasClass("open"))
elem.removeClass("open").addClass("closed")
elem.siblings("ul").hide()
else if(elem.hasClass("closed"))
elem.removeClass("closed").addClass("open")
elem.siblings("ul").show()
if($(this).hasClass("open"))
$(this).removeClass("open").addClass("closed")
$(this).siblings("ul").hide()
else if($(this).hasClass("closed"))
$(this).removeClass("closed").addClass("open")
$(this).siblings("ul").show()
anchor = $(location).attr("hash")

View File

@@ -24,5 +24,4 @@ App.WatchFormChanges =
$(document).off("page:before-change").on("page:before-change", App.WatchFormChanges.checkChanges)
App.WatchFormChanges.forms().each ->
form = $(this)
form.data("watchChanges", form.serialize())
$(this).data("watchChanges", $(this).serialize())