puiu91
11/28/2015 - 2:05 AM

gistfile1.txt


/**
 * Modal controller class
 * @return {[type]} [description]
 */
var ModalsController = function(targetModal) {

  // todo | refactor
  // (1) make class to accept an array of modals or just one
  // (2) make it so that the class is smart enough to figureout which modal is being targetted, 
  //     when made active, etc.

  // reference cell for callback
  var self = this;

  // store reference to modal
  this.modal = document.getElementById(targetModal)
  
  // this.editGeocodeModal = document.getElementById('editGeocodeModal')
  // this.anotherModal = document.getElementById('editGeocodeModal')
  // this.viewMoreDetails = document.getElementById('viewMoreDetails')

  var constructor = function() {
    self.uiClose();
  }

  constructor()
};


ModalsController.prototype.show = function() {
  this.modal.classList.add('visible')
};

ModalsController.prototype.hide = function() {
  this.modal.classList.remove('visible')
};

ModalsController.prototype.uiClose = function() {
  var parent = this

  // document.getElementById('editGeocodeModal').addEventListener('click', function(e) {
  
  // here we would need to add the listener to the container of all modals,
  // so that editGeocodeModal would actually be ModalsContainer
  this.modal.addEventListener('click', function(e) {

    // check if button was clicked
    if (e.target.nodeName === 'BUTTON') {

        // check if the correct button was clicked
        if (e.target.attributes['ui-role'].textContent === 'closeModal') {

            // get parent modal id
            var parentModalId = e.target.parentNode.parentNode.id

            // close parent modal
            parent.hide(parentModalId)
        };
    };

  }, false);

};