Home Reference Source Repository

lib/decoration.js

  1. 'use babel'
  2.  
  3. import _ from 'underscore-plus'
  4. import {Emitter} from 'atom'
  5.  
  6. var idCounter = 0
  7. var nextId = function () { return idCounter++ }
  8.  
  9. /**
  10. * The `Decoration` class represents a decoration in the Minimap.
  11. *
  12. * It has the same API than the `Decoration` class of a text editor.
  13. */
  14. export default class Decoration {
  15.  
  16. /**
  17. * Returns `true` if the passed-in decoration properties matches the
  18. * specified type.
  19. *
  20. * @param {Object} decorationProperties the decoration properties to match
  21. * @param {string} type the decoration type to match
  22. * @return {boolean} whether the decoration properties match the type
  23. */
  24. static isType (decorationProperties, type) {
  25. if (_.isArray(decorationProperties.type)) {
  26. if (decorationProperties.type.indexOf(type) >= 0) { return true }
  27. return false
  28. } else {
  29. return type === decorationProperties.type
  30. }
  31. }
  32.  
  33. /**
  34. * Creates a new decoration.
  35. *
  36. * @param {Marker} marker the target marker for the decoration
  37. * @param {Minimap} minimap the Minimap where the decoration will
  38. * be displayed
  39. * @param {Object} properties the decoration's properties
  40. */
  41. constructor (marker, minimap, properties) {
  42. /**
  43. * @access private
  44. */
  45. this.marker = marker
  46. /**
  47. * @access private
  48. */
  49. this.minimap = minimap
  50. /**
  51. * @access private
  52. */
  53. this.emitter = new Emitter()
  54. /**
  55. * @access private
  56. */
  57. this.id = nextId()
  58. /**
  59. * @access private
  60. */
  61. this.properties = null
  62. this.setProperties(properties)
  63. this.properties.id = this.id
  64. /**
  65. * @access private
  66. */
  67. this.destroyed = false
  68. /**
  69. * @access private
  70. */
  71. this.markerDestroyDisposable = this.marker.onDidDestroy(() => {
  72. this.destroy()
  73. })
  74. }
  75.  
  76. /**
  77. * Destroy this marker.
  78. *
  79. * If you own the marker, you should use `Marker#destroy` which will destroy
  80. * this decoration.
  81. */
  82. destroy () {
  83. if (this.destroyed) { return }
  84.  
  85. this.markerDestroyDisposable.dispose()
  86. this.markerDestroyDisposable = null
  87. this.destroyed = true
  88. this.emitter.emit('did-destroy')
  89. this.emitter.dispose()
  90. }
  91.  
  92. /**
  93. * Returns whether this decoration is destroyed or not.
  94. *
  95. * @return {boolean} whether this decoration is destroyed or not
  96. */
  97. isDestroyed () { return this.destroyed }
  98.  
  99. /**
  100. * Registers an event listener to the `did-change-properties` event.
  101. *
  102. * This event is triggered when the decoration update method is called.
  103. *
  104. * @param {function(change:Object):void} callback a function to call
  105. * when the event is triggered
  106. * @return {Disposable} a disposable to stop listening to the event
  107. */
  108. onDidChangeProperties (callback) {
  109. return this.emitter.on('did-change-properties', callback)
  110. }
  111.  
  112. /**
  113. * Registers an event listener to the `did-destroy` event.
  114. *
  115. * @param {function():void} callback a function to call when the event
  116. * is triggered
  117. * @return {Disposable} a disposable to stop listening to the event
  118. */
  119. onDidDestroy (callback) {
  120. return this.emitter.on('did-destroy', callback)
  121. }
  122.  
  123. /**
  124. * An id unique across all Decoration objects.
  125. *
  126. * @return {number} the decoration id
  127. */
  128. getId () { return this.id }
  129.  
  130. /**
  131. * Returns the marker associated with this Decoration.
  132. *
  133. * @return {Marker} the decoration's marker
  134. */
  135. getMarker () { return this.marker }
  136.  
  137. /**
  138. * Check if this decoration is of type `type`.
  139. *
  140. * @param {string|Array} type a type like `'line-number'`, `'line'`, etc.
  141. * `type` can also be an Array of Strings, where
  142. * it will return true if the decoration's type
  143. * matches any in the array.
  144. * @return {boolean} whether this decoration match the passed-in type
  145. */
  146. isType (type) {
  147. return Decoration.isType(this.properties, type)
  148. }
  149.  
  150. /**
  151. * Returns the Decoration's properties.
  152. *
  153. * @return {Object} the decoration's properties
  154. */
  155. getProperties () {
  156. return this.properties
  157. }
  158.  
  159. /**
  160. * Update the marker with new properties. Allows you to change the
  161. * decoration's class.
  162. *
  163. * @param {Object} newProperties the new properties for the decoration
  164. */
  165. setProperties (newProperties) {
  166. if (this.destroyed) { return }
  167.  
  168. let oldProperties = this.properties
  169. this.properties = newProperties
  170. this.properties.id = this.id
  171.  
  172. this.emitter.emit('did-change-properties', {oldProperties, newProperties})
  173. }
  174. }