-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit-sds-windowutil.js
1123 lines (979 loc) · 46.9 KB
/
bit-sds-windowutil.js
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* BITespresso namespace.
* @namespace BIT
*/
Ext.namespace("BIT");
/**
* Synology DiskStation namespace.
* @namespace BIT.SDS
*/
Ext.namespace("BIT.SDS");
Ext.namespace("BIT.SDS.Promise");
Ext.define("BIT.SDS.Promise",
/**
* @lends BIT.SDS.Promise.prototype
*/
{
/**
* @lends BIT.SDS.Promise
*/
statics: {
STATE: {
PENDING: "pending",
FULFILLED: "fulfilled",
REJECTED: "rejected"
},
/**
* Returns a promise that resolves when all of the promises passed have resolved or when the
* array contains no promises. It rejects with the reason of the first promise that rejects.
*
* @param {BIT.SDS.Promise[]} promises The promises.
* @return {BIT.SDS.Promise} A new promise.
*/
all: function(promises) {
return new BIT.SDS.Promise(function(resolve, reject) {
var results = [];
var resolved = 0;
Ext.each(promises, function(promise, i) {
if (!promise || !Ext.isFunction(promise.then)) {
promise = BIT.SDS.Promise.resolve(promise);
}
promise
.then(function(value) {
results[i] = value;
resolved++;
if (resolved === promises.length) resolve(results);
})
.catch(function(reason) {
reject(reason);
});
}, this);
if (promises.length === 0) resolve(results);
});
},
/**
* Returns a new promise that resolves or rejects as soon as one of the promises passed
* resolves or rejects, with the value or reason from that promise.
*
* @param {BIT.SDS.Promise[]} promises The promises.
* @return {BIT.SDS.Promise} A new promise.
*/
race: function(promises) {
return new BIT.SDS.Promise(function(resolve, reject) {
Ext.each(promises, function(promise) {
promise.then(resolve, reject);
}, this);
});
},
/**
* Returns a promise that is rejected with the passed reason.
*
* @param {*} reason The reason.
* @return {BIT.SDS.Promise} A new rejecting promise.
*/
reject: function(reason) {
return new BIT.SDS.Promise(function(resolve, reject) {
reject(reason);
});
},
/**
* Returns a promise that is resolved with the passed value.
*
* @param {*} value The value.
* @return {BIT.SDS.Promise} A new resolving promise.
*/
resolve: function(value) {
return new BIT.SDS.Promise(function(resolve, reject) {
resolve(value);
});
},
/**
* Tries to resolve a promise multiple times if it is rejected. The passed function will be
* called repeatedly until the promise returned by the function is resolved or the maximum
* number of attempts is reached.
*
* Returns a new promise that is fulfilled with the fulfillment value of the promise
* returned by the function, or is rejected with the same reason as the promise returned by
* the function in the last attempt.
*
* Subsequent calls of the function will be deferred by a delay specified in milliseconds.
*
* @param {Function} fn The function returning a promise.
* @param {number} tries The maximum number of attempts.
* @param {number} delay The delay.
* @return {BIT.SDS.Promise} A new promise.
*
* @example
* function trySomething() {
* function resolver(resolve, reject) {
* ... // Try to resolve promise
* }
*
* return new BIT.SDS.Promise(resolver);
* }
*
* BIT.SDS.Promise.retry(trySomething, 5, 5000)
* .then(...)
* .catch(...);
*/
retry: function(fn, tries, delay) {
return new BIT.SDS.Promise(function(resolve, reject) {
var lastRejectReason;
function retry() {
if (tries > 0) {
tries--;
fn()
.then(resolve)
.catch(function(reason) {
lastRejectReason = reason;
setTimeout(retry, delay);
});
} else {
reject(lastRejectReason);
}
}
retry();
});
}
},
clients: undefined,
result: undefined,
state: undefined,
/**
* Creates a new {@link BIT.SDS.Promise} instance.
*
* @method BIT.SDS.Promise
* @constructs
*
* @param {Function} resolver The resolver function.
*/
constructor: function(resolver) {
this.state = BIT.SDS.Promise.STATE.PENDING;
this.clients = [];
this.result = undefined;
function resolve(value) {
this.resolve(value);
}
function reject(reason) {
this.reject(reason);
}
if (Ext.isFunction(resolver)) {
try {
resolver(resolve.createDelegate(this), reject.createDelegate(this));
} catch (error) {
this.reject(error);
}
} else if (arguments.length > 0) {
throw Error("Promise resolver " + resolver + " is not a function");
}
},
/**
* Adds a callback to the promise to be called when this promise is rejected. Returns a new
* promise that will be rejected when the callback is complete.
*
* @param {Function} onRejected The rejected callback.
* @return {BIT.SDS.Promise} A new promise.
*/
catch: function(onRejected) {
return this.then(null, onRejected);
},
/**
* Adds a callback to the promise to be called when this promise is fulfilled or rejected.
* Returns a new promise that will be fulfilled or rejected when the callback is complete.
*
* @param {Function} onFinally The finally callback.
* @return {BIT.SDS.Promise} A new promise.
*/
finally: function(onFinally) {
if (!Ext.isFunction(onFinally)) return this.then(onFinally, onFinally);
function onFulfilled(value) {
return new BIT.SDS.Promise(function(resolve) {
resolve(onFinally());
}).then(function() {
return value;
});
}
function onRejected(reason) {
return new BIT.SDS.Promise(function(resolve) {
resolve(onFinally());
}).then(function() {
throw reason;
});
}
return this.then(onFulfilled.createDelegate(this), onRejected.createDelegate(this));
},
/**
* Rejects the promise with the passed reason.
*
* @param {*} reason The reason.
*/
reject: function(reason) {
if (this.state !== BIT.SDS.Promise.STATE.PENDING) return;
this.state = BIT.SDS.Promise.STATE.REJECTED;
this.result = reason;
function rejectAllClients() {
Ext.each(this.clients, function(client) {
client.rejectClient(reason);
}, this);
}
setTimeout(rejectAllClients.createDelegate(this));
},
/**
* Resolves the promise with the passed value.
*
* @param {*} value The value.
*/
resolve: function(value) {
var alreadyCalled = false;
var then;
function onFulfilled(value) {
if (!alreadyCalled) {
alreadyCalled = true;
this.resolve(value);
}
}
function onRejected(reason) {
if (!alreadyCalled) {
alreadyCalled = true;
this.reject(reason);
}
}
if (this.state !== BIT.SDS.Promise.STATE.PENDING) return;
if (value === this) return this.reject(Error("A promise cannot be resolved by itself"));
if (value && (Ext.isFunction(value) || Ext.isObject(value))) {
try {
then = value.then;
if (Ext.isFunction(then)) {
then.call(value, onFulfilled.createDelegate(this), onRejected.createDelegate(this));
return;
}
} catch (error) {
if (!alreadyCalled) this.reject(error);
return;
}
}
this.state = BIT.SDS.Promise.STATE.FULFILLED;
this.result = value;
function fulfillAllClients() {
Ext.each(this.clients, function(client) {
client.fulfillClient(value);
}, this);
}
setTimeout(fulfillAllClients.createDelegate(this));
},
/**
* Adds callbacks to the promise to be called when this promise is fulfilled or rejected.
* Returns a new promise that will be fulfilled or rejected when the callback is complete.
*
* @param {Function} onFulfilled The fulfilled callback.
* @param {Function} onRejected The rejected callback.
* @return {BIT.SDS.Promise} A new promise.
*/
then: function(onFulfilled, onRejected) {
var promise = new BIT.SDS.Promise();
var client = {
onFulfilled: onFulfilled,
onRejected: onRejected,
promise: promise,
fulfillClient: function(result) {
if (Ext.isFunction(this.onFulfilled)) {
try {
var value = this.onFulfilled.call(undefined, result);
this.promise.resolve(value);
} catch (error) {
this.promise.reject(error);
}
} else {
this.promise.resolve(result);
}
},
rejectClient: function(result) {
if (Ext.isFunction(this.onRejected)) {
try {
var value = this.onRejected.call(undefined, result);
this.promise.resolve(value);
} catch (error) {
this.promise.reject(error);
}
} else {
this.promise.reject(result);
}
}
};
switch (this.state) {
case BIT.SDS.Promise.STATE.PENDING:
this.clients.push(client);
break;
case BIT.SDS.Promise.STATE.FULFILLED:
setTimeout(client.fulfillClient.createDelegate(client, [this.result]));
break;
case BIT.SDS.Promise.STATE.REJECTED:
setTimeout(client.rejectClient.createDelegate(client, [this.result]));
break;
}
return promise;
}
});
Ext.namespace("BIT.SDS.WindowUtil");
/**
* @class BIT.SDS.WindowUtil
*
* @hideconstructor
*/
Ext.define("BIT.SDS.WindowUtil",
{
/**
* @lends BIT.SDS.WindowUtil
*/
statics: {
APP_DATA_ARRAY: [
["SYNO.SDS.AdminCenter.Application", ["5.2", "6.0", "6.1", "6.2"], [ 994, 570]],
["SYNO.SDS.App.FileStation3.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 920, 560]],
["SYNO.SDS.EzInternet.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 700, 450]],
["SYNO.SDS.HelpBrowser.Application", ["5.2", "6.0", "6.1", "6.2"], [ 995, 500]],
["SYNO.SDS.PkgManApp.Instance", ["5.2", "6.0", "6.1", "6.2"], [1060, 580]],
["SYNO.SDS.ResourceMonitor.Instance", ["5.2", "6.0", "6.1", "6.2"], [1024, 580]],
["SYNO.SDS.StorageManager.Instance", ["5.2", "6.0", "6.1", "6.2"], [1000, 600]],
["SYNO.SDS.HA.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 990, 580]],
["SYNO.SDS.LogCenter.Instance", ["5.2", "6.0", "6.1", "6.2"], [1005, 580]],
["SYNO.SDS.SecurityScan.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 990, 570]],
["SYNO.SDS.SupportForm.Application", ["5.2", "6.0", "6.1", "6.2"], [ 960, 580]],
["SYNO.SDS.App.PersonalSettings.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 850, 500]],
["SYNO.SDS.Backup.Application", ["5.2" ], [ 990, 580]],
["SYNO.SDS.ACEEditor.Application", ["5.2" ], [ 800, 400]],
["SYNO.SDS.MyDSCenter.Application", ["5.2" ], [ 990, 560]],
["SYNO.SDS.StorageReport.Application", ["5.2" ], [ 965, 580]],
["SYNO.SDS.AV.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 952, 580]],
["SYNO.SDS.AudioStation.Application", ["5.2", "6.0", "6.1", "6.2"], [ 980, 600]],
["SYNO.SDS.CardDAVServer.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 990, 560]],
["SYNO.SDS.CSTN.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 968, 580]],
["SYNO.SDS.CloudStationClient.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 900, 530]],
["SYNO.SDS.DSCloudSync.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 900, 530]],
["SYNO.SDS.CMS.Application", ["5.2", "6.0", "6.1", "6.2"], [ 990, 580]],
["SYNO.SDS.LDAP.AppInstance", ["5.2", "6.0", "6.1", "6.2"], [ 990, 665]],
["SYNO.SDS.DNS.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 990, 560]],
["SYNO.SDS.Docker.Application", ["5.2", "6.0", "6.1", "6.2"], [1060, 570]],
["SYNO.SDS.DownloadStation.Application", ["5.2", "6.0", "6.1", "6.2"], [ 980, 580]],
["SYNO.SDS.GIT.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 600, 350]],
["SYNO.SDS.Glacier.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 950, 480]],
["SYNO.SDS.HiDrive.Instance", ["5.2", "6.0" ], [1030, 580]],
["SYNO.SDS.iTunes.Application", ["5.2", "6.0", "6.1", "6.2"], [ 620, 380]],
["SYNO.SDS.JAVAMANAGER.Instance", ["5.2" ], [ 600, 300]],
["SYNO.SDS.MailServer.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 990, 560]],
["SYNO.SDS.MARIADB.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 600, 500]],
["SYNO.SDS.MediaServer.AppInstance", ["5.2", "6.0", "6.1", "6.2"], [ 950, 580]],
["SYNO.SDS.NoteStation.Application", ["5.2", "6.0", "6.1", "6.2"], [1000, 580]],
["SYNO.SDS.ProxyServer.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 990, 560]],
["SYNO.SDS.RAD.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 990, 560]],
["SYNO.SDS.SSOServer.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 990, 560]],
["SYNO.SDS.SVN.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 600, 500]],
["SYNO.SDS.VideoStation.AppInstance", ["5.2" ], [1022, 600]],
["SYNO.SDS.VPN.Instance", ["5.2", "6.0", "6.1", "6.2"], [ 860, 520]],
["SYNO.SDS.ActiveBackup.Instance", [ "6.0" ], [1050, 580]],
["SYNO.SDS.Backup.Application", [ "6.0", "6.1", "6.2"], [ 990, 580]],
["SYNO.SDS.BackupService.Instance", [ "6.0", "6.1", "6.2"], [ 810, 575]],
["SYNO.SDS.JAVA7.Instance", [ "6.0", "6.1", "6.2"], [ 600, 300]],
["SYNO.SDS.JAVA8.Instance", [ "6.0", "6.1", "6.2"], [ 600, 300]],
["SYNO.SDS.MailPlusServer.Instance", [ "6.0", "6.1", "6.2"], [1040, 600]],
["SYNO.SDS.MARIADB10.Instance", [ "6.0", "6.1", "6.2"], [ 600, 500]],
["SYNO.SDS.DSMNotify.Setting.Application", [ "6.0", "6.1", "6.2"], [ 625, 580]],
["SYNO.SDS.ClusteredShare.Application", [ "6.0", "6.1", "6.2"], [ 960, 570]],
["SYNO.SDS.DisasterRecovery.Application", [ "6.0", "6.1", "6.2"], [ 990, 580]],
["SYNO.SDS.StorageAnalyzer.Application", [ "6.0", "6.1", "6.2"], [ 965, 580]],
["SYNO.TextEditor.Application", [ "6.0", "6.1", "6.2"], [ 800, 400]],
["SYNO.SDS.WebStation.Application", [ "6.0", "6.1", "6.2"], [ 990, 580]],
["SYNO.SDS.WebDAVServer.Instance", [ "6.0", "6.1", "6.2"], [ 990, 560]],
["SYNO.Finder.Application", [ "6.1", "6.2"], [1000, 600]],
["SYNO.SDS.ActiveBackupGSuite.Instance", [ "6.1", "6.2"], [1184, 592]],
["SYNO.SDS.ActiveBackupOffice365.Instance", [ "6.1", "6.2"], [1184, 592]],
["SYNO.SDS.PrestoServer.Application", [ "6.1", "6.2"], [1024, 580]],
["SYNO.SDS.USBCopy.Application", [ "6.1", "6.2"], [ 900, 530]],
["SYNO.ActiveBackup.AppInstance", [ "6.1", "6.2"], [1184, 580]],
["SYNO.SDS.iSCSI.Application", [ "6.2"], [1042, 580]],
["SYNO.SDS.OAuthService.Instance", [ "6.2"], [ 990, 560]],
["SYNO.SDS.ADServer.Application", [ "6.2"], [ 990, 560]],
["SYNO.SDS.Virtualization.Application", [ "6.2"], [1038, 637]]
],
nextAsyncActionTime: 0,
appData: [],
/**
* Sets the restore XY position of all applications to cascaded, overlapping positions
* within the specified bounds and resets the restore size so that the windows will have
* their respective default sizes.
*
* The algorithm used ensures that each window has a position that depends entirely on the
* specified bounds, regardless of which applications are installed or which DSM version is
* used.
*
* The method call including the parameters used is logged in the console. Please note this
* down for your later information.
*
* **Note 1**: Currently open application windows will not change their size and position.
* You must close and reopen the application window to see the result. Do not move or resize
* the application window beforehand, as this immediately sets the restore size and position
* to the current window size and position.
*
* **Note 2**: The Synology CMS (Central Management System) application
* (`SYNO.SDS.CMS.Application`) does not read the restore size and position due to a bug in
* DSM. To ensure that this window has the correct size and position, each time this method
* is called, the application will be launched and the window will be set to the correct
* size and position.
*
* @param {BIT.SDS.WindowUtil~Bounds} [bounds] The bounds.
*
* @example
* BIT.SDS.WindowUtil.cascadeOverlap();
*
* @example
* BIT.SDS.WindowUtil.cascadeOverlap({x: 160, y: 139, width: 1640, height: 830});
*/
cascadeOverlap: function(bounds) {
var boundsAsLiteral;
var boundsBottomRightCorner;
var offsetX;
var offsetY;
var dsmVersion = BIT.SDS.WindowUtil.getDsmVersion();
if (bounds === undefined) bounds = BIT.SDS.WindowUtil.suggestBounds();
boundsAsLiteral = "{x: " + bounds.x + ", y: " + bounds.y + ", width: " + bounds.width + ", height: " + bounds.height + "}";
console.log("Using: BIT.SDS.WindowUtil.cascadeOverlap(" + boundsAsLiteral + ");");
offsetX = bounds.x;
offsetY = bounds.y;
boundsBottomRightCorner = {
x: bounds.x + bounds.width,
y: bounds.y + bounds.height
};
Ext.each(BIT.SDS.WindowUtil.getAppData(), function(appData) {
var windowBottomRightCorner;
var appInstances;
windowBottomRightCorner = {
x: offsetX + appData.maxDefaultWidth,
y: offsetY + appData.maxDefaultHeight
};
if (windowBottomRightCorner.x > boundsBottomRightCorner.x && windowBottomRightCorner.y > boundsBottomRightCorner.y) {
offsetX = bounds.x;
offsetY = bounds.y;
} else {
if (windowBottomRightCorner.x > boundsBottomRightCorner.x) {
if (offsetX === bounds.x) {
offsetY = bounds.y;
}
offsetX = bounds.x;
} else {
if (windowBottomRightCorner.y > boundsBottomRightCorner.y) {
// offsetX += 30;
offsetY = bounds.y;
}
}
}
if (windowBottomRightCorner.x > boundsBottomRightCorner.x && windowBottomRightCorner.y > boundsBottomRightCorner.y) {
offsetX = bounds.x;
offsetY = bounds.y;
} else {
if (windowBottomRightCorner.x > boundsBottomRightCorner.x) {
if (offsetX === bounds.x) {
offsetY = bounds.y;
}
offsetX = bounds.x;
} else {
if (windowBottomRightCorner.y > boundsBottomRightCorner.y) {
// offsetX += 30;
offsetY = bounds.y;
}
}
}
if (appData.dsmVersions.indexOf(dsmVersion) !== -1) {
BIT.SDS.WindowUtil.resetRestoreSizeAndPosition(appData.appName);
BIT.SDS.WindowUtil.setRestorePagePosition(appData.appName, offsetX, offsetY);
if (appData.appName === "SYNO.SDS.CMS.Application") {
appInstances = SYNO.SDS.AppMgr.getByAppName(appData.appName);
if ((appInstances.length === 0) && BIT.SDS.WindowUtil.isInstalled(appData.appName)) {
SYNO.SDS.AppLaunch(appData.appName, {}, false, (function() {
var x = offsetX;
var y = offsetY;
return function(appInstance) {
if (Ext.isObject(appInstance)) {
appInstance.window.setPagePosition(x, y);
}
};
})(), this);
}
}
}
offsetX += 30;
offsetY += 30;
}, this);
},
/**
* Closes the provided or all application(s).
*
* Closing application(s) is an asychronous operation, therefore this method returns a
* promise that is fulfilled when all provided applications are closed.
*
* If you call this method without providing `appNames`, all applications that can open a
* window on the DSM desktop and are currently installed on the DiskStation will be closed.
*
* @param {string[]|string} [appNames] The application name(s).
* @return {BIT.SDS.Promise} A promise.
*/
close: function(appNames) {
var MAX_TRIES = 3;
var DELAY_BETWEEN_TRIES = 5000;
var DELAY_AFTER_CLOSE = 500;
var DELAY_BEFORE_CHECK = 2000;
var appNamesForClose = [];
var promises = [];
function closeApp(appName) {
var promises = [];
Ext.each(SYNO.SDS.AppMgr.getByAppName(appName), function(appInstance) {
promises.push(BIT.SDS.Promise.retry(closeAppInstance.createDelegate(this, [appInstance]), MAX_TRIES, DELAY_BETWEEN_TRIES));
}, this);
return BIT.SDS.Promise.all(promises);
}
function closeAppInstance(appInstance) {
var delay;
if (!appInstance.window) return BIT.SDS.Promise.resolve();
delay = BIT.SDS.WindowUtil.getAndAddDelayForAsyncAction(DELAY_AFTER_CLOSE);
appInstance.window.close.defer(delay, appInstance.window);
return new BIT.SDS.Promise(function(resolve, reject) {
setTimeout(function() {
if (!appInstance.window) {
resolve();
} else {
reject();
}
}, delay + DELAY_BEFORE_CHECK);
});
}
if (appNames === undefined) appNames = BIT.SDS.WindowUtil.getAppNamesForDsmVersion();
Ext.each(appNames, function(appName) {
if (BIT.SDS.WindowUtil.isAppNameForDsmVersion(appName) && BIT.SDS.WindowUtil.isInstalled(appName) && BIT.SDS.WindowUtil.hasRunningInstance(appName)) {
appNamesForClose.push(appName);
}
}, this);
Ext.each(appNamesForClose, function(appName) {
promises.push(closeApp(appName));
}, this);
return BIT.SDS.Promise.all(promises);
},
getAndAddDelayForAsyncAction: function(delayToAdd) {
var currentTime = new Date().getTime();
var delay = BIT.SDS.WindowUtil.nextAsyncActionTime - currentTime;
delay = (delay > 0) ? delay : 0;
BIT.SDS.WindowUtil.nextAsyncActionTime = currentTime + delay + delayToAdd;
return delay;
},
/**
* An object containing data about an application.
*
* @typedef {Object} BIT.SDS.WindowUtil~AppData
* @property {string} appName The application name.
* @property {string[]} dsmVersions An array of DSM versions.
* @property {number} maxDefaultWidth The maximum default window width.
* @property {number} maxDefaultHeight The maximum default window height.
*/
/**
* Returns an array of {@link BIT.SDS.WindowUtil~AppData} for all supported applications.
*
* @return {BIT.SDS.WindowUtil~AppData[]} An array of `AppData` objects.
*/
getAppData: function() {
if (!Ext.isArray(BIT.SDS.WindowUtil.appData) || (BIT.SDS.WindowUtil.appData.length !== BIT.SDS.WindowUtil.APP_DATA_ARRAY.length)) {
BIT.SDS.WindowUtil.appData = [];
Ext.each(BIT.SDS.WindowUtil.APP_DATA_ARRAY, function(appDataElement) {
var appData = {
appName: appDataElement[0],
dsmVersions: appDataElement[1],
maxDefaultWidth: appDataElement[2][0],
maxDefaultHeight: appDataElement[2][1]
};
BIT.SDS.WindowUtil.appData.push(appData);
}, this);
}
return BIT.SDS.WindowUtil.appData;
},
/**
* Returns an array of all applications that can open a window on the DSM desktop.
*
* @return {string[]} An array of application names.
*
* @example
* BIT.SDS.WindowUtil.getAppNames();
* // => ["SYNO.SDS.AdminCenter.Application", ...]
*/
getAppNames: function() {
var appNames = [];
Ext.each(BIT.SDS.WindowUtil.getAppData(), function(appData) {
appNames.push(appData.appName);
}, this);
return appNames;
},
/**
* Returns an array of all applications that can open a window on the DSM desktop and are
* available for the DSM version currently installed on the DiskStation.
*
* @return {string[]} An array of application names.
*
* @example
* BIT.SDS.WindowUtil.getAppNamesForDsmVersion();
* // => ["SYNO.SDS.AdminCenter.Application", ...]
*/
getAppNamesForDsmVersion: function() {
var appNames = [];
var dsmVersion = BIT.SDS.WindowUtil.getDsmVersion();
Ext.each(BIT.SDS.WindowUtil.getAppData(), function(appData) {
if (appData.dsmVersions.indexOf(dsmVersion) !== -1) appNames.push(appData.appName);
}, this);
return appNames;
},
/**
* An object containing the size of an application window.
*
* @typedef {Object} BIT.SDS.WindowUtil~AppWinSize
* @property {string} appName The application name.
* @property {number} width The window width.
* @property {number} height The window height.
*/
/**
* Retrieves the respective default window size(s) of the provided or all application(s).
*
* To get the default window size, first the restore size and XY position are reset via
* {@link BIT.SDS.WindowUtil.resetRestoreSizeAndPosition}, next the application is launched
* and finally the size of the newly opened application window is retrieved.
*
* Therefore please note:
*
* - The default window size can only be retrieved for currently installed applications.
* - The applications must not be running when calling this method.
* - The current restore size and XY position will be reset for those applications.
*
* Launching the application(s) is an asychronous operation, therefore this method returns a
* promise that is fulfilled with an array of {@link BIT.SDS.WindowUtil~AppWinSize} objects.
*
* If you call this method without providing `appNames`, the default window sizes of all
* applications that can open a window on the DSM desktop and are currently installed on the
* DiskStation will be retrieved.
*
* @param {string[]|string} [appNames] The application name(s).
* @return {BIT.SDS.Promise} A promise for an array of `AppWinSize` objects.
*/
getDefaultSize: function(appNames) {
var appNamesForResetAndLaunch = [];
var promisesForAppWinsize = [];
function getAppWinSize(appInstance) {
var appWindow = appInstance.window;
var appWinSize = {
appName: appInstance.jsConfig.jsID,
};
if (appWindow.maximized || appWindow.hidden) {
if (appWindow.restoreSize) {
appWinSize.width = appWindow.restoreSize.width;
appWinSize.height = appWindow.restoreSize.height;
} else {
appWinSize.width = appWindow.width;
appWinSize.height = appWindow.height;
}
} else {
appWinSize.width = appWindow.getWidth();
appWinSize.height = appWindow.getHeight();
}
return appWinSize;
}
if (appNames === undefined) appNames = BIT.SDS.WindowUtil.getAppNamesForDsmVersion();
Ext.each(appNames, function(appName) {
if (BIT.SDS.WindowUtil.isAppNameForDsmVersion(appName) && BIT.SDS.WindowUtil.isInstalled(appName) && !BIT.SDS.WindowUtil.hasRunningInstance(appName)) {
appNamesForResetAndLaunch.push(appName);
}
}, this);
Ext.each(appNamesForResetAndLaunch, function(appName) {
BIT.SDS.WindowUtil.resetRestoreSizeAndPosition(appName);
promisesForAppWinsize.push(BIT.SDS.WindowUtil.launch(appName)
.then(function(appInstances) {
return getAppWinSize(appInstances[0]);
})
);
}, this);
return BIT.SDS.Promise.all(promisesForAppWinsize);
},
/**
* Returns the DSM version currently installed on the DiskStation. The version has the
* format: `<major version>.<minor version>`
*
* @return {string} The DSM version.
*
* @example
* BIT.SDS.WindowUtil.getDsmVersion();
* // => 6.2
*/
getDsmVersion: function() {
return _S("majorversion") + "." + _S("minorversion");
},
/**
* Returns the name of the property that holds the restore size and position of the
* application window.
*
* @param {string} appName The application name.
* @return {string} The property name.
*/
getRestoreSizePosPropertyName: function(appName) {
var restoreSizePosPropertyName = "restoreSizePos";
var dsmVersion = BIT.SDS.WindowUtil.getDsmVersion();
if (appName === "SYNO.SDS.HA.Instance") {
if (["5.2", "6.0", "6.1"].indexOf(dsmVersion) !== -1) {
restoreSizePosPropertyName = "restoreSizePos";
} else {
restoreSizePosPropertyName = "bindHAWizardWindowRestoreSizePos";
}
}
return restoreSizePosPropertyName;
},
/**
* Returns `true` if the provided application has at least one running instance.
*
* @param {string} appName The application name.
* @return {boolean} `true` if has running instance, `false` otherwise.
*/
hasRunningInstance: function(appName) {
return (SYNO.SDS.AppMgr.getByAppName(appName).length > 0);
},
/**
* Returns `true` if the provided application name is an application that can open a window
* on the DSM desktop and is available for the DSM version currently installed on the
* DiskStation.
*
* @param {string} appName The application name.
* @return {boolean} `true` if valid, `false` otherwise.
*/
isAppNameForDsmVersion: function(appName) {
return (BIT.SDS.WindowUtil.getAppNamesForDsmVersion().indexOf(appName) !== -1);
},
/**
* Returns `true` if the provided application is currently installed on the DiskStation.
*
* @param {string} appName The application name.
* @return {boolean} `true` if installed, `false` otherwise.
*/
isInstalled: function(appName) {
return (SYNO.SDS.AppUtil.getApps().indexOf(appName) !== -1);
},
/**
* Launches the provided or all application(s).
*
* Please note that already running applications will not be launched by this method.
*
* Launching the application(s) is an asychronous operation, therefore this method returns a
* promise that is fulfilled with an array of `SYNO.SDS.AppInstance` objects.
*
* If you call this method without providing `appNames`, all applications that can open a
* window on the DSM desktop and are currently installed on the DiskStation will be
* launched.
*
* @param {string[]|string} [appNames] The application name(s).
* @return {BIT.SDS.Promise} A promise for an array of `SYNO.SDS.AppInstance` objects.
*/
launch: function(appNames) {
var MAX_TRIES = 3;
var DELAY_BETWEEN_TRIES = 10000;
var DELAY_AFTER_LAUNCH = 1000;
var DELAY_BEFORE_RESOLVE = 2000;
var LAUNCH_TIMEOUT = 30000;
var appNamesForLaunch = [];
var promisesForAppInstance = [];
function launchApp(appName) {
var delay;
var promiseForAppInstance;
var rejectAfterTimeoutPromise;
delay = BIT.SDS.WindowUtil.getAndAddDelayForAsyncAction(DELAY_AFTER_LAUNCH);
promiseForAppInstance = new BIT.SDS.Promise(function(resolve, reject) {
SYNO.SDS.AppLaunch.defer(delay, this, [appName, {}, false, function(appInstance) {
var oldInstances;
if (appInstance) {
resolve.defer(DELAY_BEFORE_RESOLVE, this, [appInstance]);
} else {
oldInstances = SYNO.SDS.AppMgr.getByAppName(appName);
if (oldInstances.length > 0) {
resolve.defer(DELAY_BEFORE_RESOLVE, this, [oldInstances[oldInstances.length - 1]]);
} else {
reject();
}
}
}, this]);
});
rejectAfterTimeoutPromise = new BIT.SDS.Promise(function(resolve, reject) {
setTimeout(function() {
reject();
}, delay + LAUNCH_TIMEOUT);
});
return BIT.SDS.Promise.race([promiseForAppInstance, rejectAfterTimeoutPromise]);
}
if (appNames === undefined) appNames = BIT.SDS.WindowUtil.getAppNamesForDsmVersion();
Ext.each(appNames, function(appName) {
if (BIT.SDS.WindowUtil.isAppNameForDsmVersion(appName) && BIT.SDS.WindowUtil.isInstalled(appName) && !BIT.SDS.WindowUtil.hasRunningInstance(appName)) {
appNamesForLaunch.push(appName);
}
}, this);
Ext.each(appNamesForLaunch, function(appName) {
promisesForAppInstance.push(BIT.SDS.Promise.retry(launchApp.createDelegate(this, [appName]), MAX_TRIES, DELAY_BETWEEN_TRIES));
}, this);
return BIT.SDS.Promise.all(promisesForAppInstance);
},
/**
* Logs the respective default window size(s) of the provided or all application(s) to the
* console in CSV format. The record format is: `<application name>,<width>,<height>`
*
* To get the default window size, the method {@link BIT.SDS.WindowUtil.getDefaultSize} is
* called, therefore please note:
*
* - The default window size can only be retrieved for currently installed applications.
* - The applications must not be running when calling this method.
* - The current restore size and XY position will be reset for those applications.
*
* If you call this method without providing `appNames`, the default window sizes of all
* applications that can open a window on the DSM desktop and are currently installed on the
* DiskStation will be logged.
*
* @param {string[]|string} [appNames] The application name(s).
*
* @example
* BIT.SDS.WindowUtil.logDefaultSize();
* // SYNO.SDS.AdminCenter.Application;994;570
* // SYNO.SDS.App.FileStation3.Instance;920;560
* // ...
*
* @example
* BIT.SDS.WindowUtil.logDefaultSize("SYNO.SDS.App.FileStation3.Instance");
* // SYNO.SDS.App.FileStation3.Instance;920;560
*/
logDefaultSize: function(appNames) {
BIT.SDS.WindowUtil.getDefaultSize(appNames)
.then(function(appWinSizes) {
Ext.each(appWinSizes, function(appWinSize) {
console.log(appWinSize.appName + "," + appWinSize.width + "," + appWinSize.height);
}, this);
})
.catch(function(reason) {
console.log("Error retrieving window size: " + ((reason instanceof Error) ? reason.message : reason));
});
},
/**
* Resets the restore size and XY position of the provided application(s).
*
* If you call this method without providing `appNames`, all applications that can open a
* window on the DSM desktop and are available for the DSM version currently installed on
* the DiskStation will be reset.
*
* **Note**: Currently open application windows will not change their size and position. You
* must close and reopen the application window to see the result. Do not move or resize the
* application window beforehand, as this immediately sets the restore size and position to
* the current window size and position.
*
* @param {string[]|string} [appNames] The application name(s).
*
* @example
* BIT.SDS.WindowUtil.resetRestoreSizeAndPosition();
* // Resets the window size and position for all applications