Skip to content
Snippets Groups Projects
Commit 8dcba581 authored by Pregmodder's avatar Pregmodder
Browse files

Merge branch 'categorizer-enhancements' into 'pregmod-master'

Categorizer enhancements

See merge request !391
parents bef0441b 491447c7
No related branches found
No related tags found
No related merge requests found
...@@ -26,6 +26,10 @@ if(!Array.prototype.findIndex) { ...@@ -26,6 +26,10 @@ if(!Array.prototype.findIndex) {
/* /*
A categorizer is used to "slice" a value range into distinct categories in an efficient manner. A categorizer is used to "slice" a value range into distinct categories in an efficient manner.
If the values are objects their property named 'value' will be set to whatever
the value used for the choice was. This is important for getters, where it can be accessed
via this.value.
--- Example --- --- Example ---
Original SugarCube code Original SugarCube code
<<if _Slave.muscles > 95>> <<if _Slave.muscles > 95>>
...@@ -59,9 +63,31 @@ window.Categorizer = function() { ...@@ -59,9 +63,31 @@ window.Categorizer = function() {
.sort(function(a, b) { return b[0] - a[0]; /* reverse sort */ }); .sort(function(a, b) { return b[0] - a[0]; /* reverse sort */ });
}; };
window.Categorizer.prototype.cat = function(val, def) { window.Categorizer.prototype.cat = function(val, def) {
if(typeof val !== 'number' || isNaN(val)) { var result = def;
return def; if(typeof val === 'number' && !isNaN(val)) {
var foundCat = this.cats.find(function(e) { return val >= e[0]; });
if(foundCat) {
result = foundCat[1];
}
} }
var result = this.cats.find(function(e) { return val >= e[0]; }); // Record the value for the result's getter, if it is an object
return result ? result[1] : def; // and doesn't have the property yet
if(result === Object(result)) {
result['value'] = val;
}
return result;
};
/*
Make everything waiting for this execute. Usage:
let doSomething = function() {
... your initialization code goes here ...
}; };
if(typeof Categorizer === 'function') {
doSomething();
} else {
jQuery(document).one('categorizer.ready', doSomething);
}
*/
jQuery(document).trigger('categorizer.ready');
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment