<commit_before>'use strict';

var __ = require('underscore'),
    Backbone = require('backbone'),
    loadTemplate = require('../utils/loadTemplate'),
    isLocalServerRunning = require('../utils/isLocalServerRunning'),    
    isRemoteServerRunning = require('../utils/isRemoteServerRunning'),        
    serverConfigMd = require('../models/serverConfigMd'),
    baseModal = require('./baseModal'),
    messageModal = require('../utils/messageModal.js');

module.exports = baseModal.extend({
  className: 'server-connect-modal',

  events: {
    'click .js-save': 'saveForm',
    'click .js-restoreDefaults': 'restoreDefaults',
    'keyup input': 'inputKeydown',
    'click .js-retry': 'retry'
  },

  initialize: function(options) {
    if (!(options.model && options.model instanceof serverConfigMd)) {
      throw new Error('Please provide a ServerConfigMd instance.');
    }

    this.listenTo(this.model, 'invalid sync', function() {
      this.render();
    });

    this._state = this.getInitialState();
  },

  getInitialState: function() {
    var state = {};

    state['attempt'] = 1;
    state['status'] = this.model.isLocalServer() ?
      'trying' : 'failed';
    state['isLocal'] = this.model.isLocalServer();

    return state;
  },

  inputKeydown: function(e) {
    this.model.set(e.target.name, e.target.value);
  },

  saveForm: function() {
    this.model.save();
  },

  restoreDefaults: function() {
    this.model.clear()
      .set( __.result(this.model, 'defaults', {}) );
    this.model.validationError = {};
    this.render();
  },

  onConnectAttempt: function(attempt) {
    if (!this.isRemoved()) {
      this.setState({ attempt: Math.ceil(attempt / 8) });
    }
  },

  setState: function(state) {
    var newState;
    
    if (typeof state['attempt'] !== 'undefined') {
      this.$attempt.text(state['attempt']);
    }

    newState =  __.extend({}, this._state, state);

    if (!__.isEqual(this._state, newState)) {
      this._state = newState;

      if (!(typeof state['attempt'] !== 'undefined' && Object.keys(state).length === 1)) {
        // if the only new state is the attemp counter, no need to
        // re-render since we manually updated that above.
        this.render();
      }
    }
  },

  retry: function() {
    this.start();
  },

  start: function() {
    var self = this;

    // if (this._started) return this;
    this.serverRunning && this.serverRunning.cancel;
    this.setState(
      __.extend(this.getInitialState(), { status: 'trying' })
    );

    if (this.model.isLocalServer()) {
      this.serverRunning = isLocalServerRunning(
        this.model.getServerBaseUrl() + '/profile',
        this.model.getGuidCheckUrl(),
        {
          interval: 250,
          // todo todo todo - make 24!!!!!
          maxAttempts: 24, // for 6 seconds    
          onAttempt: __.bind(this.onConnectAttempt, this)
        }
      ).done(function() {
        self.setState({ status: 'connected'});
        self.trigger('connect');
      }).fail(function(status) {
        if (status && status === 'canceled') return;
        self.setState({ status: 'failed'});
      });
    } else {
      this.serverRunning = isRemoteServerRunning(this.model.getServerBaseUrl() + '/profile')
        .done(function() {
          self.setState({ status: 'connected'});
          self.trigger('connect');
        }).fail(function(status) {
          if (status && status === 'canceled') return;
          self.setState({ status: 'failed'});
        });
    }

    return this;
  },

  remove: function() {
    this.serverRunning && this.serverRunning.cancel();

    // TODO: don't let us leave this modal with the model in an error state.

    baseModal.prototype.remove.apply(this, arguments);
  },  

  render: function() {
    var self = this,
        scrollPos,
        $scrollContainer;

    $scrollContainer = this.$('.flexContainer.scrollOverflowYHideX');
    $scrollContainer.length && (scrollPos = $scrollContainer[0].scrollTop);

    loadTemplate('./js/templates/serverConnectModal.html', function(t) {
      self.$el.html(
        t( __.extend({}, self.model.toJSON(), { errors: self.model.validationError || {} }, self._state) )
      );

      baseModal.prototype.render.apply(self, arguments);
      self.$('.flexContainer.scrollOverflowYHideX')[0].scrollTop = scrollPos;
      self.$attempt = self.$('.attempt').text(self._state.attempt);
    });

    return this;
  }
});<commit_msg>tweaks to the input handler on the server connect modal<commit_after>'use strict';

var __ = require('underscore'),
    Backbone = require('backbone'),
    loadTemplate = require('../utils/loadTemplate'),
    isLocalServerRunning = require('../utils/isLocalServerRunning'),    
    isRemoteServerRunning = require('../utils/isRemoteServerRunning'),        
    serverConfigMd = require('../models/serverConfigMd'),
    baseModal = require('./baseModal'),
    messageModal = require('../utils/messageModal.js');

module.exports = baseModal.extend({
  className: 'server-connect-modal',

  events: {
    'click .js-save': 'saveForm',
    'click .js-restoreDefaults': 'restoreDefaults',
    'input input': 'inputEntered',
    'click .js-retry': 'retry'
  },

  initialize: function(options) {
    if (!(options.model && options.model instanceof serverConfigMd)) {
      throw new Error('Please provide a ServerConfigMd instance.');
    }

    this.listenTo(this.model, 'invalid sync', function() {
      this.render();
    });

    this._state = this.getInitialState();
  },

  getInitialState: function() {
    var state = {};

    state['attempt'] = 1;
    state['status'] = this.model.isLocalServer() ?
      'trying' : 'failed';
    state['isLocal'] = this.model.isLocalServer();

    return state;
  },

  inputEntered: function(e) {
    this.model.set(e.target.name, e.target.value);
  },

  saveForm: function() {
    this.model.save();
  },

  restoreDefaults: function() {
    this.model.clear()
      .set( __.result(this.model, 'defaults', {}) );
    this.model.validationError = {};
    this.render();
  },

  onConnectAttempt: function(attempt) {
    if (!this.isRemoved()) {
      this.setState({ attempt: Math.ceil(attempt / 8) });
    }
  },

  setState: function(state) {
    var newState;
    
    if (typeof state['attempt'] !== 'undefined') {
      this.$attempt.text(state['attempt']);
    }

    newState =  __.extend({}, this._state, state);

    if (!__.isEqual(this._state, newState)) {
      this._state = newState;

      if (!(typeof state['attempt'] !== 'undefined' && Object.keys(state).length === 1)) {
        // if the only new state is the attemp counter, no need to
        // re-render since we manually updated that above.
        this.render();
      }
    }
  },

  retry: function() {
    this.start();
  },

  start: function() {
    var self = this;

    // if (this._started) return this;
    this.serverRunning && this.serverRunning.cancel();
    this.setState(
      __.extend(this.getInitialState(), { status: 'trying' })
    );

    if (this.model.isLocalServer()) {
      this.serverRunning = isLocalServerRunning(
        this.model.getServerBaseUrl() + '/profile',
        this.model.getGuidCheckUrl(),
        {
          interval: 250,
          // todo todo todo - make 24!!!!!
          maxAttempts: 24, // for 6 seconds    
          onAttempt: __.bind(this.onConnectAttempt, this)
        }
      ).done(function() {
        self.setState({ status: 'connected'});
        self.trigger('connect');
      }).fail(function(status) {
        if (status && status === 'canceled') return;
        self.setState({ status: 'failed'});
      });
    } else {
      this.serverRunning = isRemoteServerRunning(this.model.getServerBaseUrl() + '/profile')
        .done(function() {
          self.setState({ status: 'connected'});
          self.trigger('connect');
        }).fail(function(status) {
          if (status && status === 'canceled') return;
          self.setState({ status: 'failed'});
        });
    }

    return this;
  },

  remove: function() {
    this.serverRunning && this.serverRunning.cancel();

    // TODO: don't let us leave this modal with the model in an error state.

    baseModal.prototype.remove.apply(this, arguments);
  },  

  render: function() {
    var self = this,
        scrollPos,
        $scrollContainer;

    // todo: also restore focused element
    $scrollContainer = this.$('.flexContainer.scrollOverflowYHideX');
    $scrollContainer.length && (scrollPos = $scrollContainer[0].scrollTop);

    loadTemplate('./js/templates/serverConnectModal.html', function(t) {
      self.$el.html(
        t( __.extend({}, self.model.toJSON(), { errors: self.model.validationError || {} }, self._state) )
      );

      baseModal.prototype.render.apply(self, arguments);
      self.$('.flexContainer.scrollOverflowYHideX')[0].scrollTop = scrollPos;
      self.$attempt = self.$('.attempt').text(self._state.attempt);
    });

    return this;
  }
});
{'fraction_non_alphanumeric': 0.16454991087344029, 'fraction_numerical': 0.002896613190730838, 'mean_word_length': 2.7497911445279866, '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': '4307744', 'n_tokens_mistral': 2730, 'n_tokens_neox': 2631, 'n_words': 534}