class IRuby::Input::InputForm

Public Instance Methods

submit() click to toggle source
# 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_css() click to toggle source
# 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_html() click to toggle source
# 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_js() click to toggle source
# 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