# @cjsx React.DOM
@Posts = React.createClass(
propTypes:
site: React.PropTypes.object.isRequired
getInitialState: ->
{ loading: false, posts: null }
fetchPosts: ->
return if @state.loading
@setState(loading: true)
$.getq("default", "/api/posts").always(@handleResponse).done(@handleSuccess).fail(@handleError)
handleResponse: ->
@setState(loading: false) if @isMounted()
handleSuccess: (response) ->
@setState(posts: response.posts) if @isMounted()
handleError: (error) ->
$.growl("Could not load posts: #{error.statusText} (#{error.status})", growlError)
componentWillMount: ->
@fetchPosts()
@timer = setInterval(@fetchPosts, 5000)
$(document).on("fetchPosts", @fetchPosts)
componentWillUnmount: ->
clearInterval(@timer)
$(document).off("fetchPosts", @fetchPosts)
render: ->
{@state.posts?.map(((post) ->
).bind(this))}
)
@NewPostPartial = React.createClass(
mixins: [ReactRouter.Navigation]
propTypes:
site: React.PropTypes.object.isRequired
getInitialState: ->
{ loading: false }
form: ->
$(@refs.form.getDOMNode())
handleSubmit: (event) ->
event.preventDefault()
return if @state.loading
@setState(loading: true)
$.postq("default", "/api/posts", @form().serialize()).always(@handleResponse).done(@handleSuccess).fail(@handleError)
handleResponse: ->
@setState(loading: false)
handleSuccess: (response) ->
@form()[0].reset()
$(document).trigger("fetchPosts")
@transitionTo("post_edit", post_id: response.posts.identifier)
handleError: (error) ->
$.growl(error.responseJSON?.errors.join(", ") || error.statusText, growlError)
render: ->
)
@PostPartial = React.createClass(
mixins: [ReactRouter.Navigation]
propTypes:
site: React.PropTypes.object.isRequired
post: React.PropTypes.object.isRequired
getInitialState: ->
{ loading: false }
panelClass: ->
if @props.post.added
"success"
else if @props.post.changed
"warning"
else if @props.post.deleted
"danger"
else
"default"
handleEdit: ->
@transitionTo("post_edit", post_id: @props.post.identifier)
handleRevert: ->
return if @state.loading
@setState(loading: true)
$.ajaxq("default", type: "PATCH", url: "/api/posts/#{@props.post.identifier}/revert").
always(@handleResponse).
done(@handleSuccess).
fail(@handleError)
handleDelete: ->
if @props.post.added
return unless confirm("Are you sure? This can't be undone")
return if @state.loading
@setState(loading: true)
$.ajaxq("default", type: "DELETE", url: "/api/posts/#{@props.post.identifier}").
always(@handleResponse).
done(@handleSuccess).
fail(@handleError)
handleRestore: ->
return if @state.loading
@setState(loading: true)
$.ajaxq("default", type: "PATCH", url: "/api/posts/#{@props.post.identifier}/restore").
always(@handleResponse).
done(@handleSuccess).
fail(@handleError)
handleResponse: ->
@setState(loading: false)
handleSuccess: (response) ->
$(document).trigger("fetchPosts")
handleError: (error) ->
$.growl("Could not load post: #{error.statusText} (#{error.status})", growlError)
render: ->
{@props.post.title}
{moment((new Date(@props.post.date.replace(/-/g, "/"))).toISOString()).format("LLL")}
{if @props.post.deleted
else
{if @props.post.changed
}
}
)
@PostEdit = React.createClass(
mixins: [ReactRouter.State, ReactRouter.Navigation]
propTypes:
site: React.PropTypes.object.isRequired
getInitialState: ->
{ loading: false, post: null }
form: ->
$(@refs.form.getDOMNode())
frontMatter: ->
@props.site.octodmin.front_matter
fetchPost: ->
return if @state.loading
@setState(loading: true)
$.getq("default", "/api/posts/#{@getParams().post_id}").always(@handleResponse).done(@handleSuccess).fail(@handleError)
handleUpload: (event)->
form = $("")
event.disableButtons("cmdUpload")
form.find("input").on("change", (->
data = new FormData(form[0])
$.ajaxq("default",
type: "POST"
url: "/api/posts/#{@state.post.identifier}/upload"
data: data
cache: false
contentType: false
processData: false
).always(->
event.enableButtons("cmdUpload")
).done(((result) ->
@handleUploadSuccess(event, result)
).bind(this)).fail(@handleError)
).bind(this))
form.find("input").click()
handleUploadSuccess: (event, response) ->
event.replaceSelection(response.uploads[0])
handleBack: (event) ->
event.preventDefault()
@transitionTo("app")
handleSubmit: (event) ->
event.preventDefault()
return if @state.loading
@setState(loading: true)
data = @form().serializeObject()
$.ajaxq("default", type: "PATCH", url: "/api/posts/#{@state.post.identifier}", data: data).
always(@handleResponse).
done(@handleFormSuccess).
fail(@handleError)
handleResponse: ->
@setState(loading: false)
handleSuccess: (response) ->
@setState(post: response.posts)
handleFormSuccess: (response) ->
$.growl("Post is updated", growlSuccess)
@transitionTo("post_edit", post_id: response.posts.identifier)
handleError: (error) ->
$.growl(error.responseJSON?.errors.join(", ") || error.statusText, growlError)
componentWillMount: ->
@fetchPost()
componentDidUpdate: ->
return if !@state.post || !@refs.editor
return if !/markdown$/.test(@state.post.path) && !/md$/.test(@state.post.path)
$(@refs.editor.getDOMNode()).markdown(
autofocus: false
savable: false
iconlibrary: "fa"
resize: "vertical"
fullscreen:
enable: false
additionalButtons: [
[{
name: "customGroup"
data: [{
name: "cmdUpload"
title: "Upload"
icon: "fa fa-upload"
callback: @handleUpload
}]
}]
]
)
render: ->
{if @state.post
}
)