2011-09-03 14:08:07 -04:00
|
|
|
module OmniAuth
|
2015-12-18 22:14:29 -05:00
|
|
|
class Form
|
2014-01-15 23:00:46 -05:00
|
|
|
DEFAULT_CSS = File.read(File.expand_path('../form.css', __FILE__))
|
2011-09-03 14:08:07 -04:00
|
|
|
|
|
|
|
attr_accessor :options
|
|
|
|
|
|
|
|
def initialize(options = {})
|
2014-01-15 23:00:46 -05:00
|
|
|
options[:title] ||= 'Authentication Info Required'
|
|
|
|
options[:header_info] ||= ''
|
2011-09-03 14:08:07 -04:00
|
|
|
self.options = options
|
|
|
|
|
2020-12-10 10:21:53 -05:00
|
|
|
@html = +'' # unary + string allows it to be mutable if strings are frozen
|
2011-12-08 10:54:20 -05:00
|
|
|
@with_custom_button = false
|
2013-01-10 12:13:52 -05:00
|
|
|
@footer = nil
|
2014-01-15 23:00:46 -05:00
|
|
|
header(options[:title], options[:header_info])
|
2011-09-03 14:08:07 -04:00
|
|
|
end
|
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
def self.build(options = {}, &block)
|
2011-10-26 01:27:07 -04:00
|
|
|
form = OmniAuth::Form.new(options)
|
2011-09-03 20:26:57 -04:00
|
|
|
if block.arity > 0
|
2013-01-25 13:24:52 -05:00
|
|
|
yield form
|
2011-09-03 20:26:57 -04:00
|
|
|
else
|
|
|
|
form.instance_eval(&block)
|
|
|
|
end
|
|
|
|
form
|
2011-09-03 14:08:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def label_field(text, target)
|
|
|
|
@html << "\n<label for='#{target}'>#{text}:</label>"
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def input_field(type, name)
|
|
|
|
@html << "\n<input type='#{type}' id='#{name}' name='#{name}'/>"
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def text_field(label, name)
|
|
|
|
label_field(label, name)
|
|
|
|
input_field('text', name)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def password_field(label, name)
|
|
|
|
label_field(label, name)
|
|
|
|
input_field('password', name)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def button(text)
|
2011-12-08 10:54:20 -05:00
|
|
|
@with_custom_button = true
|
2011-09-03 14:08:07 -04:00
|
|
|
@html << "\n<button type='submit'>#{text}</button>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def html(html)
|
|
|
|
@html << html
|
|
|
|
end
|
|
|
|
|
|
|
|
def fieldset(legend, options = {}, &block)
|
|
|
|
@html << "\n<fieldset#{" style='#{options[:style]}'" if options[:style]}#{" id='#{options[:id]}'" if options[:id]}>\n <legend>#{legend}</legend>\n"
|
2014-01-15 23:00:46 -05:00
|
|
|
instance_eval(&block)
|
2011-09-03 14:08:07 -04:00
|
|
|
@html << "\n</fieldset>"
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
def header(title, header_info)
|
2011-09-03 14:08:07 -04:00
|
|
|
@html << <<-HTML
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
2012-04-12 10:33:43 -04:00
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
2011-09-03 14:08:07 -04:00
|
|
|
<title>#{title}</title>
|
|
|
|
#{css}
|
|
|
|
#{header_info}
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>#{title}</h1>
|
|
|
|
<form method='post' #{"action='#{options[:url]}' " if options[:url]}noValidate='noValidate'>
|
|
|
|
HTML
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def footer
|
|
|
|
return self if @footer
|
2018-12-14 11:29:36 -05:00
|
|
|
|
2011-12-08 10:54:20 -05:00
|
|
|
@html << "\n<button type='submit'>Connect</button>" unless @with_custom_button
|
2011-09-03 14:08:07 -04:00
|
|
|
@html << <<-HTML
|
|
|
|
</form>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
HTML
|
|
|
|
@footer = true
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_html
|
|
|
|
footer
|
|
|
|
@html
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_response
|
|
|
|
footer
|
2014-01-15 23:00:46 -05:00
|
|
|
Rack::Response.new(@html, 200, 'content-type' => 'text/html').finish
|
2011-09-03 14:08:07 -04:00
|
|
|
end
|
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
protected
|
2011-09-03 14:08:07 -04:00
|
|
|
|
|
|
|
def css
|
|
|
|
"\n<style type='text/css'>#{OmniAuth.config.form_css}</style>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|