-
-
Notifications
You must be signed in to change notification settings - Fork 645
/
Copy pathPacker.js
283 lines (242 loc) · 7.63 KB
/
Packer.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
/**
* Muuri Packer
* Copyright (c) 2016-present, Niklas Rämö <inramo@gmail.com>
* Released under the MIT license
* https://github.com/haltu/muuri/blob/master/src/Packer/LICENSE.md
*/
import PackerProcessor, {
createWorkerProcessors,
destroyWorkerProcessors,
isWorkerProcessorsSupported,
} from './PackerProcessor';
export var FILL_GAPS = 1;
export var HORIZONTAL = 2;
export var ALIGN_RIGHT = 4;
export var ALIGN_BOTTOM = 8;
export var ROUNDING = 16;
export var PACKET_INDEX_ID = 0;
export var PACKET_INDEX_WIDTH = 1;
export var PACKET_INDEX_HEIGHT = 2;
export var PACKET_INDEX_OPTIONS = 3;
export var PACKET_HEADER_SLOTS = 4;
/**
* @class
* @param {Number} [numWorkers=0]
* @param {Object} [options]
* @param {Boolean} [options.fillGaps=false]
* @param {Boolean} [options.horizontal=false]
* @param {Boolean} [options.alignRight=false]
* @param {Boolean} [options.alignBottom=false]
* @param {Boolean} [options.rounding=false]
*/
function Packer(numWorkers, options) {
this._options = 0;
this._processor = null;
this._layoutQueue = [];
this._layouts = {};
this._layoutCallbacks = {};
this._layoutWorkers = {};
this._layoutWorkerData = {};
this._workers = [];
this._onWorkerMessage = this._onWorkerMessage.bind(this);
// Set initial options.
this.setOptions(options);
// Init the worker(s) or the processor if workers can't be used.
numWorkers = typeof numWorkers === 'number' ? Math.max(0, numWorkers) : 0;
if (numWorkers && isWorkerProcessorsSupported()) {
try {
this._workers = createWorkerProcessors(numWorkers, this._onWorkerMessage);
} catch (e) {
this._processor = new PackerProcessor();
}
} else {
this._processor = new PackerProcessor();
}
}
Packer.prototype._sendToWorker = function () {
if (!this._layoutQueue.length || !this._workers.length) return;
var layoutId = this._layoutQueue.shift();
var worker = this._workers.pop();
var data = this._layoutWorkerData[layoutId];
delete this._layoutWorkerData[layoutId];
this._layoutWorkers[layoutId] = worker;
worker.postMessage(data.buffer, [data.buffer]);
};
Packer.prototype._onWorkerMessage = function (msg) {
var data = new Float32Array(msg.data);
var layoutId = data[PACKET_INDEX_ID];
var layout = this._layouts[layoutId];
var callback = this._layoutCallbacks[layoutId];
var worker = this._layoutWorkers[layoutId];
if (layout) delete this._layouts[layoutId];
if (callback) delete this._layoutCallbacks[layoutId];
if (worker) delete this._layoutWorkers[layoutId];
if (layout && callback) {
layout.width = data[PACKET_INDEX_WIDTH];
layout.height = data[PACKET_INDEX_HEIGHT];
layout.slots = data.subarray(PACKET_HEADER_SLOTS, data.length);
this._finalizeLayout(layout);
callback(layout);
}
if (worker) {
this._workers.push(worker);
this._sendToWorker();
}
};
Packer.prototype._finalizeLayout = function (layout) {
var grid = layout._grid;
var isHorizontal = layout._settings & HORIZONTAL;
var isBorderBox = grid._boxSizing === 'border-box';
delete layout._grid;
delete layout._settings;
layout.styles = {};
if (isHorizontal) {
layout.styles.width =
(isBorderBox ? layout.width + grid._borderLeft + grid._borderRight : layout.width) + 'px';
} else {
layout.styles.height =
(isBorderBox ? layout.height + grid._borderTop + grid._borderBottom : layout.height) + 'px';
}
return layout;
};
/**
* @public
* @param {Object} [options]
* @param {Boolean} [options.fillGaps]
* @param {Boolean} [options.horizontal]
* @param {Boolean} [options.alignRight]
* @param {Boolean} [options.alignBottom]
* @param {Boolean} [options.rounding]
*/
Packer.prototype.setOptions = function (options) {
if (!options) return;
var fillGaps;
if (typeof options.fillGaps === 'boolean') {
fillGaps = options.fillGaps ? FILL_GAPS : 0;
} else {
fillGaps = this._options & FILL_GAPS;
}
var horizontal;
if (typeof options.horizontal === 'boolean') {
horizontal = options.horizontal ? HORIZONTAL : 0;
} else {
horizontal = this._options & HORIZONTAL;
}
var alignRight;
if (typeof options.alignRight === 'boolean') {
alignRight = options.alignRight ? ALIGN_RIGHT : 0;
} else {
alignRight = this._options & ALIGN_RIGHT;
}
var alignBottom;
if (typeof options.alignBottom === 'boolean') {
alignBottom = options.alignBottom ? ALIGN_BOTTOM : 0;
} else {
alignBottom = this._options & ALIGN_BOTTOM;
}
var rounding;
if (typeof options.rounding === 'boolean') {
rounding = options.rounding ? ROUNDING : 0;
} else {
rounding = this._options & ROUNDING;
}
this._options = fillGaps | horizontal | alignRight | alignBottom | rounding;
};
/**
* @public
* @param {Grid} grid
* @param {Number} layoutId
* @param {Item[]} items
* @param {Number} width
* @param {Number} height
* @param {Function} callback
* @returns {?Function}
*/
Packer.prototype.createLayout = function (grid, layoutId, items, width, height, callback) {
if (this._layouts[layoutId]) {
throw new Error('A layout with the provided id is currently being processed.');
}
var horizontal = this._options & HORIZONTAL;
var layout = {
id: layoutId,
items: items,
slots: null,
width: horizontal ? 0 : width,
height: !horizontal ? 0 : height,
// Temporary data, which will be removed before sending the layout data
// outside of Packer's context.
_grid: grid,
_settings: this._options,
};
// If there are no items let's call the callback immediately.
if (!items.length) {
layout.slots = [];
this._finalizeLayout(layout);
callback(layout);
return;
}
// Create layout synchronously if needed.
if (this._processor) {
layout.slots = window.Float32Array
? new Float32Array(items.length * 2)
: new Array(items.length * 2);
this._processor.computeLayout(layout, layout._settings);
this._finalizeLayout(layout);
callback(layout);
return;
}
// Worker data.
var data = new Float32Array(PACKET_HEADER_SLOTS + items.length * 2);
// Worker data header.
data[PACKET_INDEX_ID] = layoutId;
data[PACKET_INDEX_WIDTH] = layout.width;
data[PACKET_INDEX_HEIGHT] = layout.height;
data[PACKET_INDEX_OPTIONS] = layout._settings;
// Worker data items.
var i, j, item;
for (i = 0, j = PACKET_HEADER_SLOTS - 1, item; i < items.length; i++) {
item = items[i];
data[++j] = item._width + item._marginLeft + item._marginRight;
data[++j] = item._height + item._marginTop + item._marginBottom;
}
this._layoutQueue.push(layoutId);
this._layouts[layoutId] = layout;
this._layoutCallbacks[layoutId] = callback;
this._layoutWorkerData[layoutId] = data;
this._sendToWorker();
return this.cancelLayout.bind(this, layoutId);
};
/**
* @public
* @param {Number} layoutId
*/
Packer.prototype.cancelLayout = function (layoutId) {
var layout = this._layouts[layoutId];
if (!layout) return;
delete this._layouts[layoutId];
delete this._layoutCallbacks[layoutId];
if (this._layoutWorkerData[layoutId]) {
delete this._layoutWorkerData[layoutId];
var queueIndex = this._layoutQueue.indexOf(layoutId);
if (queueIndex > -1) this._layoutQueue.splice(queueIndex, 1);
}
};
/**
* @public
*/
Packer.prototype.destroy = function () {
// Move all currently used workers back in the workers array.
for (var key in this._layoutWorkers) {
this._workers.push(this._layoutWorkers[key]);
}
// Destroy all instance's workers.
destroyWorkerProcessors(this._workers);
// Reset data.
this._workers.length = 0;
this._layoutQueue.length = 0;
this._layouts = {};
this._layoutCallbacks = {};
this._layoutWorkers = {};
this._layoutWorkerData = {};
};
export default Packer;