PageRenderTime 23ms CodeModel.GetById 18ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/Frameworks/Debug/AppKit/rhino.platform/CPDocument.j

http://github.com/jfahrenkrug/MapKit-HelloWorld
Unknown | 365 lines | 364 code | 1 blank | 0 comment | 0 complexity | 283ed4ff8c97dcecbefe6e90b770f039 MD5 | raw file
  1I;21;Foundation/CPString.jI;20;Foundation/CPArray.ji;13;CPResponder.ji;20;CPWindowController.jc;17281;
  2CPSaveOperation = 0;
  3CPSaveAsOperation = 1;
  4CPSaveToOperation = 2;
  5CPAutosaveOperation = 3;
  6CPChangeDone = 0;
  7CPChangeUndone = 1;
  8CPChangeCleared = 2;
  9CPChangeReadOtherContents = 3;
 10CPChangeAutosaved = 4;
 11CPDocumentWillSaveNotification = "CPDocumentWillSaveNotification";
 12CPDocumentDidSaveNotification = "CPDocumentDidSaveNotification";
 13CPDocumentDidFailToSaveNotification = "CPDocumentDidFailToSaveNotification";
 14var CPDocumentUntitledCount = 0;
 15{var the_class = objj_allocateClassPair(CPResponder, "CPDocument"),
 16meta_class = the_class.isa;class_addIvars(the_class, [new objj_ivar("_fileURL"), new objj_ivar("_fileType"), new objj_ivar("_windowControllers"), new objj_ivar("_untitledDocumentIndex"), new objj_ivar("_hasUndoManager"), new objj_ivar("_undoManager"), new objj_ivar("_changeCount"), new objj_ivar("_readConnection"), new objj_ivar("_writeRequest")]);
 17objj_registerClassPair(the_class);
 18objj_addClassForBundle(the_class, objj_getBundleWithPath(OBJJ_CURRENT_BUNDLE.path));
 19class_addMethods(the_class, [new objj_method(sel_getUid("init"), function $CPDocument__init(self, _cmd)
 20{ with(self)
 21{
 22    self = objj_msgSendSuper({ receiver:self, super_class:objj_getClass("CPResponder") }, "init");
 23    if (self)
 24    {
 25        _windowControllers = [];
 26        _hasUndoManager = YES;
 27        _changeCount = 0;
 28        objj_msgSend(self, "setNextResponder:", CPApp);
 29    }
 30    return self;
 31}
 32}), new objj_method(sel_getUid("initWithType:error:"), function $CPDocument__initWithType_error_(self, _cmd, aType, anError)
 33{ with(self)
 34{
 35    self = objj_msgSend(self, "init");
 36    if (self)
 37        objj_msgSend(self, "setFileType:", aType);
 38    return self;
 39}
 40}), new objj_method(sel_getUid("initWithContentsOfURL:ofType:delegate:didReadSelector:contextInfo:"), function $CPDocument__initWithContentsOfURL_ofType_delegate_didReadSelector_contextInfo_(self, _cmd, anAbsoluteURL, aType, aDelegate, aDidReadSelector, aContextInfo)
 41{ with(self)
 42{
 43    self = objj_msgSend(self, "init");
 44    if (self)
 45    {
 46        objj_msgSend(self, "readFromURL:ofType:delegate:didReadSelector:contextInfo:", anAbsoluteURL, aType, aDelegate, aDidReadSelector, aContextInfo);
 47        objj_msgSend(self, "setFileURL:", anAbsoluteURL);
 48        objj_msgSend(self, "setFileType:", aType);
 49    }
 50    return self;
 51}
 52}), new objj_method(sel_getUid("initForURL:withContentsOfURL:ofType:delegate:didReadSelector:contextInfo:"), function $CPDocument__initForURL_withContentsOfURL_ofType_delegate_didReadSelector_contextInfo_(self, _cmd, anAbsoluteURL, absoluteContentsURL, aType, aDelegate, aDidReadSelector, aContextInfo)
 53{ with(self)
 54{
 55    self = objj_msgSend(self, "init");
 56    if (self)
 57    {
 58        objj_msgSend(self, "readFromURL:ofType:delegate:didReadSelector:contextInfo:", absoluteContentsURL, aType, aDelegate, aDidReadSelector, aContextInfo);
 59        objj_msgSend(self, "setFileURL:", anAbsoluteURL);
 60        objj_msgSend(self, "setFileType:", aType);
 61    }
 62    return self;
 63}
 64}), new objj_method(sel_getUid("dataOfType:error:"), function $CPDocument__dataOfType_error_(self, _cmd, aType, anError)
 65{ with(self)
 66{
 67    objj_msgSend(CPException, "raise:reason:", CPUnsupportedMethodException, "dataOfType:error: must be overridden by the document subclass.");
 68}
 69}), new objj_method(sel_getUid("readFromData:ofType:error:"), function $CPDocument__readFromData_ofType_error_(self, _cmd, aData, aType, anError)
 70{ with(self)
 71{
 72    objj_msgSend(CPException, "raise:reason:", CPUnsupportedMethodException, "readFromData:ofType: must be overridden by the document subclass.");
 73}
 74}), new objj_method(sel_getUid("makeWindowControllers"), function $CPDocument__makeWindowControllers(self, _cmd)
 75{ with(self)
 76{
 77    var controller = objj_msgSend(objj_msgSend(CPWindowController, "alloc"), "initWithWindowCibName:", nil);
 78    objj_msgSend(self, "addWindowController:", controller);
 79}
 80}), new objj_method(sel_getUid("windowControllers"), function $CPDocument__windowControllers(self, _cmd)
 81{ with(self)
 82{
 83    return _windowControllers;
 84}
 85}), new objj_method(sel_getUid("addWindowController:"), function $CPDocument__addWindowController_(self, _cmd, aWindowController)
 86{ with(self)
 87{
 88    objj_msgSend(_windowControllers, "addObject:", aWindowController);
 89    if (objj_msgSend(aWindowController, "document") != self)
 90    {
 91        objj_msgSend(aWindowController, "setNextResponder:", self);
 92        objj_msgSend(aWindowController, "setDocument:", self);
 93    }
 94}
 95}), new objj_method(sel_getUid("showWindows"), function $CPDocument__showWindows(self, _cmd)
 96{ with(self)
 97{
 98    objj_msgSend(_windowControllers, "makeObjectsPerformSelector:withObject:", sel_getUid("showWindow:"), self);
 99}
100}), new objj_method(sel_getUid("displayName"), function $CPDocument__displayName(self, _cmd)
101{ with(self)
102{
103    if (_fileURL)
104        return objj_msgSend(_fileURL, "lastPathComponent");
105    if (!_untitledDocumentIndex)
106        _untitledDocumentIndex = ++CPDocumentUntitledCount;
107 if (_untitledDocumentIndex == 1)
108    return "Untitled";
109 return "Untitled " + _untitledDocumentIndex;
110}
111}), new objj_method(sel_getUid("windowCibName"), function $CPDocument__windowCibName(self, _cmd)
112{ with(self)
113{
114    return nil;
115}
116}), new objj_method(sel_getUid("windowControllerDidLoadNib:"), function $CPDocument__windowControllerDidLoadNib_(self, _cmd, aWindowController)
117{ with(self)
118{
119}
120}), new objj_method(sel_getUid("windowControllerWillLoadNib:"), function $CPDocument__windowControllerWillLoadNib_(self, _cmd, aWindowController)
121{ with(self)
122{
123}
124}), new objj_method(sel_getUid("readFromURL:ofType:delegate:didReadSelector:contextInfo:"), function $CPDocument__readFromURL_ofType_delegate_didReadSelector_contextInfo_(self, _cmd, anAbsoluteURL, aType, aDelegate, aDidReadSelector, aContextInfo)
125{ with(self)
126{
127    objj_msgSend(_readConnection, "cancel");
128    _readConnection = objj_msgSend(CPURLConnection, "connectionWithRequest:delegate:", objj_msgSend(CPURLRequest, "requestWithURL:", anAbsoluteURL), self);
129    _readConnection.session = _CPReadSessionMake(aType, aDelegate, aDidReadSelector, aContextInfo);
130}
131}), new objj_method(sel_getUid("fileURL"), function $CPDocument__fileURL(self, _cmd)
132{ with(self)
133{
134    return _fileURL;
135}
136}), new objj_method(sel_getUid("setFileURL:"), function $CPDocument__setFileURL_(self, _cmd, aFileURL)
137{ with(self)
138{
139    if (_fileURL == aFileURL)
140        return;
141    _fileURL = aFileURL;
142    objj_msgSend(_windowControllers, "makeObjectsPerformSelector:", sel_getUid("synchronizeWindowTitleWithDocumentName"));
143}
144}), new objj_method(sel_getUid("saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:"), function $CPDocument__saveToURL_ofType_forSaveOperation_delegate_didSaveSelector_contextInfo_(self, _cmd, anAbsoluteURL, aTypeName, aSaveOperation, aDelegate, aDidSaveSelector, aContextInfo)
145{ with(self)
146{
147    var data = objj_msgSend(self, "dataOfType:error:", objj_msgSend(self, "fileType"), nil),
148        oldChangeCount = _changeCount;
149    _writeRequest = objj_msgSend(CPURLRequest, "requestWithURL:", anAbsoluteURL);
150    objj_msgSend(_writeRequest, "setHTTPMethod:", "POST");
151    objj_msgSend(_writeRequest, "setHTTPBody:", objj_msgSend(data, "string"));
152    objj_msgSend(_writeRequest, "setValue:forHTTPHeaderField:", "close", "Connection");
153    if (aSaveOperation == CPSaveOperation)
154        objj_msgSend(_writeRequest, "setValue:forHTTPHeaderField:", "true", "x-cappuccino-overwrite");
155    if (aSaveOperation != CPSaveToOperation)
156        objj_msgSend(self, "updateChangeCount:", CPChangeCleared);
157    var connection = objj_msgSend(CPURLConnection, "connectionWithRequest:delegate:", _writeRequest, self);
158    connection.session = _CPSaveSessionMake(anAbsoluteURL, aSaveOperation, oldChangeCount, aDelegate, aDidSaveSelector, aContextInfo, connection);
159}
160}), new objj_method(sel_getUid("connection:didReceiveResponse:"), function $CPDocument__connection_didReceiveResponse_(self, _cmd, aConnection, aResponse)
161{ with(self)
162{
163    if (!objj_msgSend(aResponse, "isKindOfClass:", objj_msgSend(CPHTTPURLResponse, "class")))
164        return;
165    var statusCode = objj_msgSend(aResponse, "statusCode");
166    if (statusCode === 200)
167        return;
168    var session = aConnection.session;
169    if (aConnection == _readConnection)
170    {
171        objj_msgSend(aConnection, "cancel");
172        alert("There was an error retrieving the document.");
173        objj_msgSend(session.delegate, session.didReadSelector, self, NO, session.contextInfo);
174    }
175    else
176    {
177        if (statusCode == 409)
178        {
179            objj_msgSend(aConnection, "cancel");
180            if (confirm("There already exists a file with that name, would you like to overwrite it?"))
181            {
182                objj_msgSend(_writeRequest, "setValue:forHTTPHeaderField:", "true", "x-cappuccino-overwrite");
183                objj_msgSend(aConnection, "start");
184            }
185            else
186            {
187                if (session.saveOperation != CPSaveToOperation)
188                {
189                    _changeCount += session.changeCount;
190                    objj_msgSend(_windowControllers, "makeObjectsPerformSelector:withObject:", sel_getUid("setDocumentEdited:"), objj_msgSend(self, "isDocumentEdited"));
191                }
192                _writeRequest = nil;
193                objj_msgSend(session.delegate, session.didSaveSelector, self, NO, session.contextInfo);
194            }
195        }
196    }
197}
198}), new objj_method(sel_getUid("connection:didReceiveData:"), function $CPDocument__connection_didReceiveData_(self, _cmd, aConnection, aData)
199{ with(self)
200{
201    var session = aConnection.session;
202    if (aConnection == _readConnection)
203    {
204        objj_msgSend(self, "readFromData:ofType:error:", objj_msgSend(CPData, "dataWithString:", aData), session.fileType, nil);
205        objj_msgSend(session.delegate, session.didReadSelector, self, YES, session.contextInfo);
206    }
207    else
208    {
209        if (session.saveOperation != CPSaveToOperation)
210            objj_msgSend(self, "setFileURL:", session.absoluteURL);
211        _writeRequest = nil;
212        objj_msgSend(session.delegate, session.didSaveSelector, self, YES, session.contextInfo);
213    }
214}
215}), new objj_method(sel_getUid("connection:didFailWithError:"), function $CPDocument__connection_didFailWithError_(self, _cmd, aConnection, anError)
216{ with(self)
217{
218    var session = aConnection.session;
219    if (_readConnection == aConnection)
220        objj_msgSend(session.delegate, session.didReadSelector, self, NO, session.contextInfo);
221    else
222    {
223        if (session.saveOperation != CPSaveToOperation)
224        {
225            _changeCount += session.changeCount;
226            objj_msgSend(_windowControllers, "makeObjectsPerformSelector:withObject:", sel_getUid("setDocumentEdited:"), objj_msgSend(self, "isDocumentEdited"));
227        }
228        _writeRequest = nil;
229        alert("There was an error saving the document.");
230        objj_msgSend(session.delegate, session.didSaveSelector, self, NO, session.contextInfo);
231    }
232}
233}), new objj_method(sel_getUid("connectionDidFinishLoading:"), function $CPDocument__connectionDidFinishLoading_(self, _cmd, aConnection)
234{ with(self)
235{
236    if (_readConnection == aConnection)
237        _readConnection = nil;
238}
239}), new objj_method(sel_getUid("isDocumentEdited"), function $CPDocument__isDocumentEdited(self, _cmd)
240{ with(self)
241{
242    return _changeCount != 0;
243}
244}), new objj_method(sel_getUid("updateChangeCount:"), function $CPDocument__updateChangeCount_(self, _cmd, aChangeType)
245{ with(self)
246{
247    if (aChangeType == CPChangeDone)
248        ++_changeCount;
249    else if (aChangeType == CPChangeUndone)
250        --_changeCount;
251    else if (aChangeType == CPChangeCleared)
252        _changeCount = 0;
253    objj_msgSend(_windowControllers, "makeObjectsPerformSelector:withObject:", sel_getUid("setDocumentEdited:"), objj_msgSend(self, "isDocumentEdited"));
254}
255}), new objj_method(sel_getUid("setFileType:"), function $CPDocument__setFileType_(self, _cmd, aType)
256{ with(self)
257{
258    _fileType = aType;
259}
260}), new objj_method(sel_getUid("fileType"), function $CPDocument__fileType(self, _cmd)
261{ with(self)
262{
263    return _fileType;
264}
265}), new objj_method(sel_getUid("hasUndoManager"), function $CPDocument__hasUndoManager(self, _cmd)
266{ with(self)
267{
268    return _hasUndoManager;
269}
270}), new objj_method(sel_getUid("setHasUndoManager:"), function $CPDocument__setHasUndoManager_(self, _cmd, aFlag)
271{ with(self)
272{
273    if (_hasUndoManager == aFlag)
274        return;
275    _hasUndoManager = aFlag;
276    if (!_hasUndoManager)
277        objj_msgSend(self, "setUndoManager:", nil);
278}
279}), new objj_method(sel_getUid("_undoManagerWillCloseGroup:"), function $CPDocument___undoManagerWillCloseGroup_(self, _cmd, aNotification)
280{ with(self)
281{
282    var undoManager = objj_msgSend(aNotification, "object");
283    if (objj_msgSend(undoManager, "isUndoing") || objj_msgSend(undoManager, "isRedoing"))
284        return;
285    objj_msgSend(self, "updateChangeCount:", CPChangeDone);
286}
287}), new objj_method(sel_getUid("_undoManagerDidUndoChange:"), function $CPDocument___undoManagerDidUndoChange_(self, _cmd, aNotification)
288{ with(self)
289{
290    objj_msgSend(self, "updateChangeCount:", CPChangeUndone);
291}
292}), new objj_method(sel_getUid("_undoManagerDidRedoChange:"), function $CPDocument___undoManagerDidRedoChange_(self, _cmd, aNotification)
293{ with(self)
294{
295    objj_msgSend(self, "updateChangeCount:", CPChangeDone);
296}
297}), new objj_method(sel_getUid("setUndoManager:"), function $CPDocument__setUndoManager_(self, _cmd, anUndoManager)
298{ with(self)
299{
300    var defaultCenter = objj_msgSend(CPNotificationCenter, "defaultCenter");
301    if (_undoManager)
302    {
303        objj_msgSend(defaultCenter, "removeObserver:name:object:", self, CPUndoManagerDidUndoChangeNotification, _undoManager);
304        objj_msgSend(defaultCenter, "removeObserver:name:object:", self, CPUndoManagerDidRedoChangeNotification, _undoManager);
305        objj_msgSend(defaultCenter, "removeObserver:name:object:", self, CPUndoManagerWillCloseUndoGroupNotification, _undoManager);
306    }
307    _undoManager = anUndoManager;
308    if (_undoManager)
309    {
310        objj_msgSend(defaultCenter, "addObserver:selector:name:object:", self, sel_getUid("_undoManagerDidUndoChange:"), CPUndoManagerDidUndoChangeNotification, _undoManager);
311        objj_msgSend(defaultCenter, "addObserver:selector:name:object:", self, sel_getUid("_undoManagerDidRedoChange:"), CPUndoManagerDidRedoChangeNotification, _undoManager);
312        objj_msgSend(defaultCenter, "addObserver:selector:name:object:", self, sel_getUid("_undoManagerWillCloseGroup:"), CPUndoManagerWillCloseUndoGroupNotification, _undoManager);
313    }
314}
315}), new objj_method(sel_getUid("undoManager"), function $CPDocument__undoManager(self, _cmd)
316{ with(self)
317{
318    if (_hasUndoManager && !_undoManager)
319        objj_msgSend(self, "setUndoManager:", objj_msgSend(objj_msgSend(CPUndoManager, "alloc"), "init"));
320    return _undoManager;
321}
322}), new objj_method(sel_getUid("windowWillReturnUndoManager:"), function $CPDocument__windowWillReturnUndoManager_(self, _cmd, aWindow)
323{ with(self)
324{
325    return objj_msgSend(self, "undoManager");
326}
327}), new objj_method(sel_getUid("saveDocument:"), function $CPDocument__saveDocument_(self, _cmd, aSender)
328{ with(self)
329{
330    if (_fileURL)
331    {
332        objj_msgSend(objj_msgSend(CPNotificationCenter, "defaultCenter"), "postNotificationName:object:", CPDocumentWillSaveNotification, self);
333        objj_msgSend(self, "saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:", _fileURL, objj_msgSend(self, "fileType"), CPSaveOperation, self, sel_getUid("document:didSave:contextInfo:"), NULL);
334    }
335    else
336        objj_msgSend(self, "saveDocumentAs:", self);
337}
338}), new objj_method(sel_getUid("saveDocumentAs:"), function $CPDocument__saveDocumentAs_(self, _cmd, aSender)
339{ with(self)
340{
341    _documentName = window.prompt("Document Name:");
342    if (!_documentName)
343        return;
344    objj_msgSend(objj_msgSend(CPNotificationCenter, "defaultCenter"), "postNotificationName:object:", CPDocumentWillSaveNotification, self);
345    objj_msgSend(self, "saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:", objj_msgSend(self, "proposedFileURL"), objj_msgSend(self, "fileType"), CPSaveAsOperation, self, sel_getUid("document:didSave:contextInfo:"), NULL);
346}
347}), new objj_method(sel_getUid("document:didSave:contextInfo:"), function $CPDocument__document_didSave_contextInfo_(self, _cmd, aDocument, didSave, aContextInfo)
348{ with(self)
349{
350    if (didSave)
351        objj_msgSend(objj_msgSend(CPNotificationCenter, "defaultCenter"), "postNotificationName:object:", CPDocumentDidSaveNotification, self);
352    else
353        objj_msgSend(objj_msgSend(CPNotificationCenter, "defaultCenter"), "postNotificationName:object:", CPDocumentDidFailToSaveNotification, self);
354}
355})]);
356}
357var _CPReadSessionMake = function(aType, aDelegate, aDidReadSelector, aContextInfo)
358{
359    return { fileType:aType, delegate:aDelegate, didReadSelector:aDidReadSelector, contextInfo:aContextInfo };
360}
361var _CPSaveSessionMake = function(anAbsoluteURL, aSaveOperation, aChangeCount, aDelegate, aDidSaveSelector, aContextInfo, aConnection)
362{
363    return { absoluteURL:anAbsoluteURL, saveOperation:aSaveOperation, changeCount:aChangeCount, delegate:aDelegate, didSaveSelector:aDidSaveSelector, contextInfo:aContextInfo, connection:aConnection };
364}
365