Skip to content

Instantly share code, notes, and snippets.

@otarim
Created April 28, 2017 09:19
Show Gist options
  • Select an option

  • Save otarim/5fc74da48b432be1a34d959c1cb34e28 to your computer and use it in GitHub Desktop.

Select an option

Save otarim/5fc74da48b432be1a34d959c1cb34e28 to your computer and use it in GitHub Desktop.
// 库存、规格
// [tree] 树模型
var tree = [{}]
// [tree] 生成主节点
var addSpec = function (type) {
var current = []
getBottomLeaf(tree,current)
// currentType.type = type
current.forEach(function(leaf) {
leaf.child = {
_parent: leaf,
type: type
}
})
}
// [tree] 生成叶子节点
var addSubSpec = function(sid,val,parent) {
var hint = []
getHintLeaf(tree, function(leaf) {
return leaf.child.type === parent
}, hint)
hint.forEach(function(leaf) {
leaf.child.items = leaf.child.items || []
var ret = {
_parent: leaf,
val: val,
id: sid
}
if (leaf.child.items[0] && leaf.child.items[0].child) {
ret.child = angular.copy(leaf.child.items[0].child)
// 修改引用
relink(ret)
}
leaf.child.items.push(ret)
})
}
// 增加子规格
scope.onAddSubSpec = function(spec) {
// 创建规格接口
genSubSpec(spec.id, val).then(function(val) {
spec.spec_items = spec.spec_items.concat(val)
buildFromSpecs()
scope.setCurrentSpec(null,false)
})
}
// 增加主规格
scope.onAddSpec = function(spec) {
if (!spec._check) {
var len = scope.product_specs.length
if (len === MAX_SPEC) {
$Sys.notify('最多只能添加' + MAX_SPEC + '个规格')
return
}
scope.product_specs.push({
id: spec.spec_id,
name: spec.name,
spec_items: []
})
spec._check = 1
buildFromSpecs()
}
scope.config.specs.show = false
}
// 商品主规格列表
scope.specs = []
scope.onShowSpec = function() {
getSpecs().then(function() {
scope.config.specs.show = true
})
}
scope.currentEditSpec = false
scope.onShowAddedSpec = function(index) {
scope.currentEditSpec = index
}
// 修改主规格项
scope.onToggleSpec = function(spec, currentSpec) {
if (spec._check) return
spec._check = 1
var id = currentSpec.id
scope.specs.filter(function(_spec) {
return _spec.spec_id === id
})[0]._check = 0
currentSpec.id = spec.spec_id
currentSpec.name = spec.name
currentSpec.spec_items = []
buildFromSpecs()
scope.currentEditSpec = false
}
// 删除主规格
scope.onRemoveSpec = function(item,index) {
scope.product_specs.splice(index,1)
scope.specs.filter(function(spec) {
return spec.spec_id === item.id
})[0]._check = 0
buildFromSpecs()
}
// 删除子规格
scope.onRemoveSubSpec = function(item,spec,index) {
spec.spec_items.splice(index,1)
buildFromSpecs()
}
// 修改节点的父级引用
var relink = function(leafs) {
if (leafs.child) {
leafs.child._parent = leafs
if (leafs.child.items) {
leafs.child.items.forEach(function(leaf) {
leaf._parent = leafs
relink(leaf)
})
}
}
}
// 获取最底部的节点
var getBottomLeaf = function(tree,result) {
tree.forEach(function(leaf) {
if (!leaf.child) {
result.push(leaf)
} else {
if (leaf.child.items) {
getBottomLeaf(leaf.child.items,result)
}
}
})
}
// 获取命中的叶子节点
var getHintLeaf = function(tree,matched,result) {
tree.forEach(function(leaf) {
if (matched(leaf)) {
result.push(leaf)
} else if (leaf.child && leaf.child.items) {
getHintLeaf(leaf.child.items,matched,result)
}
})
}
// 根据规格生成sku树
var buildFromSpecs = function() {
// 根据specs生成树
tree = [{}]
scope.product_specs.forEach(function(spec) {
if (!spec.spec_items.length) return
addSpec(spec.id)
spec.spec_items.forEach(function(subSpec) {
addSubSpec(subSpec.item_id,subSpec.item_name,spec.id)
})
})
}
// 删除循环引用
function removeCircular(obj) {
for(var i in obj) {
if (obj.hasOwnProperty(i)) {
if (i === '_parent') {
obj[i] = null
delete obj[i]
}
if (typeof obj[i] === 'object') {
removeCircular(obj[i])
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment