immutable = require('immutable')
{IS_MOBILE, isMobile, IS_TOUCH} = require('./feature')
underscore = require('underscore')
misc = require('smc-util/misc')
misc_page = require('./misc_page')
{defaults, required} = misc
{Markdown, TimeAgo, Tip} = require('./r_misc')
{webapp_client} = require('./webapp_client')
{alert_message} = require('./alerts')
{React, ReactDOM, rclass, rtypes, Actions, Store, redux} = require('./smc-react')
{Button, Col, Grid, FormControl, FormGroup, ListGroup, ListGroupItem, Panel, Row, ButtonGroup, Well} = require('react-bootstrap')
{User} = require('./users')
exports.redux_name = redux_name = (project_id, path) ->
return "editor-#{project_id}-#{path}"
exports.newest_content = newest_content = (message) ->
return message.get('history').first()?.get('content') ? ''
exports.sender_is_viewer = sender_is_viewer = (account_id, message) ->
account_id == message.get('sender_id')
exports.message_colors = (account_id, message) ->
if sender_is_viewer(account_id, message)
return {background: '#46b1f6', color: '#fff', message_class:'smc-message-from-viewer'}
else
return {background: '#efefef', color: '#000', lighten:{color:'#888'}}
exports.render_timeago = (message, edit) ->
if IS_TOUCH and edit?
f = edit
else
f = undefined
<span
onClick = {f}
className = "pull-right small"
style = {maxWidth:'20%', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis'}
>
<TimeAgo date={new Date(message.get('date'))} />
</span>
NAME_STYLE =
color : "#888"
marginBottom : '1px'
marginLeft : '10px'
right : 0
whiteSpace : 'nowrap'
overflow : 'hidden'
textOverflow : 'ellipsis'
position : 'absolute'
left : 0
top : 0
exports.show_user_name = show_user_name = (sender_name) ->
<div style={position:'relative', height:'1.2em', width:'100%'}>
<div className={"small"} style={NAME_STYLE}>
{sender_name}
</div>
</div>
exports.is_editing = is_editing = (message, account_id) ->
message.get('editing').has(account_id)
exports.blank_column = blank_column = ->
<Col key={2} xs={2} sm={2}></Col>
exports.render_markdown = render_markdown = (value, project_id, file_path, className) ->
<div style={marginBottom:'-10px'}>
<Markdown value={value} project_id={project_id} file_path={file_path} className={className} />
</div>
exports.render_history_title = render_history_title = ->
<ListGroupItem style={borderRadius: '10px 10px 0px 0px', textAlign:'center', padding: '0px'}>
<span style={fontStyle: 'italic', fontWeight: 'bold'}>Message History</span>
</ListGroupItem>
exports.render_history_footer = render_history_footer = ->
<ListGroupItem style={borderRadius: '0px 0px 10px 10px', marginBottom: '3px'}>
</ListGroupItem>
exports.render_history = render_history = (history, user_map) ->
if not history?
return
historyList = history.toJS().slice(1)
for index, objects of historyList
value = objects.content
value = misc.smiley
s: value
wrap: ['<span class="smc-editor-chat-smiley">', '</span>']
value = misc_page.sanitize_html_safe(value)
author = misc.trunc_middle(user_map.get(objects.author_id)?.get('first_name') + ' ' + user_map.get(objects.author_id)?.get('last_name'), 20)
if value.trim() == ''
text = "Message deleted "
else
text = "Last edit "
<Well key={index} bsSize="small" style={marginBottom:'0px'}>
<div style={marginBottom: '-10px', wordWrap:'break-word'}>
<Markdown value={value}/>
</div>
<div className="small">
{text}
<TimeAgo date={new Date(objects.date)} />
{' by ' + author}
</div>
</Well>
exports.get_user_name = get_user_name = (account_id, user_map) ->
account = user_map?.get(account_id)
if account?
account_name = account.get('first_name') + ' ' + account.get('last_name')
else
account_name = "Unknown"
exports.send_chat = send_chat = (e, log_container, mesg, actions) ->
scroll_to_bottom(log_container, actions)
e.preventDefault()
if mesg.length? and mesg.trim().length >= 1
actions.send_chat(mesg)
clear_input(actions)
exports.clear_input = clear_input = (actions) ->
actions.set_input('')
exports.is_at_bottom = is_at_bottom = (saved_position, offset, height) ->
saved_position + offset + 20 > height
exports.scroll_to_bottom = scroll_to_bottom = (log_container, actions) ->
if log_container?
node = ReactDOM.findDOMNode(log_container)
node.scrollTop = node.scrollHeight
actions.save_scroll_state(node.scrollTop, node.scrollHeight, node.offsetHeight)
actions.set_use_saved_position(false)
exports.scroll_to_position = scroll_to_position = (log_container, saved_position, offset, height, use_saved_position, actions) ->
if log_container?
actions.set_use_saved_position(not is_at_bottom(saved_position, offset, height))
node = ReactDOM.findDOMNode(log_container)
if use_saved_position
node.scrollTop = saved_position
else
scroll_to_bottom(log_container, actions)