From 0c93698f792f4a7f46be8420df9d9afc592dd987 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Wed, 29 Mar 2017 11:46:46 -0300 Subject: [PATCH] js code to crop uploaded image, moved from users to static folder --- amadeus/static/js/crop.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+), 0 deletions(-) create mode 100644 amadeus/static/js/crop.js diff --git a/amadeus/static/js/crop.js b/amadeus/static/js/crop.js new file mode 100644 index 0000000..13bb437 --- /dev/null +++ b/amadeus/static/js/crop.js @@ -0,0 +1,66 @@ +$(function () { + + /* Script para abrir o modal com a imagem selecionada */ + $("#id_image").change(function () { + if (this.files && this.files[0]) { + var reader = new FileReader(); + reader.onload = function (e) { + $("#image").attr("src", e.target.result); + $("#modalCrop").modal("show"); + } + reader.readAsDataURL(this.files[0]); + } + }); + + /* Scripts da caixa de corte da imagem */ + var $image = $("#image"); + var cropBoxData; + var canvasData; + $("#modalCrop").on("shown.bs.modal", function () { + $image.cropper({ + viewMode: 1, + aspectRatio: 1/1, + minCropBoxWidth: 200, + minCropBoxHeight: 200, + ready: function () { + $image.cropper("setCanvasData", canvasData); + $image.cropper("setCropBoxData", cropBoxData); + } + }); + }).on("hidden.bs.modal", function () { + cropBoxData = $image.cropper("getCropBoxData"); + canvasData = $image.cropper("getCanvasData"); + $image.cropper("destroy"); + }); + + $(".js-zoom-in").click(function () { + $image.cropper("zoom", 0.1); + }); + + $(".js-zoom-out").click(function () { + $image.cropper("zoom", -0.1); + }); + + /* Script para pegar os valores das dimensões e depois fechar o modal */ + $(".js-crop-and-upload").click(function () { + var cropData = $image.cropper("getData"); + $("#id_x").val(cropData["x"]); + $("#id_y").val(cropData["y"]); + $("#id_height").val(cropData["height"]); + $("#id_width").val(cropData["width"]); + $("#modalCrop").modal('hide'); + }); + + /* Script para remover o arquivo enviado caso o usuário clique em cancelar*/ + $("#id_image").on('change', function(){ + console.log(this.value); + }); + + $('#crop_cancel').on('click', function(e){ + var input = $("#id_image"); + var holder = $("#pic_holder"); + input.replaceWith(input.val('').clone(true)); + holder.replaceWith(holder.val('').clone(true)); + }); + +}); -- libgit2 0.21.2