* Removes and/or inserts bricks to the pipe at given index maintaining the * content buckets and triggering content propagation when needed. * @param {number} index Index at which bricks should be removed or inserted * @param {number} removeCount Amount of bricks to be removed * @param {
(index, removeCount, bricks = [])
| 335 | * @return {Brick[]} Array of bricks that have been removed |
| 336 | */ |
| 337 | spliceBricks (index, removeCount, bricks = []) { |
| 338 | // Normalize index |
| 339 | index = index >= 0 ? index : Math.max(this._bricks.length + index + 1, 0) |
| 340 | |
| 341 | // Reject all bucket listeners before changing bricks |
| 342 | this._bucketListeners.map(listeners => { |
| 343 | listeners.forEach(listener => listener.reject( |
| 344 | 'Pipe bricks have been changed.')) |
| 345 | return [] |
| 346 | }) |
| 347 | |
| 348 | // Instanciate serialized bricks |
| 349 | bricks = bricks.map(brick => |
| 350 | !(brick instanceof Brick) |
| 351 | ? Brick.extract(brick, this.getBrickFactory()) |
| 352 | : brick) |
| 353 | |
| 354 | // Splice internal brick array |
| 355 | const removedBricks = this._bricks.splice.apply(this._bricks, |
| 356 | [index, removeCount].concat(bricks)) |
| 357 | |
| 358 | // Splice internal state array |
| 359 | this._brickState.splice.apply(this._brickState, |
| 360 | [index, removeCount].concat(bricks.map(() => ({ |
| 361 | settingsVersion: 1, |
| 362 | busy: false |
| 363 | })))) |
| 364 | |
| 365 | // Prepare added bricks, reset removed bricks |
| 366 | let bucketInsertCount = 0 |
| 367 | let bucketRemoveCount = 0 |
| 368 | |
| 369 | bricks.forEach(brick => { |
| 370 | brick.setPipe(this) |
| 371 | this.hasView() && this.getView().addSubview(brick.getView()) |
| 372 | |
| 373 | if (brick instanceof Encoder) { |
| 374 | bucketInsertCount++ |
| 375 | } |
| 376 | |
| 377 | // Track event |
| 378 | EventManager.trigger('pipeAddBrick', { pipe: this, brick }) |
| 379 | }) |
| 380 | |
| 381 | removedBricks.forEach(brick => { |
| 382 | this.hasView() && this.getView().removeSubview(brick.getView()) |
| 383 | brick.setPipe(null) |
| 384 | |
| 385 | if (brick instanceof Encoder) { |
| 386 | bucketRemoveCount++ |
| 387 | } |
| 388 | |
| 389 | // Track event |
| 390 | EventManager.trigger('pipeRemoveBrick', { pipe: this, brick }) |
| 391 | }) |
| 392 | |
| 393 | // Update buckets if needed |
| 394 | if (bucketInsertCount > 0 || bucketRemoveCount > 0) { |
no test coverage detected