Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show and hide field groups based on selected tags
- Loading branch information
Ben Tillman
committed
Nov 22, 2011
1 parent
afbce41
commit a45a10b
Showing
7 changed files
with
102 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
class FieldGroup < ActiveRecord::Base | ||
has_many :fields | ||
belongs_to :tag | ||
|
||
def key | ||
"field_group_#{id}" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,38 @@ | ||
- asset = controller_name.singularize | ||
- f.object.tags = unsaved_param_tags(asset) if params[asset] and params[asset][:tag_list] | ||
- field_groups = {} | ||
|
||
- f.object.class.custom_fields.group_by(&:field_group_id).each do |field_group_id, fields| | ||
|
||
- field_group = FieldGroup.find(field_group_id) | ||
- next unless field_group.tag_id.nil? or f.object.tags.map(&:id).include?(field_group.tag_id) | ||
- tag = f.object.tags.detect { |tag| tag.id == field_group.tag_id } | ||
|
||
- next unless tag.present? or field_group.tag_id.nil? | ||
|
||
- collapsed = session[field_group.key].nil? | ||
= subtitle field_group.key, collapsed, field_group.name | ||
.section | ||
%small{ hidden_if(!collapsed).merge(:id => "#{field_group.key}_intro") } | ||
%div[field_group]{ hidden_if(collapsed) } | ||
%table | ||
- fields.sort_by(&:position).in_groups_of(2) do |group| | ||
%tr | ||
%td | ||
- if (field = group[0]) | ||
= f.input field.name, field.input_options | ||
%td | ||
= spacer | ||
%td | ||
- if (field = group[1]) | ||
= f.input field.name, field.input_options | ||
- container_id = "#{field_group.key}_container" | ||
- field_groups[tag.name.downcase] = container_id | ||
%div{ :id => container_id } | ||
= subtitle field_group.key, collapsed, field_group.name | ||
.section | ||
%small{ hidden_if(!collapsed).merge(:id => "#{field_group.key}_intro") } | ||
%div[field_group]{ hidden_if(collapsed) } | ||
%table | ||
- fields.sort_by(&:position).in_groups_of(2) do |group| | ||
%tr | ||
%td | ||
- if (field = group[0]) | ||
= f.input field.name, field.input_options | ||
%td | ||
= spacer | ||
%td | ||
- if (field = group[1]) | ||
= f.input field.name, field.input_options | ||
|
||
:javascript | ||
// if fbtaglist is initialized.. | ||
if (fbtaglist) { | ||
crm.set_tag_list_event('#{controller_name}', '#{asset}', '#{params[:id]}'); | ||
// Reset 'loadedSupertagForms' hash. | ||
loadedFieldGroups = $H(#{field_groups.to_json}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,6 +141,7 @@ | |
end | ||
|
||
get '/:controller/tagged/:id' => '#tagged' | ||
get '/:controller/field_group' => '#field_group' | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//---------------------------------------------------------------------------- | ||
// Adds the 'on_change' hook for the FacebookList, to AJAX load custom field groups. | ||
crm.set_tag_list_event = function(controller, asset, asset_id) { | ||
var extra_field_group_options = $H({ | ||
onAdd: function(tag, el){ | ||
// Check that the tag is not being added more than twice (case INSENSITIVE) | ||
var alreadyAdded = (fbtaglist.bits.values().findAll(function(s){return s.toLowerCase() == tag.toLowerCase() }).length > 1); | ||
if(alreadyAdded){ | ||
// turn off the onDispose hook for this call to .dispose() | ||
var onDisposeHook = fbtaglist.options.get('onDispose'); | ||
fbtaglist.options.set('onDispose', function(el){}); | ||
fbtaglist.dispose(el); | ||
fbtaglist.options.set('onDispose', onDisposeHook); | ||
} else { | ||
// load the field group if not already loaded. | ||
crm.load_field_group(controller, tag, asset_id); | ||
}; | ||
}, | ||
onDispose: function(tag){ | ||
// remove the field group if it was loaded. | ||
tag = tag.toLowerCase(); | ||
var form_id = loadedFieldGroups.get(tag); | ||
if(form_id){ | ||
$(form_id).remove(); | ||
loadedFieldGroups.unset(tag); | ||
}; | ||
} | ||
}); | ||
fbtaglist.options.update(extra_field_group_options); | ||
}; | ||
|
||
//---------------------------------------------------------------------------- | ||
// AJAX loads the form fields for each field group | ||
crm.load_field_group = function(controller, tags, asset_id) { | ||
new Ajax.Request('/' + controller + '/field_groups', { | ||
asynchronous : true, | ||
evalScripts:true, | ||
method:'get', | ||
parameters: { tags : tags, | ||
asset_id : asset_id, | ||
collapsed : "no" } | ||
}); | ||
}; | ||
|
||
//---------------------------------------------------------------------------- | ||
// Fires an 'onclick' event on all '.close' buttons in the DOM. | ||
// (closes any current edit forms) | ||
crm.close_all_forms = function() { | ||
$$('.close').each(function(el){el.onclick();}); | ||
}; | ||
|
||
// Initialize the hash to store which field groups have been loaded. | ||
// {'tag' => 'div element id'} | ||
var loadedFieldGroups = new Hash(); |