2014-11-28 20:49:41 -05:00
|
|
|
CocoView = require 'views/core/CocoView'
|
2014-08-31 02:04:45 -04:00
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
|
|
|
template = require 'templates/editor/level/level-feedback-view'
|
|
|
|
Level = require 'models/Level'
|
|
|
|
LevelFeedback = require 'models/LevelFeedback'
|
|
|
|
|
|
|
|
class LevelFeedbackCollection extends CocoCollection
|
|
|
|
model: LevelFeedback
|
|
|
|
initialize: (models, options) ->
|
|
|
|
super models, options
|
|
|
|
@url = "/db/level/#{options.level.get('slug')}/all_feedback"
|
|
|
|
|
|
|
|
comparator: (a, b) ->
|
|
|
|
score = 0
|
|
|
|
score -= 9001900190019001 if a.get('creator') is me.id
|
|
|
|
score += 9001900190019001 if b.get('creator') is me.id
|
|
|
|
score -= new Date(a.get 'created')
|
|
|
|
score -= -(new Date(b.get 'created'))
|
|
|
|
score -= 900190019001 if a.get('review')
|
|
|
|
score += 900190019001 if b.get('review')
|
|
|
|
if score < 0 then -1 else (if score > 0 then 1 else 0)
|
|
|
|
|
|
|
|
module.exports = class LevelFeedbackView extends CocoView
|
|
|
|
id: 'level-feedback-view'
|
|
|
|
template: template
|
|
|
|
className: 'tab-pane'
|
|
|
|
|
|
|
|
subscriptions:
|
|
|
|
'editor:view-switched': 'onViewSwitched'
|
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
|
|
|
|
|
|
|
getRenderData: (context={}) ->
|
|
|
|
context = super(context)
|
2014-09-01 16:51:30 -04:00
|
|
|
context.moment = moment
|
|
|
|
context.allFeedback = []
|
|
|
|
context.averageRating = 0
|
|
|
|
context.totalRatings = 0
|
2014-09-02 19:48:16 -04:00
|
|
|
if @allFeedback?.models.length
|
2014-08-31 02:04:45 -04:00
|
|
|
context.allFeedback = (m.attributes for m in @allFeedback.models when @allFeedback.models.length < 20 or m.get('review'))
|
2014-09-01 16:51:30 -04:00
|
|
|
context.averageRating = _.reduce((m.get('rating') for m in @allFeedback.models), (acc, x) -> acc + (x ? 5)) / (@allFeedback.models.length)
|
2014-08-31 02:04:45 -04:00
|
|
|
context.totalRatings = @allFeedback.models.length
|
|
|
|
else
|
|
|
|
context.loading = true
|
|
|
|
context
|
|
|
|
|
|
|
|
onViewSwitched: (e) ->
|
|
|
|
# Lazily load.
|
|
|
|
return unless e.targetURL is '#level-feedback-view'
|
|
|
|
unless @allFeedback
|
|
|
|
@allFeedback = @supermodel.loadCollection(new LevelFeedbackCollection(null, level: @options.level), 'feedback').model
|
|
|
|
@render()
|