posting.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import React from "react"
  2. import ReactDOM from "react-dom"
  3. import { PollForm } from "misago/components/poll"
  4. import PostingComponent from "misago/components/posting"
  5. import mount from "misago/utils/mount-component"
  6. export class Posting {
  7. init(ajax, snackbar, placeholder) {
  8. this._ajax = ajax
  9. this._snackbar = snackbar
  10. this._placeholder = $(placeholder)
  11. this._mode = null
  12. this._isOpen = false
  13. this._isClosing = false
  14. }
  15. open(props) {
  16. if (this._isOpen === false) {
  17. this._mode = props.mode
  18. this._isOpen = props.submit
  19. this._realOpen(props)
  20. } else if (this._isOpen !== props.submit) {
  21. let message = gettext(
  22. "You are already working on other message. Do you want to discard it?"
  23. )
  24. if (this._mode == "POLL") {
  25. message = gettext(
  26. "You are already working on a poll. Do you want to discard it?"
  27. )
  28. }
  29. const changeForm = window.confirm(message)
  30. if (changeForm) {
  31. this._mode = props.mode
  32. this._isOpen = props.submit
  33. this._realOpen(props)
  34. }
  35. } else if (this._mode == "REPLY" && props.mode == "REPLY") {
  36. this._realOpen(props)
  37. }
  38. }
  39. _realOpen(props) {
  40. if (props.mode == "POLL") {
  41. mount(<PollForm {...props} />, "posting-mount")
  42. } else {
  43. mount(<PostingComponent {...props} />, "posting-mount")
  44. }
  45. this._placeholder.addClass("slide-in")
  46. $("html, body").animate(
  47. {
  48. scrollTop: this._placeholder.offset().top
  49. },
  50. 1000
  51. )
  52. }
  53. close = () => {
  54. if (this._isOpen && !this._isClosing) {
  55. this._isClosing = true
  56. this._placeholder.removeClass("slide-in")
  57. window.setTimeout(() => {
  58. ReactDOM.unmountComponentAtNode(
  59. document.getElementById("posting-mount")
  60. )
  61. this._isClosing = false
  62. this._isOpen = false
  63. }, 300)
  64. }
  65. }
  66. }
  67. export default new Posting()