File: src/ContextActivities.js
/*
Copyright 2013 Rustici Software
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
TinCan client library
@module TinCan
@submodule TinCan.ContextActivities
**/
(function () {
"use strict";
/**
@class TinCan.ContextActivities
@constructor
*/
var ContextActivities = TinCan.ContextActivities = function (cfg) {
this.log("constructor");
/**
@property category
@type Array
*/
this.category = null;
/**
@property parent
@type Array
*/
this.parent = null;
/**
@property grouping
@type Array
*/
this.grouping = null;
/**
@property other
@type Array
*/
this.other = null;
this.init(cfg);
};
ContextActivities.prototype = {
/**
@property LOG_SRC
*/
LOG_SRC: "ContextActivities",
/**
@method log
*/
log: TinCan.prototype.log,
/**
@method init
@param {Object} [options] Configuration used to initialize
*/
init: function (cfg) {
this.log("init");
var i,
j,
objProps = [
"category",
"parent",
"grouping",
"other"
],
prop,
val
;
cfg = cfg || {};
for (i = 0; i < objProps.length; i += 1) {
prop = objProps[i];
if (cfg.hasOwnProperty(prop) && cfg[prop] !== null) {
if (Object.prototype.toString.call(cfg[prop]) === "[object Array]") {
for (j = 0; j < cfg[prop].length; j += 1) {
this.add(prop, cfg[prop][j]);
}
}
else {
val = cfg[prop];
this.add(prop, val);
}
}
}
},
/**
@method add
@param String key Property to add value to one of "category", "parent", "grouping", "other"
@return Number index where the value was added
*/
add: function (key, val) {
if (key !== "category" && key !== "parent" && key !== "grouping" && key !== "other") {
return;
}
if (this[key] === null) {
this[key] = [];
}
if (! (val instanceof TinCan.Activity)) {
val = typeof val === "string" ? { id: val } : val;
val = new TinCan.Activity (val);
}
this[key].push(val);
return this[key].length - 1;
},
/**
@method asVersion
@param {String} [version] Version to return (defaults to newest supported)
*/
asVersion: function (version) {
this.log("asVersion");
var result = {},
optionalObjProps = [
"parent",
"grouping",
"other"
],
i,
j;
version = version || TinCan.versions()[0];
for (i = 0; i < optionalObjProps.length; i += 1) {
if (this[optionalObjProps[i]] !== null && this[optionalObjProps[i]].length > 0) {
if (version === "0.9" || version === "0.95") {
if (this[optionalObjProps[i]].length > 1) {
// TODO: exception?
this.log("[warning] version does not support multiple values in: " + optionalObjProps[i]);
}
result[optionalObjProps[i]] = this[optionalObjProps[i]][0].asVersion(version);
}
else {
result[optionalObjProps[i]] = [];
for (j = 0; j < this[optionalObjProps[i]].length; j += 1) {
result[optionalObjProps[i]].push(
this[optionalObjProps[i]][j].asVersion(version)
);
}
}
}
}
if (this.category !== null && this.category.length > 0) {
if (version === "0.9" || version === "0.95") {
this.log("[error] version does not support the 'category' property: " + version);
throw new Error(version + " does not support the 'category' property");
}
else {
result.category = [];
for (i = 0; i < this.category.length; i += 1) {
result.category.push(this.category[i].asVersion(version));
}
}
}
return result;
}
};
/**
@method fromJSON
@return {Object} ContextActivities
@static
*/
ContextActivities.fromJSON = function (contextActivitiesJSON) {
ContextActivities.prototype.log("fromJSON");
var _contextActivities = JSON.parse(contextActivitiesJSON);
return new ContextActivities(_contextActivities);
};
}());