akkoma-fe/src/components/post_status_form/post_status_form.js

149 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-10-30 15:53:58 +00:00
import statusPoster from '../../services/status_poster/status_poster.service.js'
2016-11-06 18:30:35 +00:00
import MediaUpload from '../media_upload/media_upload.vue'
2016-11-25 17:21:25 +00:00
import fileTypeService from '../../services/file_type/file_type.service.js'
import Tribute from '../../../node_modules/tributejs/src/Tribute.js'
require('../../../node_modules/tributejs/scss/tribute.scss')
2016-11-06 18:30:35 +00:00
import { merge, reject, map, uniqBy } from 'lodash'
2016-11-03 16:17:32 +00:00
const buildMentionsString = ({user, attentions}, currentUser) => {
let allAttentions = [...attentions]
allAttentions.unshift(user)
allAttentions = uniqBy(allAttentions, 'id')
allAttentions = reject(allAttentions, {id: currentUser.id})
let mentions = map(allAttentions, (attention) => {
return `@${attention.screen_name}`
})
return mentions.join(' ') + ' '
}
2016-10-30 15:53:58 +00:00
const defaultCollection = {
// symbol that starts the lookup
trigger: '@',
// element to target for @mentions
iframe: null,
// class added in the flyout menu for active item
selectClass: 'highlight',
// function called on select that returns the content to insert
selectTemplate: function (item) {
return '@' + item.original.screen_name
},
// template for displaying item in menu
menuItemTemplate: function (item) {
return `<img src="${item.original.profile_image_url}"></img> <div class='name'>${item.string}</div>`
},
// template for when no match is found (optional),
// If no template is provided, menu is hidden.
noMatchTemplate: null,
// specify an alternative parent container for the menu
menuContainer: document.body,
// column to search against in the object (accepts function or string)
lookup: ({name, screen_name}) => `${name} (@${screen_name})`,
// column that contains the content to insert by default
fillAttr: 'screen_name',
// REQUIRED: array of objects to match
values: [],
// specify whether a space is required before the trigger character
requireLeadingSpace: true,
// specify whether a space is allowed in the middle of mentions
allowSpaces: false
}
const tribute = new Tribute({ collection: [] })
2016-10-30 15:53:58 +00:00
const PostStatusForm = {
2016-11-03 15:59:27 +00:00
props: [
2016-11-03 16:17:32 +00:00
'replyTo',
'repliedUser',
'attentions'
2016-11-03 15:59:27 +00:00
],
2016-11-06 18:30:35 +00:00
components: {
MediaUpload
},
2016-11-03 15:59:27 +00:00
data () {
2016-11-03 16:17:32 +00:00
let statusText = ''
if (this.replyTo) {
const currentUser = this.$store.state.users.currentUser
statusText = buildMentionsString({ user: this.repliedUser, attentions: this.attentions }, currentUser)
}
2016-10-30 15:53:58 +00:00
return {
submitDisabled: false,
2016-11-03 16:17:32 +00:00
newStatus: {
2016-11-06 18:30:35 +00:00
status: statusText,
files: []
2016-11-03 16:17:32 +00:00
}
2016-10-30 15:53:58 +00:00
}
},
computed: {
users () {
return this.$store.state.users.users
},
completions () {
let users = this.users
users = merge({values: users}, defaultCollection)
return [users]
}
},
watch: {
completions () {
tribute.collection = this.completions
}
},
mounted () {
const textarea = this.$el.querySelector('textarea')
tribute.collection = this.completions
tribute.attach(textarea)
},
2016-10-30 15:53:58 +00:00
methods: {
2016-11-03 15:59:27 +00:00
postStatus (newStatus) {
2016-10-30 15:53:58 +00:00
statusPoster.postStatus({
status: newStatus.status,
2016-11-06 18:30:35 +00:00
media: newStatus.files,
2016-11-03 15:59:27 +00:00
store: this.$store,
inReplyToStatusId: this.replyTo
2016-10-30 15:53:58 +00:00
})
2016-11-06 18:30:35 +00:00
this.newStatus = {
status: '',
files: []
}
2016-11-03 16:35:49 +00:00
this.$emit('posted')
2016-11-06 18:30:35 +00:00
},
addMediaFile (fileInfo) {
this.newStatus.files.push(fileInfo)
this.enableSubmit()
},
2016-11-26 02:00:06 +00:00
removeMediaFile (fileInfo) {
let index = this.newStatus.files.indexOf(fileInfo)
this.newStatus.files.splice(index, 1)
},
disableSubmit () {
this.submitDisabled = true
},
enableSubmit () {
this.submitDisabled = false
2016-11-25 17:21:25 +00:00
},
type (fileInfo) {
return fileTypeService.fileType(fileInfo.mimetype)
2016-10-30 15:53:58 +00:00
}
}
}
export default PostStatusForm