-
Notifications
You must be signed in to change notification settings - Fork 8
/
Ext.ux.ImageCrop.js
119 lines (117 loc) · 2.89 KB
/
Ext.ux.ImageCrop.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Licence:
* You can use the Code as you like, only the URL http//www.thomas-lauria.de/ has to be in the used Files / Code
* @author Thomas Lauria
* http://www.thomas-lauria.de
*/
Ext.ns('Ext.ux');
Ext.define('Ext.ux.ImageCrop', {
requires:['Ext.Img'],
extend: 'Ext.Component',
minWidth: 50,
minHeight: 50,
quadratic: false,
preserveRatio: true,
autoEl: {
tag: 'div',
children: [{
tag: 'div',
cls: 'image-crop-wrapper',
style: {
background: '#ffffff',
opacity: 0.5,
position: 'absolute'
}
}]
},
initComponent: function() {
this.preserveRatio = this.quadratic || this.preserveRatio;
this.callParent(arguments);
},
getResultPosition: function() {
var me = this,
parent = me.getBox(),
img = me.image.getBox(),
res = {
x: (img.x - parent.x),
y: (img.y - parent.y),
width: img.width,
height: img.height
};
me.image.getEl().setStyle({
'background-position':(-res.x)+'px '+(-res.y)+'px'
});
return res;
},
/**
* @return Object
*/
getCropData: function() {
return this.getResultPosition();
},
onRender : function(ct, position){
var me = this,
height = me.height,
width = me.width,
wrap = me.el.down('.image-crop-wrapper'),
dragConf = {
constrain: true,
constrainTo:me.el,
listeners: {
dragstart: function() {
this.image.getEl().setStyle({
'background':'transparent'
});
},
dragend: function() {
var me = this,
res = me.getResultPosition();
me.image.getEl().setStyle({
'background-image': 'url('+me.src+')',
'background-repeat': 'no-repeat'
});
me.fireEvent('changeCrop', me, res);
me.fireEvent('moveCrop', me, res);
},
scope: me
}
};
wrap.setSize(me.width, me.height);
me.el.setStyle({
background: 'url('+me.src+') no-repeat left top'
});
if(me.quadratic){
if(height > width) {
height = width;
}
else {
width = height;
}
}
me.image = Ext.create('Ext.Img',{
opacity: 1.0,
renderTo: me.el,
resizable: {
pinned: true,
preserveRatio: me.preserveRatio
},
draggable: dragConf,
constrainTo: me.el,
src: Ext.BLANK_IMAGE_URL,
height: height,
width: width,
style:{
cursor: 'move',
position: 'absolute',
background: 'url('+me.src+') no-repeat left top'
},
listeners: {
resize: function() {
res = me.getResultPosition();
me.fireEvent('changeCrop', me, res);
me.fireEvent('resizeCrop', me, res);
}
}
});
}
});