aboutsummaryrefslogtreecommitdiff
path: root/js/lib/avatar_cropper.js
diff options
context:
space:
mode:
authorBrett Profitt <brett.profitt@gmail.com>2011-10-23 11:49:27 -0700
committerBrett Profitt <brett.profitt@gmail.com>2011-10-23 11:49:27 -0700
commit1b179e3dd38c98959dd95ee42af14d1a32ab2481 (patch)
tree03b01479a9cf2ca87563f6d14c08dfb3026bdb6e /js/lib/avatar_cropper.js
parent75f971f3f4dc2517cf4ec18fe29f12f3d3e8d30a (diff)
downloadelgg-1b179e3dd38c98959dd95ee42af14d1a32ab2481.tar.gz
elgg-1b179e3dd38c98959dd95ee42af14d1a32ab2481.tar.bz2
Fixes #3914. Pulled avatar code out into its own library.
Diffstat (limited to 'js/lib/avatar_cropper.js')
-rw-r--r--js/lib/avatar_cropper.js50
1 files changed, 50 insertions, 0 deletions
diff --git a/js/lib/avatar_cropper.js b/js/lib/avatar_cropper.js
new file mode 100644
index 000000000..bfd76225b
--- /dev/null
+++ b/js/lib/avatar_cropper.js
@@ -0,0 +1,50 @@
+/**
+ * Avatar cropping
+ */
+
+elgg.provide('elgg.avatarCropper');
+
+/**
+ * Register the avatar cropper.
+ */
+elgg.avatarCropper.init = function() {
+ $('#user-avatar-cropper').imgAreaSelect({
+ selectionOpacity: 0,
+ aspectRatio: '1:1',
+ onSelectEnd: elgg.avatarCropper.selectChange,
+ onSelectChange: elgg.avatarCropper.preview
+ });
+}
+
+/**
+ * Handler for changing select area.
+ */
+elgg.avatarCropper.preview = function(img, selection) {
+ // catch for the first click on the image
+ if (selection.width == 0 || selection.height == 0) {
+ return;
+ }
+
+ var origWidth = $("#user-avatar-cropper").width();
+ var origHeight = $("#user-avatar-cropper").height();
+ var scaleX = 100 / selection.width;
+ var scaleY = 100 / selection.height;
+ $('#user-avatar-preview > img').css({
+ width: Math.round(scaleX * origWidth) + 'px',
+ height: Math.round(scaleY * origHeight) + 'px',
+ marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
+ marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
+ });
+}
+
+/**
+ * Handler for updating the form inputs after select ends
+ */
+elgg.avatarCropper.selectChange = function(img, selection) {
+ $('input[name=x1]').val(selection.x1);
+ $('input[name=x2]').val(selection.x2);
+ $('input[name=y1]').val(selection.y1);
+ $('input[name=y2]').val(selection.y2);
+}
+
+elgg.register_hook_handler('init', 'system', elgg.avatarCropper.init); \ No newline at end of file