ide.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS103: Rewrite code to no longer use __guard__
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. import App from '../../base'
  16. import EditorWatchdogManager from '../connection/EditorWatchdogManager'
  17. // We create and provide this as service so that we can access the global ide
  18. // from within other parts of the angular app.
  19. App.factory(
  20. 'ide',
  21. function ($http, queuedHttp, $modal, $q, $filter, $timeout, eventTracking) {
  22. const ide = {}
  23. ide.$http = $http
  24. ide.queuedHttp = queuedHttp
  25. ide.$q = $q
  26. ide.$filter = $filter
  27. ide.$timeout = $timeout
  28. ide.globalEditorWatchdogManager = new EditorWatchdogManager({
  29. onTimeoutHandler: meta => {
  30. eventTracking.sendMB('losing-edits', meta)
  31. // clone the meta object, reportError adds additional fields into it
  32. ide.reportError('losing-edits', Object.assign({}, meta))
  33. },
  34. })
  35. this.recentEvents = []
  36. ide.pushEvent = (type, meta) => {
  37. if (meta == null) {
  38. meta = {}
  39. }
  40. sl_console.log('event', type, meta)
  41. this.recentEvents.push({ type, meta, date: new Date() })
  42. if (this.recentEvents.length > 100) {
  43. return this.recentEvents.shift()
  44. }
  45. }
  46. ide.reportError = (error, meta) => {
  47. if (meta == null) {
  48. meta = {}
  49. }
  50. meta.user_id = window.user_id
  51. meta.project_id = window.project_id
  52. meta.client_id = __guard__(
  53. ide.socket != null ? ide.socket.socket : undefined,
  54. x => x.sessionid
  55. )
  56. meta.transport = __guard__(
  57. __guard__(
  58. ide.socket != null ? ide.socket.socket : undefined,
  59. x2 => x2.transport
  60. ),
  61. x1 => x1.name
  62. )
  63. meta.client_now = new Date()
  64. const errorObj = {}
  65. if (typeof error === 'object') {
  66. for (const key of Array.from(Object.getOwnPropertyNames(error))) {
  67. errorObj[key] = error[key]
  68. }
  69. } else if (typeof error === 'string') {
  70. errorObj.message = error
  71. }
  72. return $http.post('/error/client', {
  73. error: errorObj,
  74. meta,
  75. _csrf: window.csrfToken,
  76. })
  77. }
  78. ide.showGenericMessageModal = (title, message) =>
  79. $modal.open({
  80. templateUrl: 'genericMessageModalTemplate',
  81. controller: 'GenericMessageModalController',
  82. resolve: {
  83. title() {
  84. return title
  85. },
  86. message() {
  87. return message
  88. },
  89. },
  90. })
  91. ide.showOutOfSyncModal = (title, message, editorContent) =>
  92. $modal.open({
  93. templateUrl: 'outOfSyncModalTemplate',
  94. controller: 'OutOfSyncModalController',
  95. backdrop: false, // not dismissable by clicking background
  96. keyboard: false, // prevent dismiss via keyboard
  97. resolve: {
  98. title() {
  99. return title
  100. },
  101. message() {
  102. return message
  103. },
  104. editorContent() {
  105. return editorContent
  106. },
  107. },
  108. windowClass: 'out-of-sync-modal',
  109. })
  110. ide.showLockEditorMessageModal = (title, message) =>
  111. // modal to block the editor when connection is down
  112. $modal.open({
  113. templateUrl: 'lockEditorModalTemplate',
  114. controller: 'GenericMessageModalController',
  115. backdrop: false, // not dismissable by clicking background
  116. keyboard: false, // prevent dismiss via keyboard
  117. resolve: {
  118. title() {
  119. return title
  120. },
  121. message() {
  122. return message
  123. },
  124. },
  125. windowClass: 'lock-editor-modal',
  126. })
  127. return ide
  128. }
  129. )
  130. App.controller(
  131. 'GenericMessageModalController',
  132. function ($scope, $modalInstance, title, message) {
  133. $scope.title = title
  134. $scope.message = message
  135. return ($scope.done = () => $modalInstance.close())
  136. }
  137. )
  138. App.controller(
  139. 'OutOfSyncModalController',
  140. function ($scope, $window, title, message, editorContent) {
  141. $scope.title = title
  142. $scope.message = message
  143. $scope.editorContent = editorContent
  144. $scope.editorContentRows = editorContent.split('\n').length
  145. $scope.done = () => {
  146. // Reload the page to avoid staying in an inconsistent state.
  147. // https://github.com/overleaf/issues/issues/3694
  148. $window.location.reload()
  149. }
  150. }
  151. )
  152. function __guard__(value, transform) {
  153. return typeof value !== 'undefined' && value !== null
  154. ? transform(value)
  155. : undefined
  156. }