'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var FolderSelect = function () {
  function FolderSelect(form) {
    _classCallCheck(this, FolderSelect);

    this._form = form;
    // constante variable
    this._selectTemplate = this._form.querySelector('#selectTemplate');
    this._selectsWebsite = this._form.querySelector('#level-1');
    this._selectsCreate = [].slice.call(this._form.querySelectorAll('select[id*="level-"]'));

    // constante methode
    this._handleChangeSelectsCreate = this._changeSelectsCreate.bind(this);

    this._bindEvents();
  }

  _createClass(FolderSelect, [{
    key: '_bindEvents',
    value: function _bindEvents() {
      var _this = this;

      this._selectsCreate.forEach(function (select) {
        select.addEventListener('change', _this._handleChangeSelectsCreate);
      });
    }

    /**
     * bind event for select page create
     * @param  {[type]} e [description]
     * @return {[type]}   [description]
     */

  }, {
    key: '_changeSelectsCreate',
    value: function _changeSelectsCreate(e) {
      var selectedOption = e.currentTarget.querySelector('option:checked');

      var dataShow = selectedOption.getAttribute('data-show'),
          levelShow = selectedOption.getAttribute('data-level-show'),
          levelHide = selectedOption.getAttribute('data-level-hide');

      if (typeof levelShow !== 'undefined' && levelShow !== null && levelShow !== '') {
        this._showSubLevels(levelShow, dataShow);
      }
      if (typeof levelHide !== 'undefined' && levelHide !== null && levelHide !== '') {
        this._hideSubLevels(levelHide);
      }
    }
  }, {
    key: '_hideSubLevels',
    value: function _hideSubLevels(i) {
      var levels = [].slice.call(this._form.querySelectorAll('.level-' + i));
      while (levels.length > 0) {
        levels.forEach(function (level) {
          var options = [].slice.call(level.querySelectorAll('option'));
          Array.prototype.forEach.call(options, function (option) {
            option.selected = null;
            option.removeAttribute('selected');
          });
          level.classList.add('hidden');
        });
        levels = [].slice.call(this._form.querySelectorAll('.level-' + i++));
      }
    }
  }, {
    key: '_showSubLevels',
    value: function _showSubLevels(i, dataShow) {
      var _this2 = this;

      var levels = [].slice.call(this._form.querySelectorAll('.level-' + i));
      levels.forEach(function (level) {
        level.classList.add('hidden');

        var childs = [].slice.call(_this2._form.querySelectorAll('[data-shown=' + dataShow + ']'));
        if (childs) {
          childs.forEach(function (child) {
            var options = [].slice.call(child.querySelectorAll('option'));
            Array.prototype.forEach.call(options, function (option) {
              option.selected = null;
              option.removeAttribute('selected');
            });

            child.classList.remove('hidden');
          });
        }
      });
    }
  }]);

  return FolderSelect;
}();

exports.default = FolderSelect;