/**
 * Generated by Apache Royale Compiler from org\apache\royale\jewel\beads\views\ComboBoxView.as
 * org.apache.royale.jewel.beads.views.ComboBoxView
 *
 * @fileoverview
 *
 * @suppress {checkTypes|accessControls}
 */

goog.provide('org.apache.royale.jewel.beads.views.ComboBoxView');

goog.require('org.apache.royale.core.BeadViewBase');
goog.require('org.apache.royale.core.IComboBoxModel');
goog.require('org.apache.royale.core.IPopUpHost');
goog.require('org.apache.royale.core.IStrand');
goog.require('org.apache.royale.core.IUIBase');
goog.require('org.apache.royale.core.StyledUIBase');
goog.require('org.apache.royale.core.ValuesManager');
goog.require('org.apache.royale.events.Event');
goog.require('org.apache.royale.events.IEventDispatcher');
goog.require('org.apache.royale.geom.Point');
goog.require('org.apache.royale.jewel.Button');
goog.require('org.apache.royale.jewel.List');
goog.require('org.apache.royale.jewel.TextInput');
goog.require('org.apache.royale.jewel.beads.views.ComboBoxPopUpView');
goog.require('org.apache.royale.jewel.supportClasses.ResponsiveSizes');
goog.require('org.apache.royale.jewel.supportClasses.combobox.ComboBoxPopUp');
goog.require('org.apache.royale.jewel.supportClasses.util.getLabelFromData');
goog.require('org.apache.royale.jewel.supportClasses.util.positionInsideBoundingClientRect');
goog.require('org.apache.royale.utils.UIUtils');
goog.require('org.apache.royale.jewel.beads.controls.combobox.IComboBoxView');
goog.require('org.apache.royale.utils.Language');



/**
 * @constructor
 * @extends {org.apache.royale.core.BeadViewBase}
 * @implements {org.apache.royale.jewel.beads.controls.combobox.IComboBoxView}
 */
org.apache.royale.jewel.beads.views.ComboBoxView = function() {
  org.apache.royale.jewel.beads.views.ComboBoxView.base(this, 'constructor');
};
goog.inherits(org.apache.royale.jewel.beads.views.ComboBoxView, org.apache.royale.core.BeadViewBase);


/**
 * Prevent renaming of class. Needed for reflection.
 */
goog.exportSymbol('org.apache.royale.jewel.beads.views.ComboBoxView', org.apache.royale.jewel.beads.views.ComboBoxView);


/**
 * @private
 * @type {org.apache.royale.jewel.TextInput}
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.org_apache_royale_jewel_beads_views_ComboBoxView__textinput;


/**
 * @private
 * @type {org.apache.royale.jewel.Button}
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.org_apache_royale_jewel_beads_views_ComboBoxView__button;


/**
 * @private
 * @type {org.apache.royale.jewel.supportClasses.combobox.ComboBoxPopUp}
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.org_apache_royale_jewel_beads_views_ComboBoxView__comboPopUp;


/**
 * @private
 * @type {org.apache.royale.jewel.List}
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.org_apache_royale_jewel_beads_views_ComboBoxView__list;


/**
 * @private
 * @type {org.apache.royale.core.IComboBoxModel}
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.org_apache_royale_jewel_beads_views_ComboBoxView_model;


/**
 * @private
 * @type {Object}
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.org_apache_royale_jewel_beads_views_ComboBoxView__popUpClass;


/**
 * @private
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.org_apache_royale_jewel_beads_views_ComboBoxView_prepareForPopUp = function() {
  this.org_apache_royale_jewel_beads_views_ComboBoxView__comboPopUp.element.classList.add("open");
  document.body.classList.add("viewport");
};


/**
 * @asprivate
 * @protected
 * @param {org.apache.royale.events.Event} event
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.handleSizeChange = function(event) {
  this.sizeChangeAction();
};


/**
 * @asprivate
 * @protected
 * @param {org.apache.royale.events.Event} event
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.handleItemChange = function(event) {
  this.itemChangeAction();
};


/**
 * @asprivate
 * @royaleignorecoercion org.apache.royale.core.IComboBoxModel
 * @protected
 * @param {org.apache.royale.events.Event=} event
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.itemChangeAction = function(event) {
  event = typeof event !== 'undefined' ? event : null;
  var /** @type {Object} */ model = this._strand.getBeadByType(org.apache.royale.core.IComboBoxModel);
  this.org_apache_royale_jewel_beads_views_ComboBoxView__textinput.text = org.apache.royale.jewel.supportClasses.util.getLabelFromData(model, model.selectedItem);
};


/**
 * Size the component at start up
 * 
 * @asprivate
 * @protected
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.initSize = function() {
  this.org_apache_royale_jewel_beads_views_ComboBoxView__button.width = 39;
  if (this.host.width == 0 || this.host.width < 89) {
    var /** @type {number} */ w = this.host.width == 0 ? 200 : 89;
    this.org_apache_royale_jewel_beads_views_ComboBoxView__textinput.width = w - this.org_apache_royale_jewel_beads_views_ComboBoxView__button.width;
    this.host.width = this.org_apache_royale_jewel_beads_views_ComboBoxView__textinput.width + this.org_apache_royale_jewel_beads_views_ComboBoxView__button.width;
  } else {
    this.org_apache_royale_jewel_beads_views_ComboBoxView__textinput.width = this.host.width - this.org_apache_royale_jewel_beads_views_ComboBoxView__button.width;
  }
  this.org_apache_royale_jewel_beads_views_ComboBoxView__textinput.percentWidth = 100;
};


/**
 * Manages the resize of the component
 * 
 * @asprivate
 * @protected
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.sizeChangeAction = function() {
  this.host.width = this.org_apache_royale_jewel_beads_views_ComboBoxView__textinput.width + this.org_apache_royale_jewel_beads_views_ComboBoxView__button.width;
};


/**
 * @protected
 * @type {org.apache.royale.jewel.supportClasses.combobox.ComboBoxPopUp}
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.comboList;


/**
 *  Adapt the popup list to the right position taking into account
 *  if we are in DESKTOP screen size or in PHONE/TABLET screen size
 *
 *  @langversion 3.0
 *  @playerversion Flash 10.2
 *  @playerversion AIR 2.6
 *  @productversion Royale 0.9.4
 * @private
 * @param {org.apache.royale.events.Event=} event
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.org_apache_royale_jewel_beads_views_ComboBoxView_autoResizeHandler = function(event) {
  event = typeof event !== 'undefined' ? event : null;
  var /** @type {number} */ outerWidth = document.body.getBoundingClientRect().width;
  var /** @type {number} */ top = (window.pageYOffset || document.documentElement.scrollTop) - (document.documentElement.clientTop || 0);
  if (outerWidth > org.apache.royale.jewel.supportClasses.ResponsiveSizes.DESKTOP_BREAKPOINT) {
    this.org_apache_royale_jewel_beads_views_ComboBoxView__list.width = this.org_apache_royale_jewel_beads_views_ComboBoxView__textinput.width + this.org_apache_royale_jewel_beads_views_ComboBoxView__button.width;
    var /** @type {org.apache.royale.geom.Point} */ origin = new org.apache.royale.geom.Point(0, this.button.y + this.button.height - top);
    var /** @type {org.apache.royale.geom.Point} */ relocated = org.apache.royale.jewel.supportClasses.util.positionInsideBoundingClientRect(this._strand, this.org_apache_royale_jewel_beads_views_ComboBoxView__list, origin);
    this.org_apache_royale_jewel_beads_views_ComboBoxView__list.x = relocated.x;
    this.org_apache_royale_jewel_beads_views_ComboBoxView__list.y = relocated.y;
  } else {
    this.org_apache_royale_jewel_beads_views_ComboBoxView__list.positioner.style["left"] = "50%";
    this.org_apache_royale_jewel_beads_views_ComboBoxView__list.positioner.style["top"] = "calc(100% - 10px)";
  }
};


org.apache.royale.jewel.beads.views.ComboBoxView.prototype.get__textinput = function() {
  return this.org_apache_royale_jewel_beads_views_ComboBoxView__textinput;
};


org.apache.royale.jewel.beads.views.ComboBoxView.prototype.get__button = function() {
  return this.org_apache_royale_jewel_beads_views_ComboBoxView__button;
};


org.apache.royale.jewel.beads.views.ComboBoxView.prototype.get__popup = function() {
  return this.org_apache_royale_jewel_beads_views_ComboBoxView__comboPopUp;
};


org.apache.royale.jewel.beads.views.ComboBoxView.prototype.set__strand = function(value) {
  org.apache.royale.jewel.beads.views.ComboBoxView.superClass_.set__strand.apply(this, [ value] );
  var /** @type {org.apache.royale.core.StyledUIBase} */ host = this._strand;
  this.org_apache_royale_jewel_beads_views_ComboBoxView__textinput = new org.apache.royale.jewel.TextInput();
  this.org_apache_royale_jewel_beads_views_ComboBoxView__button = new org.apache.royale.jewel.Button();
  this.org_apache_royale_jewel_beads_views_ComboBoxView__button.text = '▼';
  this.initSize();
  host.addElement(this.org_apache_royale_jewel_beads_views_ComboBoxView__textinput);
  host.addElement(this.org_apache_royale_jewel_beads_views_ComboBoxView__button);
  this.org_apache_royale_jewel_beads_views_ComboBoxView_model = org.apache.royale.utils.Language.as(this._strand.getBeadByType(org.apache.royale.core.IComboBoxModel), org.apache.royale.core.IComboBoxModel);
  this.org_apache_royale_jewel_beads_views_ComboBoxView_model.addEventListener("selectedIndexChanged", org.apache.royale.utils.Language.closure(this.handleItemChange, this, 'handleItemChange'));
  this.org_apache_royale_jewel_beads_views_ComboBoxView_model.addEventListener("selectedItemChanged", org.apache.royale.utils.Language.closure(this.handleItemChange, this, 'handleItemChange'));
  this.org_apache_royale_jewel_beads_views_ComboBoxView_model.addEventListener("dataProviderChanged", org.apache.royale.utils.Language.closure(this.itemChangeAction, this, 'itemChangeAction'));
  this._strand.addEventListener("sizeChanged", org.apache.royale.utils.Language.closure(this.handleSizeChange, this, 'handleSizeChange'));
};


org.apache.royale.jewel.beads.views.ComboBoxView.prototype.get__popUpVisible = function() {
  return this.org_apache_royale_jewel_beads_views_ComboBoxView__comboPopUp == null ? false : true;
};


org.apache.royale.jewel.beads.views.ComboBoxView.prototype.set__popUpVisible = function(value) {
  if (value) {
    if (!this.org_apache_royale_jewel_beads_views_ComboBoxView__popUpClass) {
      this.org_apache_royale_jewel_beads_views_ComboBoxView__popUpClass = org.apache.royale.utils.Language.as(org.apache.royale.core.ValuesManager["valuesImpl"].getValue(this._strand, "iPopUp"), Object);
    }
    this.org_apache_royale_jewel_beads_views_ComboBoxView__comboPopUp = org.apache.royale.utils.Language.as(new this.org_apache_royale_jewel_beads_views_ComboBoxView__popUpClass(), org.apache.royale.jewel.supportClasses.combobox.ComboBoxPopUp);
    this.org_apache_royale_jewel_beads_views_ComboBoxView__comboPopUp.model = this.org_apache_royale_jewel_beads_views_ComboBoxView_model;
    var /** @type {org.apache.royale.core.IPopUpHost} */ popupHost = org.apache.royale.utils.UIUtils.findPopUpHost(this._strand);
    popupHost.popUpParent.addElement(this.org_apache_royale_jewel_beads_views_ComboBoxView__comboPopUp);
    this.org_apache_royale_jewel_beads_views_ComboBoxView__list = org.apache.royale.utils.Language.as(this.org_apache_royale_jewel_beads_views_ComboBoxView__comboPopUp.view, org.apache.royale.jewel.beads.views.ComboBoxPopUpView).list;
    setTimeout(org.apache.royale.utils.Language.closure(this.org_apache_royale_jewel_beads_views_ComboBoxView_prepareForPopUp, this, 'prepareForPopUp'), 300);
    window.addEventListener('resize', org.apache.royale.utils.Language.closure(this.org_apache_royale_jewel_beads_views_ComboBoxView_autoResizeHandler, this, 'autoResizeHandler'), false);
    this.org_apache_royale_jewel_beads_views_ComboBoxView_autoResizeHandler();
  } else if (this.org_apache_royale_jewel_beads_views_ComboBoxView__comboPopUp != null) {
    org.apache.royale.utils.UIUtils.removePopUp(this.org_apache_royale_jewel_beads_views_ComboBoxView__comboPopUp);
    document.body.classList.remove("viewport");
    window.removeEventListener('resize', org.apache.royale.utils.Language.closure(this.org_apache_royale_jewel_beads_views_ComboBoxView_autoResizeHandler, this, 'autoResizeHandler'), false);
    this.org_apache_royale_jewel_beads_views_ComboBoxView__comboPopUp = null;
  }
};


Object.defineProperties(org.apache.royale.jewel.beads.views.ComboBoxView.prototype, /** @lends {org.apache.royale.jewel.beads.views.ComboBoxView.prototype} */ {
/**
  * @export
  * @type {Object} */
textinput: {
get: org.apache.royale.jewel.beads.views.ComboBoxView.prototype.get__textinput},
/**
  * @export
  * @type {Object} */
button: {
get: org.apache.royale.jewel.beads.views.ComboBoxView.prototype.get__button},
/**
  * @export
  * @type {Object} */
popup: {
get: org.apache.royale.jewel.beads.views.ComboBoxView.prototype.get__popup},
/**
  * @export
  * @type {org.apache.royale.core.IStrand} */
strand: {
set: org.apache.royale.jewel.beads.views.ComboBoxView.prototype.set__strand},
/**
  * @export
  * @type {boolean} */
popUpVisible: {
get: org.apache.royale.jewel.beads.views.ComboBoxView.prototype.get__popUpVisible,
set: org.apache.royale.jewel.beads.views.ComboBoxView.prototype.set__popUpVisible}}
);


/**
 * Metadata
 *
 * @type {Object.<string, Array.<Object>>}
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.ROYALE_CLASS_INFO = { names: [{ name: 'ComboBoxView', qName: 'org.apache.royale.jewel.beads.views.ComboBoxView', kind: 'class' }], interfaces: [org.apache.royale.jewel.beads.controls.combobox.IComboBoxView] };



/**
 * Reflection
 *
 * @return {Object.<string, Function>}
 */
org.apache.royale.jewel.beads.views.ComboBoxView.prototype.ROYALE_REFLECTION_INFO = function () {
  return {
    variables: function () {return {};},
    accessors: function () {
      return {
        'textinput': { type: 'Object', access: 'readonly', declaredBy: 'org.apache.royale.jewel.beads.views.ComboBoxView'},
        'button': { type: 'Object', access: 'readonly', declaredBy: 'org.apache.royale.jewel.beads.views.ComboBoxView'},
        'popup': { type: 'Object', access: 'readonly', declaredBy: 'org.apache.royale.jewel.beads.views.ComboBoxView'},
        'strand': { type: 'org.apache.royale.core.IStrand', access: 'writeonly', declaredBy: 'org.apache.royale.jewel.beads.views.ComboBoxView'},
        'popUpVisible': { type: 'Boolean', access: 'readwrite', declaredBy: 'org.apache.royale.jewel.beads.views.ComboBoxView'}
      };
    },
    methods: function () {
      return {
        'ComboBoxView': { type: '', declaredBy: 'org.apache.royale.jewel.beads.views.ComboBoxView'}
      };
    }
  };
};