BaseStylePropertyAction.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018, 2019
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['./BasePropertyAction'], function (BaseClass) {
  8. var BaseStylePropertyAction = null;
  9. BaseStylePropertyAction = BaseClass.extend({
  10. _getWidgetSize: function _getWidgetSize(style, propertyNodes) {
  11. var container = propertyNodes[0]._layout.widget ? propertyNodes[0]._layout.widget : propertyNodes[0]._layout;
  12. var widgetSize = this._getSize(style, container.$el);
  13. return widgetSize;
  14. },
  15. _getWidgetOffset: function _getWidgetOffset(style, propertyNodes) {
  16. // We need to look at the parent offset and remove it from the widget offset val
  17. var container = propertyNodes[0]._layout.widget ? propertyNodes[0]._layout.widget : propertyNodes[0]._layout;
  18. var widgetOffset = this._getOffset(style, container.$el);
  19. var parentOffset = this._getOffset(style, propertyNodes[0]._layout.parentLayout.$el);
  20. return widgetOffset - parentOffset;
  21. },
  22. _getParentDim: function _getParentDim(style, propertyNodes) {
  23. var parentSize = this._getSize(style, propertyNodes[0]._layout.parentLayout.$el);
  24. return parentSize;
  25. },
  26. _getSize: function _getSize(style, $el) {
  27. return style === 'width' ? $el.width() : $el.height();
  28. },
  29. _getOffset: function _getOffset(style, $el) {
  30. var offset = $el.offset();
  31. return style === 'width' ? offset.left : offset.top;
  32. },
  33. _sanitizePercentSizeOnPage: function _sanitizePercentSizeOnPage(value, complimentaryProperty) {
  34. var sanitizedValue = value;
  35. sanitizedValue = Math.max(Math.min(100, sanitizedValue), 0); //% sizes need to be between 0 and 100
  36. var maxValue = 0;
  37. this.propertyNodes.forEach(function (node) {
  38. maxValue = Math.max(maxValue, parseFloat(node._layout.model.style[complimentaryProperty]));
  39. });
  40. if (maxValue + sanitizedValue > 100) {
  41. sanitizedValue = 100 - maxValue;
  42. }
  43. return sanitizedValue;
  44. },
  45. _updateInputProperty: function _updateInputProperty(property, value) {
  46. this.ignoreOnChange = true; //setValue will trigger onChange again.
  47. property.setValue(value);
  48. property.onChangeValueHold = value;
  49. this.ignoreOnChange = false;
  50. },
  51. getUnitsFromValue: function getUnitsFromValue(value) {
  52. return value.replace(/[\d.-]/g, '');
  53. },
  54. getValueString: function getValueString(numericValue, units) {
  55. return String(Number(numericValue.toFixed(units === 'px' ? 0 : 2))) + ' ' + units;
  56. },
  57. changeProperty: function changeProperty(action, id /*, value*/) {
  58. if (action.ignoreOnChange) {
  59. action.ignoreOnChange = false;
  60. return;
  61. }
  62. var numericValue = parseFloat(this.getValue());
  63. if (!isNaN(numericValue)) {
  64. numericValue = action._processChange.apply(action, [id, numericValue, this.units]);
  65. var stringValue = action.getValueString(numericValue, this.units);
  66. action._updateInputProperty(this, stringValue);
  67. } else {
  68. action._updateInputProperty(this, this.onChangeValueHold);
  69. }
  70. },
  71. wrapChangeProperty: function wrapChangeProperty() {
  72. var action = this;
  73. return function () {
  74. var args = [action].concat(Array.prototype.slice.call(arguments));
  75. action.changeProperty.apply(this, args);
  76. };
  77. },
  78. /**
  79. * Get an arbitrary number of properties from the selected nodes for use in the properties pane. Values are only present if all nodes have the same value and
  80. * the returned values are formatted properly for use in the properties pane.
  81. *
  82. * @param {Object[]} properties - Define the properties to fetch from the selected nodes.
  83. * @param {Function} properties[].getProp - Function to retrieve the property from a node. Takes the node as a parameter and returns the property value.
  84. * @param {String} properties[].units - Unit string to be appended to the final property.
  85. * @param {Boolean} properties[].break - Added during this function. Set to true once this property no longer needs to be checked.
  86. * @param {*} properties[].value - The final value used to return this property. Blank if not all properties matched.
  87. */
  88. _getValuesForProperties: function _getValuesForProperties(properties) {
  89. var _this = this;
  90. properties.forEach(function (property) {
  91. property.value = property.getProp(_this.propertyNodes[0]);
  92. });
  93. if (this.propertyNodes.length > 1) {
  94. var _loop = function _loop(i) {
  95. var canBreak = true;
  96. properties.forEach(function (property) {
  97. if (!property.break) {
  98. var currentValue = property.getProp(_this.propertyNodes[i]);
  99. if (currentValue !== property.value) {
  100. property.value = '';
  101. property.break = true;
  102. }
  103. }
  104. });
  105. if (canBreak) {
  106. return 'break';
  107. }
  108. };
  109. for (var i = 1; i < this.propertyNodes.length; i++) {
  110. var _ret = _loop(i);
  111. if (_ret === 'break') break;
  112. }
  113. }
  114. properties.forEach(function (property) {
  115. if (property.value !== '') {
  116. property.value = _this.getValueString(parseFloat(property.value), property.units);
  117. }
  118. });
  119. },
  120. showStyleProperties: function showStyleProperties(node) {
  121. var parentLayout = node._layout.parentLayout;
  122. if (parentLayout && parentLayout.model && parentLayout.model.type === 'group') {
  123. return false;
  124. }
  125. return true;
  126. },
  127. getProperties: function getProperties() {
  128. this.propertyNodes = this.selectedNodes;
  129. }
  130. });
  131. return BaseStylePropertyAction;
  132. });
  133. //# sourceMappingURL=BaseStylePropertyAction.js.map