UNPKG

@attivio/suit

Version:

Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.

350 lines (305 loc) 11.3 kB
'use strict'; exports.__esModule = true; var _class, _temp; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _reactBootstrap = require('react-bootstrap'); var _QueryResponse = require('../api/QueryResponse'); var _QueryResponse2 = _interopRequireDefault(_QueryResponse); var _AuthUtils = require('../util/AuthUtils'); var _AuthUtils2 = _interopRequireDefault(_AuthUtils); var _SimpleQueryRequest = require('../api/SimpleQueryRequest'); var _SimpleQueryRequest2 = _interopRequireDefault(_SimpleQueryRequest); var _Comment = require('../api/Comment'); var _Comment2 = _interopRequireDefault(_Comment); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var Comments = (_temp = _class = function (_React$Component) { _inherits(Comments, _React$Component); function Comments(props) { _classCallCheck(this, Comments); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _this.state = { comment: '', commentList: [], error: null, showCommentModal: false }; _this.showCommentModal = _this.showCommentModal.bind(_this); _this.hideCommentModal = _this.hideCommentModal.bind(_this); _this.captureComment = _this.captureComment.bind(_this); _this.saveComment = _this.saveComment.bind(_this); _this.clearComment = _this.clearComment.bind(_this); _this.getComments = _this.getComments.bind(_this); _this.deleteComment = _this.deleteComment.bind(_this); return _this; } Comments.prototype.componentDidMount = function componentDidMount() { this.getComments(); }; Comments.prototype.getComments = function getComments() { var _this2 = this; var searcher = this.context.searcher; var _props = this.props, docId = _props.docId, commentsTable = _props.commentsTable; var commentList = []; if (searcher) { var qr = new _SimpleQueryRequest2.default(); qr.query = 'AND(table:' + commentsTable + ', docId_s:FACET("' + docId + '"))'; qr.facets = []; qr.queryLanguage = 'advanced'; searcher.doCustomSearch(qr, function (response, error) { if (response && response.documents) { response.documents.forEach(function (doc) { commentList.push(_Comment2.default.fromDoc(doc)); }); _this2.setState({ commentList: commentList, comment: '' }); } else if (error) { _this2.setState({ error: error }); } }); } }; Comments.prototype.deleteComment = function deleteComment(id) { var jsonDoc = '{ "id" : "' + id + '", "mode" : "DELETE" }'; this.context.searcher.search.addOrDeleteDocument(JSON.parse(jsonDoc), this.getComments); }; Comments.prototype.createFormattedCommentList = function createFormattedCommentList() { var _this3 = this; var commentList = this.state.commentList; var loggedInUser = _AuthUtils2.default.getUserName(_AuthUtils2.default.getSavedUser()); var formattedCommentList = []; if (commentList && commentList.length > 0) { commentList.forEach(function (comment) { var isCommentByLoggedInUser = comment.username === loggedInUser; var removeCommentOption = isCommentByLoggedInUser && _react2.default.createElement(_reactBootstrap.Glyphicon, { glyph: 'trash', onClick: function onClick() { _this3.deleteComment(comment.id); }, style: { fontSize: '1em', color: '#003c7e', cursor: 'pointer' }, title: 'Delete this comment' }); formattedCommentList.push(_react2.default.createElement( 'div', { key: comment.id }, _react2.default.createElement( 'div', { style: { color: '#484848', backgroundColor: '#F5F5F5', borderRadius: '5px' }, title: comment.timestamp }, _react2.default.createElement( 'div', { style: { padding: '0.5em' } }, _react2.default.createElement( 'b', null, ' ', _react2.default.createElement( 'i', null, ' ', comment.username, ' ' ), ' ' ), _react2.default.createElement( 'div', { style: { float: 'right' } }, removeCommentOption ), _react2.default.createElement('br', null), _react2.default.createElement( 'span', { style: { whiteSpace: 'pre-wrap' } }, comment.text ) ) ), _react2.default.createElement('br', null) )); }); } return formattedCommentList; }; Comments.prototype.showCommentModal = function showCommentModal() { this.setState({ showCommentModal: true }); }; Comments.prototype.hideCommentModal = function hideCommentModal() { this.setState({ showCommentModal: false }); }; Comments.prototype.captureComment = function captureComment(e) { if (e.target instanceof HTMLTextAreaElement) { this.setState({ comment: e.target.value }); } }; Comments.prototype.saveComment = function saveComment() { var docId = this.props.docId; var comment = this.state.comment; var searcher = this.context.searcher; var username = _AuthUtils2.default.getUserName(_AuthUtils2.default.getSavedUser()); var d = new Date(); var loggedDateTime = d.toISOString(); var id = username.concat(loggedDateTime); var body = { fields: { comment_s: [comment], docId_s: [docId], username_s: [username], date: [loggedDateTime], table: ['comments'] }, id: id }; searcher.search.addOrDeleteDocument(JSON.parse(JSON.stringify(body)), this.getComments); }; Comments.prototype.clearComment = function clearComment() { this.setState({ comment: '' }); }; Comments.prototype.renderComments = function renderComments() { var commentList = this.state.commentList; var showComments = commentList && commentList.length > 0; if (showComments) { var formattedCommentList = this.createFormattedCommentList(); return formattedCommentList; } return _react2.default.createElement( 'div', { style: { width: '100%', textAlign: 'center', padding: '1em', color: 'gray' } }, _react2.default.createElement( 'i', null, ' No Comments Available ' ) ); }; Comments.prototype.renderCommentModal = function renderCommentModal() { var _state = this.state, showCommentModal = _state.showCommentModal, comment = _state.comment; return _react2.default.createElement( _reactBootstrap.Modal, { show: showCommentModal, onHide: this.hideCommentModal, backdrop: 'static' }, _react2.default.createElement( _reactBootstrap.Modal.Header, { closeButton: true }, _react2.default.createElement( _reactBootstrap.Modal.Title, null, 'Add or View Comments' ) ), _react2.default.createElement( _reactBootstrap.Modal.Body, null, _react2.default.createElement( _reactBootstrap.Row, null, _react2.default.createElement( _reactBootstrap.Col, { xs: 12, sm: 12, md: 12, lg: 12 }, _react2.default.createElement('textarea', { onChange: this.captureComment, value: comment, style: { width: '100%' }, rows: 5 }), _react2.default.createElement( 'div', { style: { float: 'right' } }, _react2.default.createElement( _reactBootstrap.Button, { onClick: this.clearComment }, 'Clear' ), '\xA0', _react2.default.createElement( _reactBootstrap.Button, { onClick: this.saveComment }, 'Add Comment' ) ) ), _react2.default.createElement( _reactBootstrap.Col, { xs: 12, sm: 12, md: 12, lg: 12 }, _react2.default.createElement('br', null), _react2.default.createElement('br', null) ), _react2.default.createElement( _reactBootstrap.Col, { xs: 12, sm: 12, md: 12, lg: 12 }, _react2.default.createElement( 'h5', { style: { borderBottom: '1px solid lightgray', fontWeight: 'bold' } }, ' Previously Added Comments : ' ), this.renderComments() ) ) ), _react2.default.createElement( _reactBootstrap.Modal.Footer, null, _react2.default.createElement( _reactBootstrap.Button, { onClick: this.hideCommentModal }, 'Close' ) ) ); }; Comments.prototype.renderCommentLink = function renderCommentLink() { var commentList = this.state.commentList; var commentCount = commentList && commentList.length > 0 ? commentList.length : 0; var commentLabel = commentCount > 0 ? 'Comment (' + commentCount + ')' : 'Comment'; return _react2.default.createElement( 'a', { className: 'attivio-tags-more', onClick: this.showCommentModal, role: 'button', tabIndex: 0, title: 'Add/View Comments' }, commentLabel ); }; Comments.prototype.render = function render() { return _react2.default.createElement( 'div', null, this.renderCommentLink(), this.renderCommentModal() ); }; return Comments; }(_react2.default.Component), _class.contextTypes = { searcher: _propTypes2.default.any }, _class.displayName = 'Comments', _temp); exports.default = Comments; module.exports = exports['default'];