Guest User

Untitled

a guest
Sep 13th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3.  
  4. class Bricks
  5.     constructor: (@parent_id, @col_width = 300) ->
  6.         @_ctx = $("#" + parent_id)
  7.         @_ctx.css "position", "relative"
  8.         @_items = []
  9.         @rebuild()
  10.  
  11.     appendElement: (element, width, height) ->
  12.         item = {e:element, w:width, h:height}
  13.         @_items.push item
  14.         @_addItem item
  15.  
  16.     prependElement: (element, width, height) ->
  17.         item = {e:element, w:width, h:height}
  18.         @_items.unshift item
  19.         @rebuild()
  20.  
  21.     removeElement: (index) ->
  22.         @_items.splice index, 1
  23.         @rebuild()
  24.  
  25.     clear: ->
  26.         @_ctx.html ""
  27.         @_items = []
  28.  
  29.     rebuild: ->
  30.         ctx_w = @_ctx.width()
  31.         cols = Math.ceil(ctx_w / @col_width)
  32.         @_col_w = Math.floor(ctx_w / cols)
  33.         @_col_chl = []
  34.         for n in [0..(cols - 1)]
  35.             @_col_chl[n] = 0
  36.         @_ctx.html ""
  37.         for n in @_items
  38.             @_addItem n
  39.  
  40.     _addItem: (item) ->
  41.         rh = Math.ceil(@_col_w * (item.h/item.w))
  42.         x = 0
  43.         y = 100000000
  44.         xi = 0
  45.         for c in @_col_chl
  46.             if c < y
  47.                 y = c
  48.                 xi = x
  49.             x++
  50.         x = @_col_w * xi
  51.         @_col_chl[xi] += rh
  52.  
  53.         item.e.css "position", "absolute"
  54.         item.e.css "width", @_col_w + "px"
  55.         item.e.css "height", rh + "px"
  56.         item.e.css "top", y + "px"
  57.         item.e.css "left", x + "px"
  58.  
  59.         @_ctx.append item.e
Add Comment
Please, Sign In to add comment