* Start on new theme re-work

* theme_info.json -> theme.json
* Allow pipe codes for styleSGRx
This commit is contained in:
Bryan Ashby 2015-05-12 23:04:22 -06:00
parent 58746ca9a9
commit 2aa43295e3
10 changed files with 68 additions and 61 deletions

View File

@ -211,8 +211,8 @@ function prepareClient(client, cb) {
client.user.properties.theme_id = theme.getRandomTheme() || '';
theme.getThemeInfo(client.user.properties.theme_id, function themeInfo(err, info) {
client.currentThemeInfo = info;
theme.loadTheme(client.user.properties.theme_id, function themeLoaded(err, theme) {
client.currentTheme = theme;
cb(null);
});
} else {

View File

@ -77,7 +77,6 @@ function getDefaultConfig() {
preLoginTheme : '*',
// :TODO: change to nua
users : {
usernameMin : 2,
usernameMax : 22,

View File

@ -25,9 +25,9 @@ function login(callingMenu, formData, extraArgs) {
async.parallel(
[
function loadThemeConfig(callback) {
theme.getThemeInfo(client.user.properties.theme_id, function themeInfo(err, info) {
client.currentThemeInfo = info;
callback(null);
theme.loadTheme(client.user.properties.theme_id, function themeLoaded(err, theme) {
client.currentTheme = theme;
callback(null); // always non-fatal
});
}
],

View File

@ -1,50 +1,60 @@
/* jslint node: true */
'use strict';
var Config = require('./config.js').config;
var art = require('./art.js');
var miscUtil = require('./misc_util.js');
var Log = require('./logger.js').log;
var Config = require('./config.js').config;
var art = require('./art.js');
var miscUtil = require('./misc_util.js');
var Log = require('./logger.js').log;
var fs = require('fs');
var paths = require('path');
var async = require('async');
var _ = require('lodash');
var assert = require('assert');
var fs = require('fs');
var paths = require('path');
var async = require('async');
var _ = require('lodash');
var assert = require('assert');
var stripJsonComments = require('strip-json-comments');
exports.getThemeInfo = getThemeInfo;
exports.loadTheme = loadTheme;
exports.getThemeArt = getThemeArt;
exports.getRandomTheme = getRandomTheme;
exports.initAvailableThemes = initAvailableThemes;
exports.displayThemeArt = displayThemeArt;
function getThemeInfo(themeID, cb) {
var path = paths.join(Config.paths.themes, themeID, 'theme_info.json');
function loadTheme(themeID, cb) {
var path = paths.join(Config.paths.themes, themeID, 'theme.json');
fs.readFile(path, function onData(err, data) {
fs.readFile(path, { encoding : 'utf8' }, function onData(err, data) {
if(err) {
cb(err);
} else {
try {
// :TODO: strip comments/etc. ala menu.json
var info = JSON.parse(data);
var theme = JSON.parse(stripJsonComments(data));
if(!_.isObject(theme.info)) {
cb(new Error('Invalid theme JSON'));
return;
}
assert(!_.isObject(theme.helpers)); // we create this on the fly!
//
// Create some handy helpers
//
info.getPasswordChar = function() {
var pwChar = Config.defaults.passwordChar;
if(_.isObject(info.config)) {
if(_.isString(info.config.passwordChar)) {
pwChar = info.config.passwordChar.substr(0, 1);
} else if(_.isNumber(info.config.passwordChar)) {
pwChar = String.fromCharCode(info.config.passwordChar);
theme.helpers = {
getPasswordChar : function() {
var pwChar = Config.defaults.passwordChar;
if(_.isObject(theme.defaults) && _.isObject(theme.defaults.general)) {
var themePasswordChar = theme.defaults.general.passwordChar;
if(_.isString(themePasswordChar)) {
pwChar = themePasswordChar.substr(0, 1);
} else if(_.isNumber(themePasswordChar)) {
pwChar = String.fromCharCode(themePasswordChar);
}
}
return pwChar;
}
return pwChar;
};
}
cb(null, info);
cb(null, theme);
} catch(e) {
cb(err);
}
@ -70,15 +80,13 @@ function initAvailableThemes(cb) {
},
function populateAvailable(filtered, callback) {
filtered.forEach(function onTheme(themeId) {
getThemeInfo(themeId, function onThemeInfo(err, info) {
loadTheme(themeId, function themeLoaded(err, theme) {
if(!err) {
if(!availableThemes) {
availableThemes = {};
}
availableThemes[themeId] = info;
Log.debug( { info : info }, 'Theme loaded');
availableThemes[themeId] = theme;
Log.debug( { info : theme.info }, 'Theme loaded');
}
});
});
callback(null);
}
@ -101,17 +109,6 @@ function getRandomTheme() {
}
}
/*
function getRandomTheme(cb) {
if(Object.getOwnPropertyNames(availableThemes).length > 0) {
var themeIds = Object.keys(availableThemes);
cb(null, themeIds[Math.floor(Math.random() * themeIds.length)]);
} else {
cb(new Error('No themes available'));
}
}
*/
function getThemeArt(name, themeID, options, cb) {
// allow options to be optional
if(_.isUndefined(cb)) {

View File

@ -22,7 +22,7 @@ function User() {
this.groups = {}; // id:name
this.isValid = function() {
if(self.userId <= 0 || self.username.length < 2) {
if(self.userId <= 0 || self.username.length < Config.users.usernameMin) {
return false;
}

View File

@ -196,9 +196,9 @@ function ViewController(options) {
['styleSGR1', 'styleSGR2'].forEach(function styleSgr(style) {
setViewProp(style, function(v) {
if(_.isObject(v)) {
view.styleSGR1 = ansi.getSGRFromGraphicRendition(v, true);
view[style] = ansi.getSGRFromGraphicRendition(v, true);
} else if(_.isString(v)) {
view.styleSGR1 = v;
view[style] = ansi.fromPipeCode(v);
}
});
});
@ -213,7 +213,7 @@ function ViewController(options) {
setViewProp('password', function(v) {
if(true === v) {
view.textMaskChar = self.client.currentThemeInfo.getPasswordChar();
view.textMaskChar = self.client.currentTheme.helpers.getPasswordChar();
}
});

View File

@ -0,0 +1,18 @@
{
"info" : {
"name" : "Nu Mayan",
"author" : "NuSkooler"
},
"defaults" : {
"general" : {
"passwordChar" : "φ"
}
},
"menus" : {
"matrix" : {
"VM1" : {
"itemSpacing" : 0
}
}
}
}

View File

@ -1,7 +0,0 @@
{
"name" : "Nu Mayan",
"author" : "NuSkooler",
"config" : {
"passwordChar" : "φ"
}
}

View File

@ -24,7 +24,7 @@ function login(callingMenu, formData, extraArgs) {
[
function loadThemeConfig(callback) {
theme.getThemeInfo(client.user.properties.theme_id, function themeInfo(err, info) {
client.currentThemeInfo = info;
client.currentTheme = info;
callback(null);
});
}

View File

@ -320,7 +320,7 @@
},
"TM3" : {
"items" : [ "Yarly", "Nowaii" ],
"styleSGR1" : { "fg" : 30, "intensity" : 1 },
"styleSGR1" : "|00|30|01",
"hotkeys" : { "Y" : 0, "N" : 1 }
},
"BT8" : {