touchstonejs-ui
Version:
React.js UI components for the TouchstoneJS platform http://touchstonejs.io
217 lines (184 loc) • 5.81 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var classNames = require('classnames');
var React = require('react/addons');
var Tappable = require('react-tappable');
var Transition = React.addons.CSSTransitionGroup;
var DIRECTIONS = {
'reveal-from-right': -1,
'show-from-left': -1,
'show-from-right': 1,
'reveal-from-left': 1
};
var defaultControllerState = {
direction: 0,
fade: false,
leftArrow: false,
leftButtonDisabled: false,
leftIcon: '',
leftLabel: '',
leftAction: null,
rightArrow: false,
rightButtonDisabled: false,
rightIcon: '',
rightLabel: '',
rightAction: null,
title: ''
};
function newState(from) {
var ns = _extends({}, defaultControllerState);
if (from) _extends(ns, from);
delete ns.name; // may leak from props
return ns;
}
var NavigationBar = React.createClass({
displayName: 'NavigationBar',
contextTypes: {
app: React.PropTypes.object
},
propTypes: {
name: React.PropTypes.string
},
getInitialState: function getInitialState() {
return newState(this.props);
},
componentDidMount: function componentDidMount() {
if (this.props.name) {
this.context.app.navigationBars[this.props.name] = this;
}
},
componentWillUnmount: function componentWillUnmount() {
if (this.props.name) {
delete this.context.app.navigationBars[this.props.name];
}
},
componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
this.setState(newState(nextProps));
if (nextProps.name !== this.props.name) {
if (nextProps.name) {
this.context.app.navigationBars[nextProps.name] = this;
}
if (this.props.name) {
delete this.context.app.navigationBars[this.props.name];
}
}
},
update: function update(state) {
// FIXME: what is happening here
state = newState(state);
this.setState(newState(state));
},
updateWithTransition: function updateWithTransition(state, transition) {
state = newState(state);
state.direction = DIRECTIONS[transition] || 0;
if (transition === 'fade' || transition === 'fade-contract' || transition === 'fade-expand') {
state.fade = true;
}
this.setState(state);
},
renderLeftButton: function renderLeftButton() {
var className = classNames('NavigationBarLeftButton', {
'has-arrow': this.state.leftArrow
});
return React.createElement(
Tappable,
{ onTap: this.state.leftAction, className: className, disabled: this.state.leftButtonDisabled, component: 'button' },
this.renderLeftArrow(),
this.renderLeftLabel()
);
},
renderLeftArrow: function renderLeftArrow() {
var transitionName = 'NavigationBarTransition-Instant';
if (this.state.fade || this.state.direction) {
transitionName = 'NavigationBarTransition-Fade';
}
var arrow = this.state.leftArrow ? React.createElement('span', { className: 'NavigationBarLeftArrow' }) : null;
return React.createElement(
Transition,
{ transitionName: transitionName },
arrow
);
},
renderLeftLabel: function renderLeftLabel() {
var transitionName = 'NavigationBarTransition-Instant';
if (this.state.fade) {
transitionName = 'NavigationBarTransition-Fade';
} else if (this.state.direction > 0) {
transitionName = 'NavigationBarTransition-Forwards';
} else if (this.state.direction < 0) {
transitionName = 'NavigationBarTransition-Backwards';
}
return React.createElement(
Transition,
{ transitionName: transitionName },
React.createElement(
'span',
{ key: Date.now(), className: 'NavigationBarLeftLabel' },
this.state.leftLabel
)
);
},
renderTitle: function renderTitle() {
var title = this.state.title ? React.createElement(
'span',
{ key: Date.now(), className: 'NavigationBarTitle' },
this.state.title
) : null;
var transitionName = 'NavigationBarTransition-Instant';
if (this.state.fade) {
transitionName = 'NavigationBarTransition-Fade';
} else if (this.state.direction > 0) {
transitionName = 'NavigationBarTransition-Forwards';
} else if (this.state.direction < 0) {
transitionName = 'NavigationBarTransition-Backwards';
}
return React.createElement(
Transition,
{ transitionName: transitionName },
title
);
},
renderRightButton: function renderRightButton() {
var transitionName = 'NavigationBarTransition-Instant';
if (this.state.fade || this.state.direction) {
transitionName = 'NavigationBarTransition-Fade';
}
var button = this.state.rightIcon || this.state.rightLabel ? React.createElement(
Tappable,
{ key: Date.now(), onTap: this.state.rightAction, className: 'NavigationBarRightButton', disabled: this.state.rightButtonDisabled, component: 'button' },
this.renderRightLabel(),
this.renderRightIcon()
) : null;
return React.createElement(
Transition,
{ transitionName: transitionName },
button
);
},
renderRightIcon: function renderRightIcon() {
if (!this.state.rightIcon) return null;
var className = classNames('NavigationBarRightIcon', this.state.rightIcon);
return React.createElement('span', { className: className });
},
renderRightLabel: function renderRightLabel() {
return this.state.rightLabel ? React.createElement(
'span',
{ key: Date.now(), className: 'NavigationBarRightLabel' },
this.state.rightLabel
) : null;
},
render: function render() {
return React.createElement(
'div',
{ className: 'NavigationBar' },
this.renderLeftButton(),
this.renderTitle(),
this.renderRightButton()
);
}
});
exports['default'] = NavigationBar;
module.exports = exports['default'];