Lines 2-121
Link Here
|
2 |
* Merging 2 source records into a destination record |
2 |
* Merging 2 source records into a destination record |
3 |
*/ |
3 |
*/ |
4 |
|
4 |
|
5 |
/** |
5 |
function build_target_record($sources) { |
6 |
* Check or uncheck a field or subfield in a source record |
6 |
var target_record = {}; |
7 |
* @param pField the checkbox clicked |
7 |
|
8 |
*/ |
8 |
$sources.find('.record input[type="checkbox"].fieldpick:checked').each(function() { |
9 |
function toggleField(pField) { |
9 |
var $checkbox = $(this); |
10 |
|
10 |
var $li = $checkbox.parent(); |
11 |
// Getting the key of the clicked checkbox |
11 |
var field = $checkbox.parent().find("span.field").text(); |
12 |
var ckid = $(pField).attr("id"); |
12 |
|
13 |
var tab = ckid.split('_'); |
13 |
if (!(field in target_record)) { |
14 |
var source = tab[1]; // From which record the click came from |
14 |
target_record[field] = []; |
15 |
var key = tab[2]; |
15 |
} |
16 |
var type = $(pField).attr("class"); |
16 |
target_record[field].push({ |
17 |
|
17 |
'id' : $li.attr('id'), |
18 |
// Getting field/subfield |
18 |
'tag' : field, |
19 |
var field; |
19 |
'subfields' : [] |
20 |
var subfield; |
20 |
}); |
21 |
if (type == "subfieldpick") { |
21 |
}); |
22 |
field = $(pField).parent().parent().parent().find("span.field").text(); |
22 |
|
23 |
subfield = $(pField).parent().find("span.subfield").text(); |
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(); |
29 |
|
30 |
var target_field; |
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]; |
35 |
} |
36 |
} |
37 |
if (!target_field) { |
38 |
target_field = target_record[field][0]; |
39 |
} |
40 |
} |
41 |
if (target_field) { |
42 |
target_field.subfields.push({ |
43 |
'id' : $li.attr('id'), |
44 |
'code' : subfield |
45 |
}); |
24 |
} else { |
46 |
} else { |
25 |
field = $(pField).parent().find("span.field").text(); |
47 |
$field_li.find('input.fieldpick').attr('checked', true); |
|
|
48 |
target_record[field] = [{ |
49 |
'id' : $field_li.attr('id'), |
50 |
'tag' : field, |
51 |
'subfields' : [{ |
52 |
'id' : $li.attr('id'), |
53 |
'code' : subfield |
54 |
}] |
55 |
}]; |
26 |
} |
56 |
} |
|
|
57 |
}); |
27 |
|
58 |
|
28 |
// If the field has just been checked |
59 |
return target_record; |
29 |
if (pField.checked) { |
60 |
} |
30 |
|
61 |
|
31 |
// We check for repeatability |
62 |
function field_can_be_added($sources, $li) { |
32 |
var canbeadded = true; |
63 |
target_record = build_target_record($sources); |
33 |
if (type == "subfieldpick") { |
64 |
|
34 |
var repeatable = 1; |
65 |
var tag = $li.find('span.field').text(); |
35 |
var alreadyexists = 0; |
66 |
var repeatable = true; |
36 |
if (tagslib[field] && tagslib[field][subfield]) { |
67 |
if (tag in tagslib) { |
37 |
// Note : we can't use the dot notation here (tagslib.021) because the key is a number |
68 |
repeatable = (tagslib[tag].repeatable != 0) ? true : false; |
38 |
repeatable = tagslib[field][subfield].repeatable; |
69 |
} |
39 |
// TODO : Checking for subfields |
70 |
|
40 |
} |
71 |
if ((!repeatable) && (tag in target_record)) { |
41 |
} else { |
72 |
alert(MSG_MERGEREC_ALREADY_EXISTS); |
42 |
if (tagslib[field]) { |
73 |
return false; |
43 |
repeatable = tagslib[field].repeatable; |
74 |
} |
44 |
alreadyexists = $("#resultul span.field:contains(" + field + ")"); |
75 |
|
45 |
if (repeatable == 0 && alreadyexists.length != 0) { |
76 |
return true; |
46 |
canbeadded = false; |
77 |
} |
47 |
} |
78 |
|
48 |
} |
79 |
function subfield_can_be_added($sources, $li) { |
|
|
80 |
target_record = build_target_record($sources); |
81 |
|
82 |
var $field_li = $li.parents('li'); |
83 |
var tag = $field_li.find('span.field').text(); |
84 |
var code = $li.find('span.subfield').text(); |
85 |
|
86 |
var repeatable = true; |
87 |
if (tag in tagslib && code in tagslib[tag]) { |
88 |
repeatable = (tagslib[tag][code].repeatable != 0) ? true : false; |
89 |
} |
90 |
|
91 |
if (!repeatable) { |
92 |
var target_field; |
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]; |
97 |
} |
98 |
} |
99 |
if (!target_field) { |
100 |
target_field = target_record[tag][0]; |
101 |
} |
102 |
} |
103 |
if (target_field) { |
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); |
108 |
return false; |
49 |
} |
109 |
} |
|
|
110 |
} |
111 |
} |
112 |
} |
113 |
|
114 |
return true; |
115 |
} |
50 |
|
116 |
|
51 |
// If the field is not repeatable, we check if it already exists in the result table |
117 |
function rebuild_target($sources, $target) { |
52 |
if (canbeadded == false) { |
118 |
target_record = build_target_record($sources); |
53 |
alert(MSG_MERGEREC_ALREADY_EXISTS); |
119 |
|
54 |
pField.checked = 0; |
120 |
$target.empty(); |
55 |
} else { |
121 |
var keys = $.map(target_record, function(elem, idx) { return idx }).sort(); |
56 |
|
122 |
for (k in keys) { |
57 |
// Cloning the field or subfield we picked |
123 |
var tag = keys[k]; |
58 |
var clone = $(pField).parent().clone(); |
124 |
var fields = target_record[tag]; |
59 |
|
125 |
for (i in fields) { |
60 |
// Removing the checkboxes from it |
126 |
var field = fields[i]; |
61 |
$(clone).find("input.subfieldpick, input.fieldpick").each(function() { |
127 |
if (field.subfields.length > 0) { |
62 |
$(this).remove(); |
128 |
var $field_clone = $('#' + field.id).clone(); |
63 |
}); |
129 |
$field_clone.find('ul').empty(); |
64 |
|
130 |
$field_clone.find('.fieldpick').remove(); |
65 |
// If we are a subfield |
131 |
$target.append($field_clone); |
66 |
if (type == "subfieldpick") { |
132 |
|
67 |
// then we need to find who is our parent field... |
133 |
for (j in field.subfields) { |
68 |
fieldkey = $(pField).parent().parent().parent().attr("id"); |
134 |
var subfield = field.subfields[j]; |
69 |
|
135 |
var $subfield_clone = $('#' + subfield.id).clone(); |
70 |
// Find where to add the subfield |
136 |
$subfield_clone.find('.subfieldpick').remove(); |
71 |
|
137 |
$field_clone.find('ul').append($subfield_clone); |
72 |
// First, check if the field is not already in the destination record |
|
|
73 |
if ($("#resultul li#" + fieldkey).length > 0) { |
74 |
|
75 |
// If so, we add our field to it |
76 |
$("#resultul li#" + fieldkey + " ul").append(clone); |
77 |
} else { |
78 |
|
79 |
// If not, we add the subfield to the first matching field |
80 |
var where = 0; |
81 |
$("#resultul li span.field").each(function() { |
82 |
if (where == 0 && $(this).text() == field) { |
83 |
where = this; |
84 |
} |
85 |
}); |
86 |
|
87 |
// If there is no matching field in the destination record |
88 |
if (where == 0) { |
89 |
|
90 |
// TODO: |
91 |
// We select the whole field and removing non-selected subfields, instead of... |
92 |
|
93 |
// Alerting the user |
94 |
alert(MSG_MERGEREC_SUBFIELD_PRE + " " + field + " " + MSG_MERGEREC_SUBFIELD_POST); |
95 |
pField.checked = false; |
96 |
} else { |
97 |
$(where).nextAll("ul").append(clone); |
98 |
} |
99 |
|
100 |
} |
101 |
|
102 |
} else { |
103 |
// If we are a field |
104 |
var where = 0; |
105 |
// Find where to add the field |
106 |
$("#resultul li span.field").each(function() { |
107 |
if (where == 0 && $(this).text() > field) { |
108 |
where = this; |
109 |
} |
110 |
}); |
111 |
|
112 |
$(where).parent().before(clone); |
113 |
} |
114 |
} |
138 |
} |
115 |
} else { |
139 |
} else { |
116 |
// Else, we remove it from the results tab |
140 |
$('#' + field.id).find('input.fieldpick').attr('checked', false); |
117 |
$("ul#resultul li#k" + key).remove(); |
141 |
} |
118 |
} |
142 |
} |
|
|
143 |
} |
119 |
} |
144 |
} |
120 |
|
145 |
|
121 |
/* |
146 |
/* |
Lines 124-139
function toggleField(pField) {
Link Here
|
124 |
$(document).ready(function(){ |
149 |
$(document).ready(function(){ |
125 |
// When a field is checked / unchecked |
150 |
// When a field is checked / unchecked |
126 |
$('input.fieldpick').click(function() { |
151 |
$('input.fieldpick').click(function() { |
127 |
toggleField(this); |
|
|
128 |
// (un)check all subfields |
152 |
// (un)check all subfields |
129 |
var ischecked = this.checked; |
153 |
var ischecked = this.checked; |
|
|
154 |
if (ischecked && !field_can_be_added($('#tabs'), $(this).parent())) { |
155 |
return false; |
156 |
} |
157 |
|
130 |
$(this).parent().find("input.subfieldpick").each(function() { |
158 |
$(this).parent().find("input.subfieldpick").each(function() { |
131 |
this.checked = ischecked; |
159 |
this.checked = ischecked; |
132 |
}); |
160 |
}); |
|
|
161 |
rebuild_target($('#tabs'), $('#resultul')); |
133 |
}); |
162 |
}); |
134 |
|
163 |
|
135 |
// When a field or subfield is checked / unchecked |
164 |
// When a field or subfield is checked / unchecked |
136 |
$("input.subfieldpick").click(function() { |
165 |
$("input.subfieldpick").click(function() { |
137 |
toggleField(this); |
166 |
var ischecked = this.checked; |
|
|
167 |
if (ischecked && !subfield_can_be_added($('#tabs'), $(this).parent())) { |
168 |
return false; |
169 |
} |
170 |
rebuild_target($('#tabs'), $('#resultul')); |
138 |
}); |
171 |
}); |
139 |
}); |
172 |
}); |