API Docs for: 0.50.0
Show:

File: src/Context.js

  1. /*
  2. Copyright 2012 Rustici Software
  3.  
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7.  
  8. http://www.apache.org/licenses/LICENSE-2.0
  9.  
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. */
  16.  
  17. /**
  18. TinCan client library
  19.  
  20. @module TinCan
  21. @submodule TinCan.Context
  22. **/
  23. (function () {
  24. "use strict";
  25.  
  26. /**
  27. @class TinCan.Context
  28. @constructor
  29. */
  30. var Context = TinCan.Context = function (cfg) {
  31. this.log("constructor");
  32.  
  33. /**
  34. @property registration
  35. @type String|null
  36. */
  37. this.registration = null;
  38.  
  39. /**
  40. @property instructor
  41. @type TinCan.Agent|TinCan.Group|null
  42. */
  43. this.instructor = null;
  44.  
  45. /**
  46. @property team
  47. @type TinCan.Agent|TinCan.Group|null
  48. */
  49. this.team = null;
  50.  
  51. /**
  52. @property contextActivities
  53. @type ContextActivities|null
  54. */
  55. this.contextActivities = null;
  56.  
  57. /**
  58. @property revision
  59. @type String|null
  60. */
  61. this.revision = null;
  62.  
  63. /**
  64. @property platform
  65. @type Object|null
  66. */
  67. this.platform = null;
  68.  
  69. /**
  70. @property language
  71. @type String|null
  72. */
  73. this.language = null;
  74.  
  75. /**
  76. @property statement
  77. @type StatementRef|null
  78. */
  79. this.statement = null;
  80.  
  81. /**
  82. @property extensions
  83. @type String
  84. */
  85. this.extensions = null;
  86.  
  87. this.init(cfg);
  88. };
  89. Context.prototype = {
  90. /**
  91. @property LOG_SRC
  92. */
  93. LOG_SRC: "Context",
  94.  
  95. /**
  96. @method log
  97. */
  98. log: TinCan.prototype.log,
  99.  
  100. /**
  101. @method init
  102. @param {Object} [options] Configuration used to initialize
  103. */
  104. init: function (cfg) {
  105. this.log("init");
  106.  
  107. var i,
  108. directProps = [
  109. "registration",
  110. "revision",
  111. "platform",
  112. "language",
  113. "extensions"
  114. ],
  115. agentGroupProps = [
  116. "instructor",
  117. "team"
  118. ],
  119. prop,
  120. val
  121. ;
  122.  
  123. cfg = cfg || {};
  124.  
  125. for (i = 0; i < directProps.length; i += 1) {
  126. prop = directProps[i];
  127. if (cfg.hasOwnProperty(prop) && cfg[prop] !== null) {
  128. this[prop] = cfg[prop];
  129. }
  130. }
  131. for (i = 0; i < agentGroupProps.length; i += 1) {
  132. prop = agentGroupProps[i];
  133. if (cfg.hasOwnProperty(prop) && cfg[prop] !== null) {
  134. val = cfg[prop];
  135.  
  136. if (typeof val.objectType === "undefined" || val.objectType === "Person") {
  137. val.objectType = "Agent";
  138. }
  139.  
  140. if (val.objectType === "Agent" && ! (val instanceof TinCan.Agent)) {
  141. val = new TinCan.Agent (val);
  142. } else if (val.objectType === "Group" && ! (val instanceof TinCan.Group)) {
  143. val = new TinCan.Group (val);
  144. }
  145.  
  146. this[prop] = val;
  147. }
  148. }
  149.  
  150. if (cfg.hasOwnProperty("contextActivities") && cfg.contextActivities !== null) {
  151. if (cfg.contextActivities instanceof TinCan.ContextActivities) {
  152. this.contextActivities = cfg.contextActivities;
  153. }
  154. else {
  155. this.contextActivities = new TinCan.ContextActivities(cfg.contextActivities);
  156. }
  157. }
  158.  
  159. if (cfg.hasOwnProperty("statement") && cfg.statement !== null) {
  160. if (cfg.statement instanceof TinCan.StatementRef) {
  161. this.statement = cfg.statement;
  162. }
  163. else if (cfg.statement instanceof TinCan.SubStatement) {
  164. this.statement = cfg.statement;
  165. }
  166. else if (cfg.statement.objectType === "StatementRef") {
  167. this.statement = new TinCan.StatementRef(cfg.statement);
  168. }
  169. else if (cfg.statement.objectType === "SubStatement") {
  170. this.statement = new TinCan.SubStatement(cfg.statement);
  171. }
  172. else {
  173. this.log("Unable to parse statement.context.statement property.");
  174. }
  175. }
  176. },
  177.  
  178. /**
  179. @method asVersion
  180. @param {String} [version] Version to return (defaults to newest supported)
  181. */
  182. asVersion: function (version) {
  183. this.log("asVersion");
  184. var result = {},
  185. optionalDirectProps = [
  186. "registration",
  187. "revision",
  188. "platform",
  189. "language",
  190. "extensions"
  191. ],
  192. optionalObjProps = [
  193. "instructor",
  194. "team",
  195. "contextActivities",
  196. "statement"
  197. ],
  198. i;
  199.  
  200. version = version || TinCan.versions()[0];
  201.  
  202. if (this.statement instanceof TinCan.SubStatement && version !== "0.9" && version !== "0.95") {
  203. this.log("[error] version does not support SubStatements in the 'statement' property: " + version);
  204. throw new Error(version + " does not support SubStatements in the 'statement' property");
  205. }
  206.  
  207. for (i = 0; i < optionalDirectProps.length; i += 1) {
  208. if (this[optionalDirectProps[i]] !== null) {
  209. result[optionalDirectProps[i]] = this[optionalDirectProps[i]];
  210. }
  211. }
  212. for (i = 0; i < optionalObjProps.length; i += 1) {
  213. if (this[optionalObjProps[i]] !== null) {
  214. result[optionalObjProps[i]] = this[optionalObjProps[i]].asVersion(version);
  215. }
  216. }
  217.  
  218. return result;
  219. }
  220. };
  221.  
  222. /**
  223. @method fromJSON
  224. @return {Object} Context
  225. @static
  226. */
  227. Context.fromJSON = function (contextJSON) {
  228. Context.prototype.log("fromJSON");
  229. var _context = JSON.parse(contextJSON);
  230.  
  231. return new Context(_context);
  232. };
  233. }());
  234.