[6a3a178] | 1 | 'use strict'
|
---|
| 2 | var objectAssign = require('object-assign')
|
---|
| 3 |
|
---|
| 4 | module.exports = function () {
|
---|
| 5 | return ThemeSetProto.newThemeSet()
|
---|
| 6 | }
|
---|
| 7 |
|
---|
| 8 | var ThemeSetProto = {}
|
---|
| 9 |
|
---|
| 10 | ThemeSetProto.baseTheme = require('./base-theme.js')
|
---|
| 11 |
|
---|
| 12 | ThemeSetProto.newTheme = function (parent, theme) {
|
---|
| 13 | if (!theme) {
|
---|
| 14 | theme = parent
|
---|
| 15 | parent = this.baseTheme
|
---|
| 16 | }
|
---|
| 17 | return objectAssign({}, parent, theme)
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | ThemeSetProto.getThemeNames = function () {
|
---|
| 21 | return Object.keys(this.themes)
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | ThemeSetProto.addTheme = function (name, parent, theme) {
|
---|
| 25 | this.themes[name] = this.newTheme(parent, theme)
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | ThemeSetProto.addToAllThemes = function (theme) {
|
---|
| 29 | var themes = this.themes
|
---|
| 30 | Object.keys(themes).forEach(function (name) {
|
---|
| 31 | objectAssign(themes[name], theme)
|
---|
| 32 | })
|
---|
| 33 | objectAssign(this.baseTheme, theme)
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | ThemeSetProto.getTheme = function (name) {
|
---|
| 37 | if (!this.themes[name]) throw this.newMissingThemeError(name)
|
---|
| 38 | return this.themes[name]
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | ThemeSetProto.setDefault = function (opts, name) {
|
---|
| 42 | if (name == null) {
|
---|
| 43 | name = opts
|
---|
| 44 | opts = {}
|
---|
| 45 | }
|
---|
| 46 | var platform = opts.platform == null ? 'fallback' : opts.platform
|
---|
| 47 | var hasUnicode = !!opts.hasUnicode
|
---|
| 48 | var hasColor = !!opts.hasColor
|
---|
| 49 | if (!this.defaults[platform]) this.defaults[platform] = {true: {}, false: {}}
|
---|
| 50 | this.defaults[platform][hasUnicode][hasColor] = name
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | ThemeSetProto.getDefault = function (opts) {
|
---|
| 54 | if (!opts) opts = {}
|
---|
| 55 | var platformName = opts.platform || process.platform
|
---|
| 56 | var platform = this.defaults[platformName] || this.defaults.fallback
|
---|
| 57 | var hasUnicode = !!opts.hasUnicode
|
---|
| 58 | var hasColor = !!opts.hasColor
|
---|
| 59 | if (!platform) throw this.newMissingDefaultThemeError(platformName, hasUnicode, hasColor)
|
---|
| 60 | if (!platform[hasUnicode][hasColor]) {
|
---|
| 61 | if (hasUnicode && hasColor && platform[!hasUnicode][hasColor]) {
|
---|
| 62 | hasUnicode = false
|
---|
| 63 | } else if (hasUnicode && hasColor && platform[hasUnicode][!hasColor]) {
|
---|
| 64 | hasColor = false
|
---|
| 65 | } else if (hasUnicode && hasColor && platform[!hasUnicode][!hasColor]) {
|
---|
| 66 | hasUnicode = false
|
---|
| 67 | hasColor = false
|
---|
| 68 | } else if (hasUnicode && !hasColor && platform[!hasUnicode][hasColor]) {
|
---|
| 69 | hasUnicode = false
|
---|
| 70 | } else if (!hasUnicode && hasColor && platform[hasUnicode][!hasColor]) {
|
---|
| 71 | hasColor = false
|
---|
| 72 | } else if (platform === this.defaults.fallback) {
|
---|
| 73 | throw this.newMissingDefaultThemeError(platformName, hasUnicode, hasColor)
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | if (platform[hasUnicode][hasColor]) {
|
---|
| 77 | return this.getTheme(platform[hasUnicode][hasColor])
|
---|
| 78 | } else {
|
---|
| 79 | return this.getDefault(objectAssign({}, opts, {platform: 'fallback'}))
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | ThemeSetProto.newMissingThemeError = function newMissingThemeError (name) {
|
---|
| 84 | var err = new Error('Could not find a gauge theme named "' + name + '"')
|
---|
| 85 | Error.captureStackTrace.call(err, newMissingThemeError)
|
---|
| 86 | err.theme = name
|
---|
| 87 | err.code = 'EMISSINGTHEME'
|
---|
| 88 | return err
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | ThemeSetProto.newMissingDefaultThemeError = function newMissingDefaultThemeError (platformName, hasUnicode, hasColor) {
|
---|
| 92 | var err = new Error(
|
---|
| 93 | 'Could not find a gauge theme for your platform/unicode/color use combo:\n' +
|
---|
| 94 | ' platform = ' + platformName + '\n' +
|
---|
| 95 | ' hasUnicode = ' + hasUnicode + '\n' +
|
---|
| 96 | ' hasColor = ' + hasColor)
|
---|
| 97 | Error.captureStackTrace.call(err, newMissingDefaultThemeError)
|
---|
| 98 | err.platform = platformName
|
---|
| 99 | err.hasUnicode = hasUnicode
|
---|
| 100 | err.hasColor = hasColor
|
---|
| 101 | err.code = 'EMISSINGTHEME'
|
---|
| 102 | return err
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | ThemeSetProto.newThemeSet = function () {
|
---|
| 106 | var themeset = function (opts) {
|
---|
| 107 | return themeset.getDefault(opts)
|
---|
| 108 | }
|
---|
| 109 | return objectAssign(themeset, ThemeSetProto, {
|
---|
| 110 | themes: objectAssign({}, this.themes),
|
---|
| 111 | baseTheme: objectAssign({}, this.baseTheme),
|
---|
| 112 | defaults: JSON.parse(JSON.stringify(this.defaults || {}))
|
---|
| 113 | })
|
---|
| 114 | }
|
---|
| 115 |
|
---|