All files / lib/core PaperScopeItem.ts

70.58% Statements 24/34
66.66% Branches 4/6
40% Functions 2/5
70.58% Lines 24/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                              1x 1x 1x                     1x 1x 1x       1x   554x   554x   554x 554x   1x 580x 580x 580x 580x 580x 580x 580x   1x       1x                 1x     1x 1x  
/*
 * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
 * http://paperjs.org/
 *
 * Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
 * http://juerglehni.com/ & https://puckey.studio/
 *
 * Distributed under the MIT license. See LICENSE file for details.
 *
 * All rights reserved.
 */
 
// TODO: remove eslint-disable comment and deal with errors over time
/* eslint-disable */
 
import { ref } from '~/globals';
import { Base } from '~/straps';
import { Emitter } from '~/core/Emitter';
 
/**
 * @name PaperScopeItem
 *
 * @class A private base class for all classes that have lists and references in
 * the {@link PaperScope} ({@link Project}, {@link View}, {@link Tool}), so
 * functionality can be shared.
 *
 * @private
 */
export const PaperScopeItem = Base.extend(
  Emitter,
  /** @lends PaperScopeItem# */ {
    /**
     * Creates a PaperScopeItem object.
     */
    initialize: function (activate) {
      // Store reference to the currently active global paper scope:
      this._scope = ref.paper;
      // Push it onto this._scope[this._list] and set _index:
      this._index = this._scope[this._list].push(this) - 1;
      // If the project has no active reference, activate this one
      if (activate || !this._scope[this._reference]) this.activate();
    },
 
    activate: function () {
      if (!this._scope) return false;
      var prev = this._scope[this._reference];
      if (prev && prev !== this) prev.emit('deactivate');
      this._scope[this._reference] = this;
      this.emit('activate', prev);
      return true;
    },
 
    isActive: function () {
      return this._scope[this._reference] === this;
    },
 
    remove: function () {
      if (this._index == null) return false;
      Base.splice(this._scope[this._list], null, this._index, 1);
      // Clear the active tool reference if it was pointint to this.
      if (this._scope[this._reference] == this) this._scope[this._reference] = null;
      this._scope = null;
      return true;
    },
 
    getView: function () {
      return this._scope.getView();
    },
  }
);