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