Last active
January 22, 2018 21:19
-
-
Save tomoguisuru/04000e5c7ecd34563182d24513aad4be to your computer and use it in GitHub Desktop.
Image Loader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
const { | |
computed, | |
String: { htmlSafe }, | |
run: { scheduleOnce }, | |
RSVP: { Promise }, | |
} = Ember; | |
export default Ember.Component.extend({ | |
classNames: ['preload-img'], | |
maxHeight: 0, | |
maxWidth: 0, | |
backgroundImage: computed('isLoaded', 'src', function() { | |
if (!this.isLoaded) { return; } | |
return htmlSafe(`background-image: url('${this.src}')`); | |
}), | |
imgStyle: computed('maxHeight', 'maxWidth', function() { | |
if (this.maxHeight > 0 && this.maxHeight > this.maxWidth) { | |
return htmlSafe(`max-height: ${this.maxHeight}px; height:100%;`); | |
} | |
if (this.maxWidth > 0 && this.maxWidth > this.maxHeight) { | |
return htmlSafe(`max-width: ${this.maxWidth}px; width:100%;`); | |
} | |
}), | |
_getImage(url) { | |
return new Promise((resolve, reject) => { | |
if (!url) return reject("Missing URL!"); | |
if (typeof FastBoot !== 'undefined') return; | |
let img = new Image(); | |
img.src = url; | |
img.onload = () => { | |
resolve([url, img.height, img.width]); | |
}; | |
}); | |
}, | |
_setDimensions(imgHeight, imgWidth) { | |
const { maxHeight, maxWidth } = this.getProperties(['maxHeight', 'maxWidth']); | |
if (maxWidth) { | |
const scale = this._getScale(imgWidth, maxWidth); | |
imgHeight = (imgHeight * scale); | |
imgWidth = maxWidth; | |
} else { | |
const scale = this._getScale(imgWidth, maxWidth); | |
imgHeight = maxHeight; | |
imgWidth = (maxWidth * scale); | |
} | |
return this.setProperties({ | |
imgHeight, | |
imgWidth, | |
}); | |
}, | |
_getScale(value, maxValue) { | |
return maxValue / value; | |
}, | |
didInsertElement() { | |
this._super(...arguments); | |
scheduleOnce('afterRender', this, () => { | |
this._getImage(this.src).then((results) => { | |
const [url, height, width] = results; | |
this._setDimensions(height, width); | |
this.set('isLoaded', true); | |
}); | |
}); | |
}, | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle' | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.13.0", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.16.2", | |
"ember-template-compiler": "2.16.2", | |
"ember-testing": "2.16.2" | |
}, | |
"addons": { | |
"ember-data": "2.16.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment