Skip to content

Commit

Permalink
add config option to hide user defined section
Browse files Browse the repository at this point in the history
  • Loading branch information
jambun committed May 22, 2015
1 parent 1d9a40d commit 3090c4a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ Add an entry to your `config.rb` like this:
AppConfig[:user_defined_in_basic] = {
'accessions' => ['boolean_1', 'enum_2', 'real_2'],
'digital_objects' => [],
'resources' => ['string_2', 'date_1', 'boolean_1']
'resources' => ['string_2', 'date_1', 'boolean_1'],
'hide_user_defined_section' => true
}

If you don't have a `:user_defined_in_basic` entry the plugin won't do anything.
It will log a warning at startup.

The three keys shown (`accessions`, `resources`, and `digital_objects`) are the
The three keys, `accessions`, `resources`, and `digital_objects`, are the
record types that can have a `user_defined` subrecord. For each key specified
a `user_defined` subrecord will be automatically added when a new record
is created through the staff UI, or will be added to an existing record when
Expand All @@ -53,3 +54,8 @@ to see in the `Basic Information`. The fields specified will be moved to the
If you specify a field that doesn't exist in the `user_defined` record,
a warning will be logged at startup.

The `hide_user_defined_section` key is optional. If specified with a value of
`true`, then the User Defined section will be hidden in View and Edit modes.
This will give a cleaner display, but it means it will only be possible to
edit the user defined fields that have been moved to the Basic Information
section.
16 changes: 13 additions & 3 deletions frontend/assets/user_defined_in_basic.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function UserDefinedInBasic() {
}

UserDefinedInBasic.prototype.init = function(fields, read_only_view) {
UserDefinedInBasic.prototype.init = function(fields, read_only_view, hide_user_defined_section) {
var bi = $("#basic_information");

if (bi.length == 0) {
Expand All @@ -15,10 +15,20 @@ UserDefinedInBasic.prototype.init = function(fields, read_only_view) {
var remove_btn = user_defined_section.find('.subrecord-form-remove');
if (remove_btn.length == 0) {
user_defined_section.find('.btn-default').filter(':visible').click();
user_defined_section.find('.subrecord-form-remove').attr('disabled', 'disabled');
window.scrollTo(0,0);
}

// hide the remains of the user defined section if configured thus
if (hide_user_defined_section) {
if (read_only_view) {
$('section[id$=_user_defined_]').hide();
} else {
user_defined_section.hide();
}
} else {
remove_btn.attr('disabled', 'disabled');
if (!read_only_view) {
user_defined_section.find('.subrecord-form-remove').attr('disabled', 'disabled');
}
}

fields.map(function (field) {
Expand Down
13 changes: 8 additions & 5 deletions frontend/plugin_init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
# check configuration
if AppConfig.has_key?(:user_defined_in_basic)
AppConfig[:user_defined_in_basic].each do |keys, fields|
fields.each do |field|
unless JSONModel(:user_defined).schema['properties'].include?(field)
$stderr.puts "WARNING: user_defined_in_basic plugin configuration includes " +
"a field (#{fld}) in the list for #{k} which is not a user_defined field. " +
"That's ok, we're just concerned you might have intended to refer to an actual field."
if fields.respond_to?(:each)
fields.each do |field|
unless JSONModel(:user_defined).schema['properties'].include?(field)
$stderr.puts "WARNING: user_defined_in_basic plugin configuration includes " +
"a field (#{fld}) in the list for #{k} which is not a user_defined field. " +
"That's ok, we're just concerned you might have intended to refer to an actual field."
end
end
end
end
AppConfig[:user_defined_in_basic]['hide_user_defined_section'] ||= false
else
$stderr.puts "WARNING: user_defined_in_basic plugin is active but not configured. " +
"That's ok, it just won't do anything."
Expand Down
5 changes: 3 additions & 2 deletions frontend/views/layout_head.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ end
<script>
var fields = <%= ASUtils.to_json(map_user_defined_to_labels(AppConfig[:user_defined_in_basic].fetch(controller.controller_name, []))).html_safe %>;
var read_only_view = <%= controller.action_name == 'show' %>;

var hide_user_defined_section = <%= AppConfig[:user_defined_in_basic]['hide_user_defined_section'] %>;

var init = function () {
// Funny setTimeout here to move our initialization to the
// end of the page load. We need this to run after the user
// defined field subform is in and its buttons have been wired
// up.
setTimeout(function () {
new UserDefinedInBasic().init(fields, read_only_view);
new UserDefinedInBasic().init(fields, read_only_view, hide_user_defined_section);
}, 0);
}

Expand Down

0 comments on commit 3090c4a

Please sign in to comment.