Skip to content

Commit

Permalink
Merge branch 'feature/del-issue' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
oldj committed Nov 18, 2019
2 parents ca179ea + 024cd28 commit 39039b5
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 27 deletions.
7 changes: 7 additions & 0 deletions app-ui/Panel/ListItem.less
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
}

.sys-hosts {
margin-top: 10px;
.item-icon {
margin-left: 21px;
}
Expand All @@ -99,6 +100,12 @@
opacity: 0.5;
}

:global(.platform-darwin) {
.sys-hosts {
margin-top: 0;
}
}

:global(.theme-dark) {
.list-item {
&.selected {
Expand Down
2 changes: 1 addition & 1 deletion app-ui/Panel/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

.mac_handler {
height: 36px;
//-webkit-app-region: drag;
-webkit-app-region: drag;
}

:global(.theme-dark) {
Expand Down
24 changes: 12 additions & 12 deletions app-ui/events/del_hosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
'use strict'

import Agent from '../Agent'
import treeFunc from '../../app/libs/treeFunc'

module.exports = (app, hosts) => {
let list = app.state.list
let idx = list.findIndex(item => item.id === hosts.id)
if (idx === -1) {
return
}
let id = hosts.id
let neighbors = [treeFunc.getUpItemWithCollapseState(list, id), treeFunc.getDownItemWithCollapseState(list, id)]
list = treeFunc.removeItemFromTreeById(list, hosts.id)
let next_hosts = neighbors[1] || neighbors[0] || null

list.splice(idx, 1)
//let idx = list.findIndex(item => item.id === hosts.id)
//if (idx === -1) {
// return
//}
//
//list.splice(idx, 1)

Agent.pact('saveHosts', list)
.then(list => {
app.setState({list}, () => {
// 选中下一个 hosts
let next_hosts = list[idx] || list[idx - 1] || null
if (next_hosts) {
app.setState({current: next_hosts})
}
})
app.setState({list, current: next_hosts})
})
.catch(e => console.log(e))
}
68 changes: 58 additions & 10 deletions app/libs/treeFunc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@
* @homepage: https://oldj.net
*/

const flatTree = tree => {
let list = []

tree.map(t => {
list.push(t)
let {children} = t
if (Array.isArray(children) && children.length > 0) {
list = [...list, ...flatTree(children)]
/**
* 将 tree_list 树状对象变成一个平的数组
* @param tree_list {Array} 树状对象
* @param ignore_collapsed {Boolean} 是否忽略折叠起来的对象
* @returns {Array}
*/
function flatTree(tree_list, ignore_collapsed = false) {
let arr = []

Array.isArray(tree_list) && tree_list.map((item) => {
if (!item) return

arr.push(item)

if (ignore_collapsed && item.collapsed) return

if (item.children) {
let a2 = flatTree(item.children, ignore_collapsed)
arr = arr.concat(a2)
}
})

return list
return arr
}

const getItemById = (tree, id) => {
Expand Down Expand Up @@ -97,9 +108,46 @@ const updateTree = (tree, updates) => {
return tree
}

function getParentList (list, id) {
if (list.findIndex(i => i.id === id) > -1) return list

let fl = flatTree(list)
let found = false
let parent_list = []

fl.map((i) => {
if (found) return

if (i.id === id) {
found = true
parent_list = list
} else if (i.children && i.children.find((i2) => i2.id === id)) {
found = true
parent_list = i.children
}
})

return parent_list
}

function getUpItemWithCollapseState (list, id) {
let f_list = flatTree(list, true)
let idx = f_list.findIndex(i => i.id === id)
return idx > 0 ? f_list[idx - 1] : null
}

function getDownItemWithCollapseState (list, id) {
let f_list = flatTree(list, true)
let idx = f_list.findIndex(i => i.id === id)
return f_list[idx + 1] || null
}

module.exports = {
flatTree,
updateTree,
getItemById,
getItemDetailById
getItemDetailById,
removeItemFromTreeById,
getUpItemWithCollapseState,
getDownItemWithCollapseState
}
2 changes: 1 addition & 1 deletion app/ui/app.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/ui/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/ui/common.js.LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! SwitchHosts! common.js, 2019-11-18 10:51:26 */
/*! SwitchHosts! common.js, 2019-11-18 20:18:23 */

/*!
Copyright (c) 2017 Jed Watson.
Expand Down
2 changes: 1 addition & 1 deletion app/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = [3, 5, 1, 5489]
module.exports = [3, 5, 1, 5498]

0 comments on commit 39039b5

Please sign in to comment.