bol/module/system/helpers.js

152 lines
4.3 KiB
JavaScript
Raw Permalink Normal View History

2021-07-08 10:12:12 +02:00
export const registerHandlebarsHelpers = function () {
2022-03-27 22:56:43 +02:00
Handlebars.registerHelper('isNull', function (val) {
return val == null;
});
Handlebars.registerHelper('exists', function (val) {
return val != null && val != undefined;
});
Handlebars.registerHelper('isEmpty', function (list) {
if (list) return list.length == 0;
2024-02-22 18:29:04 +01:00
else return false;
2022-03-27 22:56:43 +02:00
});
Handlebars.registerHelper('notEmpty', function (list) {
return list.length > 0;
});
Handlebars.registerHelper('isNegativeOrNull', function (val) {
return val <= 0;
});
Handlebars.registerHelper('isNegative', function (val) {
return val < 0;
});
Handlebars.registerHelper('isPositive', function (val) {
return val > 0;
});
Handlebars.registerHelper('equals', function (val1, val2) {
return val1 == val2;
});
Handlebars.registerHelper('neq', function (val1, val2) {
return val1 !== val2;
});
Handlebars.registerHelper('gt', function (val1, val2) {
return val1 > val2;
})
Handlebars.registerHelper('lt', function (val1, val2) {
return val1 < val2;
})
Handlebars.registerHelper('gte', function (val1, val2) {
return val1 >= val2;
})
Handlebars.registerHelper('lte', function (val1, val2) {
return val1 <= val2;
})
Handlebars.registerHelper('and', function (val1, val2) {
return val1 && val2;
})
Handlebars.registerHelper('or', function (val1, val2) {
return val1 || val2;
})
Handlebars.registerHelper('or3', function (val1, val2, val3) {
return val1 || val2 || val3;
})
Handlebars.registerHelper('for', function (from, to, incr, block) {
2024-02-22 18:29:04 +01:00
let accum = '';
for (let i = from; i < to; i += incr)
2022-03-27 22:56:43 +02:00
accum += block.fn(i);
return accum;
})
Handlebars.registerHelper('not', function (cond) {
return !cond;
})
Handlebars.registerHelper('count', function (list) {
return list.length;
})
2022-12-25 18:00:42 +01:00
Handlebars.registerHelper('countKeys', function (obj) {
return Object.keys(obj).length;
})
2024-08-13 10:37:56 +02:00
2022-03-27 22:56:43 +02:00
Handlebars.registerHelper('isEnabled', function (configKey) {
return game.settings.get("bol", configKey);
})
Handlebars.registerHelper('split', function (str, separator, keep) {
return str.split(separator)[keep];
})
// If you need to add Handlebars helpers, here are a few useful examples:
Handlebars.registerHelper('concat', function () {
2024-02-22 18:29:04 +01:00
let outStr = '';
for (let arg in arguments) {
2022-03-27 22:56:43 +02:00
if (typeof arguments[arg] != 'object') {
outStr += arguments[arg];
}
}
return outStr;
})
Handlebars.registerHelper('add', function (a, b) {
return parseInt(a) + parseInt(b);
});
Handlebars.registerHelper('mul', function (a, b) {
return parseInt(a) * parseInt(b);
})
Handlebars.registerHelper('sub', function (a, b) {
return parseInt(a) - parseInt(b);
})
2022-11-25 15:50:07 +01:00
Handlebars.registerHelper('abbrev2', function (a) {
2024-08-13 10:37:56 +02:00
return a.substring(0, 2);
2022-11-25 15:50:07 +01:00
})
Handlebars.registerHelper('abbrev3', function (a) {
2024-08-13 10:37:56 +02:00
return a.substring(0, 3);
2022-11-25 15:50:07 +01:00
})
2022-03-27 22:56:43 +02:00
Handlebars.registerHelper('valueAtIndex', function (arr, idx) {
return arr[idx];
})
Handlebars.registerHelper('includesKey', function (items, type, key) {
2023-04-04 13:41:22 +02:00
return items.filter(i => i.type === type).map(i => i.system.key).includes(key);
2022-03-27 22:56:43 +02:00
})
Handlebars.registerHelper('includes', function (array, val) {
return array.includes(val);
})
Handlebars.registerHelper('eval', function (expr) {
return eval(expr);
})
Handlebars.registerHelper('isOwnerOrGM', function (actor) {
console.log("Testing actor", actor.isOwner, game.userId)
2024-02-22 18:29:04 +01:00
return actor.isOwner || game.isGM;
2022-03-27 22:56:43 +02:00
})
2022-12-25 18:00:42 +01:00
Handlebars.registerHelper('upperFirst', function (text) {
if (typeof text !== 'string') return text
return text.charAt(0).toUpperCase() + text.slice(1)
})
Handlebars.registerHelper('upperFirstOnly', function (text) {
if (typeof text !== 'string') return text
return text.charAt(0).toUpperCase()
})
Handlebars.registerHelper('isCreature', function (key) {
return key == "creature" || key == "daemon";
})
2024-08-13 10:37:56 +02:00
// Handle v12 removal of this helper
Handlebars.registerHelper('select', function (selected, options) {
const escapedValue = RegExp.escape(Handlebars.escapeExpression(selected));
const rgx = new RegExp(' value=[\"\']' + escapedValue + '[\"\']');
const html = options.fn(this);
return html.replace(rgx, "$& selected");
});
2021-07-08 10:12:12 +02:00
}