2014-11-28 20:49:41 -05:00
|
|
|
RootView = require 'views/core/RootView'
|
2014-07-23 10:02:45 -04:00
|
|
|
VersionHistoryView = require './ArticleVersionsModal'
|
2014-01-03 13:32:13 -05:00
|
|
|
template = require 'templates/editor/article/edit'
|
|
|
|
Article = require 'models/Article'
|
2014-11-29 16:36:41 -05:00
|
|
|
SaveVersionModal = require 'views/editor/modal/SaveVersionModal'
|
2014-07-23 10:02:45 -04:00
|
|
|
PatchesView = require 'views/editor/PatchesView'
|
2014-11-28 20:49:41 -05:00
|
|
|
require 'views/modal/RevertModal'
|
2014-11-29 20:51:32 -05:00
|
|
|
require 'vendor/treema'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-07-17 20:16:32 -04: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'
|
2014-04-10 14:13:33 -04:00
|
|
|
'click #save-button': 'openSaveModal'
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
constructor: (options, @articleID) ->
|
|
|
|
super options
|
|
|
|
@article = new Article(_id: @articleID)
|
2014-01-26 17:46:25 -05:00
|
|
|
@article.saveBackups = true
|
2016-02-18 11:44:40 -05:00
|
|
|
@supermodel.loadModel @article
|
2014-08-26 20:34:00 -04:00
|
|
|
@pushChangesToPreview = _.throttle(@pushChangesToPreview, 500)
|
2014-03-27 14:35:20 -04:00
|
|
|
|
2014-08-26 20:34:00 -04:00
|
|
|
onLoaded: ->
|
|
|
|
super()
|
|
|
|
@buildTreema()
|
2015-03-28 16:54:44 -04:00
|
|
|
@listenTo @article, 'change', =>
|
|
|
|
@article.updateI18NCoverage()
|
|
|
|
@treema.set('/', @article.attributes)
|
2014-03-27 14:35:20 -04:00
|
|
|
|
2014-03-24 02:53:41 -04:00
|
|
|
buildTreema: ->
|
2014-04-12 13:51:02 -04:00
|
|
|
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
|
2014-04-24 20:25:16 -04:00
|
|
|
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: =>
|
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)
|
2015-08-14 15:04:00 -04:00
|
|
|
onLoadHandler = =>
|
|
|
|
if b.find('#insert').length == 1
|
|
|
|
b.find('#insert').html(m)
|
|
|
|
b.find('#title').text(@treema.data.name)
|
|
|
|
clearInterval(id)
|
|
|
|
id = setInterval(onLoadHandler, 100)
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-04-01 16:15:30 -04:00
|
|
|
afterRender: ->
|
|
|
|
super()
|
2014-08-26 20:34:00 -04:00
|
|
|
return unless @supermodel.finished()
|
2014-04-12 11:48:49 -04:00
|
|
|
@showReadOnly() if me.get('anonymous')
|
2014-07-07 18:19:22 -04:00
|
|
|
@patchesView = @insertSubView(new PatchesView(@article), @$el.find('.patches-view'))
|
|
|
|
@patchesView.load()
|
2014-04-01 16:15:30 -04:00
|
|
|
|
2014-04-10 14:13:33 -04:00
|
|
|
openPreview: ->
|
2015-08-14 15:04:00 -04:00
|
|
|
if not @preview or @preview.closed
|
|
|
|
@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
|
2014-06-23 21:25:58 -04:00
|
|
|
|
2014-04-10 14:13:33 -04:00
|
|
|
openSaveModal: ->
|
2015-10-21 18:34:07 -04:00
|
|
|
modal = new SaveVersionModal({model: @article})
|
|
|
|
@openModalView(modal)
|
|
|
|
@listenToOnce modal, 'save-new-version', @saveNewArticle
|
|
|
|
@listenToOnce modal, 'hidden', -> @stopListening(modal)
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
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)
|
2014-12-29 12:14:43 -05:00
|
|
|
res = newArticle.save(null, {type: 'POST'}) # Override PUT so we can trigger postNewVersion logic
|
2014-01-03 13:32:13 -05:00
|
|
|
return unless res
|
|
|
|
modal = @$el.find('#save-version-modal')
|
|
|
|
@enableModalInProgress(modal)
|
|
|
|
|
|
|
|
res.error =>
|
|
|
|
@disableModalInProgress(modal)
|
|
|
|
|
|
|
|
res.success =>
|
2014-06-23 21:25:58 -04:00
|
|
|
@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
|
2014-08-27 15:24:03 -04:00
|
|
|
Backbone.Mediator.publish 'editor:view-switched', {}
|