NodeStylesUtils.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * Licensed Materials - Property of IBM
  3. * IBM Cognos Products: Modeling UI (C) Copyright IBM Corp. 2016, 2020
  4. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP
  5. * Schedule Contract with IBM Corp.
  6. */
  7. define([], function() {
  8. var DEFAULT_ICON_COLOR = '';
  9. var DEFAULT_ICON = '';
  10. var styleMap = {
  11. 'copy': {color: '', icon: ''},
  12. 'reference': {color: '#00B4A0', icon: '#link_16_v7'},
  13. 'danger': {color: '#E62325', icon: '#warning_16_v7', backgroundColor: 'rgba(255,80,80,0.1)'},
  14. 'info': {color: '#595859', icon: '#information_16_v7', backgroundColor: 'rgba(255,80,80,0.1)'},
  15. 'warning': {color: '#FFB000', icon: '#warning--alt_16_v7', backgroundColor: 'rgba(255,80,80,0.1)'},
  16. 'package': {color: '#00B4A0', icon: '#repository_16_v7'}
  17. };
  18. var sizeMap = {
  19. nodeValues: {
  20. // Used to calculate width of rectangle based on label length
  21. EM_PIXEL_VALUE: 14,
  22. // Standard height of rectangle
  23. RECT_HEIGHT: 50,
  24. // Length, in pixels, of filter icon offset to center the icon
  25. FILTER_ICON_OFFSET: 26,
  26. // Standard height of an icon in the diagram
  27. ICON_HEIGHT: 16,
  28. // Standard width (should be square)
  29. ICON_WIDTH: 16,
  30. // Offset to center the text vertically in the rect
  31. TEXT_Y_OFFSET: 5,
  32. // Max width for a node (pixels)
  33. MAX_NODE_WIDTH: 160,
  34. // Length, in pixels, of error icon offset to center the icon
  35. VALIDATION_ICON_OFFSET: 8,
  36. // Height of an error icon in the diagram
  37. VALIDATION_ICON_HEIGHT: 16,
  38. // Width error (should be square)
  39. VALIDATION_ICON_WIDTH: 16,
  40. // Standard height of the details box
  41. TABLE_DETAILS_HEIGHT: 15,
  42. // Offset to center the table details text vertically in the rect
  43. TABLE_DETAILS_TEXT_Y_OFFSET: 36,
  44. },
  45. linkValues: {
  46. RECT_SIZE: 20,
  47. RECT_ROUNDED: 10,
  48. TEXT_Y_OFFSET: 3
  49. }
  50. };
  51. var NodeStylesUtils = {
  52. /**
  53. * Return the style for the given type.
  54. *
  55. * @param {String} the style type
  56. * @return {Object} the style for the given type
  57. */
  58. getStyle: function(type) {
  59. return styleMap[type];
  60. },
  61. /**
  62. * Return the style color for a node of a given type
  63. * @param {String} type - What type a node is (reference, copy, etc)
  64. */
  65. getColor: function(type){
  66. if( !(type && styleMap[type]) ){
  67. return DEFAULT_ICON_COLOR;
  68. }
  69. return styleMap[type].color;
  70. },
  71. /**
  72. * Return the style icon for a node of a given type (if needed)
  73. * @param {String} type - What type a node is (reference, copy, etc)
  74. */
  75. getIcon: function(type){
  76. if( !(type && styleMap[type]) ){
  77. return DEFAULT_ICON;
  78. }
  79. return styleMap[type].icon;
  80. },
  81. /**
  82. * Calculate the required length of a diagram node box based on length of label
  83. * @param {Object} d - A d3 selection of type 'node'
  84. */
  85. _nodeRectEMLength: function(d) {
  86. return Math.min(sizeMap.nodeValues.MAX_NODE_WIDTH / sizeMap.nodeValues.EM_PIXEL_VALUE, (sizeMap.nodeValues.ICON_WIDTH / 2) + (d.label ? (d.label.length / 2) : 0));
  87. },
  88. /*-- PSEUDO CSS STYLES FOR SVG ELEMENTS --*/
  89. /**
  90. * Provides styling for diagram nodes based on d3 selections
  91. */
  92. diagramNodeStyles: {
  93. boxColorStyle: {
  94. 'stroke': function(d) {
  95. return NodeStylesUtils.getColor(d.instanceType);
  96. }
  97. },
  98. boxStyle: {
  99. 'class': 'mui-diagram-node-box',
  100. 'height': sizeMap.nodeValues.RECT_HEIGHT,
  101. 'fill': '#FFFFFF',
  102. 'stroke-width': '2',
  103. 'transition': 'fill 150ms',
  104. 'width': function(d) {
  105. return NodeStylesUtils._nodeRectEMLength(d) + 'em';
  106. },
  107. 'transform': function(d) {
  108. return 'translate(-' + (NodeStylesUtils._nodeRectEMLength(d)) * (sizeMap.nodeValues.EM_PIXEL_VALUE / 2) + ', ' + -(sizeMap.nodeValues.RECT_HEIGHT / 2) + ')';
  109. },
  110. },
  111. validationNodeIconStyle: {
  112. 'class': 'mui_validationIcon',
  113. 'height': sizeMap.nodeValues.VALIDATION_ICON_HEIGHT,
  114. 'width': sizeMap.nodeValues.VALIDATION_ICON_WIDTH,
  115. 'xlink:href': function(d){
  116. return d.severityInfo ? NodeStylesUtils.getIcon(d.severityInfo.intent) : DEFAULT_ICON;
  117. },
  118. 'fill': function(d){
  119. return d.severityInfo ? NodeStylesUtils.getColor(d.severityInfo.intent) : DEFAULT_ICON_COLOR;
  120. },
  121. 'x': function(d) {
  122. return -((NodeStylesUtils._nodeRectEMLength(d)) * (sizeMap.nodeValues.EM_PIXEL_VALUE / 2) - sizeMap.nodeValues.VALIDATION_ICON_OFFSET);
  123. },
  124. 'y': -(sizeMap.nodeValues.VALIDATION_ICON_HEIGHT / 2),
  125. 'opacity': function(d) {
  126. return d.isValid ? 0 : 1;
  127. }
  128. },
  129. validationNodeCircleStyle: {
  130. 'r': 6,
  131. 'fill': 'transparent',
  132. 'cx': function(d) {
  133. return -((NodeStylesUtils._nodeRectEMLength(d)) * (sizeMap.nodeValues.EM_PIXEL_VALUE / 2) - sizeMap.nodeValues.VALIDATION_ICON_OFFSET) + 6;
  134. },
  135. 'cy': -(sizeMap.nodeValues.VALIDATION_ICON_HEIGHT / 2) + 6
  136. },
  137. validationLinkIconStyle: {
  138. 'class': 'mui_validationIcon',
  139. 'height': sizeMap.nodeValues.VALIDATION_ICON_HEIGHT,
  140. 'width': sizeMap.nodeValues.VALIDATION_ICON_WIDTH,
  141. 'xlink:href': function(d){
  142. return d.severityInfo ? NodeStylesUtils.getIcon(d.severityInfo.intent) : DEFAULT_ICON;
  143. },
  144. 'fill': function(d){
  145. return d.severityInfo ? NodeStylesUtils.getColor(d.severityInfo.intent) : DEFAULT_ICON_COLOR;
  146. },
  147. 'x': function(d) {
  148. return 0;
  149. },
  150. // 'y': -(sizeMap.nodeValues.VALIDATION_ICON_HEIGHT / 2),
  151. 'y': 0,
  152. 'opacity': function(d) {
  153. return d.isValid ? 0 : 1;
  154. }
  155. },
  156. validationLinkCircleStyle: {
  157. 'r': 6,
  158. 'cx': 6,
  159. 'cy': 6,
  160. 'fill': 'transparent'
  161. },
  162. validationLinkGroupIconStyle: {
  163. 'class': 'mui_validationIcon',
  164. 'height': sizeMap.nodeValues.VALIDATION_ICON_HEIGHT,
  165. 'width': sizeMap.nodeValues.VALIDATION_ICON_WIDTH,
  166. 'x': function(d) {
  167. return 0;
  168. },
  169. 'y': 0
  170. },
  171. textStyle: {
  172. 'dy': sizeMap.nodeValues.TEXT_Y_OFFSET,
  173. 'class': function(d) {
  174. return d.hidden ? 'mui-hidden' : '';
  175. }
  176. },
  177. filterIconStyle: {
  178. 'height': sizeMap.nodeValues.ICON_HEIGHT,
  179. 'width': sizeMap.nodeValues.ICON_WIDTH,
  180. 'xlink:href': '#filter_16_v7',
  181. 'fill': function(d){
  182. return NodeStylesUtils.getColor(d.instanceType);
  183. },
  184. 'x': function(d) {
  185. return ((NodeStylesUtils._nodeRectEMLength(d)) * (sizeMap.nodeValues.EM_PIXEL_VALUE / 2) - sizeMap.nodeValues.FILTER_ICON_OFFSET);
  186. },
  187. 'y': -sizeMap.nodeValues.ICON_HEIGHT,
  188. 'opacity': function(d) {
  189. if(d.filter) {
  190. return d.hidden ? 0.5 : 1;
  191. }
  192. return 0;
  193. }
  194. },
  195. referenceIconStyle: {
  196. 'height': sizeMap.nodeValues.ICON_HEIGHT,
  197. 'width': sizeMap.nodeValues.ICON_WIDTH,
  198. 'xlink:href': function(d){
  199. return NodeStylesUtils.getIcon(d.instanceType);
  200. },
  201. 'fill': function(d){
  202. return NodeStylesUtils.getColor(d.instanceType);
  203. },
  204. 'x': function(d) {
  205. return ((NodeStylesUtils._nodeRectEMLength(d)) * (sizeMap.nodeValues.EM_PIXEL_VALUE / 2) - sizeMap.nodeValues.FILTER_ICON_OFFSET);
  206. },
  207. 'y': 0,
  208. 'opacity': function(d) {
  209. return d.instanceType === 'reference' || d.instanceType === 'package' ? 1 : 0;
  210. }
  211. },
  212. tableDetailsBoxStyle: {
  213. 'class': 'mui-diagram-table-details-box',
  214. 'height': sizeMap.nodeValues.TABLE_DETAILS_HEIGHT,
  215. 'fill': function(d){
  216. return NodeStylesUtils.getColor(d.instanceType);
  217. },
  218. 'stroke': function(d){
  219. return NodeStylesUtils.getColor(d.instanceType);
  220. },
  221. 'stroke-width': '2',
  222. 'transition': 'fill 150ms',
  223. 'width': function(d) {
  224. return NodeStylesUtils._nodeRectEMLength(d) + 'em';
  225. },
  226. 'transform': function(d) {
  227. return 'translate(-' + (NodeStylesUtils._nodeRectEMLength(d)) * (sizeMap.nodeValues.EM_PIXEL_VALUE / 2) + ', ' + (sizeMap.nodeValues.RECT_HEIGHT / 2) + ')';
  228. },
  229. 'visibility': function(d) {
  230. return d.displayTableInfo ? 'visible' : 'hidden';
  231. }
  232. },
  233. tableDetailsTextStyle: {
  234. 'visibility': function(d) {
  235. return d.displayTableInfo ? 'visible' : 'hidden';
  236. },
  237. 'dy': sizeMap.nodeValues.TABLE_DETAILS_TEXT_Y_OFFSET,
  238. }
  239. },
  240. /**
  241. * Provides styling for diagram cardinality markers
  242. */
  243. CardinalityStyle: {
  244. boxStyle: {
  245. 'width': sizeMap.linkValues.RECT_SIZE,
  246. 'height': sizeMap.linkValues.RECT_SIZE,
  247. 'rx': sizeMap.linkValues.RECT_ROUNDED,
  248. 'ry': sizeMap.linkValues.RECT_ROUNDED
  249. },
  250. textStyle: {
  251. 'dx': sizeMap.linkValues.RECT_SIZE / 2,
  252. 'dy': (sizeMap.linkValues.RECT_SIZE / 2) + sizeMap.linkValues.TEXT_Y_OFFSET
  253. }
  254. }
  255. };
  256. return NodeStylesUtils;
  257. });