diff app/assets/javascripts/application.coffee @ 442:1fc5ddcde386

Some refactors
author nanaya <me@myconan.net>
date Thu, 10 Nov 2016 21:58:25 +0900
parents f0b4d6bec50e
children
line wrap: on
line diff
--- a/app/assets/javascripts/application.coffee	Thu Nov 10 21:50:27 2016 +0900
+++ b/app/assets/javascripts/application.coffee	Thu Nov 10 21:58:25 2016 +0900
@@ -13,14 +13,12 @@
     .focus()
 
 
-pasteText = ->
-  $(".js-paste-paste").val() || $(".js-paste-pl").text()
-
+loadLanguageItems = ->
+  $languageSelectBox = $("#paste_language")
+  return unless $languageSelectBox.length
 
-setHash = (newLocation = window.location.pathname) ->
-  return unless history.replaceState
-
-  history.replaceState null, "", newLocation
+  for language in hljs.listLanguages().sort()
+    $languageSelectBox.append $("<option />", value: language, text: language)
 
 
 markdownfy = (plaintext) ->
@@ -29,50 +27,58 @@
   writer.render reader.parse(plaintext)
 
 
-$(document).on "click", ".js-show-tab-pl", ->
-  setHash()
+pasteText = ->
+  $(".js-paste-paste").val() || $(".js-paste-pl").text()
 
 
-$(document).on "click", ".js-show-tab-hl", ->
-  setHash "#hl"
+highlightText = ->
   $hlBox = $(".js-paste-hl")
-  return if $hlBox.data("processed")
+  return if $hlBox.attr("data-processed") == "1"
 
   $hlBox.text pasteText()
 
   highlight = -> hljs.highlightBlock($hlBox[0])
   setTimeout highlight, 0
 
-  $hlBox.data "processed", true
+  $hlBox.attr "data-processed", "1"
 
 
-$(document).on "click", ".js-show-tab-md", ->
-  setHash "#md"
+markdownText = ->
   $mdBox = $(".js-paste-md")
-  return if $mdBox.data("processed")
+  return if $mdBox.attr("data-processed") == "1"
 
   $mdBox.html markdownfy(pasteText())
   $mdBox.find("a").attr("rel", "nofollow")
-  $mdBox.data "processed", true
+  $mdBox.attr "data-processed", "1"
 
 
-$(document).on "click", ".js-paste-preview-md", (e) ->
+setHash = (e) ->
+  return unless history.replaceState
+
+  newLocation = "##{e.currentTarget.getAttribute("data-mode")}"
+  if newLocation == "#pl"
+    newLocation = window.location.pathname
+
+  history.replaceState null, "", newLocation
+
+
+showPreview = (e) ->
   e.preventDefault()
   return if pasteText() == ""
   $(".js-paste-preview-md-box").html markdownfy(pasteText())
   $(".js-paste-preview-md-modal").modal "show"
 
 
-$(document).on "ready", ->
+switchToCurrentHash = ->
   return unless $(".js-showing-paste").length
 
   format = window.location.hash.slice(1)
-  $(".js-show-tab-#{format}").click()
+  $(".js-show-tab[data-mode=#{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)
+$ loadLanguageItems
+$ switchToCurrentHash
+$(document).on "click", ".js-paste-preview-md", showPreview
+$(document).on "click", ".js-show-tab[data-mode=hl]", highlightText
+$(document).on "click", ".js-show-tab[data-mode=md]", markdownText
+$(document).on "click", ".js-show-tab", setHash