web/static/res/js/incresize.js
author ymh <ymh.work@gmail.com>
Tue, 12 Feb 2013 10:54:34 +0100
changeset 134 678b07d351ef
parent 78 8c3f0b94d056
permissions -rw-r--r--
Added tag V01.25 for changeset fe9ca5cd905e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
     1
function IncResize()
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
     2
{
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
     3
	this.content;
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
     4
	this.top;
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
     5
	this.middle;
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
     6
	this.middleContainer;
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
     7
	this.bottom;
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
     8
	this.footer;
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
     9
	this.minTopBottomHeight;
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    10
	this.ratio;
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    11
	this.considerFooterToCenter;
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    12
	this.callbacks;
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    13
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    14
	this.init = function(contentId, topId, middleId, bottomId, footerId, minTopBottomHeight, ratio, considerFooterToCenter, callbacks)
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    15
	{
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    16
		// Content
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    17
		this.content = $('#' + contentId);
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    18
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    19
		// Top
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    20
		if (topId != null) {
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    21
			this.top = $('#' + topId); 			
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    22
		}
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    23
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    24
		// Middle
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    25
		this.middle = $('#' + middleId); 
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    26
		this.middleContainer = $(this.middle).parent();
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    27
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    28
		// Bottom
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    29
		if (bottomId !== null) {
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    30
			this.bottom = $('#' + bottomId);
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    31
		}
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    32
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    33
		// Footer
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    34
		if (footerId !== null) {
42
01415303372e écran choix
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 36
diff changeset
    35
			this.footer =  $('#' + footerId);
01415303372e écran choix
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 36
diff changeset
    36
		}
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    37
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    38
		this.minTopBottomHeight = minTopBottomHeight;
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    39
		this.ratio = ratio;
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    40
		this.considerFooterToCenter = (considerFooterToCenter === undefined || footerId === null) ? false : considerFooterToCenter;
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    41
		this.callbacks = callbacks;
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    42
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    43
		$(window).resize(this.resizeElements);
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    44
		window.onorientationchange = function() {
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    45
			incResize.resizeElements();
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    46
		}
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    47
	};
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    48
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    49
	this.resizeElements = function()
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    50
	{
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    51
		var self = incResize;
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    52
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    53
		// Compute maximun middle element height
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    54
		var heightForTopMiddleBottom = $(self.content).height();
42
01415303372e écran choix
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 36
diff changeset
    55
		if (self.footer !== undefined) {
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    56
			heightForTopMiddleBottom -= $(self.footer).height()			
42
01415303372e écran choix
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 36
diff changeset
    57
		}
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    58
		var maxMiddleHeight = heightForTopMiddleBottom - self.minTopBottomHeight * 2;
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    59
42
01415303372e écran choix
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 36
diff changeset
    60
		// Get the middle element parent width and test if it is not to big (to let place for top and bottom without adding scroll bars)
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    61
		var middleRatio = 1 / self.ratio;
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    62
		var newMiddleWidth = $(self.middleContainer).width();
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    63
		var newMiddleHeight = newMiddleWidth * middleRatio;
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    64
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    65
		if (self.top !== undefined && self.bottom !== undefined && newMiddleHeight > maxMiddleHeight) {
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    66
			newMiddleHeight = maxMiddleHeight;
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    67
			newMiddleWidth = newMiddleHeight * 1 / middleRatio;
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    68
		}
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    69
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    70
		newMiddleWidth = Math.max(newMiddleWidth, 1);
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    71
		newMiddleHeight = Math.max(newMiddleHeight, 1);
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    72
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    73
		// Change middle element size and the height of its parent
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    74
		$(self.middle).attr('width', newMiddleWidth);
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    75
		$(self.middle).attr('height', newMiddleHeight);
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    76
		$(self.middleContainer).attr('width', newMiddleWidth);
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    77
		$(self.middleContainer).attr('height', newMiddleHeight);
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    78
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    79
		// Change children size of the middle element
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    80
		var children = $(self.middle).children();
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    81
		if (children.length > 0) {
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    82
			children.attr('width', newMiddleWidth);
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    83
			children.attr('height', newMiddleHeight);
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    84
		}
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    85
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    86
		// Post register the middle element size change
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    87
		if (self.callbacks !== undefined) {
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    88
			for (var i = 0; i < self.callbacks.length; ++i) {
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    89
				self.callbacks[i](newMiddleWidth, newMiddleHeight);				
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    90
			}
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    91
		}
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    92
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    93
		// Change top and bottom heights
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    94
		if (self.top !== undefined && self.bottom !== undefined) {
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    95
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
    96
			// Get the full height and compute heigth for top and bottom
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    97
			var remainingHeight = heightForTopMiddleBottom - newMiddleHeight;
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    98
			var newTopHeight = remainingHeight / 2.0;
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
    99
			if (newTopHeight < self.minTopBottomHeight) {
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
   100
				newTopHeight = self.minTopBottomHeight;
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
   101
			}
78
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
   102
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
   103
			var newBottomHeight = newTopHeight;
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
   104
			
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
   105
			if (self.considerFooterToCenter) {
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
   106
				var halfFooterHeight = $(self.footer).height() / 2;
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
   107
				newTopHeight += halfFooterHeight;
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
   108
				newBottomHeight -= halfFooterHeight;
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
   109
			}
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
   110
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
   111
			$(self.top).css({"height" : + newTopHeight +'px', "lineHeight" : + newTopHeight + 'px'});
8c3f0b94d056 big commit after problems
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 42
diff changeset
   112
			$(self.bottom).css({"height" : + newBottomHeight + 'px', "lineHeight" : + newBottomHeight + 'px'});					
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
   113
		}
42
01415303372e écran choix
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents: 36
diff changeset
   114
	};
36
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
   115
}
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
   116
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
   117
var incResize = new IncResize();
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
   118
Edwin Razafimahatratra <edwin@robotalismsoft.com>
parents:
diff changeset
   119