Browse Source

Old items are removed now when replacing the dataset of an itemset.

css_transitions
josdejong 11 years ago
parent
commit
6468de61ea
4 changed files with 139 additions and 34 deletions
  1. +17
    -4
      src/component/itemset.js
  2. +1
    -1
      test/dataset.html
  3. +117
    -25
      vis.js
  4. +4
    -4
      vis.min.js

+ 17
- 4
src/component/itemset.js View File

@ -348,15 +348,27 @@ ItemSet.prototype.reflow = function reflow () {
* @param {DataSet | Array | DataTable} data * @param {DataSet | Array | DataTable} data
*/ */
ItemSet.prototype.setData = function setData(data) { ItemSet.prototype.setData = function setData(data) {
var me = this,
dataItems,
ids;
// unsubscribe from current dataset // unsubscribe from current dataset
var current = this.data; var current = this.data;
if (current) { if (current) {
util.forEach(this.listeners, function (callback, event) { util.forEach(this.listeners, function (callback, event) {
current.unsubscribe(event, callback); current.unsubscribe(event, callback);
}); });
// remove all drawn items
dataItems = current.get({fields: ['id']});
ids = [];
util.forEach(dataItems, function (dataItem, index) {
ids[index] = dataItem.id;
});
this._onRemove(ids);
} }
// TODO: first remove current data
// replace the dataset
if (data instanceof DataSet) { if (data instanceof DataSet) {
this.data = data; this.data = data;
} }
@ -370,14 +382,15 @@ ItemSet.prototype.setData = function setData(data) {
this.data.add(data); this.data.add(data);
} }
// subscribe to new dataset
var id = this.id; var id = this.id;
var me = this;
util.forEach(this.listeners, function (callback, event) { util.forEach(this.listeners, function (callback, event) {
me.data.subscribe(event, callback, id); me.data.subscribe(event, callback, id);
}); });
var dataItems = this.data.get({filter: ['id']});
var ids = [];
// draw all new items
dataItems = this.data.get({fields: ['id']});
ids = [];
util.forEach(dataItems, function (dataItem, index) { util.forEach(dataItems, function (dataItem, index) {
ids[index] = dataItem.id; ids[index] = dataItem.id;
}); });

+ 1
- 1
test/dataset.html View File

@ -10,7 +10,7 @@
var dataset = new vis.DataSet({ var dataset = new vis.DataSet({
fieldId: 'id', fieldId: 'id',
fieldTypes: { fieldTypes: {
start: 'ISODate'
start: 'Date'
} }
}); });

+ 117
- 25
vis.js View File

@ -5,7 +5,7 @@
* A dynamic, browser-based visualization library. * A dynamic, browser-based visualization library.
* *
* @version 0.0.8 * @version 0.0.8
* @date 2013-05-07
* @date 2013-05-08
* *
* @license * @license
* Copyright (C) 2011-2013 Almende B.V, http://almende.com * Copyright (C) 2011-2013 Almende B.V, http://almende.com
@ -1694,7 +1694,8 @@ DataSet.prototype.update = function (data, senderId) {
* {String} [type] * {String} [type]
* 'DataTable' or 'Array' (default) * 'DataTable' or 'Array' (default)
* {Object.<String, String>} [fieldTypes] * {Object.<String, String>} [fieldTypes]
* {String[]} [fields] filter fields
* {String[]} [fields] filter fields
* {function} [filter] filter items
* @param {Array | DataTable} [data] If provided, items will be appended * @param {Array | DataTable} [data] If provided, items will be appended
* to this array or table. Required * to this array or table. Required
* in case of Google DataTable * in case of Google DataTable
@ -1704,6 +1705,8 @@ DataSet.prototype.update = function (data, senderId) {
DataSet.prototype.get = function (ids, options, data) { DataSet.prototype.get = function (ids, options, data) {
var me = this; var me = this;
// TODO: simplify handling the inputs. It's quite a mess right now...
// shift arguments when first argument contains the options // shift arguments when first argument contains the options
if (util.getType(ids) == 'Object') { if (util.getType(ids) == 'Object') {
data = options; data = options;
@ -1711,20 +1714,9 @@ DataSet.prototype.get = function (ids, options, data) {
ids = undefined; ids = undefined;
} }
// merge field types
var fieldTypes = {};
if (this.options && this.options.fieldTypes) {
util.forEach(this.options.fieldTypes, function (value, field) {
fieldTypes[field] = value;
});
}
if (options && options.fieldTypes) {
util.forEach(options.fieldTypes, function (value, field) {
fieldTypes[field] = value;
});
}
var fields = options ? options.fields : undefined;
var fieldTypes = this._mergeFieldTypes(options && options.fieldTypes);
var fields = options && options.fields;
var filter = options && options.filter;
// determine the return type // determine the return type
var type; var type;
@ -1753,7 +1745,10 @@ DataSet.prototype.get = function (ids, options, data) {
if (ids == undefined) { if (ids == undefined) {
// return all data // return all data
util.forEach(this.data, function (item) { util.forEach(this.data, function (item) {
me._appendRow(data, columns, me._castItem(item));
var castedItem = me._castItem(item);
if (!castedItem || filter(castedItem)) {
me._appendRow(data, columns, castedItem);
}
}); });
} }
else if (util.isNumber(ids) || util.isString(ids)) { else if (util.isNumber(ids) || util.isString(ids)) {
@ -1762,8 +1757,10 @@ DataSet.prototype.get = function (ids, options, data) {
} }
else if (ids instanceof Array) { else if (ids instanceof Array) {
ids.forEach(function (id) { ids.forEach(function (id) {
var item = me._castItem(me.data[id], fieldTypes, fields);
me._appendRow(data, columns, item);
var castedItem = me._castItem(me.data[id], fieldTypes, fields);
if (!castedItem || filter(castedItem)) {
me._appendRow(data, columns, castedItem);
}
}); });
} }
else { else {
@ -1777,7 +1774,10 @@ DataSet.prototype.get = function (ids, options, data) {
if (ids == undefined) { if (ids == undefined) {
// return all data // return all data
util.forEach(this.data, function (item) { util.forEach(this.data, function (item) {
data.push(me._castItem(item, fieldTypes, fields));
var castedItem = me._castItem(item, fieldTypes, fields);
if (!filter || filter(castedItem)) {
data.push(castedItem);
}
}); });
} }
else if (util.isNumber(ids) || util.isString(ids)) { else if (util.isNumber(ids) || util.isString(ids)) {
@ -1786,7 +1786,10 @@ DataSet.prototype.get = function (ids, options, data) {
} }
else if (ids instanceof Array) { else if (ids instanceof Array) {
ids.forEach(function (id) { ids.forEach(function (id) {
data.push(me._castItem(me.data[id], fieldTypes, fields));
var castedItem = me._castItem(me.data[id], fieldTypes, fields);
if (!filter || filter(castedItem)) {
data.push(castedItem);
}
}); });
} }
else { else {
@ -1798,6 +1801,82 @@ DataSet.prototype.get = function (ids, options, data) {
return data; return data;
}; };
/**
* Execute a callback function for every item in the dataset.
* The order of the items is not determined.
* @param {function} callback
* @param {Object} [options] Available options:
* {Object.<String, String>} [fieldTypes]
* {String[]} [fields] filter fields
* {function} [filter] filter items
*/
DataSet.prototype.forEach = function (callback, options) {
var fieldTypes = this._mergeFieldTypes(options && options.fieldTypes);
var fields = options && options.fields;
var filter = options && options.filter;
var me = this;
util.forEach(this.data, function (item, id) {
var castedItem = me._castItem(item, fieldTypes, fields);
if (!filter || filter(castedItem)) {
callback(castedItem, id);
}
});
};
/**
* Map every item in the dataset.
* @param {function} callback
* @param {Object} [options] Available options:
* {Object.<String, String>} [fieldTypes]
* {String[]} [fields] filter fields
* {function} [filter] filter items
* @return {Object[]} mappedItems
*/
DataSet.prototype.map = function (callback, options) {
var fieldTypes = this._mergeFieldTypes(options && options.fieldTypes);
var fields = options && options.fields;
var filter = options && options.filter;
var me = this;
var mappedItems = [];
util.forEach(this.data, function (item, id) {
var castedItem = me._castItem(item, fieldTypes, fields);
if (!filter || filter(castedItem)) {
var mappedItem = callback(castedItem, id);
mappedItems.push(mappedItem);
}
});
return mappedItems;
};
/**
* Merge the provided field types with the datasets fieldtypes
* @param {Object} fieldTypes
* @returns {Object} mergedFieldTypes
* @private
*/
DataSet.prototype._mergeFieldTypes = function (fieldTypes) {
var merged = {};
// extend with the datasets fieldTypes
if (this.options && this.options.fieldTypes) {
util.forEach(this.options.fieldTypes, function (value, field) {
merged[field] = value;
});
}
// extend with provided fieldTypes
if (fieldTypes) {
util.forEach(fieldTypes, function (value, field) {
merged[field] = value;
});
}
return merged;
};
/** /**
* Remove an object by pointer or by id * Remove an object by pointer or by id
* @param {String | Number | Object | Array} id Object or id, or an array with * @param {String | Number | Object | Array} id Object or id, or an array with
@ -4291,15 +4370,27 @@ ItemSet.prototype.reflow = function reflow () {
* @param {DataSet | Array | DataTable} data * @param {DataSet | Array | DataTable} data
*/ */
ItemSet.prototype.setData = function setData(data) { ItemSet.prototype.setData = function setData(data) {
var me = this,
dataItems,
ids;
// unsubscribe from current dataset // unsubscribe from current dataset
var current = this.data; var current = this.data;
if (current) { if (current) {
util.forEach(this.listeners, function (callback, event) { util.forEach(this.listeners, function (callback, event) {
current.unsubscribe(event, callback); current.unsubscribe(event, callback);
}); });
// remove all drawn items
dataItems = current.get({fields: ['id']});
ids = [];
util.forEach(dataItems, function (dataItem, index) {
ids[index] = dataItem.id;
});
this._onRemove(ids);
} }
// TODO: first remove current data
// replace the dataset
if (data instanceof DataSet) { if (data instanceof DataSet) {
this.data = data; this.data = data;
} }
@ -4313,14 +4404,15 @@ ItemSet.prototype.setData = function setData(data) {
this.data.add(data); this.data.add(data);
} }
// subscribe to new dataset
var id = this.id; var id = this.id;
var me = this;
util.forEach(this.listeners, function (callback, event) { util.forEach(this.listeners, function (callback, event) {
me.data.subscribe(event, callback, id); me.data.subscribe(event, callback, id);
}); });
var dataItems = this.data.get({filter: ['id']});
var ids = [];
// draw all new items
dataItems = this.data.get({fields: ['id']});
ids = [];
util.forEach(dataItems, function (dataItem, index) { util.forEach(dataItems, function (dataItem, index) {
ids[index] = dataItem.id; ids[index] = dataItem.id;
}); });

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


Loading…
Cancel
Save