Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
terragou committed Mar 1, 2016
1 parent 5793449 commit 3316d7f
Show file tree
Hide file tree
Showing 10 changed files with 1,843 additions and 1,887 deletions.
279 changes: 64 additions & 215 deletions angular/controllers/productAdd.js

Large diffs are not rendered by default.

1,230 changes: 613 additions & 617 deletions angular/services/product.js

Large diffs are not rendered by default.

249 changes: 192 additions & 57 deletions angular/services/productAdd.js
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;
};
27 changes: 24 additions & 3 deletions angular/template-options/addProductForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = {
}
},
MasterVariant_SalePrice: {
'labelClass': 'required',
'labelClass': 'required',
'error': {
'messages': {
'min': 'Sale price must be lower than the original price',
Expand Down Expand Up @@ -114,7 +114,7 @@ module.exports = {
},
'unit': 'Day'
},
MasterVariant_Length: {
MasterVariant_Dimension: {
'error': {
'messages': {
'required': 'This is a required field',
Expand All @@ -124,10 +124,31 @@ module.exports = {
},
RelatedProducts: {
'inputSize': 'xxl',
'error': {
'error': {
'messages': {
'maxtagcount': 'Cannot exceed 10 related products'
}
}
},
Nothing: {

},
SEO_BoostingWeight: {
'error': {
'messages': {
'max': 'Only numbers from 1 to 10000 is allowed',
'min': 'Only numbers from 1 to 10000 is allowed',
'pattern': 'Only numbers from 1 to 10000 is allowed'
}
}
},
Remark: {
'inputSize': 'normal',
'formGroupClass': 'margin-top-30',
'error': {
'messages': {
'pattern': 'Special characters are not allowed'
}
}
}
}
12 changes: 11 additions & 1 deletion angular/template.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Generated by grunt-angular-templates
* Tue Mar 01 2016 11:54:15 GMT+0700 (Russia TZ 6 Standard Time)
* Tue Mar 01 2016 16:37:38 GMT+0700 (SE Asia Standard Time)
*/
module.exports = ["$templateCache", function($templateCache) { 'use strict';

Expand Down Expand Up @@ -29,6 +29,11 @@ module.exports = ["$templateCache", function($templateCache) { 'use strict';
);


$templateCache.put('common/input/div-with-label',
"<div><label ng-class=\"options.labelClass || {}\">{{ label }}</label><ng-transclude class=\"{{ options.transcludeClasses }}\" ng-class=\"{ 'has-error' : isInvalid(templateField()) }\"></ng-transclude><span class=\"help-block color-red\" ng-if=isInvalid(templateField()) ng-repeat=\"(key, prop) in (templateField().$error) track by key\"><span ng-bind-html=options.error.messages[key]></span></span></div>"
);


$templateCache.put('common/input/dropdown',
"<div class=form-group><div class=width-label><label class=control-label ng-class=options.labelClass ng-bind-html=options.label></label></div><div ng-class=\"['width-field-' + (options.inputSize || 'normal')]\"><ng-transclude ng-class=\"{ 'has-error-parent' : options.error.show }\"></ng-transclude><span class=\"help-block color-red\" ng-if=options.error ng-show=options.error.show ng-repeat=\"(key, prop) in options.error.conditions\"><span ng-bind-html=options.error.messages[key]></span></span></div><div ng-if=options.tooltip class=\"width-field-tooltip no-padding-left\"><i class=\"fa fa-2x fa-question-circle color-grey\" data-toggle=tooltip data-placement=right title={{options.tooltip}}></i></div></div>"
);
Expand Down Expand Up @@ -71,6 +76,11 @@ module.exports = ["$templateCache", function($templateCache) { 'use strict';
);


$templateCache.put('common/input/text-column-no-label',
"<div class=input-column><ng-transclude class=\"{{ options.transcludeClasses }}\" ng-class=\"{ 'has-error' : isInvalid(templateField()) }\"></ng-transclude><span class=\"help-block color-red\" ng-if=options.error ng-show=options.error.show ng-repeat=\"(key, prop) in (templateField().$error) track by key\">{{ options.error.messages[key] }}</span></div>"
);


$templateCache.put('common/input/text-column',
"<div class=input-column><ng-transclude></ng-transclude><span class=\"help-block color-red\" ng-if=options.error ng-show=options.error.show ng-repeat=\"(key, prop) in options.error.conditions\">{{ options.error.messages[key] }}</span></div>"
);
Expand Down
Loading

0 comments on commit 3316d7f

Please sign in to comment.