# frozen_string_literal: true class Lux::Html::Input # if you call .memo which is not existant, it translates to .as_memo with opts_prepare first # def method_missing(meth, *args, &block) # opts_prepare *args # send "as_#{meth}" # end ############################# # custom fields definitions # ############################# def as_string @opts[:type] = 'text' @opts[:autocomplete] ||= 'off' @opts.tag(:input) end alias :as_text :as_string def as_password @opts[:type] = 'password' @opts.tag(:input) end def as_hidden @opts[:type] = 'hidden' @opts.tag(:input) end def as_file @opts[:type] = 'file' @opts.tag(:input) end def as_textarea val = @opts.delete(:value) || '' comp_style = val.split(/\n/).length + val.length/100 comp_style = 4 if comp_style < 4 comp_style = 15 if comp_style > 15 @opts[:style] = "height:#{comp_style*20}px; #{@opts[:style]};" @opts.tag(:textarea, val) end alias :as_memo :as_textarea def as_checkbox id = Lux.uid hidden = { :name=>@opts.delete(:name), :type=>:hidden, :value=>@opts[:value] ? 1 : 0, :id=>id } @opts[:type] = :checkbox @opts[:onclick] = "document.getElementById('#{id}').value=this.checked ? 1 : 0; #{@opts[:onclick]}" @opts[:checked] = @opts.delete(:value) ? 1 : nil @opts.tag(:input)+hidden.tag(:input) end def as_radio @opts[:type] = :radio @opts[:checked] = @opts[:value] == @object.send(@name) ? true : nil @opts.tag(:input) end def as_select body = [] collection = @opts.delete(:collection) if nullval = @opts.delete(:null) body.push %[] if nullval end for el in prepare_collection(collection) body.push(%[]) end body = body.join("\n") @opts.tag(:select, body) end def as_tag @opts[:value] = @opts[:value].or([]).join(', ') if ['Array', 'Sequel::Postgres::PGArray'].index(@opts[:value].class.name) @opts[:id] ||= Lux.uid @opts[:type] = :text @opts[:onkeyup] = %[draw_tag('#{@opts[:id]}')] @opts[:autocomplete] ||= 'off' ret = %[ ] ret += @opts.tag(:input) ret += %[
] ret += %[] ret end def as_date @opts[:type] = 'text' @opts[:style] = 'width: 120px; display: inline;' @opts[:value] = @opts[:value].to_s.split(' +').first.to_s.sub(/:\d{2}$/,'') @opts[:hint] ||= 'YEAR - MONTH - DAY' ret = @opts.tag(:input) ret += ' • %s' % Time.ago(Time.parse(@opts[:value])) if @opts[:value].present? ret += ' • %s' % @opts[:hint] ret + %[] end def as_datetime @opts[:type] = 'text' @opts[:style] ||= 'width: 170px;' @opts[:value] = @opts[:value].to_s.split(' +').first.sub(/:\d{2}$/,'') if @opts[:value].present? ret = @opts.tag(:input) hint = [] hint.push Time.ago(Time.parse(@opts[:value]))+'. ' if @opts[:value].present? hint.push %[Now!] ret + {class:'hint'}.tag(:p, hint.join('')) end def as_datebuttons @opts[:type] = 'text' @opts[:style] = 'width:100px; display:inline;' @opts[:id] = "date_#{Lux.uid}" id = "##{@opts[:id]}" ret = @opts.tag(:input) ret += %[ ] for el in [1, 3, 7, 14, 30] date = DateTime.now+el.days name = el.to_s name += " (#{date.strftime('%a')})" if el < 7 ret += %[ ] end ret end def as_user button_text = if @opts[:value].to_i > 0 usr = User.find(@opts[:value]) "#{usr.name} (#{usr.email})" else 'Select user' end @opts[:style] = "width:auto;#{@opts[:style]};" @opts[:onclick] = %[Popup.render(this,'Select user', '/part/users/single_user?product_id=#{@object.bucket_id}');return false;] @opts.tag :button, button_text end def as_photo @opts[:type] = 'hidden' if @opts[:value].present? img = Photo.find(@opts[:value]) @image = %[ ×] end picker = @opts.tag(:input) %[Select photo#{picker}#{@image}] end def as_photos @opts[:type] = 'text' @opts[:style] = 'width:150px; display:inline;' @opts[:class] += ' mutiple' @images = [] if @opts[:value].present? for el in @opts[:value].split(',').uniq img = Photo.find(el.to_i) rescue next @images.push %[ ] end end picker = @opts.tag(:input) %[Add photo #{picker}