-
Notifications
You must be signed in to change notification settings - Fork 1
/
imagefield_crop.js
78 lines (70 loc) · 3.29 KB
/
imagefield_crop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* $Id: imagefield_crop.js,v 1.1.2.3.2.10 2009/11/11 08:12:53 yhager Exp $ */
(function ($) {
Drupal.behaviors.imagefield_crop = {
attach: function (context, settings) {
// wait till 'fadeIn' effect ends (defined in filefield_widget.inc)
setTimeout(attachJcrop, 1000, context);
//attachJcrop(context);
function attachJcrop(context) {
if ($('.cropbox', context).length == 0) {
// no cropbox, probably an image upload (http://drupal.org/node/366296)
return;
}
// add Jcrop exactly once to each cropbox
$('.cropbox', context).once(function() {
var self = $(this);
//alert("found a cropbox" + self.attr('id'));
// get the id attribute for multiple image support
var self_id = self.attr('id');
var id = self_id.substring(0, self_id.indexOf('-cropbox'));
// get the name attribute for imagefield name
var widget = self.parent().parent();
if ($(".edit-image-crop-changed", widget).val() == 1) {
$('.preview-existing', widget).css({display: 'none'});
$('.jcrop-preview', widget).css({display: 'block'});
}
$(this).Jcrop({
onChange: function(c) {
$('.preview-existing', widget).css({display: 'none'});
var preview = $('.imagefield-crop-preview', widget);
// skip newly added blank fields
if (undefined == settings.imagefield_crop[id].preview) {
return;
}
var rx = settings.imagefield_crop[id].preview.width / c.w;
var ry = settings.imagefield_crop[id].preview.height / c.h;
$('.jcrop-preview', preview).css({
width: Math.round(rx * settings.imagefield_crop[id].preview.orig_width) + 'px',
height: Math.round(ry * settings.imagefield_crop[id].preview.orig_height) + 'px',
marginLeft: '-' + Math.round(rx * c.x) + 'px',
marginTop: '-' + Math.round(ry * c.y) + 'px',
display: 'block'
});
},
onSelect: function(c) {
$('.preview-existing', widget).css({display: 'none'});
$(".edit-image-crop-x", widget).val(c.x);
$(".edit-image-crop-y", widget).val(c.y);
if (c.w) $(".edit-image-crop-width", widget).val(c.w);
if (c.h) $(".edit-image-crop-height", widget).val(c.h);
$(".edit-image-crop-changed", widget).val(1);
},
aspectRatio: settings.imagefield_crop[id].box.ratio,
boxWidth: settings.imagefield_crop[id].box.box_width,
boxHeight: settings.imagefield_crop[id].box.box_height,
minSize: [Drupal.settings.imagefield_crop[id].minimum.width, Drupal.settings.imagefield_crop[id].minimum.height],
/*
* Setting the select here calls onChange event, and we lose the original image visibility
*/
setSelect: [
parseInt($(".edit-image-crop-x", widget).val()),
parseInt($(".edit-image-crop-y", widget).val()),
parseInt($(".edit-image-crop-width", widget).val()) + parseInt($(".edit-image-crop-x", widget).val()),
parseInt($(".edit-image-crop-height", widget).val()) + parseInt($(".edit-image-crop-y", widget).val())
]
});
});
};
}
};
})(jQuery);