-
Notifications
You must be signed in to change notification settings - Fork 0
/
scale.js
99 lines (67 loc) · 1.8 KB
/
scale.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
( function( ) {
/**
* Constructor.
*/
function Scale( ) {
this._$reference = undefined;
this._$sizes = $( '.scale-sizes' );
this._$sizesList = $( '.options-list' );
this._$precision = $( '.options-precision' );
this._sizeTemplate = _.template( $( '#scale-size-template' ).html( ));
this._map = { };
var _this = this;
$( '.options' ).on( 'submit', function( event ) {
event.preventDefault( );
_this._load( );
});
this._$sizes.on( 'click', 'tr', function( event ) {
_this._setReference( $( this ));
});
this._load( );
}
/**
* Class for the reference size.
*/
Scale.REFERENCE_CLASS = 'scale-reference';
/**
* Loads sizes.
*/
Scale.prototype._load = function( ) {
this._map = { };
this._$reference = undefined;
this._$sizes.empty( );
var sizes = this._$sizesList.val( ).split( ',' );
for ( var i = 0; i < sizes.length; ++i ) {
var size = parseInt( sizes[ i ], 10 );
$size = $( this._sizeTemplate({
'index': i,
'pixels': size
}));
this._map[ i ] = {
'pixels': size,
'$ems': $size.find( '.scale-ems .scale-value' )
};
this._$sizes.append( $size );
}
this._setReference( this._$sizes.find( '.scale-size' ).first( ));
};
/**
* Sets the reference size.
*/
Scale.prototype._setReference = function( $element ) {
if ( this._$reference ) {
this._$reference.removeClass( Scale.REFERENCE_CLASS );
}
this._$reference = $element;
this._$reference.addClass( Scale.REFERENCE_CLASS );
var referenceIndex = this._$reference.attr( 'data-index' );
var referencePixels = this._map[ referenceIndex ].pixels;
var precision = this._$precision.val( );
_( this._map ).each( function( size ) {
size.$ems.text(
( size.pixels / referencePixels ).toFixed( precision )
);
});
};
new Scale( );
}( ));