HistoryManager.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-return-assign,
  5. no-undef,
  6. no-unused-vars,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS101: Remove unnecessary use of Array.from
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * DS206: Consider reworking classes to avoid initClass
  15. * DS207: Consider shorter variations of null checks
  16. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  17. */
  18. define([
  19. 'moment',
  20. 'ide/colors/ColorManager',
  21. 'ide/history/util/displayNameForUser',
  22. 'ide/history/controllers/HistoryListController',
  23. 'ide/history/controllers/HistoryDiffController',
  24. 'ide/history/directives/infiniteScroll'
  25. ], function(moment, ColorManager, displayNameForUser) {
  26. let HistoryManager
  27. return (HistoryManager = (function() {
  28. HistoryManager = class HistoryManager {
  29. static initClass() {
  30. this.prototype.BATCH_SIZE = 10
  31. }
  32. constructor(ide, $scope) {
  33. this.ide = ide
  34. this.$scope = $scope
  35. this.reset()
  36. this.$scope.toggleHistory = () => {
  37. if (this.$scope.ui.view === 'history') {
  38. return this.hide()
  39. } else {
  40. return this.show()
  41. }
  42. }
  43. this.$scope.$watch('history.selection.updates', updates => {
  44. if (updates != null && updates.length > 0) {
  45. this._selectDocFromUpdates()
  46. return this.reloadDiff()
  47. }
  48. })
  49. this.$scope.$on('entity:selected', (event, entity) => {
  50. if (this.$scope.ui.view === 'history' && entity.type === 'doc') {
  51. this.$scope.history.selection.doc = entity
  52. return this.reloadDiff()
  53. }
  54. })
  55. }
  56. show() {
  57. this.$scope.ui.view = 'history'
  58. return this.reset()
  59. }
  60. hide() {
  61. this.$scope.ui.view = 'editor'
  62. // Make sure we run the 'open' logic for whatever is currently selected
  63. return this.$scope.$emit(
  64. 'entity:selected',
  65. this.ide.fileTreeManager.findSelectedEntity()
  66. )
  67. }
  68. reset() {
  69. return (this.$scope.history = {
  70. updates: [],
  71. nextBeforeTimestamp: null,
  72. atEnd: false,
  73. selection: {
  74. updates: [],
  75. doc: null,
  76. range: {
  77. fromV: null,
  78. toV: null,
  79. start_ts: null,
  80. end_ts: null
  81. }
  82. },
  83. diff: null
  84. })
  85. }
  86. autoSelectRecentUpdates() {
  87. if (this.$scope.history.updates.length === 0) {
  88. return
  89. }
  90. this.$scope.history.updates[0].selectedTo = true
  91. let indexOfLastUpdateNotByMe = 0
  92. for (let i = 0; i < this.$scope.history.updates.length; i++) {
  93. const update = this.$scope.history.updates[i]
  94. if (this._updateContainsUserId(update, this.$scope.user.id)) {
  95. break
  96. }
  97. indexOfLastUpdateNotByMe = i
  98. }
  99. return (this.$scope.history.updates[
  100. indexOfLastUpdateNotByMe
  101. ].selectedFrom = true)
  102. }
  103. fetchNextBatchOfUpdates() {
  104. let url = `/project/${this.ide.project_id}/updates?min_count=${
  105. this.BATCH_SIZE
  106. }`
  107. if (this.$scope.history.nextBeforeTimestamp != null) {
  108. url += `&before=${this.$scope.history.nextBeforeTimestamp}`
  109. }
  110. this.$scope.history.loading = true
  111. return this.ide.$http.get(url).then(response => {
  112. const { data } = response
  113. this._loadUpdates(data.updates)
  114. this.$scope.history.nextBeforeTimestamp = data.nextBeforeTimestamp
  115. if (data.nextBeforeTimestamp == null) {
  116. this.$scope.history.atEnd = true
  117. }
  118. return (this.$scope.history.loading = false)
  119. })
  120. }
  121. reloadDiff() {
  122. let { diff } = this.$scope.history
  123. const { updates, doc } = this.$scope.history.selection
  124. const {
  125. fromV,
  126. toV,
  127. start_ts,
  128. end_ts
  129. } = this._calculateRangeFromSelection()
  130. if (doc == null) {
  131. return
  132. }
  133. if (
  134. diff != null &&
  135. diff.doc === doc &&
  136. diff.fromV === fromV &&
  137. diff.toV === toV
  138. ) {
  139. return
  140. }
  141. this.$scope.history.diff = diff = {
  142. fromV,
  143. toV,
  144. start_ts,
  145. end_ts,
  146. doc,
  147. error: false,
  148. pathname: doc.name
  149. }
  150. if (!doc.deleted) {
  151. diff.loading = true
  152. let url = `/project/${this.$scope.project_id}/doc/${diff.doc.id}/diff`
  153. if (diff.fromV != null && diff.toV != null) {
  154. url += `?from=${diff.fromV}&to=${diff.toV}`
  155. }
  156. return this.ide.$http
  157. .get(url)
  158. .then(response => {
  159. const { data } = response
  160. diff.loading = false
  161. const { text, highlights } = this._parseDiff(data)
  162. diff.text = text
  163. return (diff.highlights = highlights)
  164. })
  165. .catch(function() {
  166. diff.loading = false
  167. return (diff.error = true)
  168. })
  169. } else {
  170. diff.deleted = true
  171. diff.restoreInProgress = false
  172. diff.restoreDeletedSuccess = false
  173. return (diff.restoredDocNewId = null)
  174. }
  175. }
  176. restoreDeletedDoc(doc) {
  177. const url = `/project/${this.$scope.project_id}/doc/${doc.id}/restore`
  178. return this.ide.$http.post(url, {
  179. name: doc.name,
  180. _csrf: window.csrfToken
  181. })
  182. }
  183. restoreDiff(diff) {
  184. const url = `/project/${this.$scope.project_id}/doc/${
  185. diff.doc.id
  186. }/version/${diff.fromV}/restore`
  187. return this.ide.$http.post(url, { _csrf: window.csrfToken })
  188. }
  189. _parseDiff(diff) {
  190. let row = 0
  191. let column = 0
  192. const highlights = []
  193. let text = ''
  194. const iterable = diff.diff || []
  195. for (let i = 0; i < iterable.length; i++) {
  196. var endColumn, endRow
  197. const entry = iterable[i]
  198. let content = entry.u || entry.i || entry.d
  199. if (!content) {
  200. content = ''
  201. }
  202. text += content
  203. const lines = content.split('\n')
  204. const startRow = row
  205. const startColumn = column
  206. if (lines.length > 1) {
  207. endRow = startRow + lines.length - 1
  208. endColumn = lines[lines.length - 1].length
  209. } else {
  210. endRow = startRow
  211. endColumn = startColumn + lines[0].length
  212. }
  213. row = endRow
  214. column = endColumn
  215. const range = {
  216. start: {
  217. row: startRow,
  218. column: startColumn
  219. },
  220. end: {
  221. row: endRow,
  222. column: endColumn
  223. }
  224. }
  225. if (entry.i != null || entry.d != null) {
  226. const name = displayNameForUser(entry.meta.user)
  227. const date = moment(entry.meta.end_ts).format('Do MMM YYYY, h:mm a')
  228. if (entry.i != null) {
  229. highlights.push({
  230. label: `Added by ${name} on ${date}`,
  231. highlight: range,
  232. hue: ColorManager.getHueForUserId(
  233. entry.meta.user != null ? entry.meta.user.id : undefined
  234. )
  235. })
  236. } else if (entry.d != null) {
  237. highlights.push({
  238. label: `Deleted by ${name} on ${date}`,
  239. strikeThrough: range,
  240. hue: ColorManager.getHueForUserId(
  241. entry.meta.user != null ? entry.meta.user.id : undefined
  242. )
  243. })
  244. }
  245. }
  246. }
  247. return { text, highlights }
  248. }
  249. _loadUpdates(updates) {
  250. if (updates == null) {
  251. updates = []
  252. }
  253. let previousUpdate = this.$scope.history.updates[
  254. this.$scope.history.updates.length - 1
  255. ]
  256. for (let update of Array.from(updates)) {
  257. update.pathnames = [] // Used for display
  258. const object = update.docs || {}
  259. for (let doc_id in object) {
  260. const doc = object[doc_id]
  261. doc.entity = this.ide.fileTreeManager.findEntityById(doc_id, {
  262. includeDeleted: true
  263. })
  264. update.pathnames.push(doc.entity.name)
  265. }
  266. for (let user of Array.from(update.meta.users || [])) {
  267. if (user != null) {
  268. user.hue = ColorManager.getHueForUserId(user.id)
  269. }
  270. }
  271. if (
  272. previousUpdate == null ||
  273. !moment(previousUpdate.meta.end_ts).isSame(
  274. update.meta.end_ts,
  275. 'day'
  276. )
  277. ) {
  278. update.meta.first_in_day = true
  279. }
  280. update.selectedFrom = false
  281. update.selectedTo = false
  282. update.inSelection = false
  283. previousUpdate = update
  284. }
  285. const firstLoad = this.$scope.history.updates.length === 0
  286. this.$scope.history.updates = this.$scope.history.updates.concat(
  287. updates
  288. )
  289. if (firstLoad) {
  290. return this.autoSelectRecentUpdates()
  291. }
  292. }
  293. _calculateRangeFromSelection() {
  294. let end_ts, start_ts, toV
  295. let fromV = (toV = start_ts = end_ts = null)
  296. const selected_doc_id =
  297. this.$scope.history.selection.doc != null
  298. ? this.$scope.history.selection.doc.id
  299. : undefined
  300. for (let update of Array.from(
  301. this.$scope.history.selection.updates || []
  302. )) {
  303. for (let doc_id in update.docs) {
  304. const doc = update.docs[doc_id]
  305. if (doc_id === selected_doc_id) {
  306. if (fromV != null && toV != null) {
  307. fromV = Math.min(fromV, doc.fromV)
  308. toV = Math.max(toV, doc.toV)
  309. start_ts = Math.min(start_ts, update.meta.start_ts)
  310. end_ts = Math.max(end_ts, update.meta.end_ts)
  311. } else {
  312. ;({ fromV } = doc)
  313. ;({ toV } = doc)
  314. ;({ start_ts } = update.meta)
  315. ;({ end_ts } = update.meta)
  316. }
  317. break
  318. }
  319. }
  320. }
  321. return { fromV, toV, start_ts, end_ts }
  322. }
  323. // Set the track changes selected doc to one of the docs in the range
  324. // of currently selected updates. If we already have a selected doc
  325. // then prefer this one if present.
  326. _selectDocFromUpdates() {
  327. let doc, doc_id
  328. const affected_docs = {}
  329. for (let update of Array.from(this.$scope.history.selection.updates)) {
  330. for (doc_id in update.docs) {
  331. doc = update.docs[doc_id]
  332. affected_docs[doc_id] = doc.entity
  333. }
  334. }
  335. let selected_doc = this.$scope.history.selection.doc
  336. if (selected_doc != null && affected_docs[selected_doc.id] != null) {
  337. // Selected doc is already open
  338. } else {
  339. for (doc_id in affected_docs) {
  340. doc = affected_docs[doc_id]
  341. selected_doc = doc
  342. break
  343. }
  344. }
  345. this.$scope.history.selection.doc = selected_doc
  346. return this.ide.fileTreeManager.selectEntity(selected_doc)
  347. }
  348. _updateContainsUserId(update, user_id) {
  349. for (let user of Array.from(update.meta.users)) {
  350. if ((user != null ? user.id : undefined) === user_id) {
  351. return true
  352. }
  353. }
  354. return false
  355. }
  356. }
  357. HistoryManager.initClass()
  358. return HistoryManager
  359. })())
  360. })