Class: DrgcmsFormFields::Embedded

Inherits:
DrgcmsField show all
Defined in:
app/models/drgcms_form_fields/embedded.rb

Overview

Implementation of embedded DRG CMS form field.

Creates html required to paint embedded object on form.

Form options:

  • name: field name (required)

  • type: embedded (required)

  • form_name: name of form which will be used for editing

  • load: when is embedded iframe loaded. default=on form load, delay=on tab select, always=every time tab is selected)

  • html: html options (optional)

    • height: height of embedded object in pixels (1000)

    • width: width of embedded object in pixels (500)

Form example:

10:
  name: dc_parts
  type: embedded
  form_name: dc_part
  refresh: delay
  html:
    height: 1000

Instance Attribute Summary

Attributes inherited from DrgcmsField

#css, #js

Instance Method Summary collapse

Methods inherited from DrgcmsField

#__css_code, get_data, #hash_to_options, #html, #initialize, #record_text_for, #ro_standard, #set_css_code, #set_default_value, #set_initial_value, #set_style, #t

Constructor Details

This class inherits a constructor from DrgcmsFormFields::DrgcmsField

Instance Method Details

#renderObject

Render embedded field html code



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/drgcms_form_fields/embedded.rb', line 52

def render
  # HTML defaults. Some must be set    
  @yaml['html'] ||= {}
  @yaml['html']['width'] ||= '99%'
  html = @yaml['html'].inject('') { |r, val| r << "#{val.first}=\"#{val.last}\" " }

  @yaml['action'] ||= 'index'
  # defaults both way 
  @yaml['table']     ||= @yaml['form_name'] if @yaml['form_name']
  @yaml['form_name'] ||= @yaml['table'] if @yaml['table']

  if @yaml['name'] == @yaml['table'] or @yaml['table'] == 'dc_memory'
    tables = @yaml['table']
    ids    = @record.id
  else
    tables = @parent.tables.inject('') { |r,v| r << "#{v[1]};" } + @yaml['table']
    ids    = @parent.ids.inject('') { |r,v| r << "#{v};" } + @record.id
  end
  # edit enabled embedded form on a readonly form
  readonly = @yaml['readonly'].class == FalseClass ? nil : @readonly
  opts = { controller: 'cmsedit', action: @yaml['action'],
           ids: ids, table: tables, form_name: @yaml['form_name'], 
           field_name: @yaml['name'], iframe: "if_#{@yaml['name']}", readonly: readonly }
  # additional parameters if specified
  @yaml['params'].each { |k,v| opts[k] = @parent.dc_value_for_parameter(v) } if @yaml['params']         
         
  @html << "<iframe class='iframe_embedded' id='if_#{@yaml['name']}' name='if_#{@yaml['name']}' #{html}></iframe>"
  unless @record.new_record?
    url  = @parent.url_for(opts)
    data = if @yaml['load'].nil? || @yaml['load'].match('default')
      "src"
    else
      "data-src-#{@yaml['load']}"
    end
    @js << %Q[
$(document).ready( function() {
  $('#if_#{@yaml['name']}').attr('#{data}', '#{url}');
});]
  end
  self
end