Sha256: 3bf46040e1c82157436d445dc90140907d782c2fbd9193db1493862e050c6f19

Contents?: true

Size: 1.93 KB

Versions: 9

Compression:

Stored size: 1.93 KB

Contents

require 'securerandom'

module IRuby
  module Input
    class InputForm < Widget
      needs :fields, buttons: []

      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

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

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

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

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
iruby-0.8.0 lib/iruby/input/form.rb
iruby-0.7.4 lib/iruby/input/form.rb
iruby-0.7.3 lib/iruby/input/form.rb
iruby-0.7.2 lib/iruby/input/form.rb
iruby-0.7.1 lib/iruby/input/form.rb
iruby-0.7.0 lib/iruby/input/form.rb
iruby-0.6.1 lib/iruby/input/form.rb
iruby-0.6.0 lib/iruby/input/form.rb
iruby-0.5.0 lib/iruby/input/form.rb