step3_controller.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. App.WidgetWizardStep3Controller = Em.Controller.extend({
  20. name: "widgetWizardStep3Controller",
  21. isEditController: Em.computed.equal('content.controllerName', 'widgetEditController'),
  22. /**
  23. * @type {string}
  24. */
  25. widgetName: '',
  26. /**
  27. * @type {string}
  28. */
  29. widgetAuthor: '',
  30. /**
  31. * @type {boolean}
  32. */
  33. isSharedChecked: false,
  34. /**
  35. * @type {boolean}
  36. */
  37. isSharedCheckboxDisabled: false,
  38. /**
  39. * @type {string}
  40. */
  41. widgetScope: Em.computed.ifThenElse('isSharedChecked', 'Cluster', 'User'),
  42. /**
  43. * @type {string}
  44. */
  45. widgetDescription: '',
  46. /**
  47. * actual values of properties in API format
  48. * @type {object}
  49. */
  50. widgetProperties: {},
  51. /**
  52. * @type {Array}
  53. */
  54. widgetValues: [],
  55. /**
  56. * @type {Array}
  57. */
  58. widgetMetrics: [],
  59. /**
  60. * @type {boolean}
  61. */
  62. isSubmitDisabled: function () {
  63. var widgetNameEmpty = this.get('widgetName') ? !Boolean(this.get('widgetName').trim()) : true;
  64. return widgetNameEmpty || this.get('isNameInvalid') || this.get('isDescriptionInvalid');
  65. }.property('widgetName', 'isNameInvalid', 'isDescriptionInvalid'),
  66. /**
  67. * @type {boolean}
  68. */
  69. isNameInvalid: function () {
  70. return this.get('widgetName') ? this.get('widgetName').length >= 129 : false;
  71. }.property('widgetName'),
  72. /**
  73. * @type {boolean}
  74. */
  75. isDescriptionInvalid: function () {
  76. return this.get('widgetDescription') ? this.get('widgetDescription').length >= 2049 : false;
  77. }.property('widgetDescription'),
  78. /**
  79. * restore widget data set on 2nd step
  80. */
  81. initPreviewData: function () {
  82. this.set('widgetProperties', this.get('content.widgetProperties'));
  83. this.set('widgetValues', this.get('content.widgetValues'));
  84. this.set('widgetMetrics', this.get('content.widgetMetrics'));
  85. this.set('widgetAuthor', this.get('content.widgetAuthor'));
  86. this.set('widgetName', this.get('content.widgetName'));
  87. this.set('widgetDescription', this.get('content.widgetDescription'));
  88. this.set('isSharedChecked', this.get('content.widgetScope') == 'CLUSTER');
  89. // on editing, don't allow changing from shared scope to unshare
  90. var isSharedCheckboxDisabled = ((this.get('content.widgetScope') == 'CLUSTER') && this.get('isEditController'));
  91. this.set('isSharedCheckboxDisabled', isSharedCheckboxDisabled);
  92. if (!isSharedCheckboxDisabled) {
  93. this.addObserver('isSharedChecked', this, this.showConfirmationOnSharing);
  94. }
  95. },
  96. /**
  97. * confirmation popup
  98. * @returns {App.ModalPopup|undefined}
  99. */
  100. showConfirmationOnSharing: function () {
  101. var self = this;
  102. if (this.get('isSharedChecked')) {
  103. var bodyMessage = Em.Object.create({
  104. confirmMsg: Em.I18n.t('dashboard.widgets.browser.action.share.confirmation'),
  105. confirmButton: Em.I18n.t('dashboard.widgets.browser.action.share')
  106. });
  107. return App.showConfirmationFeedBackPopup(function (query) {
  108. self.set('isSharedChecked', true);
  109. }, bodyMessage, function (query) {
  110. self.set('isSharedChecked', false);
  111. });
  112. }
  113. },
  114. /**
  115. * collect all needed data to create new widget
  116. * @returns {{WidgetInfo: {cluster_name: *, widget_name: *, widget_type: *, description: *, scope: string, metrics: *, values: *, properties: *}}}
  117. */
  118. collectWidgetData: function () {
  119. return {
  120. WidgetInfo: {
  121. widget_name: this.get('widgetName'),
  122. widget_type: this.get('content.widgetType'),
  123. description: this.get('widgetDescription') || "",
  124. scope: this.get('widgetScope').toUpperCase(),
  125. author: this.get('widgetAuthor'),
  126. metrics: this.get('widgetMetrics').map(function (metric) {
  127. delete metric.data;
  128. return metric;
  129. }),
  130. values: this.get('widgetValues').map(function (value) {
  131. delete value.computedValue;
  132. return value;
  133. }),
  134. properties: this.get('widgetProperties')
  135. }
  136. };
  137. },
  138. cancel: function () {
  139. App.router.get(this.get('content.controllerName')).cancel();
  140. },
  141. complete: function () {
  142. App.router.send('complete', this.collectWidgetData());
  143. App.router.get(this.get('content.controllerName')).finishWizard();
  144. }
  145. });