Class: IRuby::Input::InputForm

Inherits:
Widget
  • Object
show all
Defined in:
lib/iruby/input/form.rb

Instance Method Summary collapse

Methods inherited from Widget

builder, #content, #widget_display, #widget_join

Instance Method Details

#submitObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/iruby/input/form.rb', line 65

def submit
  result = MultiJson.load(Kernel.instance.session.recv_input)

  unless result.has_key? @id
    submit
  else
    Display.clear_output
    result[@id]
  end
end

#widget_cssObject



53
54
55
56
# File 'lib/iruby/input/form.rb', line 53

def widget_css
  spacing = '#iruby-form > * { margin-bottom: 5px; }'
  widget_join :widget_css, spacing, *@fields, *@buttons
end

#widget_htmlObject



58
59
60
61
62
63
# File 'lib/iruby/input/form.rb', line 58

def widget_html
  form id: 'iruby-form', class: 'col-md-12' do
    @fields.each {|field| widget field}
  end
  @buttons.each {|button| widget button}
end

#widget_jsObject



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
# File 'lib/iruby/input/form.rb', line 8

def widget_js 
  javascript = <<-JS
    var remove = function () {
      Jupyter.notebook.kernel.send_input_reply(
        JSON.stringify({
          '#{@id = SecureRandom.uuid}': null
        })
      );
    };
    
    $("#iruby-form").on("remove", remove);

    $('#iruby-form').submit(function() {
      var result = {};
      $(this).off('remove', remove);

      $('[data-iruby-key]').each(function() {
        if ($(this).data('iruby-key')) {
          var value = $(this).data('iruby-value');
          if (value) {
            result[$(this).data('iruby-key')] = value;
          }
        }
      });

      Jupyter.notebook.kernel.send_input_reply(
        JSON.stringify({'#{@id}': result})
      );
      
      $(this).remove();
      return false;
    });

    $('#iruby-form').keydown(function(event) {
      if (event.keyCode == 13 && !event.shiftKey) { 
        $('#iruby-form').submit();
      } else if (event.keyCode == 27) { 
        $('#iruby-form').remove(); 
      }
    });
  JS

  widget_join :widget_js, javascript, *@fields, *@buttons
end