Skip to content

Commit

Permalink
Merge pull request duckduckgo#1957 from duckduckgo/brian/ia-rerendering
Browse files Browse the repository at this point in the history
Replace Amazon/Movies DOM hacking
  • Loading branch information
andrey-p committed Jul 3, 2015
2 parents 36ba0b9 + 0a4431e commit 4e967c6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 73 deletions.
29 changes: 11 additions & 18 deletions share/spice/amazon/amazon.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
(function(env) {
"use strict";

env.ddg_spice_amazon = function(api_result) {

if (!api_result || !api_result.results || !api_result.results.length || api_result.results.length == 0) {
if (!api_result || !api_result.results || !api_result.results.length) {
return Spice.failed('products');
}

Expand All @@ -17,7 +16,10 @@
sourceName: 'Amazon',
sourceUrl: api_result.more_at,
sourceIcon: true,
next: api_result.next
next: api_result.next,
rerender: [
'reviewCount'
]
},
templates: {
group: 'products',
Expand All @@ -29,32 +31,23 @@
dup: ['ASIN','img_m','img']
},
onItemShown: function(item) {
if (item.loadedRatings) { return; }

var arg = item.rating,
url = '/m.js?r=';

if (item.loadedReviews) { return; }

arg = arg.replace(/(?:.com.au|.com.br|.cn|.fr|.de|.in|.it|.co.jp|.jp|.mx|.es|.co.uk|.com|.ca?)/i, '');
arg = arg.replace('http://www.amazon/reviews/iframe?', '');

$.getJSON(url + encodeURIComponent(arg), function(r) {
if (r.stars.match(/stars-(\d)-(\d)/)) {
item.rating = RegExp.$1 + "." + RegExp.$2;
}
item.reviewCount = r.reviews;

// this is kind of dirty, relies on the item.$html being the
// memory ref to the tile dom, and re-renders the stars/renders block of html
// now that we have updated ratings data:
if (item.$html) {
var $ratingsWrapper = item.$html.find('.tile__rating,.detail__rating');
if ($ratingsWrapper && $ratingsWrapper.length) {
$ratingsWrapper.html(Handlebars.helpers.starsAndReviews(item.rating, item.reviewCount, item.url_review, true));
}
item.set({ rating: RegExp.$1 + "." + RegExp.$2 });
}
console.log("update reviewCount: ", item.id);
item.set({ reviewCount: r.reviews });
});

item.loadedRatings = true;
item.loadedReviews = 1;
}
});
}
Expand Down
38 changes: 21 additions & 17 deletions share/spice/in_theaters/in_theaters.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@
sourceName: 'Rotten Tomatoes',
sourceUrl: 'http://www.rottentomatoes.com/movie/in-theaters/',
total: api_result.movies,
itemType: 'Movies'
itemType: 'Movies',
rerender: [
'image'
]
},
normalize: function(item) {
if (filter_rating && item.mpaa_rating.toLowerCase() !== filter_rating) {
Expand Down Expand Up @@ -106,23 +109,24 @@
}
},
onItemShown: function(item) {
var id = item.alternate_ids && item.alternate_ids.imdb;

if (!id) { return; }

$.ajaxSetup({ cache: true });

if(item && item.alternate_ids && item.alternate_ids.imdb) {
$.getJSON("/js/spice/movie_image/tt" + item.alternate_ids.imdb, function(data) {
if(data && data.movie_results && data.movie_results.length > 0 && data.movie_results[0].poster_path) {
var image = "https://image.tmdb.org/t/p/w185" + data.movie_results[0].poster_path,
$html = (item.$html) ? item.$html : DDG.duckbar.tabs[item.parentId].view.$el;

$html.find(".js-movie-img").attr("src", "https://images.duckduckgo.com/iu/?f=1&u=" + encodeURIComponent(image));
$.extend(item, {
image: image,
img: image,
img_m: image
});
}
});
}

$.getJSON("/js/spice/movie_image/tt" + id, function(data) {
var path = data && data.movie_results && data.movie_results.length && data.movie_results[0].poster_path,
image = path && "https://image.tmdb.org/t/p/w185" + path;

if (image) {
item.set({
img: image,
img_m: image,
image: image
});
}
});
}
});
}
Expand Down
32 changes: 11 additions & 21 deletions share/spice/isbn/isbn.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
"use strict";
env.ddg_spice_isbn = function (api_result) {

if (!api_result || api_result.error || !api_result.results ||
!api_result.results.length || api_result.results.length == 0) {
if (!api_result || api_result.error || !api_result.results || !api_result.results.length) {
return Spice.failed('isbn');
}

var items = api_result.results;

Spice.add({
id: 'isbn',
name: 'Books',
data: items[0],
data: api_result.results,
allowMultipleCalls: true,
meta: {
itemType: 'Products',
sourceName: 'Amazon',
sourceUrl: api_result.more_at,
sourceIcon: true,
next: api_result.next
next: api_result.next,
rerender: [
'reviewCount'
]
},
templates: {
group: 'products',
Expand All @@ -31,32 +31,22 @@
dup: ['ASIN','img_m','img']
},
onItemShown: function(item) {
if (item.loadedRatings) { return; }

var arg = item.rating,
url = '/m.js?r=';

if (item.loadedReviews) { return; }

arg = arg.replace(/(?:.com.au|.com.br|.cn|.fr|.de|.in|.it|.co.jp|.mx|.es|.co.uk|.com|.ca?)/i, '');
arg = arg.replace('http://www.amazon/reviews/iframe?', '');

$.getJSON(url + encodeURIComponent(arg), function(r) {
if (r.stars.match(/stars-(\d)-(\d)/)) {
item.rating = RegExp.$1 + "." + RegExp.$2;
}
item.reviewCount = r.reviews;

// this is kind of dirty, relies on the item.$html being the
// memory ref to the tile dom, and re-renders the stars/renders block of html
// now that we have updated ratings data:
if (item.$html) {
var $ratingsWrapper = item.$html.find('.tile__rating,.detail__rating');
if ($ratingsWrapper && $ratingsWrapper.length) {
$ratingsWrapper.html(Handlebars.helpers.starsAndReviews(item.rating, item.reviewCount, item.url_review, true));
}
item.set({ rating: RegExp.$1 + "." + RegExp.$2 });
}
item.set({ reviewCount: r.reviews });
});

item.loadedRatings = true;
item.loadedReviews = 1;
}
});
};
Expand Down
36 changes: 19 additions & 17 deletions share/spice/movie/movie.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@
meta: {
sourceName: 'Rotten Tomatoes',
sourceUrl: 'https://www.rottentomatoes.com/search/?search=' + query,
itemType: 'Movies'
itemType: 'Movies',
rerender: [
'image'
]
},
normalize: function(item) {
// Modify the image from _tmb.jpg to _det.jpg
Expand Down Expand Up @@ -115,23 +118,22 @@
}]
},
onItemShown: function(item) {
if (!item.alternate_ids || !item.alternate_ids.imdb) { return; }

$.ajaxSetup({ cache: true });

if(item && item.alternate_ids && item.alternate_ids.imdb) {
$.getJSON("/js/spice/movie_image/tt" + item.alternate_ids.imdb, function(data) {
if(data && data.movie_results && data.movie_results.length > 0 && data.movie_results[0].poster_path) {
var image = "https://image.tmdb.org/t/p/w185" + data.movie_results[0].poster_path,
$html = (item.$html) ? item.$html : DDG.duckbar.tabs[item.parentId].view.$el;

$html.find(".js-movie-img").attr("src", "https://images.duckduckgo.com/iu/?f=1&u=" + encodeURIComponent(image)).show();
$.extend(item, {
image: image,
img: image,
img_m: image
});
}
});
}

$.getJSON("/js/spice/movie_image/tt" + item.alternate_ids.imdb, function(data) {
var path = data && data.movie_results && data.movie_results.length && data.movie_results[0].poster_path,
image = path && "https://image.tmdb.org/t/p/w185" + path;

if (image) {
item.set({
img: image,
img_m: image,
image: image
});
}
});
}
});
};
Expand Down

0 comments on commit 4e967c6

Please sign in to comment.