aboutsummaryrefslogtreecommitdiffstats
path: root/doc/phpdoc/js/jquery.panzoom.js
blob: 33569c52072734400ae8075fc14ac35dae943740 (plain)
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
 * jQuery PanZoom Plugin
 * Pan and zoom an image within a parent div.
 *
 * version: 0.9.0
 * @requires jQuery v1.4.2 or later (earlier probably work, but untested so far)
 *
 * Copyright (c) 2011 Ben Lumley
 * Examples and documentation at: https://github.com/benlumley/jQuery-PanZoom
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
*/

(function( $ ){

  $.fn.panZoom = function(method) {

    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist' );
    }

  };

	$.fn.panZoom.defaults = {
      zoomIn   					: 	false,
      zoomOut 					: 	false,
		  panUp							:		false,
		  panDown						:		false,
		  panLeft						:		false,
		  panRight					:		false,
			fit								: 	false,
		  out_x1						:		false,
		  out_y1						:		false,
		  out_x2						:		false,
		  out_y2						:		false,
			min_width					:   20,
			min_height 				:   20,
			zoom_step					:   3,
			pan_step  				:   3,
			debug							: 	false,
			directedit				:   false,
      aspect    				:   true,
      factor    				:   1,
		  animate   				:   true,
			animate_duration	: 	200,
		 	animate_easing		: 	'linear',
			double_click	 		: 	true,
			mousewheel				: 	true,
			mousewheel_delta	: 	1,
			draggable					:   true,
			clickandhold			: 	true
  };

	var settings = {}

  var methods = {
		'init': function (options) {
			$.extend(settings, $.fn.panZoom.defaults, options);
  		setupCSS.apply(this);
			setupData.apply(this);
			setupBindings.apply(this);
			methods.readPosition.apply(this);
		},

		'destroy': function () {
			$(window).unbind('.panZoom');
			this.removeData('panZoom');
		},

		'loadImage': function () {
			var data = this.data('panZoom');
      loadTargetDimensions.apply(this);
			methods.updatePosition.apply(this);
			if (data.last_image != null && data.last_image != this.attr('src')) {
        methods.fit.apply(this);
			}
			data.last_image = this.attr('src');
			data.loaded = true;
		},

	  'readPosition': function () {
				var data = this.data('panZoom');
		 		if (settings.out_x1) { data.position.x1 = settings.out_x1.val()*settings.factor }
		 		if (settings.out_y1) { data.position.y1 = settings.out_y1.val()*settings.factor }
		 		if (settings.out_x2) { data.position.x2 = settings.out_x2.val()*settings.factor }
		 		if (settings.out_y2) { data.position.y2 = settings.out_y2.val()*settings.factor }
				methods.updatePosition.apply(this);
		 },

		'updatePosition': function() {
			validatePosition.apply(this);
			writePosition.apply(this);
			applyPosition.apply(this);
		},

	  'fit': function () {
			var data = this.data('panZoom');
			data.position.x1 = 0;
			data.position.y1 = 0;
			data.position.x2 = data.viewport_dimensions.x;
			data.position.y2 = data.viewport_dimensions.y;
			methods.updatePosition.apply(this);
		},

		'zoomIn': function (steps) {
			var data = this.data('panZoom');
			if (typeof(steps) == 'undefined') {
				var steps = getStepDimensions.apply(this);
			}
            console.debug(data.position);
			console.debug(data.viewport_dimensions);
			data.position.x1 = data.position.x1*1 - steps.zoom.x;
			data.position.x2 = data.position.x2*1 + steps.zoom.x;
			data.position.y1 = data.position.y1*1 - steps.zoom.y;
			data.position.y2 = data.position.y2*1 + steps.zoom.y;
			methods.updatePosition.apply(this);
		 },

		'zoomOut': function (steps) {
			var data = this.data('panZoom');
			if (typeof(steps) == 'undefined') {
				var steps = getStepDimensions.apply(this);
			}
			data.position.x1 = data.position.x1*1 + steps.zoom.x;
			data.position.x2 = data.position.x2*1 - steps.zoom.x;
			data.position.y1 = data.position.y1*1 + steps.zoom.y;
			data.position.y2 = data.position.y2*1 - steps.zoom.y;
			methods.updatePosition.apply(this);
		 },

		'panUp': function () {
			var data = this.data('panZoom');
			var steps = getStepDimensions.apply(this);
			data.position.y1 -= steps.pan.y;
			data.position.y2 -= steps.pan.y;
			methods.updatePosition.apply(this);
		},

		'panDown': function () {
			var data = this.data('panZoom');
			var steps = getStepDimensions.apply(this);
			data.position.y1 = data.position.y1*1 + steps.pan.y;
			data.position.y2 = data.position.y2*1 + steps.pan.y;
			methods.updatePosition.apply(this);
		},

		'panLeft': function () {
			var data = this.data('panZoom');
			var steps = getStepDimensions.apply(this);
			data.position.x1 -= steps.pan.x;
			data.position.x2 -= steps.pan.x;
			methods.updatePosition.apply(this);
		},

		'panRight': function () {
			var data = this.data('panZoom');
			var steps = getStepDimensions.apply(this);
			data.position.x1 = data.position.x1*1 + steps.pan.x;
			data.position.x2 = data.position.x2*1 + steps.pan.x;
			methods.updatePosition.apply(this);
		},

		'mouseWheel': function (delta) {
			// first calculate how much to zoom in/out
			var steps = getStepDimensions.apply(this);
			steps.zoom.x = steps.zoom.x * (Math.abs(delta) / settings.mousewheel_delta);
			steps.zoom.y = steps.zoom.y * (Math.abs(delta) / settings.mousewheel_delta);

			// then do it
			if (delta > 0) {
				methods.zoomIn.apply(this, [steps]);
			} else if (delta < 0) {
				methods.zoomOut.apply(this, [steps]);
			}
		},

		'dragComplete': function() {
			var data = this.data('panZoom');
			data.position.x1 = this.position().left;
			data.position.y1 = this.position().top;
			data.position.x2 = this.position().left*1 + this.width();
			data.position.y2 = this.position().top*1 + this.height();
			methods.updatePosition.apply(this);
		},

		'mouseDown': function (action) {
			methods[action].apply(this);

			if (settings.clickandhold) {
				var data = this.data('panZoom');
				methods.mouseUp.apply(this);
				data.mousedown_interval = window.setInterval(function (that, action) {
					that.panZoom(action);
				}, settings.animate_duration, this, action);
			}
		},

		'mouseUp': function() {
			var data = this.data('panZoom');
			window.clearInterval(data.mousedown_interval);
		}

  }

	function setupBindings() {

		eventData = { target: this }

		// bind up controls
		if (settings.zoomIn) {
			settings.zoomIn.bind('mousedown.panZoom', eventData, function(event) {
				event.preventDefault(); event.data.target.panZoom('mouseDown', 'zoomIn');
			}).bind('mouseleave.panZoom mouseup.panZoom', eventData, function(event) {
				event.preventDefault(); event.data.target.panZoom('mouseUp');
			});
		}

		if (settings.zoomOut) {
			settings.zoomOut.bind('mousedown.panZoom', eventData, function(event) {
				event.preventDefault(); event.data.target.panZoom('mouseDown', 'zoomOut');
			}).bind('mouseleave.panZoom mouseup.panZoom', eventData, function(event) {
				event.preventDefault(); event.data.target.panZoom('mouseUp');
			});
		}

		if (settings.panUp) {
			settings.panUp.bind('mousedown.panZoom', eventData, function(event) {
				event.preventDefault(); event.data.target.panZoom('mouseDown', 'panUp');
			}).bind('mouseleave.panZoom mouseup.panZoom', eventData, function(event) {
				event.preventDefault(); event.data.target.panZoom('mouseUp');
			});
		}

		if (settings.panDown) {
			settings.panDown.bind('mousedown.panZoom', eventData, function(event) {
				event.preventDefault(); event.data.target.panZoom('mouseDown', 'panDown');
			}).bind('mouseleave.panZoom mouseup.panZoom', eventData, function(event) {
				event.preventDefault(); event.data.target.panZoom('mouseUp');
			});
		}

		if (settings.panLeft) {
			settings.panLeft.bind('mousedown.panZoom', eventData, function(event) {
				event.preventDefault(); event.data.target.panZoom('mouseDown', 'panLeft');
			}).bind('mouseleave.panZoom mouseup.panZoom', eventData, function(event) {
				event.preventDefault(); event.data.target.panZoom('mouseUp');
			});
		}

		if (settings.panRight) {
			settings.panRight.bind('mousedown.panZoom', eventData, function(event) {
				event.preventDefault(); event.data.target.panZoom('mouseDown', 'panRight');
			}).bind('mouseleave.panZoom mouseup.panZoom', eventData, function(event) {
				event.preventDefault(); event.data.target.panZoom('mouseUp');
			});
		}

		if (settings.fit) { settings.fit.bind('click.panZoom', eventData, function(event) { event.preventDefault(); event.data.target.panZoom('fit'); } ); }

		// double click
		if (settings.double_click) {
			this.bind('dblclick.panZoom', eventData, function(event, delta) { event.data.target.panZoom('zoomIn') } );
		}

		// mousewheel
		if (settings.mousewheel && typeof(this.mousewheel) == 'function') {
			this.parent().mousewheel(function(event, delta) { event.preventDefault(); $(this).find('img').panZoom('mouseWheel', delta) } );
		} else if (settings.mousewheel) {
			alert('Mousewheel requires mousewheel from jQuery tools - please include jQuery tools or disable mousewheel to remove this warning.')
		}

		// direct form input
		if (settings.directedit) {
			$(settings.out_x1).add(settings.out_y1).add(settings.out_x2).add(settings.out_y2).bind('change.panZoom blur.panZoom', eventData, function(event) { event.data.target.panZoom('readPosition') } );
		}

		if (settings.draggable && typeof(this.draggable) == 'function') {
			this.draggable({
				stop: function () { $(this).panZoom('dragComplete');	}
			});
		}	else if (settings.draggable) {
				alert('Draggable requires jQuery UI - please include jQuery UI or disable draggable to remove this warning.')
		}

		// image load
		$(this).bind('load.panZoom', eventData, function (event) { event.data.target.panZoom('loadImage') })

	}

	function setupData() {
		this.data('panZoom', {
			target_element: this,
			target_dimensions: { x: null, y: null },
			viewport_element: this.parent(),
			viewport_dimensions: { x: this.parent().width(), y: this.parent().height() },
			position: { x1: null, y1: null, x2: null, y2: null },
			last_image: null,
			loaded: false,
			mousewheel_delta: 0,
			mousedown_interval: false
		});
		if (settings.debug) {
			console.log(this.data('panZoom'));
		}
	}

	function setupCSS() {
		if (this.parent().css('position') == 'static') {
			this.parent().css('position', 'relative');
		}
		this.css({
			'position': 'absolute',
			'top': 0,
			'left': 0
		});
		if (settings.draggable) {
			this.css({
				'cursor': 'move'
			});
		}
	}

	function validatePosition() {
		var data = this.data('panZoom');
		// if dimensions are too small...
		if ( data.position.x2 - data.position.x1 < settings.min_width/settings.factor || data.position.y2 - data.position.y1 < settings.min_height/settings.factor ) {
			// and second co-ords are zero (IE: no dims set), fit image
			if (data.position.x2 == 0 || data.position.y2 == 0) {
				methods.fit.apply(this);
			}
			// otherwise, backout a bit
			else {
				if (data.position.x2 - data.position.x1 < settings.min_width/settings.factor) {
					data.position.x2 = data.position.x1*1+settings.min_width/settings.factor;
				}
				if (data.position.y2 - data.position.y1 < settings.min_height/settings.factor) {
	  			data.position.y2 = data.position.y1*1+settings.min_height/settings.factor;
				}
			}
		}

		if (settings.aspect) {
			target = data.target_dimensions.ratio;
			current = getCurrentAspectRatio.apply(this)
			if (current > target) {
				new_width = getHeight.apply(this) * target;
				diff = getWidth.apply(this) - new_width;
				data.position.x1 = data.position.x1*1 + (diff/2);
				data.position.x2 = data.position.x2*1 - (diff/2);
			} else if (current < target) {
				new_height = getWidth.apply(this) / target;
				diff = getHeight.apply(this) - new_height;
				data.position.y1 = data.position.y1*1 + (diff/2);
				data.position.y2 = data.position.y2*1 - (diff/2);
			}
		}


	}

  function applyPosition() {
		var data = this.data('panZoom');

    width = getWidth.apply(this);
    height = getHeight.apply(this);
    left_offset = getLeftOffset.apply(this);
    top_offset = getTopOffset.apply(this);

		properties = {
			'top': Math.round(top_offset),
			'left': Math.round(left_offset),
			'width': Math.round(width),
			'height': Math.round(height)
		}

		if (data.loaded && settings.animate) {
			applyAnimate.apply(this, [ properties ]);
		} else {
			applyCSS.apply(this, [ properties ]);
		}

		if (settings.debug) {
			console.log('--');
			console.log('width:' + width);
			console.log('height:' + height);
			console.log('left:' + left_offset);
			console.log('top:' + top_offset);
		}
	}

	function applyCSS() {
		this.css(	properties );
	}

	function applyAnimate() {
		this.stop().animate(	properties , settings.animate_duration, settings.animate_easing);
	}

  function getWidth() {
		var data = this.data('panZoom');
    width = (data.position.x2 - data.position.x1);
    return width;
  }

  function getLeftOffset() {
		var data = this.data('panZoom');
    return data.position.x1;
  }

  function getHeight() {
		var data = this.data('panZoom');
		height = (data.position.y2 - data.position.y1);
    return height;
  }

  function getTopOffset() {
		var data = this.data('panZoom');
		top_offset = data.position.y1;
    return top_offset;
  }

	function getCurrentAspectRatio() {
		return (getWidth.apply(this) / getHeight.apply(this));
	}

	function writePosition() {
		var data = this.data('panZoom');
 		if (settings.out_x1) { settings.out_x1.val(Math.round(data.position.x1 / settings.factor)) }
 		if (settings.out_y1) { settings.out_y1.val(Math.round(data.position.y1 / settings.factor)) }
 		if (settings.out_x2) { settings.out_x2.val(Math.round(data.position.x2 / settings.factor)) }
 		if (settings.out_y2) { settings.out_y2.val(Math.round(data.position.y2 / settings.factor)) }
	}

	function getStepDimensions() {
		var data = this.data('panZoom');
		ret = {
			zoom: {
				x: (settings.zoom_step/100 * data.viewport_dimensions.x),
				y: (settings.zoom_step/100 * data.viewport_dimensions.y)
			},
			pan: {
				x: (settings.pan_step/100 * data.viewport_dimensions.x),
				y: (settings.pan_step/100 * data.viewport_dimensions.y)
			}
		}
		return ret;
	}

	function loadTargetDimensions() {
		var data = this.data('panZoom');
		var img = document.createElement('img');
    img.src = this.attr('src');
    img.id = "jqpz-temp";
    $('body').append(img);
    data.target_dimensions.x = $('#jqpz-temp').width();
		data.target_dimensions.y = $('#jqpz-temp').height();
    $('#jqpz-temp').remove();
    data.target_dimensions.ratio = data.target_dimensions.x / data.target_dimensions.y;
	}

})( jQuery );