|
|
@@ -1,6 +1,7 @@
|
|
|
define [
|
|
|
"editor/ShareJsDoc"
|
|
|
"libs/backbone"
|
|
|
+ "libs/underscore"
|
|
|
], (ShareJsDoc) ->
|
|
|
class Document
|
|
|
@getDocument: (ide, doc_id) ->
|
|
|
@@ -9,18 +10,34 @@ define [
|
|
|
@openDocs[doc_id] = new Document(ide, doc_id)
|
|
|
return @openDocs[doc_id]
|
|
|
|
|
|
+ @hasUnsavedChanges: () ->
|
|
|
+ for doc_id, doc of (@openDocs or {})
|
|
|
+ return true if doc.hasBufferedOps()
|
|
|
+ return false
|
|
|
+
|
|
|
constructor: (@ide, @doc_id) ->
|
|
|
@connected = @ide.socket.socket.connected
|
|
|
@joined = false
|
|
|
@wantToBeJoined = false
|
|
|
+ @_checkConsistency = _.bind(@_checkConsistency, @)
|
|
|
@_bindToEditorEvents()
|
|
|
@_bindToSocketEvents()
|
|
|
|
|
|
- attachToAce: (ace) ->
|
|
|
- @doc?.attachToAce(ace)
|
|
|
+ attachToAce: (@ace) ->
|
|
|
+ @doc?.attachToAce(@ace)
|
|
|
+ editorDoc = @ace.getSession().getDocument()
|
|
|
+ editorDoc.on "change", @_checkConsistency
|
|
|
|
|
|
detachFromAce: () ->
|
|
|
@doc?.detachFromAce()
|
|
|
+ editorDoc = @ace?.getSession().getDocument()
|
|
|
+ editorDoc?.off "change", @_checkConsistency
|
|
|
+
|
|
|
+ _checkConsistency: () ->
|
|
|
+ editorValue = @ace?.getValue()
|
|
|
+ sharejsValue = @doc?.getSnapshot()
|
|
|
+ if editorValue != sharejsValue
|
|
|
+ @_onError "error", new Error("Editor text does not match server text")
|
|
|
|
|
|
getSnapshot: () ->
|
|
|
@doc?.getSnapshot()
|
|
|
@@ -28,6 +45,15 @@ define [
|
|
|
getType: () ->
|
|
|
@doc?.getType()
|
|
|
|
|
|
+ getInflightOp: () ->
|
|
|
+ @doc?.getInflightOp()
|
|
|
+
|
|
|
+ getPendingOp: () ->
|
|
|
+ @doc?.getPendingOp()
|
|
|
+
|
|
|
+ hasBufferedOps: () ->
|
|
|
+ @doc?.hasBufferedOps()
|
|
|
+
|
|
|
_bindToSocketEvents: () ->
|
|
|
@_onUpdateAppliedHandler = (update) => @_onUpdateApplied(update)
|
|
|
@ide.socket.on "otUpdateApplied", @_onUpdateAppliedHandler
|
|
|
@@ -72,6 +98,25 @@ define [
|
|
|
else
|
|
|
@_leaveDoc(callback)
|
|
|
|
|
|
+ pollSavedStatus: () ->
|
|
|
+ # returns false if doc has ops waiting to be acknowledged or
|
|
|
+ # sent that haven't changed since the last time we checked.
|
|
|
+ # Otherwise returns true.
|
|
|
+ inflightOp = @getInflightOp()
|
|
|
+ pendingOp = @getPendingOp()
|
|
|
+ if !inflightOp? and !pendingOp?
|
|
|
+ # there's nothing going on
|
|
|
+ saved = true
|
|
|
+ else if inflightOp == @oldInflightOp
|
|
|
+ saved = false
|
|
|
+ else if pendingOp?
|
|
|
+ saved = false
|
|
|
+ else
|
|
|
+ saved = true
|
|
|
+
|
|
|
+ @oldInflightOp = inflightOp
|
|
|
+ return saved
|
|
|
+
|
|
|
_cancelLeave: () ->
|
|
|
if @_leaveCallbacks?
|
|
|
delete @_leaveCallbacks
|