2 * Merging 2 source records into a destination record
5 function build_target_record($sources) {
6 var target_record = {};
8 $sources.find('.record input[type="checkbox"].fieldpick:checked').each(function() {
9 var $checkbox = $(this);
10 var $li = $checkbox.parent();
11 var field = $checkbox.parent().find("span.field").text();
13 if (!(field in target_record)) {
14 target_record[field] = [];
16 target_record[field].push({
17 'id' : $li.attr('id'),
23 $sources.find('.record input[type="checkbox"].subfieldpick:checked').each(function() {
24 var $checkbox = $(this);
25 var $li = $checkbox.parent();
26 var $field_li = $li.parents('li');
27 var field = $field_li.find('span.field').text();
28 var subfield = $li.find('span.subfield').text();
31 if (field in target_record) {
32 for (i in target_record[field]) {
33 if (target_record[field][i].id == $field_li.attr('id')) {
34 target_field = target_record[field][i];
38 target_field = target_record[field][0];
42 target_field.subfields.push({
43 'id' : $li.attr('id'),
47 $field_li.find('input.fieldpick').prop('checked', true);
48 target_record[field] = [{
49 'id' : $field_li.attr('id'),
52 'id' : $li.attr('id'),
62 function field_can_be_added($sources, $li) {
63 target_record = build_target_record($sources);
65 var tag = $li.find('span.field').text();
66 var repeatable = true;
68 repeatable = (tagslib[tag].repeatable != 0) ? true : false;
71 if ((!repeatable) && (tag in target_record)) {
72 alert(MSG_MERGEREC_ALREADY_EXISTS);
79 function subfield_can_be_added($sources, $li) {
80 target_record = build_target_record($sources);
82 var $field_li = $li.parents('li');
83 var tag = $field_li.find('span.field').text();
84 var code = $li.find('span.subfield').text();
86 var repeatable = true;
87 if (tag in tagslib && code in tagslib[tag]) {
88 repeatable = (tagslib[tag][code].repeatable != 0) ? true : false;
93 if (tag in target_record) {
94 for (i in target_record[tag]) {
95 if (target_record[tag][i].id == $field_li.attr('id')) {
96 target_field = target_record[tag][i];
100 target_field = target_record[tag][0];
104 for (i in target_field.subfields) {
105 var subfield = target_field.subfields[i];
106 if (code == subfield.code) {
107 alert(MSG_MERGEREC_SUBFIELD_ALREADY_EXISTS);
117 function rebuild_target($sources, $target) {
118 target_record = build_target_record($sources);
121 var keys = $.map(target_record, function(elem, idx) { return idx }).sort();
124 var fields = target_record[tag];
126 var field = fields[i];
127 if (parseInt(tag) < 10) {
128 var $field_clone = $('#' + field.id).clone();
129 $field_clone.find('.fieldpick').remove();
130 $target.append($field_clone);
131 } else if (field.subfields.length > 0) {
132 var $field_clone = $('#' + field.id).clone();
133 $field_clone.find('ul').empty();
134 $field_clone.find('.fieldpick').remove();
135 $target.append($field_clone);
137 for (j in field.subfields) {
138 var subfield = field.subfields[j];
139 var $subfield_clone = $('#' + subfield.id).clone();
140 $subfield_clone.find('.subfieldpick').remove();
141 $field_clone.find('ul').append($subfield_clone);
144 $('#' + field.id).find('input.fieldpick').prop('checked', false);
151 * Add actions on field and subfields checkboxes
153 $(document).ready(function(){
154 // When a field is checked / unchecked
155 $('input.fieldpick').click(function() {
156 var ischecked = this.checked;
158 $(this).prop('checked', false);
159 if (!field_can_be_added($('#tabs'), $(this).parent())) {
162 $(this).prop('checked', true);
165 // (un)check all subfields
166 $(this).parent().find("input.subfieldpick").each(function() {
167 this.checked = ischecked;
169 rebuild_target($('#tabs'), $('#resultul'));
172 // When a field or subfield is checked / unchecked
173 $("input.subfieldpick").click(function() {
174 var ischecked = this.checked;
176 $(this).prop('checked', false);
177 if (!subfield_can_be_added($('#tabs'), $(this).parent())) {
180 $(this).prop('checked', true);
182 rebuild_target($('#tabs'), $('#resultul'));