Bug 19786: Move template JavaScript to the footer: Authorities, part 2
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / modules / authorities / blinddetail-biblio-search.tt
1 [% SET footerjs = 1 %]
2 [% INCLUDE 'doc-head-open.inc' %]
3 <title>Koha &rsaquo; Authority details</title>
4 [% INCLUDE 'doc-head-close.inc' %]
5 </head>
6
7 <body id="auth_blinddetail-biblio-search" class="auth">
8
9 <div id="doc" class="yui-t7">
10    <div id="bd">
11         <div class="yui-g">
12
13         <div id="loading"> <img src="[% interface %]/[% theme %]/img/spinner-small.gif" alt="" /> Loading </div>
14
15 </div>
16 </div>
17 </div>
18
19 [% MACRO jsinclude BLOCK %]
20     <script type="text/javascript">
21         $(document).ready(function(){
22             var index_start = "[% index %]";
23             var whichfield;
24             try {
25                 whichfield = opener.opener.document.getElementById(index_start);
26             } catch(e) {
27                 return;
28             }
29
30             var field_start = whichfield.parentNode.parentNode;
31
32             // Sets the good number of form fields for the specified subfield
33             // Returns false if the cloning failed
34             function SetSubfieldNumber(subfield_name, nb) {
35                 // Nothing to do if we only have one value
36                 if(nb <= 1) {
37                     return true;
38                 }
39
40                 // Find the subfield we want to clone
41                 var re = new RegExp('^subfield' + subfield_name,'g');
42                 var subfields = $(field_start).children('div').filter( function() {
43                     return this.id.match(re);
44                 });
45
46                 // Try to add as many clones as needed
47                 try {
48                     for(var i=0; i<nb-subfields.length; i++) {
49                         window.opener.opener.CloneSubfield(subfields[0].getAttribute('id'),'[% advancedMARCEditor %]');
50                     }
51                 }
52                 catch(err) {
53                     return false;
54                 }
55                 return true;
56             }
57
58             // Fills the subfield with the values entered in argument
59             function SetSubfieldValues() {
60                 // Get the arguments
61                 var subfield_name = arguments[0];
62                 var values = new Array();
63                 for(var i=1; i<arguments.length; i++) {
64                     values.push(arguments[i]);
65                 }
66
67                 // Create the correct number of form fields for all values
68                 // If the field cloning failed, only the first value will be added to the form.
69                 SetSubfieldNumber(subfield_name, values.length);
70
71                 // Find the subfields where we will add the new values
72                 var re = new RegExp('^subfield' + subfield_name,'g');
73                 var subfields = $(field_start).children('div').filter( function() {
74                     return this.id.match(re);
75                 });
76
77                 // Add the new values to those subfields, empty the additional fields
78                 var i=0;
79                 subfields.each(function() {
80                     if(i in values) {
81                         this.getElementsByTagName('input')[1].value = values[i];
82                     }
83                     else {
84                         this.getElementsByTagName('input')[1].value = "";
85                     }
86                     i++;
87                 });
88             }
89
90             [% UNLESS ( clear ) %]
91             [% FOREACH SUBFIELD_LOO IN SUBFIELD_LOOP %]
92                 SetSubfieldValues(
93                     "[% tag_number |replace("'", "\'") |replace('"', '\"') |replace('\n', '\\n') |replace('\r', '\\r') %][% SUBFIELD_LOO.marc_subfield |replace("'", "\'") |replace('"', '\"') |replace('\n', '\\n') |replace('\r', '\\r') %]"
94                 [% FOREACH marc_value IN SUBFIELD_LOO.marc_values %]
95                     ,"[% marc_value |replace("'", "\'") |replace('"', '\"') |replace('\n', '\\n') |replace('\r', '\\r') %]"
96                 [% END %]
97                 );
98             [% END %]
99                 var indicators = field_start.getElementsByClassName('indicator flat');
100                 indicators[0].value="[% indicator1 %]";
101                 indicators[1].value="[% indicator2 %]";
102             [% END %]
103
104             // browse all its subfields (clear and $9)
105             var subfields = field_start.getElementsByTagName('input');
106             var re = /^tag_\d*_code_/;
107             for(var i=0, len = subfields.length ; i< len ; i++) { // browse all subfields
108                 if(subfields[i].hasAttribute('name') == 0 ) {continue; }  // div elements specific to Select2
109                 if(subfields[i].getAttribute('name').match(re)){ // it s a subfield
110                     var code     = subfields[i];   // code is the first input
111                     var subfield = subfields[i+1]; // subfield the second
112
113                 [% IF ( clear ) %]
114                     if (subfield){subfield.value="" ;}
115                 [% ELSE %]
116                     if(code.value=='9'){
117                         subfield.value = "[% authid |replace("'", "\'") |replace('"', '\"') |replace('\n', '\\n') |replace('\r', '\\r') %]";
118                         break;
119                     }
120                 [% END %]
121                 }
122             }
123
124             opener.close();
125             window.close();
126             return false;
127         });
128     </script>
129 [% END %]
130
131 [% INCLUDE 'intranet-bottom.inc' popup_window = 1 %]