-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage-grid-item.html
61 lines (52 loc) · 1.45 KB
/
image-grid-item.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<link rel="import" href="../polymer/polymer.html">
<polymer-element name="image-grid-item">
<template>
<link rel="stylesheet" href="image-grid-item.css" />
<div class="intrinsic-wrapper">
<content select="img"></content>
</div>
</template>
<script>
Polymer({
/**
* The callback to when the element’s initial set of
* children are guaranteed to exist.
* @method
*/
domReady: function () {
var img = this.querySelector('img');
if (this._isImageLoaded(img)) {
this._checkSize(img);
} else {
img.onload(this._checkSize.bind(this));
}
},
/**
* Adds a portrait or landscape class to an image.
*
* @method
* @private
* @param {Element} img The <img> to check.
*/
_checkSize: function (img) {
var width = img.naturalWidth;
var height = img.naturalHeight;
var isPortrait = height >= width;
img.classList.add(isPortrait ? 'portrait' : 'landscape');
},
/**
* Checks if an image is already loaded.
*
* @method
* @private
* @param {Element} img The <img> to check.
* @returns {Boolean} True if it's loaded.
*/
_isImageLoaded: function (img) {
var isComplete = img.complete;
var width = img.naturalWidth;
return (isComplete && width > 0);
}
});
</script>
</polymer-element>