2014-07-17 20:16:32 -04:00
|
|
|
RootView = require 'views/kinds/RootView'
|
2014-05-21 07:52:09 -04:00
|
|
|
template = require 'templates/editor/achievement/edit'
|
|
|
|
Achievement = require 'models/Achievement'
|
2014-08-11 08:11:26 -04:00
|
|
|
AchievementPopup = require 'views/achievements/AchievementPopup'
|
2014-07-23 10:02:45 -04:00
|
|
|
ConfirmModal = require 'views/modal/ConfirmModal'
|
2014-08-05 08:33:33 -04:00
|
|
|
errors = require 'lib/errors'
|
2014-08-08 11:14:57 -04:00
|
|
|
app = require 'application'
|
2014-05-21 07:52:09 -04:00
|
|
|
|
2014-07-17 20:16:32 -04:00
|
|
|
module.exports = class AchievementEditView extends RootView
|
2014-06-30 22:16:26 -04:00
|
|
|
id: 'editor-achievement-edit-view'
|
2014-05-21 07:52:09 -04:00
|
|
|
template: template
|
|
|
|
startsLoading: true
|
|
|
|
|
|
|
|
events:
|
2014-05-21 13:47:17 -04:00
|
|
|
'click #save-button': 'saveAchievement'
|
2014-06-08 18:33:06 -04:00
|
|
|
'click #recalculate-button': 'confirmRecalculation'
|
2014-08-08 11:14:57 -04:00
|
|
|
'click #delete-button': 'confirmDeletion'
|
2014-05-21 07:52:09 -04:00
|
|
|
|
|
|
|
subscriptions:
|
2014-05-21 13:47:17 -04:00
|
|
|
'save-new': 'saveAchievement'
|
2014-05-21 07:52:09 -04:00
|
|
|
|
|
|
|
constructor: (options, @achievementID) ->
|
|
|
|
super options
|
|
|
|
@achievement = new Achievement(_id: @achievementID)
|
|
|
|
@achievement.saveBackups = true
|
|
|
|
|
2014-08-05 08:33:33 -04:00
|
|
|
@achievement.once 'error', (achievement, jqxhr) =>
|
|
|
|
@hideLoading()
|
|
|
|
$(@$el).find('.main-content-area').children('*').not('.breadcrumb').remove()
|
|
|
|
errors.backboneFailure arguments...
|
2014-05-21 07:52:09 -04:00
|
|
|
|
|
|
|
@achievement.fetch()
|
|
|
|
@listenToOnce(@achievement, 'sync', @buildTreema)
|
|
|
|
@pushChangesToPreview = _.throttle(@pushChangesToPreview, 500)
|
|
|
|
|
|
|
|
buildTreema: ->
|
|
|
|
return if @treema? or (not @achievement.loaded)
|
|
|
|
|
|
|
|
@startsLoading = false
|
|
|
|
@render()
|
|
|
|
data = $.extend(true, {}, @achievement.attributes)
|
|
|
|
options =
|
|
|
|
data: data
|
2014-05-22 10:59:01 -04:00
|
|
|
filePath: "db/achievement/#{@achievement.get('_id')}"
|
2014-05-21 07:52:09 -04:00
|
|
|
schema: Achievement.schema
|
|
|
|
readOnly: me.get('anonymous')
|
|
|
|
callbacks:
|
|
|
|
change: @pushChangesToPreview
|
|
|
|
@treema = @$el.find('#achievement-treema').treema(options)
|
|
|
|
|
|
|
|
@treema.build()
|
|
|
|
|
|
|
|
getRenderData: (context={}) ->
|
|
|
|
context = super(context)
|
|
|
|
context.achievement = @achievement
|
|
|
|
context.authorized = me.isAdmin()
|
|
|
|
context
|
|
|
|
|
2014-06-24 07:49:54 -04:00
|
|
|
afterRender: ->
|
|
|
|
super(arguments...)
|
|
|
|
@pushChangesToPreview()
|
|
|
|
|
|
|
|
pushChangesToPreview: =>
|
2014-08-11 08:51:34 -04:00
|
|
|
$('#achievement-view').empty()
|
2014-06-24 07:49:54 -04:00
|
|
|
|
|
|
|
if @treema?
|
|
|
|
for key, value of @treema.data
|
|
|
|
@achievement.set key, value
|
|
|
|
|
|
|
|
earned =
|
|
|
|
earnedPoints: @achievement.get 'worth'
|
|
|
|
|
2014-08-11 08:11:26 -04:00
|
|
|
popup = new AchievementPopup achievement: @achievement, earnedAchievement:earned, popup: false, container: $('#achievement-view')
|
|
|
|
|
2014-06-24 07:49:54 -04:00
|
|
|
|
2014-05-21 07:52:09 -04:00
|
|
|
openSaveModal: ->
|
2014-08-07 16:03:00 -04:00
|
|
|
'Maybe later' # TODO patch patch patch
|
2014-05-21 07:52:09 -04:00
|
|
|
|
|
|
|
saveAchievement: (e) ->
|
2014-05-21 13:47:17 -04:00
|
|
|
@treema.endExistingEdits()
|
|
|
|
for key, value of @treema.data
|
|
|
|
@achievement.set(key, value)
|
|
|
|
|
|
|
|
res = @achievement.save()
|
|
|
|
|
2014-05-31 17:55:26 -04:00
|
|
|
res.error (collection, response, options) =>
|
|
|
|
console.error response
|
2014-05-21 13:47:17 -04:00
|
|
|
|
|
|
|
res.success =>
|
|
|
|
url = "/editor/achievement/#{@achievement.get('slug') or @achievement.id}"
|
2014-05-31 17:19:55 -04:00
|
|
|
document.location.href = url
|
2014-06-08 18:33:06 -04:00
|
|
|
|
2014-08-08 11:14:57 -04:00
|
|
|
confirmRecalculation: ->
|
2014-06-08 18:33:06 -04:00
|
|
|
renderData =
|
2014-06-30 22:16:26 -04:00
|
|
|
'confirmTitle': 'Are you really sure?'
|
|
|
|
'confirmBody': 'This will trigger recalculation of the achievement for all users. Are you really sure you want to go down this path?'
|
|
|
|
'confirmDecline': 'Not really'
|
|
|
|
'confirmConfirm': 'Definitely'
|
2014-06-08 18:33:06 -04:00
|
|
|
|
2014-08-08 11:14:57 -04:00
|
|
|
confirmModal = new ConfirmModal renderData
|
|
|
|
confirmModal.on 'confirm', @recalculateAchievement
|
|
|
|
@openModalView confirmModal
|
|
|
|
|
|
|
|
confirmDeletion: ->
|
|
|
|
renderData =
|
|
|
|
'confirmTitle': 'Are you really sure?'
|
|
|
|
'confirmBody': 'This will completely delete the achievement, potentially breaking a lot of stuff you don\'t want breaking. Are you entirely sure?'
|
|
|
|
'confirmDecline': 'Not really'
|
|
|
|
'confirmConfirm': 'Definitely'
|
|
|
|
|
|
|
|
confirmModal = new ConfirmModal renderData
|
|
|
|
confirmModal.on 'confirm', @deleteAchievement
|
2014-06-08 18:33:06 -04:00
|
|
|
@openModalView confirmModal
|
|
|
|
|
|
|
|
recalculateAchievement: =>
|
|
|
|
$.ajax
|
2014-07-07 06:44:44 -04:00
|
|
|
data: JSON.stringify(earnedAchievements: [@achievement.get('slug') or @achievement.get('_id')])
|
2014-06-08 18:33:06 -04:00
|
|
|
success: (data, status, jqXHR) ->
|
|
|
|
noty
|
|
|
|
timeout: 5000
|
|
|
|
text: 'Recalculation process started'
|
|
|
|
type: 'success'
|
|
|
|
layout: 'topCenter'
|
|
|
|
error: (jqXHR, status, error) ->
|
|
|
|
console.error jqXHR
|
|
|
|
noty
|
|
|
|
timeout: 5000
|
|
|
|
text: "Starting recalculation process failed with error code #{jqXHR.status}"
|
|
|
|
type: 'error'
|
|
|
|
layout: 'topCenter'
|
|
|
|
url: '/admin/earned.achievement/recalculate'
|
|
|
|
type: 'POST'
|
|
|
|
contentType: 'application/json'
|
2014-08-08 11:14:57 -04:00
|
|
|
|
|
|
|
deleteAchievement: =>
|
|
|
|
console.debug 'deleting'
|
|
|
|
$.ajax
|
|
|
|
type: 'DELETE'
|
|
|
|
success: ->
|
|
|
|
noty
|
|
|
|
timeout: 5000
|
|
|
|
text: 'Aaaand it\'s gone.'
|
|
|
|
type: 'success'
|
|
|
|
layout: 'topCenter'
|
|
|
|
_.delay ->
|
|
|
|
app.router.navigate '/editor/achievement', trigger: true
|
|
|
|
, 500
|
|
|
|
error: (jqXHR, status, error) ->
|
|
|
|
console.error jqXHR
|
|
|
|
timeout: 5000
|
|
|
|
text: "Deleting achievement failed with error code #{jqXHR.status}"
|
|
|
|
type: 'error'
|
|
|
|
layout: 'topCenter'
|
|
|
|
url: "/db/achievement/#{@achievement.id}"
|