2014-01-03 13:32:13 -05:00
|
|
|
View = require 'views/kinds/RootView'
|
2014-03-11 14:33:27 -04:00
|
|
|
VersionHistoryView = require './versions_view'
|
2014-03-27 14:35:20 -04:00
|
|
|
ErrorView = require '../../error_view'
|
2014-01-03 13:32:13 -05:00
|
|
|
template = require 'templates/editor/article/edit'
|
|
|
|
Article = require 'models/Article'
|
|
|
|
|
|
|
|
module.exports = class ArticleEditView extends View
|
|
|
|
id: "editor-article-edit-view"
|
|
|
|
template: template
|
|
|
|
startsLoading: true
|
|
|
|
|
|
|
|
events:
|
|
|
|
'click #preview-button': 'openPreview'
|
2014-03-11 14:33:27 -04:00
|
|
|
'click #history-button': 'showVersionHistory'
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
subscriptions:
|
|
|
|
'save-new-version': 'saveNewArticle'
|
|
|
|
|
|
|
|
constructor: (options, @articleID) ->
|
|
|
|
super options
|
|
|
|
@article = new Article(_id: @articleID)
|
2014-01-26 17:46:25 -05:00
|
|
|
@article.saveBackups = true
|
2014-03-27 14:35:20 -04:00
|
|
|
|
2014-04-01 16:15:30 -04:00
|
|
|
@listenToOnce(@article, 'error',
|
|
|
|
() =>
|
2014-03-27 14:35:20 -04:00
|
|
|
@hideLoading()
|
|
|
|
|
|
|
|
# Hack: editor components appear after calling insertSubView.
|
2014-04-01 16:15:30 -04:00
|
|
|
# So we need to hide them first.
|
2014-03-27 14:35:20 -04:00
|
|
|
$(@$el).find('.main-content-area').children('*').not('#error-view').remove()
|
|
|
|
|
|
|
|
@insertSubView(new ErrorView())
|
|
|
|
)
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
@article.fetch()
|
2014-04-01 16:15:30 -04:00
|
|
|
@article.loadSchema()
|
2014-03-24 02:53:41 -04:00
|
|
|
@listenToOnce(@article, 'sync', @onArticleSync)
|
2014-04-01 16:15:30 -04:00
|
|
|
@listenToOnce(@article, 'schema-loaded', @buildTreema)
|
2014-01-03 13:32:13 -05:00
|
|
|
@pushChangesToPreview = _.throttle(@pushChangesToPreview, 500)
|
|
|
|
|
2014-03-24 02:53:41 -04:00
|
|
|
onArticleSync: ->
|
2014-01-03 13:32:13 -05:00
|
|
|
@article.loaded = true
|
|
|
|
@buildTreema()
|
|
|
|
|
2014-03-24 02:53:41 -04:00
|
|
|
buildTreema: ->
|
2014-01-03 13:32:13 -05:00
|
|
|
return if @treema? or (not @article.loaded) or (not Article.hasSchema())
|
|
|
|
unless @article.attributes.body
|
|
|
|
@article.set('body', '')
|
|
|
|
@startsLoading = false
|
|
|
|
@render()
|
|
|
|
data = $.extend(true, {}, @article.attributes)
|
|
|
|
options =
|
|
|
|
data: data
|
2014-02-26 22:30:37 -05:00
|
|
|
filePath: "db/thang.type/#{@article.get('original')}"
|
2014-01-03 13:32:13 -05:00
|
|
|
schema: Article.schema.attributes
|
2014-03-03 16:21:56 -05:00
|
|
|
readOnly: true unless me.isAdmin() or @article.hasWriteAccess(me)
|
2014-01-03 13:32:13 -05:00
|
|
|
callbacks:
|
|
|
|
change: @pushChangesToPreview
|
|
|
|
@treema = @$el.find('#article-treema').treema(options)
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
@treema.build()
|
|
|
|
|
|
|
|
pushChangesToPreview: =>
|
2014-01-26 17:46:25 -05:00
|
|
|
for key, value of @treema.data
|
|
|
|
@article.set(key, value)
|
2014-01-03 13:32:13 -05:00
|
|
|
return unless @treema and @preview
|
|
|
|
m = marked(@treema.data.body)
|
|
|
|
b = $(@preview.document.body)
|
|
|
|
b.find('#insert').html(m)
|
|
|
|
b.find('#title').text(@treema.data.name)
|
|
|
|
|
2014-02-11 17:58:45 -05:00
|
|
|
getRenderData: (context={}) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
context = super(context)
|
|
|
|
context.article = @article
|
2014-03-03 15:13:02 -05:00
|
|
|
context.authorized = me.isAdmin() or @article.hasWriteAccess(me)
|
2014-01-03 13:32:13 -05:00
|
|
|
context
|
|
|
|
|
2014-04-01 16:15:30 -04:00
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
return if @startsLoading
|
|
|
|
@showReadOnly() unless me.isAdmin() or @article.hasWriteAccess(me)
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
openPreview: =>
|
2014-03-02 19:06:22 -05:00
|
|
|
@preview = window.open('/editor/article/x/preview', 'preview', 'height=800,width=600')
|
2014-01-03 13:32:13 -05:00
|
|
|
@preview.focus() if window.focus
|
|
|
|
@preview.onload = => @pushChangesToPreview()
|
|
|
|
return false
|
|
|
|
|
|
|
|
saveNewArticle: (e) ->
|
|
|
|
@treema.endExistingEdits()
|
|
|
|
for key, value of @treema.data
|
|
|
|
@article.set(key, value)
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
newArticle = if e.major then @article.cloneNewMajorVersion() else @article.cloneNewMinorVersion()
|
|
|
|
newArticle.set('commitMessage', e.commitMessage)
|
|
|
|
res = newArticle.save()
|
|
|
|
return unless res
|
|
|
|
modal = @$el.find('#save-version-modal')
|
|
|
|
@enableModalInProgress(modal)
|
|
|
|
|
|
|
|
res.error =>
|
|
|
|
@disableModalInProgress(modal)
|
|
|
|
|
|
|
|
res.success =>
|
|
|
|
modal.modal('hide')
|
|
|
|
url = "/editor/article/#{newArticle.get('slug') or newArticle.id}"
|
|
|
|
document.location.href = url
|
2014-03-11 14:33:27 -04:00
|
|
|
|
|
|
|
showVersionHistory: (e) ->
|
2014-03-12 10:22:15 -04:00
|
|
|
versionHistoryView = new VersionHistoryView article:@article, @articleID
|
2014-03-11 14:33:27 -04:00
|
|
|
@openModalView versionHistoryView
|
|
|
|
Backbone.Mediator.publish 'level:view-switched', e
|