Advertisement
Guest User

my amazonjs.js?v0.5

a guest
Apr 14th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function ($) {
  2.     if (!$) return;
  3.     var isIE6 = ($.browser.msie && $.browser.version == '6.0');
  4.     var resources = {};
  5.     var resource = resources['en'] = {
  6.         BookAuthor:'Author',
  7.         BookPublicationDate:'PublicationDate',
  8.         BookPublisher:'Publisher',
  9.         NumberOfPagesValue:'${NumberOfPages} pages',
  10.         ListPrice:'List Price',
  11.         Price:'Price',
  12.         PriceUsage:'Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [amazon.com or endless.com, as applicable] at the time of purchase will apply to the purchase of this product.',
  13.         PublicationDate:'Publication Date',
  14.         ReleaseDate:'Release Date',
  15.         SalesRank:'SalesRank',
  16.         SalesRankValue:'#${SalesRank}',
  17.         RunningTime:'Run Time',
  18.         RunningTimeValue:'${RunningTime} minutes',
  19.         CustomerReviewTitle:'${Title} Customer Review',
  20.         SeeCustomerReviews:'See Customer Reviews',
  21.         PriceUpdatedat:'(at ${UpdatedDate})'
  22.     };
  23.     $.extend({
  24.         amazonjs:{
  25.             isCustomerReviewEnabled:false,
  26.             resource:resource,
  27.             resources:resources,
  28.             setLocale:function (locale) {
  29.                 var r = this.resources[locale];
  30.                 if (r) this.resource = r;
  31.             },
  32.             initTemplate:function () {
  33.                 var r = this.resource;
  34.                 var smallImageTemplate =
  35.                     [
  36.                         '{{if SmallImage}}',
  37.                         '<div class="amazonjs_image">',
  38.                         '<a href="${DetailPageURL}" title="${Title}" target="_blank">',
  39.                         '<img src="${SmallImage.src}" width="${SmallImage.width}" height="${SmallImage.height}" style="max-width:${SmallImage.width}px" alt="${Title}"/>',
  40.                         '</a>',
  41.                         '</div>',
  42.                         '{{/if}}'
  43.                     ].join('');
  44.                 var mediumImageTemplate =
  45.                     [
  46.                         '{{if MediumImage}}',
  47.                         '<div class="amazonjs_image">',
  48.                         '<a href="${DetailPageURL}" title="${Title}" target="_blank">',
  49.                         '<img src="${MediumImage.src}" width="${MediumImage.width}" height="${MediumImage.height}" style="max-width:${MediumImage.width}px" alt="${Title}"/>',
  50.                         '</a>',
  51.                         '</div>',
  52.                         '{{/if}}'
  53.                     ].join('');
  54.                 var largeImageTemplate =
  55.                     [
  56.                         '{{if LargeImage}}',
  57.                         '<div class="amazonjs_image">',
  58.                         '<a href="${DetailPageURL}" title="${Title}" target="_blank">',
  59.                         '<img src="${LargeImage.src}" width="${LargeImage.width}" height="${LargeImage.height}" style="max-width:${LargeImage.width}px" alt="${Title}"/>',
  60.                         '</a>',
  61.                         '</div>',
  62.                         '{{/if}}'
  63.                     ].join('');
  64.                 var priceContentTemplate =
  65.                     [
  66.                         '{{if $item.isSale()}}',
  67.                         '<b>' + r.ListPrice + '</b><span class="amazonjs_listprice">${ListPrice.FormattedPrice}</span><br/>',
  68.                         '{{if OfferSummary.LowestNewPrice}}<b>' + r.Price + '</b>${OfferSummary.LowestNewPrice.FormattedPrice}{{/if}}',
  69.                         '<span>' + r.PriceUpdatedat + '</span>',
  70.                         '{{else}}',
  71.                         '<b>' + r.Price + '</b>${ListPrice.FormattedPrice}',
  72.                         '<span>' + r.PriceUpdatedat + '</span>',
  73.                         '{{/if}}'
  74.                     ].join('');
  75.                 var priceTemplate = '{{if ListPrice}}<div class="amazonjs_price" title="' + r.PriceUsage + '">' + priceContentTemplate + '</div>{{/if}}';
  76.                 var priceLiTemplate = '{{if ListPrice}}<li class="amazonjs_price" title="' + r.PriceUsage + '">' + priceContentTemplate + '</li>{{/if}}';
  77.                 var reviewLinkTemplate = '';
  78.                 if (this.isCustomerReviewEnabled) {
  79.                     reviewLinkTemplate = '<a href="${IFrameReviewURL}&TB_iframe=true&height=500&width=600" title="' + r.CustomerReviewTitle + '" target="_blank" class="amazonjs_review">' + r.SeeCustomerReviews + '</a>';
  80.                 }
  81.  
  82.                 this.partial = {
  83.                     smallImage:smallImageTemplate,
  84.                     mediumImage:mediumImageTemplate,
  85.                     largeImage:largeImageTemplate,
  86.                     price:priceTemplate
  87.                 };
  88.  
  89.                 var defaultTemplates = {
  90.                     Small:[
  91.                         '<div class="amazonjs_item">',
  92.                         smallImageTemplate,
  93.                         '<div class="amazonjs_info" style="{{if SmallImage}}margin-left:${SmallImage.width}px;{{/if}}">',
  94.                         '<h4><a href="${DetailPageURL}" title="${Title}" target="_blank">${Title}</a></h4>',
  95.                         '<ul>',
  96.                         '{{if Creator}}<li class="amazonjs_creator">${Creator}</li>{{/if}}',
  97.                         '{{if Manufacturer}}<li class="amazonjs_manufacturer">${Manufacturer}</li>{{/if}}',
  98.                         priceLiTemplate,
  99.                         '{{if PublicationDate}}<li class="amazonjs_PublicationDate"><b>' + r.PublicationDate + '</b>${PublicationDate}</li>{{/if}}',
  100.                         '{{if SalesRank}}<li class="amazonjs_SalesRank"><b>' + r.SalesRank + '</b>' + r.SalesRankValue + '</li>{{/if}}',
  101.                         '</ul>',
  102.                         '</div>',
  103.                         reviewLinkTemplate,
  104.                         '<div class="amazonjs_footer"></div>',
  105.                         '</div>'
  106.                     ],
  107.                     Music:[
  108.                         '<div class="amazonjs_item amazonjs_music">',
  109.                         mediumImageTemplate,
  110.                         '<div class="amazonjs_info" style="{{if MediumImage}}margin-left:${MediumImage.width}px;{{/if}}">',
  111.                         '<h4><a href="${DetailPageURL}" title="${Title}" target="_blank">${Title}</a></h4>',
  112.                         '<ul>',
  113.                         '{{if Artist}}<li>${Artist}</li>{{/if}}',
  114.                         '{{if Creator}}<li>${Creator}</li>{{/if}}',
  115.                         '{{if Label}}<li>${Label}</li>{{/if}}',
  116.                         priceLiTemplate,
  117.                         '{{if ReleaseDate}}<li><b>' + r.ReleaseDate + '</b>${ReleaseDate}</li>{{/if}}',
  118.                         '{{if SalesRank}}<li><b>' + r.SalesRank + '</b>' + r.SalesRankValue + '</li>{{/if}}',
  119.                         '{{if RunningTime}}<li><b>' + r.RunningTime + '</b>' + r.RunningTimeValue + '</li>{{/if}}',
  120.                         '</ul>',
  121.                         '</div>',
  122.                         reviewLinkTemplate,
  123.                         '<div class="amazonjs_footer"></div>',
  124.                         '</div>'
  125.                     ],
  126.                     Book:[
  127.                         '<div class="amazonjs_item amazonjs_book">',
  128.                         mediumImageTemplate,
  129.                         '<div class="amazonjs_info" style="{{if MediumImage}}margin-left:${MediumImage.width}px;{{/if}}">',
  130.                         '<h4><a href="${DetailPageURL}" title="${Title}" target="_blank">${Title}</a></h4>',
  131.                         '<ul>',
  132.                         '<li><b>' + r.BookAuthor + '</b>${Author}</li>',
  133.                         priceLiTemplate,
  134.                         '<li><b>' + r.BookPublicationDate + '</b>${PublicationDate}</li>',
  135.                         '{{if SalesRank}}<li><b>' + r.SalesRank + '</b>' + r.SalesRankValue + '</li>{{/if}}',
  136.                         '<li><b>${Binding}</b>' + r.NumberOfPagesValue + '</li>',
  137.                         '<li><b>ISBN-10</b>${ISBN}</li>',
  138.                         '<li><b>ISBN-13</b>${EAN}</li>',
  139.                         '<li><b>' + r.BookPublisher + '</b>${Publisher}</li>',
  140.                         '</ul>',
  141.                         '</div>',
  142.                         reviewLinkTemplate,
  143.                         '<div class="amazonjs_footer"></div>',
  144.                         '</div>'
  145.                     ],
  146.                     eBooks:[
  147.                         '<div class="amazonjs_item amazonjs_book">',
  148.                         mediumImageTemplate,
  149.                         '<div class="amazonjs_info" style="{{if MediumImage}}margin-left:${MediumImage.width}px;{{/if}}">',
  150.                         '<h4><a href="${DetailPageURL}" title="${Title}" target="_blank">${Title}</a></h4>',
  151.                         '<ul>',
  152.                         '<li><b>' + r.BookAuthor + '</b>${Author}</li>',
  153.                         priceLiTemplate,
  154.                         '<li><b>' + r.BookPublicationDate + '</b>${PublicationDate}</li>',
  155.                         '{{if SalesRank}}<li><b>' + r.SalesRank + '</b>' + r.SalesRankValue + '</li>{{/if}}',
  156.                         '<li><b>${Binding}</b>' + r.NumberOfPagesValue + '</li>',
  157.                         '<li><b>' + r.BookPublisher + '</b>${Publisher}</li>',
  158.                         '</ul>',
  159.                         '</div>',
  160.                         reviewLinkTemplate,
  161.                         '<div class="amazonjs_footer"></div>',
  162.                         '</div>'
  163.                     ]
  164.                 };
  165.                 this.template(defaultTemplates);
  166.                 var me = this;
  167.                 $.each(this.addTemplateCallbacks, function (i, callback) {
  168.                     var t = callback.call(me, me.partial);
  169.                     if (t) me.template(t);
  170.                 });
  171.             },
  172.             addTemplateCallbacks:[],
  173.             addTemplate:function (fn) {
  174.                 if (typeof(fn) == 'function') {
  175.                     this.addTemplateCallbacks.push(fn);
  176.                 }
  177.             },
  178.             template:function (templates) { // set up jQuery template
  179.                 $.each(templates, function (name, tmpl) {
  180.                     if (tmpl) {
  181.                         $.template('amazonjs' + name + 'Tpl', (typeof tmpl === 'string') ? tmpl : tmpl.join(''));
  182.                     }
  183.                 });
  184.             },
  185.             render:function (items) {
  186.                 var $items = [];
  187.                 this.initTemplate();
  188.                 $("a[rel='amazonjs']").each(function () {
  189.                     var classNames = $(this).attr('class').split(' '),
  190.                         c = classNames[0].split('_'),
  191.                         asin = c[1],
  192.                         countryCode = c[2],
  193.                         tmpl = (c.length > 3) ? c[3] : null,
  194.                         item = find(asin, countryCode);
  195.  
  196.                     if (item) {
  197.                         var $item = $.amazonjs.tmpl(item, $.amazonjs.formatTmplName(tmpl));
  198.                         $(this).replaceWith($item.hide());
  199.                         var $review = $item.find('.amazonjs_review');
  200.                         if (!isIE6) {
  201.                             $item.css('position', 'relative');
  202.                         } else {
  203.                             $review.css({
  204.                                 'float':'right',
  205.                                 'position':'static',
  206.                                 'marginRight':'32px'
  207.                             });
  208.                         }
  209.                         $review.click(function () {
  210.                             tb_show(this.title, this.href);
  211.                             this.blur();
  212.                             return false;
  213.                         });
  214.                         $items.push($item);
  215.                     } else {
  216.                         // add official link when amazon product is not fetched by using AWS API
  217.                         tmpl = 'Link' + countryCode.toUpperCase();
  218.                         item = {
  219.                             asins:asin,
  220.                             fc1:'000000',
  221.                             lc1:'0000FF',
  222.                             bc1:'000000',
  223.                             bg1:'FFFFFF',
  224.                             IS2:1,
  225.                             lt1:'_blank',
  226.                             f:'ifr',
  227.                             m:'amazon'
  228.                         };
  229.                         var $item = $.amazonjs.tmpl(item, $.amazonjs.formatTmplName(tmpl));
  230.                         $(this).replaceWith($item);
  231.                     }
  232.                 });
  233.                 fadeIn();
  234.                 function find(asin, countryCode) {
  235.                     for (var i = 0, length = items.length; i < length; i++) {
  236.                         if (items[i].ASIN == asin && items[i].CountryCode == countryCode) return items[i];
  237.                     }
  238.                 }
  239.  
  240.                 function fadeIn() {
  241.                     if ($items.length > 0) {
  242.                         var $item = $items.shift();
  243.                         $item.fadeIn();
  244.                         setTimeout(fadeIn, 100);
  245.                     }
  246.                 }
  247.             },
  248.             formatTmplName:function (key) {
  249.                 return (key) ? 'amazonjs' + key + 'Tpl' : null;
  250.             },
  251.             formatNumber:function (val) {
  252.                 val += '';
  253.                 var x = val.split('.'),
  254.                     x1 = x[0],
  255.                     x2 = x.length > 1 ? '.' + x[1] : '',
  256.                     rgx = /(\d+)(\d{3})/;
  257.                 while (rgx.test(x1)) {
  258.                     x1 = x1.replace(rgx, '$1' + ',' + '$2');
  259.                 }
  260.                 return x1 + x2;
  261.             },
  262.             formatDateTime:function (timestamp) {
  263.                 var dt = new Date(timestamp * 1000);
  264.                 var Y = dt.getFullYear(),
  265.                     m = dt.getMonth() + 1,
  266.                     d = dt.getDate(),
  267.                     H = dt.getHours(),
  268.                     i = dt.getMinutes();
  269.                 if (m < 10) m = '0' + m;
  270.                 if (d < 10) d = '0' + d;
  271.                 if (H < 10) H = '0' + H;
  272.                 if (i < 10) i = '0' + i;
  273.                 return Y + '/' + m + '/' + d + ' ' + H + ':' + i;
  274.             },
  275.             getTemplate:function (item) {
  276.                 var defaultTmpl = this.formatTmplName('Small');
  277.                 if (item && item.ProductGroup) {
  278.                     var tmpl = this.formatTmplName(item.ProductGroup);
  279.                     return $.template[tmpl] || defaultTmpl;
  280.                 }
  281.                 return defaultTmpl;
  282.             },
  283.             prepareData:function (item) {
  284.                 item.PublicationDate = item.PublicationDate || item.ReleaseDate;
  285.                 item.Manufacturer = item.Manufacturer || item.Label;
  286.                 if (item.SalesRank) item.SalesRank = this.formatNumber(item.SalesRank);
  287.                 if (item.PublicationDate) item.PublicationDate = item.PublicationDate.replace(/-/g, '/');
  288.                 if (item.ReleaseDate) item.ReleaseDate = item.ReleaseDate.replace(/-/g, '/');
  289.                 if (item.Artist && item.Creator) {
  290.                     var Creator = [];
  291.                     var a = $.isArray(item.Artist) ? item.Artist : [item.Artist];
  292.                     var c = $.isArray(item.Creator) ? item.Creator : [item.Creator];
  293.                     $.each(c, function (i, value) {
  294.                         if ($.inArray(value, a) == -1) Creator.push(value);
  295.                     });
  296.                     item.Creator = (Creator.length) ? Creator : null;
  297.                 }
  298.                 item.UpdatedDate = this.formatDateTime(item.UpdatedAt);
  299.                 return item;
  300.             },
  301.             tmpl:function (item, tmpl) {
  302.                 item = this.prepareData(item);
  303.                 // overwrite ListPrice
  304.                 if (!item.ListPrice && item.OfferSummary) {
  305.                     item.ListPrice = item.OfferSummary.LowestNewPrice;
  306.                 }
  307.                 return $.tmpl($.template[tmpl] || this.getTemplate(item), item, {isSale:function () {
  308.                     if (this.data) {
  309.                         var lp = this.data.ListPrice, la = Number(lp.Amount),
  310.                             os = this.data.OfferSummary || {}, sp = os.LowestNewPrice || {}, sa = Number(sp.Amount);
  311.                         return (!isNaN(sa) && sa < la);
  312.                     }
  313.                     return false;
  314.                 }});
  315.             }
  316.         }
  317.     });
  318.     $.amazonjs.resources['ja'] = {
  319.         BookAuthor:'著者',
  320.         BookPublicationDate: '出版日',
  321.         BookPublisher: '出版社',
  322.         NumberOfPagesValue:'${NumberOfPages}ページ',
  323.         ListPrice:'参考価格',
  324.         Price: '価格',
  325.         PriceUsage: '価格および発送可能時期は表示された日付/時刻の時点のものであり、変更される場合があります。商品の販売においては、購入の時点で [Amazon.co.jp または Javari.jp] に表示されている価格および発送可能時期の情報が適用されます。',
  326.         PublicationDate:'発売日',
  327.         ReleaseDate:'リリース日',
  328.         SalesRank:'商品ランキング',
  329.         SalesRankValue:'${SalesRank}位',
  330.         RunningTime:'時間',
  331.         RunningTimeValue:'${RunningTime}分',
  332.         CustomerReviewTitle: '${Title} カスタマーレビュー',
  333.         SeeCustomerReviews: 'カスタマーレビューを見る',
  334.         PriceUpdatedat:'(${UpdatedDate}時点)'
  335.     };
  336.     $.amazonjs.setLocale('ja');
  337. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement