/
Attribute.js
208 lines (189 loc) · 5.36 KB
/
Attribute.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
var camelcase = require("camelcase")
module.exports = Attribute
function Attribute (def) {
if (typeof def == "undefined" || def == null) {
def = {}
}
this.type = def.type || this.defaultType
this.name = def.name || ""
this.prefix = def.prefix || this.defaultPrefix
this.camelcase = def.camelcase == null ? this.defaultCamelcase : !!def.camelcase
this.property = def.property || this.name
this.onchange = def.onchange || null
this.default = null
if (Attribute.isPrimitive(def)) {
this.default = def
}
else if (def != null && def.hasOwnProperty("default")) {
this.default = def.default
}
}
/**
* Returns the type from a default value, or a definition object.
* Note: that it fails to detect whole number floats as float.
* */
Attribute.getType = function (def) {
if (typeof def == "undefined" || def == null) {
return null
}
var value
if (Attribute.isPrimitive(def)) {
value = def
}
else if (typeof def.type == "string") {
return def.type
}
else if (def.hasOwnProperty("default")) {
value = def.default
if (typeof value == "object") {
return "json"
}
}
switch (typeof value) {
case "number":
// note: it fails for 1.0
if (value === +value && value !== (value | 0)) {
return "float"
}
}
return typeof value
}
/**
* Check if this value is primitive
* */
Attribute.isPrimitive = function (value) {
switch( typeof value ) {
case "boolean":
case "number":
case "string":
return true
default:
return false
}
}
Attribute.prototype.defaultType = "string"
Attribute.prototype.defaultPrefix = ""
/** Controls if the property accessor is camelized. */
Attribute.prototype.defaultCamelcase = true
/**
* Returns the prefixed name for this attribute
* */
Object.defineProperty(Attribute.prototype, "prefixedName", {
get: function () {
return this.prefix + this.name
}
})
/**
* Called when retrieving a value from a context.
* It should return a normalized form if a serialized value.
* */
Attribute.prototype.parseValue = function (serializedValue) {
return serializedValue
}
/**
* Called when setting a value on a context.
* It should return a serialized representation of a value.
* */
Attribute.prototype.serializeValue = function (parsedValue) {
return parsedValue
}
/**
* Checks if a value should trigger an attribute removal.
* */
Attribute.prototype.shouldRemove = function( parsedValue ){
return parsedValue == null
}
/**
* Returns a context for the attribute manager methods
* */
Attribute.prototype.getContext = function (context) {
return context
}
/**
* Assign a property getter-setter to an object
* which will proxy the attribute definition's `get` and `set` methods.
* */
Attribute.prototype.defineProperty = function (obj, propertyName, getContext) {
var attribute = this
propertyName = this.camelcase ? camelcase(propertyName) : propertyName
getContext = getContext || attribute.getContext
Object.defineProperty(obj, propertyName, {
get: function () {
var context = getContext(this) || this
return attribute.get(context)
},
set: function (value) {
var context = getContext(this) || this
attribute.set(context, value, true)
}
})
}
/**
* It should return a serialized value from a context.
* */
Attribute.prototype.getFromContext = function (context, prefixedName) {
return context[prefixedName]
}
/**
* It should set a serialized value on a context.
* */
Attribute.prototype.setOnContext = function (context, prefixedName, serializedValue) {
context[prefixedName] = serializedValue
}
/**
* It should return whether or not an attribute exists on a context.
* */
Attribute.prototype.hasOnContext = function (context, prefixedName) {
return context.hasOwnProperty(prefixedName)
}
/**
* It should remove an attribute value from a context.
* */
Attribute.prototype.removeFromContext = function (context, prefixedName) {
return delete context[prefixedName]
}
/**
* Returns a parsed value from the context if it exists,
* otherwise the default value.
* */
Attribute.prototype.get = function( context ){
if (this.hasOnContext(context, this.prefixedName)) {
return this.parseValue(this.getFromContext(context, this.prefixedName))
}
else {
return this.default
}
}
/**
* Serializes a parsed value and sets it on the context,
* but only if it's not equal to the previous value.
* If the value provided triggers a removal, it removes the attribute from the context.
* */
Attribute.prototype.set = function( context, parsedValue, callOnchange ){
var previousValue = this.parseValue(this.getFromContext(context, this.prefixedName))
var newValue
// setting to the same value
if( previousValue === parsedValue ){
return
}
if (this.hasOnContext(context, this.prefixedName)) {
// removing existing value
if( this.shouldRemove(parsedValue) ){
this.removeFromContext(context, this.prefixedName)
if (this.onchange && callOnchange != false) {
this.onchange.call(context, previousValue, parsedValue)
}
return
}
}
// don't need to remove which is not there
if( this.shouldRemove(parsedValue) ){
return
}
// setting a new value
newValue = this.serializeValue(parsedValue)
this.setOnContext(context, this.prefixedName, newValue)
if (this.onchange && callOnchange != false) {
this.onchange.call(context, previousValue, parsedValue)
}
}