-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmisc.js
More file actions
144 lines (133 loc) · 5.73 KB
/
misc.js
File metadata and controls
144 lines (133 loc) · 5.73 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
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
// utils/misc.js
(function(exports) {
/**
* This function sends single or multiple adapter functions by calling functions
* specified in funcs parameter for each adapter associated with the node.
*
* funcs parameter is used as a dictionary where each key is used as name of a
* adapter function to call, and corresponding value is a list of arguments
* (i.e. must be an array). For example sendAdapterEvent(node, {method : [1,2,3]})
* will call function 'method' with arguments 1,2,3 for each adapter of the node.
*
* @param {Object} node
* @param {Object} funcs
* @return {Array} array of all returned values
*/
exports.callAdapterFunc = function(node, funcs) {
var result = [];
if (!node || node._configured === undefined)
return result;
var adapters = node._configured.adapters;
for (var adapter in adapters) {
for (var func in funcs) {
var adapterObject = adapters[adapter];
var eventHandler = adapterObject[func];
if (eventHandler) {
result.push(eventHandler.apply(adapterObject, funcs[func]));
}
}
}
return result;
};
/**
* This function sends single or multiple adapter events by calling functions
* specified in events parameter for each adapter associated with the node.
*
* events parameter is used as a dictionary where each key is used as name of a
* adapter function to call, and corresponding value is a list of arguments
* (i.e. must be an array). For example sendAdapterEvent(node, {method : [1,2,3]})
* will call function 'method' with arguments 1,2,3 for each adapter of the node.
*
* @param {Object} node
* @param {Object} events
* @return {Boolean} false if node is not configured.
*/
exports.sendAdapterEvent = function(node, events) {
if (!node || node._configured === undefined)
return false;
var adapters = node._configured.adapters;
for (var adapter in adapters) {
for (var event in events) {
var eventHandler = adapters[adapter][event];
if (eventHandler) {
eventHandler.apply(adapters[adapter], events[event]);
}
}
}
return true;
};
/**
*
* Dispatch custom HTML event
*
* @param {Object} target element or document.
* @param {string} eventType custom event type.
* @param {boolean} canBubble Whether the event propagates upward. Sets the value for the bubbles property.
* @param {boolean} cancelable Whether the event is cancelable and so preventDefault can be called. Sets the value
* for the cancelable property.
* @param {Object} detail A user-defined object that can contain additional information about the event.
* This parameter can be of any type, or null. This value is returned in the detail property of the event.
*/
exports.dispatchCustomEvent = function(target, eventType, canBubble, cancelable, detail) {
var event = document.createEvent('CustomEvent');
event.initCustomEvent(eventType, canBubble, cancelable, detail);
return target.dispatchEvent(event);
};
/** Dispatch HTML event
*
* @param {Object} target element or document
* @param {string} eventType standard event type e.g. load, click
*/
exports.dispatchEvent = function(target, eventType) {
var evt = null;
if (document.createEvent) {
evt = document.createEvent("Events");
evt.initEvent(eventType, true, true);
return target.dispatchEvent(evt);
} else if (document.createEventObject) {
evt = document.createEventObject();
return target.fireEvent('on' + eventType, evt);
}
};
var tmpCanvas, tmpContext;
exports.toImageData = function(imageData) {
if(imageData instanceof ImageData)
return imageData;
if(!imageData.data)
throw new Error("no data property");
if(!imageData.width)
throw new Error("no width property");
if(!imageData.height)
throw new Error("no height property");
if(!tmpContext) {
tmpCanvas = document.createElement('canvas');
tmpContext = tmpCanvas.getContext('2d');
}
var newImageData = tmpContext.createImageData(imageData.width, imageData.height);
for(var i = 0; i < imageData.data.length; ++i) {
var v = imageData.data[i];
if(v > 255)
v = 255;
if(v < 0)
v = 0;
newImageData.data[i] = v;
}
return newImageData;
};
exports.elementIs = function(elem, name) {
return elem && elem.tagName.toLowerCase() === name;
};
exports.encodeZIndex = function(zIndex, isLeafNode) {
if (zIndex === "auto" || zIndex === "" || zIndex === "0") {
if (isLeafNode) {
zIndex = "0"; // Always give leaf nodes an implicit stacking context with z-index of 0 to ensure they compare properly with negative z-index leaf nodes
} else {
return ""; // Otherwise this group node does not create a new stacking context, so we can return an empty string
}
}
// Pad with enough zeros to cover the maximum/minimum values (2147483647) for correct string compare results in the sorting step
zIndex = "0000000000" + zIndex;
zIndex = zIndex.slice(zIndex.length - 10); // 10 is the number of digits in the max value
return zIndex;
}
}(module.exports));