-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.js
434 lines (389 loc) · 12 KB
/
plugin.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*!
* Aloha Editor
* Author & Copyright (c) 2010 Gentics Software GmbH
* Licensed unter the terms of http://www.aloha-editor.com/license.html
*/
/**
* Register the CropNResize as GENTICS.Aloha.Plugin
*/
GENTICS.Aloha.CropNResize = new GENTICS.Aloha.Plugin('com.gentics.aloha.plugins.CropNResize');
/**
* Configure the available languages
*/
GENTICS.Aloha.CropNResize.languages = ['en', 'de'];
/**
* The image that is currently edited
*/
GENTICS.Aloha.CropNResize.obj = null;
/**
* The Jcrop API reference
* this is needed to be able to destroy the cropping frame later on
* the variable is linked to the api object whilst cropping, or set to null otherwise
* strange, but done as documented http://deepliquid.com/content/Jcrop_API.html
*/
GENTICS.Aloha.CropNResize.jcAPI = null;
/**
* this will contain an image's original properties to be able to undo previous settings
*
* when an image is clicked for the first time, a new object will be added to the array
* {
* obj : [the image object reference],
* src : [the original src url],
* width : [initial width],
* height : [initial height]
* }
*
* when an image is clicked the second time, the array will be checked for the image object
* referenct, to prevent for double entries
*/
GENTICS.Aloha.CropNResize.restoreProps = [];
/**
* resized callback is triggered right after the user finished resizing the image
* @param image jquery image object
*/
GENTICS.Aloha.CropNResize.onResized = function (image) {};
/**
* crop callback is triggered after the user clicked accept to accept his crop
* @param image jquery image object reference
* @param props cropping properties
*/
GENTICS.Aloha.CropNResize.onCropped = function (image, props) {};
/**
* reset callback is triggered before the internal reset procedure is applied
* if this function returns true, then the reset has been handled by the callback
* which means that no other reset will be applied
* if false is returned the internal reset procedure will be applied
* @param image jquery image object reference
* @return true if a reset has been applied, flase otherwise
*/
GENTICS.Aloha.CropNResize.onReset = function (image) { return false; };
/**
* button references
*/
GENTICS.Aloha.CropNResize.cropButton = null;
/**
* internal interval reference
*/
GENTICS.Aloha.CropNResize.interval = null;
/**
* a list of dom object currently attached
*/
GENTICS.Aloha.CropNResize.attachedObjects = [];
/**
* Initialize the plugin, register the buttons
*/
GENTICS.Aloha.CropNResize.init = function() {
var that = this;
// load additional js libs
jQuery('head').append(
'<script type="text/javascript" src="' + GENTICS.Aloha.settings.base + 'plugins/com.gentics.aloha.plugins.CropNResize/js/jquery-ui-1.8.7.custom.min.js"></script>' +
'<link rel="stylesheet" href="' + GENTICS.Aloha.settings.base + 'plugins/com.gentics.aloha.plugins.CropNResize/css/ui-lightness/jquery-ui-1.8.7.custom.css" />' +
'<script type="text/javascript" src="' + GENTICS.Aloha.settings.base + 'plugins/com.gentics.aloha.plugins.CropNResize/js/jquery.Jcrop.min.js"></script>' +
'<link rel="stylesheet" href="' + GENTICS.Aloha.settings.base + 'plugins/com.gentics.aloha.plugins.CropNResize/css/jquery.Jcrop.css" />' +
'<link rel="stylesheet" href="' + GENTICS.Aloha.settings.base + 'plugins/com.gentics.aloha.plugins.CropNResize/css/cropnresize.css" />');
// create image scope
GENTICS.Aloha.FloatingMenu.createScope('GENTICS.Aloha.image', ['GENTICS.Aloha.global']);
/*
* init basic settings like callbacks and options
*/
if (typeof this.settings.onResized == "function") {
this.onResized = this.settings.onResized;
}
if (typeof this.settings.onCropped == "function") {
this.onCropped = this.settings.onCropped;
}
if (typeof this.settings.onReset == "function") {
this.onReset = this.settings.onReset;
}
if (typeof this.settings.aspectRatio != "boolean") {
this.settings.aspectRatio = true;
}
/*
* image cropping stuff goes here
*/
this.cropButton = new GENTICS.Aloha.ui.Button({
'size' : 'small',
'tooltip' : this.i18n('Crop'),
'toggle' : true,
'iconClass' : 'cnr_crop',
'onclick' : function (btn, event) {
if (btn.pressed) {
that.crop();
} else {
that.endCrop();
}
}
});
// add to floating menu
GENTICS.Aloha.FloatingMenu.addButton(
'GENTICS.Aloha.image',
this.cropButton,
this.i18n('floatingmenu.tab.image'),
20
);
/*
* add a reset button
*/
GENTICS.Aloha.FloatingMenu.addButton(
'GENTICS.Aloha.image',
new GENTICS.Aloha.ui.Button({
'size' : 'small',
'tooltip' : this.i18n('Reset'),
'toggle' : false,
'iconClass' : 'cnr_reset',
'onclick' : function (btn, event) {
that.reset();
}
}),
this.i18n('floatingmenu.tab.image'),
30
);
// remove resize handles and cropping status when clicking somewhere else
GENTICS.Aloha.EventRegistry.subscribe(GENTICS.Aloha, 'selectionChanged', function(event, rangeObject, originalEvent) {
if (!jQuery(originalEvent.target).hasClass('ui-resizable-handle')) {
that.endResize();
}
});
// now attach events to images
GENTICS.Aloha.EventRegistry.subscribe(GENTICS.Aloha, 'editableCreated', function(event, editable) {
// attach events to the editable
that.attach();
});
};
/**
* attach to elements using the given filter
* you may call this function subsequently if
* you've added new images to the content
*
* @param selector optional jQuery selector which defines images to be used.
* the default selector is '.GENTICS_editable img', which you may as well
* override by providing the 'selector' option from the settings:
*
* // make all images in #maincontent editable
* GENTICS.Aloha.settings.plugins["com.gentics.aloha.plugins.CropNResize"].selector = '#maincontent img';
*/
GENTICS.Aloha.CropNResize.attach = function(selector) {
// if a selector has been provided we'll stick with this one
if (typeof selector != 'string') {
// check config for another default selector
if (typeof this.settings.selector == 'string') {
selector = this.settings.selector;
} else {
// use default selector
selector = '.GENTICS_editable img';
}
}
var that = this;
// now attach to objects matched by the selector
jQuery(selector).each(function() {
if (!that.isAttached(this)) {
var o = jQuery(this);
// we don't want to resize edit icons
if (o.parent().hasClass('GENTICS_editicon')) {
return true;
}
o.mouseup(function(e) {
that.focus(e);
e.stopPropagation();
});
}
});
};
/**
* check whether we've already attached to a dom object
* @param domobj object to check for
* @return true if were already attached
*/
GENTICS.Aloha.CropNResize.isAttached = function(domobj) {
for (var i=0; i<this.attachedObjects.length; i++) {
if (this.attachedObjects[i] === domobj) {
return true;
}
}
return false;
};
/**
* execute cleanup routine
*/
GENTICS.Aloha.CropNResize.reset = function() {
// TODO implement me
};
/**
* resets the image to it's initial properties
*/
GENTICS.Aloha.CropNResize.reset = function() {
this.endCrop();
this.endResize();
if (this.onReset(this.obj)) {
// the external reset procedure has already performed a reset, so there is no need to apply an internal reset
return;
}
for (var i=0;i<this.restoreProps.length;i++) {
// restore from restoreProps if there is a match
if (this.obj.get(0) === this.restoreProps[i].obj) {
this.obj.attr('src', this.restoreProps[i].src);
this.obj.width(this.restoreProps[i].width);
this.obj.height(this.restoreProps[i].height);
return;
}
}
}
/**
* initialize crop confirm and cancel buttons and move them to the tracker position
*/
GENTICS.Aloha.CropNResize.initCropButtons = function() {
jQuery('body').append(
'<div id="GENTICS_CropNResize_btns">' +
'<button class="cnr_crop_apply" title="' + this.i18n('Accept') +
'" onclick="GENTICS.Aloha.CropNResize.acceptCrop();">✔</button>' +
'<button class="cnr_crop_cancel" title="' + this.i18n('Cancel') +
'" onclick="GENTICS.Aloha.CropNResize.endCrop();">✖</button>' +
'</div>'
);
var btns = jQuery('#GENTICS_CropNResize_btns');
var oldLeft = 0;
var oldTop = 0;
this.interval = setInterval(function () {
var jt = jQuery('.jcrop-tracker:first');
var off = jt.offset();
if (jt.css('height') != '0px' && jt.css('width') != '0px') {
btns.fadeIn('slow');
}
// move the icons to the bottom right side
off.top = parseInt(off.top + jt.height() + 3);
off.left = parseInt(off.left + jt.width() - 55);
// comparison to old values hinders flickering bug in FF
if (oldLeft != off.left || oldTop != off.top) {
btns.offset(off);
}
oldLeft = off.left;
oldTop = off.top;
}, 10);
};
/**
* destroy crop confirm and cancel buttons
*/
GENTICS.Aloha.CropNResize.destroyCropButtons = function () {
jQuery('#GENTICS_CropNResize_btns').remove();
clearInterval(this.interval);
};
/**
* this will be called, when the crop button is pressed, and cropping starts
*/
GENTICS.Aloha.CropNResize.crop = function () {
var that = this;
this.endResize();
this.initCropButtons();
this.jcAPI = jQuery.Jcrop(this.obj, {
onSelect : function () {
// ugly hack to keep scope :(
setTimeout(function () {
GENTICS.Aloha.FloatingMenu.setScope('GENTICS.Aloha.image');
}, 10);
}
});
};
/**
* end cropping
* will toggle buttons accordingly and remove all cropping markup
*/
GENTICS.Aloha.CropNResize.endCrop = function () {
if (this.jcAPI) {
this.jcAPI.destroy();
this.jcAPI = null;
}
this.destroyCropButtons();
this.cropButton.extButton.toggle(false);
this.resize();
};
/**
* accept the current cropping area and apply the crop
*/
GENTICS.Aloha.CropNResize.acceptCrop = function () {
/*
* this.jcAPI.tellSelect()
Object
h: 218
w: 296
x: 45
x2: 341
y: 36
y2: 254
__proto__: Object
*/
this.onCropped(this.obj, this.jcAPI.tellSelect());
this.endCrop();
this.resize();
};
/**
* start resizing
*
* uses a load of jQueryUI.resizable() option, which can be passed through via the plugin's settings object
* aspectRatio
* maxHeight
* minHeight
* maxWidth
* minWidth
* grid
*/
GENTICS.Aloha.CropNResize.resize = function () {
var that = this;
this.obj.resizable({
stop : function (event, ui) {
that.onResized(that.obj);
// this is so ugly, but I could'nt figure out how to do it better...
setTimeout(function () {
GENTICS.Aloha.FloatingMenu.setScope('GENTICS.Aloha.image');
that.done(event);
}, 10);
},
// the rest of the settings is directly set through the plugin settings object
aspectRatio : that.settings.aspectRatio,
maxHeight : that.settings.maxHeight,
minHeight : that.settings.minHeight,
maxWidth : that.settings.maxWidth,
minWidth : that.settings.minWidth,
grid : that.settings.grid
});
};
/**
* end resizing
* will toggle buttons accordingly and remove all markup that has been added for cropping
*/
GENTICS.Aloha.CropNResize.endResize = function () {
if (this.obj) {
this.obj.resizable("destroy");
}
};
/**
* an image has been clicked
*/
GENTICS.Aloha.CropNResize.focus = function (e) {
GENTICS.Aloha.FloatingMenu.setScope('GENTICS.Aloha.image');
this.obj = jQuery(e.target);
this.restoreProps.push({
obj : e.srcElement,
src : this.obj.attr('src'),
width : this.obj.width(),
height : this.obj.height()
});
this.resize(); // init resizing by default
this.updateFM();
};
/**
* this is called when the cropping or resizing process has finished
*/
GENTICS.Aloha.CropNResize.done = function (e) {
this.updateFM();
};
/**
* reposition the floating menu
*/
GENTICS.Aloha.CropNResize.updateFM = function () {
var o = this.obj.offset();
GENTICS.Aloha.FloatingMenu.floatTo({
x : o.left,
y : (o.top - 100)
});
};