Line 0
Link Here
|
|
|
1 |
$(document).ready(function() { |
2 |
|
3 |
window.doSearch = function() { |
4 |
// In case the source doesn't supply data required for DT to calculate |
5 |
// pagination, we need to do it ourselves |
6 |
var ownPagination = false; |
7 |
var directionSet = false; |
8 |
var start = 0; |
9 |
var forward = true; // true == forward, false == backwards |
10 |
// Arbitrary starting value, it will be corrected by the first |
11 |
// page of results |
12 |
var pageSize = 20; |
13 |
|
14 |
var tableTmpl = { |
15 |
ajax: { |
16 |
cache: true, // Prevent DT appending a "_" cache param |
17 |
}, |
18 |
columns: [ |
19 |
// defaultContent prevents DT from choking if |
20 |
// the API response doesn't return a column |
21 |
{ |
22 |
title: 'Source', |
23 |
data: 'source', |
24 |
defaultContent: '' |
25 |
}, |
26 |
{ |
27 |
data: 'title', |
28 |
defaultContent: '' |
29 |
}, |
30 |
{ |
31 |
data: 'author', |
32 |
defaultContent: '' |
33 |
}, |
34 |
{ |
35 |
data: 'isbn', |
36 |
defaultContent: '' |
37 |
}, |
38 |
{ |
39 |
data: 'issn', |
40 |
defaultContent: '' |
41 |
}, |
42 |
{ |
43 |
data: 'date', |
44 |
defaultContent: '' |
45 |
} |
46 |
] |
47 |
}; |
48 |
|
49 |
// render functions don't get copied across when we make a dereferenced |
50 |
// copy of them, so we have to reattach them once we have a copy |
51 |
// Here we store them |
52 |
var renders = { |
53 |
title: function(data, type, row) { |
54 |
return row.url ? |
55 |
'<a href="'+row.url+'" target="_blank">'+row.title+'</a>' : |
56 |
row.title; |
57 |
}, |
58 |
source: function(data, type, row) { |
59 |
return row.opac_url ? |
60 |
'<a href="'+row.opac_url+'" target="_blank">'+row.source+'</a>' : |
61 |
row.source; |
62 |
} |
63 |
}; |
64 |
|
65 |
services.forEach(function(service) { |
66 |
// Create a deferenced copy of our table definition object |
67 |
var tableDef = JSON.parse(JSON.stringify(tableTmpl)); |
68 |
// Iterate the table's columns array and add render functions |
69 |
// as necessary |
70 |
tableDef.columns.forEach(function(column) { |
71 |
if (renders[column.data]) { |
72 |
column.render = renders[column.data]; |
73 |
} |
74 |
}); |
75 |
tableDef.ajax.dataSrc = function(data) { |
76 |
var results = data.results.search_results; |
77 |
// The source appears to be returning it's own pagination |
78 |
// data |
79 |
if ( |
80 |
results.hasOwnProperty('recordsFiltered') || |
81 |
results.hasOwnProperty('recordsTotal') |
82 |
) { |
83 |
return results; |
84 |
} |
85 |
// Set up our own pagination values based on what we just |
86 |
// got back |
87 |
ownPagination = true; |
88 |
directionSet = false; |
89 |
pageSize = results.length; |
90 |
// These values are completely arbitrary, but they enable |
91 |
// us to display pagination links |
92 |
data.recordsFiltered = 5000, |
93 |
data.recordsTotal = 5000; |
94 |
|
95 |
return results; |
96 |
}; |
97 |
tableDef.ajax.data = function(data) { |
98 |
// Datatables sends a bunch of superfluous params |
99 |
// that we don't want to litter our API schema |
100 |
// with, so just remove them from the request |
101 |
if (data.hasOwnProperty('columns')) { |
102 |
delete data.columns; |
103 |
} |
104 |
if (data.hasOwnProperty('draw')) { |
105 |
delete data.draw; |
106 |
} |
107 |
if (data.hasOwnProperty('order')) { |
108 |
delete data.order; |
109 |
} |
110 |
if (data.hasOwnProperty('search')) { |
111 |
delete data.search; |
112 |
} |
113 |
// If we're handling our own pagination, set the properties |
114 |
// that DT will send in the request |
115 |
if (ownPagination) { |
116 |
start = forward ? start + pageSize : start - pageSize; |
117 |
data.start = start; |
118 |
data.length = pageSize; |
119 |
} |
120 |
// We may need to restrict the service IDs being queries, this |
121 |
// needs to be handled in the plugin's API module |
122 |
var restrict = $('#service_id_restrict'). |
123 |
attr('data-service_id_restrict_ids'); |
124 |
if (restrict && restrict.length > 0) { |
125 |
data.restrict = restrict; |
126 |
} |
127 |
}; |
128 |
// Add any datatables config options passed from the service |
129 |
// to the table definition |
130 |
tableDef.ajax.url = service.endpoint + metadata; |
131 |
if (service.hasOwnProperty('datatablesConfig')) { |
132 |
var conf = service.datatablesConfig; |
133 |
for (var key in conf) { |
134 |
// The config from the service definition comes from a Perl |
135 |
// hashref, therefore can't contain true/false, so we |
136 |
// special case it |
137 |
if (conf.hasOwnProperty(key)) { |
138 |
if (conf[key] == 'false') { |
139 |
// Special case false values |
140 |
tableDef[key] = false; |
141 |
} else if (conf[key] == 'true') { |
142 |
// Special case true values |
143 |
tableDef[key] = true; |
144 |
} else { |
145 |
// Copy the property value |
146 |
tableDef[key] = conf[key]; |
147 |
} |
148 |
} |
149 |
} |
150 |
} |
151 |
// Create event watchers for the "next" and "previous" pagination |
152 |
// links, this enables us to set the direction the next request is |
153 |
// going in when we're doing our own pagination. We use "hover" |
154 |
// because the click event is caught after the request has been |
155 |
// sent |
156 |
tableDef.drawCallback = function() { |
157 |
$('.paginate_button.next:not(.disabled)', |
158 |
this.api().table().container() |
159 |
).on('hover', function() { |
160 |
forward = true; |
161 |
directionSet = true; |
162 |
}); |
163 |
$('.paginate_button.previous:not(.disabled)', |
164 |
this.api().table().container() |
165 |
).on('hover', function() { |
166 |
forward = false; |
167 |
directionSet = true; |
168 |
}); |
169 |
} |
170 |
// Initialise the table |
171 |
KohaTable(service.id, tableDef); |
172 |
}); |
173 |
} |
174 |
|
175 |
|
176 |
}); |