vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.5 KiB

  1. var assert = require('assert'),
  2. moment = require('moment'),
  3. vis = require('../vis.js'),
  4. DataSet = vis.DataSet,
  5. DataView = vis.DataView;
  6. var groups = new DataSet();
  7. // add items with different groups
  8. groups.add([
  9. {id: 1, content: 'Item 1', group: 1},
  10. {id: 2, content: 'Item 2', group: 2},
  11. {id: 3, content: 'Item 3', group: 2},
  12. {id: 4, content: 'Item 4', group: 1},
  13. {id: 5, content: 'Item 5', group: 3}
  14. ]);
  15. var group2 = new DataView(groups, {
  16. filter: function (item) {
  17. return item.group == 2;
  18. }
  19. });
  20. // test getting the filtered data
  21. assert.deepEqual(group2.get(), [
  22. {id: 2, content: 'Item 2', group: 2},
  23. {id: 3, content: 'Item 3', group: 2}
  24. ]);
  25. // test filtering the view contents
  26. assert.deepEqual(group2.get({
  27. filter: function (item) {
  28. return item.id > 2;
  29. }
  30. }), [
  31. {id: 3, content: 'Item 3', group: 2}
  32. ]);
  33. // test event subscription
  34. var groupsTriggerCount = 0;
  35. groups.subscribe('*', function () {
  36. groupsTriggerCount++;
  37. });
  38. var group2TriggerCount = 0;
  39. group2.subscribe('*', function () {
  40. group2TriggerCount++;
  41. });
  42. groups.update({id:2, content: 'Item 2 (changed)'});
  43. assert.equal(groupsTriggerCount, 1);
  44. assert.equal(group2TriggerCount, 1);
  45. groups.update({id:5, content: 'Item 5 (changed)'});
  46. assert.equal(groupsTriggerCount, 2);
  47. assert.equal(group2TriggerCount, 1);
  48. // detach the view from groups
  49. group2.setData(null);
  50. assert.equal(groupsTriggerCount, 2);
  51. assert.equal(group2TriggerCount, 2);
  52. groups.update({id:2, content: 'Item 2 (changed again)'});
  53. assert.equal(groupsTriggerCount, 3);
  54. assert.equal(group2TriggerCount, 2);