1 $(document).ready(function() {
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;
9 var forward = true; // true == forward, false == backwards
10 // Arbitrary starting value, it will be corrected by the first
16 cache: true, // Prevent DT appending a "_" cache param
19 // defaultContent prevents DT from choking if
20 // the API response doesn't return a column
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
53 title: function(data, type, row) {
55 '<a href="'+row.url+'" target="_blank">'+row.title+'</a>' :
58 source: function(data, type, row) {
60 '<a href="'+row.opac_url+'" target="_blank">'+row.source+'</a>' :
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
70 tableDef.columns.forEach(function(column) {
71 if (renders[column.data]) {
72 column.render = renders[column.data];
75 tableDef.ajax.dataSrc = function(data) {
76 var results = data.results.search_results;
77 // The source appears to be returning it's own pagination
80 data.hasOwnProperty('recordsFiltered') ||
81 data.hasOwnProperty('recordsTotal')
85 // Set up our own pagination values based on what we just
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;
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')) {
104 if (data.hasOwnProperty('draw')) {
107 if (data.hasOwnProperty('order')) {
110 if (data.hasOwnProperty('search')) {
113 // If we're handling our own pagination, set the properties
114 // that DT will send in the request
116 start = forward ? start + pageSize : start - pageSize;
118 data['length'] = pageSize;
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;
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
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;
145 // Copy the property value
146 tableDef[key] = conf[key];
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
156 tableDef.drawCallback = function() {
157 $('.paginate_button.next:not(.disabled)',
158 this.api().table().container()
159 ).on('hover', function() {
163 $('.paginate_button.previous:not(.disabled)',
164 this.api().table().container()
165 ).on('hover', function() {
170 // Initialise the table
171 $('#'+service.id ).dataTable(
172 $.extend(true, {}, dataTablesDefaults, tableDef)