all files / lib/ui/ BaseForm.js

60.47% Statements 26/43
11.76% Branches 2/17
18.18% Functions 2/11
86.67% Lines 26/30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                                                    
var _ = require('lodash');
 
var util = require('slap-util');
 
var BaseWidget = require('base-widget');
var Slap = require('./Slap');
 
function BaseForm (opts) {
  var self = this;
 
  Iif (!(self instanceof BaseForm)) return new BaseForm(opts);
 
  BaseWidget.call(self, _.merge({
    hidden: true,
    height: 1,
    left: 0,
    right: 0,
    bottom: 0
  }, Slap.global.options.form, opts));
  Eif (self.parent instanceof Pane) {
    self.pane = self.parent;
    self.pane.forms.push(self);
  }
}
BaseForm.prototype.__proto__ = BaseWidget.prototype;
 
BaseForm.prototype.cancel = function () { this.emit('cancel'); };
BaseForm.prototype.submit = function () { this.emit('submit'); };
BaseForm.prototype._initHandlers = function () {
  var self = this;
  self.on('element keypress', function (el, ch, key) {
    switch (self.resolveBinding(key)) {
      case 'cancel': self.cancel(); return false;
    };
  });
  self.on('show', function () { self.focus(); });
  self.on('hide', function () {
    self.screen.slap._stopKeyPropagation().done();
    if (self.screen.focused.hasAncestor(self.pane) && !self.screen.slap.focused.visible) self.pane.focus();
  });
  self.on('element blur', function (el) { if (self.visible && !self.hasFocus(true)) self.cancel(); });
  self.on('element submit', function (el) { if (el !== self) self.submit(); });
  self.on('element cancel', function (el) { if (el !== self) self.cancel(); });
  self.on('cancel', function () { self.hide(); });
 
  return BaseWidget.prototype._initHandlers.apply(self, arguments);
};
 
module.exports = BaseForm;
 
var Pane = require('./Pane'); // circular import