szurubooru/client/js/util/handlebars-helpers.js

114 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-04-05 17:57:26 +02:00
'use strict';
const views = require('../util/views.js');
2016-04-05 17:57:26 +02:00
const handlebars = require('handlebars');
const misc = require('./misc.js');
2016-04-14 12:11:31 +02:00
function makeLabel(options, attrs) {
if (!options.hash.text) {
return '';
}
if (!attrs) {
attrs = {};
}
attrs.for = options.hash.id;
return views.makeNonVoidElement('label', attrs, options.hash.text);
}
2016-04-06 22:34:21 +02:00
handlebars.registerHelper('reltime', function(time) {
2016-04-05 17:57:26 +02:00
return new handlebars.SafeString(
views.makeNonVoidElement(
'time',
{datetime: time, title: time},
misc.formatRelativeTime(time)));
2016-04-05 17:57:26 +02:00
});
2016-04-06 22:34:21 +02:00
handlebars.registerHelper('thumbnail', function(url) {
return new handlebars.SafeString(
2016-04-10 22:13:01 +02:00
views.makeNonVoidElement('span', {
class: 'thumbnail',
style: 'background-image: url(\'{0}\')'.format(url)
}, views.makeVoidElement('img', {alt: 'thumbnail', src: url})));
});
2016-04-06 22:34:21 +02:00
handlebars.registerHelper('toLowerCase', function(str) {
return str.toLowerCase();
});
handlebars.registerHelper('radio', function(options) {
return new handlebars.SafeString('{0}{1}'.format(
views.makeVoidElement('input', {
id: options.hash.id,
name: options.hash.name,
value: options.hash.value,
type: 'radio',
2016-04-10 15:55:56 +02:00
checked: options.hash.selectedValue === options.hash.value,
required: options.hash.required,
}),
2016-04-14 12:11:31 +02:00
makeLabel(options, {class: 'radio'})));
});
handlebars.registerHelper('checkbox', function(options) {
return new handlebars.SafeString('{0}{1}'.format(
views.makeVoidElement('input', {
id: options.hash.id,
name: options.hash.name,
value: options.hash.value,
type: 'checkbox',
checked: options.hash.checked !== undefined ?
options.hash.checked : false,
required: options.hash.required,
}),
2016-04-14 12:11:31 +02:00
makeLabel(options, {class: 'checkbox'})));
});
handlebars.registerHelper('select', function(options) {
return new handlebars.SafeString('{0}{1}'.format(
2016-04-14 12:11:31 +02:00
makeLabel(options),
views.makeNonVoidElement(
'select',
{id: options.hash.id, name: options.hash.name},
Object.keys(options.hash.keyValues).map(key => {
return views.makeNonVoidElement(
'option',
{value: key, selected: key === options.hash.selectedKey},
options.hash.keyValues[key]);
}).join(''))));
});
handlebars.registerHelper('input', function(options) {
return new handlebars.SafeString('{0}{1}'.format(
2016-04-14 12:11:31 +02:00
makeLabel(options),
views.makeVoidElement(
'input', {
type: options.hash.inputType,
name: options.hash.name,
id: options.hash.id,
value: options.hash.value || '',
required: options.hash.required,
pattern: options.hash.pattern,
placeholder: options.hash.placeholder,
})));
});
handlebars.registerHelper('textInput', function(options) {
options.hash.inputType = 'text';
return handlebars.helpers.input(options);
});
handlebars.registerHelper('passwordInput', function(options) {
options.hash.inputType = 'password';
return handlebars.helpers.input(options);
});
handlebars.registerHelper('emailInput', function(options) {
options.hash.inputType = 'email';
return handlebars.helpers.input(options);
});
2016-04-10 22:13:01 +02:00
handlebars.registerHelper('alignFlexbox', function(options) {
return new handlebars.SafeString(
Array.from(misc.range(20))
.map(() => '<li class="flexbox-dummy"></li>').join(''));
});