Skip to content

Commit 01117a2

Browse files
committed
first working version
1 parent d32b40e commit 01117a2

4 files changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
$(document).bind("loadedrecordform.aspace", init);
2+
3+
$(window).load(init);
4+
5+
function init() {
6+
if ($("#basic_information").find('.form-group').length == 0) {
7+
return;
8+
}
9+
10+
var bi = document.getElementById("basic_information");
11+
var for_fld = bi.querySelector("label").getAttribute('for');
12+
13+
if (for_fld == null) {
14+
return;
15+
}
16+
17+
var record_type = '';
18+
if (for_fld.lastIndexOf('accession') === 0) {
19+
record_type = 'accession';
20+
} else if (for_fld.lastIndexOf('resource') === 0) {
21+
record_type = 'resource';
22+
} else if (for_fld.lastIndexOf('digital_object') === 0) {
23+
record_type = 'digital_object';
24+
} else {
25+
return;
26+
}
27+
28+
var fields = {};
29+
<% ['accession', 'digital_object', 'resource'].each do |rec| %>
30+
fields['<%= rec %>'] = [];
31+
<% AppConfig[:user_defined_in_basic][rec].each do |fld| %>
32+
fields['<%= rec %>'].push('<%= fld %>');
33+
<% end %>
34+
<% end %>
35+
36+
for (i = 0; i < fields[record_type].length; i++) {
37+
var field = fields[record_type][i];
38+
var fld_lab = document.getElementById(record_type + "_user_defined_").
39+
querySelector("[for="+record_type+"_user_defined__"+field+"_]");
40+
41+
if (fld_lab != null) {
42+
bi.insertBefore(fld_lab.parentElement, bi.querySelector("div.audit-display-wide"));
43+
}
44+
}
45+
}

frontend/plugin_init.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Rails.application.config.after_initialize do
2+
3+
AspaceFormHelper.class_eval do
4+
5+
PROPERTIES_TO_EXCLUDE_FROM_READ_ONLY_VIEW = ["jsonmodel_type", "lock_version", "_resolved", "uri", "ref", "create_time", "system_mtime", "user_mtime", "created_by", "last_modified_by", "sort_name_auto_generate", "suppressed", "display_string", "file_uri"]
6+
7+
# This is a copy of read_only_view in the AspaceFormHelper
8+
# The only significant difference is that it adds a 'for' attribute to
9+
# the control-label so that the js can tell which field it belongs to
10+
# cut and paste coding - sorry, bad
11+
def awesomer_read_only_view(hash, opts = {})
12+
jsonmodel_type = hash["jsonmodel_type"]
13+
schema = JSONModel(jsonmodel_type).schema
14+
prefix = opts[:plugin] ? 'plugins.' : ''
15+
html = "<div class='form-horizontal'>"
16+
17+
hash.reject {|k,v| PROPERTIES_TO_EXCLUDE_FROM_READ_ONLY_VIEW.include?(k)}.each do |property, value|
18+
19+
if schema and schema["properties"].has_key?(property)
20+
if (schema["properties"][property].has_key?('dynamic_enum'))
21+
value = I18n.t("#{prefix}enumerations.#{schema["properties"][property]["dynamic_enum"]}.#{value}", :default => value)
22+
elsif schema["properties"][property].has_key?("enum")
23+
value = I18n.t("#{prefix}#{jsonmodel_type.to_s}.#{property}_#{value}", :default => value)
24+
elsif schema["properties"][property]["type"] === "boolean"
25+
value = value === true ? "True" : "False"
26+
elsif schema["properties"][property]["type"] === "date"
27+
value = value.blank? ? "" : Date.strptime(value, "%Y-%m-%d")
28+
elsif schema["properties"][property]["type"] === "array"
29+
# this view doesn't support arrays
30+
next
31+
elsif value.kind_of? Hash
32+
# can't display an object either
33+
next
34+
end
35+
end
36+
37+
html << "<div class='form-group'>"
38+
html << "<div class='control-label col-sm-2' for='#{opts['parent']}_#{jsonmodel_type.to_s}__#{property}_'>#{I18n.t("#{prefix}#{jsonmodel_type.to_s}.#{property}")}</div>"
39+
html << "<div class='label-only col-md-9'>#{value}</div>"
40+
html << "</div>"
41+
42+
end
43+
44+
html << "</div>"
45+
46+
html.html_safe
47+
end
48+
49+
end
50+
51+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= javascript_include_tag "#{AppConfig[:frontend_prefix]}assets/user_defined_in_basic.js" %>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<%
2+
section_id = "user_defined" if section_id.blank?
3+
4+
%>
5+
6+
<section id="<%= section_id %>" class="subrecord-form-dummy">
7+
<h3><%= I18n.t("user_defined._singular") %></h3>
8+
9+
<div class="subrecord-form-container">
10+
<div class="subrecord-form-fields">
11+
<%= awesomer_read_only_view(user_defined, 'parent' => context['jsonmodel_type']) %>
12+
</div>
13+
</div>
14+
</section>

0 commit comments

Comments
 (0)