all files / lib/ui/ BaseFindForm.js

42.86% Statements 24/56
6.25% Branches 1/16
25% Functions 2/8
52.17% Lines 24/46
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                                                                                                    
var _ = require('lodash');
 
var Slap = require('./Slap');
var BaseForm = require('./BaseForm');
var BaseWidget = require('base-widget');
var Field = require('editor-widget').Field;
 
var util = require('slap-util');
 
function BaseFindForm (opts) {
  var self = this;
 
  Iif (!(self instanceof BaseFindForm)) return new BaseFindForm(opts);
 
  BaseForm.call(self, _.merge({
    prevEditorState: {}
  }, Slap.global.options.form.baseFind, opts));
 
  self.findField = new Field(_.merge({
    parent: self,
    top: 0,
    left: 0,
    right: 0
  }, Slap.global.options.editor, Slap.global.options.field, self.options.findField));
}
BaseFindForm.prototype.__proto__ = BaseForm.prototype;
 
BaseFindForm.prototype.find = function (text, direction) {
  var self = this;
  self.screen.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;
  if (prevEditorState.selection) editor.selection.setRange(prevEditorState.selection);
  if (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;
    if (!prevEditorState.selection) prevEditorState.selection = editor.selection.getRange();
    if (!prevEditorState.scroll) prevEditorState.scroll = editor.scroll;
    self.findField.focus();
    self.find(textBuf.getText());
  });
  self.on('hide', function () {
    if (!_.some(self.pane.forms, 'visible')) {
      prevEditorState.selection = null;
      prevEditorState.scroll = null;
    }
  });
 
  textBuf.onDidChange(function () { self.find(textBuf.getText()); });
  self.findField.on('keypress', function (ch, key) {
    var text = textBuf.getText();
    switch (self.resolveBinding(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;