<commit_before>Backbone = require "backbone"
_ = require 'underscore'
sd = require("sharify").data
mediator = require '../../../lib/mediator.coffee'
BlockView = require '../../../components/block_collection/client/block_view.coffee'

module.exports = class BlockSkeletonView extends Backbone.View

  initialize: (options)->
    @channel = options.channel if options?.channel

    @collection.on "add", @appendBlock, @
    @collection.on "created:block", @appendBlock, @
    @collection.on "placeholder:added", @appendBlock, @
    @collection.on "merge:skeleton", @renderSkeleton, @
    @collection.on "placeholders:replaced", @completeRequest, @

    @collection.loadSkeleton()

    super

  appendBlock: (model)->
    containerMethod = if model?.options?.wait is true then 'after' else 'append'
    new BlockView
      container: @$el
      model: model
      containerMethod: containerMethod

  renderSkeleton: ->
    @queue = []
    @pages = []
    @addWaypoints()

  addWaypoints: ->
    sel = '.grid__block'
    @$("#{sel}:nth-child(#{@collection.options.per}n)").not('.grid__block--new-block').each (i, el)=>
      unless $(el).is(@$("#{sel}:last"))
        @addPage i+2
        $(el).after "<span id='page-#{i+2}' class='pagemarker' data-page='#{i+2}'></span>"

    $("<span class='pagemarker' data-page='last'></span>").insertAfter(@$("#{sel}:last"))


    # init waypoints (for top and bottom)
    that = this
    $('.pagemarker').waypoint
      handler: (direction) -> that.checkWaypoint this, direction

    $('.pagemarker').waypoint<commit_msg>creates block but doesnt update yet<commit_after>Backbone = require "backbone"
_ = require 'underscore'
sd = require("sharify").data
mediator = require '../../../lib/mediator.coffee'
Block = require '../../../models/block.coffee'
BlockView = require '../../../components/block_collection/client/block_view.coffee'

module.exports = class BlockSkeletonView extends Backbone.View

  initialize: (options)->
    @channel = options.channel if options?.channel

    @collection.on "add", @appendBlock, @
    @collection.on "created:block", @appendBlock, @
    @collection.on "placeholder:added", @appendBlock, @
    @collection.on "merge:skeleton", @renderSkeleton, @
    @collection.on "placeholders:replaced", @completeRequest, @

    mediator.on 'upload:done', @makeBlock, @

    @collection.loadSkeleton()

    super

  makeBlock: (location) ->
    console.log 'makeBlock'
    block = new Block block_type: "Block", source: location
    @collection.create block.toJSON(),
      url: "#{sd.API_URL}/channels/#{@channel.get('slug')}/blocks"
      wait: true

  appendBlock: (model)->
    containerMethod = if model?.options?.wait is true then 'after' else 'append'
    new BlockView
      container: @$el
      model: model
      containerMethod: containerMethod

  renderSkeleton: ->
    @queue = []
    @pages = []
    @addWaypoints()

  addWaypoints: ->
    sel = '.grid__block'
    @$("#{sel}:nth-child(#{@collection.options.per}n)").not('.grid__block--new-block').each (i, el)=>
      unless $(el).is(@$("#{sel}:last"))
        @addPage i+2
        $(el).after "<span id='page-#{i+2}' class='pagemarker' data-page='#{i+2}'></span>"

    $("<span class='pagemarker' data-page='last'></span>").insertAfter(@$("#{sel}:last"))


    # init waypoints (for top and bottom)
    that = this
    $('.pagemarker').waypoint
      handler: (direction) -> that.checkWaypoint this, direction

    $('.pagemarker').waypoint
{'fraction_non_alphanumeric': 0.18118869013271782, 'fraction_numerical': 0.0017311021350259665, 'mean_word_length': 3.6350267379679146, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 24, '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': '3009171', 'n_tokens_mistral': 1117, 'n_tokens_neox': 1077, 'n_words': 221}