Fix format type mismatch
authorSteven Chan <steven3416@gmail.com>
Mon, 8 Sep 2014 00:24:24 +0000 (17:24 -0700)
committerSteven Chan <steven3416@gmail.com>
Mon, 8 Sep 2014 19:45:43 +0000 (12:45 -0700)
The format type that is listed on search results, record details, and
place hold form doesn't match what is listed in the selection option
list of the checkout dialogue.

To remove the mismatch, we need to use the format types found in the
checkout action object, which is gotten from Availability, rather than
the basic list found from Metadata.  The difference is presumably due to
Advantage accounts.

For search results or record details, we omit the call for Metadata and
substitute the format types from checkout action. For place hold form,
we cannot omit a call to Metadata, because it supplies title details as
well as format types. Therefore, we have to reschedule the asynchronous
calls to Metadata and Availability to run in parallel, wait for both
results, and then check to see if there is a checkout action in
Availability before being able to do the right thing.

Signed-off-by: Steven Chan <steven3416@gmail.com>
src/od_data.coffee
src/od_pages_myopac.coffee
src/od_pages_opac.coffee
src/overdrive.coffee

index 4b37d61..55a820a 100644 (file)
@@ -70,6 +70,7 @@ define [
                        @zero()
                        @hold email_address if @actions?.hold
                        @proxies @actions if @actions?
+                       @action_formats()
 
                        return @
 
@@ -89,6 +90,15 @@ define [
                                _.where(@actions.hold.fields, name: 'emailAddress')[0].value = email_address
                        return @
 
+               # Surface the format options list that might be buried in an actions object
+               action_formats: ->
+                       xs = @actions?.checkout?.fields
+                       return @ unless xs?.length > 0
+                       break for x in xs when x.name is 'formatType'
+                       return @ unless x.options.length > 0
+                       @formats = ( { id: v, name: '' } for v in x.options )
+                       return @
+
 
        class Holds extends U
                constructor: (x) ->
index 8e80111..770ee22 100644 (file)
@@ -280,7 +280,7 @@ define [
                _row_meta: (meta, classnames...) ->
 
                        status = if arguments.length is 0 then value: false else 'destroy'
-                       @find(".#{n}").progressbar(status) for n in ['title', 'author', 'formats']
+                       try @find(".#{n}").progressbar(status) for n in ['title', 'author', 'formats']
 
                        return @ unless meta
 
@@ -299,7 +299,7 @@ define [
                                        when 'thumbnail' then $n.empty().append $thumbnail
                                        when 'title'     then $n.empty().append $title
                                        when 'author'    then $n.empty().append $author
-                                       when 'formats'   then $n._show_formats meta
+                                       when 'formats'   then $n._show_formats meta?.formats
                        return @
 
                _holds_row_avail1: (hold) ->
index 9ced00d..e64dee3 100644 (file)
@@ -4,8 +4,13 @@
 define [
        'jquery'
        'lodash'
+       'od_api'
        'jquery-ui'
-], ($, _) ->
+], (
+       $
+       _
+       od
+) ->
 
        $.fn.extend
 
@@ -14,10 +19,10 @@ define [
                        return @ unless x
 
                        $x =
-                               if x.formats
+                               if x?.length > 0
                                        $('<ul>')
                                                .css 'padding-left', '20px'
-                                               .append _.map x.formats, (f) -> $('<li>').append f.name
+                                               .append _.map x, (f) -> $('<li>').append f.name or od.labels f.id or f.id
                                else
                                        $('<span>')
                                                .css 'color', 'red'
@@ -78,7 +83,7 @@ define [
                        status = if arguments.length is 0 then value: false else 'destroy'
                        @find('.result_holdings_table .formats')
                                .progressbar status
-                               ._show_formats meta
+                               ._show_formats meta?.formats
                                .end()
 
                _results_avail: (avail) ->
@@ -138,7 +143,7 @@ define [
                        status = if arguments.length is 0 then value: false else 'destroy'
                        @find '.formats'
                                .progressbar status
-                               ._show_formats meta
+                               ._show_formats meta?.formats
                                .end()
 
                _record_avail: (avail) ->
index b351611..4b74e41 100644 (file)
@@ -192,8 +192,10 @@ require [
 
                                        # Initiate request for each Overdrive product ID
                                        for id in ids
-                                               od.apiMetadata id: id
                                                od.apiAvailability id: id
+                                               # If the user isn't logged in, the format list will
+                                               # always be found from Metadata
+                                               od.apiMetadata id: id unless logged_in
 
                                        # Cache the relationship between product IDs and patron
                                        # holds and checkouts, ie, has the patron placed a hold on
@@ -209,6 +211,14 @@ require [
                                                ._results_avail x
                                                ._replace_place_hold_link x, interested[x.id]?.type
 
+                                       # Irregular logic warning: If the user is logged in, the
+                                       # format list might be found from Availability if the item
+                                       # is available for checkout, otherwise it will be found
+                                       # from Metadata.
+                                       if logged_in
+                                               if x.available then $("##{x.id}")._results_meta x else od.apiMetadata id: x.id
+
+
                'opac\/record': (interested = {}) ->
 
                        # Add an empty container of format and availability values
@@ -220,7 +230,7 @@ require [
                                'od.interests': (ev, x) ->
 
                                        # Initiate request for metadata and availability values when
-                                       od.apiMetadata id: id
+                                       od.apiMetadata id: id unless logged_in
                                        od.apiAvailability id: id
 
                                        # Has the user placed a hold on an ID or checked out an ID?
@@ -234,6 +244,9 @@ require [
                                        $("##{x.id}")._record_avail x
                                        $('#rdetail_actions_div')._replace_place_hold_link x, interested[x.id]?.type
 
+                                       if logged_in
+                                               if x.available then $("##{x.id}")._record_meta x else od.apiMetadata id: x.id
+
                # For the case where the patron is trying to place a hold if not logged
                # in, there is a loophole in the Availability API; if using a patron
                # access token and patron already has a hold on it, avail.actions.hold
@@ -273,34 +286,37 @@ require [
                        od.$.on
 
                                'od.interests': (ev, x) ->
-                                       od.apiMetadata id: id
-                                       od.apiAvailability id: id
+
                                        # Has the user placed a hold on an ID or checked out an ID?
                                        interested = x.byID
 
-                               'od.metadata': (ev, x) ->
-                                       # Fill in metadata columns
-                                       $("##{x.id}")._row_meta x, 'thumbnail', 'title', 'author', 'formats'
-
-                               'od.availability': (ev, x) ->
-                                       # Check if this patron has checked out or placed a hold on
-                                       # avail.id and if so, then go back two pages to the result list
-                                       # or record page. The page being skipped over is the login page
-                                       # that comes up because the user needs to log in before being
-                                       # able to see the place hold page.  Thus, the logic is only
-                                       # relevant if the user has not logged in before trying to place
-                                       # a hold.
-                                       if interested[x.id]?.type
-                                               window.history.go -2
-
-                                       else
-                                               $("##{x.id}")
-                                               # Fill in availability column
-                                               ._holdings_row_avail x
-                                               # Auto-focus on place hold or checkout button
-                                               .find '.opac-button.hold, .opac-button.checkout'
-                                                       .focus()
-                                                       .end()
+                                       $.when(
+                                               od.apiMetadata id: id
+                                               od.apiAvailability id: id
+                                       )
+                                       .then (x, y) ->
+
+                                               # Check if this patron has checked out or placed a hold on
+                                               # avail.id and if so, then go back two pages to the result list
+                                               # or record page. The page being skipped over is the login page
+                                               # that comes up because the user needs to log in before being
+                                               # able to see the place hold page.  Thus, the logic is only
+                                               # relevant if the user has not logged in before trying to place
+                                               # a hold.
+                                               if interested[y.id]?.type
+                                                       window.history.go -2
+
+                                               else
+                                                       $("##{x.id}")._row_meta(x, 'thumbnail', 'title', 'author')
+                                                       $("##{x.id}")._row_meta (if y.available then y else x), 'formats'
+
+                                                       $("##{y.id}")
+                                                       # Fill in availability column
+                                                       ._holdings_row_avail y
+                                                       # Auto-focus on place hold or checkout button
+                                                       .find '.opac-button.hold, .opac-button.checkout'
+                                                               .focus()
+                                                               .end()
 
                'myopac\/holds': ->