# Code coverage report for lib/ui/Pane.js

## Statements: 55.68% (49 / 88) Branches: 33.33% (17 / 51) Functions: 66.67% (6 / 9) Lines: 62.67% (47 / 75) Ignored: none

All files » lib/ui/ » Pane.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
    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
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    1231
    1

    1

    1

    1
    2

    2

    2

    2

    2

    2

    2
    2
    2
    2

    2

    1

    1
    1
    1
    1
    1
    1
    1
    1
    1

    1

    1

    1
    2
    2
    2

    2
    1
    1
    1
    4
    4

    1

    2

    1

    1
    1
    1
    1
    1
    1
     var blessed = require('blessed');
    var _ = require('lazy.js');

    var BaseElement = require('./BaseElement');

    var util = require('../util');

    function Pane (opts) {
      var self = this;

      Iif (!(self instanceof blessed.Node)) return new Pane(opts);

      BaseElement.call(self, _({
          top:    Slap.global.header.options.headerPosition === 'top'    ? 1 : 0,
          bottom: Slap.global.header.options.headerPosition === 'bottom' ? 1 : 0,
          left: 0,
          right: 0,
        })
        .merge(Slap.global.options.pane || {})
        .merge(opts || {})
        .toObject());
      self.left = self.slap.fileBrowser.visible ? self.slap.fileBrowser.width : 0;

      self.forms = self.forms || [];

      self.editor = self.options.editor || new Editor({
        parent: self,
        top: 0,
        left: 0,
        right: 0,
        bottom: 0
      });

      self.findForm = new FindForm({parent: self});
      self.goLineForm = new GoLineForm({parent: self});
      self.saveAsForm = new SaveAsForm({parent: self});
      self.saveAsCloseForm = new SaveAsCloseForm({parent: self});

      self.slap.panes.push(self);
    }
    Pane.prototype.__proto__ = BaseElement.prototype;

    Pane.prototype.setCurrent = function () {
      var self = this;
      var slap = self.slap;
      var panes = slap.panes;
      var paneIndex = panes.indexOf(self);
      Iif (paneIndex === -1) { paneIndex = panes.length; panes.push(self); }
      slap.data.currentPane = paneIndex;
      self.ready.then(function () { Eif (self.isAttached()) self.focus(); }).done();
      return self;
    };
    Pane.prototype.close = function () {
      var self = this;
      self.detach();

      var slap = self.slap;
      var paneIndex = slap.panes.indexOf(self);
      if (paneIndex !== -1) {
        slap.panes.splice(paneIndex, 1);
        if (slap.panes.length) {
          slap.panes[Math.max(paneIndex - 1, 0)].setCurrent();
        } else {
          slap.fileBrowser.focus();
        }
      }

      self.emit('close');
      return self;
    };
    Pane.prototype.requestClose = function () {
      var self = this;
      if (self.editor.textBuf.isModified()) {
        var parent = self.parent;
        var currentPaneSaveAsCloseForm = ((parent.panes || [])[parent.data.currentPane] || {}).saveAsCloseForm || {};
        if (currentPaneSaveAsCloseForm.visible) {
          currentPaneSaveAsCloseForm.once('hide', self.requestClose.bind(self));
        } else {
          self.setCurrent();
          self.saveAsCloseForm.show();
        }
        return false;
      } else {
        self.close();
        return true;
      }
    };

    Pane.prototype._initHandlers = function () {
      var self = this;
      var editor = self.editor;
      self.on('element keypress', function (el, ch, key) {
        switch (util.getBinding(self.options.bindings, key)) {
          case 'save': if (!editor.readOnly()) editor.textBuf.getPath() ? editor.save().done() : self.saveAsForm.show(); return false;
          case 'saveAs': if (!editor.readOnly()) self.saveAsForm.show(); return false;
          case 'close': self.requestClose(); return false;
          case 'find': self.findForm.show(); return false;
          case 'goLine': self.goLineForm.show(); return false;
        }
      });

      self.on('focus', function () {
        self.setFront();
        var formHasFocus = false;
        var visibleForms = self.forms.filter(function (form) {
          formHasFocus = formHasFocus || form.hasFocus(true);
          return form.visible;
        });
        Iif (!formHasFocus && visibleForms.length) visibleForms[0].focus();
      });

      return BaseElement.prototype._initHandlers.apply(self, arguments);
    };

    module.exports = Pane;

    var Slap = require('./Slap');
    var Editor = require('./Editor');
    var SaveAsForm = require('./SaveAsForm');
    var SaveAsCloseForm = require('./SaveAsCloseForm');
    var FindForm = require('./FindForm');
    var GoLineForm = require('./GoLineForm');