toggle_hosts.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * @author oldj
  3. * @blog https://oldj.net
  4. */
  5. 'use strict'
  6. import Agent from '../Agent'
  7. import save from './save'
  8. import treeFunc from '../../app/libs/treeFunc'
  9. module.exports = async (app, hosts) => {
  10. hosts.on = !hosts.on
  11. let {id} = hosts
  12. let lang = app.state.lang
  13. let pref = await Agent.pact('getPref')
  14. let list = app.state.list.slice(0)
  15. let flat_list = treeFunc.flatTree(list)
  16. let {parent_list, parent} = treeFunc.getItemDetailById(list, id)
  17. let pref_is_single = pref.choice_mode === 'single'
  18. let choice_mode = pref_is_single ? 1 : 2
  19. if (parent) {
  20. choice_mode = parent.folder_mode === 0 ? choice_mode : parent.folder_mode
  21. }
  22. if (choice_mode === 1 && hosts.on) {
  23. parent_list.map(item => {
  24. if (item.id !== id) {
  25. item.on = false
  26. }
  27. })
  28. }
  29. let item = flat_list.find(item => item.id === id)
  30. if (!item) {
  31. list.push(Object.assign({}, hosts))
  32. } else {
  33. item.on = hosts.on
  34. }
  35. await save(app, list, hosts)
  36. Agent.pact('statRecord', 'switch')
  37. .catch(e => console.log(e))
  38. Agent.pact('notify', 'SwitchHosts!', lang.hosts_switched)
  39. .catch(e => console.log(e))
  40. }