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