forked from Flipboard/react-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayerMixin.js
93 lines (76 loc) · 2.34 KB
/
LayerMixin.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
'use strict';
// Adapted from ReactART:
// https://github.com/reactjs/react-art
var FrameUtils = require('./FrameUtils');
var DrawingUtils = require('./DrawingUtils');
var EventTypes = require('./EventTypes');
var LAYER_GUID = 0;
var LayerMixin = {
construct: function(element) {
this._currentElement = element;
this._layerId = LAYER_GUID++;
},
getPublicInstance: function() {
return this.node;
},
putEventListener: function(type, listener) {
var subscriptions = this.subscriptions || (this.subscriptions = {});
var listeners = this.listeners || (this.listeners = {});
listeners[type] = listener;
if (listener) {
if (!subscriptions[type]) {
subscriptions[type] = this.node.subscribe(type, listener, this);
}
} else {
if (subscriptions[type]) {
subscriptions[type]();
delete subscriptions[type];
}
}
},
handleEvent: function(event) {
// TODO
},
destroyEventListeners: function() {
// TODO
},
applyLayerProps: function (prevProps, props) {
var layer = this.node;
var style = (props && props.style) ? props.style : {};
layer._originalStyle = style;
// Common layer properties
layer.alpha = style.alpha;
layer.backgroundColor = style.backgroundColor;
layer.borderColor = style.borderColor;
layer.borderRadius = style.borderRadius;
layer.clipRect = style.clipRect;
layer.frame = FrameUtils.make(style.left || 0, style.top || 0, style.width || 0, style.height || 0);
layer.scale = style.scale;
layer.translateX = style.translateX;
layer.translateY = style.translateY;
layer.zIndex = style.zIndex;
// Generate backing store ID as needed.
if (props.useBackingStore) {
layer.backingStoreId = this._layerId;
}
// Register events
for (var type in EventTypes) {
this.putEventListener(EventTypes[type], props[type]);
}
},
mountComponentIntoNode: function(rootID, container) {
throw new Error(
'You cannot render a Canvas component standalone. ' +
'You need to wrap it in a Surface.'
);
},
unmountComponent: function() {
// Purge backing stores on unmount.
var layer = this.node;
if (layer.backingStoreId) {
DrawingUtils.invalidateBackingStore(layer.backingStoreId);
}
this.destroyEventListeners();
}
};
module.exports = LayerMixin;