File: docs.coffee

package info (click to toggle)
coffeescript 1.12.8~dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,748 kB
  • sloc: javascript: 815; makefile: 57; xml: 9; sh: 6
file content (95 lines) | stat: -rw-r--r-- 2,884 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
sourceFragment = "try:"

# Set up the compilation function, to run when you stop typing.
compileSource = ->
  source = $('#repl_source').val()
  results = $('#repl_results')
  window.compiledJS = ''
  try
    window.compiledJS = CoffeeScript.compile source, bare: on
    el = results[0]
    if el.innerText
      el.innerText = window.compiledJS
    else
      results.text(window.compiledJS)
    results.removeClass 'error'
    $('.minibutton.run').removeClass 'error'
  catch {location, message}
    if location?
      message = "Error on line #{location.first_line + 1}: #{message}"
    results.text(message).addClass 'error'
    $('.minibutton.run').addClass 'error'

  # Update permalink
  $('#repl_permalink').attr 'href', "##{sourceFragment}#{encodeURIComponent source}"

# Listen for keypresses and recompile.
$('#repl_source').keyup -> compileSource()

# Use tab key to insert tabs
$('#repl_source').keydown (e) ->
  if e.keyCode is 9
    e.preventDefault()
    textbox = e.target
    # Insert tab character at caret or in selection
    textbox.value = textbox.value[0...textbox.selectionStart] + "\t" + textbox.value[textbox.selectionEnd...]
    # Put caret in correct position
    textbox.selectionEnd = ++textbox.selectionStart

# Eval the compiled js.
evalJS = ->
  try
    eval window.compiledJS
  catch error then alert error

# Load the console with a string of CoffeeScript.
window.loadConsole = (coffee) ->
  $('#repl_source').val coffee
  compileSource()
  $('.navigation.try').addClass('active')
  false

# Helper to hide the menus.
closeMenus = ->
  $('.navigation.active').removeClass 'active'

$('.minibutton.run').click -> evalJS()

# Bind navigation buttons to open the menus.
$('.navigation').click (e) ->
  return if e.target.tagName.toLowerCase() is 'a'
  return false if $(e.target).closest('.repl_wrapper').length
  if $(this).hasClass('active')
    closeMenus()
  else
    closeMenus()
    $(this).addClass 'active'
  false

$(document).on 'click', '[href="#try"]', (e) ->
  $('.navigation.try').addClass 'active'

# Dismiss console if Escape pressed or click falls outside console
# Trigger Run button on Ctrl-Enter
$(document.body)
  .keydown (e) ->
    closeMenus() if e.which == 27
    evalJS() if e.which == 13 and (e.metaKey or e.ctrlKey) and $('.minibutton.run:visible').length
  .click (e) ->
    return false if $(e.target).hasClass('minibutton')
    closeMenus()

$('#open_webchat').click ->
  $(this).replaceWith $('<iframe src="http://webchat.freenode.net/?channels=coffeescript" width="625" height="400"></iframe>')

$("#repl_permalink").click (e) ->
    window.location = $(this).attr("href")
    false

# If source code is included in location.hash, display it.
hash = decodeURIComponent location.hash.replace(/^#/, '')
if hash.indexOf(sourceFragment) == 0
    src = hash.substr sourceFragment.length
    loadConsole src

compileSource()