2014-11-28 20:49:41 -05:00
|
|
|
RootView = require 'views/core/RootView'
|
2014-10-27 20:11:48 -04:00
|
|
|
locale = require 'locale/locale'
|
|
|
|
Patch = require 'models/Patch'
|
2016-09-07 19:15:54 -04:00
|
|
|
Patches = require 'collections/Patches'
|
|
|
|
PatchModal = require 'views/editor/PatchModal'
|
2014-10-27 20:11:48 -04:00
|
|
|
template = require 'templates/i18n/i18n-edit-model-view'
|
2014-11-28 20:49:41 -05:00
|
|
|
deltasLib = require 'core/deltas'
|
2014-10-27 20:11:48 -04:00
|
|
|
|
2016-09-07 19:15:54 -04:00
|
|
|
###
|
|
|
|
This view is the superclass for all views which Diplomats use to submit translations
|
|
|
|
for database documents. They all work mostly the same, except they each set their
|
|
|
|
`@modelClass` which is a patchable Backbone model class, and they use `@wrapRow()`
|
|
|
|
to dynamically specify which properties are being translated.
|
|
|
|
###
|
|
|
|
|
|
|
|
UNSAVED_CHANGES_MESSAGE = 'You have unsaved changes! Really discard them?'
|
2014-11-28 19:38:50 -05:00
|
|
|
|
2014-10-27 20:11:48 -04:00
|
|
|
module.exports = class I18NEditModelView extends RootView
|
|
|
|
className: 'editor i18n-edit-model-view'
|
|
|
|
template: template
|
2014-12-29 12:14:43 -05:00
|
|
|
|
2014-10-27 20:11:48 -04:00
|
|
|
events:
|
2016-09-07 19:15:54 -04:00
|
|
|
'input .translation-input': 'onInputChanged'
|
2014-10-27 20:11:48 -04:00
|
|
|
'change #language-select': 'onLanguageSelectChanged'
|
|
|
|
'click #patch-submit': 'onSubmitPatch'
|
2016-09-07 19:15:54 -04:00
|
|
|
'click .open-patch-link': 'onClickOpenPatchLink'
|
2014-10-27 20:11:48 -04:00
|
|
|
|
|
|
|
constructor: (options, @modelHandle) ->
|
|
|
|
super(options)
|
2016-09-07 19:15:54 -04:00
|
|
|
|
2014-10-27 20:11:48 -04:00
|
|
|
@model = new @modelClass(_id: @modelHandle)
|
2016-09-07 19:15:54 -04:00
|
|
|
@supermodel.trackRequest(@model.fetch())
|
|
|
|
@patches = new Patches()
|
|
|
|
@listenTo @patches, 'change', -> @renderSelectors('#patches-col')
|
|
|
|
@patches.comparator = '_id'
|
|
|
|
@supermodel.trackRequest(@patches.fetchMineFor(@model))
|
|
|
|
|
2014-10-27 20:11:48 -04:00
|
|
|
@selectedLanguage = me.get('preferredLanguage', true)
|
2016-09-07 19:15:54 -04:00
|
|
|
@madeChanges = false
|
2014-12-29 12:14:43 -05:00
|
|
|
|
2014-10-27 20:11:48 -04:00
|
|
|
showLoading: ($el) ->
|
|
|
|
$el ?= @$el.find('.outer-content')
|
|
|
|
super($el)
|
2014-12-29 12:14:43 -05:00
|
|
|
|
2014-10-27 20:11:48 -04:00
|
|
|
onLoaded: ->
|
|
|
|
super()
|
2016-09-07 19:15:54 -04:00
|
|
|
@originalModel = @model.clone()
|
2014-10-27 20:11:48 -04:00
|
|
|
|
|
|
|
getRenderData: ->
|
|
|
|
c = super()
|
|
|
|
|
|
|
|
c.model = @model
|
|
|
|
c.selectedLanguage = @selectedLanguage
|
2014-12-29 12:14:43 -05:00
|
|
|
|
2014-10-27 20:11:48 -04:00
|
|
|
@translationList = []
|
|
|
|
if @supermodel.finished() then @buildTranslationList() else []
|
|
|
|
result.index = index for result, index in @translationList
|
|
|
|
c.translationList = @translationList
|
2014-12-29 12:14:43 -05:00
|
|
|
|
2014-10-27 20:11:48 -04:00
|
|
|
c
|
2014-12-29 12:14:43 -05:00
|
|
|
|
2014-10-27 20:11:48 -04:00
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
|
2016-09-07 19:15:54 -04:00
|
|
|
@ignoreLanguageSelectChanges = true
|
2014-10-27 20:11:48 -04:00
|
|
|
$select = @$el.find('#language-select').empty()
|
|
|
|
@addLanguagesToSelect($select, @selectedLanguage)
|
2014-11-25 19:20:41 -05:00
|
|
|
@$el.find('option[value="en-US"]').remove()
|
2016-09-07 19:15:54 -04:00
|
|
|
@ignoreLanguageSelectChanges = false
|
2014-10-27 20:11:48 -04:00
|
|
|
editors = []
|
|
|
|
|
|
|
|
@$el.find('tr[data-format="markdown"]').each((index, el) =>
|
2015-03-11 09:45:35 -04:00
|
|
|
foundEnEl = enEl=$(el).find('.english-value-row div')[0]
|
|
|
|
if foundEnEl?
|
|
|
|
englishEditor = ace.edit(foundEnEl)
|
|
|
|
englishEditor.el = enEl
|
|
|
|
englishEditor.setReadOnly(true)
|
|
|
|
editors.push englishEditor
|
|
|
|
foundToEl = toEl=$(el).find('.to-value-row div')[0]
|
|
|
|
if foundToEl?
|
|
|
|
toEditor = ace.edit(foundToEl)
|
|
|
|
toEditor.el = toEl
|
|
|
|
toEditor.on 'change', @onEditorChange
|
|
|
|
editors.push toEditor
|
2014-10-27 20:11:48 -04:00
|
|
|
)
|
2014-12-29 12:14:43 -05:00
|
|
|
|
2014-10-27 20:11:48 -04:00
|
|
|
for editor in editors
|
|
|
|
session = editor.getSession()
|
|
|
|
session.setTabSize 2
|
|
|
|
session.setMode 'ace/mode/markdown'
|
|
|
|
session.setNewLineMode = 'unix'
|
|
|
|
session.setUseSoftTabs true
|
|
|
|
editor.setOptions({ maxLines: Infinity })
|
|
|
|
|
|
|
|
onEditorChange: (event, editor) =>
|
|
|
|
return if @destroyed
|
|
|
|
index = $(editor.el).data('index')
|
|
|
|
rowInfo = @translationList[index]
|
|
|
|
value = editor.getValue()
|
|
|
|
@onTranslationChanged(rowInfo, value)
|
|
|
|
|
|
|
|
wrapRow: (title, key, enValue, toValue, path, format) ->
|
2014-12-29 12:14:43 -05:00
|
|
|
@translationList.push {
|
2014-10-27 20:11:48 -04:00
|
|
|
title: title,
|
2014-12-29 12:14:43 -05:00
|
|
|
key: key,
|
|
|
|
enValue: enValue,
|
|
|
|
toValue: toValue or '',
|
2014-10-27 20:11:48 -04:00
|
|
|
path: path
|
|
|
|
format: format
|
|
|
|
}
|
|
|
|
|
|
|
|
buildTranslationList: -> [] # overwrite
|
2014-12-29 12:14:43 -05:00
|
|
|
|
2014-10-27 20:11:48 -04:00
|
|
|
onInputChanged: (e) ->
|
|
|
|
index = $(e.target).data('index')
|
|
|
|
rowInfo = @translationList[index]
|
|
|
|
value = $(e.target).val()
|
|
|
|
@onTranslationChanged(rowInfo, value)
|
|
|
|
|
|
|
|
onTranslationChanged: (rowInfo, value) ->
|
|
|
|
#- Navigate down to where the translation will live
|
|
|
|
base = @model.attributes
|
2014-12-29 12:14:43 -05:00
|
|
|
|
2014-10-27 20:11:48 -04:00
|
|
|
for seg in rowInfo.path
|
|
|
|
base = base[seg]
|
|
|
|
|
|
|
|
base = base.i18n
|
|
|
|
|
|
|
|
base[@selectedLanguage] ?= {}
|
|
|
|
base = base[@selectedLanguage]
|
2014-12-29 12:14:43 -05:00
|
|
|
|
2014-10-27 20:11:48 -04:00
|
|
|
if rowInfo.key.length > 1
|
|
|
|
for seg in rowInfo.key[..-2]
|
|
|
|
base[seg] ?= {}
|
|
|
|
base = base[seg]
|
2014-12-29 12:14:43 -05:00
|
|
|
|
2014-10-27 20:11:48 -04:00
|
|
|
#- Set the data in a non-kosher way
|
|
|
|
base[rowInfo.key[rowInfo.key.length-1]] = value
|
|
|
|
@model.saveBackup()
|
2014-12-29 12:14:43 -05:00
|
|
|
|
2014-10-27 20:11:48 -04:00
|
|
|
#- Enable patch submit button
|
|
|
|
@$el.find('#patch-submit').attr('disabled', null)
|
2016-09-07 19:15:54 -04:00
|
|
|
@madeChanges = true
|
2014-10-27 20:11:48 -04:00
|
|
|
|
|
|
|
onLanguageSelectChanged: (e) ->
|
2016-09-07 19:15:54 -04:00
|
|
|
return if @ignoreLanguageSelectChanges
|
|
|
|
if @madeChanges
|
|
|
|
return unless confirm(UNSAVED_CHANGES_MESSAGE)
|
2014-10-27 20:11:48 -04:00
|
|
|
@selectedLanguage = $(e.target).val()
|
2014-11-25 19:20:41 -05:00
|
|
|
if @selectedLanguage
|
|
|
|
me.set('preferredLanguage', @selectedLanguage)
|
|
|
|
me.patch()
|
2016-09-07 19:15:54 -04:00
|
|
|
@madeChanges = false
|
|
|
|
@model.set(@originalModel.clone().attributes)
|
2014-10-27 20:11:48 -04:00
|
|
|
@render()
|
2014-12-29 12:14:43 -05:00
|
|
|
|
2016-09-07 19:15:54 -04:00
|
|
|
onClickOpenPatchLink: (e) ->
|
|
|
|
patchID = $(e.currentTarget).data('patch-id')
|
|
|
|
patch = @patches.get(patchID)
|
|
|
|
modal = new PatchModal(patch, @model)
|
|
|
|
@openModalView(modal)
|
2014-12-29 12:14:43 -05:00
|
|
|
|
2016-09-07 19:15:54 -04:00
|
|
|
onLeaveMessage: ->
|
|
|
|
if @madeChanges
|
|
|
|
return UNSAVED_CHANGES_MESSAGE
|
2014-12-29 12:14:43 -05:00
|
|
|
|
2016-09-07 19:15:54 -04:00
|
|
|
onSubmitPatch: (e) ->
|
|
|
|
delta = @originalModel.getDeltaWith(@model)
|
|
|
|
flattened = deltasLib.flattenDelta(delta)
|
|
|
|
collection = _.string.underscored @model.constructor.className
|
|
|
|
patch = new Patch({
|
|
|
|
delta
|
|
|
|
target: { collection, 'id': @model.id }
|
|
|
|
commitMessage: "Diplomat submission for lang #{@selectedLanguage}: #{flattened.length} change(s)."
|
|
|
|
})
|
|
|
|
errors = patch.validate()
|
2014-10-27 20:11:48 -04:00
|
|
|
button = $(e.target)
|
|
|
|
button.attr('disabled', 'disabled')
|
2016-09-07 19:15:54 -04:00
|
|
|
return button.text('No changes submitted, did not save patch.') unless delta
|
2014-10-27 20:11:48 -04:00
|
|
|
return button.text('Failed to Submit Changes') if errors
|
2016-09-07 19:15:54 -04:00
|
|
|
res = patch.save(null, { url: _.result(@model, 'url') + '/patch' })
|
2014-10-27 20:11:48 -04:00
|
|
|
return button.text('Failed to Submit Changes') unless res
|
2015-01-07 19:15:26 -05:00
|
|
|
button.text('Submitting...')
|
2016-09-07 19:15:54 -04:00
|
|
|
Promise.resolve(res)
|
|
|
|
.then =>
|
|
|
|
@madeChanges = false
|
|
|
|
@patches.add(patch)
|
|
|
|
@renderSelectors('#patches-col')
|
2015-02-05 14:47:27 -05:00
|
|
|
button.text('Submit Changes')
|
2016-09-07 19:15:54 -04:00
|
|
|
.catch =>
|
|
|
|
button.text('Error Submitting Changes')
|
|
|
|
@$el.find('#patch-submit').attr('disabled', null)
|
|
|
|
|