-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppController.j
849 lines (563 loc) · 21.3 KB
/
AppController.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
/*
* AppController.j
* NewApplication
*
* Created by You on January 29, 2013.
* Copyright 2013, Your Company All rights reserved.
*/
@import <AppKit/CPSplitView.j>
@import <AppKit/CPColorPanel.j>
@import <AppKit/CPToolbar.j>
@import <AppKit/CPPopUpButton.j>
@import <AppKit/CPSegmentedControl.j>
@import <AppKit/CPAlert.j>
@import <AppKit/CPOutlineView.j>
@import "NewFileDialog.j"
@import "FileViewerController.j"
@import "NewProjectDialog.j"
@import "CibEditorView.j"
@import "CibInspectorView.j"
@import "AceEditor.j"
var CompileAndRunToolbarItemIdentifier = @"CompileAndRunToolbarItemIdentifier",
DebugReleasePopupToolbarItemIdentifier = @"DebugReleasePopupToolbarItemIdentifier",
PanelVisibleControlToolbarItemIdentifier = @"PanelVisibleControlToolbarItemIdentifier";
@implementation AppController : CPObject
{
CPOutlineView outlineView;
CPSplitView splitView;
NewFileDialog newFileDialog;
NewProjectDialog newProjectDialog;
FileViewerController fileViewerController;
CibEditorView cibView;
CibInspectorView cibInspectorView;
AceEditor aceEditor;
id _testWindow;
int _activeContextMode;
int lastCenterSplitPos;
CPDictionary _currentProject;
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
contentView = [theWindow contentView];
var toolbar = [[CPToolbar alloc] initWithIdentifier:"ToolBar"];
[toolbar setDelegate:self];
[theWindow setToolbar:toolbar];
var bounds = [contentView bounds];
splitView = [[CPSplitView alloc] initWithFrame:bounds];
[splitView setAutoresizingMask:CPViewWidthSizable|CPViewHeightSizable];
outlineView = [[CPOutlineView alloc] initWithFrame:CGRectMake(0,0, 240, CGRectGetHeight([contentView bounds]))];
[outlineView setHasHeader:NO];
[outlineView setAutoresizingMask:CPViewHeightSizable];
fileViewerController = [[FileViewerController alloc] init];
[outlineView setDataSource:fileViewerController];
[outlineView setDelegate:self];
var column1 = [[CPTableColumn alloc] initWithIdentifier:@"COL1"];
[column1 setWidth:200];
[outlineView addTableColumn:column1];
[outlineView setEmptyText:@""];
[outlineView setBackgroundColor:[CPColor colorWithHexString:@"ebedf1"]];
_activeContextMode = 2;
var centerView = [[CPView alloc] initWithFrame:CGRectMake(240,0, CGRectGetWidth(bounds) - 540, CGRectGetHeight(bounds))];
[centerView setBackgroundColor:[CPColor colorWithWhite:0.87 alpha:1.0]];
[centerView setAutoresizingMask:CPViewWidthSizable];
cibView = [[CibEditorView alloc] initWithFrame:[centerView bounds]];
[cibView setHidden:YES];
[centerView addSubview:cibView];
aceEditor = [[AceEditor alloc] initWithFrame:[centerView bounds]];
[aceEditor setHidden:YES];
[centerView addSubview:aceEditor];
[splitView fixDivider:YES atIndex:0];
[splitView fixDivider:YES atIndex:1];
[splitView setDividerThickness:0];
var rightView = [[CPSplitView alloc] initWithFrame:CGRectMake(CGRectGetWidth(bounds) - 300, 0, 300, CGRectGetHeight(bounds))];
cibInspectorView = [[CibInspectorView alloc] initWithFrame:[rightView bounds]];
[cibInspectorView setHidden:YES];
[rightView addSubview:cibInspectorView];
[splitView addSubview:outlineView];
[splitView addSubview:centerView];
[splitView addSubview:rightView];
[contentView addSubview:splitView];
[self setupMenuBar];
_currentProjectId = Nil;
[theWindow orderFront:self];
[aceEditor setup];
_testWindow = nil;
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(fileMarkedAsUnsaved:)
name:AceEditorMarkFileUnsavedNotification object:aceEditor];
}
-(void) fileMarkedAsUnsaved:(CPNotification)aNotification
{
var sel = [outlineView selectedRow];
var item = [outlineView itemAtRow:sel];
if([item objectForKey:@"isLoaded"])
[outlineView reloadData];
}
- (CPArray)toolbarItemIdentifiers:(CPToolbar)aToolbar
{
return [DebugReleasePopupToolbarItemIdentifier, CPToolbarFlexibleSpaceItemIdentifier, CompileAndRunToolbarItemIdentifier,
CPToolbarFlexibleSpaceItemIdentifier,
CPToolbarFlexibleSpaceItemIdentifier, CPToolbarFlexibleSpaceItemIdentifier, PanelVisibleControlToolbarItemIdentifier];
}
//this delegate method returns the actual toolbar item for the given identifier
- (CPToolbarItem)toolbar:(CPToolbar)aToolbar itemForItemIdentifier:(CPString)anItemIdentifier
{
var toolbarItem = [[CPToolbarItem alloc] initWithItemIdentifier:anItemIdentifier];
if (anItemIdentifier === CompileAndRunToolbarItemIdentifier)
{
[toolbarItem setImage:[CPImage imageNamed:@"build.png"]];
[toolbarItem setAlternateImage:[CPImage imageNamed:@"build_highlighted.png"]];
[toolbarItem setTarget:self];
[toolbarItem setAction:@selector(buildAndRun:)];
[toolbarItem setLabel:@"Run"];
}
else if(anItemIdentifier === DebugReleasePopupToolbarItemIdentifier)
{
var popup = [[CPPopUpButton alloc] initWithFrame:CGRectMake(0,0, 150, 25)];
[popup addItemWithTitle:@"Debug"];
[popup addItemWithTitle:@"Release"];
[popup setBezelStyle:CPTexturedRoundedBezelStyle];
[toolbarItem setView:popup];
[toolbarItem setLabel:@"Configuration"];
}
else if(anItemIdentifier === PanelVisibleControlToolbarItemIdentifier)
{
var seg = [[CPSegmentedControl alloc] initWithFrame:CGRectMake(0,0,115, 25)];
[seg setImagePosition:CPImageOnly];
[seg setSegmentStyle:CPSegmentStyleTexturedRounded];
[seg setTrackingStyle:CPSegmentSwitchTrackingSelectAny];
[seg setTarget:self];
[seg setAction:@selector(onToggleVisiblePanels:)];
[seg setSegmentCount:3];
[seg setImage:[CPImage imageNamed:@"leftPanel.png"] forSegment:0];
[seg setImage:[CPImage imageNamed:@"leftPanel_highlighted.png"]
forSegment:0
inState:CPControlSelectedState];
[seg setImage:[CPImage imageNamed:@"bottomPanel.png"] forSegment:1];
[seg setImage:[CPImage imageNamed:@"bottomPanel_highlighted.png"]
forSegment:1
inState:CPControlSelectedState];
[seg setImage:[CPImage imageNamed:@"rightPanel.png"] forSegment:2];
[seg setImage:[CPImage imageNamed:@"rightPanel_highlighted.png"]
forSegment:2
inState:CPControlSelectedState];
[toolbarItem setView:seg];
[seg setSelected:YES forSegment:0];
[seg setSelected:YES forSegment:1];
[seg setSelected:YES forSegment:2];
[toolbarItem setLabel:@"View"];
}
return toolbarItem;
}
-(void) buildAndRun:(id) sender
{
if(_currentProject)
{
if(_testWindow)
_testWindow.close();
[self saveAllFiles];
var appDir = [_currentProject objectForKey:@"directory"];
BUILD.buildClientExecutable(appDir, appDir, true);
_testWindow = GUI.Window.open('file://' + appDir + "/index.html", {
position: 'center',
width: 800,
height: 600,
title : [_currentProject objectForKey:@"name"]
});
var loadOnce = false;
_testWindow.on("loaded", function(){
if(!loadOnce)
_testWindow.reload();
loadOnce = true;
})
}
}
-(IBAction) onToggleVisiblePanels:(id)sender
{
var index = [sender changedSegment],
bounds = [[splitView superview] bounds],
selected = [sender isSelectedForSegment:index];
if(index == 0)
{
var x = [splitView positionOfDividerAtIndex:0];
if(x > 0 && !selected)
{
[splitView setPosition:0 ofDividerAtIndex:0];
}
else if(x < 240 && selected)
{
[splitView setPosition:240 ofDividerAtIndex:0];
}
}
if(index === 1)
{
var y = [cibView positionOfDividerAtIndex:0];
if(y >= lastCenterSplitPos && selected)
{
[cibView fixDivider:NO atIndex:0];
[cibView setPosition:lastCenterSplitPos ofDividerAtIndex:0];
}
else if(y <= CGRectGetHeight(bounds) && !selected )
{
lastCenterSplitPos = y;
[cibView fixDivider:YES atIndex:0];
[cibView setPosition:CGRectGetHeight(bounds) ofDividerAtIndex:0];
}
}
if(index == 2)
{
var x = [splitView positionOfDividerAtIndex:1];
if(x >= CGRectGetWidth(bounds)-300 && selected)
{
[splitView setPosition:CGRectGetWidth(bounds)-300 ofDividerAtIndex:1];
}
else if(x <= CGRectGetWidth(bounds) && !selected)
{
[splitView setPosition:CGRectGetWidth(bounds) ofDividerAtIndex:1];
}
}
}
-(void) outlineViewSelectionDidChange:(CPNotification)aNotification
{
var sel = [outlineView selectedRow];
var item = [outlineView itemAtRow:sel];
var isDir = [item objectForKey:@"isDirectory"];
if(!isDir)
{
var filename = [item objectForKey:@"name"],
filepath = [item objectForKey:@"path"];
var result = readFile(filepath);
if(result)
{
var stime = result.time;
var fileInfo = [fileViewerController infoForFile:filepath];
var ftime = [fileInfo objectForKey:@"timestamp"];
if(stime > ftime)
{
[fileInfo setObject:result.contents forKey:@"contents"];
[fileInfo setObject:result.time forKey:@"timestamp"];
[fileInfo setObject:YES forKey:@"isLoaded"];
}
if([filename hasSuffix:@".cib"])
{
[self switchContext:1];
}
else
{
[self switchContext:0];
[aceEditor setFile:fileInfo];
[fileInfo setObject:NO forKey:@"unsaved"];
[outlineView reloadData];
}
}
}
}
-(void) saveAllFiles
{
var allFilePaths = [fileViewerController allFiles],
count = [allFilePaths count],
i = 0;
for(; i < count; i++)
{
var filepath = [allFilePaths objectAtIndex:i];
var fileInfo = [fileViewerController infoForFile:filepath];
if([fileInfo objectForKey:@"unsaved"])
{
var result = saveFile(filepath, [fileInfo objectForKey:@"contents"]);
if(result)
{
[fileInfo setObject:result.time forKey:@"timestamp"];
[fileInfo setObject:NO forKey:@"unsaved"];
}
}
}
[outlineView reloadData];
}
-(void) onSaveFile:(id)sender
{
var sel = [outlineView selectedRow];
var item = [outlineView itemAtRow:sel];
var isDir = [item objectForKey:@"isDirectory"];
if(!isDir)
{
var filename = [item objectForKey:@"name"],
filepath = [item objectForKey:@"path"];
var result = saveFile(filepath, [item objectForKey:@"contents"]);
if(result)
{
[item setObject:result.time forKey:@"timestamp"];
[item setObject:NO forKey:@"unsaved"];
[outlineView reloadData];
}
}
}
-(void) onOpenProject:(id)sender
{
setTimeout(function(){
var chooser = $("#fileopen");
chooser.change(function(evt) {
var file = null;
if(evt.target.files.length > 0)
file = evt.target.files[0];
if(file)
{
var reader = new FileReader();
var directory = PATH.dirname($(this).val());
reader.onload = function(e)
{
var result = reader.result;
try
{
var projInfo = JSON.parse(result);
if(projInfo.name)
{
setTimeout(function(){
[self doOpenProject:@{
@"name" : projInfo.name,
@"directory" : directory
}];
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
}, 500);
}
}
catch(err)
{
}
}
reader.readAsText(file);
}
});
chooser.trigger('click');
}, 160);
}
-(void) doOpenProject:(CPDictionary)projectToOpen
{
_currentProject = projectToOpen;
var projdir = [_currentProject objectForKey:@"directory"];
var theFiles = getFilesInDirectory(projdir);
if(theFiles)
{
[fileViewerController setProjectName:[_currentProject objectForKey:@"name"]];
[fileViewerController setData:theFiles];
[outlineView reloadData];
[outlineView expandItem:[outlineView itemAtRow:0]];
window.document.title = "Atlas - " + [_currentProject objectForKey:@"name"];
}
}
-(void) setupMenuBar
{
[CPMenu setMenuBarVisible:YES];
var fileMenu = [CPMenu menuWithTitle:@"File"];
var newMenuItem = [CPMenuItem menuItemWithTitle:@"New..."];
var newMenu = [CPMenu menuWithTitle:@"newItem"];
var newFile = [CPMenuItem menuItemWithTitle:@"New File..."];
[newFile setTarget:self];
[newFile setAction:@selector(newFile:)];
var newProject = [CPMenuItem menuItemWithTitle:@"New Project..."];
[newProject setTarget:self];
[newProject setAction:@selector(newProject:)]
[newMenu addItem:newFile];
[newMenu addItem:newProject];
[newMenuItem setSubmenu:newMenu];
[fileMenu addItem:newMenuItem];
var openFile = [CPMenuItem menuItemWithTitle:@"Open Project..."];
$("body").append($("<input type='file' id='fileopen'></input>"));
[openFile setTarget:self];
[openFile setAction:@selector(onOpenProject:)];
[fileMenu addItem:openFile];
[fileMenu addSeparator];
var saveItem = [CPMenuItem menuItemWithTitle:@"Save"];
[saveItem setTarget:self];
[saveItem setAction:@selector(onSaveFile:)];
[fileMenu addItem:saveItem];
[fileMenu addItem:[CPMenuItem menuItemWithTitle:@"Save As..."]];
[fileMenu addSeparator];
[fileMenu addItem:[CPMenuItem menuItemWithTitle:@"Close File"]];
[[CPApp mainMenu] addItem:fileMenu];
var editMenu = [CPMenu menuWithTitle:@"Edit"];
[editMenu addItem:[CPMenuItem menuItemWithTitle:@"Undo"]];
[editMenu addItem:[CPMenuItem menuItemWithTitle:@"Redo"]];
[editMenu addSeparator];
[editMenu addItem:[CPMenuItem menuItemWithTitle:@"Copy"]];
[editMenu addItem:[CPMenuItem menuItemWithTitle:@"Cut"]];
[editMenu addItem:[CPMenuItem menuItemWithTitle:@"Paste"]];
[[CPApp mainMenu] addItem:editMenu];
}
-(void) newProject:(id)sender
{
if(!newProjectDialog)
{
newProjectDialog = [[NewProjectDialog alloc] init];
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(onNewProjectCreated:)
name:@"NewProjectCreatedNotification" object:newProjectDialog];
}
[newProjectDialog reset];
[newProjectDialog center];
[newProjectDialog makeKeyAndOrderFront:nil];
}
-(void) newFile:(id)sender
{
if(!newFileDialog)
{
newFileDialog = [[NewFileDialog alloc] init];
}
[newFileDialog reset];
[newFileDialog center];
[newFileDialog makeKeyAndOrderFront:nil];
}
-(void)onNewProjectCreated:(CPNotification)aNotification
{
[self doOpenProject:[newProjectDialog newProject]];
}
-(void) switchContext:(int)contextMode
{
if(contextMode !== _activeContextMode)
{
_activeContextMode = contextMode;
if(_activeContextMode == 0) //code editor
{
[cibInspectorView setHidden:YES];
[cibView setHidden:YES];
[aceEditor setHidden:NO];
}
else if(_activeContextMode == 1) //cib editor
{
[cibView setHidden:NO];
[cibInspectorView setHidden:NO];
[aceEditor setHidden:YES];
}
else
{
[cibView setHidden:NO];
[cibInspectorView setHidden:NO];
[aceEditor setHidden:NO];
}
}
}
@end
@implementation OpenProjectWindow : CPWindow
{
DOMElement _inputFileEl;
CPButton _openBtn;
CPButton _cancelBtn;
CPDictionary _projectToOpen;
id _target @accessors(property=target);
SEL _action @accessors(property=action);
}
-(id) init
{
self = [super initWithContentRect:CGRectMake(0,0, 340, 180) styleMask:CPClosableWindowMask];
if( self )
{
_projectToOpen = nil;
[self setTitle:@"Open Project"];
var cv = [self contentView];
var label = [CPTextField labelWithString:@"Select the Project's Info.json file:"];
[label setFrameOrigin:CGPointMake(15,15)];
[cv addSubview:label];
_inputFileEl = $("<input type='file' id='fileopen'></input>");
_inputFileEl.click(function(evt){
evt.stopPropagation();
});
_inputFileEl.on("change", function(evt){
var reader = new FileReader();
if(evt.target.files.length > 0)
{
var file = evt.target.files[0];
reader.onload = function(e)
{
var result = reader.result;
try
{
var projInfo = JSON.parse(result);
if(projInfo.projectDir)
{
_projectToOpen = @{
@"name" : projInfo.name,
@"directory" : projInfo.projectDir
};
}
}
catch(err)
{
}
}
reader.readAsText(file);
}
});
_inputFileEl.css({
position : "absolute",
left : 15,
top : 45
});
cv._DOMElement.append(_inputFileEl);
_cancelBtn = [CPButton buttonWithTitle:@"Cancel"];
[_cancelBtn setFrame:CGRectMake(160, 140, 80,25)];
[_cancelBtn setTarget:self];
[_cancelBtn setAction:@selector(orderOut:)];
[cv addSubview:_cancelBtn];
_openBtn = [CPButton buttonWithTitle:@"Open"];
[_openBtn setFrame:CGRectMake(245, 140,80,25)];
[_openBtn setTarget:self];
[_openBtn setAction:@selector(onOpen:)];
[cv addSubview:_openBtn];
}
return self;
}
-(void) onOpen:(id)sender
{
if(_projectToOpen)
[_target performSelector:_action withObject:_projectToOpen];
[self orderOut:nil];
}
@end
function saveFile(filePath, data)
{
var stats = FS.statSync(filePath);
var time = stats.mtime.getTime();
FS.writeFileSync(filePath, data);
return {time : time};
}
function readFile(filePath)
{
var exists = FS.existsSync(filePath);
if(exists)
{
var stats = FS.statSync(filePath);
var time = stats.mtime.getTime();
var data = FS.readFileSync(filePath, "UTF-8");
return {time : time, contents : data};
}
return null;
}
function getFilesInDirectory(dirPath)
{
var theFiles = [];
var exists = FS.existsSync(dirPath);
if(!exists)
return null;
var files = FS.readdirSync(dirPath),
count = files.length,
i = 0;
for(; i < count; i++)
{
var file = files[i];
if(file.length > 0 && file[0] != ".")
{
var fobj = {"name" : file, "path" : PATH.join(dirPath, file)};
var stats = FS.statSync(PATH.join(dirPath, file));
if(stats.isDirectory())
{
fobj.isDirectory = true;
fobj.files = getFilesInDirectory(PATH.join(dirPath, file));
theFiles.push(fobj);
}
else if(stats.isFile())
{
fobj.isDirectory = false;
theFiles.push(fobj);
}
}
}
return theFiles;
}