Code coverage report for api/locales/ContextualizableObject.js

Statements: 81.55% (84 / 103)      Branches: 66.67% (62 / 93)      Functions: 91.67% (11 / 12)      Lines: 81.55% (84 / 103)      Ignored: none     

All files » api/locales/ » ContextualizableObject.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 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 240 241 242 243 244 245 246 247 248 249 250 251 252 2531                         1         561 561 2         1           1 1 1     1 1 1     1             560 574     574     560     1   1 1                     218           2054   480   1574                                     5           94 94 94 87   94 10     10     94 94           5 5     5 5   5 5 5 5 5 5 5                                 5 2   5           575 575 575 94 94   575 5 5   575   575 575     575 2 2 2   2 2 2 2     575                       37         37 1   36     36 167 72 72 72     72             36                       36 3 3     36         1  
var FieldDBObject = require("./../FieldDBObject").FieldDBObject,
  Q = require("q");
 
/**
 * @class The ContextualizableObject allows the user to label data with grammatical tags
 *        i.e. passive, causative. This is useful for searches.
 *
 * @name  ContextualizableObject
 * @description The initialize function brings up a field in which the user
 *              can enter tags.@class Object of Datum validation states
 * @extends Object
 * @constructs
 */
var ContextualizableObject = function ContextualizableObject(json) {
  // if (!this._fieldDBtype) {
  //   this._fieldDBtype = "Activities";
  // }
  // this.debugMode = true;
  this.debug("Constructing ContextualizableObject ", json);
  if (json && typeof json === "string") {
    if (ContextualizableObject.compatibleWithSimpleStrings) {
      // Dont localize this, return what you received to be backward compatible
 
      // http://stackoverflow.com/questions/1978049/what-values-can-a-constructor-return-to-avoid-returning-this
      /* jshint ignore:start  */
      return new String(json);
      /* jshint ignore:end  */
 
      // Can also throw the string and catch it and use the original value...
      // throw json;
    }
    var stringAsKey = "locale_" + json.replace(/[^a-zA-Z0-9-]/g, "_");
    var value = json;
    json = {
      default: stringAsKey
    };
    this.originalString = value;
    this.data = this.data || {};
    this.data[stringAsKey] = {
      "message": value
    };
    this.add(stringAsKey, value);
 
    // if (this.contextualizer && this.contextualizer.contextualize(for_context) === for_context) {
    //   this.contextualizer.updateContextualization(for_context, locale_string)
    //   this.debug("added to contextualizer "+ this.contextualizer.contextualize(for_context));
    // }
  }
  for (var member in json) {
    Iif (!json.hasOwnProperty(member) || member === "contextualizer") {
      continue;
    }
    this.add(member, json[member]);
  }
 
  Object.apply(this, arguments);
};
 
var forcedebug = false;
 
ContextualizableObject.compatibleWithSimpleStrings = true;
ContextualizableObject.prototype = Object.create(Object.prototype, /** @lends ContextualizableObject.prototype */ {
  constructor: {
    value: ContextualizableObject
  },
 
  fieldDBtype: {
    value: "ContextualizableObject"
  },
 
  contextualizer: {
    get: function() {
      return FieldDBObject.prototype.contextualizer;
    }
  },
 
  debug: {
    value: function(message, message2, message3, message4) {
      if (FieldDBObject.application && FieldDBObject.application.contextualizer) {
        // console.log("using  FieldDBObject.application.contextualizer.debug " +  FieldDBObject.application.contextualizer.debugMode);
        return FieldDBObject.application.contextualizer.debug;
      } else {
        Iif (forcedebug) {
          console.log(this.fieldDBtype.toUpperCase() + "-DEBUG FORCED: " + message);
 
          if (message2) {
            console.log(message2);
          }
          if (message3) {
            console.log(message3);
          }
          if (message4) {
            console.log(message4);
          }
        }
      }
    }
  },
 
  todo: {
    value: function() {
      return FieldDBObject.prototype.todo.apply(this, arguments);
    }
  },
 
  contextualize: {
    value: function(locale_string) {
      this.debug("requesting contextualization of " + locale_string);
      var contextualizedString;
      if (this.contextualizer) {
        contextualizedString = this.contextualizer.contextualize(locale_string);
      }
      if (!contextualizedString || contextualizedString === locale_string) {
        Iif (this.data && this.data[locale_string]) {
          contextualizedString = this.data[locale_string].message;
        } else {
          contextualizedString = locale_string;
        }
      }
      this.debug("::" + contextualizedString + "::");
      return contextualizedString;
    }
  },
 
  updateContextualization: {
    value: function(for_context, locale_string) {
      this.debug("updateContextualization" + for_context);
      var updatePromiseOrSyncrhonousConfirmed,
        self = this;
 
      Eif (this.contextualizer) {
        this.debug(this.contextualizer.data);
 
        updatePromiseOrSyncrhonousConfirmed = this.contextualizer.updateContextualization(for_context, locale_string);
        Eif (updatePromiseOrSyncrhonousConfirmed !== true) {
          self.todo("Test async updatePromiseOrSyncrhonousConfirmed");
          Q.nextTick(function() {
            Eif (self.contextualizer) {
              var updated = self.contextualizer.contextualize(for_context);
              Iif ((!updated || updated === for_context) && self.data) {
                self.data[for_context] = self.data[for_context] || {
                  message: ""
                };
                self.data[for_context].message = locale_string;
              }
            } else {
              console.warn("This is strange, i lost my contextualizer...", self);
            }
          });
        }
      }
      // this.data = this.data || {};
      // this.data[for_context] = {
      //   message: locale_string
      // };
 
      if (this._default === for_context) {
        this.originalString = locale_string;
      }
      return updatePromiseOrSyncrhonousConfirmed;
    }
  },
 
  add: {
    value: function(for_context, locale_string) {
      var underscoreNotation = "_" + for_context;
      this[underscoreNotation] = locale_string;
      this.__defineGetter__(for_context, function() {
        this.debug("overidding getter");
        return this.contextualize(this[underscoreNotation]);
      });
      this.__defineSetter__(for_context, function(value) {
        this.debug("overidding setter " + underscoreNotation, value);
        this.updateContextualization(this[underscoreNotation], value);
      });
      this.debug("adding string to ContextualizableObject's own data " + for_context);
      //if there is no contextualizer, add this to the local data
      this.data = this.data || {};
      this.data[for_context] = {
        "message": locale_string
      };
      if (for_context.indexOf("locale_") === 0 || for_context.indexOf("localized_") === 0) {
        this.debug("intializing the data in this ContextualizableObject");
        this.debug(" for_context " + for_context);
        this.debug(" locale_string " + locale_string);
        // If the contextualizer doesnt have a value for this string, add it to the contextualizations... (this could introduce a lot of data into the localizations)
        Eif (this.contextualizer) {
          this.debug(" adding to contextualizer: " + for_context + " as " + locale_string);
          this.contextualizer.updateContextualization(for_context, locale_string);
          this.debug("added to contextualizer " + this.contextualizer.contextualize(for_context));
        }
      }
      this.debug("data", this.data);
    }
  },
 
  isEmpty: {
    value: function() {
      return FieldDBObject.prototype.isEmpty.apply(this, arguments);
    }
  },
 
  toJSON: {
    value: function(includeEvenEmptyAttributes, removeEmptyAttributes) {
      var json = {},
        aproperty,
        underscorelessProperty;
 
      // this.debugMode = true;
      if (!ContextualizableObject.compatibleWithSimpleStrings && this.originalString) {
        return this.originalString;
      } else {
        this.debug("Original string is not defined, returning an object. ", ContextualizableObject.compatibleWithSimpleStrings, this.originalString);
      }
 
      for (aproperty in this) {
        if (this.hasOwnProperty(aproperty) && typeof this[aproperty] !== "function" && aproperty !== "contextualizer" && aproperty.indexOf("_") === 0) {
          underscorelessProperty = aproperty.replace(/^_/, "");
          Eif (!removeEmptyAttributes || (removeEmptyAttributes && !this.isEmpty(aproperty))) {
            Iif (this[aproperty] && typeof this[aproperty].toJSON === "function") {
              json[underscorelessProperty] = this[aproperty].toJSON(includeEvenEmptyAttributes, removeEmptyAttributes);
            } else {
              json[underscorelessProperty] = this[aproperty];
            }
          }
        }
      }
 
      /* if the caller requests a complete object include the default for all defauls by calling get on them */
      Iif (includeEvenEmptyAttributes && this.INTERNAL_MODELS) {
        for (aproperty in this.INTERNAL_MODELS) {
          if (!json[aproperty] && this.INTERNAL_MODELS) {
            if (this.INTERNAL_MODELS[aproperty] && typeof this.INTERNAL_MODELS[aproperty] === "function" && typeof new this.INTERNAL_MODELS[aproperty]().toJSON === "function") {
              json[aproperty] = new this.INTERNAL_MODELS[aproperty]().toJSON(includeEvenEmptyAttributes, removeEmptyAttributes);
            } else {
              json[aproperty] = this.INTERNAL_MODELS[aproperty];
            }
          }
        }
      }
      // Preseve the original string in this mini-contextualizer if it was originally a string
      if (json.default && this.originalString) {
        json[json.default] = this.originalString;
        delete json.originalString;
      }
 
      return json;
    }
  }
 
});
exports.ContextualizableObject = ContextualizableObject;