Permalink
Browse files

Update TryRuby sample to support multiline, and got rid of JavaScript…

… callback

- EventHandler.of(HtmlEventArgs) now works
  • Loading branch information...
1 parent f7c374a commit c0c59f430949114f982137da0e6f42bc4969d103 @jschementi committed Jul 3, 2008
@@ -11,7 +11,7 @@
body {
padding: 0px; margin: 50px; background-color: #222;
}
- #code, #result, #prompt {
+ #code, #result, .prompt {
border: 0px;
font-size: 16px;
line-height: 20px;
@@ -20,17 +20,22 @@
color: #eee;
}
#code {
- width: 500px;
+ width: 90%;
+ height: 20px;
+ position: relative;
+ left: -1px; top: -1px;
}
+ .active {}
#run {
display: none;
}
#run_form {
display: inline;
color: white;
}
- #prompt {
+ .prompt {
color: yellow;
+ float: left;
}
#console {
padding: 15px;
@@ -74,27 +79,6 @@
<%= javascript_include_tag :defaults %>
<%= javascript_include_tag 'scrolling' %>
- <!-- TODO: remove this when I can hook HTML keyboard events properly -->
- <script type="text/javascript">
- function submitenter(myField, e) {
- var keycode;
- if(window.event) {
- keycode = window.event.keyCode;
- } else if(e) {
- keycode = e.which;
- } else {
- return true;
- }
-
- if(keycode == 13) {
- document.getElementById('run').click();
- return false;
- } else {
- return true;
- }
- }
- </script>
-
<%= silverlight_include_tag :defaults %>
</head>
<body>
@@ -1,52 +1,119 @@
-def compute
+class Tutorial
+ attr_reader :current
+
+ def get_instructions
+ $app.download "/tryruby/instructions.json" do |s, a|
+ @instructions = JSON.parse a.result
+ move_on
+ end
+ end
+
+ def next_instruction
+ @current ||= 0
+ $d.instructions.html = @instructions[@current].to_s
+ @current += 1 if @current < @instructions.size - 1
+ end
+
+ def move_on
+ @instructions ||= nil
+ @instructions.nil? ? get_instructions : next_instruction
+ end
+end
+
+def run_code
+ @multiline = true
@code = $d.code.value.to_s
- @result = eval(@code, console_binding) unless @code.empty?
+ @result = eval @code unless @code.empty?
+ @multiline = false
rescue => e
@result = e.class
+ @multiline = false
ensure
- show_result
- @code = @result = nil
+ unless @multiline
+ show_prompt
+ show_result(@code, @result)
+ show_defaults
+ scroll_to_code
+ remember
+ reset
+ else
+ $d.code.style[:height] = "#{$d.code.style[:height].to_s[0..-2].to_i + 20}px"
+ #show_sub_prompt
+ end
end
-def show_result
- $d.result.append(tag("span", :id => 'prompt') { "&raquo;&nbsp;" })
- $d.result.append(@code.empty? ? tag("br") : (tag("span"){ @code } + tag("div"){ @result.inspect }))
+def remember
+ @history ||= []
+ @history << @code
+end
+
+def reset
+ @code = @result = @multiline = nil
+end
+
+def show_result(code, result)
+ code = code.split("\n").collect{|line| line.split(" ").join("&nbsp;")}.join("<br />")
+ $d.result.append(code.empty? ? tag("br") : (tag("span"){ code } + tag("div"){ result.inspect }))
+end
+
+def show_sub_prompt
+ $d.result.append(tag("span", :class => 'prompt') { sub_prompt_html })
+end
+
+def show_prompt
+ $d.result.append(tag("span", :class => 'prompt') { prompt_html })
+end
+
+def prompt_html
+ "&raquo;&nbsp;"
+ #"ir&gt;&nbsp;"
+end
+
+def sub_prompt_html
+ "|&nbsp;"
+end
+
+def show_defaults
$d.code.value = ""
+ $d.code.focus
+ $d.prompt.html = prompt_html
+ $d.code.style[:height] = "20px"
+ $d.code.rows = "1"
end
-def move_on
- @instructions ||= nil
- @instructions.nil? ? get_instructions : next_instruction
+def show_loading
+ $d.loading.html = tag('img', :src => '/images/loadinfo.net.gif', :alt => 'evaluating ...')
end
-def get_instructions(&block)
- download "/tryruby/instructions.json" do |s, a|
- @instructions = JSON.parse a.result
- move_on
- end
+def show_loaded
+ $d.loading.html = ''
end
-def next_instruction
- @current ||= 0
- $d.instructions.html = @instructions[@current].to_s
- @current += 1 if @current < @instructions.size - 1
+def scroll_to_code
+ scroll_to('code')
end
-def hook_run
- $d.run.onclick do |s, a|
- document.loading.html = tag('img', :src => '/images/loadinfo.net.gif', :alt => 'evaluating ...')
- compute
- move_on
- document.loading.html = ''
- HtmlPage.window.eval "moveTo('console', 'code')"
- end
+def scroll_to(element)
+ HtmlPage.window.eval "moveTo('console', '#{element}')"
end
-def console_binding
- binding
+KEYS = {
+ 13 => 'run'
+}
+
+@tutorial = Tutorial.new
+@tutorial.get_instructions
+show_defaults
+
+$d.code.onkeypress do |s, a|
+ show_loading
+ if a.character_code.to_i == 13
+ run_code
+ @tutorial.move_on
+ end
+ show_loaded
end
-$d.code.value = ''
-$d.code.focus
-hook_run
-get_instructions
+# Demo script
+$d.code.value = "2 + 6"
+run_code
@@ -7,9 +7,13 @@
though sometimes you will get browser auto-complete once you start typing. =)
</div>
+<div style="display: none">
+ <a href="javascript:void(0)" id="previous" onclick="alert('prev')">Previous Line</a>
+ <a href="javascript:void(0)" id="next" onclick="alert('next)">Next Line</a>
+</div>
<div id="console">
<div id="result"></div>
- <span id="prompt">&raquo;&nbsp;</span><form id="run_form" action="javascript:void(0)"><input type="text" id="code" onkeypress="return submitenter(this, event)" /><input type="submit" id="run" value="Run" /></form>
+ <span id="prompt" class="prompt"></span><form id="run_form" action="javascript:void(0)"><textarea type="text" rows="1" id="code"></textarea><input type="submit" id="run" value="Run" /></form> <!-- return submitenter(this, event) -->
<span id="loading"></span>
</div>
<div id="tutorial">
@@ -77,7 +77,7 @@ def method_missing(m, *args, &block)
end
else
# TODO: want to do EventHandler.of(HtmlEventArgs) to get proper arguments back
- unless attach_event(m.to_s.to_clr_string, System::EventHandler.new(&block))
+ unless attach_event(m.to_s.to_clr_string, System::EventHandler.of(HtmlEventArgs).new(&block))
raise e
end
end
@@ -8,7 +8,7 @@ def silverlight_include_tag(options = :defaults)
def silverlight_object(options = {})
defaults = {
:start => "app",
- :debug => false,
+ :debug => true,
:reportErrors => "errorLocation",
:properties => {
:width => 1,

0 comments on commit c0c59f4

Please sign in to comment.