/Frameworks/AppKit/browser.platform/CPDocument.j

http://github.com/polymar/polish · Unknown · 325 lines · 325 code · 0 blank · 0 comment · 0 complexity · ba29944fa9a5c4ca782b792ebd05f20e MD5 · raw file

  1. I;21;Foundation/CPString.jI;20;Foundation/CPArray.ji;13;CPResponder.ji;20;CPWindowController.jc;12379;
  2. CPSaveOperation=0;
  3. CPSaveAsOperation=1;
  4. CPSaveToOperation=2;
  5. CPAutosaveOperation=3;
  6. CPChangeDone=0;
  7. CPChangeUndone=1;
  8. CPChangeCleared=2;
  9. CPChangeReadOtherContents=3;
  10. CPChangeAutosaved=4;
  11. CPDocumentWillSaveNotification="CPDocumentWillSaveNotification";
  12. CPDocumentDidSaveNotification="CPDocumentDidSaveNotification";
  13. CPDocumentDidFailToSaveNotification="CPDocumentDidFailToSaveNotification";
  14. var _1=0;
  15. var _2=objj_allocateClassPair(CPResponder,"CPDocument"),_3=_2.isa;
  16. class_addIvars(_2,[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")]);
  17. objj_registerClassPair(_2);
  18. objj_addClassForBundle(_2,objj_getBundleWithPath(OBJJ_CURRENT_BUNDLE.path));
  19. class_addMethods(_2,[new objj_method(sel_getUid("init"),function(_4,_5){
  20. with(_4){
  21. _4=objj_msgSendSuper({receiver:_4,super_class:objj_getClass("CPResponder")},"init");
  22. if(_4){
  23. _windowControllers=[];
  24. _hasUndoManager=YES;
  25. _changeCount=0;
  26. objj_msgSend(_4,"setNextResponder:",CPApp);
  27. }
  28. return _4;
  29. }
  30. }),new objj_method(sel_getUid("initWithType:error:"),function(_6,_7,_8,_9){
  31. with(_6){
  32. _6=objj_msgSend(_6,"init");
  33. if(_6){
  34. objj_msgSend(_6,"setFileType:",_8);
  35. }
  36. return _6;
  37. }
  38. }),new objj_method(sel_getUid("initWithContentsOfURL:ofType:delegate:didReadSelector:contextInfo:"),function(_a,_b,_c,_d,_e,_f,_10){
  39. with(_a){
  40. _a=objj_msgSend(_a,"init");
  41. if(_a){
  42. objj_msgSend(_a,"readFromURL:ofType:delegate:didReadSelector:contextInfo:",_c,_d,_e,_f,_10);
  43. objj_msgSend(_a,"setFileURL:",_c);
  44. objj_msgSend(_a,"setFileType:",_d);
  45. }
  46. return _a;
  47. }
  48. }),new objj_method(sel_getUid("initForURL:withContentsOfURL:ofType:delegate:didReadSelector:contextInfo:"),function(_11,_12,_13,_14,_15,_16,_17,_18){
  49. with(_11){
  50. _11=objj_msgSend(_11,"init");
  51. if(_11){
  52. objj_msgSend(_11,"readFromURL:ofType:delegate:didReadSelector:contextInfo:",_14,_15,_16,_17,_18);
  53. objj_msgSend(_11,"setFileURL:",_13);
  54. objj_msgSend(_11,"setFileType:",_15);
  55. }
  56. return _11;
  57. }
  58. }),new objj_method(sel_getUid("dataOfType:error:"),function(_19,_1a,_1b,_1c){
  59. with(_19){
  60. objj_msgSend(CPException,"raise:reason:",CPUnsupportedMethodException,"dataOfType:error: must be overridden by the document subclass.");
  61. }
  62. }),new objj_method(sel_getUid("readFromData:ofType:error:"),function(_1d,_1e,_1f,_20,_21){
  63. with(_1d){
  64. objj_msgSend(CPException,"raise:reason:",CPUnsupportedMethodException,"readFromData:ofType: must be overridden by the document subclass.");
  65. }
  66. }),new objj_method(sel_getUid("makeWindowControllers"),function(_22,_23){
  67. with(_22){
  68. var _24=objj_msgSend(objj_msgSend(CPWindowController,"alloc"),"initWithWindowCibName:",nil);
  69. objj_msgSend(_22,"addWindowController:",_24);
  70. }
  71. }),new objj_method(sel_getUid("windowControllers"),function(_25,_26){
  72. with(_25){
  73. return _windowControllers;
  74. }
  75. }),new objj_method(sel_getUid("addWindowController:"),function(_27,_28,_29){
  76. with(_27){
  77. objj_msgSend(_windowControllers,"addObject:",_29);
  78. if(objj_msgSend(_29,"document")!=_27){
  79. objj_msgSend(_29,"setNextResponder:",_27);
  80. objj_msgSend(_29,"setDocument:",_27);
  81. }
  82. }
  83. }),new objj_method(sel_getUid("showWindows"),function(_2a,_2b){
  84. with(_2a){
  85. objj_msgSend(_windowControllers,"makeObjectsPerformSelector:withObject:",sel_getUid("showWindow:"),_2a);
  86. }
  87. }),new objj_method(sel_getUid("displayName"),function(_2c,_2d){
  88. with(_2c){
  89. if(_fileURL){
  90. return objj_msgSend(_fileURL,"lastPathComponent");
  91. }
  92. if(!_untitledDocumentIndex){
  93. _untitledDocumentIndex=++_1;
  94. }
  95. if(_untitledDocumentIndex==1){
  96. return "Untitled";
  97. }
  98. return "Untitled "+_untitledDocumentIndex;
  99. }
  100. }),new objj_method(sel_getUid("windowCibName"),function(_2e,_2f){
  101. with(_2e){
  102. return nil;
  103. }
  104. }),new objj_method(sel_getUid("windowControllerDidLoadNib:"),function(_30,_31,_32){
  105. with(_30){
  106. }
  107. }),new objj_method(sel_getUid("windowControllerWillLoadNib:"),function(_33,_34,_35){
  108. with(_33){
  109. }
  110. }),new objj_method(sel_getUid("readFromURL:ofType:delegate:didReadSelector:contextInfo:"),function(_36,_37,_38,_39,_3a,_3b,_3c){
  111. with(_36){
  112. objj_msgSend(_readConnection,"cancel");
  113. _readConnection=objj_msgSend(CPURLConnection,"connectionWithRequest:delegate:",objj_msgSend(CPURLRequest,"requestWithURL:",_38),_36);
  114. _readConnection.session=_3d(_39,_3a,_3b,_3c);
  115. }
  116. }),new objj_method(sel_getUid("fileURL"),function(_3e,_3f){
  117. with(_3e){
  118. return _fileURL;
  119. }
  120. }),new objj_method(sel_getUid("setFileURL:"),function(_40,_41,_42){
  121. with(_40){
  122. if(_fileURL==_42){
  123. return;
  124. }
  125. _fileURL=_42;
  126. objj_msgSend(_windowControllers,"makeObjectsPerformSelector:",sel_getUid("synchronizeWindowTitleWithDocumentName"));
  127. }
  128. }),new objj_method(sel_getUid("saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:"),function(_43,_44,_45,_46,_47,_48,_49,_4a){
  129. with(_43){
  130. var _4b=objj_msgSend(_43,"dataOfType:error:",objj_msgSend(_43,"fileType"),nil),_4c=_changeCount;
  131. _writeRequest=objj_msgSend(CPURLRequest,"requestWithURL:",_45);
  132. objj_msgSend(_writeRequest,"setHTTPMethod:","POST");
  133. objj_msgSend(_writeRequest,"setHTTPBody:",objj_msgSend(_4b,"string"));
  134. objj_msgSend(_writeRequest,"setValue:forHTTPHeaderField:","close","Connection");
  135. if(_47==CPSaveOperation){
  136. objj_msgSend(_writeRequest,"setValue:forHTTPHeaderField:","true","x-cappuccino-overwrite");
  137. }
  138. if(_47!=CPSaveToOperation){
  139. objj_msgSend(_43,"updateChangeCount:",CPChangeCleared);
  140. }
  141. var _4d=objj_msgSend(CPURLConnection,"connectionWithRequest:delegate:",_writeRequest,_43);
  142. _4d.session=_4e(_45,_47,_4c,_48,_49,_4a,_4d);
  143. }
  144. }),new objj_method(sel_getUid("connection:didReceiveResponse:"),function(_4f,_50,_51,_52){
  145. with(_4f){
  146. if(!objj_msgSend(_52,"isKindOfClass:",objj_msgSend(CPHTTPURLResponse,"class"))){
  147. return;
  148. }
  149. var _53=objj_msgSend(_52,"statusCode");
  150. if(_53===200){
  151. return;
  152. }
  153. var _54=_51.session;
  154. if(_51==_readConnection){
  155. objj_msgSend(_51,"cancel");
  156. alert("There was an error retrieving the document.");
  157. objj_msgSend(_54.delegate,_54.didReadSelector,_4f,NO,_54.contextInfo);
  158. }else{
  159. if(_53==409){
  160. objj_msgSend(_51,"cancel");
  161. if(confirm("There already exists a file with that name, would you like to overwrite it?")){
  162. objj_msgSend(_writeRequest,"setValue:forHTTPHeaderField:","true","x-cappuccino-overwrite");
  163. objj_msgSend(_51,"start");
  164. }else{
  165. if(_54.saveOperation!=CPSaveToOperation){
  166. _changeCount+=_54.changeCount;
  167. objj_msgSend(_windowControllers,"makeObjectsPerformSelector:withObject:",sel_getUid("setDocumentEdited:"),objj_msgSend(_4f,"isDocumentEdited"));
  168. }
  169. _writeRequest=nil;
  170. objj_msgSend(_54.delegate,_54.didSaveSelector,_4f,NO,_54.contextInfo);
  171. }
  172. }
  173. }
  174. }
  175. }),new objj_method(sel_getUid("connection:didReceiveData:"),function(_55,_56,_57,_58){
  176. with(_55){
  177. var _59=_57.session;
  178. if(_57==_readConnection){
  179. objj_msgSend(_55,"readFromData:ofType:error:",objj_msgSend(CPData,"dataWithString:",_58),_59.fileType,nil);
  180. objj_msgSend(_59.delegate,_59.didReadSelector,_55,YES,_59.contextInfo);
  181. }else{
  182. if(_59.saveOperation!=CPSaveToOperation){
  183. objj_msgSend(_55,"setFileURL:",_59.absoluteURL);
  184. }
  185. _writeRequest=nil;
  186. objj_msgSend(_59.delegate,_59.didSaveSelector,_55,YES,_59.contextInfo);
  187. }
  188. }
  189. }),new objj_method(sel_getUid("connection:didFailWithError:"),function(_5a,_5b,_5c,_5d){
  190. with(_5a){
  191. var _5e=_5c.session;
  192. if(_readConnection==_5c){
  193. objj_msgSend(_5e.delegate,_5e.didReadSelector,_5a,NO,_5e.contextInfo);
  194. }else{
  195. if(_5e.saveOperation!=CPSaveToOperation){
  196. _changeCount+=_5e.changeCount;
  197. objj_msgSend(_windowControllers,"makeObjectsPerformSelector:withObject:",sel_getUid("setDocumentEdited:"),objj_msgSend(_5a,"isDocumentEdited"));
  198. }
  199. _writeRequest=nil;
  200. alert("There was an error saving the document.");
  201. objj_msgSend(_5e.delegate,_5e.didSaveSelector,_5a,NO,_5e.contextInfo);
  202. }
  203. }
  204. }),new objj_method(sel_getUid("connectionDidFinishLoading:"),function(_5f,_60,_61){
  205. with(_5f){
  206. if(_readConnection==_61){
  207. _readConnection=nil;
  208. }
  209. }
  210. }),new objj_method(sel_getUid("isDocumentEdited"),function(_62,_63){
  211. with(_62){
  212. return _changeCount!=0;
  213. }
  214. }),new objj_method(sel_getUid("updateChangeCount:"),function(_64,_65,_66){
  215. with(_64){
  216. if(_66==CPChangeDone){
  217. ++_changeCount;
  218. }else{
  219. if(_66==CPChangeUndone){
  220. --_changeCount;
  221. }else{
  222. if(_66==CPChangeCleared){
  223. _changeCount=0;
  224. }
  225. }
  226. }
  227. objj_msgSend(_windowControllers,"makeObjectsPerformSelector:withObject:",sel_getUid("setDocumentEdited:"),objj_msgSend(_64,"isDocumentEdited"));
  228. }
  229. }),new objj_method(sel_getUid("setFileType:"),function(_67,_68,_69){
  230. with(_67){
  231. _fileType=_69;
  232. }
  233. }),new objj_method(sel_getUid("fileType"),function(_6a,_6b){
  234. with(_6a){
  235. return _fileType;
  236. }
  237. }),new objj_method(sel_getUid("hasUndoManager"),function(_6c,_6d){
  238. with(_6c){
  239. return _hasUndoManager;
  240. }
  241. }),new objj_method(sel_getUid("setHasUndoManager:"),function(_6e,_6f,_70){
  242. with(_6e){
  243. if(_hasUndoManager==_70){
  244. return;
  245. }
  246. _hasUndoManager=_70;
  247. if(!_hasUndoManager){
  248. objj_msgSend(_6e,"setUndoManager:",nil);
  249. }
  250. }
  251. }),new objj_method(sel_getUid("_undoManagerWillCloseGroup:"),function(_71,_72,_73){
  252. with(_71){
  253. var _74=objj_msgSend(_73,"object");
  254. if(objj_msgSend(_74,"isUndoing")||objj_msgSend(_74,"isRedoing")){
  255. return;
  256. }
  257. objj_msgSend(_71,"updateChangeCount:",CPChangeDone);
  258. }
  259. }),new objj_method(sel_getUid("_undoManagerDidUndoChange:"),function(_75,_76,_77){
  260. with(_75){
  261. objj_msgSend(_75,"updateChangeCount:",CPChangeUndone);
  262. }
  263. }),new objj_method(sel_getUid("_undoManagerDidRedoChange:"),function(_78,_79,_7a){
  264. with(_78){
  265. objj_msgSend(_78,"updateChangeCount:",CPChangeDone);
  266. }
  267. }),new objj_method(sel_getUid("setUndoManager:"),function(_7b,_7c,_7d){
  268. with(_7b){
  269. var _7e=objj_msgSend(CPNotificationCenter,"defaultCenter");
  270. if(_undoManager){
  271. objj_msgSend(_7e,"removeObserver:name:object:",_7b,CPUndoManagerDidUndoChangeNotification,_undoManager);
  272. objj_msgSend(_7e,"removeObserver:name:object:",_7b,CPUndoManagerDidRedoChangeNotification,_undoManager);
  273. objj_msgSend(_7e,"removeObserver:name:object:",_7b,CPUndoManagerWillCloseUndoGroupNotification,_undoManager);
  274. }
  275. _undoManager=_7d;
  276. if(_undoManager){
  277. objj_msgSend(_7e,"addObserver:selector:name:object:",_7b,sel_getUid("_undoManagerDidUndoChange:"),CPUndoManagerDidUndoChangeNotification,_undoManager);
  278. objj_msgSend(_7e,"addObserver:selector:name:object:",_7b,sel_getUid("_undoManagerDidRedoChange:"),CPUndoManagerDidRedoChangeNotification,_undoManager);
  279. objj_msgSend(_7e,"addObserver:selector:name:object:",_7b,sel_getUid("_undoManagerWillCloseGroup:"),CPUndoManagerWillCloseUndoGroupNotification,_undoManager);
  280. }
  281. }
  282. }),new objj_method(sel_getUid("undoManager"),function(_7f,_80){
  283. with(_7f){
  284. if(_hasUndoManager&&!_undoManager){
  285. objj_msgSend(_7f,"setUndoManager:",objj_msgSend(objj_msgSend(CPUndoManager,"alloc"),"init"));
  286. }
  287. return _undoManager;
  288. }
  289. }),new objj_method(sel_getUid("windowWillReturnUndoManager:"),function(_81,_82,_83){
  290. with(_81){
  291. return objj_msgSend(_81,"undoManager");
  292. }
  293. }),new objj_method(sel_getUid("saveDocument:"),function(_84,_85,_86){
  294. with(_84){
  295. if(_fileURL){
  296. objj_msgSend(objj_msgSend(CPNotificationCenter,"defaultCenter"),"postNotificationName:object:",CPDocumentWillSaveNotification,_84);
  297. objj_msgSend(_84,"saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:",_fileURL,objj_msgSend(_84,"fileType"),CPSaveOperation,_84,sel_getUid("document:didSave:contextInfo:"),NULL);
  298. }else{
  299. objj_msgSend(_84,"saveDocumentAs:",_84);
  300. }
  301. }
  302. }),new objj_method(sel_getUid("saveDocumentAs:"),function(_87,_88,_89){
  303. with(_87){
  304. _documentName=window.prompt("Document Name:");
  305. if(!_documentName){
  306. return;
  307. }
  308. objj_msgSend(objj_msgSend(CPNotificationCenter,"defaultCenter"),"postNotificationName:object:",CPDocumentWillSaveNotification,_87);
  309. objj_msgSend(_87,"saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:",objj_msgSend(_87,"proposedFileURL"),objj_msgSend(_87,"fileType"),CPSaveAsOperation,_87,sel_getUid("document:didSave:contextInfo:"),NULL);
  310. }
  311. }),new objj_method(sel_getUid("document:didSave:contextInfo:"),function(_8a,_8b,_8c,_8d,_8e){
  312. with(_8a){
  313. if(_8d){
  314. objj_msgSend(objj_msgSend(CPNotificationCenter,"defaultCenter"),"postNotificationName:object:",CPDocumentDidSaveNotification,_8a);
  315. }else{
  316. objj_msgSend(objj_msgSend(CPNotificationCenter,"defaultCenter"),"postNotificationName:object:",CPDocumentDidFailToSaveNotification,_8a);
  317. }
  318. }
  319. })]);
  320. var _3d=function(_8f,_90,_91,_92){
  321. return {fileType:_8f,delegate:_90,didReadSelector:_91,contextInfo:_92};
  322. };
  323. var _4e=function(_93,_94,_95,_96,_97,_98,_99){
  324. return {absoluteURL:_93,saveOperation:_94,changeCount:_95,delegate:_96,didSaveSelector:_97,contextInfo:_98,connection:_99};
  325. };