Class: Netzke::FormPanel

Inherits:
Base
  • Object
show all
Includes:
DataAccessor, FormPanelApi, FormPanelJs, Plugins::ConfigurationTool
Defined in:
lib/netzke/form_panel.rb

Overview

FormPanel

Represents Ext.form.FormPanel

Configuration

  • :data_class_name - name of the ActiveRecord model that provides data to this GridPanel.

  • :record - record to be displayd in the form. Takes precedence over :record_id

  • :record_id - id of the record to be displayd in the form. Also see :record

In the :ext_config hash (see Netzke::Base) the following FormPanel specific options are available:

  • :mode - when set to :config, FormPanel loads in configuration mode

Direct Known Subclasses

PropertyEditor, SearchPanel

Constant Summary collapse

XTYPE_MAP =
{
  :integer => :numberfield,
  :boolean => :xcheckbox,
  :date => :datefield,
  :datetime => :xdatetime,
  :text => :textarea,
  :json => :jsonfield
  # :string => :textfield
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Plugins::ConfigurationTool

#config_tool_needed?, #config_with_config_tool, included, #initial_aggregatees_with_config_tool, #js_config_with_config_tool, #tools_with_config_tool

Methods included from DataAccessor

#apply_helpers, #normalize_array_of_columns, #normalize_column, #normalize_columns, #predefined_columns

Methods included from FormPanelApi

#array_of_values, #configuration_panel__fields__get_combobox_options, #create_or_update_record, #get_combobox_options, #load, #submit

Methods included from FormPanelJs

included

Constructor Details

#initialize(*args) ⇒ FormPanel

Returns a new instance of FormPanel.



45
46
47
48
49
# File 'lib/netzke/form_panel.rb', line 45

def initialize(*args)
  super
  apply_helpers
  @record = config[:record] || data_class && data_class.find_by_id(config[:record_id])
end

Instance Attribute Details

#recordObject

Returns the value of attribute record.



43
44
45
# File 'lib/netzke/form_panel.rb', line 43

def record
  @record
end

Class Method Details

.configObject

Class-level configuration with defaults



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/netzke/form_panel.rb', line 20

def self.config
  set_default_config({
    :config_tool_available       => true,
    
    :default_config => {
      :ext_config => {
        :bbar => %w{ apply },
        :tools => %w{ }
      },
      :persistent_config => false
    }
  })
end

.config_columnsObject

columns to be displayed by the FieldConfigurator (which is GridPanel-based)



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/netzke/form_panel.rb', line 95

def self.config_columns
  [
    {:name => :name, :type => :string, :editor => :combobox, :width => 200},
    {:name => :hidden, :type => :boolean, :editor => :checkbox, :width => 40, :header => "Excl"},
    {:name => :disabled, :type => :boolean, :editor => :checkbox, :width => 40, :header => "Dis"},
    {:name => :xtype, :type => :string},
    {:name => :value, :type => :string},
    {:name => :field_label, :type => :string},
    {:name => :input_type, :type => :string}
  ]
end

.include_jsObject

Extra javascripts



35
36
37
38
39
# File 'lib/netzke/form_panel.rb', line 35

def self.include_js
  [
    "#{File.dirname(__FILE__)}/form_panel_extras/javascripts/xcheckbox.js"
  ]
end

.property_fieldsObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/netzke/form_panel.rb', line 107

def self.property_fields
  res = [
    {:name => :ext_config__title,               :type => :string},
    {:name => :ext_config__header,              :type => :boolean, :default => true}
     # {:name => :ext_config__bbar,              :type => :json},
    # {:name => :ext_config__prohibit_create,     :type => :boolean},
    # {:name => :ext_config__prohibit_update,     :type => :boolean},
    # {:name => :ext_config__prohibit_delete,     :type => :boolean},
    # {:name => :ext_config__prohibit_read,       :type => :boolean}
  ]
  
  res
  
end

Instance Method Details

#actionsObject



76
77
78
79
80
# File 'lib/netzke/form_panel.rb', line 76

def actions
  {
    :apply => {:text => 'Apply'}
  }
end

#columnsObject



82
83
84
# File 'lib/netzke/form_panel.rb', line 82

def columns
  @columns ||= get_columns.convert_keys{|k| k.to_sym}
end

#configuration_widgetsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/netzke/form_panel.rb', line 55

def configuration_widgets
  res = []
  
  res << {
    :name              => 'fields',
    :widget_class_name => "FieldsConfigurator",
    :active            => true,
    :widget            => self,
    :persistent_config => true
  }

  res << {
    :name               => 'general',
    :widget_class_name  => "PropertyEditor",
    :widget             => self,
    :ext_config         => {:title => false}
  }
  
  res
end

#data_classObject



51
52
53
# File 'lib/netzke/form_panel.rb', line 51

def data_class
  @data_class ||= config[:data_class_name] && config[:data_class_name].constantize
end

#default_columnsObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/netzke/form_panel.rb', line 155

def default_columns
  # columns specified in widget's config
  columns_from_config = config[:columns] && normalize_columns(config[:columns])
  
  if columns_from_config
    # reverse-merge each column hash from config with each column hash from exposed_attributes (columns from config have higher priority)
    for c in columns_from_config
      corresponding_exposed_column = predefined_columns.find{ |k| k[:name] == c[:name] }
      c.reverse_merge!(corresponding_exposed_column) if corresponding_exposed_column
    end
    columns_for_create = columns_from_config
  elsif predefined_columns
    # we didn't have columns configured in widget's config, so, use the columns from the data class
    columns_for_create = predefined_columns
  else
    raise ArgumentError, "No columns specified for widget '#{id_name}'"
  end
  
  columns_for_create.map! do |c|
    if data_class
      # Try to figure out the configuration from data class
      # detect :assoc__method
      if c[:name].to_s.index('__')
        assoc_name, method = c[:name].to_s.split('__').map(&:to_sym)
        if assoc = data_class.reflect_on_association(assoc_name)
          assoc_column = assoc.klass.columns_hash[method.to_s]
          assoc_method_type = assoc_column.try(:type)
          if assoc_method_type
            c[:xtype] ||= XTYPE_MAP[assoc_method_type] == :xcheckbox ? :xcheckbox : :combobox
          end
        end
      end
  
      # detect association column (e.g. :category_id)
      if assoc = data_class.reflect_on_all_associations.detect{|a| a.primary_key_name.to_sym == c[:name]}
        c[:xtype] ||= :combobox
        assoc_method = %w{name title label id}.detect{|m| (assoc.klass.instance_methods + assoc.klass.column_names).include?(m) } || assoc.klass.primary_key
        c[:name] = "#{assoc.name}__#{assoc_method}".to_sym
      end
      c[:hidden] = true if c[:name] == data_class.primary_key.to_sym && c[:hidden].nil? # hide ID column by default

    end
    
    # detect column type
    type = c[:type] || data_class && data_class.columns_hash[c[:name].to_s].try(:type) || :string
    c[:type] ||= type
    
    c[:xtype] ||= XTYPE_MAP[type] unless XTYPE_MAP[type].nil?

    # if the column is finally simply {:name => "something"}, cut it down to "something"
    c.reject{ |k,v| k == :name }.empty? ? c[:name] : c
  end
  
  columns_for_create
  
end

#get_columnsObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/netzke/form_panel.rb', line 128

def get_columns
  if config[:persistent_config]
    persistent_config['layout__columns'] ||= default_columns
    res = normalize_array_of_columns(persistent_config['layout__columns'])
  else
    res = default_columns
  end

  # merge values for each field if the record is specified
  @record && res.map! do |c|
    value = @record.send(normalize_column(c)[:name])
    value.nil? ? c : normalize_column(c).merge(:value => value)
  end

  res
end

#js_configObject

parameters used to instantiate the JS object



87
88
89
90
91
92
# File 'lib/netzke/form_panel.rb', line 87

def js_config
  res = super
  res.merge!(:clmns => columns)
  res.merge!(:data_class_name => config[:data_class_name])
  res
end

#normalized_columnsObject

Normalized columns



123
124
125
# File 'lib/netzke/form_panel.rb', line 123

def normalized_columns
  @normalized_columns ||= normalize_columns(columns)
end