Bug 29407: Make the pickup locations dropdown JS reusable
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / ill-availability.js
1 $(document).ready(function() {
2
3     var getLinks = function(row) {
4         if (!row.links || row.links.length === 0) {
5             return false;
6         }
7         return row.links.map(function(link) {
8             return '<a href="' + link.url + '" target="_blank">' +
9                 link.text +
10                 '</a>';
11         });
12     };
13
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;
19         var start = 0;
20         var forward = true; // true == forward, false == backwards
21         // Arbitrary starting value, it will be corrected by the first
22         // page of results
23         var pageSize = 20;
24
25         var tableTmpl = {
26             ajax: {
27                 cache: true, // Prevent DT appending a "_" cache param
28             },
29             columns: [
30                 // defaultContent prevents DT from choking if
31                 // the API response doesn't return a column
32                 {
33                     title: 'Source',
34                     data: 'source',
35                     defaultContent: ''
36                 },
37                 {
38                     data: 'title',
39                     defaultContent: ''
40                 },
41                 {
42                     data: 'author',
43                     defaultContent: ''
44                 },
45                 {
46                     data: 'isbn',
47                     defaultContent: ''
48                 },
49                 {
50                     data: 'issn',
51                     defaultContent: ''
52                 },
53                 {
54                     data: 'date',
55                     defaultContent: ''
56                 }
57             ]
58         };
59
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
62         // Here we store them
63         var renders = {
64             title: function(data, type, row) {
65                 var links = getLinks(row);
66                 if (links) {
67                     return row.title + ' - ' + links.join(', ');
68                 } else if (row.url) {
69                     return '<a href="' + row.url  + '" target="_blank">' +
70                         row.title +
71                         '</a>';
72                 } else {
73                     return row.title;
74                 }
75             },
76             source: function(data, type, row) {
77                 return row.opac_url ?
78                     '<a href="'+row.opac_url+'" target="_blank">'+row.source+'</a>' :
79                     row.source;
80             }
81         };
82
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
87             // as necessary
88             tableDef.columns.forEach(function(column) {
89                 if (renders[column.data]) {
90                     column.render = renders[column.data];
91                 }
92             });
93             tableDef.ajax.dataSrc = function(data) {
94                 var results = data.results.search_results;
95                 // The source appears to be returning it's own pagination
96                 // data
97                 if (
98                     data.hasOwnProperty('recordsFiltered') ||
99                     data.hasOwnProperty('recordsTotal')
100                 ) {
101                     return results;
102                 }
103                 // Set up our own pagination values based on what we just
104                 // got back
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;
112
113                 return results;
114             };
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')) {
120                     delete data.columns;
121                 }
122                 if (data.hasOwnProperty('draw')) {
123                     delete data.draw;
124                 }
125                 if (data.hasOwnProperty('order')) {
126                     delete data.order;
127                 }
128                 if (data.hasOwnProperty('search')) {
129                     delete data.search;
130                 }
131                 // If we're handling our own pagination, set the properties
132                 // that DT will send in the request
133                 if (ownPagination) {
134                     start = forward ? start + pageSize : start - pageSize;
135                     data.start = start;
136                     data['length'] = pageSize;
137                 }
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;
144                 }
145             };
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
154                     // special case it
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;
162                         } else {
163                             // Copy the property value
164                             tableDef[key] = conf[key];
165                         }
166                     }
167                 }
168             }
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
173             // sent
174             tableDef.drawCallback = function() {
175                 $('.paginate_button.next:not(.disabled)',
176                     this.api().table().container()
177                 ).on('hover', function() {
178                     forward = true;
179                     directionSet = true;
180                 });
181                 $('.paginate_button.previous:not(.disabled)',
182                     this.api().table().container()
183                 ).on('hover', function() {
184                     forward = false;
185                     directionSet = true;
186                 });
187             }
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 columns_settings = [
192                                 {
193                                         cannot_be_modified: 0,
194                                         cannot_be_toggled: 0,
195                                         columnname: 'source',
196                                         is_hidden: 0
197                                 },
198                                 {
199                                         cannot_be_modified: 0,
200                                         cannot_be_toggled: 0,
201                                         columnname: 'title',
202                                         is_hidden: 0
203                                 },
204                                 {
205                                         cannot_be_modified: 0,
206                                         cannot_be_toggled: 0,
207                                         columnname: 'author',
208                                         is_hidden: 0
209                                 },
210                                 {
211                                         cannot_be_modified: 0,
212                                         cannot_be_toggled: 0,
213                                         columnname: 'isbn',
214                                         is_hidden: 0
215                                 },
216                                 {
217                                         cannot_be_modified: 0,
218                                         cannot_be_toggled: 0,
219                                         columnname: 'issn',
220                                         is_hidden: 0
221                                 },
222                                 {
223                                         cannot_be_modified: 0,
224                                         cannot_be_toggled: 0,
225                                         columnname: 'date',
226                                         is_hidden: 0
227                                 }
228             ];
229             // Hide pagination buttons if appropriate
230             tableDef.drawCallback = function() {
231                 var pagination = $(this).closest('.dataTables_wrapper')
232                     .find('.dataTables_paginate');
233                 pagination.toggle(this.api().page.info().pages > 1);
234             }
235             KohaTable(service.id, tableDef, columns_settings);
236         });
237     }
238
239
240 });