all files / lib/ui/ FindForm.js

37.88% Statements 25/66
5.56% Branches 1/18
28.57% Functions 2/7
38.46% Lines 25/65
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                                                                                                                                            
var _ = require('lodash');
var lodash = require('lodash');
 
var util = require('slap-util');
 
var BaseWidget = require('base-widget');
var Slap = require('./Slap');
var BaseFindForm = require('./BaseFindForm');
 
FindForm._label = " find (/.*/ for regex): ";
FindForm._regExpRegExp = /^\/(.+)\/(\w*)$/i;
FindForm._invalidRegExpMessageRegExp = /^(Invalid regular expression:|Invalid flags supplied to RegExp constructor)/;
function FindForm (opts) {
  var self = this;
 
  Iif (!(self instanceof FindForm)) return new FindForm(opts);
 
  BaseFindForm.call(self, _.merge({
    findField: {left: FindForm._label.length}
  }, Slap.global.options.form.find, opts));
 
  self.findLabel = new BaseWidget(_.merge({
    parent: self,
    tags: true,
    content: FindForm._label,
    top: 0,
    height: 1,
    left: 0,
    width: FindForm._label.length,
    style: self.options.style
  }, self.options.findLabel));
}
FindForm.prototype.__proto__ = BaseFindForm.prototype;
 
FindForm.prototype.selectRange = function (range) {
  var self = this;
  var editor = self.pane.editor;
  var selection = editor.selection;
  selection.setRange(range);
  var visibleRange = editor.visiblePos(range);
  editor.clipScroll([visibleRange.start, visibleRange.end]);
  return self;
};
FindForm.prototype._initHandlers = function () {
  var self = this;
  var header = self.screen.slap.header;
  var editor = self.pane.editor;
  var selection = editor.selection;
 
  self.on('hide', function () {
    editor.destroyMarkers({type: 'findMatch'});
    editor._updateContent();
  });
  self.on('find', lodash.throttle(function (pattern, direction) {
    direction = direction || 0;
    editor.destroyMarkers({type: 'findMatch'});
    try {
      var regExpMatch = pattern.match(FindForm._regExpRegExp);
      pattern = new RegExp(regExpMatch[1], regExpMatch[2].replace('g', '') + 'g');
    } catch (e) {
      if (e.message.match(FindForm._invalidRegExpMessageRegExp)) {
        header.message(e.message, 'error');
        self.resetEditor();
        return;
      }
      pattern = new RegExp(_.escapeRegExp(pattern), 'img');
    }
 
    var selectionRange = selection.getRange();
    var matches = [];
    editor.textBuf[direction === -1
      ? 'backwardsScan'
      : 'scan'](pattern, function (match) {
      matches.push(match);
      editor.textBuf.markRange(match.range, {type: 'findMatch'});
    });
 
    if (!matches.length) {
      header.message("no matches", 'warning');
      self.resetEditor();
      return;
    }
    if (!matches.some(function (match) {
      var matchRange = match.range;
      var cmp = matchRange.start.compare(selectionRange.start);
      if (cmp === direction) {
        self.selectRange(matchRange);
        return true;
      } else if (!cmp && matches.length === 1) {
        header.message("this is the only occurrence", 'info');
        return true;
      }
    })) {
      header.message("search wrapped", 'info');
      self.selectRange(matches[0].range);
    }
    editor._updateContent();
  }, self.options.perf.findThrottle));
  return BaseFindForm.prototype._initHandlers.apply(self, arguments);
};
 
module.exports = FindForm;