Retrospring/app/assets/javascripts/settings.coffee

102 lines
3.6 KiB
CoffeeScript
Raw Normal View History

2014-12-12 10:05:35 -08:00
# see GitHub issue #11
($ document).on "submit", "form#edit_user", (evt) ->
if ($ "input#user_current_password").val().length == 0
evt.preventDefault()
2014-12-29 04:52:19 -08:00
$("button[data-target=#modal-passwd]").trigger 'click'
2015-05-10 22:58:24 -07:00
readImage = (file, callback) ->
fr = new FileReader()
fr.addEventListener "load", (e) ->
callback e.target.result
fr.readAsBinaryString file
freeURL = () ->
if window.URL? or window.webkitURL?
readImage = (file, callback) ->
callback (window.URL || window.webkitURL).createObjectURL file
freeURL = (url) ->
URL.revokeObjectURL url
2014-12-29 04:52:19 -08:00
# Profile pic
($ document).on 'change', 'input#user_profile_picture[type=file]', ->
2015-05-10 22:58:24 -07:00
input = this
2014-12-29 04:52:19 -08:00
2014-12-29 07:29:46 -08:00
($ '#profile-picture-crop-controls').slideUp 400, ->
if input.files and input.files[0]
2015-05-10 22:58:24 -07:00
readImage input.files[0], (src) ->
2014-12-29 07:29:46 -08:00
cropper = ($ '#profile-picture-cropper')
preview = ($ '#profile-picture-preview')
updateVars = (data, action) ->
($ '#crop_x').val Math.floor(data.x / data.scale)
($ '#crop_y').val Math.floor(data.y / data.scale)
($ '#crop_w').val Math.floor(data.w / data.scale)
($ '#crop_h').val Math.floor(data.h / data.scale)
# rx = 100 / data.w
# ry = 100 / data.h
# ($ '#profile-picture-preview').css
# width: Math.round(rx * preview[0].naturalWidth) + 'px'
# height: Math.round(ry * preview[0].naturalHeight) + 'px'
# marginLeft: '-' + Math.round(rx * data.x) + 'px'
# marginTop: '-' + Math.round(ry * data.y) + 'px'
2014-12-29 07:29:46 -08:00
cropper.on 'load', ->
2015-05-10 22:58:24 -07:00
if ({}.toString).call(src) == "[object URL]"
freeURL src
2014-12-29 07:29:46 -08:00
side = if cropper[0].naturalWidth > cropper[0].naturalHeight
cropper[0].naturalHeight
2014-12-29 04:52:19 -08:00
else
2014-12-29 07:29:46 -08:00
cropper[0].naturalWidth
cropper.guillotine
width: side
height: side
onChange: updateVars
updateVars cropper.guillotine('getData'), 'drag' # just because
2014-12-29 07:29:46 -08:00
2015-05-08 09:20:57 -07:00
unless ($ '#profile-picture-crop-controls')[0].dataset.bound?
($ '#cropper-zoom-out').click -> cropper.guillotine 'zoomOut'
($ '#cropper-zoom-in').click -> cropper.guillotine 'zoomIn'
($ '#profile-picture-crop-controls')[0].dataset.bound = true
2014-12-29 04:52:19 -08:00
($ '#profile-picture-crop-controls').slideDown()
2015-05-10 22:58:24 -07:00
cropper.attr 'src', src
2015-05-08 08:44:16 -07:00
($ document).on 'change', 'input#user_profile_header[type=file]', ->
2015-05-10 22:58:24 -07:00
input = this
2015-05-08 08:44:16 -07:00
($ '#profile-header-crop-controls').slideUp 400, ->
if input.files and input.files[0]
2015-05-10 22:58:24 -07:00
readImage input.files[0], (src) ->
2015-05-08 08:44:16 -07:00
cropper = ($ '#profile-header-cropper')
preview = ($ '#profile-header-preview')
updateVars = (data, action) ->
($ '#crop_h_x').val Math.floor(data.x / data.scale)
($ '#crop_h_y').val Math.floor(data.y / data.scale)
($ '#crop_h_w').val Math.floor(data.w / data.scale)
($ '#crop_h_h').val Math.floor(data.h / data.scale)
cropper.on 'load', ->
2015-05-10 22:58:24 -07:00
if ({}.toString).call(src) == "[object URL]"
freeURL src
2015-05-08 08:44:16 -07:00
cropper.guillotine
width: 1500
2015-05-08 08:49:10 -07:00
height: 350
2015-05-08 08:44:16 -07:00
onChange: updateVars
2015-05-08 08:49:10 -07:00
updateVars cropper.guillotine('getData'), 'drag'
2015-05-08 08:44:16 -07:00
2015-05-08 09:20:57 -07:00
unless ($ '#profile-header-crop-controls')[0].dataset.bound?
($ '#cropper-header-zoom-out').click -> cropper.guillotine 'zoomOut'
($ '#cropper-header-zoom-in').click -> cropper.guillotine 'zoomIn'
($ '#profile-header-crop-controls')[0].dataset.bound = true
2015-05-08 08:44:16 -07:00
($ '#profile-header-crop-controls').slideDown()
2015-05-10 22:58:24 -07:00
cropper.attr 'src', src