Skip to content

Commit 0696d8f

Browse files
author
Duke
committedJan 3, 2016
add channel lazy image, closes #165
1 parent 61d3717 commit 0696d8f

File tree

7 files changed

+66
-42
lines changed

7 files changed

+66
-42
lines changed
 

‎app/components/channel-lazy-image.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import Ember from 'ember';
2+
3+
const { computed, isEmpty } = Ember;
4+
5+
const IMAGE_HOST = 'https://images$x$.uhura.io';
6+
const DEFAULT_IMAGE = '/assets/channel-placeholder.png';
7+
8+
export default Ember.Component.extend({
9+
tagName: 'img',
10+
classNames: ['channel-img'],
11+
attributeBindings: 'src',
12+
imageURL: false,
13+
14+
src: computed('imageURL', function() {
15+
return this.get('imageURL') || DEFAULT_IMAGE;
16+
}),
17+
18+
didInsertElement() {
19+
const that = this;
20+
let image = new Image();
21+
image.src = this.get('cachedImageSource');
22+
image.onload = function() {
23+
if (typeof this.naturalWidth !== 'undefined' && this.naturalWidth !== 0) {
24+
Ember.run(() => {
25+
if (!that.get('isDestroyed')) {
26+
that.set('imageURL', this.src);
27+
}
28+
});
29+
}
30+
};
31+
},
32+
33+
cachedImageSource: computed('channel.imageURL', 'channel.image_url', function() {
34+
const imageURL = this.get('channel.imageURL') || this.get('channel.image_url');
35+
36+
if (isEmpty(imageURL)) {
37+
return DEFAULT_IMAGE;
38+
}
39+
40+
const isCached = imageURL.indexOf('.uhura.io') !== -1;
41+
if (isCached) {
42+
return imageURL;
43+
}
44+
45+
const hostIndex = Math.floor(Math.random() * (2 - 1 + 1)) + 1;
46+
const imageHost = IMAGE_HOST.replace('$x$', hostIndex);
47+
return `${imageHost}/resolve?url=${imageURL}`;
48+
})
49+
});

‎app/mixins/models/channel.js

+1-30
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import DS from 'ember-data';
22
import Ember from 'ember';
33

44
let { computed } = Ember;
5-
6-
const IMAGE_HOST = 'https://images$x$.uhura.io';
7-
85
export default Ember.Mixin.create({
96
title: DS.attr('string'),
107
image_url: DS.attr('string'),
@@ -27,31 +24,5 @@ export default Ember.Mixin.create({
2724
shortDescription += '...';
2825
}
2926
return shortDescription;
30-
}),
31-
imageURL: computed('image_url', function() {
32-
const that = this;
33-
let image = new Image();
34-
image.src = this._realImageUrl();
35-
image.onload = function() {
36-
if (typeof this.naturalWidth !== 'undefined' && this.naturalWidth !== 0) {
37-
Ember.run(() => {
38-
if (!that.isDestroyed) {
39-
that.set('imageURL', this.src);
40-
}
41-
});
42-
}
43-
};
44-
45-
return '/assets/channel-placeholder.png';
46-
}),
47-
48-
_realImageUrl() {
49-
let url = this.get('image_url');
50-
if (url !== '' && url.indexOf('.uhura.io') === -1) {
51-
const imageHostIndex = Math.floor(Math.random() * (2 - 1 + 1)) + 1;
52-
const imageHost = IMAGE_HOST.replace('$x$', imageHostIndex);
53-
return `${imageHost}/resolve?url=${this.get('image_url')}`;
54-
}
55-
return url;
56-
}
27+
})
5728
});

‎app/routes/home.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,11 @@ export default Ember.Route.extend({
88

99
model() {
1010
return hash({
11-
channels: this.get('client').request('', 'top', 'channels').then(this.fixImageURL),
11+
channels: this.get('client').request('', 'top', 'channels').then((data) => data.channels),
1212
episodes: this.buildEpisodes()
1313
});
1414
},
1515

16-
fixImageURL(data) {
17-
const { channels } = data;
18-
return channels.map((channel) => {
19-
channel.imageURL = channel.image_url;
20-
return channel;
21-
});
22-
},
23-
2416
buildEpisodes() {
2517
return [
2618
{

‎app/templates/components/categories-list.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{{#each category.sixChannels as |channel|}}
99
<li class="category-channel mdl-cell mdl-cell--4-col">
1010
{{#link-to 'channel' channel.id}}
11-
<img src={{channel.imageURL}} />
11+
{{channel-lazy-image channel=channel}}
1212
{{/link-to}}
1313
</li>
1414
{{/each}}

‎app/templates/components/channel-card.hbs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<div id="channel-{{channel.id}}">
22
<div class="channel-img__contaniner mdl-shadow--2dp">
3-
<img class="channel-img" data-channel-image-id={{channel.id}} src={{channel.imageURL}} />
3+
{{channel-lazy-image channel=channel}}
44
</div>
55
<h4>{{channel.title}}</h4>
66
</div>
77

88
<div class="channel-card__info mdl-shadow--2dp" for="channel-{{channel.id}}">
9-
<img class="channel-img" data-channel-image-id={{channel.id}} src={{channel.imageURL}} />
9+
{{channel-lazy-image channel=channel}}
1010
<h5>{{channel.title}}</h5>
1111
{{#link-to 'channel' channel.id class="mdl-button mdl-js-button
1212
mdl-js-ripple-effect mdl-button--colored"}}Open{{/link-to}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{yield}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { moduleForComponent, test } from 'ember-qunit';
2+
import hbs from 'htmlbars-inline-precompile';
3+
4+
moduleForComponent('channel-lazy-image', 'Integration | Component | channel lazy image', {
5+
integration: true
6+
});
7+
8+
test('it renders', function(assert) {
9+
this.render(hbs`{{channel-lazy-image }}`);
10+
assert.equal(this.$('img').attr('src'), '/assets/channel-placeholder.png');
11+
});

0 commit comments

Comments
 (0)
Please sign in to comment.