8
0

position.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /**
  7. * Position in the tree. Position is always located before or after a node.
  8. *
  9. * @memberOf core.treeView
  10. */
  11. export default class Position {
  12. /**
  13. * Creates a position.
  14. *
  15. * @param {core.treeView.Element} parent Position parent element.
  16. * @param {Number} offset Position offset.
  17. */
  18. constructor( parent, offset ) {
  19. /**
  20. * Position parent element.
  21. *
  22. * @member {core.treeView.Element} core.treeView.Position#parent
  23. */
  24. this.parent = parent;
  25. /**
  26. * Position offset.
  27. *
  28. * @member {Number} core.treeView.Position#offset
  29. */
  30. this.offset = offset;
  31. }
  32. /**
  33. * Returns a new instance of Position with offset incremented by `shift` value.
  34. *
  35. * @param {Number} shift How position offset should get changed. Accepts negative values.
  36. * @returns {core.treeView.Position} Shifted position.
  37. */
  38. getShiftedBy( shift ) {
  39. let shifted = Position.createFromPosition( this );
  40. let offset = shifted.offset + shift;
  41. shifted.offset = offset < 0 ? 0 : offset;
  42. return shifted;
  43. }
  44. /**
  45. * Checks whether this position equals given position.
  46. *
  47. * @param {core.treeView.Position} otherPosition Position to compare with.
  48. * @returns {Boolean} True if positions are same.
  49. */
  50. isEqual( otherPosition ) {
  51. return this == otherPosition || ( this.parent == otherPosition.parent && this.offset == otherPosition.offset );
  52. }
  53. /**
  54. * Creates and returns a new instance of Position, which is equal to passed position.
  55. *
  56. * @param {core.treeView.Position} position Position to be cloned.
  57. * @returns {core.treeView.Position}
  58. */
  59. static createFromPosition( position ) {
  60. return new this( position.parent, position.offset );
  61. }
  62. }