Bug 10650 - export merge-record.inc javascript into a file
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / js / merge-record.js
1 /*
2  * Merging 2 source records into a destination record
3  */
4
5 /**
6  * Check or uncheck a field or subfield in a source record
7  * @param pField the checkbox clicked
8  */
9 function toggleField(pField) {
10
11     // Getting the key of the clicked checkbox
12     var ckid   = $(pField).attr("id");
13     var tab    = ckid.split('_');
14     var source = tab[1]; // From which record the click came from
15     var key    = tab[2];
16     var type   = $(pField).attr("class");
17
18     // Getting field/subfield
19     var field;
20     var subfield;
21     if (type == "subfieldpick") {
22         field = $(pField).parent().parent().parent().find("span.field").text();
23         subfield = $(pField).parent().find("span.subfield").text();
24     } else {
25         field = $(pField).parent().find("span.field").text();
26     }
27
28     // If the field has just been checked
29     if (pField.checked) {
30
31         // We check for repeatability
32         var canbeadded = true;
33         if (type == "subfieldpick") {
34             var repeatable = 1;
35             var alreadyexists = 0;
36             if (tagslib[field] && tagslib[field][subfield]) {
37                 // Note : we can't use the dot notation here (tagslib.021) because the key is a number
38                 repeatable = tagslib[field][subfield].repeatable;
39                 // TODO : Checking for subfields
40             }
41         } else {
42             if (tagslib[field]) {
43                 repeatable = tagslib[field].repeatable;
44                 alreadyexists = $("#resultul span.field:contains(" + field + ")");
45                 if (repeatable == 0 && alreadyexists.length != 0) {
46                     canbeadded = false;
47                 }
48             }
49         }
50
51         // If the field is not repeatable, we check if it already exists in the result table
52         if (canbeadded == false) {
53             alert(MSG_MERGEREC_ALREADY_EXISTS);
54             pField.checked = 0;
55         } else {
56
57             // Cloning the field or subfield we picked
58             var clone = $(pField).parent().clone();
59
60             // Removing the checkboxes from it
61             $(clone).find("input.subfieldpick, input.fieldpick").each(function() {
62                 $(this).remove();
63             });
64
65             // If we are a subfield
66             if (type == "subfieldpick") {
67                 // then we need to find who is our parent field...
68                 fieldkey = $(pField).parent().parent().parent().attr("id");
69
70                 // Find where to add the subfield
71
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         }
115     } else {
116         // Else, we remove it from the results tab
117         $("ul#resultul li#k" + key).remove();
118     }
119 }
120
121 /*
122  * Add actions on field and subfields checkboxes
123  */
124 $(document).ready(function(){
125     // When a field is checked / unchecked
126     $('input.fieldpick').click(function() {
127         toggleField(this);
128         // (un)check all subfields
129         var ischecked = this.checked;
130         $(this).parent().find("input.subfieldpick").each(function() {
131             this.checked = ischecked;
132         });
133     });
134
135     // When a field or subfield is checked / unchecked
136     $("input.subfieldpick").click(function() {
137         toggleField(this);
138     });
139 });