Created
August 27, 2015 20:24
-
-
Save alexhawkins/514c15735bba99d0850e to your computer and use it in GitHub Desktop.
cool lodash code
This file contains hidden or 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
/* checks to see if item id arrangement in packages has changed */ | |
let compareItemIds = (oldIds, newIds) => { | |
return _.difference(newIds, oldIds).concat(_.difference(oldIds, newIds)); | |
}; | |
/* gets the item of ids of current packages and orignal packages | |
and compares them for changes */ | |
let haveItemsChanged = () => { | |
let curIds = []; | |
let oldIds = []; | |
let currentPackages = $scope.packages; | |
let orginalPackages = $scope.orginalPackages; | |
return _.filter(orginalPackages, (pkg, index) => { | |
_.each(pkg.package_items, (item) => { | |
oldIds.push(_.pick(item, 'id').id); | |
}); | |
_.each(currentPackages[index].package_items, (item) => { | |
curIds.push(_.pick(item, 'id').id); | |
}); | |
return [].concat(compareItemIds(oldIds, curIds)).length > 0; | |
}).length > 0; | |
}; | |
/* updates items on drop if item different than | |
initial drag item, resets selected to true */ | |
$scope.onDrop = () => { | |
$scope.itemsSwapped = haveItemsChanged(); | |
if ($scope.itemsSwapped) { | |
$scope.reset(); // make sure all packages are selected on swap | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment