EditorManager.coffee 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. }
  13. @$scope.$on "entity:selected", (event, entity) =>
  14. if (@$scope.ui.view != "track-changes" and entity.type == "doc")
  15. @openDoc(entity)
  16. initialized = false
  17. @$scope.$on "file-tree:initialized", () =>
  18. if !initialized
  19. initialized = true
  20. @autoOpenDoc()
  21. autoOpenDoc: () ->
  22. open_doc_id =
  23. $.localStorage("doc.open_id.#{@$scope.project_id}") or
  24. @$scope.project.rootDoc_id
  25. return if !open_doc_id?
  26. doc = @ide.fileTreeManager.findEntityById(open_doc_id)
  27. return if !doc?
  28. @openDoc(doc)
  29. openDoc: (doc, options = {}) ->
  30. @$scope.ui.view = "editor"
  31. done = () =>
  32. if options.gotoLine?
  33. @$scope.$broadcast "editor:gotoLine", options.gotoLine
  34. if doc.id == @$scope.editor.open_doc_id and !options.forceReopen
  35. @$scope.$apply () =>
  36. done()
  37. return
  38. @$scope.editor.open_doc_id = doc.id
  39. $.localStorage "doc.open_id.#{@$scope.project_id}", doc.id
  40. @ide.fileTreeManager.selectEntity(doc)
  41. @$scope.editor.opening = true
  42. @_openNewDocument doc, (error, sharejs_doc) =>
  43. if error?
  44. @ide.showGenericServerErrorMessage()
  45. return
  46. @$scope.$broadcast "doc:opened"
  47. @$scope.$apply () =>
  48. @$scope.editor.opening = false
  49. @$scope.editor.sharejs_doc = sharejs_doc
  50. done()
  51. _openNewDocument: (doc, callback = (error, sharejs_doc) ->) ->
  52. current_sharejs_doc = @$scope.editor.sharejs_doc
  53. if current_sharejs_doc?
  54. current_sharejs_doc.leaveAndCleanUp()
  55. @_unbindFromDocumentEvents(current_sharejs_doc)
  56. new_sharejs_doc = Document.getDocument @ide, doc.id
  57. new_sharejs_doc.join (error) =>
  58. return callback(error) if error?
  59. @_bindToDocumentEvents(doc, new_sharejs_doc)
  60. callback null, new_sharejs_doc
  61. _bindToDocumentEvents: (doc, sharejs_doc) ->
  62. sharejs_doc.on "error", (error) =>
  63. @openDoc(doc, forceReopen: true)
  64. @ide.showGenericMessageModal(
  65. "Out of sync"
  66. "Sorry, this file has gone out of sync and we need to do a full refresh. Please let us know if this happens frequently."
  67. )
  68. sharejs_doc.on "externalUpdate", () =>
  69. @ide.showGenericMessageModal(
  70. "Document Updated Externally"
  71. "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."
  72. )
  73. _unbindFromDocumentEvents: (document) ->
  74. document.off()
  75. getCurrentDocValue: () ->
  76. @$scope.editor.sharejs_doc?.getSnapshot()
  77. getCurrentDocId: () ->
  78. @$scope.editor.open_doc_id