codecombat/app/views/editor/article/ArticleEditView.coffee

103 lines
3.1 KiB
CoffeeScript
Raw Normal View History

RootView = require 'views/core/RootView'
VersionHistoryView = require './ArticleVersionsModal'
2014-01-03 13:32:13 -05:00
template = require 'templates/editor/article/edit'
Article = require 'models/Article'
SaveVersionModal = require 'views/editor/modal/SaveVersionModal'
PatchesView = require 'views/editor/PatchesView'
require 'views/modal/RevertModal'
2014-01-03 13:32:13 -05:00
module.exports = class ArticleEditView extends RootView
2014-06-30 22:16:26 -04:00
id: 'editor-article-edit-view'
2014-01-03 13:32:13 -05:00
template: template
events:
'click #preview-button': 'openPreview'
2014-03-11 14:33:27 -04:00
'click #history-button': 'showVersionHistory'
'click #save-button': 'openSaveModal'
2014-01-03 13:32:13 -05:00
subscriptions:
'editor:save-new-version': 'saveNewArticle'
2014-01-03 13:32:13 -05:00
constructor: (options, @articleID) ->
super options
@article = new Article(_id: @articleID)
@article.saveBackups = true
2014-08-26 20:34:00 -04:00
@supermodel.loadModel @article, 'article'
@pushChangesToPreview = _.throttle(@pushChangesToPreview, 500)
2014-08-26 20:34:00 -04:00
onLoaded: ->
super()
@buildTreema()
2014-03-24 02:53:41 -04:00
buildTreema: ->
return if @treema? or (not @article.loaded)
2014-01-03 13:32:13 -05:00
unless @article.attributes.body
@article.set('body', '')
data = $.extend(true, {}, @article.attributes)
options =
data: data
2014-02-26 22:30:37 -05:00
filePath: "db/thang.type/#{@article.get('original')}"
2014-04-12 04:35:56 -04:00
schema: Article.schema
readOnly: me.get('anonymous')
2014-01-03 13:32:13 -05:00
callbacks:
change: @pushChangesToPreview
@treema = @$el.find('#article-treema').treema(options)
@treema.build()
pushChangesToPreview: =>
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
context.authorized = not me.get('anonymous')
2014-01-03 13:32:13 -05:00
context
afterRender: ->
super()
2014-08-26 20:34:00 -04:00
return unless @supermodel.finished()
@showReadOnly() if me.get('anonymous')
@patchesView = @insertSubView(new PatchesView(@article), @$el.find('.patches-view'))
@patchesView.load()
openPreview: ->
@preview = window.open('/editor/article/preview', 'preview', 'height=800,width=600')
2014-01-03 13:32:13 -05:00
@preview.focus() if window.focus
@preview.onload = => @pushChangesToPreview()
return false
openSaveModal: ->
@openModalView(new SaveVersionModal({model: @article}))
2014-01-03 13:32:13 -05:00
saveNewArticle: (e) ->
@treema.endExistingEdits()
for key, value of @treema.data
@article.set(key, value)
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 =>
@article.clearBackup()
2014-01-03 13:32:13 -05:00
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-06-30 22:16:26 -04:00
versionHistoryView = new VersionHistoryView article: @article, @articleID
2014-03-11 14:33:27 -04:00
@openModalView versionHistoryView
Backbone.Mediator.publish 'editor:view-switched', {}