Code coverage report for slap/lib/ui/BaseFindForm.js

Statements: 91.07% (51 / 56)      Branches: 50% (11 / 22)      Functions: 100% (8 / 8)      Lines: 95.65% (44 / 46)      Ignored: none     

All files » slap/lib/ui/ » BaseFindForm.js
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 771 1   1 1 1   1   1 2   2   2             2             1   1 5 5 5 1 5   1 3 3 3 3 3     1 2 2 2 2 1 1 1 1 1   2 1 1 1       4 2 15 15           2     1  
var blessed = require('blessed');
var _ = require('lazy.js');
 
var Slap = require('./Slap');
var BaseForm = require('./BaseForm');
var Field = require('./Field');
 
var util = require('../util');
 
function BaseFindForm (opts) {
  var self = this;
 
  Iif (!(self instanceof blessed.Node)) return new BaseFindForm(opts);
 
  BaseForm.call(self, _({
      prevEditorState: {}
    })
    .merge(Slap.global.options.form.baseFind || {})
    .merge(opts || {})
    .toObject());
 
  self.findField = new Field(_({
    parent: self,
    top: 0,
    left: 0,
    right: 0
  }).merge(self.options.findField || {}).toObject());
}
BaseFindForm.prototype.__proto__ = BaseForm.prototype;
 
BaseFindForm.prototype.find = function (text, direction) {
  var self = this;
  self.slap.header.message(null);
  if (text) self.emit('find', text, direction);
  else self.resetEditor();
  return self;
};
BaseFindForm.prototype.resetEditor = function () {
  var self = this;
  var prevEditorState = self.options.prevEditorState;
  var editor = self.pane.editor;
  Eif (prevEditorState.selection) editor.selection.setRange(prevEditorState.selection);
  Eif (prevEditorState.scroll) { editor.scroll = prevEditorState.scroll; editor._updateContent(); }
};
 
BaseFindForm.prototype._initHandlers = function () {
  var self = this;
  var textBuf = self.findField.textBuf;
  var prevEditorState = self.options.prevEditorState;
  self.on('show', function () {
    var editor = self.pane.editor;
    Eif (!prevEditorState.selection) prevEditorState.selection = editor.selection.getRange();
    Eif (!prevEditorState.scroll) prevEditorState.scroll = editor.scroll;
    self.findField.focus();
    self.find(textBuf.getText());
  });
  self.on('hide', function () {
    Eif (_(self.pane.forms).pluck('visible').compact().none()) {
      prevEditorState.selection = null;
      prevEditorState.scroll = null;
    }
  });
 
  textBuf.on('changed', function () { self.find(textBuf.getText()); });
  self.findField.on('keypress', function (ch, key) {
    var text = textBuf.getText();
    switch (util.getBinding(self.options.bindings, key)) {
      case 'next': self.find(text, 1); return false;
      case 'prev': self.find(text, -1); return false;
    };
  });
 
  return BaseForm.prototype._initHandlers.apply(self, arguments);
};
 
module.exports = BaseFindForm;