/
finto_yso-kauno.pl
executable file
·163 lines (149 loc) · 5.38 KB
/
finto_yso-kauno.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/perl
# Copyright 2019 KohaSuomi oy
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use C4::Context;
use C4::Languages;
my $builder = sub {
my ( $params ) = @_;
my $function_name = $params->{id};
my %args;
my $vocab = "yso kauno";
my $language = C4::Languages::getlanguage() || 'fi';
$language = (split(/-/, $language))[0];
my $langcode = "fin";
$langcode = "eng" if ($language eq 'en');
$langcode = "swe" if ($language eq 'sv');
my $js = <<END_OF_JS;
<script type="text/javascript">
//<![CDATA[
if (!window.FintoCache) window.FintoCache = [];
function gatherdata$function_name(id, sels) {
if (!sels.id)
return;
var fid = \$('#'+id).parent().parent().attr('id');
var re = /^(tag_..._)/;
var found = fid.match(re);
var ind2 = found[1] + 'indicator2_';
var sfid0 = found[1] + 'subfield_0_';
var sfid2 = found[1] + 'subfield_2_';
var sf2val;
var sf0val;
if (sels.userdef) {
sf2val = "local";
sf0val = "";
} else {
if (sels.vocab) {
sf2val = sels.vocab + "/$langcode";
\$('#'+id).data('vocab', sels.vocab);
}
if (sels.uri) {
sf0val = sels.uri;
\$('#'+id).data('uri', sels.uri);
}
}
/*\$('#'+id).parent().parent().find("input[name^='"+ind2+"']").val("7");*/
if (typeof sf2val !== "undefined") \$('#'+id).parent().parent().find("input[id^='"+sfid2+"']").val(sf2val);
if (typeof sf0val !== "undefined") \$('#'+id).parent().parent().find("input[id^='"+sfid0+"']").val(sf0val);
}
function Focus$function_name(elementid, force) {
if (\$("#" + elementid).data("select2-enabled") == 1) { return; };
\$("#" + elementid).data("select2-enabled", 1).select2({
width: 'resolve',
data: { id:"", text: "" },
initSelection: function(element, callback) {
var v = element.val();
var duri = element.data('uri');
var dvocab = element.data('vocab');
callback({ id:v, text:v, uri: duri, vocab: dvocab });
},
escapeMarkup: function(m) { return m; },
/*allowClear: true,*/
/*tags: false,*/
/*multiple: false,*/
/*maximumSelectionLength: 1,*/
/*maximumSelectionSize: 1,*/
/*createSearchChoice: function(term, data) { return { id: term, text: term + " <i>(local)</i>", userdef: true }; },*/
minimumInputLength: 2,
ajax: {
url:'https://api.finto.fi/rest/v1/search',
dataType: 'json',
delay: 250,
quietMillis: 250,
cache: true,
data: function(params) {
var query = {
vocab: '$vocab',
query: '*' + params + '*',
lang: '$language',
type: 'skos:Concept',
unique: 1,
}
return query;
},
processResults: function(data) {
var tmp = \$.map(data.results, function(obj){
var sl = obj.prefLabel;
if (obj.altLabel) { sl += " <i>("+obj.altLabel+")</i>" };
if (obj.vocab && / /.test("$vocab")) { sl += " <i>("+obj.vocab+")</i>" }
return { id: obj.prefLabel,
text: sl,
uri: obj.uri,
vocab: obj.vocab }
});
return { results: tmp };
},
transport: function(params) {
if (params.dataType == "json") {
var cachekey = params.data.vocab+","+params.data.query+","+params.data.lang;
if (window.FintoCache && window.FintoCache[cachekey]) {
var res = window.FintoCache[cachekey];
params.success(res);
return {
abort: function() { console.log("FINTO: AJAX call aborted"); }
}
} else {
var \$request = \$.ajax(params);
\$request.then(function (data) {
window.FintoCache[cachekey] = data;
return data;
})
.then(params.success)
\$request.fail(params.failure);
return \$request;
}
} else {
var \$request = \$.ajax(params);
\$request.then(params.success);
\$request.fail(params.failure);
return \$request;
}
},
}
})
.focus()
.on('select2-blur', function() { var sels=\$(this).select2('data'); gatherdata$function_name(\$(this).attr('id'), sels); \$(this).data("select2-enabled", 0); \$(this).off('select2-blur'); \$(this).off('select2-close'); \$(this).off('select2-select'); \$(this).off('change'); \$(this).select2('destroy'); })
.on('select2-select', function(e) { \$(this).trigger({ type: 'select2-blur' }); })
.on('select2-close', function() { \$(this).trigger({ type: 'select2-select' }); })
.data('select2').open()
}
//]]>
</script>
END_OF_JS
return $js;
};
return { builder => $builder };