UNPKG

lumen-foundation-apps

Version:
73 lines (68 loc) 2.21 kB
'use strict'; var React = require('react'); var cloneWithProps = require('react/lib/cloneWithProps'); var foundationApi = require('../utils/foundation-api'); var PopupToggle = require('../popup/toggle'); var Trigger = React.createClass({ displayName: 'Trigger', getDefaultProps: function getDefaultProps() { return { open: null, close: null, toggle: null, hardToggle: null, popupToggle: null, notify: null }; }, getCloseId: function getCloseId() { if (this.props.close) { return this.props.close; } else { var parentElement = false; var tempElement = this.getDOMNode().parentNode; while (parentElement === false) { if (tempElement.nodeName == 'BODY') { parentElement = ''; } if (typeof tempElement.getAttribute('data-closable') !== 'undefined' && tempElement.getAttribute('data-closable') !== false) { parentElement = tempElement; } tempElement = tempElement.parentNode; } return parentElement.getAttribute('id'); } }, clickHandler: function clickHandler(e) { e.preventDefault(); if (this.props.open) { foundationApi.publish(this.props.open, 'open'); } else if (this.props.close !== null) { foundationApi.publish(this.getCloseId(), 'close'); } else if (this.props.toggle) { foundationApi.publish(this.props.toggle, 'toggle'); } else if (this.props.hardToggle) { foundationApi.closeActiveElements({ exclude: this.props.hardToggle }); foundationApi.publish(this.props.hardToggle, 'toggle'); } else if (this.props.notify) { foundationApi.publish(this.props.notify, { title: this.props.title, content: this.props.content, position: this.props.position, color: this.props.color, image: this.props.image }); } }, render: function render() { if (this.props.popupToggle) { return React.createElement(PopupToggle, this.props); } else { var child = React.Children.only(this.props.children); return cloneWithProps(child, { onClick: this.clickHandler }); } } }); module.exports = Trigger;