This commit is contained in:
Jeffrey Morgan 2015-05-17 14:19:20 -07:00
parent 5c0dba6b07
commit 256c66e466
22 changed files with 764 additions and 14 deletions

BIN
images/connect-to-hub.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -83,7 +83,8 @@
"request": "^2.55.0",
"request-progress": "^0.3.1",
"rimraf": "^2.3.2",
"underscore": "^1.8.3"
"underscore": "^1.8.3",
"validator": "^3.39.0"
},
"devDependencies": {
"babel": "^5.1.10",

View File

@ -0,0 +1,20 @@
import alt from '../alt';
import hub from '../utils/HubUtil';
class AccountActions {
login(username, password) {
this.dispatch({});
hub.login(username, password);
}
signup (username, password, email, subscribe) {
this.dispatch({});
hub.signup(username, password, email, subscribe);
}
skip () {
this.dispatch({});
}
}
export default alt.createActions(AccountActions);

View File

@ -0,0 +1,19 @@
import alt from '../alt';
import router from '../router';
class AccountServerActions {
constructor () {
this.generateActions(
'loggedin',
'errors'
);
}
signedup ({username, verified}) {
router.get().transitionTo('search');
this.dispatch({username, verified});
}
}
export default alt.createActions(AccountServerActions);

View File

@ -0,0 +1,9 @@
import alt from '../alt';
class RepositoryActions {
fetch () {
this.dispatch({});
}
}
export default alt.createActions(RepositoryActions);

View File

@ -0,0 +1,12 @@
import alt from '../alt';
class RepositoryServerActions {
constructor () {
this.generateActions(
'fetched',
'error'
);
}
}
export default alt.createActions(RepositoryServerActions);

View File

@ -13,6 +13,11 @@ var urlUtil = require ('./utils/URLUtil');
var app = remote.require('app');
var request = require('request');
var docker = require('./utils/DockerUtil');
var hub = require('./utils/HubUtil');
var Router = require('react-router');
var routes = require('./routes');
var routerContainer = require('./router');
webUtil.addWindowSizeSaving();
webUtil.addLiveReload();
@ -27,12 +32,25 @@ setInterval(function () {
metrics.track('app heartbeat');
}, 14400000);
var router = Router.create({
routes: routes
});
router.run(Handler => React.render(<Handler/>, document.body));
routerContainer.set(router);
SetupStore.setup().then(() => {
Menu.setApplicationMenu(Menu.buildFromTemplate(template()));
docker.init();
router.transitionTo('search');
//if (!localStorage.getItem('account.prompted')) {
if (true) {
if (!hub.loggedin()) {
router.transitionTo('signup');
} else {
router.transitionTo('search');
}
} else {
router.transitionTo('search');
}
}).catch(err => {
metrics.track('Setup Failed', {
step: 'catch',
@ -63,3 +81,7 @@ ipc.on('application:open-url', opts => {
urlUtil.openUrl(opts.url, flags, app.getVersion());
});
});
module.exports = {
router: router
};

View File

@ -0,0 +1,57 @@
var React = require('react/addons');
var Router = require('react-router');
var RetinaImage = require('react-retina-image');
var Header = require('./Header.react');
var metrics = require('../utils/MetricsUtil');
var accountStore = require('../stores/AccountStore');
var accountActions = require('../actions/AccountActions');
module.exports = React.createClass({
mixins: [Router.Navigation],
getInitialState: function () {
return accountStore.getState();
},
handleSkip: function () {
accountActions.skip();
this.transitionTo('search');
metrics.track('Skipped Signup');
},
handleClose: function () {
this.transitionTo('search');
metrics.track('Closed Signup');
},
componentDidMount: function () {
accountStore.listen(this.update);
},
update: function () {
this.setState(accountStore.getState());
},
render: function () {
let close = this.state.prompted ?
<a className="btn btn-action btn-skip" onClick={this.handleClose}>Close</a> :
<a className="btn btn-action btn-skip" onClick={this.handleSkip}>Skip For Now</a>;
return (
<div className="setup">
<Header />
<div className="form-section">
<RetinaImage src={'connect-to-hub.png'} checkIfRetinaImgExists={false}/>
<Router.RouteHandler errors={this.state.errors} loading={this.state.loading} {...this.props}/>
</div>
<div className="desc">
<div className="content">
<h1>Connect to Docker Hub</h1>
<p>Pull and run private Docker Hub images by connecting your Docker Hub account to Kitematic.</p>
{close}
</div>
</div>
</div>
);
}
});

View File

@ -0,0 +1,81 @@
var _ = require('underscore');
var React = require('react/addons');
var Router = require('react-router');
var validator = require('validator');
var accountActions = require('../actions/AccountActions');
var metrics = require('../utils/MetricsUtil');
var shell = require('shell');
module.exports = React.createClass({
mixins: [Router.Navigation, React.addons.LinkedStateMixin],
getInitialState: function () {
return {
username: '',
password: '',
errors: {}
};
},
componentWillReceiveProps: function (nextProps) {
this.setState({errors: nextProps.errors});
},
validate: function () {
let errors = {};
if (!validator.isLowercase(this.state.username) || !validator.isAlphanumeric(this.state.username) || !validator.isLength(this.state.username, 4, 30)) {
errors.username = 'Must be 4-30 lower case letters or numbers';
}
if (!validator.isLength(this.state.password, 5)) {
errors.password = 'Must be at least 5 characters long';
}
return errors;
},
handleBlur: function () {
this.setState({errors: _.omit(this.validate(), (val, key) => !this.state[key].length)});
},
handleLogin: function () {
let errors = this.validate();
this.setState({errors});
if (_.isEmpty(errors)) {
accountActions.login(this.state.username, this.state.password);
}
metrics.track('Logged In to Docker Hub');
},
handleClickSignup: function () {
if (!this.props.loading) {
this.transitionTo('signup');
}
},
handleClickForgotPassword: function () {
shell.openExternal('https://hub.docker.com/account/forgot-password/');
},
render: function () {
console.log(this.state.errors);
let loading = this.props.loading ? <div className="spinner la-ball-clip-rotate la-dark"><div></div></div> : null;
return (
<form className="form-connect">
<input ref="usernameInput"maxLength="30" name="username" placeholder="username" type="text" disabled={this.props.loading} valueLink={this.linkState('username')} onBlur={this.handleBlur}/>
<p className="error-message">{this.state.errors.username}</p>
<input ref="passwordInput" name="password" placeholder="password" type="password" disabled={this.props.loading} valueLink={this.linkState('password')} onBlur={this.handleBlur}/>
<p className="error-message">{this.state.errors.password}</p>
<a className="link" onClick={this.handleClickForgotPassword}>Forgot your password?</a>
<p className="error-message">{this.state.errors.detail}</p>
<div className="submit">
{loading}
<button className="btn btn-action" disabled={this.props.loading} onClick={this.handleLogin} type="submit">Log In</button>
</div>
<br/>
<div className="extra">Don&#39;t have an account yet? <a disabled={this.state.loading} onClick={this.handleClickSignup}>Sign Up</a></div>
</form>
);
}
});

View File

@ -0,0 +1,86 @@
var _ = require('underscore');
var React = require('react/addons');
var Router = require('react-router');
var validator = require('validator');
var accountActions = require('../actions/AccountActions');
var metrics = require('../utils/MetricsUtil');
module.exports = React.createClass({
mixins: [Router.Navigation, React.addons.LinkedStateMixin],
getInitialState: function () {
return {
username: '',
password: '',
email: '',
subscribe: true,
errors: {}
};
},
componentWillReceiveProps: function (nextProps) {
this.setState({errors: nextProps.errors});
},
validate: function () {
let errors = {};
if (!validator.isLowercase(this.state.username) || !validator.isAlphanumeric(this.state.username) || !validator.isLength(this.state.username, 4, 30)) {
errors.username = 'Must be 4-30 lower case letters or numbers';
}
if (!validator.isLength(this.state.password, 5)) {
errors.password = 'Must be at least 5 characters long';
}
if (!validator.isEmail(this.state.email)) {
errors.email = 'Must be a valid email address';
}
return errors;
},
handleBlur: function () {
this.setState({errors: _.omit(this.validate(), (val, key) => !this.state[key].length)});
},
handleSignUp: function () {
let errors = this.validate();
this.setState({errors});
if (_.isEmpty(errors)) {
accountActions.signup(this.state.username, this.state.password, this.state.email, this.state.subscribe);
}
metrics.track('Signed Up for Docker Hub');
},
handleClickLogin: function () {
if (!this.props.loading) {
this.transitionTo('login');
}
},
render: function () {
let loading = this.props.loading ? <div className="spinner la-ball-clip-rotate la-dark"><div></div></div> : null;
return (
<form className="form-connect" onSubmit={this.handleSignUp}>
<input ref="usernameInput" maxLength="30" name="username" placeholder="Username" type="text" disabled={this.props.loading} valueLink={this.linkState('username')} onBlur={this.handleBlur}/>
<p className="error-message">{this.state.errors.username}</p>
<input ref="emailInput" name="email" placeholder="Email" type="text" valueLink={this.linkState('email')} disabled={this.props.loading} onBlur={this.handleBlur}/>
<p className="error-message">{this.state.errors.email}</p>
<input ref="passwordInput" name="password" placeholder="Password" type="password" valueLink={this.linkState('password')} disabled={this.props.loading} onBlur={this.handleBlur}/>
<p className="error-message">{this.state.errors.password}</p>
<div className="checkbox">
<label>
<input type="checkbox" disabled={this.props.loading} checkedLink={this.linkState('subscribe')}/> Subscribe to the Docker newsletter.
</label>
</div>
<p className="error-message">{this.state.errors.detail}</p>
<div className="submit">
{loading}
<button className="btn btn-action" disabled={this.props.loading} type="submit">Sign Up</button>
</div>
<br/>
<div className="extra">Already have an account? <a disabled={this.state.loading} onClick={this.handleClickLogin}>Log In</a></div>
</form>
);
}
});

View File

@ -21,8 +21,8 @@ var Containers = React.createClass({
getInitialState: function () {
return {
sidebarOffset: 0,
containers: {},
sorted: [],
containers: containerStore.getState().containers,
sorted: this.sorted(containerStore.getState().containers),
updateAvailable: false,
currentButtonLabel: ''
};
@ -43,9 +43,8 @@ var Containers = React.createClass({
containerStore.unlisten(this.update);
},
update: function () {
let containers = containerStore.getState().containers;
let sorted = _.values(containers).sort(function (a, b) {
sorted: function (containers) {
return _.values(containers).sort(function (a, b) {
if (a.State.Downloading && !b.State.Downloading) {
return -1;
} else if (!a.State.Downloading && b.State.Downloading) {
@ -60,6 +59,11 @@ var Containers = React.createClass({
}
}
});
},
update: function () {
let containers = containerStore.getState().containers;
let sorted = this.sorted(containerStore.getState().containers);
let name = this.context.router.getCurrentParams().name;
if (containerStore.getState().pending) {

View File

@ -1,8 +1,11 @@
var Router = require('react-router');
var routes = require('./routes');
module.exports = {
router: null,
var router = Router.create({
routes: routes
});
get: function () {
return this.router;
},
module.exports = router;
set: function (router) {
this.router = router;
}
};

View File

@ -1,5 +1,8 @@
var React = require('react/addons');
var Setup = require('./components/Setup.react');
var Account = require('./components/Account.react');
var AccountSignup = require('./components/AccountSignup.react');
var AccountLogin = require('./components/AccountLogin.react');
var Containers = require('./components/Containers.react');
var ContainerDetails = require('./components/ContainerDetails.react');
var ContainerHome = require('./components/ContainerHome.react');
@ -28,6 +31,10 @@ var App = React.createClass({
var routes = (
<Route name="app" path="/" handler={App}>
<Route name="account" path="/account" handler={Account}>
<Route name="signup" path="/account/signup" handler={AccountSignup}/>
<Route name="login" path="/account/login" handler={AccountLogin}/>
</Route>
<Route name="containers" handler={Containers}>
<Route name="container" path="containers/details/:name" handler={ContainerDetails}>
<DefaultRoute name="containerHome" handler={ContainerHome} />

View File

@ -0,0 +1,57 @@
import alt from '../alt';
import accountServerActions from '../actions/AccountServerActions';
import accountActions from '../actions/AccountActions';
class AccountStore {
constructor () {
this.bindActions(accountServerActions);
this.bindActions(accountActions);
this.prompted = localStorage.getItem('account.prompted') || false;
this.loading = false;
this.errors = {};
this.verified = false;
this.username = null;
}
skip () {
this.setState({
prompted: true
});
localStorage.setItem('account.prompted', true);
}
login () {
this.setState({
loading: true,
errors: {}
});
}
signup () {
this.setState({
loading: true,
errors: {}
});
}
loggedin ({username}) {
this.setState({username, errors: {}, loading: false});
}
signedup ({username}) {
this.setState({username, errors: {}, loading: false});
}
verified ({verified}) {
this.setState({verified});
}
errors ({errors}) {
console.log(errors);
this.setState({errors, loading: false});
}
}
export default alt.createStore(AccountStore);

View File

@ -0,0 +1,29 @@
import alt from '../alt';
import repositoryServerActions from '../actions/RepositoryServerActions';
class RepositoryStore {
constructor () {
this.bindActions(repositoryServerActions);
this.repos = [];
this.loading = false;
this.error = null;
}
fetch () {
this.setState({
repos: [],
error: null,
loading: true
});
}
fetched ({repos}) {
this.setState({repos, loading: false});
}
error ({error}) {
this.setState({error, loading: false});
}
}
export default alt.createStore(RepositoryStore);

70
src/utils/HubUtil.js Normal file
View File

@ -0,0 +1,70 @@
var request = require('request');
var accountServerActions = require('../actions/AccountServerActions');
module.exports = {
// Returns the base64 encoded index token or null if no token exists
config: function () {
let config = localStorage.getItem('auth.config');
if (!config) {
return null;
}
return config;
},
// Retrives the current jwt hub token or null if no token exists
jwt: function () {
let jwt = localStorage.getItem('auth.jwt');
if (!jwt) {
return null;
}
return jwt;
},
loggedin: function () {
return this.jwt() && this.config();
},
// Places a token under ~/.dockercfg and saves a jwt to localstore
login: function (username, password) {
request.post('https://hub.docker.com/v2/users/login/', {form: {username, password}}, (err, response, body) => {
let data = JSON.parse(body);
if (response.statusCode === 200) {
accountServerActions.loggedin({username, verified: true});
// TODO: save username to localstorage
if (data.token) {
localStorage.setItem('auth.jwt', data.token);
}
} else if (response.statusCode === 401) {
if (data && data.detail && data.detail.indexOf('Account not active yet') !== -1) {
accountServerActions.loggedin({username, verified: false});
} else {
accountServerActions.errors({errors: data});
}
}
});
},
// Signs up and places a token under ~/.dockercfg and saves a jwt to localstore
signup: function (username, password, email, subscribe) {
request.post('https://hub.docker.com/v2/users/signup/', {
form: {
username,
password,
email,
subscribe
}
}, (err, response, body) => {
// TODO: save username to localstorage
if (response.statusCode === 204) {
accountServerActions.signedup({username, verified: false});
} else {
let data = JSON.parse(body);
let errors = {};
for (let key in data) {
errors[key] = data[key][0];
}
accountServerActions.errors({errors});
}
});
},
};

45
src/utils/RegHubUtil.js Normal file
View File

@ -0,0 +1,45 @@
var request = require('request');
var async = require('async');
var repositoryServerActions = require('../actions/RepositoryServerActions');
module.exports = {
// Returns the base64 encoded index token or null if no token exists
repos: function (jwt) {
// TODO: provide jwt
request.get({
url: 'https://registry.hub.docker.com/v2/namespaces/',
headers: {
Authorization: `JWT ${jwt}`
}
}, (error, response, body) => {
if (error) {
repositoryServerActions.error({error});
}
let data = JSON.parse(body);
let namespaces = data.namespaces;
async.map(namespaces, (namespace, cb) => {
request.get({
url: `https://registry.hub.docker.com/v2/repositories/${namespace}`,
headers: {
Authorization: `JWT ${jwt}`
}
}, (error, response, body) => {
if (error) {
repositoryServerActions.error({error});
}
let data = JSON.parse(body);
cb(null, data.results);
});
}, (error, lists) => {
let repos = [];
for (let list of lists) {
repos = repos.concat(list);
}
repositoryServerActions.fetched({repos});
});
});
}
};

View File

@ -1,7 +1,7 @@
.header {
position: absolute;
min-width: 100%;
min-height: 40px;
min-height: 60px;
-webkit-app-region: drag;
-webkit-user-select: none;
&.no-drag {

View File

@ -15,6 +15,7 @@
@import "container-home.less";
@import "container-logs.less";
@import "container-settings.less";
@import "spinner.less";
@import "animation.less";
html, body {
@ -23,6 +24,7 @@ html, body {
overflow: hidden;
background: none;
-webkit-user-select: none;
-webkit-user-drag: none;
font-family: @font-regular;
cursor: default;
img {

View File

@ -30,6 +30,135 @@
}
}
.form-section {
display: flex;
width: 50%;
flex: 0 auto;
align-items: flex-end;
justify-content: center;
padding-right: 60px;
padding-left: 80px;
flex-direction: column;
img {
width: 323px;
height: 64px;
}
form {
margin-top: 40px;
text-align: right;
input[type="text"], input[type="password"] {
display: block;
border: 0;
border-bottom: 1px solid @gray-lightest;
color: @gray-normal;
font-weight: 300;
padding: 10px 5px;
transition: all 0.25s;
font-size: 18px;
width: 340px;
&.error {
border-bottom: 1px solid @brand-negative;
&:focus {
border-bottom: 1px solid @brand-negative;
}
}
&:focus {
outline: 0;
border-bottom: 1px solid @brand-action;
}
&::-webkit-input-placeholder {
color: @gray-lighter;
font-weight: 400;
}
}
div.checkbox {
text-align: left;
color: @gray-normal;
}
div.submit {
margin-top: 10px;
display: flex;
flex-direction: row;
justify-content: flex-end;
align-items: center;
.spinner {
margin-right: 10px;
flex: 0 auto;
}
button[type="submit"] {
flex: 0 auto;
display: block;
font-size: 18px;
padding: 10px 20px;
color: white;
background-color: @brand-action;
border: 0;
&:hover {
background-color: darken(@brand-action, 5%);
}
}
}
hr {
border-top: 1px solid #D7DFEA;
}
.extra {
text-align: center;
font-size: 14px;
color: @gray-normal;
margin-top: 16px;
.btn {
margin-left: 6px;
position: relative;
top: -3px;
}
a {
color: @brand-primary;
}
}
.link {
display: block;
font-size: 10px;
text-align: left;
position: relative;
top: -15px;
left: 5px;
}
.error-message {
font-size: 12px;
margin-top: 5px;
margin-bottom: 0;
min-height: 17px;
color: @brand-negative;
}
div.error {
font-size: 13px;
color: @gray-normal;
color: @brand-negative;
background-color: lighten(@brand-negative, 32%);
padding: 10px;
border-radius: 4px;
width: 340px;
display: none;
}
}
}
.btn-skip {
position: absolute;
bottom: 20px;
left: 20px;
font-size: 14px;
}
.desc {
display: flex;
width: 50%;

97
styles/spinner.less Normal file
View File

@ -0,0 +1,97 @@
/*
The MIT License (MIT)
Copyright (c) 2014-2015 Daniel Cardoso
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
@import "variables.less";
.la-ball-clip-rotate {
display: block;
}
.la-ball-clip-rotate > div {
box-sizing: content-box;
color: #fff;
background: #fff;
border-color: #fff;
border-style: solid;
border-width: 0;
}
.la-ball-clip-rotate:after {
display: table;
clear: both;
line-height: 0;
content: "";
}
.la-ball-clip-rotate.la-dark > div {
color: @brand-primary;
background: @brand-primary;
border-color: @brand-primary;
}
/*
* Animation
*/
@-webkit-keyframes ball-clip-rotate {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(180deg);
}
100% {
transform: rotate(360deg);
}
}
.la-ball-clip-rotate {
width: 32px;
height: 32px;
}
.la-ball-clip-rotate > div {
display: block;
float: left;
width: 28px;
height: 28px;
margin: 0;
background: transparent !important;
border-style: solid;
border-width: 1.8px;
border-bottom-color: transparent !important;
border-radius: 100%;
-webkit-animation: ball-clip-rotate 0.9s linear infinite;
}
.la-ball-clip-rotate.la-sm {
width: 16px;
height: 16px;
}
.la-ball-clip-rotate.la-sm > div {
width: 14px;
height: 14px;
margin: 0;
border-width: 1px;
}
.la-ball-clip-rotate.la-lg {
width: 48px;
height: 48px;
}
.la-ball-clip-rotate.la-lg > div {
width: 42px;
height: 42px;
margin: 0;
border-width: 3px;
}
.la-ball-clip-rotate.la-2x {
width: 64px;
height: 64px;
}
.la-ball-clip-rotate.la-2x > div {
width: 56px;
height: 56px;
margin: 0;
border-width: 4px;
}