EditorManager.coffee 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. define [
  2. "ide/editor/Document"
  3. "ide/editor/directives/aceEditor"
  4. "ide/editor/controllers/SavingNotificationController"
  5. ], (Document) ->
  6. class EditorManager
  7. constructor: (@ide, @$scope) ->
  8. @$scope.editor = {
  9. sharejs_doc: null
  10. open_doc_id: null
  11. opening: true
  12. gotoLine: null
  13. }
  14. @$scope.$on "entity:selected", (event, entity) =>
  15. if (@$scope.ui.view != "track-changes" and entity.type == "doc")
  16. @openDoc(entity)
  17. initialized = false
  18. @$scope.$on "file-tree:initialized", () =>
  19. if !initialized
  20. initialized = true
  21. @autoOpenDoc()
  22. autoOpenDoc: () ->
  23. open_doc_id =
  24. $.localStorage("doc.open_id.#{@$scope.project_id}") or
  25. @$scope.project.rootDoc_id
  26. return if !open_doc_id?
  27. doc = @ide.fileTreeManager.findEntityById(open_doc_id)
  28. return if !doc?
  29. @openDoc(doc)
  30. openDoc: (doc, options = {}) ->
  31. @$scope.ui.view = "editor"
  32. done = () =>
  33. if options.gotoLine?
  34. @$scope.$broadcast "editor:gotoLine", options.gotoLine
  35. if doc.id == @$scope.editor.open_doc_id and !options.forceReopen
  36. @$scope.$apply () =>
  37. done()
  38. return
  39. @$scope.editor.open_doc_id = doc.id
  40. $.localStorage "doc.open_id.#{@$scope.project_id}", doc.id
  41. @ide.fileTreeManager.selectEntity(doc)
  42. @$scope.editor.opening = true
  43. @_openNewDocument doc, (error, sharejs_doc) =>
  44. if error?
  45. @ide.showGenericServerErrorMessage()
  46. return
  47. @$scope.$broadcast "doc:opened"
  48. @$scope.$apply () =>
  49. @$scope.editor.opening = false
  50. @$scope.editor.sharejs_doc = sharejs_doc
  51. done()
  52. _openNewDocument: (doc, callback = (error, sharejs_doc) ->) ->
  53. current_sharejs_doc = @$scope.editor.sharejs_doc
  54. if current_sharejs_doc?
  55. current_sharejs_doc.leaveAndCleanUp()
  56. @_unbindFromDocumentEvents(current_sharejs_doc)
  57. new_sharejs_doc = Document.getDocument @ide, doc.id
  58. new_sharejs_doc.join (error) =>
  59. return callback(error) if error?
  60. @_bindToDocumentEvents(doc, new_sharejs_doc)
  61. callback null, new_sharejs_doc
  62. _bindToDocumentEvents: (doc, sharejs_doc) ->
  63. sharejs_doc.on "error", (error) =>
  64. @openDoc(doc, forceReopen: true)
  65. @ide.showGenericMessageModal(
  66. "Out of sync"
  67. "Sorry, this file has gone out of sync and we need to do a full refresh. Please let us know if this happens frequently."
  68. )
  69. sharejs_doc.on "externalUpdate", () =>
  70. @ide.showGenericMessageModal(
  71. "Document Updated Externally"
  72. "This document was just updated externally. Any recent changes you have made may have been overwritten. To see previous versions please look in the history."
  73. )
  74. _unbindFromDocumentEvents: (document) ->
  75. document.off()
  76. getCurrentDocValue: () ->
  77. @$scope.editor.sharejs_doc?.getSnapshot()
  78. getCurrentDocId: () ->
  79. @$scope.editor.open_doc_id