Use jQuery.noConflict to avoid conflict with other libraries
[sitka/overdrive-evergreen-opac.git] / src / od_pages_opac.coffee
index 9ced00d..9a45122 100644 (file)
@@ -2,10 +2,17 @@
 # None of the extensions directly use the API, but they depend on od_action which does.
 
 define [
-       'jquery'
+       'jquery-noconflict'
        'lodash'
+       'od_api'
+       'od_config'
        'jquery-ui'
-], ($, _) ->
+], (
+       $
+       _
+       od
+       config
+) ->
 
        $.fn.extend
 
@@ -14,10 +21,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'
@@ -29,7 +36,9 @@ define [
                # Return an Overdrive product ID or null from a given DOM context.
                # The context can be represented as a jQuery object or as a selector string
                _productID: ->
-                       href = $('a[href*="downloads.bclibrary.ca"], a[href*="elm.lib.overdrive.com"]', @).attr('href')
+                       hrefs = []
+                       hrefs.push('a[href*="' + productbaseURL + '"]') for productbaseURL in config.productbaseURLs
+                       href = $(hrefs.join(','), @).attr('href')
                        /ID=(.+)/.exec(href)?[1]
 
                # Modify search result row to show e-holdings (available formats and
@@ -59,18 +68,21 @@ define [
                        ids = []
                        for row in @ when id = $(row)._productID()
 
-                               # Cache the ID so that we don't need to traverse the DOM again
-                               ids.push id
+                               # skip this row if it is a grouped record (metarecord)
+                               unless $(row).find("abbr.unapi-id[title*='metabib-metarecord']").length > 0
 
-                               # Adorn each row with a product ID
-                               $(row).prop 'id', id
-                               # Add an empty container of format and availability values
-                               .find '.results_info_table > tbody'
-                                       .append result
-                                       .end()
-                               # Set up progress bars
-                               ._results_meta()
-                               ._results_avail()
+                                       # Cache the ID so that we don't need to traverse the DOM again
+                                       ids.push id
+
+                                       # Adorn each row with a product ID
+                                       $(row).prop 'id', id
+                                       # Add an empty container of format and availability values
+                                       .find '.results_info_table > tbody'
+                                               .append result
+                                               .end()
+                                       # Set up progress bars
+                                       ._results_meta()
+                                       ._results_avail()
 
                        return ids
 
@@ -78,7 +90,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) ->
@@ -89,19 +101,13 @@ define [
 
                        return @ unless avail
 
-                       $x =
-                               if avail.available is undefined
-                                       $('<span>')
-                                               .css 'color', 'red'
-                                               .text 'No longer available'
-                               else
-                                       tpl = _.template """
-                                       <span><%= n_avail %> of <%= n_owned %> available, <%= n_holds %> holds</span>
-                                       """
-                                       $ tpl
-                                               n_avail: avail.copiesAvailable
-                                               n_owned: avail.copiesOwned
-                                               n_holds:  avail.numberOfHolds
+                       tpl = _.template """
+                       <span><%= n_avail %> of <%= n_owned %> available, <%= n_holds %> holds</span>
+                       """
+                       $x = $ tpl
+                               n_avail: avail.copiesAvailable
+                               n_owned: avail.copiesOwned
+                               n_holds:  avail.numberOfHolds
 
                        @find('.result_holdings_table .status')
                                .append $x
@@ -138,7 +144,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) ->
@@ -150,19 +156,13 @@ define [
 
                        return @ unless avail
 
-                       $x =
-                               if avail.available is undefined
-                                       $('<span>')
-                                               .css 'color', 'red'
-                                               .text 'No longer available'
-                               else
-                                       tpl = _.template """
-                                       <span><%= n_avail %> of <%= n_owned %> available, <%= n_holds %> holds</span>
-                                       """
-                                       $ tpl
-                                               n_avail: avail.copiesAvailable
-                                               n_owned: avail.copiesOwned
-                                               n_holds: avail.numberOfHolds
+                       tpl = _.template """
+                       <span><%= n_avail %> of <%= n_owned %> available, <%= n_holds %> holds</span>
+                       """
+                       $x = $ tpl
+                               n_avail: avail.copiesAvailable
+                               n_owned: avail.copiesOwned
+                               n_holds:  avail.numberOfHolds
 
                        @find '.status'
                                .append $x
@@ -173,11 +173,6 @@ define [
                _replace_place_hold_link: (avail, type_of_interest) ->
                        return @ unless avail
                        
-                       if avail.available is undefined
-                               @find '.place_hold'
-                                       .remove()
-                                       .end()
-
                        # Find the place hold link that we want to replace
                        $a = @find '.place_hold > a'