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 objectPrefix = '#object-',
59 themePrefix = '#theme-';
60 if (initial_hash.substr(0, objectPrefix.length) === objectPrefix) {
61 $("[href=#picture-objects]").trigger('click');
62 } else if (initial_hash.substr(0, themePrefix.length) === themePrefix) {
63 $("[href=#picture-themes]").trigger('click');
65 mk = $("[href=" + initial_hash + "]").eq(0);
66 self._initial_mark = self.createMark({
68 coords: mk.data('coords')
73 self.options.areas_links.hover(function() {
76 var coords = $this.data("coords");
77 this._picture_mark = self.createMark({
78 // label: $this.text(),
82 $(this._picture_mark).remove();
83 this._picture_mark = undefined;
84 }).click(function(ev) {
86 var $mark = self.element.find('.mark').eq(0);
87 var markPos = $mark.offset();
88 markPos = [markPos.left, markPos.top];
89 var markSize = [ $mark.width(), $mark.height() ]
90 var wSize = [ window.innerWidth, window.innerHeight ];
92 markPos[0] + markSize[0]/2 - wSize[0]/2,
93 markPos[1] + markSize[1]/2 - wSize[1]/2
103 currentSize: function() {
104 return [this.element.width(), this.element.height() ];
107 currentZoom: function() { return this._zoom; },
109 initOriginal: function() {
110 if (this._original_avaialble > this.ORIGINAL_LOADING &&
111 this._original_avaialble < this.ORIGINAL_SHOWN) {
112 this.element.css({'background-image': 'url('+ this.original.attr('src')+')', 'background-size': this.initial_size[0]+'px'});
113 this._original_avaialble = this.ORIGINAL_SHOWN;
117 zoom: function(steps) {
119 var t = this._zoom + steps;
120 return this.zoomTo(t);
123 zoomForStep: function(step) {
125 // max_step-1 => max %
126 if (step < 0) step = 0;
127 var zoomperc = 100 + step * this.options.step;
128 if (zoomperc * this.initial_size[0] > this.original_size[0] * 100) {
129 zoomperc = this.original_size[0] * 100 / this.initial_size[0];
134 zoomTo: function(level) {
135 var ratio = this.zoomForStep(level) / 100;
136 var new_width = ratio * this.initial_size[0];
137 var new_height = ratio * this.initial_size[1];
138 var cs = this.currentSize();
139 if (new_width == cs[0])
143 'width': new_width + 'px',
144 'height': new_height + 'px',
145 'background-size': new_width + 'px',
151 this.element.css(target);
152 if (this._initial_mark) {
153 this._initial_mark = this.redrawMark(this._initial_mark);
158 allowedPosition: function(off) {
159 var x = undefined, fix_x = undefined;
160 var y = undefined, fix_y = undefined;
161 var w = this.element.width();
162 var h = this.element.height();
163 var cw = $(window).width();
164 var ch = $(window).height();
165 var off = off || this.element.offset();
192 if (fix_x !== undefined || fix_y !== undefined)
193 return { top: fix_y, left: fix_x };
197 redrawMark: function(mark) {
199 var $newmark = this.createMark($mark.data('mark'));
206 // coords: [x, y, w, h]
208 createMark: function(mark) {
209 if (mark.label === undefined)
211 var $mark = $('<div class="mark"><div class="label">' +
212 mark.label + '</div></div>');
213 var cs = this.currentSize()
214 var ratio = cs[0] / this.original_size[0];
215 var scale = function (v) {
218 if (mark.coords[1][0] < 0 || mark.coords[1][1] < 0) { // whole
219 var s = this.original_size;
220 if (mark.coords[1][0] < 0) mark.coords[1][0] = s[0];
221 if (mark.coords[1][1] < 0) mark.coords[1][1] = s[1];
224 var coords = [[scale(mark.coords[0][0]), scale(mark.coords[0][1])],
225 [scale(mark.coords[1][0]), scale(mark.coords[1][1])]];
226 this.element.append($mark);
227 $mark.width(coords[1][0] - coords[0][0]);
228 $mark.height(coords[1][1] - coords[0][1]);
229 $mark.css({left: coords[0][0], top: coords[0][1]});
231 $mark.data('mark', mark);
237 $(document).ready(function() {
238 $.highlightFade.defaults.speed = 3000;
240 $('#menu .dropdown').each(function() {
242 $($t.attr('href')).hide().insertAfter(this);
245 $(".picture-wrap").pictureviewer({
246 plus_button: $(".button.plus"),
247 minus_button: $(".button.minus"),
248 areas_links: $("#picture-objects a, #picture-themes a"),