Class: Netzke::PropertyEditor

Inherits:
FormPanel
  • Object
show all
Defined in:
lib/netzke/property_editor.rb

Constant Summary

Constants inherited from FormPanel

FormPanel::XTYPE_MAP

Instance Attribute Summary

Attributes inherited from FormPanel

#record

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FormPanel

#columns, config, config_columns, #configuration_widgets, #data_class, #default_columns, include_js, #js_config, #normalized_columns, property_fields

Methods included from Netzke::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) ⇒ PropertyEditor

Returns a new instance of PropertyEditor.



4
5
6
7
# File 'lib/netzke/property_editor.rb', line 4

def initialize(*args)
  super
  @widget = @passed_config[:widget]
end

Class Method Details

.js_extend_propertiesObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/netzke/property_editor.rb', line 31

def self.js_extend_properties
  {
    :label_width => 200,
    
    # Disable the 'gear' tool for now
    :gear => <<-END_OF_JAVASCRIPT.l,
    
    :restore_defaults => <<-END_OF_JAVASCRIPT.l,
    
    :get_commit_data => <<-END_OF_JAVASCRIPT.l
      function(){
        if (!this.getForm().isValid()) {this.getForm().reset()}; // if some fields are invalid, don't send anything
        var values = this.getForm().getValues();
        for (var k in values) {
          if (values[k] == "") {values[k] = null}
        }
        return values;
      }
    END_OF_JAVASCRIPT
  }
end

Instance Method Details

#actionsObject



15
16
17
# File 'lib/netzke/property_editor.rb', line 15

def actions
  {:restore_defaults => {:text => "Restore defaults"}}
end

#commit(data) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/netzke/property_editor.rb', line 78

def commit(data)
  fields = @widget.class.property_fields
  data.each_pair do |property, value|
    field = fields.detect{ |f| f[:name] == property.to_sym }
    # default = @widget.config[property].nil? ? field[:default] : @widget.config[property]
    default = @widget.flat_initial_config(property).nil? ? field[:default] : @widget.flat_initial_config(property)
    # Only store the value in persistent config when it's different from the default one
    if field[:type] == :boolean
      # handle boolean type separately
      value = value.to_b
      @widget.persistent_config[property] = value ^ default ? value : nil
    else 
      if field[:type] == :json
        value = ActiveSupport::JSON.decode(value)
      end

      # Empty string means "null" for now...
      # value = nil if value.blank?
      # logger.debug "!!! value: #{value.inspect}\n"
      
      @widget.persistent_config[property] = default == value ? nil : value
    end
  end
  {}
end

#get_columnsObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/netzke/property_editor.rb', line 19

def get_columns
  fields = @widget.class.property_fields

  for f in fields
    f[:value] = @widget.flat_config(f[:name]).nil? ? f[:default] : @widget.flat_config(f[:name])
    f[:xtype] = XTYPE_MAP[f[:type]]
    f[:field_label] = f[:name].to_s.gsub("__", "/").humanize
  end
  
  fields
end

#independent_configObject



9
10
11
12
13
# File 'lib/netzke/property_editor.rb', line 9

def independent_config
  res = super
  res[:ext_config][:bbar] = %w{ restore_defaults }
  res
end

#restore_defaults(params) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/netzke/property_editor.rb', line 62

def restore_defaults(params)
  values = []
  columns.each do |c|
    init_config = @widget.flat_initial_config.detect{ |ic| ic[:name] == c[:name] }
    
    if init_config.nil?
      property_fields ||= @widget.class.property_fields
      values << property_fields.detect{ |f| f[:name] == c[:name] }[:default]
    else
      values << init_config[:value]
    end
    
  end
  {:set_form_values => values}
end