// NanoBaseHelpers is where the base template helpers (common to all templates) are stored
NanoBaseHelpers = function ()
{
var _baseHelpers = {
// change ui styling to "syndicate mode"
syndicateMode: function() {
$('body').css("background-color","#8f1414");
$('body').css("background-image","url('uiBackground-Syndicate.png')");
$('body').css("background-position","50% 0");
$('body').css("background-repeat","repeat-x");
$('#uiTitleFluff').css("background-image","url('uiTitleFluff-Syndicate.png')");
$('#uiTitleFluff').css("background-position","50% 50%");
$('#uiTitleFluff').css("background-repeat", "no-repeat");
return '';
},
// Generate a Byond link
link: function( text, icon, parameters, status, elementClass, elementId) {
var iconHtml = '';
var iconClass = 'noIcon';
if (typeof icon != 'undefined' && icon)
{
iconHtml = '
';
iconClass = 'hasIcon';
}
if (typeof elementClass == 'undefined' || !elementClass)
{
elementClass = 'link';
}
var elementIdHtml = '';
if (typeof elementId != 'undefined' && elementId)
{
elementIdHtml = 'id="' + elementId + '"';
}
if (typeof status != 'undefined' && status)
{
return '' + iconHtml + text + '
';
}
return '' + iconHtml + text + '
';
},
// Round a number to the nearest integer
round: function(number) {
return Math.round(number);
},
// Returns the number fixed to 1 decimal
fixed: function(number) {
return Math.round(number * 10) / 10;
},
// Round a number down to integer
floor: function(number) {
return Math.floor(number);
},
// Round a number up to integer
ceil: function(number) {
return Math.ceil(number);
},
abs: function(number) {
return Math.abs(number);
},
// Format a string (~string("Hello {0}, how are {1}?", 'Martin', 'you') becomes "Hello Martin, how are you?")
string: function() {
if (arguments.length == 0)
{
return '';
}
else if (arguments.length == 1)
{
return arguments[0];
}
else if (arguments.length > 1)
{
stringArgs = [];
for (var i = 1; i < arguments.length; i++)
{
stringArgs.push(arguments[i]);
}
return arguments[0].format(stringArgs);
}
return '';
},
formatNumber: function(x) {
// From http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
},
// Capitalize the first letter of a string. From http://stackoverflow.com/questions/1026069/capitalize-the-first-letter-of-string-in-javascript
capitalizeFirstLetter: function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
// Display a bar. Used to show health, capacity, etc. Use difClass if the entire display bar class should be different
displayBar: function(value, rangeMin, rangeMax, styleClass, showText, difClass, direction) {
if (rangeMin < rangeMax)
{
if (value < rangeMin)
{
value = rangeMin;
}
else if (value > rangeMax)
{
value = rangeMax;
}
}
else
{
if (value > rangeMin)
{
value = rangeMin;
}
else if (value < rangeMax)
{
value = rangeMax;
}
}
if (typeof styleClass == 'undefined' || !styleClass)
{
styleClass = '';
}
if (typeof showText == 'undefined' || !showText)
{
showText = '';
}
if (typeof difClass == 'undefined' || !difClass)
{
difClass = ''
}
if(typeof direction == 'undefined' || !direction)
{
direction = 'width'
}
else
{
direction = 'height'
}
var percentage = Math.round((value - rangeMin) / (rangeMax - rangeMin) * 100);
return '';
},
// Display DNA Blocks (for the DNA Modifier UI)
displayDNABlocks: function(dnaString, selectedBlock, selectedSubblock, blockSize, paramKey) {
if (!dnaString)
{
return 'Please place a valid subject into the DNA modifier.
';
}
var characters = dnaString.split('');
var html = '1
';
var block = 1;
var subblock = 1;
for (index in characters)
{
if (!characters.hasOwnProperty(index) || typeof characters[index] === 'object')
{
continue;
}
var parameters;
if (paramKey.toUpperCase() == 'UI')
{
parameters = { 'selectUIBlock' : block, 'selectUISubblock' : subblock };
}
else
{
parameters = { 'selectSEBlock' : block, 'selectSESubblock' : subblock };
}
var status = 'linkActive';
if (block == selectedBlock && subblock == selectedSubblock)
{
status = 'selected';
}
html += '
' + characters[index] + '
'
index++;
if (index % blockSize == 0 && index < characters.length)
{
block++;
subblock = 1;
html += '
' + block + '
';
}
else
{
subblock++;
}
}
html += '
';
return html;
}
};
return {
addHelpers: function ()
{
NanoTemplate.addHelpers(_baseHelpers);
},
removeHelpers: function ()
{
for (var helperKey in _baseHelpers)
{
if (_baseHelpers.hasOwnProperty(helperKey))
{
NanoTemplate.removeHelper(helperKey);
}
}
}
};
} ();