Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 204 lines (185 sloc) 7.85 kB
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
1 # Extension module.
6c28d90 @max99x Coffee cleanup. No change in functionality.
max99x authored
2 # Encapsulates for all session/state loading saving logic.
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
3 # TODO(amasad): Graceful localStorage degradation to cookies.
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
4 $ = jQuery
21758d6 @amasad adapt to new backend.\n Fallback from html5 history to hashchange
amasad authored
5 PUSHSTATE_SUPPORTED = 'pushState' of history
e1109d0 @max99x Throttled saving by 2sec (just against accidental Ctrl+S holding);
max99x authored
6 WAIT_BETWEEN_SAVES = 2000
5869232 @amasad social buttons
amasad authored
7 SHARE_TEMPLATE =
8 twitter: ->
9 text = 'Check out my REPL session - '
10 related = 'replit'
11 url = window.location.href
12 uri = $.param {
13 text
14 url
15 related
16 }
17 """
18 <a href="https://twitter.com/share?#{uri}" target="_blank"></a>
19 """
6c28d90 @max99x Coffee cleanup. No change in functionality.
max99x authored
20
5869232 @amasad social buttons
amasad authored
21 facebook: ->
22 """
23 <a href="javascript:var d=document,f='http://www.facebook.com/share',l=d.location,e=encodeURIComponent,p='.php?src=bm&v=4&i=1315186262&u='+e(l.href)+'&t='+e(d.title);1;try{if (!/^(.*\.)?facebook\.[^.]*$/.test(l.host))throw(0);share_internal_bookmarklet(p)}catch(z) {a=function() {if (!window.open(f+'r'+p,'sharer','toolbar=0,status=0,resizable=1,width=626,height=436'))l.href=f+p};if (/Firefox/.test(navigator.userAgent))setTimeout(a,0);else{a()}}void(0)"></a>
24 """
6c28d90 @max99x Coffee cleanup. No change in functionality.
max99x authored
25
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
26 # Unofficial!
5869232 @amasad social buttons
amasad authored
27 gplus: ->
28 text = 'Check out my REPL session - ' + window.location.href
29 text = encodeURI text
30 """
31 <a href="https://m.google.com/app/plus/x/bggo8s9j8yqo/?v=compose&content=#{text}&login=1&pli=1&hideloc=1" target="_blank"></a>
32 """
6c28d90 @max99x Coffee cleanup. No change in functionality.
max99x authored
33
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
34 $.extend REPLIT,
35 session:
36 eval_history: []
21758d6 @amasad adapt to new backend.\n Fallback from html5 history to hashchange
amasad authored
37 pushState: (text) ->
38 if PUSHSTATE_SUPPORTED
6365e15 @amasad Don't clear the page hash on pushState. issue #47
amasad authored
39 # We only want to clear state #0 (Session path), restore the page hash
40 # after clearing the session path.
41 {hash} = window.location
21758d6 @amasad adapt to new backend.\n Fallback from html5 history to hashchange
amasad authored
42 window.history.pushState null, null, "/#{text}"
6365e15 @amasad Don't clear the page hash on pushState. issue #47
amasad authored
43 window.location.hash = hash
21758d6 @amasad adapt to new backend.\n Fallback from html5 history to hashchange
amasad authored
44 else
45 REPLIT.setHash 0, text
6c28d90 @max99x Coffee cleanup. No change in functionality.
max99x authored
46
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
47 # Resets application to its initial state (handler for language_loaded event).
48 reset_state = (e, lang_name) ->
49 localStorage.setItem 'lang_name', lang_name
b13176d @amasad Hide replay button after actual replay
amasad authored
50 $('#replay-button').hide()
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
51 @session = {}
52 @session.eval_history = []
21758d6 @amasad adapt to new backend.\n Fallback from html5 history to hashchange
amasad authored
53 REPLIT.pushState ''
6c28d90 @max99x Coffee cleanup. No change in functionality.
max99x authored
54
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
55 $ ->
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
56 # If there exists a REPLIT_DATA variable, then we are in a saved session.
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
57 if REPLIT_DATA?
58 # Load the language specified by the incoming session data.
5ec899e @max99x Fixed language not appearing in header when loading a session.
max99x authored
59 REPLIT.current_lang_name = REPLIT_DATA.language
dfd1c81 @amasad Adapted the session module to pager changes.
amasad authored
60 REPLIT.OpenPage 'workspace', ->
61 REPLIT.LoadLanguage REPLIT_DATA.language, ->
62 # Set the editor text.
a9005a9 @max99x Fixed ISMOBILE being tested on the wrong object in sessions.
max99x authored
63 REPLIT.editor.getSession().setValue REPLIT_DATA.editor_text if not REPLIT.ISMOBILE
dfd1c81 @amasad Adapted the session module to pager changes.
amasad authored
64 # Get the session data.
60df559 @max99x Updated sessions.coffee to the new sessions API.
max99x authored
65 REPLIT.session.id = REPLIT_DATA.session_id
66 REPLIT.session.rid = REPLIT_DATA.revision_id
dfd1c81 @amasad Adapted the session module to pager changes.
amasad authored
67 REPLIT.session.saved_eval_history = REPLIT_DATA.eval_history
68 # Show the replay button.
69 $('#replay-button').show()
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
70 # Delete the incoming session data from the server since we have
71 # extracted everything we neeed.
dfd1c81 @amasad Adapted the session module to pager changes.
amasad authored
72 delete window['REPLIT_DATA']
73 # On each language load after this one reset the state.
74 REPLIT.$this.bind 'language_loaded', reset_state
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
75 else
76 # We are not in a saved session.
77 # Safely bind the reset state function.
78 REPLIT.$this.bind 'language_loaded', reset_state
79 lang_name = localStorage.getItem('lang_name')
c78eac4 @max99x Fixed Analytics reporting 0 timing when loading language from localSt…
max99x authored
80 if lang_name?
81 REPLIT.loading_saved_lang = true
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
82
c78eac4 @max99x Fixed Analytics reporting 0 timing when loading language from localSt…
max99x authored
83 # We have a saved local settings for language to load. Delay this until
84 # the Analytics modules has set its hook so it can catch language loading.
85 $ ->
86 REPLIT.current_lang_name = lang_name
87 REPLIT.OpenPage 'workspace', ->
88 REPLIT.LoadLanguage lang_name
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
89 else
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
90 # This is the first visit; show language overlay.
21758d6 @amasad adapt to new backend.\n Fallback from html5 history to hashchange
amasad authored
91 $('#languages-back').bind 'click.language_modal', (e) ->
92 e.stopImmediatePropagation()
93 return false
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
94 $('#content-languages .language-group li').bind 'click.language_modal', (e) ->
21758d6 @amasad adapt to new backend.\n Fallback from html5 history to hashchange
amasad authored
95 REPLIT.Modal false
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
96
21758d6 @amasad adapt to new backend.\n Fallback from html5 history to hashchange
amasad authored
97 REPLIT.$this.bind 'language_loaded.language_modal', (e) ->
98 $('#languages-back').unbind 'click.language_modal'
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
99
5869232 @amasad social buttons
amasad authored
100 REPLIT.OpenPage 'languages'
21758d6 @amasad adapt to new backend.\n Fallback from html5 history to hashchange
amasad authored
101 REPLIT.Modal true
6c28d90 @max99x Coffee cleanup. No change in functionality.
max99x authored
102
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
103 # Click handler for the replay button.
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
104 $('#replay-button').click (e) ->
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
105 # Get the history comming from the server.
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
106 history = REPLIT.session.saved_eval_history
dfd1c81 @amasad Adapted the session module to pager changes.
amasad authored
107 locked = false
108 locked_queue = []
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
109 index = -1
110 # Executes a command from history and waits for the result to continue
111 # with the next command.
dfd1c81 @amasad Adapted the session module to pager changes.
amasad authored
112 handler = ->
113 if not locked
114 index++
115 if history[index]?
116 # Set the prompt text to the command in question.
117 REPLIT.jqconsole.SetPromptText history[index]
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
118 # Remove multiline handler from jqconsole to ensure it doesn't
119 # continue to the next line.
dfd1c81 @amasad Adapted the session module to pager changes.
amasad authored
120 _multiline = REPLIT.jqconsole.multiline_callback
121 REPLIT.jqconsole.multiline_callback = undefined
122 # Simulate an enter button on jqconsole.
123 REPLIT.jqconsole._HandleEnter()
124 # Reassign the multiline handler.
125 REPLIT.jqconsole.multiline_callback = _multiline
126 else
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
127 # There is no more commands; unbind the handler.
dfd1c81 @amasad Adapted the session module to pager changes.
amasad authored
128 REPLIT.$this.unbind 'result', handler
129 REPLIT.$this.unbind 'error', handler
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
130 # We are done with the eval history from the server; delete it.
dfd1c81 @amasad Adapted the session module to pager changes.
amasad authored
131 delete REPLIT.session['saved_eval_history']
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
132 else
dfd1c81 @amasad Adapted the session module to pager changes.
amasad authored
133 locked_queue.push handler
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
134
dfd1c81 @amasad Adapted the session module to pager changes.
amasad authored
135 input_lock = ->
136 locked = true
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
137
dfd1c81 @amasad Adapted the session module to pager changes.
amasad authored
138 input_unlock = ->
139 locked = false
140 fn = locked_queue.shift()
141 setTimeout fn, 100 if fn?
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
142
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
143 REPLIT.$this.bind 'result', handler
144 REPLIT.$this.bind 'error', handler
dfd1c81 @amasad Adapted the session module to pager changes.
amasad authored
145 REPLIT.$this.bind 'input', input_unlock
146 REPLIT.$this.bind 'input_request', input_lock
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
147 # Initiate the first handler to start executing history commands.
148 handler()
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
149 # This button can only be clicked once. Now hide it.
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
150 $(this).hide()
6c28d90 @max99x Coffee cleanup. No change in functionality.
max99x authored
151
e1109d0 @max99x Throttled saving by 2sec (just against accidental Ctrl+S holding);
max99x authored
152 saveSession = (e) ->
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
153 # Can't save if we haven't selected a language yet.
154 if not REPLIT.current_lang? then return
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
155 # Get the post data to save.
156 post_data =
21758d6 @amasad adapt to new backend.\n Fallback from html5 history to hashchange
amasad authored
157 language: REPLIT.current_lang.system_name
6f98ff7 @amasad Adapted session module to mobiles
amasad authored
158 editor_text: REPLIT.editor.getSession().getValue() if not REPLIT.ISMOBILE
6c28d90 @max99x Coffee cleanup. No change in functionality.
max99x authored
159 eval_history: JSON.stringify REPLIT.session.eval_history
37b45b3 @amasad Send over to the server the console dump.
amasad authored
160 console_dump: REPLIT.jqconsole.Dump();
161
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
162 # If we are already REPLing on a saved session, get its id.
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
163 post_data.id = REPLIT.session.id if REPLIT.session.id?
164 # Do the actual save request.
165 $.post '/save', post_data, (data) ->
60df559 @max99x Updated sessions.coffee to the new sessions API.
max99x authored
166 {session_id, revision_id} = data
5869232 @amasad social buttons
amasad authored
167 $savebox = $('#save-box')
60df559 @max99x Updated sessions.coffee to the new sessions API.
max99x authored
168 # Update URL.
169 if revision_id > 0
170 REPLIT.pushState session_id + '/' + revision_id
171 else
172 REPLIT.pushState session_id
173 # Update IDs.
174 REPLIT.session.id = session_id
175 REPLIT.session.rid = revision_id
6c28d90 @max99x Coffee cleanup. No change in functionality.
max99x authored
176
5869232 @amasad social buttons
amasad authored
177 # Render social share links.
60df559 @max99x Updated sessions.coffee to the new sessions API.
max99x authored
178 $savebox.find('li.twitter a').replaceWith SHARE_TEMPLATE.twitter()
179 $savebox.find('li.facebook a').replaceWith SHARE_TEMPLATE.facebook()
180 $savebox.find('li.gplus a').replaceWith SHARE_TEMPLATE.gplus()
5869232 @amasad social buttons
amasad authored
181 $savebox.find('input').val window.location.href
2eb0dc7 @amasad Populate editor file download link href.
amasad authored
182 $savebox.find('.downloads a.editor').attr 'href', "/download/editor/#{session_id}/#{revision_id}/"
37b45b3 @amasad Send over to the server the console dump.
amasad authored
183 $savebox.find('.downloads a.repl').attr 'href', "/download/repl/#{session_id}/#{revision_id}/"
5869232 @amasad social buttons
amasad authored
184 $savebox.slideDown()
63c47ef @amasad When savebox is show, if clicked anywhere the box slides up.
amasad authored
185 $savebox.click (e) ->
186 return e.stopPropagation()
187 $('body').bind 'click.closesave', ->
188 $savebox.slideUp()
189 $('body').unbind('click.closesave')
6c28d90 @max99x Coffee cleanup. No change in functionality.
max99x authored
190
e1109d0 @max99x Throttled saving by 2sec (just against accidental Ctrl+S holding);
max99x authored
191 # Disable share button for a little while.
192 unbindSaveButton()
193 setTimeout bindSaveButton, WAIT_BETWEEN_SAVES
194
195 bindSaveButton = -> $('#button-save').click saveSession
196 unbindSaveButton = -> $('#button-save').unbind 'click'
197 bindSaveButton()
198
5869232 @amasad social buttons
amasad authored
199 $('#save-box input').click -> $(this).select()
45e62c2 @amasad Refactored app.coffee to 3 core modules and 2 extensions.
amasad authored
200 # When any command is evaled, save it in the eval_history array of the session
201 # object, in order to send it to the server on save.
202 REPLIT.$this.bind 'eval', (e, command) ->
ece2221 @max99x Cleaned up session code; prevented saving with no language selected.
max99x authored
203 REPLIT.session.eval_history.push command
Something went wrong with that request. Please try again.