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

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

goog.require('org.apache.royale.core.BeadViewBase');
goog.require('org.apache.royale.core.IBeadModel');
goog.require('org.apache.royale.core.IDateChooserModel');
goog.require('org.apache.royale.core.IFormatBead');
goog.require('org.apache.royale.core.IPopUpHost');
goog.require('org.apache.royale.core.IStrand');
goog.require('org.apache.royale.core.UIBase');
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.DateChooser');
goog.require('org.apache.royale.jewel.Table');
goog.require('org.apache.royale.jewel.TextInput');
goog.require('org.apache.royale.jewel.beads.controls.datefield.DateFieldMaskedTextInput');
goog.require('org.apache.royale.jewel.beads.controls.textinput.MaxNumberCharacters');
goog.require('org.apache.royale.jewel.beads.views.DateChooserView');
goog.require('org.apache.royale.jewel.supportClasses.ResponsiveSizes');
goog.require('org.apache.royale.jewel.supportClasses.util.positionInsideBoundingClientRect');
goog.require('org.apache.royale.utils.UIUtils');
goog.require('org.apache.royale.core.IBeadView');
goog.require('org.apache.royale.utils.Language');



/**
 *  constructor.
 *
 *  @langversion 3.0
 *  @playerversion Flash 10.2
 *  @playerversion AIR 2.6
 *  @productversion Royale 0.9.4
 * @constructor
 * @extends {org.apache.royale.core.BeadViewBase}
 * @implements {org.apache.royale.core.IBeadView}
 */
org.apache.royale.jewel.beads.views.DateFieldView = function() {
  org.apache.royale.jewel.beads.views.DateFieldView.base(this, 'constructor');
};
goog.inherits(org.apache.royale.jewel.beads.views.DateFieldView, org.apache.royale.core.BeadViewBase);


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


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


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


/**
 * @royaleignorecoercion org.apache.royale.core.UIBase
 * @protected
 * @return {Object}
 */
org.apache.royale.jewel.beads.views.DateFieldView.prototype.getHost = function() {
  return this._strand;
};


/**
 * @private
 * @param {org.apache.royale.events.Event} event
 */
org.apache.royale.jewel.beads.views.DateFieldView.prototype.org_apache_royale_jewel_beads_views_DateFieldView_handleInitComplete = function(event) {
  var /** @type {org.apache.royale.core.IFormatBead} */ formatter = org.apache.royale.utils.Language.as(this._strand.getBeadByType(org.apache.royale.core.IFormatBead), org.apache.royale.core.IFormatBead);
  formatter.addEventListener("formatChanged", org.apache.royale.utils.Language.closure(this.org_apache_royale_jewel_beads_views_DateFieldView_handleFormatChanged, this, 'handleFormatChanged'));
  var /** @type {org.apache.royale.core.IBeadModel} */ model = org.apache.royale.utils.Language.as(this._strand.getBeadByType(org.apache.royale.core.IBeadModel), org.apache.royale.core.IBeadModel);
  org.apache.royale.utils.Language.as(model, org.apache.royale.events.IEventDispatcher, true).addEventListener("selectedDateChanged", org.apache.royale.utils.Language.closure(this.org_apache_royale_jewel_beads_views_DateFieldView_selectionChangeHandler, this, 'selectionChangeHandler'));
};


/**
 * @private
 * @param {org.apache.royale.events.Event} event
 */
org.apache.royale.jewel.beads.views.DateFieldView.prototype.org_apache_royale_jewel_beads_views_DateFieldView_handlePopUpInitComplete = function(event) {
  this.getHost().dispatchEvent(new org.apache.royale.events.Event("dateChooserInitComplete"));
};


/**
 * @private
 * @param {org.apache.royale.events.Event} event
 */
org.apache.royale.jewel.beads.views.DateFieldView.prototype.org_apache_royale_jewel_beads_views_DateFieldView_handleFormatChanged = function(event) {
  var /** @type {org.apache.royale.core.IFormatBead} */ formatter = org.apache.royale.utils.Language.as(event.target, org.apache.royale.core.IFormatBead);
  this.org_apache_royale_jewel_beads_views_DateFieldView__textInput.text = formatter.formattedString;
};


/**
 * @private
 * @type {org.apache.royale.jewel.DateChooser}
 */
org.apache.royale.jewel.beads.views.DateFieldView.prototype.org_apache_royale_jewel_beads_views_DateFieldView__popUp;


/**
 * @private
 * @type {boolean}
 */
org.apache.royale.jewel.beads.views.DateFieldView.prototype.org_apache_royale_jewel_beads_views_DateFieldView__popUpVisible;


/**
 * @private
 * @type {boolean}
 */
org.apache.royale.jewel.beads.views.DateFieldView.prototype.org_apache_royale_jewel_beads_views_DateFieldView__showingPopup;


/**
 * @private
 */
org.apache.royale.jewel.beads.views.DateFieldView.prototype.org_apache_royale_jewel_beads_views_DateFieldView_prepareForPopUp = function() {
  this.org_apache_royale_jewel_beads_views_DateFieldView__popUp.addClass("open");
  document.body.classList.add("viewport");
};


/**
 * @asprivate
 * @private
 * @param {org.apache.royale.events.Event=} event
 */
org.apache.royale.jewel.beads.views.DateFieldView.prototype.org_apache_royale_jewel_beads_views_DateFieldView_selectionChangeHandler = function(event) {
  event = typeof event !== 'undefined' ? event : null;
  this.getHost().dispatchEvent(new org.apache.royale.events.Event("selectedDateChanged"));
};


/**
 * @private
 * @type {org.apache.royale.jewel.Table}
 */
org.apache.royale.jewel.beads.views.DateFieldView.prototype.org_apache_royale_jewel_beads_views_DateFieldView_daysTable;


/**
 *  When set to "auto" this resize handler monitors the width of the app window
 *  and switch between fixed and float modes.
 * 
 *  Note:This could be done with media queries, but since it handles open/close
 *  maybe this is the right way
 *
 *  @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.DateFieldView.prototype.org_apache_royale_jewel_beads_views_DateFieldView_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) {
    var /** @type {org.apache.royale.geom.Point} */ origin = new org.apache.royale.geom.Point(0, this.org_apache_royale_jewel_beads_views_DateFieldView__button.y + this.org_apache_royale_jewel_beads_views_DateFieldView__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_DateFieldView_daysTable, origin);
    this.org_apache_royale_jewel_beads_views_DateFieldView_daysTable.x = relocated.x;
    this.org_apache_royale_jewel_beads_views_DateFieldView_daysTable.y = relocated.y;
  } else {
    this.org_apache_royale_jewel_beads_views_DateFieldView_daysTable.positioner.style.left = '50%';
    this.org_apache_royale_jewel_beads_views_DateFieldView_daysTable.positioner.style.top = 'calc(100% - 10px)';
  }
};


org.apache.royale.jewel.beads.views.DateFieldView.prototype.get__menuButton = function() {
  return this.org_apache_royale_jewel_beads_views_DateFieldView__button;
};


org.apache.royale.jewel.beads.views.DateFieldView.prototype.get__textInput = function() {
  return this.org_apache_royale_jewel_beads_views_DateFieldView__textInput;
};


org.apache.royale.jewel.beads.views.DateFieldView.prototype.set__strand = function(value) {
  org.apache.royale.jewel.beads.views.DateFieldView.superClass_.set__strand.apply(this, [ value] );
  this.org_apache_royale_jewel_beads_views_DateFieldView__textInput = new org.apache.royale.jewel.TextInput();
  this.org_apache_royale_jewel_beads_views_DateFieldView__textInput.addBead(new org.apache.royale.jewel.beads.controls.datefield.DateFieldMaskedTextInput());
  var /** @type {org.apache.royale.jewel.beads.controls.textinput.MaxNumberCharacters} */ maxNumberCharacters = new org.apache.royale.jewel.beads.controls.textinput.MaxNumberCharacters();
  maxNumberCharacters.maxlength = 10;
  this.org_apache_royale_jewel_beads_views_DateFieldView__textInput.addBead(maxNumberCharacters);
  this.getHost().addElement(this.org_apache_royale_jewel_beads_views_DateFieldView__textInput);
  this.org_apache_royale_jewel_beads_views_DateFieldView__button = new org.apache.royale.jewel.Button();
  this.org_apache_royale_jewel_beads_views_DateFieldView__button.text = "&darr;";
  this.getHost().addElement(this.org_apache_royale_jewel_beads_views_DateFieldView__button);
  
  this.getHost().addEventListener("initComplete", org.apache.royale.utils.Language.closure(this.org_apache_royale_jewel_beads_views_DateFieldView_handleInitComplete, this, 'handleInitComplete'));
};


org.apache.royale.jewel.beads.views.DateFieldView.prototype.get__popUp = function() {
  return this.org_apache_royale_jewel_beads_views_DateFieldView__popUp;
};


org.apache.royale.jewel.beads.views.DateFieldView.prototype.get__popUpVisible = function() {
  return this.org_apache_royale_jewel_beads_views_DateFieldView__popUpVisible;
};


org.apache.royale.jewel.beads.views.DateFieldView.prototype.set__popUpVisible = function(value) {
  if (this.org_apache_royale_jewel_beads_views_DateFieldView__showingPopup)
    return;
  if (value != this.org_apache_royale_jewel_beads_views_DateFieldView__popUpVisible) {
    this.org_apache_royale_jewel_beads_views_DateFieldView__showingPopup = true;
    this.org_apache_royale_jewel_beads_views_DateFieldView__popUpVisible = value;
    if (value) {
      var /** @type {Object} */ 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_DateFieldView__popUp = org.apache.royale.utils.Language.as(new popUpClass(), org.apache.royale.jewel.DateChooser);
      this.org_apache_royale_jewel_beads_views_DateFieldView__popUp.className = "datechooser-popup";
      this.org_apache_royale_jewel_beads_views_DateFieldView__popUp.addEventListener("initComplete", org.apache.royale.utils.Language.closure(this.org_apache_royale_jewel_beads_views_DateFieldView_handlePopUpInitComplete, this, 'handlePopUpInitComplete'));
      var /** @type {org.apache.royale.core.IDateChooserModel} */ model = org.apache.royale.utils.Language.as(this._strand.getBeadByType(org.apache.royale.core.IDateChooserModel), org.apache.royale.core.IDateChooserModel);
      this.org_apache_royale_jewel_beads_views_DateFieldView__popUp.selectedDate = model.selectedDate;
      this.org_apache_royale_jewel_beads_views_DateFieldView__popUp.model.dayNames = model.dayNames;
      this.org_apache_royale_jewel_beads_views_DateFieldView__popUp.model.monthNames = model.monthNames;
      this.org_apache_royale_jewel_beads_views_DateFieldView__popUp.model.firstDayOfWeek = model.firstDayOfWeek;
      var /** @type {org.apache.royale.core.IPopUpHost} */ host = org.apache.royale.utils.Language.as(org.apache.royale.utils.UIUtils.findPopUpHost(this.getHost()), org.apache.royale.core.IPopUpHost);
      host.popUpParent.addElement(this.org_apache_royale_jewel_beads_views_DateFieldView__popUp);
      this.org_apache_royale_jewel_beads_views_DateFieldView_daysTable = org.apache.royale.utils.Language.as(this.popUp.view, org.apache.royale.jewel.beads.views.DateChooserView).daysTable;
      setTimeout(org.apache.royale.utils.Language.closure(this.org_apache_royale_jewel_beads_views_DateFieldView_prepareForPopUp, this, 'prepareForPopUp'), 300);
      window.addEventListener('resize', org.apache.royale.utils.Language.closure(this.org_apache_royale_jewel_beads_views_DateFieldView_autoResizeHandler, this, 'autoResizeHandler'), false);
      this.org_apache_royale_jewel_beads_views_DateFieldView_autoResizeHandler();
    } else {
      org.apache.royale.utils.UIUtils.removePopUp(this.org_apache_royale_jewel_beads_views_DateFieldView__popUp);
      document.body.classList.remove("viewport");
      window.removeEventListener('resize', org.apache.royale.utils.Language.closure(this.org_apache_royale_jewel_beads_views_DateFieldView_autoResizeHandler, this, 'autoResizeHandler'), false);
      this.org_apache_royale_jewel_beads_views_DateFieldView__popUp.removeEventListener("initComplete", org.apache.royale.utils.Language.closure(this.org_apache_royale_jewel_beads_views_DateFieldView_handlePopUpInitComplete, this, 'handlePopUpInitComplete'));
      this.org_apache_royale_jewel_beads_views_DateFieldView__popUp = null;
    }
  }
  this.org_apache_royale_jewel_beads_views_DateFieldView__showingPopup = false;
};


Object.defineProperties(org.apache.royale.jewel.beads.views.DateFieldView.prototype, /** @lends {org.apache.royale.jewel.beads.views.DateFieldView.prototype} */ {
/**
  * @export
  * @type {org.apache.royale.jewel.Button} */
menuButton: {
get: org.apache.royale.jewel.beads.views.DateFieldView.prototype.get__menuButton},
/**
  * @export
  * @type {org.apache.royale.jewel.TextInput} */
textInput: {
get: org.apache.royale.jewel.beads.views.DateFieldView.prototype.get__textInput},
/**
  * @export
  * @type {org.apache.royale.core.IStrand} */
strand: {
set: org.apache.royale.jewel.beads.views.DateFieldView.prototype.set__strand},
/**
  * @export
  * @type {org.apache.royale.jewel.DateChooser} */
popUp: {
get: org.apache.royale.jewel.beads.views.DateFieldView.prototype.get__popUp},
/**
  * @export
  * @type {boolean} */
popUpVisible: {
get: org.apache.royale.jewel.beads.views.DateFieldView.prototype.get__popUpVisible,
set: org.apache.royale.jewel.beads.views.DateFieldView.prototype.set__popUpVisible}}
);


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



/**
 * Reflection
 *
 * @return {Object.<string, Function>}
 */
org.apache.royale.jewel.beads.views.DateFieldView.prototype.ROYALE_REFLECTION_INFO = function () {
  return {
    variables: function () {return {};},
    accessors: function () {
      return {
        'menuButton': { type: 'org.apache.royale.jewel.Button', access: 'readonly', declaredBy: 'org.apache.royale.jewel.beads.views.DateFieldView'},
        'textInput': { type: 'org.apache.royale.jewel.TextInput', access: 'readonly', declaredBy: 'org.apache.royale.jewel.beads.views.DateFieldView'},
        'strand': { type: 'org.apache.royale.core.IStrand', access: 'writeonly', declaredBy: 'org.apache.royale.jewel.beads.views.DateFieldView'},
        'popUp': { type: 'org.apache.royale.jewel.DateChooser', access: 'readonly', declaredBy: 'org.apache.royale.jewel.beads.views.DateFieldView'},
        'popUpVisible': { type: 'Boolean', access: 'readwrite', declaredBy: 'org.apache.royale.jewel.beads.views.DateFieldView'}
      };
    },
    methods: function () {
      return {
        'DateFieldView': { type: '', declaredBy: 'org.apache.royale.jewel.beads.views.DateFieldView'}
      };
    }
  };
};