PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/bundles/app/strut.deck/SlideCollection.js

https://gitlab.com/yutiansut/Strut
JavaScript | 88 lines | 41 code | 10 blank | 37 comment | 0 complexity | fcbf43c2411f0f310891fb38da1facff MD5 | raw file
  1. /**
  2. * @author Matt Crinklaw-Vogt
  3. */
  4. define(["common/Calcium", "./Slide"],
  5. function(Backbone, Slide) {
  6. // TODO Is this even used anywhere? If not, should probably be deleted.
  7. /**
  8. * Comparator function for SlideCollection. Compares slides by their indexes.
  9. * @see Backbone.Collection.comparator
  10. *
  11. * @param {Slide} l
  12. * @param {Slide} r
  13. * @returns {number}
  14. */
  15. var slideComparator = function(l, r) {
  16. return l.get("index") - r.get("index");
  17. };
  18. /**
  19. * @class SlideCollection
  20. * @augments Backbone.Collection
  21. */
  22. return Backbone.Collection.extend({
  23. model: Slide,
  24. /**
  25. * Initialize collection model.
  26. */
  27. initialize: function() {
  28. this.on("add", this._updateIndexes, this);
  29. this.on("remove", this._updateIndexes, this);
  30. },
  31. /**
  32. * Update slide indexes on any changes to the contents of collection.
  33. *
  34. * @private
  35. */
  36. _updateIndexes: function() {
  37. this.models.forEach(function(model, idx) {
  38. return model.set("index", idx);
  39. });
  40. },
  41. /**
  42. * Update transition positions after slides have moved
  43. *
  44. * @param {Slide[]} slidesCopy
  45. * @returns {SlideCollection} this
  46. */
  47. slidesReorganized: function(slidesCopy) {
  48. var transitions = [];
  49. this.models.forEach(function(model, i) {
  50. transitions.push(slidesCopy[i].getPositionData());
  51. }, this);
  52. var silent = { silent: true };
  53. transitions.forEach(function(transition, i) {
  54. this.models[i].set(transition, silent);
  55. }, this);
  56. this.models.forEach(function(model, i) {
  57. model.set('index', i);
  58. });
  59. return this;
  60. },
  61. /**
  62. * Change position of slides in SlideWell if their order is changed in collection.
  63. *
  64. * @param {Slide} l
  65. * @param {Slide} r
  66. * @private
  67. */
  68. _swapTransitionPositions: function(l, r) {
  69. var silent, tempPosData;
  70. tempPosData = l.getPositionData();
  71. silent = {
  72. silent: true
  73. };
  74. l.set(r.getPositionData(), silent);
  75. r.set(tempPosData, silent);
  76. }
  77. });
  78. });