-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfoPanel.js
135 lines (118 loc) · 3.41 KB
/
InfoPanel.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
/**
* @class SAS.InfoPanel
**/
var InfoPanel = function ($holder) {
var _self = this;
//region private fields and methods
var _$holder = $holder;
var _$properties;
var _activeColor;
/** @type {Array} */
var _numericProps;
/** @type {Array} */
var _textProps;
var _init = function () {
_$properties = $('<div class="properties-list">').appendTo(_$holder);
};
var _formatDollar = function (val) {
return '$' + _formatThousands(val);
};
var _formatThousands = function (val) {
return val.toLocaleString();
};
var _valueDiv = function (val) {
if (!val) val = '-';
return $('<div class="info-panel-val">').text(val);
};
var _valueDivFn = function (fn) {
return function (val) {
return _valueDiv(fn(val));
};
};
var _addProp = function (properties, prop, label, fn) {
var $row = $('<div class="info-panel-row">').appendTo(_$properties);
var $iconBox = $('<div class="info-panel-icon">').appendTo($row);
if (_colorProperty === prop) {
$('<div class="info-panel-color">').css('backgroundColor', _activeColor).appendTo($iconBox);
}
$('<div class="info-panel-label">').text(label || prop).appendTo($row);
var val = properties[prop];
fn = fn || _valueDiv;
fn(val).appendTo($row)
};
var _showProperties = function (properties, color) {
_activeColor = color;
if (!properties) return;
_$properties.empty();
$.each(_textProps, function (i, prop) {
if (typeof prop === 'string') {
_addProp(properties, prop);
} else {
_addProp(properties, prop[0], prop[1]);
}
});
$.each(_numericProps, function (i, prop) {
_addProp(properties, prop);
});
};
//endregion
//region protected fields and methods (use 'p_' to differentiate).
this.p_this = function () {
return _self;
};
//endregion
//region public API
this.showProperties = function (properties, color) {
_showProperties(properties, color);
};
/** @type {String} */
var _colorProperty;
/**
* @param {String} [colorProperty]
* @return {String|SAS.InfoPanel}
*/
this.colorProperty = function (colorProperty) {
if (!arguments.length) {
return _colorProperty;
}
_colorProperty = colorProperty;
return _self.p_this();
};
/** @type {String} */
var _sizeProperty;
/**
* @param {String} [sizeProperty]
* @return {String|SAS.InfoPanel}
*/
this.sizeProperty = function (sizeProperty) {
if (!arguments.length) {
return _sizeProperty;
}
_sizeProperty = sizeProperty;
return _self.p_this();
};
/**
* @param {Array} [numericProps]
* @return {Array|SAS.InfoPanel}
*/
this.numericProps = function (numericProps) {
if (!arguments.length) {
return _numericProps;
}
_numericProps = numericProps;
return _self;
};
/**
* @param {Array} [textProps]
* @return {Array|SAS.InfoPanel}
*/
this.textProps = function (textProps) {
if (!arguments.length) {
return _textProps;
}
_textProps = textProps;
return _self;
};
//endregion
_init();
};