Fix format type mismatch
[sitka/overdrive-evergreen-opac.git] / src / od_pages_opac.coffee
1 # Define custom jQuery extensions to rewrite content of existing pages
2 # None of the extensions directly use the API, but they depend on od_action which does.
3
4 define [
5         'jquery'
6         'lodash'
7         'od_api'
8         'jquery-ui'
9 ], (
10         $
11         _
12         od
13 ) ->
14
15         $.fn.extend
16
17                 # Append a list of formats from a metadata object to this container
18                 _show_formats: (x) ->
19                         return @ unless x
20
21                         $x =
22                                 if x?.length > 0
23                                         $('<ul>')
24                                                 .css 'padding-left', '20px'
25                                                 .append _.map x, (f) -> $('<li>').append f.name or od.labels f.id or f.id
26                                 else
27                                         $('<span>')
28                                                 .css 'color', 'red'
29                                                 .text 'No available formats'
30
31                         @append $x
32
33
34                 # Return an Overdrive product ID or null from a given DOM context.
35                 # The context can be represented as a jQuery object or as a selector string
36                 _productID: ->
37                         href = $('a[href*="downloads.bclibrary.ca"], a[href*="elm.lib.overdrive.com"]', @).attr('href')
38                         /ID=(.+)/.exec(href)?[1]
39
40                 # Modify search result row to show e-holdings (available formats and
41                 # copy status of an e-title) in a result_holdings_table
42                 _results: ->
43                         result = """
44                         <tr name="e_holdings" class="result_table_title_cell">
45                                 <td colspan="2">
46                                         <table class="result_holdings_table">
47                                                 <thead>
48                                                         <tr>
49                                                                 <th>Available Formats</th>
50                                                                 <th>Status</th>
51                                                         </tr>
52                                                 </thead>
53                                                 <tbody>
54                                                         <tr>
55                                                                 <td class="formats"></td>
56                                                                 <td class="status"></td>
57                                                         </tr>
58                                                 </tbody>
59                                         </table>
60                                 </td>
61                         </tr>
62                         """
63                         # For each result row that has an embedded Overdrive product ID
64                         ids = []
65                         for row in @ when id = $(row)._productID()
66
67                                 # Cache the ID so that we don't need to traverse the DOM again
68                                 ids.push id
69
70                                 # Adorn each row with a product ID
71                                 $(row).prop 'id', id
72                                 # Add an empty container of format and availability values
73                                 .find '.results_info_table > tbody'
74                                         .append result
75                                         .end()
76                                 # Set up progress bars
77                                 ._results_meta()
78                                 ._results_avail()
79
80                         return ids
81
82                 _results_meta: (meta) ->
83                         status = if arguments.length is 0 then value: false else 'destroy'
84                         @find('.result_holdings_table .formats')
85                                 .progressbar status
86                                 ._show_formats meta?.formats
87                                 .end()
88
89                 _results_avail: (avail) ->
90                         status = if arguments.length is 0 then value: false else 'destroy'
91                         @find('.result_holdings_table .status')
92                                 .progressbar status
93                                 .end()
94
95                         return @ unless avail
96
97                         $x =
98                                 if avail.available is undefined
99                                         $('<span>')
100                                                 .css 'color', 'red'
101                                                 .text 'No longer available'
102                                 else
103                                         tpl = _.template """
104                                         <span><%= n_avail %> of <%= n_owned %> available, <%= n_holds %> holds</span>
105                                         """
106                                         $ tpl
107                                                 n_avail: avail.copiesAvailable
108                                                 n_owned: avail.copiesOwned
109                                                 n_holds:  avail.numberOfHolds
110
111                         @find('.result_holdings_table .status')
112                                 .append $x
113                                 .end()
114
115                 _record: ->
116
117                         # Find the product ID of this record
118                         id = @_productID()
119                         return unless id
120
121                         $record = $ """
122                         <div id="copy_hold_counts"><div id="#{id}">
123                                 <span id="rdetail_copy_counts">
124                                         <h2>Available formats</h2>
125                                         <div class="formats"></div>
126                                 </span>
127                                 <span id="rdetail_hold_counts">
128                                         <h2>Status</h2>
129                                         <div class="status"></div>
130                                 </span>
131                         </div></div>
132                         """
133
134                         $record
135                                 ._record_meta()
136                                 ._record_avail()
137
138                         @after $record
139                         return id
140
141                 _record_meta: (meta) ->
142
143                         status = if arguments.length is 0 then value: false else 'destroy'
144                         @find '.formats'
145                                 .progressbar status
146                                 ._show_formats meta?.formats
147                                 .end()
148
149                 _record_avail: (avail) ->
150
151                         status = if arguments.length is 0 then value: false else 'destroy'
152                         @find '.status'
153                                 .progressbar status
154                                 .end()
155
156                         return @ unless avail
157
158                         $x =
159                                 if avail.available is undefined
160                                         $('<span>')
161                                                 .css 'color', 'red'
162                                                 .text 'No longer available'
163                                 else
164                                         tpl = _.template """
165                                         <span><%= n_avail %> of <%= n_owned %> available, <%= n_holds %> holds</span>
166                                         """
167                                         $ tpl
168                                                 n_avail: avail.copiesAvailable
169                                                 n_owned: avail.copiesOwned
170                                                 n_holds: avail.numberOfHolds
171
172                         @find '.status'
173                                 .append $x
174                                 .end()
175
176                 # Replace a place hold link with another link that is more relevant to
177                 # the availability of the title in a row context.
178                 _replace_place_hold_link: (avail, type_of_interest) ->
179                         return @ unless avail
180                         
181                         if avail.available is undefined
182                                 @find '.place_hold'
183                                         .remove()
184                                         .end()
185
186                         # Find the place hold link that we want to replace
187                         $a = @find '.place_hold > a'
188
189                         # Parse the existing link text string for an indication of the item
190                         # format
191                         item_format = (text = $a.text()).match(/E-book/) or text.match(/E-audiobook/) or 'E-item'
192                         # Parse the existing link title for an item title, or if absent,
193                         # default to item format
194                         item_title = $a.prop('title').match(/on (.+)/)?[1] or item_format
195
196                         # Calculate the new text, title, and href properties, depending on
197                         # whether the user has an interest on the item and whether the item
198                         # is available or not
199                         [text, title, href] = switch type_of_interest
200
201                                 # If the user has already placed a hold on the item,
202                                 # we modify the link to go to the holds list
203                                 when 'hold'
204                                         [
205                                                 'Go to<br>E-items On Hold'
206                                                 'Go to E-items On Hold'
207                                                 '/eg/opac/myopac/holds?e_items'
208                                         ]
209
210                                 # If the user has already checked out the item,
211                                 # we modify the link to go to the checkout list
212                                 when 'checkout'
213                                         [
214                                                 'Go to<br>E-items Checked Out'
215                                                 'Go to E-items Checked Out'
216                                                 '/eg/opac/myopac/circs?e_items'
217                                         ]
218
219                                 # If the user has no prior interest in the item, we modify the
220                                 # link to go to the place hold form with the relevant query
221                                 # parameters.
222                                 else
223                                         params = $.param
224                                                 e_items: 1
225                                                 interested: avail.id
226
227                                         # The new text depends on avail.available and on the format, eg,
228                                         # Place Hold on E-book
229                                         # Place Hold on E-audiobook
230                                         # Check Out E-book
231                                         # Check Out E-audiobook
232                                         verb = if avail.available then 'Check Out' else 'Place Hold on'
233                                         url = if avail.available then '/eg/opac/myopac/circs' else '/eg/opac/myopac/holds'
234                                         [
235                                                 "#{verb}<br>#{item_format}"
236                                                 "#{verb} #{item_title}"
237                                                 "#{url}?#{params}"
238                                         ]
239
240                         # Replacing the link means we have to do three things.
241                         $a
242                         # Change the title and href properties of the link
243                         .prop
244                                 title: title
245                                 href: href
246                         # Change the alt property of the link's image, which we equate to
247                         # the link's title property
248                         .find 'img'
249                                 .prop 'alt', title
250                                 .end()
251                         # Change the contents of the text label, which resides in a
252                         # container with two possible class names, depending on whether we
253                         # are on the results page or the record page
254                         .find '.result_place_hold, .place_hold'
255                                 .replaceWith text
256                                 .end()
257
258                         return @