File size: 2,202 Bytes
b456468
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Submenu from '@/ui/submenuBase';
import templateHtml from '@/ui/template/submenu/mask';
import { assignmentForDestroy, isSupportFileApi } from '@/util';

/**
 * Mask ui class
 * @class
 * @ignore
 */
class Mask extends Submenu {
  constructor(subMenuElement, { locale, makeSvgIcon, menuBarPosition, usageStatistics }) {
    super(subMenuElement, {
      locale,
      name: 'mask',
      makeSvgIcon,
      menuBarPosition,
      templateHtml,
      usageStatistics,
    });

    this._els = {
      applyButton: this.selector('.tie-mask-apply'),
      maskImageButton: this.selector('.tie-mask-image-file'),
    };
  }

  /**
   * Destroys the instance.
   */
  destroy() {
    this._removeEvent();

    assignmentForDestroy(this);
  }

  /**
   * Add event for mask
   * @param {Object} actions - actions for crop
   *   @param {Function} actions.loadImageFromURL - load image action
   *   @param {Function} actions.applyFilter - apply filter action
   */
  addEvent(actions) {
    const loadMaskFile = this._loadMaskFile.bind(this);
    const applyMask = this._applyMask.bind(this);

    this.eventHandler = {
      loadMaskFile,
      applyMask,
    };

    this.actions = actions;
    this._els.maskImageButton.addEventListener('change', loadMaskFile);
    this._els.applyButton.addEventListener('click', applyMask);
  }

  /**
   * Remove event
   * @private
   */
  _removeEvent() {
    this._els.maskImageButton.removeEventListener('change', this.eventHandler.loadMaskFile);
    this._els.applyButton.removeEventListener('click', this.eventHandler.applyMask);
  }

  /**
   * Apply mask
   * @private
   */
  _applyMask() {
    this.actions.applyFilter();
    this._els.applyButton.classList.remove('active');
  }

  /**
   * Load mask file
   * @param {object} event - File change event object
   * @private
   */
  _loadMaskFile(event) {
    let imgUrl;

    if (!isSupportFileApi()) {
      alert('This browser does not support file-api');
    }

    const [file] = event.target.files;

    if (file) {
      imgUrl = URL.createObjectURL(file);
      this.actions.loadImageFromURL(imgUrl, file);
      this._els.applyButton.classList.add('active');
    }
  }
}

export default Mask;