all files / app/components/editInProgress/ editInProgress.service.js

100% Statements 45/45
100% Branches 7/7
100% Functions 13/13
100% Lines 45/45
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147                                  245×   245× 245× 245× 245×                         98× 98× 98× 20×     20×         12× 12×       78×   98×   98×             98× 98×     20× 20×   12×       86× 86× 86× 86×                             91×                             28×                           91×     210× 210×     245× 245× 245×        
/**
 * @ngdoc service
 * @name app.components.editInProgress
 * @description
 * Prompt the user to cancel the edit in progress.  Notify controllers of the
 * outcome.
 *
 * An edit is in progress when the user has chosen a command to show a data entry form, and
 * has changed the value of a form field.
 */
(function () {
  'use strict';
 
  angular
    .module('app.components')
    .service('editInProgress', Service);
 
  /** @ngInject */
  function Service($q, modalConfirm, $rootScope, autoFocus) {
 
    var service = this;
 
    service.closeEditors = closeEditors;
    service.registerOnQueryState = registerOnQueryState;
    service.registerOnConfirmed = registerOnConfirmed;
    service.registerOnClose = registerOnClose;
 
    /**
     * @ngdoc function
     * @name closeEditors
     * @methodOf app.components.editInProgress
     * @description
     * If an edit is in progress, display a modal prompt to cancel the edit.
     *
     * @returns {Object} promise
     * * Resolved when the edit is cancelled or when there is no edit in progress
     * * Reject if the edit is not cancelled (by the user)
     */
    function closeEditors() {
      var state = emitQuery();
      var promise;
      if (!state.pristine) {
        angular.merge(state.labels, {
          cancel: 'No'
        });
        promise = modalConfirm.confirm(state.labels).then(
          function () {
            return emitClose(state).then(function () {
              emitConfirmed(state, true);
            });
          },
          function () {
            emitConfirmed(state, false);
            return $q.reject();
          });
      }
      else
        promise = emitClose(state);
 
      return promise;
 
      function emitQuery() {
        var data = {
          pristine: true,
          labels: {
            title: 'Discard Changes',
            message: 'Do you want to discard changes on this page?'
          }
        };
        $rootScope.$emit(QUERY_EVENT, data);
        return data;
      }
 
      function emitConfirmed(state, resolved) {
        $rootScope.$emit(CONFIRMED_EVENT, state, resolved);
        if (!resolved && state.autoFocus && state.autoFocusScope) {
          // Restore focus
          autoFocus(state.autoFocusScope, state.autoFocus);
        }
      }
 
      function emitClose(state) {
        var deferred = [];
        $rootScope.$emit(CLOSE_EVENT, state, deferred);
        var all = $q.all(deferred);
        return all;
      }
    }
 
    /**
     * @ngdoc function
     * @name registerOnQueryState
     * @methodOf app.components.editInProgress
     * @description
     * Register a controller callback to query for the editing state.
     * @param {Object} scope
     * Controller scope
     * @param {Object} callback
     * Controller callback
     */
    function registerOnQueryState(scope, callback) {
      registerEvent(scope, QUERY_EVENT, callback);
    }
 
    /**
     * @ngdoc function
     * @name registerOnConfirmed
     * @methodOf app.components.editInProgress
     * @description
     * Register a callback to inform controllers that the user was prompted to
     * close the current editor
     * @param {Object} scope
     * Controller scope
     * @param {Object} callback
     * Controller callback
     */
    function registerOnConfirmed(scope, callback) {
      registerEvent(scope, CONFIRMED_EVENT, callback);
    }
 
    /**
     * @ngdoc function
     * @name registerOnClose
     * @methodOf app.components.editInProgress
     * @description
     * Register a callback to inform controllers that editors should be closed.
     * @param {Object} scope
     * Controller scope
     * @param {Object} callback
     * Controller callback
     */
    function registerOnClose(scope, callback) {
      registerEvent(scope, CLOSE_EVENT, callback);
    }
 
    function registerEvent(scope, name, callBack) {
      var on = $rootScope.$on(name, callBack);
      scope.$on('$destroy', on);
    }
 
    var QUERY_EVENT = 'editing-in-progress:query';
    var CONFIRMED_EVENT = 'editing-in-progress:confirmed';
    var CLOSE_EVENT = 'editing-in-progress:close';
  }
 
})();