Browse Source

Renamed DataSet option fieldTypes to convert, and renamed util.cast to util.convert

css_transitions
josdejong 11 years ago
parent
commit
8643fdcc91
13 changed files with 137 additions and 137 deletions
  1. +41
    -41
      src/DataSet.js
  2. +1
    -1
      src/DataView.js
  3. +3
    -3
      src/graph/dotparser.js
  4. +2
    -2
      src/timeline/Range.js
  5. +1
    -1
      src/timeline/Timeline.js
  6. +1
    -1
      src/timeline/component/GroupSet.js
  7. +2
    -2
      src/timeline/component/TimeAxis.js
  8. +12
    -12
      src/util.js
  9. +1
    -1
      test/dataset.html
  10. +4
    -4
      test/dataset.js
  11. +1
    -1
      test/timeline.html
  12. +63
    -63
      vis.js
  13. +5
    -5
      vis.min.js

+ 41
- 41
src/DataSet.js View File

@ -4,7 +4,7 @@
* Usage: * Usage:
* var dataSet = new DataSet({ * var dataSet = new DataSet({
* fieldId: '_id', * fieldId: '_id',
* fieldTypes: {
* convert: {
* // ... * // ...
* } * }
* }); * });
@ -29,7 +29,7 @@
* @param {Object} [options] Available options: * @param {Object} [options] Available options:
* {String} fieldId Field name of the id in the * {String} fieldId Field name of the id in the
* items, 'id' by default. * items, 'id' by default.
* {Object.<String, String} fieldTypes
* {Object.<String, String} convert
* A map with field names as key, * A map with field names as key,
* and the field type as value. * and the field type as value.
* @constructor DataSet * @constructor DataSet
@ -41,17 +41,17 @@ function DataSet (options) {
this.options = options || {}; this.options = options || {};
this.data = {}; // map with data indexed by id this.data = {}; // map with data indexed by id
this.fieldId = this.options.fieldId || 'id'; // name of the field containing id this.fieldId = this.options.fieldId || 'id'; // name of the field containing id
this.fieldTypes = {}; // field types by field name
this.convert = {}; // field types by field name
if (this.options.fieldTypes) {
for (var field in this.options.fieldTypes) {
if (this.options.fieldTypes.hasOwnProperty(field)) {
var value = this.options.fieldTypes[field];
if (this.options.convert) {
for (var field in this.options.convert) {
if (this.options.convert.hasOwnProperty(field)) {
var value = this.options.convert[field];
if (value == 'Date' || value == 'ISODate' || value == 'ASPDate') { if (value == 'Date' || value == 'ISODate' || value == 'ASPDate') {
this.fieldTypes[field] = 'Date';
this.convert[field] = 'Date';
} }
else { else {
this.fieldTypes[field] = value;
this.convert[field] = value;
} }
} }
} }
@ -262,7 +262,7 @@ DataSet.prototype.update = function (data, senderId) {
* {Object} options An Object with options. Available options: * {Object} options An Object with options. Available options:
* {String} [type] Type of data to be returned. Can * {String} [type] Type of data to be returned. Can
* be 'DataTable' or 'Array' (default) * be 'DataTable' or 'Array' (default)
* {Object.<String, String>} [fieldTypes]
* {Object.<String, String>} [convert]
* {String[]} [fields] field names to be returned * {String[]} [fields] field names to be returned
* {function} [filter] filter items * {function} [filter] filter items
* {String | function} [order] Order the items by * {String | function} [order] Order the items by
@ -319,14 +319,14 @@ DataSet.prototype.get = function (args) {
} }
// build options // build options
var fieldTypes = options && options.fieldTypes || this.options.fieldTypes;
var convert = options && options.convert || this.options.convert;
var filter = options && options.filter; var filter = options && options.filter;
var items = [], item, itemId, i, len; var items = [], item, itemId, i, len;
// cast items
// convert items
if (id != undefined) { if (id != undefined) {
// return a single item // return a single item
item = me._getItem(id, fieldTypes);
item = me._getItem(id, convert);
if (filter && !filter(item)) { if (filter && !filter(item)) {
item = null; item = null;
} }
@ -334,7 +334,7 @@ DataSet.prototype.get = function (args) {
else if (ids != undefined) { else if (ids != undefined) {
// return a subset of items // return a subset of items
for (i = 0, len = ids.length; i < len; i++) { for (i = 0, len = ids.length; i < len; i++) {
item = me._getItem(ids[i], fieldTypes);
item = me._getItem(ids[i], convert);
if (!filter || filter(item)) { if (!filter || filter(item)) {
items.push(item); items.push(item);
} }
@ -344,7 +344,7 @@ DataSet.prototype.get = function (args) {
// return all items // return all items
for (itemId in this.data) { for (itemId in this.data) {
if (this.data.hasOwnProperty(itemId)) { if (this.data.hasOwnProperty(itemId)) {
item = me._getItem(itemId, fieldTypes);
item = me._getItem(itemId, convert);
if (!filter || filter(item)) { if (!filter || filter(item)) {
items.push(item); items.push(item);
} }
@ -420,7 +420,7 @@ DataSet.prototype.getIds = function (options) {
var data = this.data, var data = this.data,
filter = options && options.filter, filter = options && options.filter,
order = options && options.order, order = options && options.order,
fieldTypes = options && options.fieldTypes || this.options.fieldTypes,
convert = options && options.convert || this.options.convert,
i, i,
len, len,
id, id,
@ -435,7 +435,7 @@ DataSet.prototype.getIds = function (options) {
items = []; items = [];
for (id in data) { for (id in data) {
if (data.hasOwnProperty(id)) { if (data.hasOwnProperty(id)) {
item = this._getItem(id, fieldTypes);
item = this._getItem(id, convert);
if (filter(item)) { if (filter(item)) {
items.push(item); items.push(item);
} }
@ -452,7 +452,7 @@ DataSet.prototype.getIds = function (options) {
// create unordered list // create unordered list
for (id in data) { for (id in data) {
if (data.hasOwnProperty(id)) { if (data.hasOwnProperty(id)) {
item = this._getItem(id, fieldTypes);
item = this._getItem(id, convert);
if (filter(item)) { if (filter(item)) {
ids.push(item[this.fieldId]); ids.push(item[this.fieldId]);
} }
@ -496,7 +496,7 @@ DataSet.prototype.getIds = function (options) {
* The order of the items is not determined. * The order of the items is not determined.
* @param {function} callback * @param {function} callback
* @param {Object} [options] Available options: * @param {Object} [options] Available options:
* {Object.<String, String>} [fieldTypes]
* {Object.<String, String>} [convert]
* {String[]} [fields] filter fields * {String[]} [fields] filter fields
* {function} [filter] filter items * {function} [filter] filter items
* {String | function} [order] Order the items by * {String | function} [order] Order the items by
@ -504,7 +504,7 @@ DataSet.prototype.getIds = function (options) {
*/ */
DataSet.prototype.forEach = function (callback, options) { DataSet.prototype.forEach = function (callback, options) {
var filter = options && options.filter, var filter = options && options.filter,
fieldTypes = options && options.fieldTypes || this.options.fieldTypes,
convert = options && options.convert || this.options.convert,
data = this.data, data = this.data,
item, item,
id; id;
@ -523,7 +523,7 @@ DataSet.prototype.forEach = function (callback, options) {
// unordered // unordered
for (id in data) { for (id in data) {
if (data.hasOwnProperty(id)) { if (data.hasOwnProperty(id)) {
item = this._getItem(id, fieldTypes);
item = this._getItem(id, convert);
if (!filter || filter(item)) { if (!filter || filter(item)) {
callback(item, id); callback(item, id);
} }
@ -536,7 +536,7 @@ DataSet.prototype.forEach = function (callback, options) {
* Map every item in the dataset. * Map every item in the dataset.
* @param {function} callback * @param {function} callback
* @param {Object} [options] Available options: * @param {Object} [options] Available options:
* {Object.<String, String>} [fieldTypes]
* {Object.<String, String>} [convert]
* {String[]} [fields] filter fields * {String[]} [fields] filter fields
* {function} [filter] filter items * {function} [filter] filter items
* {String | function} [order] Order the items by * {String | function} [order] Order the items by
@ -545,15 +545,15 @@ DataSet.prototype.forEach = function (callback, options) {
*/ */
DataSet.prototype.map = function (callback, options) { DataSet.prototype.map = function (callback, options) {
var filter = options && options.filter, var filter = options && options.filter,
fieldTypes = options && options.fieldTypes || this.options.fieldTypes,
convert = options && options.convert || this.options.convert,
mappedItems = [], mappedItems = [],
data = this.data, data = this.data,
item; item;
// cast and filter items
// convert and filter items
for (var id in data) { for (var id in data) {
if (data.hasOwnProperty(id)) { if (data.hasOwnProperty(id)) {
item = this._getItem(id, fieldTypes);
item = this._getItem(id, convert);
if (!filter || filter(item)) { if (!filter || filter(item)) {
mappedItems.push(callback(item, id)); mappedItems.push(callback(item, id));
} }
@ -747,13 +747,13 @@ DataSet.prototype.min = function (field) {
DataSet.prototype.distinct = function (field) { DataSet.prototype.distinct = function (field) {
var data = this.data, var data = this.data,
values = [], values = [],
fieldType = this.options.fieldTypes[field],
fieldType = this.options.convert[field],
count = 0; count = 0;
for (var prop in data) { for (var prop in data) {
if (data.hasOwnProperty(prop)) { if (data.hasOwnProperty(prop)) {
var item = data[prop]; var item = data[prop];
var value = util.cast(item[field], fieldType);
var value = util.convert(item[field], fieldType);
var exists = false; var exists = false;
for (var i = 0; i < count; i++) { for (var i = 0; i < count; i++) {
if (values[i] == value) { if (values[i] == value) {
@ -797,8 +797,8 @@ DataSet.prototype._addItem = function (item) {
var d = {}; var d = {};
for (var field in item) { for (var field in item) {
if (item.hasOwnProperty(field)) { if (item.hasOwnProperty(field)) {
var type = this.fieldTypes[field]; // type may be undefined
d[field] = util.cast(item[field], type);
var fieldType = this.convert[field]; // type may be undefined
d[field] = util.convert(item[field], fieldType);
} }
} }
this.data[id] = d; this.data[id] = d;
@ -807,13 +807,13 @@ DataSet.prototype._addItem = function (item) {
}; };
/** /**
* Get an item. Fields can be casted to a specific type
* Get an item. Fields can be converted to a specific type
* @param {String} id * @param {String} id
* @param {Object.<String, String>} [fieldTypes] Cast field types
* @param {Object.<String, String>} [convert] field types to convert
* @return {Object | null} item * @return {Object | null} item
* @private * @private
*/ */
DataSet.prototype._getItem = function (id, fieldTypes) {
DataSet.prototype._getItem = function (id, convert) {
var field, value; var field, value;
// get the item from the dataset // get the item from the dataset
@ -822,35 +822,35 @@ DataSet.prototype._getItem = function (id, fieldTypes) {
return null; return null;
} }
// cast the items field types
var casted = {},
// convert the items field types
var converted = {},
fieldId = this.fieldId, fieldId = this.fieldId,
internalIds = this.internalIds; internalIds = this.internalIds;
if (fieldTypes) {
if (convert) {
for (field in raw) { for (field in raw) {
if (raw.hasOwnProperty(field)) { if (raw.hasOwnProperty(field)) {
value = raw[field]; value = raw[field];
// output all fields, except internal ids // output all fields, except internal ids
if ((field != fieldId) || !(value in internalIds)) { if ((field != fieldId) || !(value in internalIds)) {
casted[field] = util.cast(value, fieldTypes[field]);
converted[field] = util.convert(value, convert[field]);
} }
} }
} }
} }
else { else {
// no field types specified, no casting needed
// no field types specified, no converting needed
for (field in raw) { for (field in raw) {
if (raw.hasOwnProperty(field)) { if (raw.hasOwnProperty(field)) {
value = raw[field]; value = raw[field];
// output all fields, except internal ids // output all fields, except internal ids
if ((field != fieldId) || !(value in internalIds)) { if ((field != fieldId) || !(value in internalIds)) {
casted[field] = value;
converted[field] = value;
} }
} }
} }
} }
return casted;
return converted;
}; };
/** /**
@ -875,8 +875,8 @@ DataSet.prototype._updateItem = function (item) {
// merge with current item // merge with current item
for (var field in item) { for (var field in item) {
if (item.hasOwnProperty(field)) { if (item.hasOwnProperty(field)) {
var type = this.fieldTypes[field]; // type may be undefined
d[field] = util.cast(item[field], type);
var fieldType = this.convert[field]; // type may be undefined
d[field] = util.convert(item[field], fieldType);
} }
} }

+ 1
- 1
src/DataView.js View File

@ -96,7 +96,7 @@ DataView.prototype.setData = function (data) {
* {Object} options An Object with options. Available options: * {Object} options An Object with options. Available options:
* {String} [type] Type of data to be returned. Can * {String} [type] Type of data to be returned. Can
* be 'DataTable' or 'Array' (default) * be 'DataTable' or 'Array' (default)
* {Object.<String, String>} [fieldTypes]
* {Object.<String, String>} [convert]
* {String[]} [fields] field names to be returned * {String[]} [fields] field names to be returned
* {function} [filter] filter items * {function} [filter] filter items
* {String | function} [order] Order the items by * {String | function} [order] Order the items by

+ 3
- 3
src/graph/dotparser.js View File

@ -327,13 +327,13 @@
next(); next();
} }
if (token == 'false') { if (token == 'false') {
token = false; // cast to boolean
token = false; // convert to boolean
} }
else if (token == 'true') { else if (token == 'true') {
token = true; // cast to boolean
token = true; // convert to boolean
} }
else if (!isNaN(Number(token))) { else if (!isNaN(Number(token))) {
token = Number(token); // cast to number
token = Number(token); // convert to number
} }
tokenType = TOKENTYPE.IDENTIFIER; tokenType = TOKENTYPE.IDENTIFIER;
return; return;

+ 2
- 2
src/timeline/Range.js View File

@ -139,8 +139,8 @@ Range.prototype.setRange = function(start, end) {
* @private * @private
*/ */
Range.prototype._applyRange = function(start, end) { Range.prototype._applyRange = function(start, end) {
var newStart = (start != null) ? util.cast(start, 'Number') : this.start;
var newEnd = (end != null) ? util.cast(end, 'Number') : this.end;
var newStart = (start != null) ? util.convert(start, 'Number') : this.start;
var newEnd = (end != null) ? util.convert(end, 'Number') : this.end;
var diff; var diff;
// check for valid number // check for valid number

+ 1
- 1
src/timeline/Timeline.js View File

@ -143,7 +143,7 @@ Timeline.prototype.setItems = function(items) {
} }
if (!(items instanceof DataSet)) { if (!(items instanceof DataSet)) {
newItemSet = new DataSet({ newItemSet = new DataSet({
fieldTypes: {
convert: {
start: 'Date', start: 'Date',
end: 'Date' end: 'Date'
} }

+ 1
- 1
src/timeline/component/GroupSet.js View File

@ -120,7 +120,7 @@ GroupSet.prototype.setGroups = function setGroups(groups) {
} }
else { else {
this.groupsData = new DataSet({ this.groupsData = new DataSet({
fieldTypes: {
convert: {
start: 'Date', start: 'Date',
end: 'Date' end: 'Date'
} }

+ 2
- 2
src/timeline/component/TimeAxis.js View File

@ -487,8 +487,8 @@ TimeAxis.prototype.reflow = function () {
// calculate range and step // calculate range and step
this._updateConversion(); this._updateConversion();
var start = util.cast(range.start, 'Date'),
end = util.cast(range.end, 'Date'),
var start = util.convert(range.start, 'Date'),
end = util.convert(range.end, 'Date'),
minimumStep = this.toTime((props.minorCharWidth || 10) * 5) - this.toTime(0); minimumStep = this.toTime((props.minorCharWidth || 10) * 5) - this.toTime(0);
this.step = new TimeStep(start, end, minimumStep); this.step = new TimeStep(start, end, minimumStep);
changed += update(props.range, 'start', start.valueOf()); changed += update(props.range, 'start', start.valueOf());

+ 12
- 12
src/util.js View File

@ -98,7 +98,7 @@ util.extend = function (a, b) {
}; };
/** /**
* Cast an object to another type
* Convert an object to another type
* @param {Boolean | Number | String | Date | Moment | Null | undefined} object * @param {Boolean | Number | String | Date | Moment | Null | undefined} object
* @param {String | undefined} type Name of the type. Available types: * @param {String | undefined} type Name of the type. Available types:
* 'Boolean', 'Number', 'String', * 'Boolean', 'Number', 'String',
@ -106,7 +106,7 @@ util.extend = function (a, b) {
* @return {*} object * @return {*} object
* @throws Error * @throws Error
*/ */
util.cast = function cast(object, type) {
util.convert = function convert(object, type) {
var match; var match;
if (object === undefined) { if (object === undefined) {
@ -159,7 +159,7 @@ util.cast = function cast(object, type) {
} }
else { else {
throw new Error( throw new Error(
'Cannot cast object of type ' + util.getType(object) +
'Cannot convert object of type ' + util.getType(object) +
' to type Date'); ' to type Date');
} }
@ -185,7 +185,7 @@ util.cast = function cast(object, type) {
} }
else { else {
throw new Error( throw new Error(
'Cannot cast object of type ' + util.getType(object) +
'Cannot convert object of type ' + util.getType(object) +
' to type Date'); ' to type Date');
} }
@ -211,7 +211,7 @@ util.cast = function cast(object, type) {
} }
else { else {
throw new Error( throw new Error(
'Cannot cast object of type ' + util.getType(object) +
'Cannot convert object of type ' + util.getType(object) +
' to type ISODate'); ' to type ISODate');
} }
@ -236,12 +236,12 @@ util.cast = function cast(object, type) {
} }
else { else {
throw new Error( throw new Error(
'Cannot cast object of type ' + util.getType(object) +
'Cannot convert object of type ' + util.getType(object) +
' to type ASPDate'); ' to type ASPDate');
} }
default: default:
throw new Error('Cannot cast object of type ' + util.getType(object) +
throw new Error('Cannot convert object of type ' + util.getType(object) +
' to type "' + type + '"'); ' to type "' + type + '"');
} }
}; };
@ -568,7 +568,7 @@ util.preventDefault = function preventDefault (event) {
util.option = {}; util.option = {};
/** /**
* Cast a value as boolean
* Convert a value into a boolean
* @param {Boolean | function | undefined} value * @param {Boolean | function | undefined} value
* @param {Boolean} [defaultValue] * @param {Boolean} [defaultValue]
* @returns {Boolean} bool * @returns {Boolean} bool
@ -586,7 +586,7 @@ util.option.asBoolean = function (value, defaultValue) {
}; };
/** /**
* Cast a value as number
* Convert a value into a number
* @param {Boolean | function | undefined} value * @param {Boolean | function | undefined} value
* @param {Number} [defaultValue] * @param {Number} [defaultValue]
* @returns {Number} number * @returns {Number} number
@ -604,7 +604,7 @@ util.option.asNumber = function (value, defaultValue) {
}; };
/** /**
* Cast a value as string
* Convert a value into a string
* @param {String | function | undefined} value * @param {String | function | undefined} value
* @param {String} [defaultValue] * @param {String} [defaultValue]
* @returns {String} str * @returns {String} str
@ -622,7 +622,7 @@ util.option.asString = function (value, defaultValue) {
}; };
/** /**
* Cast a size or location in pixels or a percentage
* Convert a size or location into a string with pixels or a percentage
* @param {String | Number | function | undefined} value * @param {String | Number | function | undefined} value
* @param {String} [defaultValue] * @param {String} [defaultValue]
* @returns {String} size * @returns {String} size
@ -644,7 +644,7 @@ util.option.asSize = function (value, defaultValue) {
}; };
/** /**
* Cast a value as DOM element
* Convert a value into a DOM element
* @param {HTMLElement | function | undefined} value * @param {HTMLElement | function | undefined} value
* @param {HTMLElement} [defaultValue] * @param {HTMLElement} [defaultValue]
* @returns {HTMLElement | null} dom * @returns {HTMLElement | null} dom

+ 1
- 1
test/dataset.html View File

@ -9,7 +9,7 @@
<script> <script>
var dataset = new vis.DataSet({ var dataset = new vis.DataSet({
fieldId: 'id', fieldId: 'id',
fieldTypes: {
convert: {
start: 'Date' start: 'Date'
} }
}); });

+ 4
- 4
test/dataset.js View File

@ -6,7 +6,7 @@ var assert = require('assert'),
var now = new Date(); var now = new Date();
var data = new DataSet({ var data = new DataSet({
fieldTypes: {
convert: {
start: 'Date', start: 'Date',
end: 'Date' end: 'Date'
} }
@ -41,10 +41,10 @@ assert.deepEqual(data.get({
]); ]);
// cast dates
// convert dates
assert.deepEqual(data.get({ assert.deepEqual(data.get({
fields: ['id', 'start'], fields: ['id', 'start'],
fieldTypes: {start: 'Number'}
convert: {start: 'Number'}
}).sort(sort), [ }).sort(sort), [
{id: 1, start: now.valueOf()}, {id: 1, start: now.valueOf()},
{id: 2, start: now.valueOf()}, {id: 2, start: now.valueOf()},
@ -56,7 +56,7 @@ assert.deepEqual(data.get({
// get a single item // get a single item
assert.deepEqual(data.get(1, { assert.deepEqual(data.get(1, {
fields: ['id', 'start'], fields: ['id', 'start'],
fieldTypes: {start: 'ISODate'}
convert: {start: 'ISODate'}
}), { }), {
id: 1, id: 1,
start: now.toISOString() start: now.toISOString()

+ 1
- 1
test/timeline.html View File

@ -41,7 +41,7 @@
// create a dataset with items // create a dataset with items
var now = moment().minutes(0).seconds(0).milliseconds(0); var now = moment().minutes(0).seconds(0).milliseconds(0);
var items = new vis.DataSet({ var items = new vis.DataSet({
fieldTypes: {
convert: {
start: 'Date', start: 'Date',
end: 'Date' end: 'Date'
}, },

+ 63
- 63
vis.js View File

@ -129,7 +129,7 @@ util.extend = function (a, b) {
}; };
/** /**
* Cast an object to another type
* Convert an object to another type
* @param {Boolean | Number | String | Date | Moment | Null | undefined} object * @param {Boolean | Number | String | Date | Moment | Null | undefined} object
* @param {String | undefined} type Name of the type. Available types: * @param {String | undefined} type Name of the type. Available types:
* 'Boolean', 'Number', 'String', * 'Boolean', 'Number', 'String',
@ -137,7 +137,7 @@ util.extend = function (a, b) {
* @return {*} object * @return {*} object
* @throws Error * @throws Error
*/ */
util.cast = function cast(object, type) {
util.convert = function convert(object, type) {
var match; var match;
if (object === undefined) { if (object === undefined) {
@ -190,7 +190,7 @@ util.cast = function cast(object, type) {
} }
else { else {
throw new Error( throw new Error(
'Cannot cast object of type ' + util.getType(object) +
'Cannot convert object of type ' + util.getType(object) +
' to type Date'); ' to type Date');
} }
@ -216,7 +216,7 @@ util.cast = function cast(object, type) {
} }
else { else {
throw new Error( throw new Error(
'Cannot cast object of type ' + util.getType(object) +
'Cannot convert object of type ' + util.getType(object) +
' to type Date'); ' to type Date');
} }
@ -242,7 +242,7 @@ util.cast = function cast(object, type) {
} }
else { else {
throw new Error( throw new Error(
'Cannot cast object of type ' + util.getType(object) +
'Cannot convert object of type ' + util.getType(object) +
' to type ISODate'); ' to type ISODate');
} }
@ -267,12 +267,12 @@ util.cast = function cast(object, type) {
} }
else { else {
throw new Error( throw new Error(
'Cannot cast object of type ' + util.getType(object) +
'Cannot convert object of type ' + util.getType(object) +
' to type ASPDate'); ' to type ASPDate');
} }
default: default:
throw new Error('Cannot cast object of type ' + util.getType(object) +
throw new Error('Cannot convert object of type ' + util.getType(object) +
' to type "' + type + '"'); ' to type "' + type + '"');
} }
}; };
@ -599,7 +599,7 @@ util.preventDefault = function preventDefault (event) {
util.option = {}; util.option = {};
/** /**
* Cast a value as boolean
* Convert a value into a boolean
* @param {Boolean | function | undefined} value * @param {Boolean | function | undefined} value
* @param {Boolean} [defaultValue] * @param {Boolean} [defaultValue]
* @returns {Boolean} bool * @returns {Boolean} bool
@ -617,7 +617,7 @@ util.option.asBoolean = function (value, defaultValue) {
}; };
/** /**
* Cast a value as number
* Convert a value into a number
* @param {Boolean | function | undefined} value * @param {Boolean | function | undefined} value
* @param {Number} [defaultValue] * @param {Number} [defaultValue]
* @returns {Number} number * @returns {Number} number
@ -635,7 +635,7 @@ util.option.asNumber = function (value, defaultValue) {
}; };
/** /**
* Cast a value as string
* Convert a value into a string
* @param {String | function | undefined} value * @param {String | function | undefined} value
* @param {String} [defaultValue] * @param {String} [defaultValue]
* @returns {String} str * @returns {String} str
@ -653,7 +653,7 @@ util.option.asString = function (value, defaultValue) {
}; };
/** /**
* Cast a size or location in pixels or a percentage
* Convert a size or location into a string with pixels or a percentage
* @param {String | Number | function | undefined} value * @param {String | Number | function | undefined} value
* @param {String} [defaultValue] * @param {String} [defaultValue]
* @returns {String} size * @returns {String} size
@ -675,7 +675,7 @@ util.option.asSize = function (value, defaultValue) {
}; };
/** /**
* Cast a value as DOM element
* Convert a value into a DOM element
* @param {HTMLElement | function | undefined} value * @param {HTMLElement | function | undefined} value
* @param {HTMLElement} [defaultValue] * @param {HTMLElement} [defaultValue]
* @returns {HTMLElement | null} dom * @returns {HTMLElement | null} dom
@ -1157,7 +1157,7 @@ EventBus.prototype.emit = function (event, data, source) {
* Usage: * Usage:
* var dataSet = new DataSet({ * var dataSet = new DataSet({
* fieldId: '_id', * fieldId: '_id',
* fieldTypes: {
* convert: {
* // ... * // ...
* } * }
* }); * });
@ -1182,7 +1182,7 @@ EventBus.prototype.emit = function (event, data, source) {
* @param {Object} [options] Available options: * @param {Object} [options] Available options:
* {String} fieldId Field name of the id in the * {String} fieldId Field name of the id in the
* items, 'id' by default. * items, 'id' by default.
* {Object.<String, String} fieldTypes
* {Object.<String, String} convert
* A map with field names as key, * A map with field names as key,
* and the field type as value. * and the field type as value.
* @constructor DataSet * @constructor DataSet
@ -1194,17 +1194,17 @@ function DataSet (options) {
this.options = options || {}; this.options = options || {};
this.data = {}; // map with data indexed by id this.data = {}; // map with data indexed by id
this.fieldId = this.options.fieldId || 'id'; // name of the field containing id this.fieldId = this.options.fieldId || 'id'; // name of the field containing id
this.fieldTypes = {}; // field types by field name
this.convert = {}; // field types by field name
if (this.options.fieldTypes) {
for (var field in this.options.fieldTypes) {
if (this.options.fieldTypes.hasOwnProperty(field)) {
var value = this.options.fieldTypes[field];
if (this.options.convert) {
for (var field in this.options.convert) {
if (this.options.convert.hasOwnProperty(field)) {
var value = this.options.convert[field];
if (value == 'Date' || value == 'ISODate' || value == 'ASPDate') { if (value == 'Date' || value == 'ISODate' || value == 'ASPDate') {
this.fieldTypes[field] = 'Date';
this.convert[field] = 'Date';
} }
else { else {
this.fieldTypes[field] = value;
this.convert[field] = value;
} }
} }
} }
@ -1415,7 +1415,7 @@ DataSet.prototype.update = function (data, senderId) {
* {Object} options An Object with options. Available options: * {Object} options An Object with options. Available options:
* {String} [type] Type of data to be returned. Can * {String} [type] Type of data to be returned. Can
* be 'DataTable' or 'Array' (default) * be 'DataTable' or 'Array' (default)
* {Object.<String, String>} [fieldTypes]
* {Object.<String, String>} [convert]
* {String[]} [fields] field names to be returned * {String[]} [fields] field names to be returned
* {function} [filter] filter items * {function} [filter] filter items
* {String | function} [order] Order the items by * {String | function} [order] Order the items by
@ -1472,14 +1472,14 @@ DataSet.prototype.get = function (args) {
} }
// build options // build options
var fieldTypes = options && options.fieldTypes || this.options.fieldTypes;
var convert = options && options.convert || this.options.convert;
var filter = options && options.filter; var filter = options && options.filter;
var items = [], item, itemId, i, len; var items = [], item, itemId, i, len;
// cast items
// convert items
if (id != undefined) { if (id != undefined) {
// return a single item // return a single item
item = me._getItem(id, fieldTypes);
item = me._getItem(id, convert);
if (filter && !filter(item)) { if (filter && !filter(item)) {
item = null; item = null;
} }
@ -1487,7 +1487,7 @@ DataSet.prototype.get = function (args) {
else if (ids != undefined) { else if (ids != undefined) {
// return a subset of items // return a subset of items
for (i = 0, len = ids.length; i < len; i++) { for (i = 0, len = ids.length; i < len; i++) {
item = me._getItem(ids[i], fieldTypes);
item = me._getItem(ids[i], convert);
if (!filter || filter(item)) { if (!filter || filter(item)) {
items.push(item); items.push(item);
} }
@ -1497,7 +1497,7 @@ DataSet.prototype.get = function (args) {
// return all items // return all items
for (itemId in this.data) { for (itemId in this.data) {
if (this.data.hasOwnProperty(itemId)) { if (this.data.hasOwnProperty(itemId)) {
item = me._getItem(itemId, fieldTypes);
item = me._getItem(itemId, convert);
if (!filter || filter(item)) { if (!filter || filter(item)) {
items.push(item); items.push(item);
} }
@ -1573,7 +1573,7 @@ DataSet.prototype.getIds = function (options) {
var data = this.data, var data = this.data,
filter = options && options.filter, filter = options && options.filter,
order = options && options.order, order = options && options.order,
fieldTypes = options && options.fieldTypes || this.options.fieldTypes,
convert = options && options.convert || this.options.convert,
i, i,
len, len,
id, id,
@ -1588,7 +1588,7 @@ DataSet.prototype.getIds = function (options) {
items = []; items = [];
for (id in data) { for (id in data) {
if (data.hasOwnProperty(id)) { if (data.hasOwnProperty(id)) {
item = this._getItem(id, fieldTypes);
item = this._getItem(id, convert);
if (filter(item)) { if (filter(item)) {
items.push(item); items.push(item);
} }
@ -1605,7 +1605,7 @@ DataSet.prototype.getIds = function (options) {
// create unordered list // create unordered list
for (id in data) { for (id in data) {
if (data.hasOwnProperty(id)) { if (data.hasOwnProperty(id)) {
item = this._getItem(id, fieldTypes);
item = this._getItem(id, convert);
if (filter(item)) { if (filter(item)) {
ids.push(item[this.fieldId]); ids.push(item[this.fieldId]);
} }
@ -1649,7 +1649,7 @@ DataSet.prototype.getIds = function (options) {
* The order of the items is not determined. * The order of the items is not determined.
* @param {function} callback * @param {function} callback
* @param {Object} [options] Available options: * @param {Object} [options] Available options:
* {Object.<String, String>} [fieldTypes]
* {Object.<String, String>} [convert]
* {String[]} [fields] filter fields * {String[]} [fields] filter fields
* {function} [filter] filter items * {function} [filter] filter items
* {String | function} [order] Order the items by * {String | function} [order] Order the items by
@ -1657,7 +1657,7 @@ DataSet.prototype.getIds = function (options) {
*/ */
DataSet.prototype.forEach = function (callback, options) { DataSet.prototype.forEach = function (callback, options) {
var filter = options && options.filter, var filter = options && options.filter,
fieldTypes = options && options.fieldTypes || this.options.fieldTypes,
convert = options && options.convert || this.options.convert,
data = this.data, data = this.data,
item, item,
id; id;
@ -1676,7 +1676,7 @@ DataSet.prototype.forEach = function (callback, options) {
// unordered // unordered
for (id in data) { for (id in data) {
if (data.hasOwnProperty(id)) { if (data.hasOwnProperty(id)) {
item = this._getItem(id, fieldTypes);
item = this._getItem(id, convert);
if (!filter || filter(item)) { if (!filter || filter(item)) {
callback(item, id); callback(item, id);
} }
@ -1689,7 +1689,7 @@ DataSet.prototype.forEach = function (callback, options) {
* Map every item in the dataset. * Map every item in the dataset.
* @param {function} callback * @param {function} callback
* @param {Object} [options] Available options: * @param {Object} [options] Available options:
* {Object.<String, String>} [fieldTypes]
* {Object.<String, String>} [convert]
* {String[]} [fields] filter fields * {String[]} [fields] filter fields
* {function} [filter] filter items * {function} [filter] filter items
* {String | function} [order] Order the items by * {String | function} [order] Order the items by
@ -1698,15 +1698,15 @@ DataSet.prototype.forEach = function (callback, options) {
*/ */
DataSet.prototype.map = function (callback, options) { DataSet.prototype.map = function (callback, options) {
var filter = options && options.filter, var filter = options && options.filter,
fieldTypes = options && options.fieldTypes || this.options.fieldTypes,
convert = options && options.convert || this.options.convert,
mappedItems = [], mappedItems = [],
data = this.data, data = this.data,
item; item;
// cast and filter items
// convert and filter items
for (var id in data) { for (var id in data) {
if (data.hasOwnProperty(id)) { if (data.hasOwnProperty(id)) {
item = this._getItem(id, fieldTypes);
item = this._getItem(id, convert);
if (!filter || filter(item)) { if (!filter || filter(item)) {
mappedItems.push(callback(item, id)); mappedItems.push(callback(item, id));
} }
@ -1900,13 +1900,13 @@ DataSet.prototype.min = function (field) {
DataSet.prototype.distinct = function (field) { DataSet.prototype.distinct = function (field) {
var data = this.data, var data = this.data,
values = [], values = [],
fieldType = this.options.fieldTypes[field],
fieldType = this.options.convert[field],
count = 0; count = 0;
for (var prop in data) { for (var prop in data) {
if (data.hasOwnProperty(prop)) { if (data.hasOwnProperty(prop)) {
var item = data[prop]; var item = data[prop];
var value = util.cast(item[field], fieldType);
var value = util.convert(item[field], fieldType);
var exists = false; var exists = false;
for (var i = 0; i < count; i++) { for (var i = 0; i < count; i++) {
if (values[i] == value) { if (values[i] == value) {
@ -1950,8 +1950,8 @@ DataSet.prototype._addItem = function (item) {
var d = {}; var d = {};
for (var field in item) { for (var field in item) {
if (item.hasOwnProperty(field)) { if (item.hasOwnProperty(field)) {
var type = this.fieldTypes[field]; // type may be undefined
d[field] = util.cast(item[field], type);
var fieldType = this.convert[field]; // type may be undefined
d[field] = util.convert(item[field], fieldType);
} }
} }
this.data[id] = d; this.data[id] = d;
@ -1960,13 +1960,13 @@ DataSet.prototype._addItem = function (item) {
}; };
/** /**
* Get an item. Fields can be casted to a specific type
* Get an item. Fields can be converted to a specific type
* @param {String} id * @param {String} id
* @param {Object.<String, String>} [fieldTypes] Cast field types
* @param {Object.<String, String>} [convert] field types to convert
* @return {Object | null} item * @return {Object | null} item
* @private * @private
*/ */
DataSet.prototype._getItem = function (id, fieldTypes) {
DataSet.prototype._getItem = function (id, convert) {
var field, value; var field, value;
// get the item from the dataset // get the item from the dataset
@ -1975,35 +1975,35 @@ DataSet.prototype._getItem = function (id, fieldTypes) {
return null; return null;
} }
// cast the items field types
var casted = {},
// convert the items field types
var converted = {},
fieldId = this.fieldId, fieldId = this.fieldId,
internalIds = this.internalIds; internalIds = this.internalIds;
if (fieldTypes) {
if (convert) {
for (field in raw) { for (field in raw) {
if (raw.hasOwnProperty(field)) { if (raw.hasOwnProperty(field)) {
value = raw[field]; value = raw[field];
// output all fields, except internal ids // output all fields, except internal ids
if ((field != fieldId) || !(value in internalIds)) { if ((field != fieldId) || !(value in internalIds)) {
casted[field] = util.cast(value, fieldTypes[field]);
converted[field] = util.convert(value, convert[field]);
} }
} }
} }
} }
else { else {
// no field types specified, no casting needed
// no field types specified, no converting needed
for (field in raw) { for (field in raw) {
if (raw.hasOwnProperty(field)) { if (raw.hasOwnProperty(field)) {
value = raw[field]; value = raw[field];
// output all fields, except internal ids // output all fields, except internal ids
if ((field != fieldId) || !(value in internalIds)) { if ((field != fieldId) || !(value in internalIds)) {
casted[field] = value;
converted[field] = value;
} }
} }
} }
} }
return casted;
return converted;
}; };
/** /**
@ -2028,8 +2028,8 @@ DataSet.prototype._updateItem = function (item) {
// merge with current item // merge with current item
for (var field in item) { for (var field in item) {
if (item.hasOwnProperty(field)) { if (item.hasOwnProperty(field)) {
var type = this.fieldTypes[field]; // type may be undefined
d[field] = util.cast(item[field], type);
var fieldType = this.convert[field]; // type may be undefined
d[field] = util.convert(item[field], fieldType);
} }
} }
@ -2164,7 +2164,7 @@ DataView.prototype.setData = function (data) {
* {Object} options An Object with options. Available options: * {Object} options An Object with options. Available options:
* {String} [type] Type of data to be returned. Can * {String} [type] Type of data to be returned. Can
* be 'DataTable' or 'Array' (default) * be 'DataTable' or 'Array' (default)
* {Object.<String, String>} [fieldTypes]
* {Object.<String, String>} [convert]
* {String[]} [fields] field names to be returned * {String[]} [fields] field names to be returned
* {function} [filter] filter items * {function} [filter] filter items
* {String | function} [order] Order the items by * {String | function} [order] Order the items by
@ -3130,8 +3130,8 @@ Range.prototype.setRange = function(start, end) {
* @private * @private
*/ */
Range.prototype._applyRange = function(start, end) { Range.prototype._applyRange = function(start, end) {
var newStart = (start != null) ? util.cast(start, 'Number') : this.start;
var newEnd = (end != null) ? util.cast(end, 'Number') : this.end;
var newStart = (start != null) ? util.convert(start, 'Number') : this.start;
var newEnd = (end != null) ? util.convert(end, 'Number') : this.end;
var diff; var diff;
// check for valid number // check for valid number
@ -4648,8 +4648,8 @@ TimeAxis.prototype.reflow = function () {
// calculate range and step // calculate range and step
this._updateConversion(); this._updateConversion();
var start = util.cast(range.start, 'Date'),
end = util.cast(range.end, 'Date'),
var start = util.convert(range.start, 'Date'),
end = util.convert(range.end, 'Date'),
minimumStep = this.toTime((props.minorCharWidth || 10) * 5) - this.toTime(0); minimumStep = this.toTime((props.minorCharWidth || 10) * 5) - this.toTime(0);
this.step = new TimeStep(start, end, minimumStep); this.step = new TimeStep(start, end, minimumStep);
changed += update(props.range, 'start', start.valueOf()); changed += update(props.range, 'start', start.valueOf());
@ -6354,7 +6354,7 @@ GroupSet.prototype.setGroups = function setGroups(groups) {
} }
else { else {
this.groupsData = new DataSet({ this.groupsData = new DataSet({
fieldTypes: {
convert: {
start: 'Date', start: 'Date',
end: 'Date' end: 'Date'
} }
@ -6871,7 +6871,7 @@ Timeline.prototype.setItems = function(items) {
} }
if (!(items instanceof DataSet)) { if (!(items instanceof DataSet)) {
newItemSet = new DataSet({ newItemSet = new DataSet({
fieldTypes: {
convert: {
start: 'Date', start: 'Date',
end: 'Date' end: 'Date'
} }
@ -7352,13 +7352,13 @@ Timeline.prototype.getItemRange = function getItemRange() {
next(); next();
} }
if (token == 'false') { if (token == 'false') {
token = false; // cast to boolean
token = false; // convert to boolean
} }
else if (token == 'true') { else if (token == 'true') {
token = true; // cast to boolean
token = true; // convert to boolean
} }
else if (!isNaN(Number(token))) { else if (!isNaN(Number(token))) {
token = Number(token); // cast to number
token = Number(token); // convert to number
} }
tokenType = TOKENTYPE.IDENTIFIER; tokenType = TOKENTYPE.IDENTIFIER;
return; return;

+ 5
- 5
vis.min.js
File diff suppressed because it is too large
View File


Loading…
Cancel
Save