Code coverage report for src/flux/helpers.coffee

Statements: 83.87% (104 / 124)      Branches: 67.86% (38 / 56)      Functions: 88.24% (30 / 34)      Lines: 83.87% (104 / 124)      Ignored: none     

All files » src/flux/ » helpers.coffee
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239                         793×     49×             288× 288× 288× 288× 288× 288× 39×   288×                   190× 106×   84× 84× 84×     737× 737× 737×                   737× 737×   737×         49× 49× 49× 49× 46×   49× 49× 49× 49×         49× 49× 49×                               221× 221× 121×   221× 221×     94×                         188×     52264× 52264× 52264× 2856×   49408× 412×   48996×         73×     571×     939×         1342×     323×           45310×         77×         792×               59× 59× 1327×   59× 59× 59× 59× 59×           51×   51× 51× 51×                
var CREATE_KEY, CrudConfig, DELETED, DELETING, FAILED, LOADED, LOADING, SAVING, _, extendConfig, flux, idCounter, isNew, makeSimpleStore;
 
_ = require('underscore');
 
flux = require('flux-react');
 
LOADING = 'loading';
 
LOADED = 'loaded';
 
FAILED = 'failed';
 
SAVING = 'saving';
 
DELETING = 'deleting';
 
DELETED = 'deleted';
 
idCounter = 0;
 
CREATE_KEY = function() {
  return "_CREATING_" + (idCounter++);
};
 
isNew = function(id) {
  return /_CREATING_/.test(id);
};
 
CrudConfig = function() {
  return {
    _asyncStatus: {},
    _local: {},
    _changed: {},
    _errors: {},
    _reload: {},
    reset: function() {
      this._asyncStatus = {};
      this._local = {};
      this._changed = {};
      this._errors = {};
      this._reload = {};
      if (typeof this._reset === "function") {
        this._reset();
      }
      return this.emitChange();
    },
    FAILED: function(status, msg, id) {
      this._asyncStatus[id] = FAILED;
      this._errors[id] = msg;
      if (typeof this._failed === "function") {
        this._failed({
          status: status,
          msg: msg
        }, id);
      }
      Eif (status !== 0) {
        delete this._local[id];
        return this.emitChange();
      }
    },
    load: function(id) {
      if (this._asyncStatus[id] === LOADED && this._HACK_DO_NOT_RELOAD) {
        return;
      }
      this._reload[id] = false;
      this._asyncStatus[id] = LOADING;
      return this.emitChange();
    },
    loaded: function(obj, id) {
      var i, len, ref, ref1, ref2, ref3, step;
      this._asyncStatus[id] = LOADED;
      Iif (this._local[id] && obj.HACK_LOCAL_STEP_COMPLETION && this._local[id].steps) {
        ref = this._local[id].steps;
        for (i = 0, len = ref.length; i < len; i++) {
          step = ref[i];
          if (step.is_completed && (((ref1 = step.content) != null ? (ref2 = ref1.questions) != null ? (ref3 = ref2[0]) != null ? ref3.answers[0] : void 0 : void 0 : void 0) != null) && !step.correct_answer_id) {
            step.correct_answer_id = step.content.questions[0].answers[0].id;
            step.feedback_html = 'Some <em>FAKE</em> feedback';
          }
        }
      }
      Eif (obj) {
        this._local[id] = (typeof this._loaded === "function" ? this._loaded(obj, id) : void 0) || obj;
      }
      return this.emitChange();
    },
    save: function(id, obj) {
      this._asyncStatus[id] = SAVING;
      return this.emitChange();
    },
    saved: function(result, id) {
      var obj;
      this._asyncStatus[id] = LOADED;
      obj = typeof this._saved === "function" ? this._saved(result, id) : void 0;
      if (obj) {
        result = obj;
      }
      Eif (result) {
        this._local[id] = result;
        this._local[result.id] = result;
        delete this._changed[result.id];
      } else {
        console.warn('API WARN: Server did not return JSON after saving. Patching locally');
        this._local[id] = _.extend(this._local[id], this._changed[id]);
      }
      delete this._changed[id];
      delete this._errors[id];
      return this.emitChange();
    },
    create: function(localId, attributes) {
      Iif (attributes == null) {
        attributes = {};
      }
      Iif (!isNew(localId)) {
        throw new Error('BUG: MUST provide a local id');
      }
      this._local[localId] = {};
      this._changed[localId] = attributes;
      return this._asyncStatus[localId] = LOADED;
    },
    created: function(result, localId) {
      this._local[localId] = result;
      this._local[result.id] = result;
      this._asyncStatus[localId] = LOADED;
      this._asyncStatus[result.id] = LOADED;
      return this.emitChange();
    },
    _change: function(id, obj) {
      var base;
      if ((base = this._changed)[id] == null) {
        base[id] = {};
      }
      _.extend(this._changed[id], obj);
      return this.emitChange();
    },
    _save: function(id) {
      return this._asyncStatus[id] = SAVING;
    },
    "delete": function(id) {
      return this._asyncStatus[id] = DELETING;
    },
    deleted: function(result, id) {
      this._asyncStatus[id] = DELETED;
      delete this._local[id];
      return this.emitChange();
    },
    clearChanged: function(id) {
      return delete this._changed[id];
    },
    HACK_DO_NOT_RELOAD: function(bool) {
      return this._HACK_DO_NOT_RELOAD = bool;
    },
    _get: function(id) {
      var val;
      val = this._local[id];
      if (!(val || this._asyncStatus[id] === SAVING)) {
        return null;
      }
      if (_.isArray(val)) {
        return val;
      } else {
        return _.extend({}, val, this._changed[id]);
      }
    },
    exports: {
      isUnknown: function(id) {
        return !this._asyncStatus[id];
      },
      isLoading: function(id) {
        return this._asyncStatus[id] === LOADING;
      },
      isLoaded: function(id) {
        return this._asyncStatus[id] === LOADED;
      },
      isDeleting: function(id) {
        return this._asyncStatus[id] === DELETING;
      },
      isSaving: function(id) {
        return this._asyncStatus[id] === SAVING;
      },
      isFailed: function(id) {
        return this._asyncStatus[id] === FAILED;
      },
      getAsyncStatus: function(id) {
        return this._asyncStatus[id];
      },
      get: function(id) {
        return this._get(id);
      },
      isChanged: function(id) {
        return !_.isEmpty(this._changed[id]);
      },
      getChanged: function(id) {
        return this._changed[id] || {};
      },
      freshLocalId: function() {
        return CREATE_KEY();
      },
      isNew: function(id) {
        return isNew(id);
      },
      reload: function(id) {
        return this._reload[id];
      }
    }
  };
};
 
makeSimpleStore = function(storeConfig) {
  var actions, actionsConfig, store;
  actionsConfig = _.omit(storeConfig, function(value, key) {
    return typeof value !== 'function' || key === 'exports';
  });
  actionsConfig = _.keys(actionsConfig);
  actions = flux.createActions(actionsConfig);
  storeConfig.actions = _.values(actions);
  store = flux.createStore(storeConfig);
  return {
    actions: actions,
    store: store
  };
};
 
extendConfig = function(newConfig, origConfig) {
  if (newConfig.exports == null) {
    newConfig.exports = {};
  }
  _.defaults(newConfig, origConfig);
  _.defaults(newConfig.exports, origConfig.exports);
  return newConfig;
};
 
module.exports = {
  CrudConfig: CrudConfig,
  makeSimpleStore: makeSimpleStore,
  extendConfig: extendConfig
};