-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Component.js
3865 lines (3442 loc) · 154 KB
/
Component.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
/*
* ${copyright}
*/
// Provides base class sap.ui.core.Component for all components
sap.ui.define([
'./Manifest',
'./ComponentMetadata',
'./Element',
'sap/base/util/extend',
'sap/base/util/deepExtend',
'sap/base/util/merge',
'sap/ui/base/ManagedObject',
'sap/ui/base/ManagedObjectRegistry',
'sap/ui/core/ResizeHandler',
'sap/ui/thirdparty/URI',
'sap/ui/performance/trace/Interaction',
'sap/base/assert',
'sap/base/Log',
'sap/base/util/ObjectPath',
'sap/base/util/UriParameters',
'sap/base/util/isPlainObject',
'sap/base/util/LoaderExtensions',
'sap/ui/VersionInfo',
'sap/ui/core/mvc/ViewType'
], function(
Manifest,
ComponentMetadata,
Element,
extend,
deepExtend,
merge,
ManagedObject,
ManagedObjectRegistry,
ResizeHandler,
URI,
Interaction,
assert,
Log,
ObjectPath,
UriParameters,
isPlainObject,
LoaderExtensions,
VersionInfo,
ViewType
) {
"use strict";
/* global Promise */
var ServiceStartupOptions = {
lazy: "lazy",
eager: "eager",
waitFor: "waitFor"
};
/**
* Utility function which adds SAP-specific parameters to a URI instance
*
* @param {URI} oUri URI.js instance
* @private
*/
function addSapParams(oUri) {
['sap-client', 'sap-server'].forEach(function(sName) {
if (!oUri.hasSearch(sName)) {
var sValue = sap.ui.getCore().getConfiguration().getSAPParam(sName);
if (sValue) {
oUri.addSearch(sName, sValue);
}
}
});
}
/**
* Utility function which merges a map of property definitions to track
* from which "source" a property was defined.
*
* This function is used to find out which Component has defined
* which "dataSource/model".
*
* @param {object} mDefinitions Map with definitions to check
* @param {object} mDefinitionSource Object to extend with definition - source mapping
* @param {object} mSourceData Actual map with definitions
* @param {object} oSource Corresponding source object which should be assigned to the definitions-source map
* @private
*/
function mergeDefinitionSource(mDefinitions, mDefinitionSource, mSourceData, oSource) {
if (mSourceData) {
for (var sName in mDefinitions) {
if (!mDefinitionSource[sName] && mSourceData[sName] && mSourceData[sName].uri) {
mDefinitionSource[sName] = oSource;
}
}
}
}
/**
* Returns the configuration of a manifest section or the value for a
* specific path. If no section or key is specified, the return value is null.
*
* @param {sap.ui.core.ComponentMetadata} oMetadata the Component metadata
* @param {sap.ui.core.Manifest} oManifest the manifest
* @param {string} sKey Either the manifest section name (namespace) or a concrete path
* @param {boolean} [bMerged] Indicates whether the manifest entry is merged with the manifest entries of the parent component.
* @return {any|null} Value of the manifest section or the key (could be any kind of value)
* @private
* @see {@link sap.ui.core.Component#getManifestEntry}
*/
function getManifestEntry(oMetadata, oManifest, sKey, bMerged) {
var oData = oManifest.getEntry(sKey);
// merge / extend should only be done for objects or when entry wasn't found
if (oData !== undefined && !isPlainObject(oData)) {
return oData;
}
// merge the configuration of the parent manifest with local manifest
// the configuration of the static component metadata will be ignored
var oParent, oParentData;
if (bMerged && (oParent = oMetadata.getParent()) instanceof ComponentMetadata) {
oParentData = oParent.getManifestEntry(sKey, bMerged);
}
// only extend / clone if there is data
// otherwise "null" will be converted into an empty object
if (oParentData || oData) {
oData = deepExtend({}, oParentData, oData);
}
return oData;
}
/**
* Utility function which creates a metadata proxy object for the given
* metadata object
*
* @param {sap.ui.core.ComponentMetadata} oMetadata the Component metadata
* @param {sap.ui.core.Manifest} oManifest the manifest
* @return {sap.ui.core.ComponentMetadata} a metadata proxy object
*/
function createMetadataProxy(oMetadata, oManifest) {
// create a proxy for the metadata object and simulate to be an
// instance of the original metadata object of the Component
// => retrieving the prototype from the original metadata to
// support to proxy sub-classes of ComponentMetadata
var oMetadataProxy = Object.create(Object.getPrototypeOf(oMetadata));
// provide internal access to the static metadata object
oMetadataProxy._oMetadata = oMetadata;
oMetadataProxy._oManifest = oManifest;
// copy all functions from the metadata object except of the
// manifest related functions which will be instance specific now
for (var m in oMetadata) {
if (!/^(getManifest|getManifestObject|getManifestEntry|getMetadataVersion)$/.test(m) && typeof oMetadata[m] === "function") {
oMetadataProxy[m] = oMetadata[m].bind(oMetadata);
}
}
// return the content of the manifest instead of the static metadata
oMetadataProxy.getManifest = function() {
return oManifest && oManifest.getJson();
};
oMetadataProxy.getManifestObject = function() {
return oManifest;
};
oMetadataProxy.getManifestEntry = function(sKey, bMerged) {
return getManifestEntry(oMetadata, oManifest, sKey, bMerged);
};
oMetadataProxy.getMetadataVersion = function() {
return 2; // instance specific manifest => metadata version 2!
};
return oMetadataProxy;
}
/**
* Calls the function <code>fn</code> once and marks all ManagedObjects
* created during that call as "owned" by the given ID.
*
* @param {function} fn Function to execute
* @param {string} sOwnerId Id of the owner
* @param {Object} [oThisArg=undefined] Value to use as <code>this</code> when executing <code>fn</code>
* @return {any} result of function <code>fn</code>
*/
function runWithOwner(fn, sOwnerId, oThisArg) {
assert(typeof fn === "function", "fn must be a function");
var oldOwnerId = ManagedObject._sOwnerId;
try {
ManagedObject._sOwnerId = sOwnerId;
return fn.call(oThisArg);
} finally {
ManagedObject._sOwnerId = oldOwnerId;
}
}
/**
* Creates and initializes a new Component with the given <code>sId</code> and
* settings.
*
* The set of allowed entries in the <code>mSettings</code> object depends on
* the concrete subclass and is described there. See {@link sap.ui.core.Component}
* for a general description of this argument.
*
* @param {string}
* [sId] Optional ID for the new control; generated automatically if
* no non-empty ID is given. Note: this can be omitted, no matter
* whether <code>mSettings</code> are given or not!
* @param {object}
* [mSettings] Optional object with initial settings for the
* new Component instance
* @public
*
* @class Base Class for Components.
* Components are independent and reusable parts of UI5 applications.
* They facilitate the encapsulation of closely related parts of an application,
* thus enabling developers to structure and maintain their applications more easily.
*
* @extends sap.ui.base.ManagedObject
* @abstract
* @author SAP SE
* @version ${version}
* @alias sap.ui.core.Component
* @since 1.9.2
*/
var Component = ManagedObject.extend("sap.ui.core.Component", /** @lends sap.ui.core.Component.prototype */
{
constructor : function(sId, mSettings) {
// create a copy of arguments for later handover to ManagedObject
var args = Array.prototype.slice.call(arguments);
// identify how the constructor has been used to extract the settings
if (typeof sId !== "string") {
mSettings = sId;
sId = undefined;
}
/**
* Checks whether a settings object was provided plus a proxy for
* the metadata object. If <strong>true</strong> the metadata proxy
* and the manifest will be stored at the instance of the Component.
*
* @param {string} [mSettings._metadataProxy]
* The proxy object for the metadata
*/
if (mSettings && typeof mSettings._metadataProxy === "object") {
// set the concrete metadata proxy and the manifest and
// delete the metadata proxy setting to avoid assert issues
this._oMetadataProxy = mSettings._metadataProxy;
this._oManifest = mSettings._metadataProxy._oManifest;
delete mSettings._metadataProxy;
/**
* Returns the metadata object which has been adopted to return
* the <strong>instance specific</strong> manifest.
*
* @return {object} the proxy object of the component metadata
*/
this.getMetadata = function() {
return this._oMetadataProxy;
};
}
if (mSettings && typeof mSettings._cacheTokens === "object") {
this._mCacheTokens = mSettings._cacheTokens;
delete mSettings._cacheTokens;
}
if (mSettings && Array.isArray(mSettings._activeTerminologies)) {
this._aActiveTerminologies = mSettings._activeTerminologies;
delete mSettings._activeTerminologies;
}
// registry of models from manifest
if (mSettings && typeof mSettings._manifestModels === "object") {
// use already created models from sap.ui.component.load if available
this._mManifestModels = mSettings._manifestModels;
delete mSettings._manifestModels;
} else {
this._mManifestModels = {};
}
// registry for services
this._mServices = {};
this._oKeepAliveConfig = this.getManifestEntry("/sap.ui5/keepAlive");
if (this._oKeepAliveConfig) {
this._oKeepAliveConfig.supported = !!this._oKeepAliveConfig.supported;
}
this._bIsActive = true;
this._aDestroyables = [];
ManagedObject.apply(this, args);
},
metadata : {
stereotype : "component",
"abstract": true,
specialSettings: {
/*
* Component data
*/
componentData: 'any'
},
version : "0.0",
/*enable/disable type validation by MessageManager
handleValidation: 'boolean'*/
includes : [], // css, javascript files that should be used in the component
dependencies : { // external dependencies
libs : [],
components : [],
ui5version : ""
},
config: {}, // static configuration
customizing: { // component/view customizing
/* Example:
"sap.ui.viewReplacements": {
"sap.xx.org.Main": {
viewName: "sap.xx.new.Main",
type: "XML"
}
},
"sap.ui.viewExtensions": {
"sap.xx.new.Main": {
"extensionX": {
name: "sap.xx.new.Fragment1",
type: "sap.ui.core.XMLFragment"
},
"extensionY": {
...
}
}
},
"sap.ui.controllerExtensions": {
"sap.xx.org.Main": {
"controllerName": "sap.xx.new.Main",
"controllerNames": ["sap.xx.new.Sub1", "sap.xx.new.Sub2"]
}
},
"sap.ui.viewModification": {
"sap.xx.new.Main": {
"myControlId": {
text: "{i18n_custom>mytext}"
}
}
}
*/
},
/*
properties: {
config : "any"
},
*/
library: "sap.ui.core"
}
}, /* Metadata constructor */ ComponentMetadata);
// apply the registry plugin
ManagedObjectRegistry.apply(Component, {
onDeregister: function(sComponentId) {
Element.registry.forEach(function(oElement) {
if ( oElement._sapui_candidateForDestroy && oElement._sOwnerId === sComponentId && !oElement.getParent() ) {
Log.debug("destroying dangling template " + oElement + " when destroying the owner component");
oElement.destroy();
}
});
}
});
/**
* Helper function to retrieve owner (extension) component holding the customizing configuration.
* @param {string|sap.ui.core.Component|sap.ui.base.ManagedObject} vObject Component Id, component instance or ManagedObject
* @throws {Error} If 'getExtensionComponent' function is given, but does not return an instance.
* @returns {sap.ui.core.Component|undefined} The owner component or <code>undefined</code>
*/
function getCustomizingComponent(vObject) {
var oComponent, sComponentId;
if (!sap.ui.getCore().getConfiguration().getDisableCustomizing()) {
if (typeof vObject === "string") {
sComponentId = vObject;
} else if (vObject && typeof vObject.isA === "function" && !vObject.isA("sap.ui.core.Component")) {
sComponentId = Component.getOwnerIdFor(vObject);
} else {
oComponent = vObject;
}
if (sComponentId) {
oComponent = Component.get(sComponentId);
}
if (oComponent) {
if (oComponent.getExtensionComponent) {
oComponent = oComponent.getExtensionComponent();
if (!oComponent) {
throw new Error("getExtensionComponent() must return an instance.");
}
}
}
}
return oComponent;
}
/**
* @param {string|sap.ui.base.ManagedObject|sap.ui.core.Component} vObject Either Component Id, ManagedObject or component instance
* @param {object} mOptions Info object to retrieve the customizing config
* @param {object} mOptions.type Either <code>sap.ui.viewExtension</code>, <code>sap.ui.controllerReplacement</code>, <code>sap.ui.viewReplacement</code>, <code>sap.ui.viewModification</code> or <code>sap.ui.controllerExtension</code>
* @param {object} mOptions.name Name of the customizing configuration
* @param {object} [mOptions.extensionName] If type <code>sap.ui.viewExtension</code>, the extension name must be provided
* @param {object} oExtInfo Config object containing the view/fragment and extension name
* @throws {Error} If 'getExtensionComponent' function is given, but does not return an instance.
* @returns {object|undefined} Object containing the customizing config or <code>undefined</code>
* @static
* @private ui5-restricted sap.ui.core
*/
Component.getCustomizing = function(vObject, mOptions) {
var sType = mOptions.type,
sPath = "/sap.ui5/extends/extensions/" + sType + "/" + mOptions.name;
if (sType === "sap.ui.viewExtensions") {
sPath += "/" + mOptions.extensionName;
}
var oComponent = getCustomizingComponent(vObject);
return oComponent ? oComponent._getManifestEntry(sPath, true) : undefined;
};
/**
* Returns the metadata for the Component class.
*
* @return {sap.ui.core.ComponentMetadata} Metadata for the Component class.
* @static
* @public
* @name sap.ui.core.Component.getMetadata
* @function
*/
/**
* Returns the metadata for the specific class of the current instance.
*
* @return {sap.ui.core.ComponentMetadata} Metadata for the specific class of the current instance.
* @public
* @name sap.ui.core.Component#getMetadata
* @function
*/
/**
* Returns the manifest defined in the metadata of the component.
* If not specified, the return value is null.
*
* @return {object} manifest.
* @public
* @since 1.33.0
*/
Component.prototype.getManifest = function() {
if (!this._oManifest) {
return this.getMetadata().getManifest();
} else {
return this._oManifest.getJson();
}
};
/**
* Returns the configuration of a manifest section or the value for a
* specific path. If no section or key is specified, the return value is null.
*
* Example:
* <code>
* {
* "sap.ui5": {
* "dependencies": {
* "libs": {
* "sap.m": {}
* },
* "components": {
* "my.component.a": {}
* }
* }
* });
* </code>
*
* The configuration above can be accessed in the following ways:
* <ul>
* <li><b>By section/namespace</b>: <code>oComponent.getManifestEntry("sap.ui5")</code></li>
* <li><b>By path</b>: <code>oComponent.getManifestEntry("/sap.ui5/dependencies/libs")</code></li>
* </ul>
*
* By section/namespace returns the configuration for the specified manifest
* section and by path allows to specify a concrete path to a dedicated entry
* inside the manifest. The path syntax always starts with a slash (/).
*
* @param {string} sKey Either the manifest section name (namespace) or a concrete path
* @return {any|null} Value of the manifest section or the key (could be any kind of value)
* @public
* @since 1.33.0
*/
Component.prototype.getManifestEntry = function(sKey) {
return this._getManifestEntry(sKey);
};
/**
* Returns the configuration of a manifest section or the value for a
* specific path. If no section or key is specified, the return value is null.
*
* @param {string} sKey Either the manifest section name (namespace) or a concrete path
* @param {boolean} [bMerged] Indicates whether the manifest entry is merged with the manifest entries of the parent component.
* @return {any|null} Value of the manifest section or the key (could be any kind of value)
* @see {@link #getManifestEntry}
* @private
* @since 1.34.2
*/
Component.prototype._getManifestEntry = function(sKey, bMerged) {
if (!this._oManifest) {
return this.getMetadata().getManifestEntry(sKey, bMerged);
} else {
return getManifestEntry(this.getMetadata(), this._oManifest, sKey, bMerged);
}
};
/**
* Returns the manifest object.
* @return {sap.ui.core.Manifest} manifest.
* @public
* @since 1.33.0
*/
Component.prototype.getManifestObject = function() {
if (!this._oManifest) {
return this.getMetadata().getManifestObject();
} else {
return this._oManifest;
}
};
/**
* Returns true, if the Component instance is a variant.
*
* A Component is a variant if the property sap.ui5/componentName
* is present in the manifest and if this property and the sap.app/id
* differs.
*
* @return {boolean} true, if the Component instance is a variant
* @private
* @since 1.45.0
*/
Component.prototype._isVariant = function() {
if (this._oManifest) {
// read the "/sap.ui5/componentName" which should be present for variants
var sComponentName = this.getManifestEntry("/sap.ui5/componentName");
// a variant differs in the "/sap.app/id" and "/sap.ui5/componentName"
return sComponentName && sComponentName !== this.getManifestEntry("/sap.app/id");
} else {
return false;
}
};
/**
* Activates the Customizing configuration for the given Component.
* @param {string} sComponentName the name of the component to activate
* @private
* @deprecated Since 1.21.0 as it is handled by component instantiation
*/
Component.activateCustomizing = function(sComponentName) {
// noop since it will be handled by component instantiation
};
/**
* Deactivates the Customizing configuration for the given Component.
* @param {string} sComponentName Name of the Component to activate
* @private
* @deprecated Since 1.21.0 as it is handled by component termination
*/
Component.deactivateCustomizing = function(sComponentName) {
// noop since it will be handled by component termination
};
// ---- Ownership functionality ------------------------------------------------------------
//
// Implementation note: the whole ownership functionality is now part of Component
// a) to ensure that only Components are used as owners
// b) to keep component related code out of ManagedObject as far as possible
//
// Only exception is the _sOwnerId property and its assignment in the ManagedObject
// constructor, but that doesn't require much knowledge about components
/**
* Returns the ID of the object in whose "context" the given ManagedObject has been created.
*
* For objects that are not ManagedObjects or for which the owner is unknown,
* <code>undefined</code> will be returned as owner ID.
*
* <strong>Note</strong>: Ownership for objects is only checked by the framework at the time
* when they are created. It is not checked or updated afterwards. And it can only be detected
* while the {@link sap.ui.core.Component#runAsOwner Component.runAsOwner} function is executing.
* Without further action, this is only the case while the content of a UIComponent is
* {@link sap.ui.core.UIComponent#createContent constructed} or when a
* {@link sap.ui.core.routing.Router Router} creates a new View and its content.
*
* <strong>Note</strong>: This method does not guarantee that the returned owner ID belongs
* to a Component. Currently, it always does. But future versions of UI5 might introduce a
* more fine grained ownership concept, e.g. taking Views into account. Callers that
* want to deal only with components as owners, should use the following method:
* {@link sap.ui.core.Component.getOwnerComponentFor Component.getOwnerComponentFor}.
* It guarantees that the returned object (if any) will be a Component.
*
* <strong>Further note</strong> that only the ID of the owner is recorded. In rare cases,
* when the lifecycle of a ManagedObject is not bound to the lifecycle of its owner,
* (e.g. by the means of aggregations), then the owner might have been destroyed already
* whereas the ManagedObject is still alive. So even the existence of an owner ID is
* not a guarantee for the existence of the corresponding owner.
*
* @param {sap.ui.base.ManagedObject} oObject Object to retrieve the owner ID for
* @return {string} ID of the owner or <code>undefined</code>
* @static
* @public
* @since 1.15.1
*/
Component.getOwnerIdFor = function(oObject) {
assert(oObject instanceof ManagedObject, "oObject must be given and must be a ManagedObject");
var sOwnerId = ( oObject instanceof ManagedObject ) && oObject._sOwnerId;
return sOwnerId || undefined; // no or empty id --> undefined
};
/**
* Returns the Component instance in whose "context" the given ManagedObject has been created
* or <code>undefined</code>.
*
* This is a convenience wrapper around {@link sap.ui.core.Component.getOwnerIdFor Component.getOwnerIdFor}.
* If the owner ID cannot be determined for reasons documented on <code>getOwnerForId</code>
* or when the Component for the determined ID no longer exists, <code>undefined</code>
* will be returned.
*
* @param {sap.ui.base.ManagedObject} oObject Object to retrieve the owner Component for
* @return {sap.ui.core.Component} the owner Component or <code>undefined</code>.
* @static
* @public
* @since 1.25.1
*/
Component.getOwnerComponentFor = function(oObject) {
return Component.get(Component.getOwnerIdFor(oObject));
};
/**
* Calls the function <code>fn</code> once and marks all ManagedObjects
* created during that call as "owned" by this Component.
*
* Nested calls of this method are supported (e.g. inside a newly created,
* nested component). The currently active owner Component will be remembered
* before executing <code>fn</code> and restored afterwards.
*
* @param {function} fn Function to execute
* @return {any} result of function <code>fn</code>
* @since 1.25.1
* @public
*/
Component.prototype.runAsOwner = function(fn) {
if (!this.isActive()) {
throw new Error("Execute 'runAsOwner' on an inactive owner component is not supported. Component: '" +
this.getMetadata().getName() + "' with id '" + this.getId() + "'.");
}
return runWithOwner(fn, this.getId());
};
// ---- ----
/**
* Components don't have a facade and therefore return themselves as their interface.
*
* @returns {this} <code>this</code> as there's no facade for components
* @see sap.ui.base.Object#getInterface
* @public
*/
Component.prototype.getInterface = function() {
return this;
};
/*
* initialize the Component and keep the component data
*/
Component.prototype._initCompositeSupport = function(mSettings) {
// make user specific data available during component instantiation
this.oComponentData = mSettings && mSettings.componentData;
// manifest initialization (loading dependencies, includes, ... / register customizing)
// => either call init on the instance specific manifest or the static one on the ComponentMetadata
if (this._oManifest) {
this._oManifest.init(this);
} else {
this.getMetadata().init();
}
if (this._isVariant()) {
// in case of variants we ensure to register the module path for the variant
// to allow module loading of code extensibility relative to the manifest
var sAppId = this._oManifest.getEntry("/sap.app/id");
if (sAppId) {
registerModulePath(sAppId, this._oManifest.resolveUri("./", "manifest"));
}
}
// init the component models
this.initComponentModels();
// error handler (if exists)
if (this.onWindowError) {
this._fnWindowErrorHandler = function(oEvent) {
var oError = oEvent.originalEvent;
this.onWindowError(oError.message, oError.filename, oError.lineno);
}.bind(this);
window.addEventListener("error", this._fnWindowErrorHandler);
}
// before unload handler (if exists)
if (this.onWindowBeforeUnload) {
this._fnWindowBeforeUnloadHandler = function(oEvent) {
var vReturnValue = this.onWindowBeforeUnload.apply(this, arguments);
// set returnValue for Chrome
if (typeof (vReturnValue) === 'string') {
oEvent.returnValue = vReturnValue;
oEvent.preventDefault();
return vReturnValue;
}
}.bind(this);
window.addEventListener("beforeunload", this._fnWindowBeforeUnloadHandler);
}
// unload handler (if exists)
if (this.onWindowUnload) {
this._fnWindowUnloadHandler = this.onWindowUnload.bind(this);
window.addEventListener("unload", this._fnWindowUnloadHandler);
}
};
/**
* Returns the list of Promises for which an automatic destroy is scheduled.
* Logs an error in case the application Component is missing a mandatory
* constructor super call.
* For compatibility reason we must not fail in this obviously broken scenario!
*
* @private
*/
Component.prototype._getDestroyables = function() {
if (!this._aDestroyables) {
Log.error("Mandatory super constructor not called for Component: '" + this.getManifestObject().getComponentName() + "'.",
null,
"sap.ui.support",
function() {
return { type: "missingSuperConstructor" };
});
this._aDestroyables = [];
}
return this._aDestroyables;
};
/*
* clean up the component and its dependent entities like models or event handlers
*/
Component.prototype.destroy = function() {
// destroy all services
for (var sLocalServiceAlias in this._mServices) {
if (this._mServices[sLocalServiceAlias].instance) {
this._mServices[sLocalServiceAlias].instance.destroy();
}
}
delete this._mServices;
// destroy all models created via manifest definition
for (var sModelName in this._mManifestModels) {
this._mManifestModels[sModelName].destroy();
}
delete this._mManifestModels;
// remove the event handlers
if (this._fnWindowErrorHandler) {
window.removeEventListener("error", this._fnWindowErrorHandler);
delete this._fnWindowErrorHandler;
}
if (this._fnWindowBeforeUnloadHandler) {
window.removeEventListener("beforeunload", this._fnWindowBeforeUnloadHandler);
delete this._fnWindowBeforeUnloadHandler;
}
if (this._fnWindowUnloadHandler) {
window.removeEventListener("unload", this._fnWindowUnloadHandler);
delete this._fnWindowUnloadHandler;
}
// destroy event bus
if (this._oEventBus) {
this._oEventBus.destroy();
delete this._oEventBus;
}
function fnDestroy(oInstance) {
if (oInstance && !oInstance._bIsBeingDestroyed) {
oInstance.destroy();
}
}
// trigger an async destroy for all registered commponent promises
var aDestroyables = this._getDestroyables();
for (var i = 0; i < aDestroyables.length; i++ ) {
aDestroyables[i] = aDestroyables[i].then(fnDestroy);
}
// destroy the object
ManagedObject.prototype.destroy.apply(this, arguments);
// unregister for messaging (on MessageManager)
sap.ui.getCore().getMessageManager().unregisterObject(this);
// manifest exit (unload includes, ... / unregister customzing)
// => either call exit on the instance specific manifest or the static one on the ComponentMetadata
if (this._oManifest) {
this._oManifest.exit(this);
delete this._oManifest;
} else {
this.getMetadata().exit();
}
return Promise.all(aDestroyables);
};
/**
* Returns user specific data object
*
* @return {object} componentData
* @public
* @since 1.15.0
*/
Component.prototype.getComponentData = function() {
return this.oComponentData;
};
/**
* Returns the event bus of this component.
* @return {sap.ui.core.EventBus} the event bus
* @since 1.20.0
* @public
*/
Component.prototype.getEventBus = function() {
if (!this._oEventBus) {
var EventBus = sap.ui.require("sap/ui/core/EventBus");
if (!EventBus) {
var sClassName = this.getMetadata().getName();
Log.warning("Synchronous loading of EventBus, due to #getEventBus() call on Component '" + sClassName + "'.", "SyncXHR", null, function() {
return {
type: "SyncXHR",
name: sClassName
};
});
// We don't expect the application to use this API anymore (see Dev-Guide)
// For the application it is recommended to declare the EventBus via sap.ui.require or sap.ui.define
EventBus = sap.ui.requireSync("sap/ui/core/EventBus"); // legacy-relevant
}
this._oEventBus = new EventBus();
if (!this.isActive()) {
this._oEventBus.suspend();
}
}
return this._oEventBus;
};
/**
* Determines if the component is active
*
* @returns {boolean} If the component is active <code>true</code>, otherwise <code>false</code>
* @since 1.88
* @private
* @ui5-restricted sap.ui.core
*/
Component.prototype.isActive = function() {
return this._bIsActive;
};
/**
* Initializes the component models and services with the configuration
* as defined in the manifest.json.
*
* @private
*/
Component.prototype.initComponentModels = function() {
// in case of having no parent metadata we simply skip that function
// since this would mean to init the models on the Component base class
var oMetadata = this.getMetadata();
if (oMetadata.isBaseClass()) {
return;
}
// retrieve the merged sap.app and sap.ui5 sections of the manifest
// to create the models for the component + inherited ones
var oManifestDataSources = this._getManifestEntry("/sap.app/dataSources", true) || {};
var oManifestModels = this._getManifestEntry("/sap.ui5/models", true) || {};
// pass the models and data sources to the internal helper
this._initComponentModels(oManifestModels, oManifestDataSources, this._mCacheTokens);
};
/**
* Initializes the component models and services which are passed as
* parameters to this function.
*
* @param {object} mModels models configuration from manifest.json
* @param {object} mDataSources data sources configuration from manifest.json
* @param {object} mCacheTokens cache tokens for OData models
*
* @private
*/
Component.prototype._initComponentModels = function(mModels, mDataSources, mCacheTokens) {
var sComponentName = this.getManifestObject().getComponentName();
var mAllModelConfigs = Component._findManifestModelClasses({
models: mModels,
dataSources: mDataSources,
componentName: sComponentName
});
Component._loadManifestModelClasses(mAllModelConfigs, sComponentName);
var mAllModelConfigurations = Component._createManifestModelConfigurations({
models: mAllModelConfigs,
dataSources: mDataSources,
component: this,
mergeParent: true,
cacheTokens: mCacheTokens,
activeTerminologies: this.getActiveTerminologies()
}),
mModelConfigurations = {},
sModelName;
if (!mAllModelConfigurations) {
return;
}
// filter out models which are already created
for (sModelName in mAllModelConfigurations) {
if (!this._mManifestModels[sModelName]) {
mModelConfigurations[sModelName] = mAllModelConfigurations[sModelName];
}
}
// create all models which are not created, yet.
var mCreatedModels = Component._createManifestModels(mModelConfigurations, sComponentName);
for (sModelName in mCreatedModels) {
// keep the model instance to be able to destroy the created models on component destroy
this._mManifestModels[sModelName] = mCreatedModels[sModelName];
}
// set all the models to the component
for (sModelName in this._mManifestModels) {
var oModel = this._mManifestModels[sModelName];
// apply the model to the component with provided name ("" as key means unnamed model)
this.setModel(oModel, sModelName || undefined);
}
};
/**
* Returns a service interface for the {@link sap.ui.core.service.Service Service}
* declared in the descriptor for components (manifest.json). The declaration needs
* to be done in the <code>sap.ui5/services</code> section as follows:
* <pre>
* {
* [...]
* "sap.ui5": {
* "services": {
* "myLocalServiceAlias": {
* "factoryName": "my.ServiceFactory",
* ["optional": true]
* }
* }
* }
* [...]
* }
* </pre>
* The service declaration is used to define a mapping between the local
* alias for the service that can be used in the Component and the name of