Files
nairobi/app/assets/javascripts/polls.js.coffee
Javi Martín ba325526ae Use Array.apply to repeat a loop
In JavaScript we cannot easily repeat something N times, and
CoffeeScript's range loop compiles into complex JavaScript.

We could also use `Array.from({ length: N })`, but that's not supported
by old browsers like Internet Explorer 11.
2019-09-11 03:14:17 +02:00

34 lines
1018 B
CoffeeScript

"use strict"
App.Polls =
generateToken: ->
strings = Array.apply(null, length: 6).map ->
Math.random().toString(36).substr(2) # remove `0.`
strings.join("").substring(0, 64)
replaceToken: (token) ->
$(".js-question-answer").each ->
token_param = this.search.slice(-6)
if token_param == "token="
this.href = this.href + token
initialize: ->
token = App.Polls.generateToken()
App.Polls.replaceToken(token)
$(".zoom-link").on "click", (event) ->
element = event.target
answer = $(element).closest("div.answer")
if $(answer).hasClass("medium-6")
$(answer).removeClass("medium-6")
$(answer).addClass("answer-divider")
unless $(answer).hasClass("first")
$(answer).insertBefore($(answer).prev("div.answer"))
else
$(answer).addClass("medium-6")
$(answer).removeClass("answer-divider")
unless $(answer).hasClass("first")
$(answer).insertAfter($(answer).next("div.answer"))