ui.js 19.1 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 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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
import snippet from 'tui-code-snippet';
import { HELP_MENUS } from './consts';
import { getSelector, assignmentForDestroy, cls } from './util';
import mainContainer from './ui/template/mainContainer';
import controls from './ui/template/controls';

import Theme from './ui/theme/theme';
import Shape from './ui/shape';
import Crop from './ui/crop';
import Flip from './ui/flip';
import Rotate from './ui/rotate';
import Text from './ui/text';
import Mask from './ui/mask';
import Icon from './ui/icon';
import Draw from './ui/draw';
import Filter from './ui/filter';
import Locale from './ui/locale/locale';

const SUB_UI_COMPONENT = {
  Shape,
  Crop,
  Flip,
  Rotate,
  Text,
  Mask,
  Icon,
  Draw,
  Filter,
};

const BI_EXPRESSION_MINSIZE_WHEN_TOP_POSITION = '1300';

/**
 * Default UI Class
 * @class
 * @param {string|HTMLElement} element - Wrapper's element or selector
 * @param {Object} [options] - Ui setting options
 *   @param {number} options.loadImage - Init default load image
 *   @param {number} options.initMenu - Init start menu
 *   @param {Boolean} [options.menuBarPosition=bottom] - Let
 *   @param {Boolean} [options.applyCropSelectionStyle=false] - Let
 *   @param {Boolean} [options.usageStatistics=false] - Use statistics or not
 *   @param {Object} [options.uiSize] - ui size of editor
 *     @param {string} options.uiSize.width - width of ui
 *     @param {string} options.uiSize.height - height of ui
 * @param {Object} actions - ui action instance
 */
class Ui {
  constructor(element, options, actions) {
    this.options = this._initializeOption(options);
    this._actions = actions;
    this.submenu = false;
    this.imageSize = {};
    this.uiSize = {};
    this._locale = new Locale(this.options.locale);
    this.theme = new Theme(this.options.theme);
    this.eventHandler = {};
    this._submenuChangeTransection = false;
    this._selectedElement = null;
    this._mainElement = null;
    this._editorElementWrap = null;
    this._editorElement = null;
    this._menuElement = null;
    this._subMenuElement = null;
    this._makeUiElement(element);
    this._setUiSize();
    this._initMenuEvent = false;

    this._makeSubMenu();
  }

  /**
   * Destroys the instance.
   */
  destroy() {
    this._removeUiEvent();
    this._destroyAllMenu();
    this._selectedElement.innerHTML = '';

    assignmentForDestroy(this);
  }

  /**
   * Set Default Selection for includeUI
   * @param {Object} option - imageEditor options
   * @returns {Object} - extends selectionStyle option
   * @ignore
   */
  setUiDefaultSelectionStyle(option) {
    return snippet.extend(
      {
        applyCropSelectionStyle: true,
        applyGroupSelectionStyle: true,
        selectionStyle: {
          cornerStyle: 'circle',
          cornerSize: 16,
          cornerColor: '#fff',
          cornerStrokeColor: '#fff',
          transparentCorners: false,
          lineWidth: 2,
          borderColor: '#fff',
        },
      },
      option
    );
  }

  /**
   * Change editor size
   * @param {Object} resizeInfo - ui & image size info
   *   @param {Object} [resizeInfo.uiSize] - image size dimension
   *     @param {string} resizeInfo.uiSize.width - ui width
   *     @param {string} resizeInfo.uiSize.height - ui height
   *   @param {Object} [resizeInfo.imageSize] - image size dimension
   *     @param {Number} resizeInfo.imageSize.oldWidth - old width
   *     @param {Number} resizeInfo.imageSize.oldHeight - old height
   *     @param {Number} resizeInfo.imageSize.newWidth - new width
   *     @param {Number} resizeInfo.imageSize.newHeight - new height
   * @example
   * // Change the image size and ui size, and change the affected ui state together.
   * imageEditor.ui.resizeEditor({
   *     imageSize: {oldWidth: 100, oldHeight: 100, newWidth: 700, newHeight: 700},
   *     uiSize: {width: 1000, height: 1000}
   * });
   * @example
   * // Apply the ui state while preserving the previous attribute (for example, if responsive Ui)
   * imageEditor.ui.resizeEditor();
   */
  resizeEditor({ uiSize, imageSize = this.imageSize } = {}) {
    if (imageSize !== this.imageSize) {
      this.imageSize = imageSize;
    }
    if (uiSize) {
      this._setUiSize(uiSize);
    }

    const { width, height } = this._getCanvasMaxDimension();
    const editorElementStyle = this._editorElement.style;
    const { menuBarPosition } = this.options;

    editorElementStyle.height = `${height}px`;
    editorElementStyle.width = `${width}px`;

    this._setEditorPosition(menuBarPosition);

    this._editorElementWrap.style.bottom = `0px`;
    this._editorElementWrap.style.top = `0px`;
    this._editorElementWrap.style.left = `0px`;
    this._editorElementWrap.style.width = `100%`;

    const selectElementClassList = this._selectedElement.classList;

    if (
      menuBarPosition === 'top' &&
      this._selectedElement.offsetWidth < BI_EXPRESSION_MINSIZE_WHEN_TOP_POSITION
    ) {
      selectElementClassList.add('tui-image-editor-top-optimization');
    } else {
      selectElementClassList.remove('tui-image-editor-top-optimization');
    }
  }

  /**
   * Change help button status
   * @param {string} buttonType - target button type
   * @param {Boolean} enableStatus - enabled status
   * @ignore
   */
  changeHelpButtonEnabled(buttonType, enableStatus) {
    const buttonClassList = this._buttonElements[buttonType].classList;

    buttonClassList[enableStatus ? 'add' : 'remove']('enabled');
  }

  /**
   * Change delete button status
   * @param {Object} [options] - Ui setting options
   *   @param {object} [options.loadImage] - Init default load image
   *   @param {string} [options.initMenu] - Init start menu
   *   @param {string} [options.menuBarPosition=bottom] - Let
   *   @param {boolean} [options.applyCropSelectionStyle=false] - Let
   *   @param {boolean} [options.usageStatistics=false] - Send statistics ping or not
   * @returns {Object} initialize option
   * @private
   */
  _initializeOption(options) {
    return snippet.extend(
      {
        loadImage: {
          path: '',
          name: '',
        },
        locale: {},
        menuIconPath: '',
        menu: ['crop', 'flip', 'rotate', 'draw', 'shape', 'icon', 'text', 'mask', 'filter'],
        initMenu: '',
        uiSize: {
          width: '100%',
          height: '100%',
        },
        menuBarPosition: 'bottom',
      },
      options
    );
  }

  /**
   * Set ui container size
   * @param {Object} uiSize - ui dimension
   *   @param {string} uiSize.width - css width property
   *   @param {string} uiSize.height - css height property
   * @private
   */
  _setUiSize(uiSize = this.options.uiSize) {
    const elementDimension = this._selectedElement.style;
    elementDimension.width = uiSize.width;
    elementDimension.height = uiSize.height;
  }

  /**
   * Make submenu dom element
   * @private
   */
  _makeSubMenu() {
    snippet.forEach(this.options.menu, (menuName) => {
      const SubComponentClass =
        SUB_UI_COMPONENT[menuName.replace(/^[a-z]/, ($0) => $0.toUpperCase())];

      // make menu element
      this._makeMenuElement(menuName);

      // menu btn element
      this._buttonElements[menuName] = this._menuElement.querySelector(`.tie-btn-${menuName}`);

      // submenu ui instance
      this[menuName] = new SubComponentClass(this._subMenuElement, {
        locale: this._locale,
        makeSvgIcon: this.theme.makeMenSvgIconSet.bind(this.theme),
        menuBarPosition: this.options.menuBarPosition,
        usageStatistics: this.options.usageStatistics,
      });
    });
  }

  /**
   * Make primary ui dom element
   * @param {string|HTMLElement} element - Wrapper's element or selector
   * @private
   */
  _makeUiElement(element) {
    let selectedElement;

    window.snippet = snippet;

    if (element.nodeType) {
      selectedElement = element;
    } else {
      selectedElement = document.querySelector(element);
    }
    const selector = getSelector(selectedElement);

    selectedElement.classList.add('tui-image-editor-container');
    selectedElement.innerHTML =
      controls({
        locale: this._locale,
        biImage: this.theme.getStyle('common.bi'),
        loadButtonStyle: this.theme.getStyle('loadButton'),
        downloadButtonStyle: this.theme.getStyle('downloadButton'),
      }) +
      mainContainer({
        locale: this._locale,
        biImage: this.theme.getStyle('common.bi'),
        commonStyle: this.theme.getStyle('common'),
        headerStyle: this.theme.getStyle('header'),
        loadButtonStyle: this.theme.getStyle('loadButton'),
        downloadButtonStyle: this.theme.getStyle('downloadButton'),
        submenuStyle: this.theme.getStyle('submenu'),
      });

    this._selectedElement = selectedElement;
    this._selectedElement.classList.add(this.options.menuBarPosition);

    this._mainElement = selector('.tui-image-editor-main');
    this._editorElementWrap = selector('.tui-image-editor-wrap');
    this._editorElement = selector('.tui-image-editor');
    this._menuElement = selector('.tui-image-editor-menu');
    this._subMenuElement = selector('.tui-image-editor-submenu');
    this._buttonElements = {
      download: this._selectedElement.querySelectorAll('.tui-image-editor-download-btn'),
      load: this._selectedElement.querySelectorAll('.tui-image-editor-load-btn'),
    };

    this._addHelpMenus();
  }

  /**
   * make array for help menu output, including partitions.
   * @returns {Array}
   * @private
   */
  _makeHelpMenuWithPartition() {
    const helpMenuWithPartition = [...HELP_MENUS, ''];
    helpMenuWithPartition.splice(3, 0, '');

    return helpMenuWithPartition;
  }

  /**
   * Add help menu
   * @private
   */
  _addHelpMenus() {
    const helpMenuWithPartition = this._makeHelpMenuWithPartition();

    snippet.forEach(helpMenuWithPartition, (menuName) => {
      if (!menuName) {
        this._makeMenuPartitionElement();
      } else {
        this._makeMenuElement(menuName, ['normal', 'disabled', 'hover'], 'help');

        if (menuName) {
          this._buttonElements[menuName] = this._menuElement.querySelector(`.tie-btn-${menuName}`);
        }
      }
    });
  }

  /**
   * Make menu partition element
   * @private
   */
  _makeMenuPartitionElement() {
    const partitionElement = document.createElement('li');
    const partitionInnerElement = document.createElement('div');
    partitionElement.className = cls('item');
    partitionInnerElement.className = cls('icpartition');
    partitionElement.appendChild(partitionInnerElement);

    this._menuElement.appendChild(partitionElement);
  }

  /**
   * Make menu button element
   * @param {string} menuName - menu name
   * @param {Array} useIconTypes - Possible values are  \['normal', 'active', 'hover', 'disabled'\]
   * @param {string} menuType - 'normal' or 'help'
   * @private
   */
  _makeMenuElement(menuName, useIconTypes = ['normal', 'active', 'hover'], menuType = 'normal') {
    const btnElement = document.createElement('li');
    const menuItemHtml = this.theme.makeMenSvgIconSet(useIconTypes, menuName);

    this._addTooltipAttribute(btnElement, menuName);
    btnElement.className = `tie-btn-${menuName} ${cls('item')} ${menuType}`;
    btnElement.innerHTML = menuItemHtml;

    this._menuElement.appendChild(btnElement);
  }

  /**
   * Add help action event
   * @private
   */
  _addHelpActionEvent() {
    snippet.forEach(HELP_MENUS, (helpName) => {
      this.eventHandler[helpName] = () => this._actions.main[helpName]();
      this._buttonElements[helpName].addEventListener('click', this.eventHandler[helpName]);
    });
  }

  /**
   * Remove help action event
   * @private
   */
  _removeHelpActionEvent() {
    snippet.forEach(HELP_MENUS, (helpName) => {
      this._buttonElements[helpName].removeEventListener('click', this.eventHandler[helpName]);
    });
  }

  /**
   * Add attribute for menu tooltip
   * @param {HTMLElement} element - menu element
   * @param {string} tooltipName - tooltipName
   * @private
   */
  _addTooltipAttribute(element, tooltipName) {
    element.setAttribute(
      'tooltip-content',
      this._locale.localize(tooltipName.replace(/^[a-z]/g, ($0) => $0.toUpperCase()))
    );
  }

  /**
   * Add download event
   * @private
   */
  _addDownloadEvent() {
    this.eventHandler.download = () => this._actions.main.download();
    snippet.forEach(this._buttonElements.download, (element) => {
      element.addEventListener('click', this.eventHandler.download);
    });
  }

  _removeDownloadEvent() {
    snippet.forEach(this._buttonElements.download, (element) => {
      element.removeEventListener('click', this.eventHandler.download);
    });
  }

  /**
   * Add load event
   * @private
   */
  _addLoadEvent() {
    this.eventHandler.loadImage = (event) => this._actions.main.load(event.target.files[0]);

    snippet.forEach(this._buttonElements.load, (element) => {
      element.addEventListener('change', this.eventHandler.loadImage);
    });
  }

  /**
   * Remmove load event
   * @private
   */
  _removeLoadEvent() {
    snippet.forEach(this._buttonElements.load, (element) => {
      element.removeEventListener('change', this.eventHandler.loadImage);
    });
  }

  /**
   * Add menu event
   * @param {string} menuName - menu name
   * @private
   */
  _addMainMenuEvent(menuName) {
    this.eventHandler[menuName] = () => this.changeMenu(menuName);
    this._buttonElements[menuName].addEventListener('click', this.eventHandler[menuName]);
  }

  /**
   * Add menu event
   * @param {string} menuName - menu name
   * @private
   */
  _addSubMenuEvent(menuName) {
    this[menuName].addEvent(this._actions[menuName]);
  }

  /**
   * Add menu event
   * @private
   */
  _addMenuEvent() {
    snippet.forEach(this.options.menu, (menuName) => {
      this._addMainMenuEvent(menuName);
      this._addSubMenuEvent(menuName);
    });
  }

  /**
   * Remove menu event
   * @private
   */
  _removeMainMenuEvent() {
    snippet.forEach(this.options.menu, (menuName) => {
      this._buttonElements[menuName].removeEventListener('click', this.eventHandler[menuName]);
    });
  }

  /**
   * Get editor area element
   * @returns {HTMLElement} editor area html element
   * @ignore
   */
  getEditorArea() {
    return this._editorElement;
  }

  /**
   * Add event for menu items
   * @ignore
   */
  activeMenuEvent() {
    if (this._initMenuEvent) {
      return;
    }

    this._addHelpActionEvent();
    this._addDownloadEvent();
    this._addMenuEvent();
    this._initMenu();
    this._initMenuEvent = true;
  }

  /**
   * Remove ui event
   * @private
   */
  _removeUiEvent() {
    this._removeHelpActionEvent();
    this._removeDownloadEvent();
    this._removeLoadEvent();
    this._removeMainMenuEvent();
  }

  /**
   * Destroy all menu instance
   * @private
   */
  _destroyAllMenu() {
    snippet.forEach(this.options.menu, (menuName) => {
      this[menuName].destroy();
    });
  }

  /**
   * Init canvas
   * @ignore
   */
  initCanvas() {
    const loadImageInfo = this._getLoadImage();
    if (loadImageInfo.path) {
      this._actions.main.initLoadImage(loadImageInfo.path, loadImageInfo.name).then(() => {
        this.activeMenuEvent();
      });
    }

    this._addLoadEvent();

    const gridVisual = document.createElement('div');

    gridVisual.className = cls('grid-visual');
    const grid = `<table>
           <tr><td class="dot left-top"></td><td></td><td class="dot right-top"></td></tr>
           <tr><td></td><td></td><td></td></tr>
           <tr><td class="dot left-bottom"></td><td></td><td class="dot right-bottom"></td></tr>
         </table>`;
    gridVisual.innerHTML = grid;
    this._editorContainerElement = this._editorElement.querySelector(
      '.tui-image-editor-canvas-container'
    );
    this._editorContainerElement.appendChild(gridVisual);
  }

  /**
   * get editor area element
   * @returns {Object} load image option
   * @private
   */
  _getLoadImage() {
    return this.options.loadImage;
  }

  /**
   * change menu
   * @param {string} menuName - menu name
   * @param {boolean} toggle - whether toogle or not
   * @param {boolean} discardSelection - discard selection
   * @ignore
   */
  changeMenu(menuName, toggle = true, discardSelection = true) {
    if (!this._submenuChangeTransection) {
      this._submenuChangeTransection = true;
      this._changeMenu(menuName, toggle, discardSelection);
      this._submenuChangeTransection = false;
    }
  }

  /**
   * change menu
   * @param {string} menuName - menu name
   * @param {boolean} toggle - whether toogle or not
   * @param {boolean} discardSelection - discard selection
   * @private
   */
  _changeMenu(menuName, toggle, discardSelection) {
    if (this.submenu) {
      this._buttonElements[this.submenu].classList.remove('active');
      this._mainElement.classList.remove(`tui-image-editor-menu-${this.submenu}`);
      if (discardSelection) {
        this._actions.main.discardSelection();
      }
      this._actions.main.changeSelectableAll(true);
      this[this.submenu].changeStandbyMode();
    }

    if (this.submenu === menuName && toggle) {
      this.submenu = null;
    } else {
      this._buttonElements[menuName].classList.add('active');
      this._mainElement.classList.add(`tui-image-editor-menu-${menuName}`);
      this.submenu = menuName;
      this[this.submenu].changeStartMode();
    }

    this.resizeEditor();
  }

  /**
   * Init menu
   * @private
   */
  _initMenu() {
    if (this.options.initMenu) {
      const evt = document.createEvent('MouseEvents');
      evt.initEvent('click', true, false);
      this._buttonElements[this.options.initMenu].dispatchEvent(evt);
    }

    if (this.icon) {
      this.icon.registDefaultIcon();
    }
  }

  /**
   * Get canvas max Dimension
   * @returns {Object} - width & height of editor
   * @private
   */
  _getCanvasMaxDimension() {
    const { maxWidth, maxHeight } = this._editorContainerElement.style;
    const width = parseFloat(maxWidth);
    const height = parseFloat(maxHeight);

    return {
      width,
      height,
    };
  }

  /**
   * Set editor position
   * @param {string} menuBarPosition - top or right or bottom or left
   * @private
   */
  // eslint-disable-next-line complexity
  _setEditorPosition(menuBarPosition) {
    const { width, height } = this._getCanvasMaxDimension();
    const editorElementStyle = this._editorElement.style;
    let top = 0;
    let left = 0;

    if (this.submenu) {
      if (menuBarPosition === 'bottom') {
        if (height > this._editorElementWrap.scrollHeight - 150) {
          top = (height - this._editorElementWrap.scrollHeight) / 2;
        } else {
          top = (150 / 2) * -1;
        }
      } else if (menuBarPosition === 'top') {
        if (height > this._editorElementWrap.offsetHeight - 150) {
          top = 150 / 2 - (height - (this._editorElementWrap.offsetHeight - 150)) / 2;
        } else {
          top = 150 / 2;
        }
      } else if (menuBarPosition === 'left') {
        if (width > this._editorElementWrap.offsetWidth - 248) {
          left = 248 / 2 - (width - (this._editorElementWrap.offsetWidth - 248)) / 2;
        } else {
          left = 248 / 2;
        }
      } else if (menuBarPosition === 'right') {
        if (width > this._editorElementWrap.scrollWidth - 248) {
          left = (width - this._editorElementWrap.scrollWidth) / 2;
        } else {
          left = (248 / 2) * -1;
        }
      }
    }
    editorElementStyle.top = `${top}px`;
    editorElementStyle.left = `${left}px`;
  }
}

export default Ui;