Sha256: 283e4c6417f868c049d645cbe725c06cd3567dea8f43b31efd0876e11be7a84f

Contents?: true

Size: 1.82 KB

Versions: 5

Compression:

Stored size: 1.82 KB

Contents

class @Cropper extends Backbone.View

  horizontalAttributes : ['x', 'w']
  verticalAttributes : ['y', 'h']

  className : 'add_new_popup'

  events :
    'click' : 'check'
    'click [data-crop]' : 'crop'
    'click [data-close]' : 'close'

  setOriginalDimensions : (originalDimensions) ->
    @originalDimensions = originalDimensions

  show : (imagePath) ->
    @render(imagePath)
    @initCrop()
    @$el.show()

  render : (imagePath) ->
    @$el.html @_getHTML(imagePath : imagePath)

  initCrop : () ->
    @$cropAttributes = @$el.find('[data-crop-attribute]')
    [originalWidth, originalHeight] = @originalDimensions
    $image = @$el.find('.crop-image')
    $image.on 'load', () =>
      @horizontalRatio = originalWidth / $image.width()
      @verticalRatio = originalHeight / $image.height()
      $image.Jcrop
        onSelect : @_updateCropAttributes

  check : (e) ->
    @closePopup() if $(e.target).closest('.crop').length is 0

  crop : (e) ->
    e.preventDefault()
    @closePopup()
    @trigger 'crop-image', @_getCropData()
    false

  close : (e) ->
    e.preventDefault()
    @closePopup()

  closePopup : () ->
    @$el.hide()

  _getCropData : () ->
    cropData = {}
    @$cropAttributes.each () ->
      $cropAttribute = $(this)
      cropData[$cropAttribute.data('crop-attribute')] = $cropAttribute.val()
    cropData

  _updateCropAttributes : (attributes) =>
    @_scaleAttributes(attributes)
    @$cropAttributes.each () ->
      $cropAttribute = $(this)
      $cropAttribute.val(attributes[$cropAttribute.data('crop-attribute')])

  _scaleAttributes : (attributes) ->
    _.each @horizontalAttributes, (attribute) =>
      attributes[attribute] *= @horizontalRatio
    _.each @verticalAttributes, (attribute) =>
      attributes[attribute] *= @verticalRatio

  _getHTML : (data) ->
    JST['rademade_admin/app/templates/crop'](data)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rademade_admin-0.1.5 app/assets/javascripts/rademade_admin/app/common/cropper.coffee
rademade_admin-0.1.4 app/assets/javascripts/rademade_admin/app/common/cropper.coffee
rademade_admin-0.1.3 app/assets/javascripts/rademade_admin/app/common/cropper.coffee
rademade_admin-0.1.2 app/assets/javascripts/rademade_admin/app/common/cropper.coffee
rademade_admin-0.1.1 app/assets/javascripts/rademade_admin/app/common/cropper.coffee