Advertisement
Guest User

Untitled

a guest
May 3rd, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. angular.module('umbraco').controller('Umbraco.PropertyEditors.CheckboxListController', function ($scope) {
  2. if (angular.isObject($scope.model.config.items)) {
  3. //now we need to format the items in the dictionary because we always want to have an array
  4. var newItems = [];
  5. var vals = _.values($scope.model.config.items);
  6. var keys = _.keys($scope.model.config.items);
  7. for (var i = 0; i < vals.length; i++) {
  8. newItems.push({
  9. id: keys[i],
  10. sortOrder: vals[i].sortOrder,
  11. value: vals[i].value
  12. });
  13. }
  14. //ensure the items are sorted by the provided sort order
  15. newItems.sort(function (a, b) {
  16. return a.sortOrder > b.sortOrder ? 1 : b.sortOrder > a.sortOrder ? -1 : 0;
  17. });
  18. //re-assign
  19. $scope.model.config.items = newItems;
  20. }
  21. function setupViewModel() {
  22. $scope.selectedItems = [];
  23. //now we need to check if the value is null/undefined, if it is we need to set it to "" so that any value that is set
  24. // to "" gets selected by default
  25. if ($scope.model.value === null || $scope.model.value === undefined) {
  26. $scope.model.value = [];
  27. }
  28. for (var i = 0; i < $scope.model.config.items.length; i++) {
  29. var isChecked = _.contains($scope.model.value, $scope.model.config.items[i].id);
  30. $scope.selectedItems.push({
  31. checked: isChecked,
  32. key: $scope.model.config.items[i].id,
  33. val: $scope.model.config.items[i].value
  34. });
  35. }
  36. }
  37. setupViewModel();
  38. //update the model when the items checked changes
  39. $scope.$watch('selectedItems', function (newVal, oldVal) {
  40. $scope.model.value = [];
  41. for (var x = 0; x < $scope.selectedItems.length; x++) {
  42. if ($scope.selectedItems[x].checked) {
  43. $scope.model.value.push($scope.selectedItems[x].key);
  44. }
  45. }
  46. }, true);
  47. //here we declare a special method which will be called whenever the value has changed from the server
  48. //this is instead of doing a watch on the model.value = faster
  49. $scope.model.onValueChanged = function (newVal, oldVal) {
  50. //update the display val again if it has changed from the server
  51. setupViewModel();
  52. };
  53. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement