toggle_hosts.js 851 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * @author oldj
  3. * @blog https://oldj.net
  4. */
  5. 'use strict'
  6. import Agent from '../Agent'
  7. import save from './save'
  8. module.exports = (app, hosts) => {
  9. hosts.on = !hosts.on
  10. Agent.pact('getPref')
  11. .then(pref => {
  12. let list = app.state.list.slice(0)
  13. let is_single = pref.choice_mode === 'single'
  14. if (is_single && hosts.on) {
  15. list.map(item => {
  16. if (item.id !== hosts.id) {
  17. item.on = false
  18. }
  19. })
  20. }
  21. return list
  22. })
  23. .then(list => {
  24. let idx = list.findIndex(item => item.id === hosts.id)
  25. if (idx === -1) {
  26. list.push(Object.assign({}, hosts))
  27. } else {
  28. let old_hosts = list[idx]
  29. list.splice(idx, 1, Object.assign({}, old_hosts, hosts))
  30. }
  31. save(app, list, hosts)
  32. })
  33. Agent.pact('statRecord', 'switch')
  34. }