<commit_before>const ModalWindow = function(opts){

  // opts.namespace
  // opts.template

  this.opts = opts;

  const animateForward = 'modal-visible-animated-in';  
  const obj            = this;
  var   modalSelector  = '.modal-main'
  var   promise        = nil //new Promise;

  // Specific modal window
  if(opts.namespace) modalSelector = modalSelector+'.'+opts.namespace;

  const _log = function(msg) {
    if(console) console.log('[L][ModalWindow]', msg);
  }

  // Selector generator that scopes to the namespace
  // Passing nothing will just give the parent "modal-main"
  const _genSel = function(selector){
    
    if(!selector) return modalSelector;

    var ary = selector.split(',').map($.proxy(function(str){
      return [modalSelector, str.trim()].join(' ');
    }, this));

    var result = ary.join(',');

    return ary.join(',');
  }

  const isOpen = function(){
    return $('body').hasClass(animateForward);
  }

  const delayedClassRemove = function(){
    setTimeout(function(){
      $('body')
        .removeClass('modal-visible')
        .removeClass('modal-visible-animated-out');
    }, 300);
  }

  // +template+ can be a DOM to a function
  //
  // DOM it will call .html() to get the contents to fill
  //
  // Function it will call and use the returned contents
  // if false it doesn't fill so one can do their own.
  const _renderTemplate = function(){

    if(!opts.template) return;

    var str = "";

    if(typeof(opts.template)=='function') {
      _log('function template, calling it');
      str = opts.template();
    } else {
      _log("Rendering template: '"+opts.template+"'");
      str = $(opts.template).html();
    }
    
    if(str!=false) $(_genSel('.modal-item')).html(str);
  }

  const _openModal = function(){
    _log("Opening modal: '"+modalSelector+"'");
    $('.modal-main').removeClass('selected')
    $(_genSel()).addClass('selected');
  }

  // Stops the background from scrolling
  const safariFix = function(enable){
    $('body').toggleClass('modal-safari-fix', enable)
  }

  this.generateSelector = function(selector){
    return _genSel(selector);
  }

  this.open = function(){
    
    _log("Opening");

    _renderTemplate();

    _openModal();

    $('body').addClass('modal-visible').addClass(animateForward);

    setTimeout(function(){
      safariFix(true);
    }, 400);
  }

  this.close = function(){
    $('body').removeClass(animateForward);
    $('body').addClass('modal-visible-animated-out');
    delayedClassRemove();
    safariFix(false);

    return promise.resolve()
  }

  this.toggle = function(){
    if(isOpen())
      this.close();
    else
      this.open();
  }

  this.attachEvents = function(selector){

    if(!selector) selector = _genSel('.action-modal-window');

    _log("Attaching events to '"+selector+"'");

    $(document).on('click touchstart', selector, function(){

      _log("'"+selector+"' event fired!");
      
      obj.toggle();
    })
  }
<commit_msg>Using Promise; Render template mod<commit_after>const ModalWindow = function(opts){

  // opts.namespace [CSS selector]
  // opts.template [CSS selector| function]

  this.opts = opts;

  const animateForward = 'modal-visible-animated-in';  
  const obj            = this;
  var   modalSelector  = '.modal-main'

  // Specific modal window
  if(opts.namespace) modalSelector = modalSelector+'.'+opts.namespace;

  const _log = function(msg) {
    if(console) console.log('[L][ModalWindow]', msg);
  }

  // Selector generator that scopes to the namespace
  // Passing nothing will just give the parent "modal-main"
  const _genSel = function(selector){
    
    if(!selector) return modalSelector;

    var ary = selector.split(',').map($.proxy(function(str){
      return [modalSelector, str.trim()].join(' ');
    }, this));

    var result = ary.join(',');

    return ary.join(',');
  }

  const isOpen = function(){
    return $('body').hasClass(animateForward);
  }

  const delayedClassRemove = function(){
    setTimeout(function(){
      $('body')
        .removeClass('modal-visible')
        .removeClass('modal-visible-animated-out');
    }, 300);
  }

  parseTemplate = function(hsh, str){

    for (var _key in hsh) {
      _log("checking for key '"+_key+"' value '"+hsh[_key]+"'")
      str = str.replace("{{"+_key+"}}", hsh[_key])
    }

    return str
  }
  // +template+ can be a DOM to a function
  //
  // DOM it will call .html() to get the contents to fill
  //
  // Function it will call and use the returned contents
  // if false it doesn't fill so one can do their own.
  const _renderTemplate = function(options){

    if(!opts.template) return;

    var str = "";

    if(typeof(opts.template)=='function') {
      _log('function template, calling it');
      str = opts.template(obj);
    } else {
      _log("Rendering template: '"+opts.template+"'")
      
      str = $(opts.template).html()
      str = parseTemplate(options, str)
    }
    
    if(str!=false) $(_genSel('.modal-item')).html(str);
  }

  const _openModal = function(){
    _log("Opening modal: '"+modalSelector+"'");
    $('.modal-main').removeClass('selected')
    $(_genSel()).addClass('selected');
  }

  // Stops the background from scrolling
  const safariFix = function(enable){
    $('body').toggleClass('modal-safari-fix', enable)
  }

  // Called on #close; #open returns the Promise
  var _promiseResolve = function(options){
    return options
  }

  this.open = function(options){

    _log("Opening");

    _renderTemplate(options);

    _openModal();

    $('body').addClass('modal-visible').addClass(animateForward);

    setTimeout(function(){ safariFix(true); }, 400);

    const _promise = new Promise(function(resolve, reject){
      _promiseResolve = resolve
    });

    return _promise;
  }

  this.close = function(options){

    _log("Closing")

    $('body').removeClass(animateForward);
    $('body').addClass('modal-visible-animated-out');

    delayedClassRemove();

    safariFix(false);

    _promiseResolve(options)

    return obj;
  }

  this.generateSelector = function(selector){
    return _genSel(selector);
  }

  this.toggle = function(){
    if(isOpen())
      this.close();
    else
      this.open();
  }

  this.attachEvents = function(selector){

    if(!selector) selector = _genSel('.action-modal-window');

    _log("Attaching events to '"+selector+"'");

    $(document).on('click touchstart', selector, function(){

      _log("'"+selector+"' event fired!");
      
      obj.toggle();
    })
  }

{'fraction_non_alphanumeric': 0.16398467432950192, 'fraction_numerical': 0.0018390804597701149, 'mean_word_length': 3.0408668730650157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2640451', 'n_tokens_mistral': 2066, 'n_tokens_neox': 1979, 'n_words': 453}