Skip to content
This repository
Newer
Older
100644 189 lines (172 sloc) 7.15 kb
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
1 # Extension module.
6c28d90c » max99x
2011-09-08 Coffee cleanup. No change in functionality.
2 # Encapsulates for all session/state loading saving logic.
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
3 # TODO(amasad): Graceful localStorage degradation to cookies.
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
4 $ = jQuery
21758d64 » amasad
2011-09-16 adapt to new backend.\n Fallback from html5 history to hashchange
5 PUSHSTATE_SUPPORTED = 'pushState' of history
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
6
5869232b » amasad
2011-09-05 social buttons
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 """
6c28d90c » max99x
2011-09-08 Coffee cleanup. No change in functionality.
20
5869232b » amasad
2011-09-05 social buttons
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 """
6c28d90c » max99x
2011-09-08 Coffee cleanup. No change in functionality.
25
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
26 # Unofficial!
5869232b » amasad
2011-09-05 social buttons
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 """
6c28d90c » max99x
2011-09-08 Coffee cleanup. No change in functionality.
33
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
34 $.extend REPLIT,
35 session:
36 eval_history: []
21758d64 » amasad
2011-09-16 adapt to new backend.\n Fallback from html5 history to hashchange
37 pushState: (text) ->
38 if PUSHSTATE_SUPPORTED
39 window.history.pushState null, null, "/#{text}"
40 else
41 REPLIT.setHash 0, text
6c28d90c » max99x
2011-09-08 Coffee cleanup. No change in functionality.
42
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
43 # Resets application to its initial state (handler for language_loaded event).
44 reset_state = (e, lang_name) ->
45 localStorage.setItem 'lang_name', lang_name
b13176d9 » amasad
2011-09-04 Hide replay button after actual replay
46 $('#replay-button').hide()
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
47 @session = {}
48 @session.eval_history = []
21758d64 » amasad
2011-09-16 adapt to new backend.\n Fallback from html5 history to hashchange
49 REPLIT.pushState ''
6c28d90c » max99x
2011-09-08 Coffee cleanup. No change in functionality.
50
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
51 $ ->
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
52 # If there exists a REPLIT_DATA variable, then we are in a saved session.
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
53 if REPLIT_DATA?
54 # Load the language specified by the incoming session data.
5ec899eb » max99x
2011-09-19 Fixed language not appearing in header when loading a session.
55 REPLIT.current_lang_name = REPLIT_DATA.language
dfd1c81d » amasad
2011-09-12 Adapted the session module to pager changes.
56 REPLIT.OpenPage 'workspace', ->
57 REPLIT.LoadLanguage REPLIT_DATA.language, ->
58 # Set the editor text.
6f98ff71 » amasad
2011-09-12 Adapted session module to mobiles
59 REPLIT.editor.getSession().setValue REPLIT_DATA.editor_text if not @ISMOBILE
dfd1c81d » amasad
2011-09-12 Adapted the session module to pager changes.
60 # Get the session data.
60df559d » max99x
2011-09-19 Updated sessions.coffee to the new sessions API.
61 REPLIT.session.id = REPLIT_DATA.session_id
62 REPLIT.session.rid = REPLIT_DATA.revision_id
dfd1c81d » amasad
2011-09-12 Adapted the session module to pager changes.
63 REPLIT.session.saved_eval_history = REPLIT_DATA.eval_history
64 # Show the replay button.
65 $('#replay-button').show()
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
66 # Delete the incoming session data from the server since we have
67 # extracted everything we neeed.
dfd1c81d » amasad
2011-09-12 Adapted the session module to pager changes.
68 delete window['REPLIT_DATA']
69 # On each language load after this one reset the state.
70 REPLIT.$this.bind 'language_loaded', reset_state
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
71 else
72 # We are not in a saved session.
73 # Safely bind the reset state function.
74 REPLIT.$this.bind 'language_loaded', reset_state
75 lang_name = localStorage.getItem('lang_name')
c78eac4f » max99x
2011-09-13 Fixed Analytics reporting 0 timing when loading language from localSt…
76 if lang_name?
77 REPLIT.loading_saved_lang = true
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
78
c78eac4f » max99x
2011-09-13 Fixed Analytics reporting 0 timing when loading language from localSt…
79 # We have a saved local settings for language to load. Delay this until
80 # the Analytics modules has set its hook so it can catch language loading.
81 $ ->
82 REPLIT.current_lang_name = lang_name
83 REPLIT.OpenPage 'workspace', ->
84 REPLIT.LoadLanguage lang_name
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
85 else
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
86 # This is the first visit; show language overlay.
21758d64 » amasad
2011-09-16 adapt to new backend.\n Fallback from html5 history to hashchange
87 $('#languages-back').bind 'click.language_modal', (e) ->
88 e.stopImmediatePropagation()
89 return false
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
90 $('#content-languages .language-group li').bind 'click.language_modal', (e) ->
21758d64 » amasad
2011-09-16 adapt to new backend.\n Fallback from html5 history to hashchange
91 REPLIT.Modal false
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
92
21758d64 » amasad
2011-09-16 adapt to new backend.\n Fallback from html5 history to hashchange
93 REPLIT.$this.bind 'language_loaded.language_modal', (e) ->
94 $('#languages-back').unbind 'click.language_modal'
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
95
5869232b » amasad
2011-09-05 social buttons
96 REPLIT.OpenPage 'languages'
21758d64 » amasad
2011-09-16 adapt to new backend.\n Fallback from html5 history to hashchange
97 REPLIT.Modal true
6c28d90c » max99x
2011-09-08 Coffee cleanup. No change in functionality.
98
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
99 # Click handler for the replay button.
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
100 $('#replay-button').click (e) ->
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
101 # Get the history comming from the server.
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
102 history = REPLIT.session.saved_eval_history
dfd1c81d » amasad
2011-09-12 Adapted the session module to pager changes.
103 locked = false
104 locked_queue = []
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
105 index = -1
106 # Executes a command from history and waits for the result to continue
107 # with the next command.
dfd1c81d » amasad
2011-09-12 Adapted the session module to pager changes.
108 handler = ->
109 if not locked
110 index++
111 if history[index]?
112 # Set the prompt text to the command in question.
113 REPLIT.jqconsole.SetPromptText history[index]
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
114 # Remove multiline handler from jqconsole to ensure it doesn't
115 # continue to the next line.
dfd1c81d » amasad
2011-09-12 Adapted the session module to pager changes.
116 _multiline = REPLIT.jqconsole.multiline_callback
117 REPLIT.jqconsole.multiline_callback = undefined
118 # Simulate an enter button on jqconsole.
119 REPLIT.jqconsole._HandleEnter()
120 # Reassign the multiline handler.
121 REPLIT.jqconsole.multiline_callback = _multiline
122 else
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
123 # There is no more commands; unbind the handler.
dfd1c81d » amasad
2011-09-12 Adapted the session module to pager changes.
124 REPLIT.$this.unbind 'result', handler
125 REPLIT.$this.unbind 'error', handler
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
126 # We are done with the eval history from the server; delete it.
dfd1c81d » amasad
2011-09-12 Adapted the session module to pager changes.
127 delete REPLIT.session['saved_eval_history']
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
128 else
dfd1c81d » amasad
2011-09-12 Adapted the session module to pager changes.
129 locked_queue.push handler
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
130
dfd1c81d » amasad
2011-09-12 Adapted the session module to pager changes.
131 input_lock = ->
132 locked = true
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
133
dfd1c81d » amasad
2011-09-12 Adapted the session module to pager changes.
134 input_unlock = ->
135 locked = false
136 fn = locked_queue.shift()
137 setTimeout fn, 100 if fn?
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
138
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
139 REPLIT.$this.bind 'result', handler
140 REPLIT.$this.bind 'error', handler
dfd1c81d » amasad
2011-09-12 Adapted the session module to pager changes.
141 REPLIT.$this.bind 'input', input_unlock
142 REPLIT.$this.bind 'input_request', input_lock
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
143 # Initiate the first handler to start executing history commands.
144 handler()
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
145 # This button can only be clicked once. Now hide it.
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
146 $(this).hide()
6c28d90c » max99x
2011-09-08 Coffee cleanup. No change in functionality.
147
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
148 $('#button-save').click (e) ->
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
149 # Can't save if we haven't selected a language yet.
150 if not REPLIT.current_lang? then return
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
151 # Get the post data to save.
152 post_data =
21758d64 » amasad
2011-09-16 adapt to new backend.\n Fallback from html5 history to hashchange
153 language: REPLIT.current_lang.system_name
6f98ff71 » amasad
2011-09-12 Adapted session module to mobiles
154 editor_text: REPLIT.editor.getSession().getValue() if not REPLIT.ISMOBILE
6c28d90c » max99x
2011-09-08 Coffee cleanup. No change in functionality.
155 eval_history: JSON.stringify REPLIT.session.eval_history
156
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
157 # If we are already REPLing on a saved session, get its id.
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
158 post_data.id = REPLIT.session.id if REPLIT.session.id?
159 # Do the actual save request.
160 $.post '/save', post_data, (data) ->
60df559d » max99x
2011-09-19 Updated sessions.coffee to the new sessions API.
161 console.log data
162 {session_id, revision_id} = data
5869232b » amasad
2011-09-05 social buttons
163 $savebox = $('#save-box')
60df559d » max99x
2011-09-19 Updated sessions.coffee to the new sessions API.
164 # Update URL.
165 if revision_id > 0
166 REPLIT.pushState session_id + '/' + revision_id
167 else
168 REPLIT.pushState session_id
169 # Update IDs.
170 REPLIT.session.id = session_id
171 REPLIT.session.rid = revision_id
6c28d90c » max99x
2011-09-08 Coffee cleanup. No change in functionality.
172
5869232b » amasad
2011-09-05 social buttons
173 # Render social share links.
60df559d » max99x
2011-09-19 Updated sessions.coffee to the new sessions API.
174 $savebox.find('li.twitter a').replaceWith SHARE_TEMPLATE.twitter()
175 $savebox.find('li.facebook a').replaceWith SHARE_TEMPLATE.facebook()
176 $savebox.find('li.gplus a').replaceWith SHARE_TEMPLATE.gplus()
5869232b » amasad
2011-09-05 social buttons
177 $savebox.find('input').val window.location.href
178 $savebox.slideDown()
63c47ef1 » amasad
2011-09-12 When savebox is show, if clicked anywhere the box slides up.
179 $savebox.click (e) ->
180 return e.stopPropagation()
181 $('body').bind 'click.closesave', ->
182 $savebox.slideUp()
183 $('body').unbind('click.closesave')
6c28d90c » max99x
2011-09-08 Coffee cleanup. No change in functionality.
184
5869232b » amasad
2011-09-05 social buttons
185 $('#save-box input').click -> $(this).select()
45e62c28 » amasad
2011-09-04 Refactored app.coffee to 3 core modules and 2 extensions.
186 # When any command is evaled, save it in the eval_history array of the session
187 # object, in order to send it to the server on save.
188 REPLIT.$this.bind 'eval', (e, command) ->
ece22214 » max99x
2011-09-17 Cleaned up session code; prevented saving with no language selected.
189 REPLIT.session.eval_history.push command
Something went wrong with that request. Please try again.