aboutsummaryrefslogtreecommitdiff
path: root/src/components/settings_modal/helpers/setting.js
blob: 0971b919223361ed09bab3882a139b2c6cc3ea34 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import Checkbox from 'src/components/checkbox/checkbox.vue'
import ModifiedIndicator from './modified_indicator.vue'
import ProfileSettingIndicator from './profile_setting_indicator.vue'
import DraftButtons from './draft_buttons.vue'
import { get, set } from 'lodash'

export default {
  components: {
    Checkbox,
    ModifiedIndicator,
    DraftButtons,
    ProfileSettingIndicator
  },
  props: {
    path: {
      type: String,
      required: true
    },
    disabled: {
      type: Boolean,
      default: false
    },
    parentPath: {
      type: String
    },
    parentInvert: {
      type: Boolean,
      default: false
    },
    expert: {
      type: [Number, String],
      default: 0
    },
    source: {
      type: String,
      default: 'default'
    },
    draftMode: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      draft: null
    }
  },
  created () {
    if (this.draftMode) {
      this.draft = this.state
    }
  },
  computed: {
    state () {
      const value = get(this.configSource, this.path)
      if (value === undefined) {
        return this.defaultState
      } else {
        return value
      }
    },
    shouldBeDisabled () {
      const parentValue = this.parentPath !== undefined ? get(this.configSource, this.parentPath) : null
      return this.disabled || (parentValue !== null ? (this.parentInvert ? parentValue : !parentValue) : false)
    },
    configSource () {
      switch (this.source) {
        case 'profile':
          return this.$store.state.profileConfig
        case 'admin':
          return this.$store.state.adminSettings.config
        default:
          return this.$store.getters.mergedConfig
      }
    },
    configSink () {
      switch (this.source) {
        case 'profile':
          return (k, v) => this.$store.dispatch('setProfileOption', { name: k, value: v })
        case 'admin':
          return (k, v) => this.$store.dispatch('pushAdminSetting', { path: k, value: v })
        default:
          return (k, v) => this.$store.dispatch('setOption', { name: k, value: v })
      }
    },
    defaultState () {
      switch (this.source) {
        case 'profile':
          return {}
        default:
          return get(this.$store.getters.defaultConfig, this.path)
      }
    },
    isProfileSetting () {
      return this.source === 'profile'
    },
    isChanged () {
      switch (this.source) {
        case 'profile':
        case 'admin':
          return false
        default:
          return this.state !== this.defaultState
      }
    },
    isDirty () {
      return this.draftMode && this.draft !== this.state
    },
    canHardReset () {
      return this.source === 'admin' && this.$store.state.adminSettings.modifiedPaths.has(this.path)
    },
    matchesExpertLevel () {
      return (this.expert || 0) <= this.$store.state.config.expertLevel > 0
    }
  },
  methods: {
    getValue (e) {
      return e.target.value
    },
    update (e) {
      if (this.draftMode) {
        this.draft = this.getValue(e)
      } else {
        this.configSink(this.path, this.getValue(e))
      }
    },
    commitDraft () {
      if (this.draftMode) {
        this.configSink(this.path, this.draft)
      }
    },
    reset () {
      console.log('reset')
      if (this.draftMode) {
        console.log(this.draft)
        console.log(this.state)
        this.draft = this.state
      } else {
        set(this.$store.getters.mergedConfig, this.path, this.defaultState)
      }
    },
    hardReset () {
      switch (this.source) {
        case 'admin':
          return this.$store.dispatch('resetAdminSetting', { path: this.path })
            .then(() => { this.draft = this.state })
        default:
          console.warn('Hard reset not implemented yet!')
      }
    }
  }
}