3 $.widget('wl.pictureviewer', {
6 step: 20, // step in % of initial image
7 plus_button: undefined,
8 minus_button: undefined,
11 ORIGINAL_AVAILABLE: 1,
17 self.element.data('width'),
18 self.element.data('height')
20 self.original_size = [
21 self.element.data('original-width'),
22 self.element.data('original-height')
27 self.original = $('<img src="'+ self.element.attr('data-original-url') +'">');
29 self._original_avaialble = self.ORIGINAL_LOADING;
30 function original_loaded() {
31 self._original_avaialble = self.ORIGINAL_AVAILABLE;
32 self.options.plus_button.removeClass('inactive');
33 self.options.minus_button.removeClass('inactive');
34 console.log("Original image available, sizes initial: " + self.initial_size + ", original: " + self.original_size);
36 self.original.load(original_loaded);
38 self.element.width(self.initial_size[0]);
39 self.element.height(self.initial_size[1]);
41 if (self.options.plus_button)
42 self.options.plus_button.click(
43 function(ev) { self.zoom(1); });
44 if (self.options.minus_button)
45 self.options.minus_button.click(
46 function(ev) { self.zoom(-1); });
48 self._initial_mark = null;
49 function clean_initial_mark() {
50 if (self._initial_mark) {
51 self._initial_mark.remove();
52 self._initial_mark = null;
55 var initial_hash = window.location.hash;
58 if (initial_hash.startsWith('#object-')) {
59 $("[href=#picture-objects]").trigger('click');
60 } else if (initial_hash.startsWith('#theme-')) {
61 $("[href=#picutre-themes]").trigger('click');
63 mk = $("[href=" + initial_hash + "]").eq(0);
64 self._initial_mark = self.createMark({
66 coords: mk.data('coords')
71 self.options.areas_links.hover(function() {
74 var coords = $this.data("coords");
75 this._picture_mark = self.createMark({
76 // label: $this.text(),
80 $(this._picture_mark).remove();
81 this._picture_mark = undefined;
82 }).click(function(ev) {
84 var $mark = self.element.find('.mark').eq(0);
85 var markPos = $mark.offset();
86 markPos = [markPos.left, markPos.top];
87 var markSize = [ $mark.width(), $mark.height() ]
88 var wSize = [ window.innerWidth, window.innerHeight ];
90 markPos[0] + markSize[0]/2 - wSize[0]/2,
91 markPos[1] + markSize[1]/2 - wSize[1]/2
101 currentSize: function() {
102 return [this.element.width(), this.element.height() ];
105 currentZoom: function() { return this._zoom; },
107 initOriginal: function() {
108 if (this._original_avaialble > this.ORIGINAL_LOADING &&
109 this._original_avaialble < this.ORIGINAL_SHOWN) {
110 this.element.css({'background-image': 'url('+ this.original.attr('src')+')', 'background-size': this.initial_size[0]+'px'});
111 this._original_avaialble = this.ORIGINAL_SHOWN;
115 zoom: function(steps) {
117 var t = this._zoom + steps;
118 return this.zoomTo(t);
121 zoomForStep: function(step) {
123 // max_step-1 => max %
124 if (step < 0) step = 0;
125 var zoomperc = 100 + step * this.options.step;
126 if (zoomperc * this.initial_size[0] > this.original_size[0] * 100) {
127 zoomperc = this.original_size[0] * 100 / this.initial_size[0];
132 zoomTo: function(level) {
133 var ratio = this.zoomForStep(level) / 100;
134 var new_width = ratio * this.initial_size[0];
135 var new_height = ratio * this.initial_size[1];
136 var cs = this.currentSize();
137 if (new_width == cs[0])
141 'width': new_width + 'px',
142 'height': new_height + 'px',
143 'background-size': new_width + 'px',
149 this.element.css(target);
150 if (this._initial_mark) {
151 this._initial_mark = this.redrawMark(this._initial_mark);
156 allowedPosition: function(off) {
157 var x = undefined, fix_x = undefined;
158 var y = undefined, fix_y = undefined;
159 var w = this.element.width();
160 var h = this.element.height();
161 var cw = $(window).width();
162 var ch = $(window).height();
163 var off = off || this.element.offset();
190 if (fix_x !== undefined || fix_y !== undefined)
191 return { top: fix_y, left: fix_x };
195 redrawMark: function(mark) {
197 var $newmark = this.createMark($mark.data('mark'));
204 // coords: [x, y, w, h]
206 createMark: function(mark) {
207 if (mark.label === undefined)
209 var $mark = $('<div class="mark"><div class="label">' +
210 mark.label + '</div></div>');
211 var cs = this.currentSize()
212 var ratio = cs[0] / this.original_size[0];
213 var scale = function (v) {
216 if (mark.coords[1][0] < 0 || mark.coords[1][1] < 0) { // whole
217 var s = this.original_size;
218 if (mark.coords[1][0] < 0) mark.coords[1][0] = s[0];
219 if (mark.coords[1][1] < 0) mark.coords[1][1] = s[1];
222 var coords = [[scale(mark.coords[0][0]), scale(mark.coords[0][1])],
223 [scale(mark.coords[1][0]), scale(mark.coords[1][1])]];
224 this.element.append($mark);
225 $mark.width(coords[1][0] - coords[0][0]);
226 $mark.height(coords[1][1] - coords[0][1]);
227 $mark.css({left: coords[0][0], top: coords[0][1]});
229 $mark.data('mark', mark);
236 $(document).ready(function() {
237 $.highlightFade.defaults.speed = 3000;
239 $('#menu .dropdown').each(function() {
241 $($t.attr('href')).hide().insertAfter(this);
244 $(".picture-wrap").pictureviewer({
245 plus_button: $(".button.plus"),
246 minus_button: $(".button.minus"),
247 areas_links: $("#picture-objects a, #picture-themes a"),