Code coverage report for src/components/unsaved-state.cjsx

Statements: 43.59% (17 / 39)      Branches: 35.71% (5 / 14)      Functions: 43.75% (7 / 16)      Lines: 43.59% (17 / 39)      Ignored: none     

All files » src/components/ » unsaved-state.cjsx
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                 62×     62×                                                                                                                            
var ACTIVE, Promise, React, TransitionAssistant, TutorDialog, UnsavedStateMixin, _, moment;
 
ACTIVE = [];
 
_ = require('underscore');
 
Promise = require('es6-promise').Promise;
 
moment = require('moment');
 
React = require('react');
 
TutorDialog = require('./tutor-dialog');
 
UnsavedStateMixin = {
  componentWillMount: function() {
    return ACTIVE.push(this);
  },
  componentWillUnmount: function() {
    return ACTIVE.splice(ACTIVE.indexOf(this), 1);
  },
  _cannotTransition: function() {
    return typeof this.hasUnsavedState === "function" ? this.hasUnsavedState() : void 0;
  },
  _unsavedMessages: function() {
    return (typeof this.unsavedStateMessages === "function" ? this.unsavedStateMessages() : void 0) || [this.constructor.displayName + " has unsaved data"];
  }
};
 
TransitionAssistant = {
  canTransition: function() {
    return !_.any(ACTIVE, function(c) {
      return c._cannotTransition();
    });
  },
  unsavedMessages: function() {
    return _.flatten(_.invoke(ACTIVE, '_unsavedMessages'), 1);
  },
  checkTransitionStateTo: function(destination) {
    return new Promise((function(_this) {
      return function(onOk, onCancel) {
        var body, i, message;
        if (_this.canTransition()) {
          return onOk();
        } else {
          body = React.createElement("div", null, (function() {
            var j, len, ref, results;
            ref = this.unsavedMessages();
            results = [];
            for (i = j = 0, len = ref.length; j < len; i = ++j) {
              message = ref[i];
              results.push(React.createElement("p", {
                "key": i
              }, message));
            }
            return results;
          }).call(_this));
          return TutorDialog.show({
            title: "Proceed to " + destination + " ?",
            body: body
          }).then(function() {
            _this.lastCancel = moment();
            return onOk();
          }, onCancel);
        }
      };
    })(this));
  },
  wasJustApproved: function() {
    return this.lastCancel && this.lastCancel.isBefore(moment().add(1, 'second'));
  },
  startMonitoring: function() {
    delete this.startMonitoring;
    return window.onbeforeunload = (function(_this) {
      return function() {
        if (!(_this.canTransition() || _this.wasJustApproved())) {
          return _this.unsavedMessages().join("\n");
        }
      };
    })(this);
  }
};
 
module.exports = {
  UnsavedStateMixin: UnsavedStateMixin,
  TransitionAssistant: TransitionAssistant
};