<commit_before>RootView = require 'views/core/RootView'
template = require 'templates/editor/poll/poll-edit-view'
Poll = require 'models/Poll'
UserPollsRecord = require 'models/UserPollsRecord'
PollModal = require 'views/play/modal/PollModal'
ConfirmModal = require 'views/editor/modal/ConfirmModal'
PatchesView = require 'views/editor/PatchesView'
errors = require 'core/errors'
app = require 'core/application'

module.exports = class PollEditView extends RootView
  id: 'editor-poll-edit-view'
  template: template

  events:
    'click #save-button': 'savePoll'
    'click #delete-button': 'confirmDeletion'

  constructor: (options, @pollID) ->
    super options
    @loadPoll()
    @loadUserPollsRecord()
    @pushChangesToPreview = _.throttle(@pushChangesToPreview, 500)

  loadPoll: ->
    @poll = new Poll _id: @pollID
    @poll.saveBackups = true
    @supermodel.loadModel @poll

  loadUserPollsRecord: ->
    url = "/db/user.polls.record/-/user/#{me.id}"
    @userPollsRecord = new UserPollsRecord().setURL url
    onRecordSync = ->
      return if @destroyed
      @userPollsRecord.url = -> '/db/user.polls.record/' + @id
    @listenToOnce @userPollsRecord, 'sync', onRecordSync
    @userPollsRecord = @supermodel.loadModel(@userPollsRecord).model
    onRecordSync.call @ if @userPollsRecord.loaded

  onLoaded: ->
    super()
    @buildTreema()
    @listenTo @poll, 'change', =>
      @poll.updateI18NCoverage()
      @treema.set('/', @poll.attributes)

  buildTreema: ->
    return if @treema? or (not @poll.loaded)
    data = $.extend(true, {}, @poll.attributes)
    options =
      data: data
      filePath: "db/poll/#{@poll.get('_id')}"
      schema: Poll.schema
      readOnly: me.get('anonymous')
      callbacks:
        change: => @pushChangesToPreview() unless @hush
    @treema = @$el.find('#poll-treema').treema(options)
    @treema.build()
    @treema.childrenTreemas.answers?.open 1
    @pushChangesToPreview()

  afterRender: ->
    super()
    return unless @supermodel.finished()
    @pushChangesToPreview()
    @patchesView = @insertSubView(new PatchesView(@poll), @$el.find('.patches-view'))
    @patchesView.load()

  pushChangesToPreview: =>
    return unless @treema
    @$el.find('#poll-view').empty()
    for key, value of @treema.data
      @poll.set key, value<commit_msg>#4006 fix for editing poll - admin is required<commit_after>RootView = require 'views/core/RootView'
template = require 'templates/editor/poll/poll-edit-view'
Poll = require 'models/Poll'
UserPollsRecord = require 'models/UserPollsRecord'
PollModal = require 'views/play/modal/PollModal'
ConfirmModal = require 'views/editor/modal/ConfirmModal'
PatchesView = require 'views/editor/PatchesView'
errors = require 'core/errors'
app = require 'core/application'

module.exports = class PollEditView extends RootView
  id: 'editor-poll-edit-view'
  template: template

  events:
    'click #save-button': 'savePoll'
    'click #delete-button': 'confirmDeletion'

  constructor: (options, @pollID) ->
    super options
    @loadPoll()
    @loadUserPollsRecord()
    @pushChangesToPreview = _.throttle(@pushChangesToPreview, 500)

  loadPoll: ->
    @poll = new Poll _id: @pollID
    @poll.saveBackups = true
    @supermodel.loadModel @poll

  loadUserPollsRecord: ->
    url = "/db/user.polls.record/-/user/#{me.id}"
    @userPollsRecord = new UserPollsRecord().setURL url
    onRecordSync = ->
      return if @destroyed
      @userPollsRecord.url = -> '/db/user.polls.record/' + @id
    @listenToOnce @userPollsRecord, 'sync', onRecordSync
    @userPollsRecord = @supermodel.loadModel(@userPollsRecord).model
    onRecordSync.call @ if @userPollsRecord.loaded

  onLoaded: ->
    super()
    @buildTreema()
    @listenTo @poll, 'change', =>
      @poll.updateI18NCoverage()
      @treema.set('/', @poll.attributes)

  buildTreema: ->
    return if @treema? or (not @poll.loaded) or (not me.isAdmin())
    data = $.extend(true, {}, @poll.attributes)
    options =
      data: data
      filePath: "db/poll/#{@poll.get('_id')}"
      schema: Poll.schema
      readOnly: me.get('anonymous')
      callbacks:
        change: => @pushChangesToPreview() unless @hush
    @treema = @$el.find('#poll-treema').treema(options)
    @treema.build()
    @treema.childrenTreemas.answers?.open 1
    @pushChangesToPreview()

  afterRender: ->
    super()
    return unless @supermodel.finished()
    @pushChangesToPreview()
    @patchesView = @insertSubView(new PatchesView(@poll), @$el.find('.patches-view'))
    @patchesView.load()

  pushChangesToPreview: =>
    return unless @treema
    @$el.find('#poll-view').empty()
    for key, value of @treema.data
      @poll.set key, value
{'fraction_non_alphanumeric': 0.14218516142826598, 'fraction_numerical': 0.003420996365191362, 'mean_word_length': 3.631683168316832, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4879707', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1550, 'n_words': 307}