'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; }; }();

var _on = require('on');

var _on2 = _interopRequireDefault(_on);

var _popup = require('./popup');

var _popup2 = _interopRequireDefault(_popup);

var _smileys = require('./smileys');

var _smileys2 = _interopRequireDefault(_smileys);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

// @Source: http://apps.timwhitlock.info/emoji/tables/unicode

var SmileyPicker = function () {
  function SmileyPicker(wrapper) {
    _classCallCheck(this, SmileyPicker);

    this.popup = new _popup2.default(wrapper);
    this.wrapper = wrapper;
    var smileyHTML = '<div class="smiley-title">Emoji</div>';
    for (var smileys in _smileys2.default) {
      var countSmiley = 0;
      smileyHTML += '<div class="smiley-subtitle">' + smileys + ' :</div>\n                    <table cellpadding="0" cellspacing="0">\n                        <tbody>\n                          <tr>';
      _smileys2.default[smileys].forEach(function (smiley) {
        if (countSmiley > 9) {
          smileyHTML += '</tr><tr>';
          countSmiley = 0;
        }
        smileyHTML += '<td class="wysiwyg-toolbar-smiley" data-smiley="' + smiley.icon + '">\n                        <span class="" title="' + smiley.desc + '" data-smiley="' + smiley.icon + '">' + smiley.icon + '</span>\n                       </td>';
        countSmiley++;
      });
      smileyHTML += '</tr>\n                      </tbody>\n                    </table>';
    }
    this.wrapper.innerHTML = smileyHTML;
    this.bindEvt();
  }

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

      this.onSmiley = (0, _on2.default)(this);
      this.wrapper.addEventListener('click', function (e) {
        var target = e.target;
        if (target.getAttribute('data-smiley') != null) {
          _this.onSmiley._fire('&nbsp;' + target.getAttribute('data-smiley') + '&nbsp;');
          _this.popup.close();
        }
      });
    }
  }, {
    key: 'show',
    value: function show(el) {
      var elBounds = el.getBoundingClientRect();
      this.popup.open(elBounds.left, elBounds.top + elBounds.height + 5);
    }
  }]);

  return SmileyPicker;
}();

exports.default = SmileyPicker;