convert_unit_widget_view_mixin.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. /**
  20. * Unit conversion mixin for widget views.
  21. * @type {Em.Mixin}
  22. */
  23. App.ConvertUnitWidgetViewMixin = Em.Mixin.create(App.BaseUnitConvertMixin, {
  24. /**
  25. * Get converted value according to widget value format from specified config property value.
  26. *
  27. * @method widgetValueByConfigAttributes
  28. * @param {String|Number} value - config property value to convert
  29. * @param {Boolean} [returnObject=false] - returned value should be an array of objects
  30. * @returns {Number|Object[]}
  31. */
  32. widgetValueByConfigAttributes: function(value, returnObject) {
  33. return this.convertValue(value, this._converterGetPropertyAttributes(), this._converterGetWidgetUnits(), returnObject);
  34. },
  35. /**
  36. * Get converted value according to config property unit format from specified widget value.
  37. *
  38. * @param {String|Number|Object[]} value - widget value to convert
  39. * @returns {String}
  40. * @method configValueByWidget
  41. */
  42. configValueByWidget: function(value) {
  43. var cfgValue = this.convertValue(value, this._converterGetWidgetUnits(), this._converterGetPropertyAttributes());
  44. return '' + this.get('config.stackConfigProperty.valueAttributes.type') === 'int' ? Math.round(cfgValue) : cfgValue;
  45. },
  46. /**
  47. * @private
  48. * @method _converterGetWidgetUnits
  49. * @throw Error
  50. * @returns {String|String[]}
  51. */
  52. _converterGetWidgetUnits: function() {
  53. var widgetAttributes = this.get('config.stackConfigProperty.widget');
  54. var widgetUnits = Em.getWithDefault(widgetAttributes, 'units.0.unit-name', false) || Em.getWithDefault(widgetAttributes, 'units.0.unit', false);
  55. Em.assert('Invalid widget units for ' + this.get('config.name') + ' widget object: ' + JSON.stringify(widgetAttributes), widgetUnits);
  56. return widgetUnits;
  57. },
  58. /**
  59. * @private
  60. * @method _converterGetPropertyAttributes
  61. * @throw Error
  62. * @returns {String|String[]}
  63. */
  64. _converterGetPropertyAttributes: function() {
  65. var propertyAttributes = this.get('config.stackConfigProperty.valueAttributes');
  66. var propertyUnits = Em.getWithDefault(propertyAttributes, 'unit', false) || Em.getWithDefault(propertyAttributes, 'type', false);
  67. Em.assert('Invalid property unit type for ' + this.get('config.name') + ' valueAttributes: ' + JSON.stringify(propertyAttributes), propertyUnits);
  68. return propertyUnits;
  69. }
  70. });