gitlab-org--gitlab-foss/app/assets/javascripts/gl_crop.js.coffee

149 lines
3.9 KiB
CoffeeScript
Raw Normal View History

2016-03-17 14:44:08 +00:00
class GitLabCrop
2016-03-17 19:30:38 +00:00
constructor: (input, opts = {}) ->
@fileInput = $(input)
# We should rename to avoid spec to fail
# Form will submit the proper input filed with a file using FormData
@fileInput
.attr('name', "#{@fileInput.attr('name')}-trigger")
.attr('id', "#{@fileInput.attr('id')}-trigger")
2016-03-17 14:44:08 +00:00
# Set defaults
{
@exportWidth = 200
@exportHeight = 200
@cropBoxWidth = 200
@cropBoxHeight = 200
2016-03-17 19:59:43 +00:00
@form = @fileInput.parents('form')
2016-03-17 14:44:08 +00:00
2016-03-17 19:59:43 +00:00
# Required params
@filename
@previewImage
@modalCrop
@pickImageEl
@uploadImageBtn
@modalCropImg
2016-03-17 14:44:08 +00:00
} = opts
# Ensure needed elements are jquery objects
2016-03-17 19:59:43 +00:00
# If selector is provided we will convert them to a jQuery Object
2016-03-22 16:45:23 +00:00
@filename = @getElement(@filename)
@previewImage = @getElement(@previewImage)
@pickImageEl = @getElement(@pickImageEl)
2016-03-17 19:59:43 +00:00
# Modal elements usually are outside the @form element
@modalCrop = if _.isString(@modalCrop) then $(@modalCrop) else @modalCrop
2016-03-17 19:59:43 +00:00
@uploadImageBtn = if _.isString(@uploadImageBtn) then $(@uploadImageBtn) else @uploadImageBtn
@modalCropImg = if _.isString(@modalCropImg) then $(@modalCropImg) else @modalCropImg
2016-03-17 14:44:08 +00:00
@cropActionsBtn = @modalCrop.find('[data-method]')
@bindEvents()
2016-03-22 16:45:23 +00:00
getElement: (selector) ->
$(selector, @form)
2016-03-17 14:44:08 +00:00
bindEvents: ->
self = @
@fileInput.on 'change', (e) ->
self.onFileInputChange(e, @)
@pickImageEl.on 'click', @onPickImageClick
@modalCrop.on 'shown.bs.modal', @onModalShow
@modalCrop.on 'hidden.bs.modal', @onModalHide
@uploadImageBtn.on 'click', @onUploadImageBtnClick
@cropActionsBtn.on 'click', (e) ->
btn = @
self.onActionBtnClick(btn)
@croppedImageBlob = null
onPickImageClick: =>
@fileInput.trigger('click')
onModalShow: =>
self = @
2016-03-17 14:44:08 +00:00
@modalCropImg.cropper(
viewMode: 1
center: false
aspectRatio: 1
modal: true
scalable: false
rotatable: false
zoomable: true
dragMode: 'move'
guides: false
zoomOnTouch: false
zoomOnWheel: false
cropBoxMovable: false
cropBoxResizable: false
toggleDragModeOnDblclick: false
built: ->
container = $(@).cropper 'getContainerData'
cropBoxWidth = self.cropBoxWidth;
cropBoxHeight = self.cropBoxHeight;
2016-03-17 14:44:08 +00:00
$(@).cropper('setCropBoxData',
width: cropBoxWidth,
height: cropBoxHeight,
left: (container.width - cropBoxWidth) / 2,
top: (container.height - cropBoxHeight) / 2
)
)
onModalHide: =>
@modalCropImg
.attr('src', '') # Remove attached image
.cropper('destroy') # Destroy cropper instance
onUploadImageBtnClick: (e) =>
e.preventDefault()
@setBlob()
@setPreview()
2016-03-17 14:44:08 +00:00
@modalCrop.modal('hide')
onActionBtnClick: (btn) ->
data = $(btn).data()
if @modalCropImg.data('cropper') && data.method
data = $.extend {}, data
result = @modalCropImg.cropper data.method, data.option
onFileInputChange: (e, input) ->
@readFile(input)
readFile: (input) ->
self = @
reader = new FileReader
reader.onload = ->
self.modalCropImg.attr('src', reader.result)
self.modalCrop.modal('show')
reader.readAsDataURL(input.files[0])
dataURLtoBlob: (dataURL) ->
binary = atob(dataURL.split(',')[1])
array = []
for v, k in binary
array.push(binary.charCodeAt(k))
new Blob([new Uint8Array(array)], type: 'image/png')
setPreview: ->
@previewImage.attr('src', @dataURL)
filename = @fileInput.val().replace(/^.*[\\\/]/, '')
@filename.text(filename)
2016-03-17 14:44:08 +00:00
setBlob: ->
@dataURL = @modalCropImg.cropper('getCroppedCanvas',
width: 200
height: 200
).toDataURL('image/png')
@croppedImageBlob = @dataURLtoBlob(@dataURL)
2016-03-17 14:44:08 +00:00
getBlob: ->
@croppedImageBlob
$.fn.glCrop = (opts) ->
return @.each ->
$(@).data('glcrop', new GitLabCrop(@, opts))