combo_config_widget_view.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * Combo box widget view for config property.
  21. * @type {Em.View}
  22. */
  23. App.ComboConfigWidgetView = App.ConfigWidgetView.extend({
  24. templateName: require('templates/common/configs/widgets/combo_config_widget'),
  25. classNames: ['widget-config', 'combo-widget'],
  26. /**
  27. * Object with following structure:
  28. * {String} .value - value in widget format
  29. * {Object[]} .valuesList - map of entries and entry_labels
  30. * {String} .configValue - value in config format
  31. * {String} .widgetValue - value in widget format
  32. *
  33. * @property content
  34. * @type {Em.Object}
  35. */
  36. content: null,
  37. didInsertElement: function() {
  38. this.generateContent();
  39. this.toggleWidgetState();
  40. this.initPopover();
  41. this._super();
  42. },
  43. /**
  44. * Generate content for view. Set values map and current value.
  45. *
  46. * @method generateContent
  47. */
  48. generateContent: function() {
  49. this.set('content', Em.Object.create({}));
  50. this.set('content.valuesList', this.convertToWidgetUnits(this.get('config.stackConfigProperty.valueAttributes')));
  51. this.set('content.value', this.generateWidgetValue(this.get('config.value')));
  52. },
  53. /**
  54. * Generate values map according to widget/value format.
  55. *
  56. * @method convertToWidgetUnits
  57. * @param {Object} valueAttributes
  58. * @returns {Object[]} - values list map @see content.valuesList
  59. */
  60. convertToWidgetUnits: function(valueAttributes) {
  61. return Em.get(valueAttributes, 'entries').map(function(item) {
  62. return Em.Object.create({
  63. configValue: item.value,
  64. widgetValue: item.label || item.value
  65. });
  66. });
  67. },
  68. /**
  69. * Get widget value by specified config value.
  70. *
  71. * @method generateWidgetValue
  72. * @param {String} value - value in config property format
  73. * @returns {String}
  74. */
  75. generateWidgetValue: function(value) {
  76. return this.get('content.valuesList').findProperty('configValue', value).get('widgetValue');
  77. },
  78. /**
  79. * Get config value by specified widget value.
  80. *
  81. * @method generateConfigValue
  82. * @param {String} value - value in widget property format
  83. * @returns {String}
  84. */
  85. generateConfigValue: function(value) {
  86. return this.get('content.valuesList').findProperty('widgetValue', value).get('configValue');
  87. },
  88. /**
  89. * Action to set config value.
  90. *
  91. * @method setConfigValue
  92. * @param {Object} e
  93. */
  94. setConfigValue: function(e) {
  95. this.set('config.value', e.context);
  96. this.set('content.value', this.generateWidgetValue(e.context));
  97. this.get('controller').removeCurrentFromDependentList(this.get('config'));
  98. this.sendRequestRorDependentConfigs(this.get('config'));
  99. },
  100. /**
  101. * @override App.ConfigWidgetView.restoreValue
  102. * @method restoreValue
  103. */
  104. restoreValue: function() {
  105. this._super();
  106. this.setConfigValue({ context: this.get('config.defaultValue') });
  107. }
  108. });