BPUR: change to live tracking of dirty properties (variant price update still failing)

This commit is contained in:
Rob H
2013-06-07 09:26:00 +05:30
committed by Rohan Mitchell
parent c80cba7fa5
commit 5258cba2a2
3 changed files with 148 additions and 49 deletions

View File

@@ -36,29 +36,69 @@ productsApp.directive('ngDecimal', function () {
}
});
productsApp.directive('ngTrackProduct', function(){
return {
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
var property = attrs.ngTrackProduct;
var clean_value = angular.copy(scope.product[property]);
element.bind('blur', function() {
if (scope.product[property] == clean_value) removeCleanProperty(scope.dirtyProducts, scope.product.id, property);
else addDirtyProperty(scope.dirtyProducts, scope.product.id, property, scope.product[property]);
scope.$apply(scope.displayDirtyProducts());
});
}
}
});
productsApp.directive('ngTrackVariant', function(){
return {
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
var property = attrs.ngTrackVariant;
var clean_value = angular.copy(scope.variant[property]);
element.bind('blur', function() {
var dirtyVariants = {};
if (scope.dirtyProducts.hasOwnProperty(scope.product.id) && scope.dirtyProducts[scope.product.id].hasOwnProperty("variants")) dirtyVariants = scope.dirtyProducts[scope.product.id].variants;
if (scope.variant[property] == clean_value){
removeCleanProperty(dirtyVariants, scope.variant.id, property);
if (dirtyVariants == {}) removeCleanProperty(scope.dirtyProducts, scope.product.id, "variants");
}
else {
addDirtyProperty(dirtyVariants, scope.variant.id, property, scope.variant[property]);
addDirtyProperty(scope.dirtyProducts, scope.product.id, "variants", dirtyVariants);
}
scope.$apply(scope.displayDirtyProducts());
});
}
}
});
productsApp.controller('AdminBulkProductsCtrl', function($scope, $timeout, $http, dataFetcher) {
$scope.dirtyProducts = {};
$scope.updateStatusMessage = {
text: "",
style: {}
}
$scope.refreshSuppliers = function(){
dataFetcher('/enterprises/suppliers.json').then(function(data){
$scope.suppliers = data;
});
};
$scope.refreshProducts = function(){
dataFetcher('/admin/products/bulk_index.json').then(function(data){
$scope.products = angular.copy(data);
$scope.cleanProducts = angular.copy(data);
});
};
$scope.updateOnHand = function(product){
product.on_hand = onHand(product);
}
$scope.updateStatusMessage = {
text: "",
style: {}
}
$scope.updateProducts = function(productsToSubmit){
$scope.displayUpdating();
$http({
@@ -68,6 +108,7 @@ productsApp.controller('AdminBulkProductsCtrl', function($scope, $timeout, $http
})
.success(function(data){
if (angular.toJson($scope.products) == angular.toJson(data)){
$scope.products = angular.copy(data);
$scope.cleanProducts = angular.copy(data);
$scope.displaySuccess();
}
@@ -79,13 +120,12 @@ productsApp.controller('AdminBulkProductsCtrl', function($scope, $timeout, $http
$scope.displayFailure("Server returned with error status: "+status);
});
}
$scope.prepareProductsForSubmit = function(){
var productsToSubmit = getDirtyObjects($scope.products,$scope.cleanProducts);
productsToSubmit = filterSubmitProducts(productsToSubmit);
var productsToSubmit = filterSubmitProducts($scope.dirtyProducts);
$scope.updateProducts(productsToSubmit);
}
$scope.setMessage = function(model,text,style,timeout){
model.text = text;
model.style = style;
@@ -93,18 +133,24 @@ productsApp.controller('AdminBulkProductsCtrl', function($scope, $timeout, $http
$timeout(function() { $scope.setMessage(model,"",{},false); }, timeout, true);
}
}
$scope.displayUpdating = function(){
$scope.setMessage($scope.updateStatusMessage,"Updating...",{ color: "orange" },false);
}
$scope.displaySuccess = function(){
$scope.setMessage($scope.updateStatusMessage,"Update complete",{ color: "green" },3000);
}
$scope.displayFailure = function(failMessage){
$scope.setMessage($scope.updateStatusMessage,"Updating failed. "+failMessage,{ color: "red" },10000);
}
$scope.displayDirtyProducts = function(){
var changedProductCount = Object.keys($scope.dirtyProducts).length;
if (changedProductCount > 0) $scope.setMessage($scope.updateStatusMessage,"Changes to "+Object.keys($scope.dirtyProducts).length+" products remain unsaved.",{ color: "gray" },false);
else $scope.setMessage($scope.updateStatusMessage,"",{},false);
}
});
productsApp.factory('dataFetcher', function($http,$q){
@@ -221,31 +267,33 @@ function getDirtyObjects(testObjects, cleanObjects){
function filterSubmitProducts(productsToFilter){
var filteredProducts= [];
if (productsToFilter instanceof Array){
for (i in productsToFilter) {
if (productsToFilter[i].hasOwnProperty("id")){
if (productsToFilter instanceof Object){
var productKeys = Object.keys(productsToFilter);
for (i in productKeys) {
if (productsToFilter[productKeys[i]].hasOwnProperty("id")){
var filteredProduct = {};
var filteredVariants = [];
if (productsToFilter[i].hasOwnProperty("variants")){
for (j in productsToFilter[i].variants){
if (productsToFilter[i].variants[j].deleted_at == null && productsToFilter[i].variants[j].hasOwnProperty("id")){
if (productsToFilter[productKeys[i]].hasOwnProperty("variants")){
var variantKeys = Object.keys(productsToFilter[productKeys[i]].variants);
for (j in variantKeys){
if (productsToFilter[productKeys[i]].variants[variantKeys[j]].deleted_at == null && productsToFilter[productKeys[i]].variants[variantKeys[j]].hasOwnProperty("id")){
filteredVariants[j] = {};
filteredVariants[j].id = productsToFilter[i].variants[j].id;
if (productsToFilter[i].variants[j].hasOwnProperty("on_hand")) filteredVariants[j].on_hand = productsToFilter[i].variants[j].on_hand;
if (productsToFilter[i].variants[j].hasOwnProperty("price")) filteredVariants[j].price = productsToFilter[i].variants[j].price;
filteredVariants[j].id = productsToFilter[productKeys[i]].variants[variantKeys[j]].id;
if (productsToFilter[productKeys[i]].variants[variantKeys[j]].hasOwnProperty("on_hand")) filteredVariants[j].on_hand = productsToFilter[productKeys[i]].variants[variantKeys[j]].on_hand;
if (productsToFilter[productKeys[i]].variants[variantKeys[j]].hasOwnProperty("price")) filteredVariants[j].price = productsToFilter[productKeys[i]].variants[variantKeys[j]].price;
}
}
}
var hasUpdatableProperty = false;
filteredProduct.id = productsToFilter[i].id;
if (productsToFilter[i].hasOwnProperty("name")) { filteredProduct.name = productsToFilter[i].name; hasUpdatableProperty = true; }
if (productsToFilter[i].hasOwnProperty("supplier_id")) { filteredProduct.supplier_id = productsToFilter[i].supplier_id; hasUpdatableProperty = true; }
//if (productsToFilter[i].hasOwnProperty("master")) filteredProduct.master_attributes = productsToFilter[i].master
if (productsToFilter[i].hasOwnProperty("price")) { filteredProduct.price = productsToFilter[i].price; hasUpdatableProperty = true; }
if (productsToFilter[i].hasOwnProperty("on_hand") && filteredVariants.length == 0) { filteredProduct.on_hand = productsToFilter[i].on_hand; hasUpdatableProperty = true; } //only update if no variants present
if (productsToFilter[i].hasOwnProperty("available_on")) { filteredProduct.available_on = productsToFilter[i].available_on; hasUpdatableProperty = true; }
filteredProduct.id = productsToFilter[productKeys[i]].id;
if (productsToFilter[productKeys[i]].hasOwnProperty("name")) { filteredProduct.name = productsToFilter[productKeys[i]].name; hasUpdatableProperty = true; }
if (productsToFilter[productKeys[i]].hasOwnProperty("supplier_id")) { filteredProduct.supplier_id = productsToFilter[productKeys[i]].supplier_id; hasUpdatableProperty = true; }
//if (productsToFilter[productKeys[i]].hasOwnProperty("master")) filteredProduct.master_attributes = productsToFilter[productKeys[i]].master
if (productsToFilter[productKeys[i]].hasOwnProperty("price")) { filteredProduct.price = productsToFilter[productKeys[i]].price; hasUpdatableProperty = true; }
if (productsToFilter[productKeys[i]].hasOwnProperty("on_hand") && filteredVariants.length == 0) { filteredProduct.on_hand = productsToFilter[productKeys[i]].on_hand; hasUpdatableProperty = true; } //only update if no variants present
if (productsToFilter[productKeys[i]].hasOwnProperty("available_on")) { filteredProduct.available_on = productsToFilter[productKeys[i]].available_on; hasUpdatableProperty = true; }
if (filteredVariants.length > 0) { filteredProduct.variants_attributes = filteredVariants; hasUpdatableProperty = true; } // Note that the name of the property changes to enable mass assignment of variants attributes with rails
if (hasUpdatableProperty) filteredProducts.push(filteredProduct);
@@ -255,6 +303,22 @@ function filterSubmitProducts(productsToFilter){
return filteredProducts;
}
function addDirtyProperty(dirtyObjects, objectID, propertyName, propertyValue){
if (dirtyObjects.hasOwnProperty(objectID)){
dirtyObjects[objectID][propertyName] = propertyValue;
}
else {
dirtyObjects[objectID] = {};
dirtyObjects[objectID]["id"] = objectID;
dirtyObjects[objectID][propertyName] = propertyValue;
}
}
function removeCleanProperty(dirtyObjects, objectID, propertyName){
if (dirtyObjects.hasOwnProperty(objectID) && dirtyObjects[objectID].hasOwnProperty(propertyName)) delete dirtyObjects[objectID][propertyName];
if (dirtyObjects.hasOwnProperty(objectID) && Object.keys(dirtyObjects[objectID]).length <= 1) delete dirtyObjects[objectID];
}
function isEmpty(object){
for (var i in object){
if (object.hasOwnProperty(i)){