forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyle-property.ts
More file actions
109 lines (84 loc) · 3.32 KB
/
style-property.ts
File metadata and controls
109 lines (84 loc) · 3.32 KB
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
import definition = require("ui/styling/style-property");
import types = require("utils/types");
import observable = require("ui/core/dependency-observable");
var propertiesByName = {};
var propertiesByCssName = {};
var callbackByShorthandName = new Map<string, (value: any) => Array<definition.KeyValuePair<definition.Property, any>>>();
var inheritableProperties: Array<Property> = [];
function registerProperty(property: Property) {
if (propertiesByCssName[property.cssName]) {
throw new Error("Property with name " + property.cssName + " is already registered!");
}
propertiesByCssName[property.cssName] = property;
propertiesByName[property.name] = property;
if (property.metadata.inheritable) {
inheritableProperties.push(property);
}
}
export function getShorthandPairs(name: string, value: any): Array<definition.KeyValuePair<definition.Property, any>> {
var callback = callbackByShorthandName.get(name);
if (callback) {
return callback(value)
}
return undefined;
}
export function registerShorthandCallback(name: string, callback: (value: any) => Array<definition.KeyValuePair<definition.Property, any>>): void {
if (callbackByShorthandName.has(name)) {
throw new Error("Shorthand callback already registered for property: " + name);
}
callbackByShorthandName.set(name, callback);
}
export function getPropertyByName(name: string): Property {
return propertiesByName[name];
}
export function getPropertyByCssName(name: string): Property {
return propertiesByCssName[name];
}
export function eachProperty(callback: (property: Property) => void) {
types.verifyCallback(callback);
var i;
var key;
var keys = Object.keys(propertiesByName);
for (i = 0; i < keys.length; i++) {
key = keys[i];
callback(propertiesByName[key]);
}
}
export function eachInheritableProperty(callback: (property: Property) => void) {
types.verifyCallback(callback);
var i;
for (i = 0; i < inheritableProperties.length; i++) {
callback(inheritableProperties[i]);
}
}
export class Property extends observable.Property implements definition.Property {
private _cssName;
constructor(name: string, cssName: string, metadata: observable.PropertyMetadata, valueConverter?: (value: any) => any) {
super(name, "Style", metadata, valueConverter);
this._cssName = cssName;
registerProperty(this);
}
public get cssName(): string {
return this._cssName;
}
public _getEffectiveValue(entry: observable.PropertyEntry): any {
if (types.isDefined(entry.visualStateValue)) {
entry.valueSource = observable.ValueSource.VisualState;
return entry.visualStateValue;
}
if (types.isDefined(entry.localValue)) {
entry.valueSource = observable.ValueSource.Local;
return entry.localValue;
}
if (types.isDefined(entry.cssValue)) {
entry.valueSource = observable.ValueSource.Css;
return entry.cssValue;
}
if (types.isDefined(entry.inheritedValue)) {
entry.valueSource = observable.ValueSource.Inherited;
return entry.inheritedValue;
}
entry.valueSource = observable.ValueSource.Default;
return this.metadata.defaultValue;
}
}