1 $(document).ready(function() {
3 var getLinks = function(row) {
4 if (!row.links || row.links.length === 0) {
7 return row.links.map(function(link) {
8 return '<a href="' + link.url + '" target="_blank">' +
14 window.doSearch = function() {
15 // In case the source doesn't supply data required for DT to calculate
16 // pagination, we need to do it ourselves
17 var ownPagination = false;
18 var directionSet = false;
20 var forward = true; // true == forward, false == backwards
21 // Arbitrary starting value, it will be corrected by the first
27 cache: true, // Prevent DT appending a "_" cache param
30 // defaultContent prevents DT from choking if
31 // the API response doesn't return a column
60 // render functions don't get copied across when we make a dereferenced
61 // copy of them, so we have to reattach them once we have a copy
64 title: function(data, type, row) {
65 var links = getLinks(row);
67 return row.title + ' - ' + links.join(', ');
69 return '<a href="' + row.url + '" target="_blank">' +
76 source: function(data, type, row) {
78 '<a href="'+row.opac_url+'" target="_blank">'+row.source+'</a>' :
83 services.forEach(function(service) {
84 // Create a deferenced copy of our table definition object
85 var tableDef = JSON.parse(JSON.stringify(tableTmpl));
86 // Iterate the table's columns array and add render functions
88 tableDef.columns.forEach(function(column) {
89 if (renders[column.data]) {
90 column.render = renders[column.data];
93 tableDef.ajax.dataSrc = function(data) {
94 var results = data.results.search_results;
95 // The source appears to be returning it's own pagination
98 data.hasOwnProperty('recordsFiltered') ||
99 data.hasOwnProperty('recordsTotal')
103 // Set up our own pagination values based on what we just
105 ownPagination = true;
106 directionSet = false;
107 pageSize = results.length;
108 // These values are completely arbitrary, but they enable
109 // us to display pagination links
110 data.recordsFiltered = 5000,
111 data.recordsTotal = 5000;
115 tableDef.ajax.data = function(data) {
116 // Datatables sends a bunch of superfluous params
117 // that we don't want to litter our API schema
118 // with, so just remove them from the request
119 if (data.hasOwnProperty('columns')) {
122 if (data.hasOwnProperty('draw')) {
125 if (data.hasOwnProperty('order')) {
128 if (data.hasOwnProperty('search')) {
131 // If we're handling our own pagination, set the properties
132 // that DT will send in the request
134 start = forward ? start + pageSize : start - pageSize;
136 data['length'] = pageSize;
138 // We may need to restrict the service IDs being queries, this
139 // needs to be handled in the plugin's API module
140 var restrict = $('#service_id_restrict').
141 attr('data-service_id_restrict_ids');
142 if (restrict && restrict.length > 0) {
143 data.restrict = restrict;
146 // Add any datatables config options passed from the service
147 // to the table definition
148 tableDef.ajax.url = service.endpoint + metadata;
149 if (service.hasOwnProperty('datatablesConfig')) {
150 var conf = service.datatablesConfig;
151 for (var key in conf) {
152 // The config from the service definition comes from a Perl
153 // hashref, therefore can't contain true/false, so we
155 if (conf.hasOwnProperty(key)) {
156 if (conf[key] == 'false') {
157 // Special case false values
158 tableDef[key] = false;
159 } else if (conf[key] == 'true') {
160 // Special case true values
161 tableDef[key] = true;
163 // Copy the property value
164 tableDef[key] = conf[key];
169 // Create event watchers for the "next" and "previous" pagination
170 // links, this enables us to set the direction the next request is
171 // going in when we're doing our own pagination. We use "hover"
172 // because the click event is caught after the request has been
174 tableDef.drawCallback = function() {
175 $('.paginate_button.next:not(.disabled)',
176 this.api().table().container()
177 ).on('hover', function() {
181 $('.paginate_button.previous:not(.disabled)',
182 this.api().table().container()
183 ).on('hover', function() {
188 // Initialise the table
189 // Since we're not able to use the columns settings in core,
190 // we need to mock the object that it would return
191 var table_settings = {
194 cannot_be_modified : 0,
195 cannot_be_toggled : 0,
196 columnname : 'source',
200 cannot_be_modified : 0,
201 cannot_be_toggled : 0,
202 columnname : 'title',
206 cannot_be_modified : 0,
207 cannot_be_toggled : 0,
208 columnname : 'author',
212 cannot_be_modified : 0,
213 cannot_be_toggled : 0,
218 cannot_be_modified : 0,
219 cannot_be_toggled : 0,
224 cannot_be_modified : 0,
225 cannot_be_toggled : 0,
231 // Hide pagination buttons if appropriate
232 tableDef.drawCallback = function() {
233 var pagination = $(this).closest('.dataTables_wrapper')
234 .find('.dataTables_paginate');
235 pagination.toggle(this.api().page.info().pages > 1);
237 KohaTable(service.id, tableDef, table_settings);