-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspidocheScaler.js
184 lines (144 loc) · 5.35 KB
/
spidocheScaler.js
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
/*!
* SpidocheScaler - Scale the dom width jQuery
*
* Version : 1.2.3
* Author : Spidoche - spidoche.com
* Require : jQuery v1.7.2 or later
* IE support : ie9 or later
*
* Plugin URL : https://github.com/spidoche/spidocheScaler
* Demo URL : http://spidoche.com/spidocheScaler/
*/
/* globals jQuery, window */
/** jshint -W117 */
/** jshint -W098 */
(function($){
// custom select class
function SpidocheScaler(item, options) {
this.options = $.extend({
maxWidth : 800,
minWidth : 0,
destroyAt: -1
}, options);
this.item = $(item);
this.is_init = false;
this.is_destroy = false;
this.load();
}
SpidocheScaler.prototype = {
init: function() {
//console.log('init');
var that = this;
var $el = that.item;
var timestamp = new Date().getTime();
var $container = null;
// Add uid to each object (required to give a unique name to resize event)
that.uid = timestamp;
// Create a warpper
$el.wrap('<div class="spidochescaler-container" />');
$container = $el.parent();
// Scale on load
that.scale_dom($el, $container, $container.width(), $el.outerHeight(true));
},
scale_dom: function($el, $container, maxWidth, maxHeight) {
// Get options
var settings = this.options;
// Remove or set maxWidth
if($container.width() > settings.maxWidth){
$el.removeAttr('style').parent().removeAttr('style');
return;
}else{
$el.css({width: settings.maxWidth});
}
// Init some variable
var scaleX = 1,
scaleY = 1,
base_scale = 1,
base_scaleX = 1,
base_scaleY = 1;
// Get element base dimension
var base_width = $el.width(),
base_height = $el.outerHeight(true);
// Set the dimension
scaleX = maxWidth / base_width;
scaleY = maxHeight / base_height;
base_scaleX = scaleX;
base_scaleY = scaleY;
base_scale = (scaleX > scaleY) ? scaleY : scaleX;
// Apply dimension if sup to minWidth !BETA TEST
if($container.width() > settings.minWidth){
if($container.hasClass('spidochescaler-container')){
$container.css({
height: base_height*base_scale,
// Fix issue when container has float (since 1.2.3)
display: 'inline-block',
width: '100%',
verticalAlign: 'top'
});
}
$el.css({
width : settings.maxWidth,
margin: 0,
transformOrigin : '0 0',
transform : 'scale(' + base_scale + ')'
});
}
},
resize: function(){
var that = this;
var $el = that.item;
var $container = $el.parent();
var destroy_at = that.options.destroyAt;
//console.log(this.is_init);
$(window).on('resize.domScaler.'+that.uid,function () {
//console.log(that.is_init);
if($(window).width() > destroy_at && that.is_init === true ){
that.scale_dom($el, $container, $container.width(), $el.outerHeight(true));
}else if($(window).width() <= destroy_at && that.is_init === true){
that.destroy();
//console.log('dd');
}else if($(window).width() > destroy_at && that.is_init === false){
that.load();
that.is_init = true;
}
});
},
load: function(){
// Init if window width up to destroy breakpoint
if($(window).width() > this.options.destroyAt ){
this.init();
this.is_init = true;
}
this.resize();
},
destroy: function() {
//console.log('destroy');
// Remove current item inline style
this.item.removeAttr('style');
// Remove container
if(this.item.parent().hasClass('spidochescaler-container')){
this.item.unwrap();
}
this.is_init = false;
// Check if unbind resize is required
//$(window).off('resize.domScaler.'+this.uid);
}
}; // END Prototype SpidocheScaler
// jQuery plugin interface
$.fn.spidochescaler = function(opt) {
var args = Array.prototype.slice.call(arguments, 1);
return this.each(function() {
var item = $(this);
var instance = item.data('SpidocheScaler');
if(!instance) {
// Create plugin instance if not created
item.data('SpidocheScaler', new SpidocheScaler(this, opt));
} else {
// Otherwise check arguments for method call
if(typeof opt === 'string') {
instance[opt].apply(instance, args);
}
}
});
};
}(jQuery));