Guest User

Untitled

a guest
Jan 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.46 KB | None | 0 0
  1. /*
  2. * AppController.j
  3. * desk_pad
  4. *
  5. * Created by You on May 18, 2011.
  6. * Copyright 2011, Your Company All rights reserved.
  7. */
  8.  
  9. @import <Foundation/CPObject.j>
  10. @import <AppKit/CPTableView.j>
  11. @import <AppKit/CPTextField.j>
  12. @import <AppKit/CPPopUpButton.j>
  13. @import <AppKit/CPButtonBar.j>
  14. @import "Client.j"
  15. @import "ClientArrayController.j"
  16. @import "Project.j"
  17. @import "ProjectArrayController.j"
  18. @import "WorkTime.j"
  19. @import "WorkTimeArrayController.j"
  20. @import "Dns.j"
  21. @import "DnsArrayController.j"
  22. @import "DnsRecordsOutlineController.j"
  23. //@import "TabTableView.j"
  24.  
  25. @implementation AppController : CPObject
  26. {
  27. CPWindow theWindow; //this "outlet" is connected automatically by the Cib
  28.  
  29. CPMutableArray clients;
  30. CPMutableDictionary clientsLookup;
  31. IBOutlet ClientArrayController aClientArrayController;
  32. IBOutlet CPTableView aClientsTable;
  33. IBOutlet CPButton saveClientButton;
  34.  
  35. CPMutableArray projects;
  36. IBOutlet ProjectArrayController aProjectArrayController;
  37. IBOutlet CPTableView aProjectsTable;
  38. IBOutlet CPButton saveProjectButton;
  39. IBOutlet CPPopUpButton clientsForProjectsPopUp;
  40. //CPPopUpButton projectPopUp;
  41.  
  42. CPMutableArray workTimes;
  43. IBOutlet WorkTimeArrayController aWorkTimeArrayController;
  44. IBOutlet CPTableView aWorkTimesTable;
  45.  
  46. CPMutableArray dns;
  47. IBOutlet DnsArrayController aDnsArrayController;
  48. IBOutlet CPTableView aDnsTable;
  49. IBOutlet CPButton saveDnsButton;
  50. IBOutlet CPPopUpButton clientsForDnsPopUp;
  51. IBOutlet CPOutlineView aDnsOutline;
  52.  
  53.  
  54. IBOutlet CPTabView myTabView;
  55. IBOutlet CPButton clientsSwitchButton;
  56. IBOutlet CPButton projectsSwitchButton;
  57. IBOutlet CPButton workTimesSwitchButton;
  58. IBOutlet CPButton dnsSwitchButton;
  59.  
  60. }
  61. -(id)init
  62. {
  63.  
  64. var clients = [Client all];
  65. [clients class];
  66.  
  67. var projects = [Project all];
  68. [projects class];
  69.  
  70. var workTimes = [WorkTime all];
  71. [workTimes class];
  72.  
  73. var dns = [Dns all];
  74. [dns class];
  75.  
  76. return self;
  77. }
  78.  
  79. - (void)switchTabFromButton:(id)sender
  80. {
  81. switch([sender title]) {
  82. case @"Clients":
  83. [myTabView selectTabViewItemAtIndex:0];
  84. break;
  85. case @"Projects":
  86. [myTabView selectTabViewItemAtIndex:1];
  87. break;
  88. case @"WorkTimes":
  89. [myTabView selectTabViewItemAtIndex:2];
  90. break;
  91. case @"DNS":
  92. [myTabView selectTabViewItemAtIndex:3];
  93. break;
  94. }
  95. }
  96.  
  97. - (void)saveClient:(id)sender
  98. {
  99. var aClient = [[aClientArrayController selectedObjects] lastObject];
  100. if (![aClient cdbId]) {
  101. [aClient setCdbId:[[aClient class] couchId:aClient]];
  102. }
  103. [aClient save];
  104. if ([clientsLookup valueForKey:[aClient cdbId]] == null) {
  105. [clientsForProjectsPopUp addItemWithTitle:[aClient name]];
  106. [clientsLookup setObject:item forKey:[aClient cdbId]];
  107. }
  108. }
  109.  
  110. - (void)saveProject:(id)sender
  111. {
  112. var aProject = [[aProjectArrayController selectedObjects] lastObject];
  113. if (![aProject cdbId]) {
  114. [aProject setCdbId:[[aProject class] couchId:aProject]];
  115. }
  116. var selectedClientId = [[clients objectAtIndex:[clientsForProjectsPopUp indexOfSelectedItem]] cdbId];
  117. [aProject setClientId:selectedClientId];
  118. [aProject save];
  119.  
  120. if ([[projectPopUp itemTitles] indexOfObject:[aProject name]] < 0) {
  121. [projectPopUp addItemWithTitle:[aProject name]];
  122. }
  123. if ([WTProjectsArray indexOfObject:[aProject cdbId]] < 0) {
  124. [WTProjectsArray addObject:[aProject cdbId]];
  125. }
  126.  
  127. var projectColumn = [aWorkTimesTable tableColumnWithIdentifier:@"projectId"];
  128. var projectPopUp = [[CPPopUpButton alloc] initWithFrame:CGRectMake([projectColumn width], 12)];
  129. [projects enumerateObjectsUsingBlock:function(item) {
  130. [projectPopUp addItemWithTitle:[item name]];
  131. }];
  132. [projectColumn setDataView:projectPopUp];
  133. [aWorkTimesTable reloadData];
  134. }
  135.  
  136. - (void)saveDns:(id)sender
  137. {
  138. var aDns = [[aDnsArrayController selectedObjects] lastObject];
  139. if (![aDns cdbId]) {
  140. [aDns setCdbId:[[aDns class] couchId:aProject]];
  141. }
  142. var selectedClientId = [[clients objectAtIndex:[clientsForDnsPopUp indexOfSelectedItem]] cdbId];
  143. [aDns setClientId:selectedClientId];
  144. [aDns save];
  145. }
  146.  
  147. - (void)applicationDidFinishLaunching:(CPNotification)aNotification
  148. {
  149. // This is called when the application is done loading.
  150. [clientsSwitchButton setAction:@selector(switchTabFromButton:)];
  151. [projectsSwitchButton setAction:@selector(switchTabFromButton:)];
  152. [workTimesSwitchButton setAction:@selector(switchTabFromButton:)];
  153. [dnsSwitchButton setAction:@selector(switchTabFromButton:)];
  154. [saveClientButton setAction:@selector(saveClient:)];
  155. [saveProjectButton setAction:@selector(saveProject:)];
  156. [saveDnsButton setAction:@selector(saveDns:)];
  157.  
  158.  
  159. clientsLookup = [[CPMutableDictionary alloc] init];
  160. [clients enumerateObjectsUsingBlock:function(item) {
  161. [clientsForProjectsPopUp addItemWithTitle:[item name]];
  162. [clientsForDnsPopUp addItemWithTitle:[item name]];
  163. [clientsLookup setObject:item forKey:[item cdbId]];
  164. }];
  165.  
  166. [myTabView selectTabViewItemAtIndex:0];
  167. }
  168.  
  169.  
  170. - (void)observeValueForKeyPath:(CPString)aKeyPath
  171. ofObject:(id)anObject
  172. change:(CPDictionary)aChange
  173. context:(id)aContext
  174. {
  175. var aItem = [[anObject arrangedObjects] objectAtIndex:[anObject selectionIndex]];
  176. var new_value = [aChange valueForKey:@"CPKeyValueChangeNewKey"];
  177. var old_value = [aChange valueForKey:@"CPKeyValueChangeOldKey"];
  178.  
  179. //CPLog.debug(@"KVO %@ %@", aContext, aItem);
  180. //console.log(aItem);
  181.  
  182. /* Project */
  183. if ([aItem class] == [Project class]) {
  184. var clientIndex = [clients indexOfObject:[clientsLookup objectForKey:[aItem clientId]]];
  185. [clientsForProjectsPopUp selectItemAtIndex:clientIndex];
  186. }
  187.  
  188. /* Dns */
  189. if ([aItem class] == [Dns class]) {
  190. var clientIndex = [clients indexOfObject:[clientsLookup objectForKey:[aItem clientId]]];
  191. [clientsForDnsPopUp selectItemAtIndex:clientIndex];
  192. var dnsOutlineController = [[DnsRecordsOutlineController alloc] initWithDns:aItem];
  193. console.log("init dnsOutlineController", dnsOutlineController );
  194. [aDnsOutline setDataSource:dnsOutlineController];
  195. //[aDnsOutline setDelegate:dnsOutlineController];
  196. //[aDnsOutline expandItem:nil expandChildren:YES];
  197. //[scrollView setDocumentView:aDnsOutline];
  198.  
  199. [aDnsOutline reloadData];
  200. console.log("reload item dnsOutlineController");
  201. //[aDnsOutline reloadItem:nil reloadChildren:NO];
  202. }
  203.  
  204. /* WorkTime */
  205. if ([aItem class] == [WorkTime class]) {
  206. if (![new_value isEqual:old_value]) {
  207. if (aContext == "date" || aContext == "time") {
  208. var aNewWorkTime = [[WorkTime alloc] init];
  209. [aNewWorkTime setAttributes:[aWorkTime attributes]];
  210. [aNewWorkTime setIdentifier:null];
  211. [aNewWorkTime setCdbId:[[aNewWorkTime class] couchId:aNewWorkTime]];
  212. [aNewWorkTime save]; // TODO check if it really got saved
  213. [aItem destroy];
  214. } else {
  215. [aItem save];
  216. }
  217. }
  218. }
  219. }
  220.  
  221. /*! Notification responder of TARemoveTableRow
  222. @param aNotification the received notification. This notification will contains as object the row
  223. */
  224. - (void)tableRowRemoved:(CPNotification)aNotification
  225. {
  226. var row = [[aNotification object] lastObject];
  227. if ([row cdbId]) {
  228. [row destroy]; // TODO remove itesm with space propogate error to interface
  229. }
  230. //CPLog.debug([[[aNotification object] lastObject] date]);
  231. }
  232.  
  233. - (void)awakeFromCib
  234. {
  235. //CPLog.debug(self);
  236. [[CPNotificationCenter defaultCenter]
  237. addObserver:self
  238. selector:@selector(tableRowRemoved:)
  239. name:@"TARemoveTableRow" object:nil];
  240.  
  241. [aWorkTimeArrayController addObserver:self forKeyPath:@"arrangedObjects.date" options:nil context:@"date"];
  242. [aWorkTimeArrayController addObserver:self forKeyPath:@"arrangedObjects.time" options:nil context:@"time"];
  243. [aWorkTimeArrayController addObserver:self forKeyPath:@"arrangedObjects.duration" options:nil context:@"duration"];
  244. [aWorkTimeArrayController addObserver:self forKeyPath:@"arrangedObjects.projectId" options:nil context:@"projectId"];
  245. [aWorkTimeArrayController addObserver:self forKeyPath:@"arrangedObjects.billable" options:nil context:@"billable"];
  246. [aWorkTimeArrayController addObserver:self forKeyPath:@"arrangedObjects.category" options:nil context:@"category"];
  247. [aWorkTimeArrayController addObserver:self forKeyPath:@"arrangedObjects.comment" options:nil context:@"comment"];
  248.  
  249. [aProjectArrayController addObserver:self forKeyPath:@"selection.name" options:nil context:@"name"];
  250. [aDnsArrayController addObserver:self forKeyPath:@"selection.domain" options:nil context:@"domain"];
  251.  
  252. //[aWorkTimeArrayController addObserver:self forKeyPath:@"selectedObjects" options:nil context:@"selected"];
  253. [theWindow setFullPlatformWindow:YES];
  254.  
  255. // billable popup
  256. var billableColumn = [aWorkTimesTable tableColumnWithIdentifier:@"billable"];
  257. var billablePopUp = [[CPPopUpButton alloc] initWithFrame:CGRectMake([billableColumn width], 12)];
  258. [WTBillableArray enumerateObjectsUsingBlock:function(item) {
  259. [billablePopUp addItemWithTitle:item];
  260. }];
  261. [billableColumn setDataView:billablePopUp];
  262.  
  263. // category popup
  264. var categoryColumn = [aWorkTimesTable tableColumnWithIdentifier:@"category"];
  265. var categoryPopUp = [[CPPopUpButton alloc] initWithFrame:CGRectMake([categoryColumn width], 12)];
  266. [WTCategoryArray enumerateObjectsUsingBlock:function(item) {
  267. [categoryPopUp addItemWithTitle:item];
  268. }];
  269. [categoryColumn setDataView:categoryPopUp];
  270.  
  271. // projects popup
  272. var projectColumn = [aWorkTimesTable tableColumnWithIdentifier:@"projectId"];
  273. var projectPopUp = [[CPPopUpButton alloc] initWithFrame:CGRectMake([projectColumn width], 12)];
  274. [projects enumerateObjectsUsingBlock:function(item) {
  275. [projectPopUp addItemWithTitle:[item name]];
  276. }];
  277. CPLog.debug("old dataview %@", [projectColumn dataView]);
  278. [projectColumn setDataView:projectPopUp];
  279.  
  280. [aWorkTimesTable reloadData];
  281. }
  282.  
  283.  
  284. /*- (void)keyDown:(CPEvent)event
  285. {
  286. console.log("key pressed");
  287. [super keyDown:event]; // let somebody else handle the event
  288. }*/
  289.  
  290. @end
Add Comment
Please, Sign In to add comment