Local changes.
[redakcja.git] / redakcja / static / js / lib / jquery / jquery.imgareaselect.js
1 /*
2  * imgAreaSelect jQuery plugin
3  * version 0.9.3
4  *
5  * Copyright (c) 2008-2010 Michal Wojciechowski (odyniec.net)
6  *
7  * Dual licensed under the MIT (MIT-LICENSE.txt)
8  * and GPL (GPL-LICENSE.txt) licenses.
9  *
10  * http://odyniec.net/projects/imgareaselect/
11  *
12  */
13
14 (function($) {
15
16 var abs = Math.abs,
17     max = Math.max,
18     min = Math.min,
19     round = Math.round;
20
21 function div() {
22     return $('<div/>');
23 }
24
25 $.imgAreaSelect = function (img, options) {
26     var
27
28         $img = $(img),
29
30         imgLoaded,
31
32         $box = div(),
33         $area = div(),
34         $border = div().add(div()).add(div()).add(div()),
35         $outer = div().add(div()).add(div()).add(div()),
36         $handles = $([]),
37
38         $areaOpera,
39
40         left, top,
41
42         imgOfs = { left: 0, top: 0 },
43
44         imgWidth, imgHeight,
45
46         $parent,
47
48         parOfs = { left: 0, top: 0 },
49
50         zIndex = 0,
51
52         position = 'absolute',
53
54         startX, startY,
55
56         scaleX, scaleY,
57
58         resizeMargin = 10,
59
60         resize,
61
62         minWidth, minHeight, maxWidth, maxHeight,
63
64         aspectRatio,
65
66         shown,
67
68         x1, y1, x2, y2,
69
70         selection = { x1: 0, y1: 0, x2: 0, y2: 0, width: 0, height: 0 },
71
72         docElem = document.documentElement,
73
74         $p, d, i, o, w, h, adjusted;
75
76     function viewX(x) {
77         return x + imgOfs.left - parOfs.left;
78     }
79
80     function viewY(y) {
81         return y + imgOfs.top - parOfs.top;
82     }
83
84     function selX(x) {
85         return x - imgOfs.left + parOfs.left;
86     }
87
88     function selY(y) {
89         return y - imgOfs.top + parOfs.top;
90     }
91
92     function evX(event) {
93         return event.pageX - parOfs.left;
94     }
95
96     function evY(event) {
97         return event.pageY - parOfs.top;
98     }
99
100     function getSelection(noScale) {
101         var sx = noScale || scaleX, sy = noScale || scaleY;
102
103         return { x1: round(selection.x1 * sx),
104             y1: round(selection.y1 * sy),
105             x2: round(selection.x2 * sx),
106             y2: round(selection.y2 * sy),
107             width: round(selection.x2 * sx) - round(selection.x1 * sx),
108             height: round(selection.y2 * sy) - round(selection.y1 * sy) };
109     }
110
111     function setSelection(x1, y1, x2, y2, noScale) {
112         var sx = noScale || scaleX, sy = noScale || scaleY;
113
114         selection = {
115             x1: round(x1 / sx),
116             y1: round(y1 / sy),
117             x2: round(x2 / sx),
118             y2: round(y2 / sy)
119         };
120
121         selection.width = selection.x2 - selection.x1;
122         selection.height = selection.y2 - selection.y1;
123     }
124
125     function adjust() {
126         if (!$img.width())
127             return;
128
129         imgOfs = { left: round($img.offset().left), top: round($img.offset().top) };
130
131         imgWidth = $img.width();
132         imgHeight = $img.height();
133
134         minWidth = options.minWidth || 0;
135         minHeight = options.minHeight || 0;
136         maxWidth = min(options.maxWidth || 1<<24, imgWidth);
137         maxHeight = min(options.maxHeight || 1<<24, imgHeight);
138
139         if ($().jquery == '1.3.2' && position == 'fixed' &&
140             !docElem['getBoundingClientRect'])
141         {
142             imgOfs.top += max(document.body.scrollTop, docElem.scrollTop);
143             imgOfs.left += max(document.body.scrollLeft, docElem.scrollLeft);
144         }
145
146         parOfs = $.inArray($parent.css('position'), ['absolute', 'relative']) + 1 ?
147             { left: round($parent.offset().left) - $parent.scrollLeft(),
148                 top: round($parent.offset().top) - $parent.scrollTop() } :
149             position == 'fixed' ?
150                 { left: $(document).scrollLeft(), top: $(document).scrollTop() } :
151                 { left: 0, top: 0 };
152
153         left = viewX(0);
154         top = viewY(0);
155
156         if (selection.x2 > imgWidth || selection.y2 > imgHeight)
157             doResize();
158     }
159
160     function update(resetKeyPress) {
161         if (!shown) return;
162
163         $box.css({ left: viewX(selection.x1), top: viewY(selection.y1) })
164             .add($area).width(w = selection.width).height(h = selection.height);
165
166         $area.add($border).add($handles).css({ left: 0, top: 0 });
167
168         $border
169             .width(max(w - $border.outerWidth() + $border.innerWidth(), 0))
170             .height(max(h - $border.outerHeight() + $border.innerHeight(), 0));
171
172         $($outer[0]).css({ left: left, top: top,
173             width: selection.x1, height: imgHeight });
174         $($outer[1]).css({ left: left + selection.x1, top: top,
175             width: w, height: selection.y1 });
176         $($outer[2]).css({ left: left + selection.x2, top: top,
177             width: imgWidth - selection.x2, height: imgHeight });
178         $($outer[3]).css({ left: left + selection.x1, top: top + selection.y2,
179             width: w, height: imgHeight - selection.y2 });
180
181         w -= $handles.outerWidth();
182         h -= $handles.outerHeight();
183
184         switch ($handles.length) {
185         case 8:
186             $($handles[4]).css({ left: w / 2 });
187             $($handles[5]).css({ left: w, top: h / 2 });
188             $($handles[6]).css({ left: w / 2, top: h });
189             $($handles[7]).css({ top: h / 2 });
190         case 4:
191             $handles.slice(1,3).css({ left: w });
192             $handles.slice(2,4).css({ top: h });
193         }
194
195         if (resetKeyPress !== false) {
196             if ($.imgAreaSelect.keyPress != docKeyPress)
197                 $(document).unbind($.imgAreaSelect.keyPress,
198                     $.imgAreaSelect.onKeyPress);
199
200             if (options.keys)
201                 $(document)[$.imgAreaSelect.keyPress](
202                     $.imgAreaSelect.onKeyPress = docKeyPress);
203         }
204
205         if ($.browser.msie && $border.outerWidth() - $border.innerWidth() == 2) {
206             $border.css('margin', 0);
207             setTimeout(function () { $border.css('margin', 'auto'); }, 0);
208         }
209     }
210
211     function doUpdate(resetKeyPress) {
212         adjust();
213         update(resetKeyPress);
214         x1 = viewX(selection.x1); y1 = viewY(selection.y1);
215         x2 = viewX(selection.x2); y2 = viewY(selection.y2);
216     }
217
218     function hide($elem, fn) {
219         options.fadeSpeed ? $elem.fadeOut(options.fadeSpeed, fn) : $elem.hide();
220
221     }
222
223     function areaMouseMove(event) {
224         var x = selX(evX(event)) - selection.x1,
225             y = selY(evY(event)) - selection.y1;
226
227         if (!adjusted) {
228             adjust();
229             adjusted = true;
230
231             $box.one('mouseout', function () { adjusted = false; });
232         }
233
234         resize = '';
235
236         if (options.resizable) {
237             if (y <= resizeMargin)
238                 resize = 'n';
239             else if (y >= selection.height - resizeMargin)
240                 resize = 's';
241             if (x <= resizeMargin)
242                 resize += 'w';
243             else if (x >= selection.width - resizeMargin)
244                 resize += 'e';
245         }
246
247         $box.css('cursor', resize ? resize + '-resize' :
248             options.movable ? 'move' : '');
249         if ($areaOpera)
250             $areaOpera.toggle();
251     }
252
253     function docMouseUp(event) {
254         $('body').css('cursor', '');
255
256         if (options.autoHide || selection.width * selection.height == 0)
257             hide($box.add($outer), function () { $(this).hide(); });
258
259         options.onSelectEnd(img, getSelection());
260
261         $(document).unbind('mousemove', selectingMouseMove);
262         $box.mousemove(areaMouseMove);
263     }
264
265     function areaMouseDown(event) {
266         if (event.which != 1) return false;
267
268         adjust();
269
270         if (resize) {
271             $('body').css('cursor', resize + '-resize');
272
273             x1 = viewX(selection[/w/.test(resize) ? 'x2' : 'x1']);
274             y1 = viewY(selection[/n/.test(resize) ? 'y2' : 'y1']);
275
276             $(document).mousemove(selectingMouseMove)
277                 .one('mouseup', docMouseUp);
278             $box.unbind('mousemove', areaMouseMove);
279         }
280         else if (options.movable) {
281             startX = left + selection.x1 - evX(event);
282             startY = top + selection.y1 - evY(event);
283
284             $box.unbind('mousemove', areaMouseMove);
285
286             $(document).mousemove(movingMouseMove)
287                 .one('mouseup', function () {
288                     options.onSelectEnd(img, getSelection());
289
290                     $(document).unbind('mousemove', movingMouseMove);
291                     $box.mousemove(areaMouseMove);
292                 });
293         }
294         else
295             $img.mousedown(event);
296
297         return false;
298     }
299
300     function fixAspectRatio(xFirst) {
301         if (aspectRatio)
302             if (xFirst) {
303                 x2 = max(left, min(left + imgWidth,
304                     x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1)));
305
306                 y2 = round(max(top, min(top + imgHeight,
307                     y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1))));
308                 x2 = round(x2);
309             }
310             else {
311                 y2 = max(top, min(top + imgHeight,
312                     y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1)));
313                 x2 = round(max(left, min(left + imgWidth,
314                     x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1))));
315                 y2 = round(y2);
316             }
317     }
318
319     function doResize() {
320         x1 = min(x1, left + imgWidth);
321         y1 = min(y1, top + imgHeight);
322
323         if (abs(x2 - x1) < minWidth) {
324             x2 = x1 - minWidth * (x2 < x1 || -1);
325
326             if (x2 < left)
327                 x1 = left + minWidth;
328             else if (x2 > left + imgWidth)
329                 x1 = left + imgWidth - minWidth;
330         }
331
332         if (abs(y2 - y1) < minHeight) {
333             y2 = y1 - minHeight * (y2 < y1 || -1);
334
335             if (y2 < top)
336                 y1 = top + minHeight;
337             else if (y2 > top + imgHeight)
338                 y1 = top + imgHeight - minHeight;
339         }
340
341         x2 = max(left, min(x2, left + imgWidth));
342         y2 = max(top, min(y2, top + imgHeight));
343
344         fixAspectRatio(abs(x2 - x1) < abs(y2 - y1) * aspectRatio);
345
346         if (abs(x2 - x1) > maxWidth) {
347             x2 = x1 - maxWidth * (x2 < x1 || -1);
348             fixAspectRatio();
349         }
350
351         if (abs(y2 - y1) > maxHeight) {
352             y2 = y1 - maxHeight * (y2 < y1 || -1);
353             fixAspectRatio(true);
354         }
355
356         selection = { x1: selX(min(x1, x2)), x2: selX(max(x1, x2)),
357             y1: selY(min(y1, y2)), y2: selY(max(y1, y2)),
358             width: abs(x2 - x1), height: abs(y2 - y1) };
359
360         update();
361
362         options.onSelectChange(img, getSelection());
363     }
364
365     function selectingMouseMove(event) {
366         x2 = resize == '' || /w|e/.test(resize) || aspectRatio ? evX(event) : viewX(selection.x2);
367         y2 = resize == '' || /n|s/.test(resize) || aspectRatio ? evY(event) : viewY(selection.y2);
368
369         doResize();
370
371         return false;
372
373     }
374
375     function doMove(newX1, newY1) {
376         x2 = (x1 = newX1) + selection.width;
377         y2 = (y1 = newY1) + selection.height;
378
379         $.extend(selection, { x1: selX(x1), y1: selY(y1), x2: selX(x2),
380             y2: selY(y2) });
381
382         update();
383
384         options.onSelectChange(img, getSelection());
385     }
386
387     function movingMouseMove(event) {
388         x1 = max(left, min(startX + evX(event), left + imgWidth - selection.width));
389         y1 = max(top, min(startY + evY(event), top + imgHeight - selection.height));
390
391         doMove(x1, y1);
392
393         event.preventDefault();
394
395         return false;
396     }
397
398     function startSelection() {
399         adjust();
400
401         x2 = x1;
402         y2 = y1;
403
404         doResize();
405
406         resize = '';
407
408         if ($outer.is(':not(:visible)'))
409             $box.add($outer).hide().fadeIn(options.fadeSpeed||0);
410
411         shown = true;
412
413         $(document).unbind('mouseup', cancelSelection)
414             .mousemove(selectingMouseMove).one('mouseup', docMouseUp);
415         $box.unbind('mousemove', areaMouseMove);
416
417         options.onSelectStart(img, getSelection());
418     }
419
420     function cancelSelection() {
421         $(document).unbind('mousemove', startSelection);
422         hide($box.add($outer));
423
424         selection = { x1: selX(x1), y1: selY(y1), x2: selX(x1), y2: selY(y1),
425                 width: 0, height: 0 };
426
427         options.onSelectChange(img, getSelection());
428         options.onSelectEnd(img, getSelection());
429     }
430
431     function imgMouseDown(event) {
432         if (event.which != 1 || $outer.is(':animated')) return false;
433
434         adjust();
435         startX = x1 = evX(event);
436         startY = y1 = evY(event);
437
438         $(document).one('mousemove', startSelection)
439             .one('mouseup', cancelSelection);
440
441         return false;
442     }
443
444     function windowResize() {
445         doUpdate(false);
446     }
447
448     function imgLoad() {
449         imgLoaded = true;
450
451         setOptions(options = $.extend({
452             classPrefix: 'imgareaselect',
453             movable: true,
454             resizable: true,
455             parent: 'body',
456             onInit: function () {},
457             onSelectStart: function () {},
458             onSelectChange: function () {},
459             onSelectEnd: function () {}
460         }, options));
461
462         $box.add($outer).css({ visibility: '' });
463
464         if (options.show) {
465             shown = true;
466             adjust();
467             update();
468             $box.add($outer).hide().fadeIn(options.fadeSpeed||0);
469         }
470
471         setTimeout(function () { options.onInit(img, getSelection()); }, 0);
472     }
473
474     var docKeyPress = function(event) {
475         var k = options.keys, d, t, key = event.keyCode;
476
477         d = !isNaN(k.alt) && (event.altKey || event.originalEvent.altKey) ? k.alt :
478             !isNaN(k.ctrl) && event.ctrlKey ? k.ctrl :
479             !isNaN(k.shift) && event.shiftKey ? k.shift :
480             !isNaN(k.arrows) ? k.arrows : 10;
481
482         if (k.arrows == 'resize' || (k.shift == 'resize' && event.shiftKey) ||
483             (k.ctrl == 'resize' && event.ctrlKey) ||
484             (k.alt == 'resize' && (event.altKey || event.originalEvent.altKey)))
485         {
486             switch (key) {
487             case 37:
488                 d = -d;
489             case 39:
490                 t = max(x1, x2);
491                 x1 = min(x1, x2);
492                 x2 = max(t + d, x1);
493                 fixAspectRatio();
494                 break;
495             case 38:
496                 d = -d;
497             case 40:
498                 t = max(y1, y2);
499                 y1 = min(y1, y2);
500                 y2 = max(t + d, y1);
501                 fixAspectRatio(true);
502                 break;
503             default:
504                 return;
505             }
506
507             doResize();
508         }
509         else {
510             x1 = min(x1, x2);
511             y1 = min(y1, y2);
512
513             switch (key) {
514             case 37:
515                 doMove(max(x1 - d, left), y1);
516                 break;
517             case 38:
518                 doMove(x1, max(y1 - d, top));
519                 break;
520             case 39:
521                 doMove(x1 + min(d, imgWidth - selX(x2)), y1);
522                 break;
523             case 40:
524                 doMove(x1, y1 + min(d, imgHeight - selY(y2)));
525                 break;
526             default:
527                 return;
528             }
529         }
530
531         return false;
532     };
533
534     function styleOptions($elem, props) {
535         for (option in props)
536             if (options[option] !== undefined)
537                 $elem.css(props[option], options[option]);
538     }
539
540     function setOptions(newOptions) {
541         if (newOptions.parent)
542             ($parent = $(newOptions.parent)).append($box.add($outer));
543
544         $.extend(options, newOptions);
545
546         adjust();
547
548         if (newOptions.handles != null) {
549             $handles.remove();
550             $handles = $([]);
551
552             i = newOptions.handles ? newOptions.handles == 'corners' ? 4 : 8 : 0;
553
554             while (i--)
555                 $handles = $handles.add(div());
556
557             $handles.addClass(options.classPrefix + '-handle').css({
558                 position: 'absolute',
559                 fontSize: 0,
560                 zIndex: zIndex + 1 || 1
561             });
562
563             if (!parseInt($handles.css('width')) >= 0)
564                 $handles.width(5).height(5);
565
566             if (o = options.borderWidth)
567                 $handles.css({ borderWidth: o, borderStyle: 'solid' });
568
569             styleOptions($handles, { borderColor1: 'border-color',
570                 borderColor2: 'background-color',
571                 borderOpacity: 'opacity' });
572         }
573
574         scaleX = options.imageWidth / imgWidth || 1;
575         scaleY = options.imageHeight / imgHeight || 1;
576
577         if (newOptions.x1 != null) {
578             setSelection(newOptions.x1, newOptions.y1, newOptions.x2,
579                 newOptions.y2);
580             newOptions.show = !newOptions.hide;
581         }
582
583         if (newOptions.keys)
584             options.keys = $.extend({ shift: 1, ctrl: 'resize' },
585                 newOptions.keys);
586
587         $outer.addClass(options.classPrefix + '-outer');
588         $area.addClass(options.classPrefix + '-selection');
589         for (i = 0; i++ < 4;)
590             $($border[i-1]).addClass(options.classPrefix + '-border' + i);
591
592         styleOptions($area, { selectionColor: 'background-color',
593             selectionOpacity: 'opacity' });
594         styleOptions($border, { borderOpacity: 'opacity',
595             borderWidth: 'border-width' });
596         styleOptions($outer, { outerColor: 'background-color',
597             outerOpacity: 'opacity' });
598         if (o = options.borderColor1)
599             $($border[0]).css({ borderStyle: 'solid', borderColor: o });
600         if (o = options.borderColor2)
601             $($border[1]).css({ borderStyle: 'dashed', borderColor: o });
602
603         $box.append($area.add($border).add($handles).add($areaOpera));
604
605         if ($.browser.msie) {
606             if (o = $outer.css('filter').match(/opacity=([0-9]+)/))
607                 $outer.css('opacity', o[1]/100);
608             if (o = $border.css('filter').match(/opacity=([0-9]+)/))
609                 $border.css('opacity', o[1]/100);
610         }
611
612         if (newOptions.hide)
613             hide($box.add($outer));
614         else if (newOptions.show && imgLoaded) {
615             shown = true;
616             $box.add($outer).fadeIn(options.fadeSpeed||0);
617             doUpdate();
618         }
619
620         aspectRatio = (d = (options.aspectRatio || '').split(/:/))[0] / d[1];
621
622         $img.add($outer).unbind('mousedown', imgMouseDown);
623
624         if (options.disable || options.enable === false) {
625             $box.unbind('mousemove', areaMouseMove).unbind('mousedown', areaMouseDown);
626             $(window).unbind('resize', windowResize);
627         }
628         else {
629             if (options.enable || options.disable === false) {
630                 if (options.resizable || options.movable)
631                     $box.mousemove(areaMouseMove).mousedown(areaMouseDown);
632
633                 $(window).resize(windowResize);
634             }
635
636             if (!options.persistent)
637                 $img.add($outer).mousedown(imgMouseDown);
638         }
639
640         options.enable = options.disable = undefined;
641     }
642
643     this.remove = function () {
644         $img.unbind('mousedown', imgMouseDown);
645         $box.add($outer).remove();
646     };
647
648     this.getOptions = function () { return options; };
649
650     this.setOptions = setOptions;
651
652     this.getSelection = getSelection;
653
654     this.setSelection = setSelection;
655
656     this.update = doUpdate;
657
658     $p = $img;
659
660     while ($p.length) {
661         zIndex = max(zIndex,
662             !isNaN($p.css('z-index')) ? $p.css('z-index') : zIndex);
663         if ($p.css('position') == 'fixed')
664             position = 'fixed';
665
666         $p = $p.parent(':not(body)');
667     }
668
669     zIndex = options.zIndex || zIndex;
670
671     if ($.browser.msie)
672         $img.attr('unselectable', 'on');
673
674     $.imgAreaSelect.keyPress = $.browser.msie ||
675         $.browser.safari ? 'keydown' : 'keypress';
676
677     if ($.browser.opera)
678         $areaOpera = div().css({ width: '100%', height: '100%',
679             position: 'absolute', zIndex: zIndex + 2 || 2 });
680
681     $box.add($outer).css({ visibility: 'hidden', position: position,
682         overflow: 'hidden', zIndex: zIndex || '0' });
683     $box.css({ zIndex: zIndex + 2 || 2 });
684     $area.add($border).css({ position: 'absolute', fontSize: 0 });
685
686     img.complete || img.readyState == 'complete' || !$img.is('img') ?
687         imgLoad() : $img.one('load', imgLoad);
688 };
689
690 $.fn.imgAreaSelect = function (options) {
691     options = options || {};
692
693     this.each(function () {
694         if ($(this).data('imgAreaSelect')) {
695             if (options.remove) {
696                 $(this).data('imgAreaSelect').remove();
697                 $(this).removeData('imgAreaSelect');
698             }
699             else
700                 $(this).data('imgAreaSelect').setOptions(options);
701         }
702         else if (!options.remove) {
703             if (options.enable === undefined && options.disable === undefined)
704                 options.enable = true;
705
706             $(this).data('imgAreaSelect', new $.imgAreaSelect(this, options));
707         }
708     });
709
710     if (options.instance)
711         return $(this).data('imgAreaSelect');
712
713     return this;
714 };
715
716 })(jQuery);