stackedMap.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. angular.module('ui.bootstrap.stackedMap', [])
  2. /**
  3. * A helper, internal data structure that acts as a map but also allows getting / removing
  4. * elements in the LIFO order
  5. */
  6. .factory('$$stackedMap', function() {
  7. return {
  8. createNew: function() {
  9. var stack = [];
  10. return {
  11. add: function(key, value) {
  12. stack.push({
  13. key: key,
  14. value: value
  15. });
  16. },
  17. get: function(key) {
  18. for (var i = 0; i < stack.length; i++) {
  19. if (key == stack[i].key) {
  20. return stack[i];
  21. }
  22. }
  23. },
  24. keys: function() {
  25. var keys = [];
  26. for (var i = 0; i < stack.length; i++) {
  27. keys.push(stack[i].key);
  28. }
  29. return keys;
  30. },
  31. top: function() {
  32. return stack[stack.length - 1];
  33. },
  34. remove: function(key) {
  35. var idx = -1;
  36. for (var i = 0; i < stack.length; i++) {
  37. if (key == stack[i].key) {
  38. idx = i;
  39. break;
  40. }
  41. }
  42. return stack.splice(idx, 1)[0];
  43. },
  44. removeTop: function() {
  45. return stack.splice(stack.length - 1, 1)[0];
  46. },
  47. length: function() {
  48. return stack.length;
  49. }
  50. };
  51. }
  52. };
  53. });