armstrong-react
Version:
Rocketmakers Armstrong library of React components
63 lines (62 loc) • 2.63 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
var React = require("react");
var classBuilder_1 = require("./../../utilities/classBuilder");
var Image = (function (_super) {
__extends(Image, _super);
function Image() {
_super.call(this);
this.state = { source: "" };
}
Image.prototype.getRandomUser = function () {
var _this = this;
var url = "http://api.randomuser.me/?exc=login,name,location,email,registered,dob,phone,cell,id,nat" + (this.props.sampleUserSeed ? "&seed=" + this.props.sampleUserSeed : '');
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var response = JSON.parse(xmlHttp.responseText);
var pictureUrl = response.results[0].picture.large;
_this.setState({ source: pictureUrl });
}
};
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
};
Image.prototype.componentDidMount = function () {
var height = this.props.height;
var width = this.props.width;
if (!this.props.height && this.props.width) {
height = this.props.width;
}
if (!this.props.width && this.props.height) {
width = this.props.height;
}
var source;
if (this.props.source) {
this.setState({ source: this.props.source });
}
else if (this.props.sampleUser) {
this.getRandomUser();
}
else if (!this.props.noPlaceholder && !this.props.sampleUser) {
this.setState({ source: "http://dummyimage.com/" + height + "x" + width + "/4f5c69/ffffff.png" });
}
};
Image.prototype.render = function () {
return (React.createElement("img", __assign({src: this.state.source}, this.props, {height: this.props.height, width: this.props.width, className: classBuilder_1.classNames(this.props.className, classBuilder_1.cd("rounded", this.props.rounded))})));
};
return Image;
}(React.Component));
exports.Image = Image;