Skip to content

Instantly share code, notes, and snippets.

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

  • Save otarim/69a1154f538e7c753711a772df841dd4 to your computer and use it in GitHub Desktop.

Select an option

Save otarim/69a1154f538e7c753711a772df841dd4 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])
}
}
}
}
// 生成sku
function getSku(leafs,result) {
leafs.forEach(function(leaf) {
var isBottom = false
if (leaf._parent) {
leaf._sku = (leaf._parent._sku ? leaf._parent._sku + ',' : '') + leaf.id
}
if (!leaf.child && leaf._parent) {
isBottom = true
} else if(leaf.child) {
if (leaf.child.items && leaf.child.items.length) {
getSku(leaf.child.items,result)
} else {
isBottom = true
}
}
if (isBottom) {
leaf.model = new SkuModel()
result.push({
spec_item_ids: leaf._sku,
_val: leaf.val,
model: leaf.model
})
}
})
}
// 根据规格生成sku视图
scope.$watch('product_specs', function(nVal,oVal) {
if (nVal && nVal !== oVal) {
// 生成提交数据所需sku
scope.product_skus = []
getSku(tree,scope.product_skus)
// 生成表格所需的rowspan结构
var len = scope.product_specs.length,
count = 0,
ret = {}
if (!len) return
while(len) {
len--
var spec = scope.product_specs[len]
ret[count] = [angular.copy(spec)].concat(ret[count] || [])
if (!count) {
count = spec.spec_items.length
} else {
if (!spec.spec_items.length) {
continue
} else {
count *= spec.spec_items.length
}
}
}
var spLen = scope.product_specs[0].spec_items.length
var len = scope.product_skus.length,
count = 0
while(count < len) {
Object.keys(ret).forEach(function(dist) {
if (count % dist === 0) {
ret[dist].forEach(function(_dist) {
var test = _dist.spec_items.shift()
if (test) {
scope.product_skus[count]._row = scope.product_skus[count]._row || []
scope.product_skus[count]._row.push({
text: test.item_name,
val: dist
})
_dist.spec_items.push(test)
}
})
}
})
count++
}
// 排序
scope.product_skus.forEach(function(sku) {
if (sku._row) {
sku._row.sort(function(a,b) {
return parseInt(a.val, 10) < parseInt(b.val, 10)
})
}
})
}
},true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment