-
Notifications
You must be signed in to change notification settings - Fork 25
/
script.coffee
184 lines (151 loc) · 4.08 KB
/
script.coffee
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# jQuery Helpers
# ---------------
# Indent using the tab key
# Via http://stackoverflow.com/a/6140696
$.fn.tabify = ->
@keydown (e) ->
# tab was pressed
return if e.keyCode != 9 or e.ctrlKey or e.shiftKey
# get caret position/selection
start = @selectionStart
end = @selectionEnd
$this = $(this)
value = $this.val()
# set textarea value to: text before caret + tab + text after caret
$this.val("#{value.substring(0, start)}\t#{value.substring(end)}")
# put caret at right position again (add one for the tab)
@selectionStart = @selectionEnd = start + 1
# prevent the focus lose
e.preventDefault()
# Conditionnally ask for confirmation before closing the window
# Via http://stackoverflow.com/a/2923258/311657
# Takes a callback that returns the message to show or nothing.
# Example:
# $(window).confirmClose(function(event) {
# if (true)
# return "Are you sure?"
# })
$.fn.confirmClose = (callback) ->
this[0].onbeforeunload = (event) ->
message = callback(event)
return if !message
if typeof event == 'undefined'
event = window.event
if event
event.returnValue = message
message
# Edith
# ------
# Edith initializer
# Arguments:
# - form: jQuery object containing a form with a textarea to watch
# Example:
# new Edith($('form'))
class Edith
constructor: (@form) ->
# Configure
@textarea = @form.find('textarea')
@changed = false
@saving = false
@favicon('black')
@textarea.focus()
# Bind events
@textarea.on('input', @onChange)
@textarea.tabify()
$(window).confirmClose(@onConfirmClose)
$(window).keydown(@onKeydown)
# Replace the current favicon with the element's text.
# Arguments:
# - color: to give to faviconImage.
# Example:
# this.favicon("pink")
favicon: (color) ->
href = @faviconImage(@textarea.val(), color)
link = $('<link rel="icon" />').attr('href', href)
$('link[rel~=icon]').remove()
$('head').append(link)
# Returns the data-url for image resembling a page.
# Arguments:
# - text: to mimic the first lines
# - color: for these lines.
# Example:
# this.faviconImage("Line1\nLine2", "orange")
faviconImage: (text, color) ->
# Prepare text
lines = []
text = text.split(/\n/)
for textLine in text[0..4]
length = $.trim(textLine).length
length = 10 if length > 10
lines.push $.trim(textLine).length
# Canvas
canvas = document.createElement('canvas')
ctx = canvas.getContext('2d')
canvas.width = 16
canvas.height = 16
# Page
ctx.rect(1, 1, 14, 14)
ctx.fillStyle = '#eee'
ctx.strokeStyle = '#333'
ctx.stroke()
ctx.fill()
# Lines
ctx.strokeStyle = color
ctx.beginPath()
ctx.ln
linesY = [4, 7, 10, 13]
for length, i in lines
ctx.moveTo(3, linesY[i])
ctx.lineTo(3 + length, linesY[i])
ctx.stroke()
canvas.toDataURL("image/png")
# Redirect after all changes have been saved
# Arguments:
# - url: to redirect to
# Example:
# this.redirect("/")
redirect: (url) ->
if @saving
setTimeout(->
@redirect(url)
, 100)
else
window.location = url
checkForChange: ->
if @changed && !@saving
@changed = false
@save()
save: ->
@saving = true
text = @textarea.val()
$.ajax
type: if text == "" then "delete" else "put"
url: @form.attr('action')
data: { text: text }
success: @onSave
error: @onSaveError
# Events
onKeydown: (event) =>
if event.ctrlKey && String.fromCharCode(event.keyCode) == "E"
href = window.location.href
href = href.replace(/\/$/, "/index")
@redirect "#{href}.html"
onConfirmClose: =>
if @saving or @changed
"Your last modifications didn't get saved yet."
onChange: =>
@favicon('grey')
@changed = true
@checkForChange()
onSave: =>
@saving = false
@checkForChange()
unless @saving
@favicon('black')
onSaveError: =>
@saving = false
@checkForChange()
unless @saving
@favicon('red')
# Start
new Edith($('#save'))