Blame view

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