ebd47cb2cc1fc668fb10002ef35c136ee816aca5
[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         'od_config'
9         'jquery-ui'
10 ], (
11         $
12         _
13         od
14         config
15 ) ->
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 =
24                                 if x?.length > 0
25                                         $('<ul>')
26                                                 .css 'padding-left', '20px'
27                                                 .append _.map x, (f) -> $('<li>').append f.name or od.labels f.id or f.id
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: ->
39                         hrefs = []
40                         hrefs.push('a[href*="' + productbaseURL + '"]') for productbaseURL in config.productbaseURLs
41                         href = $(hrefs.join(','), @).attr('href')
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                                 # skip this row if it is a grouped record (metarecord)
72                                 unless $(row).find("abbr.unapi-id[title*='metabib-metarecord']").length > 0
73
74                                         # Cache the ID so that we don't need to traverse the DOM again
75                                         ids.push id
76
77                                         # Adorn each row with a product ID
78                                         $(row).prop 'id', id
79                                         # Add an empty container of format and availability values
80                                         .find '.results_info_table > tbody'
81                                                 .append result
82                                                 .end()
83                                         # Set up progress bars
84                                         ._results_meta()
85                                         ._results_avail()
86
87                         return ids
88
89                 _results_meta: (meta) ->
90                         status = if arguments.length is 0 then value: false else 'destroy'
91                         @find('.result_holdings_table .formats')
92                                 .progressbar status
93                                 ._show_formats meta?.formats
94                                 .end()
95
96                 _results_avail: (avail) ->
97                         status = if arguments.length is 0 then value: false else 'destroy'
98                         @find('.result_holdings_table .status')
99                                 .progressbar status
100                                 .end()
101
102                         return @ unless avail
103
104                         tpl = _.template """
105                         <span><%= n_avail %> of <%= n_owned %> available, <%= n_holds %> holds</span>
106                         """
107                         $x = $ tpl
108                                 n_avail: avail.copiesAvailable
109                                 n_owned: avail.copiesOwned
110                                 n_holds:  avail.numberOfHolds
111
112                         @find('.result_holdings_table .status')
113                                 .append $x
114                                 .end()
115
116                 _record: ->
117
118                         # Find the product ID of this record
119                         id = @_productID()
120                         return unless id
121
122                         $record = $ """
123                         <div id="copy_hold_counts"><div id="#{id}">
124                                 <span id="rdetail_copy_counts">
125                                         <h2>Available formats</h2>
126                                         <div class="formats"></div>
127                                 </span>
128                                 <span id="rdetail_hold_counts">
129                                         <h2>Status</h2>
130                                         <div class="status"></div>
131                                 </span>
132                         </div></div>
133                         """
134
135                         $record
136                                 ._record_meta()
137                                 ._record_avail()
138
139                         @after $record
140                         return id
141
142                 _record_meta: (meta) ->
143
144                         status = if arguments.length is 0 then value: false else 'destroy'
145                         @find '.formats'
146                                 .progressbar status
147                                 ._show_formats meta?.formats
148                                 .end()
149
150                 _record_avail: (avail) ->
151
152                         status = if arguments.length is 0 then value: false else 'destroy'
153                         @find '.status'
154                                 .progressbar status
155                                 .end()
156
157                         return @ unless avail
158
159                         tpl = _.template """
160                         <span><%= n_avail %> of <%= n_owned %> available, <%= n_holds %> holds</span>
161                         """
162                         $x = $ tpl
163                                 n_avail: avail.copiesAvailable
164                                 n_owned: avail.copiesOwned
165                                 n_holds:  avail.numberOfHolds
166
167                         @find '.status'
168                                 .append $x
169                                 .end()
170
171                 # Replace a place hold link with another link that is more relevant to
172                 # the availability of the title in a row context.
173                 _replace_place_hold_link: (avail, type_of_interest) ->
174                         return @ unless avail
175                         
176                         # Find the place hold link that we want to replace
177                         $a = @find '.place_hold > a'
178
179                         # Parse the existing link text string for an indication of the item
180                         # format
181                         item_format = (text = $a.text()).match(/E-book/) or text.match(/E-audiobook/) or 'E-item'
182                         # Parse the existing link title for an item title, or if absent,
183                         # default to item format
184                         item_title = $a.prop('title').match(/on (.+)/)?[1] or item_format
185
186                         # Calculate the new text, title, and href properties, depending on
187                         # whether the user has an interest on the item and whether the item
188                         # is available or not
189                         [text, title, href] = switch type_of_interest
190
191                                 # If the user has already placed a hold on the item,
192                                 # we modify the link to go to the holds list
193                                 when 'hold'
194                                         [
195                                                 'Go to<br>E-items On Hold'
196                                                 'Go to E-items On Hold'
197                                                 '/eg/opac/myopac/holds?e_items'
198                                         ]
199
200                                 # If the user has already checked out the item,
201                                 # we modify the link to go to the checkout list
202                                 when 'checkout'
203                                         [
204                                                 'Go to<br>E-items Checked Out'
205                                                 'Go to E-items Checked Out'
206                                                 '/eg/opac/myopac/circs?e_items'
207                                         ]
208
209                                 # If the user has no prior interest in the item, we modify the
210                                 # link to go to the place hold form with the relevant query
211                                 # parameters.
212                                 else
213                                         params = $.param
214                                                 e_items: 1
215                                                 interested: avail.id
216
217                                         # The new text depends on avail.available and on the format, eg,
218                                         # Place Hold on E-book
219                                         # Place Hold on E-audiobook
220                                         # Check Out E-book
221                                         # Check Out E-audiobook
222                                         verb = if avail.available then 'Check Out' else 'Place Hold on'
223                                         url = if avail.available then '/eg/opac/myopac/circs' else '/eg/opac/myopac/holds'
224                                         [
225                                                 "#{verb}<br>#{item_format}"
226                                                 "#{verb} #{item_title}"
227                                                 "#{url}?#{params}"
228                                         ]
229
230                         # Replacing the link means we have to do three things.
231                         $a
232                         # Change the title and href properties of the link
233                         .prop
234                                 title: title
235                                 href: href
236                         # Change the alt property of the link's image, which we equate to
237                         # the link's title property
238                         .find 'img'
239                                 .prop 'alt', title
240                                 .end()
241                         # Change the contents of the text label, which resides in a
242                         # container with two possible class names, depending on whether we
243                         # are on the results page or the record page
244                         .find '.result_place_hold, .place_hold'
245                                 .replaceWith text
246                                 .end()
247
248                         return @