-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,843 additions
and
1,887 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,207 @@ | ||
module.exports = function(Product, Brand, AttributeSet, ImageService, GlobalCategory, $q, Category) { | ||
'ngInject'; | ||
var $productAdd = {}; | ||
|
||
/* | ||
* Wraps around multiple services, | ||
* and solves dependencies needed for AddProduct view variables | ||
* to be parsable | ||
*/ | ||
$productAdd.fill = function(globalCatId, pageLoader, sharedDataSet, | ||
sharedFormData, breadcrumbs, controlFlags, variationFactorIndices, ivFormData) { | ||
|
||
|
||
var deferred = $q.defer(); | ||
pageLoader.load('Downloading Attribute Sets..'); | ||
|
||
AttributeSet.getByCategory(globalCatId) | ||
.then(function(data) { | ||
sharedDataSet.AttributeSets = data.map(function(aset) { | ||
aset.AttributeSetTagMaps = aset.AttributeSetTagMaps.map(function(asti) { | ||
return asti.Tag.TagName; | ||
}); | ||
return aset; | ||
var angular = require('angular'); | ||
module.exports = function (Product, Brand, AttributeSet, ImageService, GlobalCategory, $q, Category, util) { | ||
'ngInject'; | ||
var $productAdd = {}; | ||
|
||
/** | ||
* | ||
* Rebuild variations array from set of attribute options in dataset | ||
* | ||
* @param {FormData} formData | ||
* @param {DataSet} dataSet | ||
*/ | ||
$productAdd.generateVariants = function (formData, dataSet) { | ||
var vHashSet = {}; | ||
var prevVariants = angular.copy(formData.Variants); | ||
prevVariants.forEach(function (elem, index) { | ||
vHashSet[elem.text] = prevVariants[index]; | ||
}); | ||
|
||
//Unset | ||
prevVariants = undefined; | ||
|
||
if (ivFormData) { | ||
pageLoader.load('Indexing AttributeSet'); | ||
sharedFormData.AttributeSet = sharedDataSet.AttributeSets[sharedDataSet.AttributeSets.map(function(o) { | ||
return o.AttributeSetId | ||
}).indexOf(ivFormData.AttributeSet.AttributeSetId)]; | ||
formData.Variants = []; | ||
var trackVariant = new Set(); | ||
|
||
var parse = function(ivFormData, FullAttributeSet) { | ||
pageLoader.load('Loading product data..'); | ||
var inverseResult = Product.deserialize(ivFormData, FullAttributeSet); | ||
var VARIANT_DUMMY_FACTOR = ''; | ||
var expand = function (A0, B0) { | ||
|
||
//copy it out | ||
Object.keys(inverseResult.formData).forEach(function(key) { | ||
sharedFormData[key] = inverseResult.formData[key]; | ||
}) | ||
var AVId = null; | ||
var BVId = null; | ||
var B = B0; | ||
var A = A0; | ||
|
||
console.log("After Inverse Transformation", sharedFormData); | ||
if (sharedFormData.Variants.length > 0) { | ||
controlFlags.variation = "enable"; | ||
if (_.has(A0, 'AttributeValue.AttributeValueId')) { | ||
AVId = A0.AttributeValue.AttributeValueId; | ||
A = A0.AttributeValue.AttributeValueEn; | ||
} | ||
sharedDataSet.attributeOptions = inverseResult.attributeOptions || sharedDataSet.attributeOptions; | ||
if (sharedDataSet.attributeOptions[1].options.length > 0) { | ||
variationFactorIndices.pushSecond(); | ||
|
||
if (angular.isDefined(B0)) { | ||
if (_.has(B0, 'AttributeValue.AttributeValueId')) { | ||
BVId = B0.AttributeValue.AttributeValueId; | ||
B = B0.AttributeValue.AttributeValueEn; | ||
} | ||
} else { | ||
//B is not defined | ||
B = VARIANT_DUMMY_FACTOR; | ||
} | ||
}; | ||
parse(ivFormData, sharedFormData.AttributeSet); | ||
|
||
var kpair = {}; | ||
var firstAttribute = { | ||
AttributeId: dataSet.attributeOptions[0].Attribute.AttributeId, | ||
AttributeValues: (!AVId ? [] : [{ | ||
AttributeValueId: AVId | ||
}]), | ||
ValueEn: A | ||
}; | ||
|
||
var secondAttribute = { | ||
AttributeId: dataSet.attributeOptions[1].Attribute.AttributeId, | ||
AttributeValues: (!BVId ? [] : [{ | ||
AttributeValueId: BVId | ||
}]), | ||
ValueEn: B | ||
}; | ||
|
||
kpair.FirstAttribute = firstAttribute; | ||
kpair.SecondAttribute = secondAttribute; | ||
kpair.text = util.variant.toString(firstAttribute, secondAttribute); | ||
//Copy default value over from main variant | ||
kpair.ProductNameEn = formData.MasterVariant.ProductNameEn; | ||
kpair.ProductNameTh = formData.MasterVariant.ProductNameTh; | ||
kpair.Display = dataSet.VariantDisplayOption[0].value; | ||
kpair.Visibility = true; | ||
kpair.DimensionUnit = "MM"; | ||
kpair.WeightUnit = "G"; | ||
kpair.Sku = (formData.MasterVariant.Sku || "SKU") + "-" + (Number((formData.Variants || []).length) + 1); | ||
kpair.OriginalPrice = formData.MasterVariant.OriginalPrice; | ||
kpair.SalePrice = formData.MasterVariant.SalePrice; | ||
kpair.Quantity = formData.MasterVariant.Quantity; | ||
kpair.Length = formData.MasterVariant.Length; | ||
kpair.Width = formData.MasterVariant.Width; | ||
kpair.Height = formData.MasterVariant.Height; | ||
kpair.Upc = formData.MasterVariant.Upc; | ||
kpair.Weight = formData.MasterVariant.Weight; | ||
kpair.DescriptionFullEn = formData.MasterVariant.DescriptionFullEn; | ||
kpair.DescriptionFullTh = formData.MasterVariant.DescriptionFullTh; | ||
kpair.DescriptionShortEn = formData.MasterVariant.DescriptionShortEn; | ||
kpair.DescriptionShortTh = formData.MasterVariant.DescriptionShortTh; | ||
kpair.Images = angular.copy(formData.MasterImages); | ||
kpair.VideoLinks = angular.copy(formData.VideoLinks); | ||
kpair.PrepareDay = formData.PrepareDay; | ||
kpair.SEO = angular.copy(formData.SEO || {}); | ||
kpair.SEO.ProductUrlKeyEn = ""; | ||
|
||
if (kpair.text in vHashSet) { | ||
//Replace with value from vHashSet | ||
kpair = vHashSet[kpair.text]; | ||
} | ||
|
||
var hashNew = (util.variant.toString(kpair.FirstAttribute, kpair.SecondAttribute)); | ||
if (!trackVariant.has(hashNew)) { | ||
//Only push new variant if don't exist | ||
|
||
formData.Variants.push(kpair); | ||
trackVariant.add(hashNew); | ||
} | ||
|
||
} | ||
|
||
pageLoader.load('Downloading Category Tree..'); | ||
//Load Global Cat | ||
GlobalCategory.getAll().then(function(data) { | ||
sharedDataSet.GlobalCategories = GlobalCategory.getAllForSeller(Category.transformNestedSetToUITree(data)); | ||
sharedFormData.GlobalCategories[0] = Category.findByCatId(globalCatId, sharedDataSet.GlobalCategories); | ||
breadcrumbs.globalCategory = Category.createCatStringById(globalCatId, sharedDataSet.GlobalCategories); | ||
console.log(breadcrumbs, "breadcrumb"); | ||
pageLoader.load('Preparing content..'); | ||
deferred.resolve(); | ||
}); | ||
//Multiply out unmultiplied options | ||
if (dataSet.attributeOptions && Object.keys(dataSet.attributeOptions).length > 0) { | ||
for (var aKey in dataSet.attributeOptions[0].options) { | ||
var A = dataSet.attributeOptions[0].options[aKey]; | ||
|
||
if (angular.isDefined(dataSet.attributeOptions[1]['options']) && dataSet.attributeOptions[1].options.length == 0) { | ||
expand(A); | ||
} | ||
|
||
for (var bKey in dataSet.attributeOptions[1].options) { | ||
var B = dataSet.attributeOptions[1].options[bKey]; | ||
expand(A, B); | ||
} | ||
} | ||
} | ||
|
||
formData.DefaultVariant = formData.Variants[0]; | ||
}; | ||
|
||
|
||
|
||
|
||
/** | ||
* | ||
* Fill product add page with data of related dependencies | ||
* | ||
* @param {Integer} globalCatId | ||
* @param {AddProductPageLoader} pageLoader | ||
* @param {DataSet} sharedDataSet | ||
* @param {FormData} sharedFormData | ||
* @param {object} breadcrumbs | ||
* @param {object} controlFlags | ||
* @param {object} variationFactorIndices | ||
* @param {InverseFormData} ivFormData (Optional) | ||
*/ | ||
$productAdd.fill = function (globalCatId, pageLoader, sharedDataSet, | ||
sharedFormData, breadcrumbs, controlFlags, variationFactorIndices, ivFormData) { | ||
|
||
|
||
var deferred = $q.defer(); | ||
pageLoader.load('Downloading Attribute Sets..'); | ||
|
||
AttributeSet.getByCategory(globalCatId) | ||
.then(function (data) { | ||
sharedDataSet.AttributeSets = data.map(function (aset) { | ||
aset.AttributeSetTagMaps = aset.AttributeSetTagMaps.map(function (asti) { | ||
return asti.Tag.TagName; | ||
}); | ||
return aset; | ||
}); | ||
|
||
|
||
if (ivFormData) { | ||
pageLoader.load('Indexing AttributeSet'); | ||
sharedFormData.AttributeSet = sharedDataSet.AttributeSets[sharedDataSet.AttributeSets.map(function (o) { | ||
return o.AttributeSetId | ||
}).indexOf(ivFormData.AttributeSet.AttributeSetId)]; | ||
|
||
var parse = function (ivFormData, FullAttributeSet) { | ||
pageLoader.load('Loading product data..'); | ||
var inverseResult = Product.deserialize(ivFormData, FullAttributeSet); | ||
|
||
//copy it out | ||
Object.keys(inverseResult.formData).forEach(function (key) { | ||
sharedFormData[key] = inverseResult.formData[key]; | ||
}) | ||
|
||
console.log("After Inverse Transformation", sharedFormData); | ||
if (sharedFormData.Variants.length > 0) { | ||
controlFlags.variation = "enable"; | ||
} | ||
sharedDataSet.attributeOptions = inverseResult.attributeOptions || sharedDataSet.attributeOptions; | ||
if (sharedDataSet.attributeOptions[1].options.length > 0) { | ||
variationFactorIndices.pushSecond(); | ||
} | ||
}; | ||
parse(ivFormData, sharedFormData.AttributeSet); | ||
} | ||
|
||
pageLoader.load('Downloading Category Tree..'); | ||
//Load Global Cat | ||
GlobalCategory.getAll().then(function (data) { | ||
sharedDataSet.GlobalCategories = GlobalCategory.getAllForSeller(Category.transformNestedSetToUITree(data)); | ||
sharedFormData.GlobalCategories[0] = Category.findByCatId(globalCatId, sharedDataSet.GlobalCategories); | ||
breadcrumbs.globalCategory = Category.createCatStringById(globalCatId, sharedDataSet.GlobalCategories); | ||
console.log(breadcrumbs, "breadcrumb"); | ||
pageLoader.load('Preparing content..'); | ||
deferred.resolve(); | ||
}); | ||
|
||
|
||
}); | ||
}); | ||
|
||
return deferred.promise; | ||
}; | ||
return deferred.promise; | ||
}; | ||
|
||
return $productAdd; | ||
return $productAdd; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.