-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathNUKit.j
807 lines (624 loc) · 27.3 KB
/
NUKit.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
/*
* Copyright (c) 2016, Alcatel-Lucent Inc
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
@import <Foundation/Foundation.j>
@import "NUAssociators.j"
@import "NUCategories.j"
@import "NUControls.j"
@import "NUDataSources.j"
@import "NUDataViews.j"
@import "NUDataViewsLoaders.j"
@import "NUHierarchyControllers.j"
@import "NUModels.j"
@import "NUModules.j"
@import "NUSkins.j"
@import "NUTransformers.j"
@import "NUUtils.j"
@import "NUWindowControllers.j"
var NUKitDefaultKit ,
NUKitDataViewsRegistryDataViewsRegistry = @{},
NUKitDelegate_didLogin_ = 1 << 1,
NUKitDelegate_willLogout_ = 1 << 2,
NUKitDelegate_shouldLogin_ = 1 << 3,
NUKitDelegate_improperLoginReason_ = 1 << 4,
NUKitDelegate_shouldLogout_ = 1 << 5,
NUKitDelegate_improperLogoutReason_ = 1 << 6;
NUKitUserLoggedInNotification = @"NUKitUserLoggedInNotification";
NUKitUserLoggedOutNotification = @"NUKitUserLoggedOutNotification";
NUKitParameterShowDebugToolTips = NO;
NUKitParameterCat = NO;
NUKitParameterExperimentalMode = NO;
NUKitParameterVeryExperimentalMode = NO;
NUPermissionLevelAdmin = @"admin";
NUPermissionLevelAdminOperator = @"adminoperator"
NUPermissionLevelOperator = @"operator";
NUPermissionLevelPowerUser = @"poweruser";
NUPermissionLevelRoot = @"root";
NUPermissionLevelSecurityAdmin = @"securityadmin"
NUPermissionLevelSystem = @"system";
NUPermissionLevelUser = @"user";
function nukit_set_permission_level_system_value(value) {
NUPermissionLevelSystem = value;
}
function nukit_set_permission_level_root_value(value) {
NUPermissionLevelRoot = value;
}
function nukit_set_permission_level_operator_value(value) {
NUPermissionLevelOperator = value;
}
function nukit_set_permission_level_admin_value(value) {
NUPermissionLevelAdmin = value;
}
function nukit_set_permission_level_power_user_value(value) {
NUPermissionLevelPowerUser = value;
}
function nukit_set_permission_level_user_value_value(value) {
NUPermissionLevelUser = value;
}
function nukit_set_permission_level_adminoperator_value_value(value) {
NUPermissionLevelAdminOperator = value;
}
function nukit_set_permission_level_securityadmin_value_value(value) {
NUPermissionLevelSecurityAdmin = value;
}
@implementation NUKit : CPObject
{
BOOL _usesPoweredBy @accessors(property=usesPoweredBy);
CPBundle _bundle @accessors(getter=bundle);
CPDictionary _moduleColorConfiguration @accessors(property=moduleColorConfiguration);
CPImage _applicationLogo @accessors(property=applicationLogo);
CPImage _companyLogo @accessors(property=companyLogo);
CPString _APIPrefix @accessors(property=APIPrefix);
CPString _applicationName @accessors(property=applicationName);
CPString _autoServerBaseURL @accessors(property=autoServerBaseURL);
CPString _companyName @accessors(property=companyName);
CPString _copyright @accessors(property=copyright);
id _rootAPI @accessors(property=rootAPI);
NULoginWindowController _loginWindowController @accessors(getter=loginWindowController);
NUMainWindowController _mainWindowController @accessors(property=mainWindowController);
NUMessagesWindowController _messagesWindowController @accessors(getter=messagesWindowController);
NUServerFaultWindowController _serverFaultWindowController @accessors(getter=serverFaultWindowController);
BOOL _isAppClosing;
CPArray _sharedModules;
CPPopover _lockedPopover;
CPView _lockedPopoverView;
id _delegate;
unsigned _implementedDelegateMethods;
}
#pragma mark -
#pragma mark Class Methods
+ (NUKit)kit
{
if (!NUKitDefaultKit)
NUKitDefaultKit = [NUKit new];
return NUKitDefaultKit;
}
#pragma mark -
#pragma mark Initialization
- (id)init
{
if (self = [super init])
{
_moduleColorConfiguration = @{
@"main-view-background": NUSkinColorGreyLighter,
@"tableview-view-background": NUSkinColorWhite,
@"masking-view-background": NUSkinColorGreyLighter,
@"title-container-view-background": NUSkinColorGreyLight,
@"title-field-foreground": NUSkinColorBlack,
@"subtitle-container-view-background": NUSkinColorGreyLight,
@"subtitle-field-foreground": NUSkinColorBlack,
@"total-entities-field-foreground": NUSkinColorBlack,
@"total-entities-field-background": NUSkinColorGreyLighter,
@"edition-popover-title-foreground": NUSkinColorBlue,
@"moddule-popover-title-field-foreground": NUSkinColorWhite,
@"module-popover-title-view-background": NUSkinColorBlack,
@"editor-container-view-background": NUSkinColorGreyLight,
@"editor-container-shadow-color": NUSkinColorGrey,
@"main-table-view-container-background": NUSkinColorGreyLight,
@"toolbar-background": NUSkinColorGreyLight,
@"toolbar-foreground": NUSkinColorBlack,
@"footer-background": NUSkinColorGreyLight,
@"footer-foreground": NUSkinColorBlack,
@"chooser-popover-banner-background": NUSkinColorBlack,
@"chooser-popover-banner-foreground": NUSkinColorWhite,
}
_bundle = [CPBundle bundleWithIdentifier:@"net.nuagenetworks.nukit"];
[self installStyleSheetOnDocument:document];
_APIPrefix = @"api/";
_sharedModules = @{};
_loginWindowController = [NULoginWindowController new];
_mainWindowController = [NUMainWindowController new];
_messagesWindowController = [NUMessagesWindowController new];
_serverFaultWindowController = [NUServerFaultWindowController new];
_lockedPopoverView = [CPView new];
[_lockedPopoverView setBackgroundColor:NUSkinColorWhite];
[_lockedPopoverView setAlphaValue:0.5];
[_lockedPopoverView setInAnimation:@"fadeInHalf" duration:0.5];
[_lockedPopoverView setOutAnimation:@"fadeOutHalf" duration:0.5];
[[CPUserDefaults standardUserDefaults] registerDefaults:@{@"NUAPIURL": [[CPBundle mainBundle] objectForInfoDictionaryKey:@"NUAPIURL"]}];
}
return self;
}
- (void)loadFrameworkDataViews
{
[[[NUInternalDataViewsLoader alloc] initWithCibName:@"DataViews" bundle:[self bundle]] load];
}
- (void)installStyleSheetOnDocument:(id)aDocument
{
var resourceURL = [[[self bundle] resourceURL] absoluteString],
head = aDocument.getElementsByTagName('head')[0],
animatecss = aDocument.createElement('link'),
appcss = aDocument.createElement('link'),
spinnercss = aDocument.createElement('link');
animatecss.id = @"animate.css";
animatecss.rel = @"stylesheet";
animatecss.type = @"text/css";
animatecss.href = resourceURL + @"/animate.css";
animatecss.media = @"all";
head.appendChild(animatecss);
appcss.id = @"app.css";
appcss.rel = @"stylesheet";
appcss.type = @"text/css";
appcss.href = resourceURL + @"/app.css";
appcss.media = @"all";
head.appendChild(appcss);
spinnercss.id = @"spinner.css";
spinnercss.rel = @"stylesheet";
spinnercss.type = @"text/css";
spinnercss.href = resourceURL + @"/spinner.css";
spinnercss.media = @"all";
head.appendChild(spinnercss);
}
- (void)registerCoreModule:(NUModule)aModule
{
[_mainWindowController registerCoreModule:aModule];
}
- (void)registerPrincipalModule:(NUModule)aModule withButtonImage:(CPImage)anImage altImage:(CPImage)anAltImage toolTip:(CPString)aToolTip identifier:(CPString)anIdentifier availableToRoles:(CPArray)someRoles
{
var button = [[CPButton alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
[button setBordered:NO];
[button setButtonType:CPMomentaryChangeButton];
[button setValue:anImage forThemeAttribute:@"image" inState:CPThemeStateNormal];
[button setValue:anAltImage forThemeAttribute:@"image" inState:CPThemeStateHighlighted];
[button setToolTip:aToolTip];
_cucappID(button, anIdentifier);
[self registerPrincipalModule:aModule accessButton:button availableToRoles:someRoles];
}
- (void)registerPrincipalModule:(NUModule)aModule accessButton:(CPButton)aButton availableToRoles:(CPArray)someRoles
{
[_mainWindowController registerPrincipalModule:aModule accessButton:aButton availableToRoles:someRoles];
}
- (void)configureContextDefaultFirstResponderTags:(CPArray)anArray
{
[NUModuleContext setDefaultFirstResponderTags:anArray];
}
#pragma mark -
#pragma mark Utilities
- (void)_sendLogOutNotification
{
[[CPNotificationCenter defaultCenter] postNotificationName:NUKitUserLoggedOutNotification object:self];
}
- (void)_sendLogInNotification
{
[[CPNotificationCenter defaultCenter] postNotificationName:NUKitUserLoggedInNotification object:self];
}
#pragma mark -
#pragma mark Notification Handlers
- (void)startListenNotification
{
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(_didReceiveServerUnreachableNotification:) name:NURESTPushCenterServerUnreachable object:nil];
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(_didReceiveXHRErrorNotification:) name:NURESTConnectionFailureNotification object:nil];
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(_didReceiveUserIdleNotification:) name:NURESTConnectionIdleTimeoutNotification object:nil];
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(_didReceiveRESTErrorNotification:) name:NURESTErrorNotification object:nil];
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(_didReceiveRESTConfirmationNotification:) name:NURESTConfirmationNotification object:nil];
}
- (void)_didReceiveServerUnreachableNotification:(CPNotification)aNotification
{
if (_isAppClosing)
return;
[[self mainWindowController] close];
[[self serverFaultWindowController] showWindow:nil];
[self _sendLogOutNotification];
}
- (void)_didReceiveUserIdleNotification:(CPNotification)aNotification
{
var lastUserInputTimeStamp = Math.round([[CPApp currentEvent] timestamp]),
lastRegisteredTimeStamp = Math.round([aNotification userInfo]);
if (lastUserInputTimeStamp - lastRegisteredTimeStamp <= 0)
{
[self performLogout];
[[self loginWindowController] setMessage:@"User idle for too long"];
}
else
[NURESTConnection resetIdleTimeout];
}
- (void)_didReceiveXHRErrorNotification:(CPNotification)aNotification
{
[self performLogout];
[[self loginWindowController] setMessage:[[aNotification userInfo] hasTimeouted] ? @"Request timeout" : @"Unknown connection error"];
[[self loginWindowController] setButtonLoginEnabled:YES];
}
- (void)_didReceiveRESTErrorNotification:(CPNotification)aNotification
{
[[self messagesWindowController] pushMessage:[aNotification object]];
}
- (void)_didReceiveRESTConfirmationNotification:(CPNotification)aNotification
{
[[self messagesWindowController] pushMessage:[aNotification object]];
}
#pragma mark -
#pragma mark Arguments Parsing
- (id)valueForApplicationArgument:(CPString)aString
{
if (window.location.search.indexOf(aString) == -1)
return nil;
return _get_query_parameter_with_name(aString) || YES;
}
- (CPString)stringValueForApplicationArgument:(CPString)aString
{
var value = [self valueForApplicationArgument:aString];
return [value isKindOfClass:CPString] && [value length] ? value : nil;
}
- (void)parseStandardApplicationArguments
{
if ([self valueForApplicationArgument:@"debugtooltips"])
NUKitParameterShowDebugToolTips = YES;
if ([self valueForApplicationArgument:@"catmode"])
NUKitParameterCat = YES;
if ([self valueForApplicationArgument:@"legacyscrollers"])
[CPScrollView setGlobalScrollerStyle:CPScrollerStyleLegacy];
if ([self valueForApplicationArgument:@"debug"])
{
CPLogUnregister(CPLogConsole);
CPLogRegister(CPLogConsole, "trace");
}
if ([self valueForApplicationArgument:@"realmanmode"])
[NURESTConnection setAutoConfirm:YES];
if ([self valueForApplicationArgument:@"overlymanlymanmode"])
{
[NURESTConnection setAutoConfirm:YES];
[NUModule setAutoConfirm:YES]
}
if ([self valueForApplicationArgument:@"notimeout"])
[NURESTConnection setTimeoutValue:nil];
if ([self valueForApplicationArgument:@"experimental"])
NUKitParameterExperimentalMode = YES;
if ([self valueForApplicationArgument:@"veryexperimental"])
NUKitParameterVeryExperimentalMode = YES;
}
#pragma mark -
#pragma mark Shared Modules Management
- (void)registerSharedModule:(NUModule)aSharedModule withIdentifier:(CPString)anIdentifier
{
if (![_sharedModules containsKey:anIdentifier])
[_sharedModules setObject:aSharedModule forKey:anIdentifier];
}
- (NUModule)sharedModuleWithIdentifier:(CPString)anIdentifier
{
return [_sharedModules objectForKey:anIdentifier];
}
#pragma mark -
#pragma mark Data View Management
- (void)registerDataView:(NUAsbtractDataView)aDataView withIdentifier:(CPString)aName
{
if (![NUKitDataViewsRegistryDataViewsRegistry containsKey:aName])
[NUKitDataViewsRegistryDataViewsRegistry setObject:aDataView forKey:aName];
}
- (id)registeredDataViewWithIdentifier:(CPString)aName
{
return [NUKitDataViewsRegistryDataViewsRegistry objectForKey:aName];
}
#pragma mark -
#pragma mark External Platform Windows Management
- (void)closeExternalWindows
{
[CPPlatform closeAllPlatformWindows];
}
#pragma mark -
#pragma mark Delegates
- (void)setDelegate:(id)aDelegate
{
if (aDelegate == _delegate)
return;
_delegate = aDelegate;
_implementedDelegateMethods = 0;
if ([_delegate respondsToSelector:@selector(applicationDidLogin:)])
_implementedDelegateMethods |= NUKitDelegate_didLogin_;
if ([_delegate respondsToSelector:@selector(applicationWillLogout:)])
_implementedDelegateMethods |= NUKitDelegate_willLogout_;
if ([_delegate respondsToSelector:@selector(applicationShouldLogin:)])
_implementedDelegateMethods |= NUKitDelegate_shouldLogin_;
if ([_delegate respondsToSelector:@selector(applicationShouldLogin:)])
_implementedDelegateMethods |= NUKitDelegate_shouldLogin_;
if ([_delegate respondsToSelector:@selector(applicationImproperLoginReason:)])
_implementedDelegateMethods |= NUKitDelegate_improperLoginReason_;
if ([_delegate respondsToSelector:@selector(applicationShouldLogout:)])
_implementedDelegateMethods |= NUKitDelegate_shouldLogout_;
if ([_delegate respondsToSelector:@selector(applicationImproperLogoutReason:)])
_implementedDelegateMethods |= NUKitDelegate_improperLogoutReason_;
}
- (void)_sendDelegateDidLogin
{
if (_implementedDelegateMethods & NUKitDelegate_didLogin_)
[_delegate applicationDidLogin:self];
}
- (void)_sendDelegateWillLogout
{
if (_implementedDelegateMethods & NUKitDelegate_willLogout_)
[_delegate applicationWillLogout:self];
}
- (BOOL)_sendDelegateShouldLogin
{
if (_implementedDelegateMethods & NUKitDelegate_shouldLogin_)
return [_delegate applicationShouldLogin:self];
return YES;
}
- (CPString)_sendDelegateImproperLoginReason
{
if (_implementedDelegateMethods & NUKitDelegate_improperLoginReason_)
return [_delegate applicationImproperLoginReason:self];
}
- (BOOL)_sendDelegateShouldLogout
{
if (_implementedDelegateMethods & NUKitDelegate_shouldLogout_)
return [_delegate applicationShouldLogout:self];
return YES;
}
- (CPString)_sendDelegateImproperLogoutReason
{
if (_implementedDelegateMethods & NUKitDelegate_improperLogoutReason_)
return [_delegate applicationImproperLogoutReason:self];
}
#pragma mark -
#pragma mark REST URL Management
- (CPURL)RESTBaseURL
{
var baseURLString = [[CPUserDefaults standardUserDefaults] objectForKey:@"NUAPIURL"],
baseURL = [CPURL URLWithString:baseURLString[baseURLString.length - 1] != @"/" ? baseURLString + @"/" : baseURLString],
APIVersion = @"v" + [[CPBundle mainBundle] objectForInfoDictionaryKey:@"NUAPIVersion"].replace(".", "_"),
finalRESTURL;
var customAPIVersion = [self valueForApplicationArgument:@"apiversion"];
if (customAPIVersion)
APIVersion = customAPIVersion;
if (baseURL == @"auto/")
baseURL = [CPURL URLWithString:[self _discoverServerURL] + @"/"];
finalRESTURL = [CPURL URLWithString:(_APIPrefix + APIVersion + "/") relativeToURL:baseURL];
CPLog.info("REST URL base is set to %@", finalRESTURL);
return finalRESTURL;
}
- (CPString)_discoverServerURL
{
if (!_autoServerBaseURL || _autoServerBaseURL == "")
{
var origin = window.location.origin;
if (!origin || typeof(origin) == "undefined")
{
var protocol = window.location.protocol,
hostname = window.location.hostname,
port = window.location.port;
origin = protocol + "//" + hostname;
if (port && port != @"")
origin += ":" + port;
}
return origin;
}
else
return _autoServerBaseURL;
}
#pragma mark -
#pragma mark Application Name and Icon Management
- (void)bindApplicationNameToObject:(id)anObject withKeyPath:(CPString)aKeyPath
{
[[NUKitToolBar defaultToolBar] bindApplicationNameToObject:anObject withKeyPath:aKeyPath];
}
- (void)bindApplicationIconToObject:(id)anObject withKeyPath:(CPString)aKeyPath
{
[[NUKitToolBar defaultToolBar] bindApplicationIconToObject:anObject withKeyPath:aKeyPath];
}
#pragma mark -
#pragma mark Session Management
- (void)manageLoginWindow
{
var userInfo = [self stringValueForApplicationArgument:@"userinfo"],
user = [self stringValueForApplicationArgument:@"user"],
org = [self stringValueForApplicationArgument:@"org"],
pass = [self stringValueForApplicationArgument:@"pass"],
api = [self stringValueForApplicationArgument:@"api"] || @"auto";
if (userInfo && api && org)
[self performAutoLoginWithUserInfo:userInfo organization:org url:api];
else if (user && org && pass && api)
[self performLoginWithUserName:user organization:org password:pass url:api];
else
[[self loginWindowController] showWindow:self];
}
- (void)_continueLogin
{
[[NURESTLoginController defaultController] setAPIKey:[[self rootAPI] APIKey]];
[[NURESTPushCenter defaultCenter] start];
[[self serverFaultWindowController] close];
[[self loginWindowController] close];
[[self loginWindowController] emptyPasswordField];
[[self mainWindowController] showWindow:self];
[[[self mainWindowController] window] platformWindow]._DOMWindow.addEventListener("beforeunload", function(){
_isAppClosing = YES;
}, NO);
[self _sendLogInNotification];
[self _sendDelegateDidLogin];
[[NUKitToolBar defaultToolBar] setNeedsLayout];
}
- (void)performLoginWithUserName:(CPString)aUserName organization:(CPString)anOrganization password:(CPString)aPassword url:(CPString)anURL
{
anURL = anURL.replace(/ /g, "");
if (anURL != @"auto" && anURL != @"" && anURL)
anURL = [CPURL URLWithString:anURL];
[[CPUserDefaults standardUserDefaults] setObject:anURL forKey:@"NUAPIURL"];
[[CPUserDefaults standardUserDefaults] setObject:aUserName forKey:@"RESTServerUserName"];
[[CPUserDefaults standardUserDefaults] setObject:anOrganization forKey:@"RESTServerUserCompany"];
var finalURL = [self RESTBaseURL];
[[NURESTLoginController defaultController] setUser:aUserName];
[[NURESTLoginController defaultController] setCompany:anOrganization];
[[NURESTLoginController defaultController] setPassword:aPassword];
[[NURESTLoginController defaultController] setURL:finalURL];
[[NURESTLoginController defaultController] setAPIKey:nil];
[[self rootAPI] setID:nil];
[[self rootAPI] fetchAndCallSelector:@selector(_didFetchUser:connection:) ofObject:self];
}
- (void)performAutoLoginWithUserInfo:(CPString)someUserInfo organization:(CPString)anOrganization url:(CPString)anURL
{
[[CPUserDefaults standardUserDefaults] setObject:anURL forKey:@"NUAPIURL"];
var URL = [self RESTBaseURL],
JSONinfo = JSON.parse(atob(someUserInfo));
[[self rootAPI] objectFromJSON:JSONinfo];
[[NURESTLoginController defaultController] setUser:[[self rootAPI] userName]];
[[NURESTLoginController defaultController] setCompany:anOrganization];
[[NURESTLoginController defaultController] setPassword:nil];
[[NURESTLoginController defaultController] setURL:URL];
[[NURESTLoginController defaultController] setAPIKey:[[self rootAPI] APIKey]];
if ([self _sendDelegateShouldLogin])
[self _continueLogin];
}
- (void)_didFetchUser:(id)anUser connection:(NURESTConnection)aConnection
{
[[NURESTLoginController defaultController] setPassword:nil];
switch ([aConnection responseCode])
{
case NURESTConnectionResponseCodeSuccess:
if ([self _sendDelegateShouldLogin])
[self _continueLogin];
else
{
[[self loginWindowController] shakeWindow];
[[self loginWindowController] setMessage:[self _sendDelegateImproperLoginReason]];
}
break;
case NURESTConnectionResponseCodeNotFound:
[[self loginWindowController] shakeWindow];
[[self loginWindowController] setMessage:@"Resource not found"];
break;
case NURESTConnectionResponseCodeUnauthorized:
[[self loginWindowController] shakeWindow];
[[self loginWindowController] setMessage:@"Invalid credentials"];
break;
case NURESTConnectionResponseCodeConflict:
[[self loginWindowController] shakeWindow];
var responseObject = [[aConnection responseData] JSONObject] || @"Unknown connection error";
[[self loginWindowController] setMessage:responseObject.errors[0].descriptions[0].title];
break;
default:
[[self loginWindowController] shakeWindow];
[[self loginWindowController] setMessage:@"Unable to connect to the endpoint"];
}
[[self loginWindowController] setButtonLoginEnabled:YES];
}
- (void)performLogout
{
if ([self _sendDelegateShouldLogout])
[self performLogoutWithAlert:nil];
else
alert([self _sendDelegateImproperLogoutReason])
}
- (void)performLogoutWithAlert:(CPAlert)anAlert
{
[self _sendDelegateWillLogout];
[self _sendLogOutNotification];
[[self serverFaultWindowController] close];
[[self mainWindowController] close];
[[self loginWindowController] showWindow:self];
[self unlockCurrentPopover];
[self closeExternalWindows];
[[NURESTLoginController defaultController] reset];
[[NURESTPushCenter defaultCenter] stop];
if (anAlert)
[anAlert runModal];
}
#pragma mark -
#pragma mark Inspector Management
- (void)openInspectorForSelectedObject
{
var responder = [[CPApp keyWindow] firstResponder],
inspectedObject;
switch ([responder className])
{
case @"CPTableView":
inspectedObject = [[responder dataSource] objectAtIndex:[responder selectedRow]];
break;
case @"NUGroupItemTableView":
inspectedObject = [[[[responder dataSource] content] objectAtIndex:[responder selectedRow]] objectValue];
break;
case @"CPOutlineView":
inspectedObject = [responder itemAtRow:[responder selectedRow]];
break;
case @"NUTreeView":
case @"NUGraphView":
case @"OSMMapView":
inspectedObject = [[responder selectedItems] firstObject];
break;
}
[self openInspectorForObject:inspectedObject];
}
- (void)openInspectorForObject:(id)anObject
{
if (![anObject isKindOfClass:NURESTObject])
return;
var inspectedObjectID = [anObject ID];
if ([NUInspectorWindowController isInspectorOpenedForObjectWithID:inspectedObjectID])
{
[[NUInspectorWindowController inspectorForObjectWithID:inspectedObjectID] makeKeyInspector];
return;
}
var inspector = [NUInspectorWindowController new];
[inspector window];
[inspector setInspectedObject:anObject];
[inspector showWindow:self];
}
- (void)registerAdditionalInspectorModuleClass:(Class)aClass cibName:(CPString)aCibName displayDecisionFunction:(Function)aFunction
{
[NUInspectorWindowController registerAdditionalModuleClass:aClass cibName:aCibName displayDecisionFunction:aFunction];
}
#pragma mark -
#pragma mark Popover Locking
- (void)lockCurrentPopover
{
var latestWindow = [[CPApp windows] lastObject];
if ([latestWindow className] != _CPPopoverWindow)
return;
_lockedPopover = latestWindow._delegate;
if (_lockedPopover && [_lockedPopover behavior] == CPPopoverBehaviorTransient)
{
var contentView = [[_lockedPopover contentViewController] view];
[_lockedPopoverView setFrame:[contentView bounds]];
[contentView addSubview:_lockedPopoverView];
[_lockedPopover setBehavior:CPPopoverBehaviorApplicationDefined];
}
}
- (void)unlockCurrentPopover
{
if (!_lockedPopover)
return;
[_lockedPopoverView removeFromSuperview];
[_lockedPopover setBehavior:CPPopoverBehaviorTransient];
}
@end