diff app/assets/javascripts/application.coffee @ 398:851432f21603

Nope, failing at redesigning.
author nanaya <me@myconan.net>
date Sun, 06 Sep 2015 03:52:52 +0900
parents 8ceb373e32d8
children a18e19139f57
line wrap: on
line diff
--- a/app/assets/javascripts/application.coffee	Sat Jun 27 14:46:01 2015 +0900
+++ b/app/assets/javascripts/application.coffee	Sun Sep 06 03:52:52 2015 +0900
@@ -1,4 +1,58 @@
 #= require jquery
-#= require jquery_ujs
+#= require bootstrap
+#= require highlightjs
+#= require commonmark
+
+$(document).on "click", ".js-paste-clear", (e) ->
+  e.preventDefault()
+  $(".js-paste-paste")
+    .val("")
+    .focus()
+
+
+pasteText = ->
+  $(".js-paste-pl").text()
+
+
+setHash = (newLocation = window.location.pathname) ->
+  return unless history.replaceState
+
+  newLocation ||= window.location.pathname
+  history.replaceState null, "", newLocation
+
+
+$(document).on "click", ".js-show-tab-pl", ->
+  setHash()
+
 
-#= require new_paste
+$(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")
+
+  reader = new commonmark.Parser
+  writer = new commonmark.HtmlRenderer(safe: true)
+  parsed = reader.parse pasteText()
+
+  $mdBox.html writer.render(parsed)
+  $mdBox.find("a").attr("rel", "nofollow")
+  $mdBox.data "processed", true
+
+$(document).on "ready", ->
+  return if $(".js-showing-paste").length < 1
+
+  format = window.location.hash.slice(1)
+  $(".js-show-tab-#{format}").click()