view app/assets/javascripts/application.coffee @ 425:9369ad2f2ce8

Simplify logic
author nanaya <me@myconan.net>
date Mon, 26 Oct 2015 14:09:13 +0900
parents f0b4d6bec50e
children 1fc5ddcde386
line wrap: on
line source

#= require jquery
#= require bootstrap/transition
#= require bootstrap/collapse
#= require bootstrap/modal
#= require bootstrap/tab
#= require highlightjs
#= require commonmark

$(document).on "click", ".js-paste-clear", (e) ->
  e.preventDefault()
  $(".js-paste-paste")
    .val("")
    .focus()


pasteText = ->
  $(".js-paste-paste").val() || $(".js-paste-pl").text()


setHash = (newLocation = window.location.pathname) ->
  return unless history.replaceState

  history.replaceState null, "", newLocation


markdownfy = (plaintext) ->
  reader = new commonmark.Parser
  writer = new commonmark.HtmlRenderer(safe: true)
  writer.render reader.parse(plaintext)


$(document).on "click", ".js-show-tab-pl", ->
  setHash()


$(document).on "click", ".js-show-tab-hl", ->
  setHash "#hl"
  $hlBox = $(".js-paste-hl")
  return if $hlBox.data("processed")

  $hlBox.text pasteText()

  highlight = -> hljs.highlightBlock($hlBox[0])
  setTimeout highlight, 0

  $hlBox.data "processed", true


$(document).on "click", ".js-show-tab-md", ->
  setHash "#md"
  $mdBox = $(".js-paste-md")
  return if $mdBox.data("processed")

  $mdBox.html markdownfy(pasteText())
  $mdBox.find("a").attr("rel", "nofollow")
  $mdBox.data "processed", true


$(document).on "click", ".js-paste-preview-md", (e) ->
  e.preventDefault()
  return if pasteText() == ""
  $(".js-paste-preview-md-box").html markdownfy(pasteText())
  $(".js-paste-preview-md-modal").modal "show"


$(document).on "ready", ->
  return unless $(".js-showing-paste").length

  format = window.location.hash.slice(1)
  $(".js-show-tab-#{format}").click()


$(document).on "ready", ->
  $languageSelectBox = $("#paste_language")
  return unless $languageSelectBox.length

  for language in hljs.listLanguages().sort()
    $languageSelectBox.append $("<option />", value: language, text: language)