IndexedCollection.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import common from './common.js'
  2. class IndexedCollection {
  3. constructor() {
  4. this.list = []
  5. this.index = {}
  6. Object.defineProperty(this, 'length', {
  7. get: function () {
  8. return this.list.length
  9. },
  10. })
  11. if (typeof this.getIndex != 'function') {
  12. throw new Error('IndexedCollection.getIndex not implemented in subclass.')
  13. }
  14. }
  15. forEach(e) {
  16. this.list.forEach(e)
  17. }
  18. add(pano) {
  19. this.list.push(pano)
  20. this.index[this.getIndex(pano)] = pano
  21. }
  22. extend(e) {
  23. for (var t = 0; t < e.length; t++) this.add(e[t])
  24. }
  25. get(panoId) {
  26. return this.index[panoId]
  27. }
  28. first() {
  29. return this.list[0]
  30. }
  31. last() {
  32. return this.list[this.list.length - 1]
  33. }
  34. reIndex() {
  35. this.index = {}
  36. var e = this
  37. this.forEach(function (t) {
  38. e.index[e.getIndex(t)] = t
  39. })
  40. }
  41. filter(e) {
  42. var t = this.list.filter(e)
  43. return this.reIndex(), t
  44. }
  45. reduce(e, t) {
  46. return this.list.reduce(e, t)
  47. }
  48. sort(e) {
  49. return this.list.sort(e)
  50. }
  51. indexOf(e) {
  52. for (var t = 0; t < this.list.length; ++t) if (this.list[t] === e) return t
  53. return -1
  54. }
  55. clone() {
  56. //xzw add
  57. var newobj = new this.constructor()
  58. newobj.extend(this.list)
  59. return newobj
  60. }
  61. }
  62. export default IndexedCollection