Source: shape-controller.js

  1. define([
  2. 'jquery',
  3. 'underscore',
  4. 'viewcontroller',
  5. 'shape-editor',
  6. 'shapes'
  7. ], function($, _, ViewControllers, Shape, shapes) {
  8. // we only use the base attribute class, no need to get the base class
  9. var EmperorAttributeABC = ViewControllers.EmperorAttributeABC;
  10. var ShapeEditor = Shape.ShapeEditor;
  11. var ShapeFormatter = Shape.ShapeFormatter;
  12. /**
  13. * @class ShapeController
  14. *
  15. * Manipulates and displays the shape of objects on screen.
  16. *
  17. * @param {UIState} uiState The shared state
  18. * @param {Node} container Container node to create the controller in.
  19. * @param {Object} decompViewDict This is object is keyed by unique
  20. * identifiers and the values are DecompositionView objects referring to a
  21. * set of objects presented on screen. This dictionary will usually be shared
  22. * by all the tabs in the application. This argument is passed by reference.
  23. * Note that only the decompositions of type 'scatter' will be controlled,
  24. * other types will be ignored.
  25. *
  26. * @return {ShapeController} An instance of ShapeController
  27. * @constructs ShapeController
  28. * @extends EmperorAttributeABC
  29. */
  30. function ShapeController(uiState, container, decompViewDict) {
  31. var helpmenu = 'Change the shapes representing groups of data on the plot';
  32. var title = 'Shape';
  33. // Constant for width in slick-grid
  34. var SLICK_WIDTH = 100, scope = this;
  35. var name, value, shapeItem;
  36. // Build the options dictionary
  37. var options = {
  38. 'valueUpdatedCallback': function(e, args) {
  39. var val = args.item.category, shape = args.item.value;
  40. var group = args.item.plottables;
  41. var element = scope.getView();
  42. scope.setPlottableAttributes(element, shape, group);
  43. },
  44. 'categorySelectionCallback': function(evt, params) {
  45. var category = scope.$select.val();
  46. var decompViewDict = scope.getView();
  47. // getting all unique values per categories
  48. var uniqueVals = decompViewDict.decomp.getUniqueValuesByCategory(
  49. category);
  50. // Reset all to shapes to default
  51. var attributes = {};
  52. for (var index in uniqueVals) {
  53. attributes[uniqueVals[index]] = 'Sphere';
  54. }
  55. // fetch the slickgrid-formatted data
  56. var data = decompViewDict.setCategory(
  57. attributes, scope.setPlottableAttributes, category);
  58. scope.setSlickGridDataset(data);
  59. },
  60. 'slickGridColumn': {
  61. id: 'title', name: '', field: 'value',
  62. sortable: false, maxWidth: SLICK_WIDTH, minWidth: SLICK_WIDTH,
  63. editor: ShapeEditor,
  64. formatter: ShapeFormatter
  65. }
  66. };
  67. // shapes are only supported for scatter types
  68. var reshapeable = {};
  69. for (var key in decompViewDict) {
  70. if (decompViewDict[key].decomp.isScatterType()) {
  71. reshapeable[key] = decompViewDict[key];
  72. }
  73. }
  74. EmperorAttributeABC.call(this, uiState, container, title, helpmenu,
  75. reshapeable, options);
  76. return this;
  77. }
  78. ShapeController.prototype = Object.create(EmperorAttributeABC.prototype);
  79. ShapeController.prototype.constructor = EmperorAttributeABC;
  80. /**
  81. *
  82. * Private method to reset the shape of all the objects to spheres.
  83. *
  84. * @extends EmperorAttributeABC
  85. * @private
  86. *
  87. */
  88. ShapeController.prototype._resetAttribute = function() {
  89. EmperorAttributeABC.prototype._resetAttribute.call(this);
  90. var scope = this;
  91. _.each(this.decompViewDict, function(view) {
  92. scope.setPlottableAttributes(view, 'Sphere', view.decomp.plottable);
  93. view.needsUpdate = true;
  94. });
  95. };
  96. /**
  97. * Helper function to set the shape of plottable
  98. *
  99. * @param {Object} scope The scope where the plottables exist
  100. * @param {string} shape String representation of the shape to be applied
  101. * to the plottables.
  102. * @param {Object[]} group Array of objects that should be changed in scope
  103. */
  104. ShapeController.prototype.setPlottableAttributes =
  105. function(scope, shape, group) {
  106. if (scope.UIState['view.viewType'] == 'parallel-plot')
  107. return;
  108. var idx, factor = scope.getGeometryFactor();
  109. // get the appropriately sized geometry
  110. var geometry = shapes.getGeometry(shape, factor);
  111. if (geometry === undefined) {
  112. throw new Error('Unknown shape ' + shape);
  113. }
  114. _.each(group, function(element) {
  115. idx = element.idx;
  116. scope.markers[idx].geometry = geometry;
  117. scope.markers[idx].userData.shape = shape;
  118. });
  119. scope.needsUpdate = true;
  120. };
  121. return ShapeController;
  122. });