-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.js
170 lines (162 loc) · 4.73 KB
/
matrix.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
// $Id: matrix.js,v 1.1.2.9.2.1 2011/01/18 10:10:05 aaron1234nz Exp $
(function($) {
//Global container.
window.matrix = {
/**
* window.matrix.sum()
* @param elements
* Class of elements to act on
* @param field_name
* The name of the field
* @param destination
* The ID of the div to place the result
* @param prefix
* String to put in front of the result
* @param suffix
* String to put afer the result
* @param rounding
* Number of decimal places to round to
*/
sum: function(elements, field_name, destination, prefix, suffix, rounding) {
var sum = 0;
$('#matrix-field-'+ field_name +' .' + elements).each(function(index){
var num = parseFloat($(this).val());
if (!isNaN(num)) {
sum += num;
}
});
if (!isNaN(rounding)) {
sum = sum.toFixed(rounding);
}
$('#matrix-result-'+ field_name +'-'+ destination).html(prefix + sum + suffix);
$('#matrix-field-'+ field_name +' .matrix-cell-' + destination).val(prefix + sum + suffix);
},
/**
* window.matrix.average()
* @param elements
* Class of elements to act on
* @param destination
* The ID of the div to place the result
* @param field_name
* The name of the field
* @param prefix
* String to put in front of the result
* @param suffix
* String to put afer the result
* @param rounding
* Number of decimal places to round to
*/
average: function(elements, destination, field_name, prefix, suffix, rounding) {
var total = 0;
var average = 0;
var count = $('.' + elements).length;
$('#matrix-field-'+ field_name +' .' + elements).each(function(index){
var num = parseFloat($(this).val());
if (!isNaN(num)) {
total += num;
}
});
console.log(count);
if (count > 0) {
average = total/count;
}
if (!isNaN(rounding)) {
average = average.toFixed(rounding);
}
$('#matrix-result-'+ field_name +'-'+ destination).html(prefix + average + suffix);
$('#matrix-field-'+ field_name +' .matrix-cell-' + destination).val(prefix + average + suffix);
},
/**
* window.matrix.min()
* @param elements
* Class of elements to act on
* @param destination
* The ID of the div to place the result
* @param field_name
* The name of the field
* @param prefix
* String to put in front of the result
* @param suffix
* String to put afer the result
* @param rounding
* Number of decimal places to round to
*/
min: function(elements, destination, field_name, prefix, suffix, rounding) {
$('#matrix-field-'+ field_name +' .' + elements).each(function(index){
var num = parseFloat($(this).val());
if (isNaN(min) && !isNaN(num)) {
min = num;
}
if (!isNaN(num) && num < min) {
min = num;
}
});
if (!isNaN(rounding)) {
min = min.toFixed(rounding);
}
$('#matrix-result-'+ field_name +'-'+ destination).html(prefix + min + suffix);
$('#matrix-field-'+ field_name +' .matrix-cell-' + destination).val(prefix + min + suffix);
},
/**
* window.matrix.max()
* @param elements
* Class of elements to act on
* @param destination
* The ID of the div to place the result
* @param field_name
* The name of the field
* @param prefix
* String to put in front of the result
* @param suffix
* String to put afer the result
* @param rounding
* Number of decimal places to round to
*/
max: function(elements, destination, field_name, prefix, suffix, rounding) {
$('#matrix-field-'+ field_name +' .' + elements).each(function(index){
var num = parseFloat($(this).val());
if (isNaN(max) && !isNaN(num)) {
max = num;
}
if (!isNaN(num) && num > max) {
max = num;
}
});
if (!isNaN(rounding)) {
max = max.toFixed(rounding);
}
$('#matrix-result-'+ field_name +'-'+ destination).html(preifx + max + suffix);
$('#matrix-field-'+ field_name +' .matrix-cell-' + destination).val(prefix + max + suffix);
},
/**
* window.matrix.custom()
* @param callback
* Name of the PHP callback function
* @param elements
* Class of elements to act on
* @param destination
* The ID of the div to place the result
* @param field_name
* The name of the field
*/
custom: function(callback, elements, destination, field_name) {
var data = [];
$('#matrix-field-'+ field_name +' .' + elements).each(function(index){
data.push($(this).val());
});
$.ajax({
type: "POST",
url: Drupal.settings.basePath + "matrix/calculation",
data: "callback=" + callback + "&data=" + data,
success: function(result) {
if (result['error']) {
alert(result['error']);
}
else {
$('#matrix-result-'+ field_name +'-'+ destination).html(result['data']);
$('#matrix-field-'+ field_name +' .matrix-cell-' + destination).val(result['data']);
}
}
});
}
}})(jQuery);