toggle_hosts.js 999 B

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