forked from mizzao/meteor-sharejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharejs-client.coffee
70 lines (56 loc) · 2.01 KB
/
sharejs-client.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
Handlebars.registerHelper "sharejsText", (docid, options) ->
id = options.hash.id || "sharejsTextEditor"
return new Handlebars.SafeString Template._sharejsText(id: id, docid: docid)
aceConfigCallback = null
Handlebars.registerHelper "sharejsAce", (docid, options) ->
id = options.hash.id || "sharejsAceEditor"
aceConfigCallback = options.hash.callback
return new Handlebars.SafeString Template._sharejsAce(id: id, docid: docid)
host = window.location.host
cleanup = ->
# Detach event listeners from the textarea, unless you want crazy shit happenin'
if @_elem
@_elem.detach_share?()
@_elem = null
# Detach ace editor, if any
if @_editor
@_doc?.detach_ace?()
@_editor = null
# Close connection to the node server
if @_doc
@_doc.close()
@_doc = null
Template._sharejsText.rendered = ->
# close any previous docs if attached to rerender
cleanup.call(@)
@_elem = document.getElementById(@data.id);
sharejs.open @data.docid, 'text', 'http://' + host + '/channel', (error, doc) =>
if error
@_elem.disabled = true
console.log error
else
# Don't attach duplicate editors if re-render happens too fast
return unless @_editor? and doc.name is @data.docid
@_elem.disabled = false
doc.attach_textarea(@_elem)
@_doc = doc
Template._sharejsText.destroyed = ->
cleanup.call(@)
Template._sharejsAce.rendered = ->
# close any previous docs if attached to rerender
cleanup.call(@)
@_editor = ace.edit(@data.id)
@_editor.setReadOnly(true); # Disable editing until share is connected
sharejs.open @data.docid, 'text', 'http://' + host + '/channel', (error, doc) =>
if error
console.log error
else
# Don't attach duplicate editors if re-render happens too fast
return unless @_editor? and doc.name is @data.docid
doc.attach_ace(@_editor)
@_editor.setReadOnly(false);
@_doc = doc
# Configure the editor as requested
aceConfigCallback?(@_editor)
Template._sharejsAce.destroyed = ->
cleanup.call(@)