widget_section.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.WidgetSectionMixin = Ember.Mixin.create({
  20. /**
  21. * UI default layout name
  22. */
  23. defaultLayoutName: function () {
  24. var heatmapType;
  25. if (this.get('content.serviceName')) {
  26. heatmapType = this.get('content.serviceName').toLowerCase();
  27. } else {
  28. heatmapType = "system";
  29. }
  30. return "default_" + heatmapType + this.layoutNameSuffix;
  31. }.property('content.serviceName'),
  32. /**
  33. * UI section name
  34. */
  35. sectionName: function () {
  36. if (this.get('content.serviceName')) {
  37. return this.get('content.serviceName') + this.sectionNameSuffix;
  38. } else {
  39. return "SYSTEM" + this.sectionNameSuffix
  40. }
  41. }.property('content.serviceName'),
  42. /**
  43. * Does Service has widget descriptor defined in the stack
  44. * @type {boolean}
  45. */
  46. isServiceWithEnhancedWidgets: function () {
  47. var isServiceWithWidgetdescriptor;
  48. var serviceName = this.get('content.serviceName');
  49. if (serviceName) {
  50. isServiceWithWidgetdescriptor = App.StackService.find().findProperty('serviceName', serviceName).get('isServiceWithWidgets');
  51. } else if (this.get('sectionName') === 'SYSTEM_HEATMAPS') {
  52. isServiceWithWidgetdescriptor = true;
  53. }
  54. return isServiceWithWidgetdescriptor && App.supports.customizedWidgets;
  55. }.property('content.serviceName'),
  56. /**
  57. * @Type {App.WidgetLayout}
  58. */
  59. activeWidgetLayout: {},
  60. /**
  61. * @type {Em.A}
  62. */
  63. widgets: function () {
  64. if (this.get('isWidgetsLoaded')) {
  65. if (this.get('activeWidgetLayout.widgets')) {
  66. return this.get('activeWidgetLayout.widgets').toArray();
  67. } else {
  68. return [];
  69. }
  70. }
  71. }.property('isWidgetsLoaded'),
  72. /**
  73. * load widgets defined by user
  74. * @returns {$.ajax}
  75. */
  76. loadActiveWidgetLayout: function () {
  77. this.set('activeWidgetLayout', {});
  78. this.set('isWidgetsLoaded', false);
  79. if (this.get('isServiceWithEnhancedWidgets')) {
  80. return App.ajax.send({
  81. name: 'widget.layout.get',
  82. sender: this,
  83. data: {
  84. layoutName: this.get('defaultLayoutName'),
  85. serviceName: this.get('content.serviceName')
  86. },
  87. success: 'loadActiveWidgetLayoutSuccessCallback'
  88. });
  89. } else {
  90. this.set('isWidgetsLoaded', true);
  91. }
  92. },
  93. /**
  94. * success callback of <code>loadActiveWidgetLayout()</code>
  95. * @param {object|null} data
  96. */
  97. loadActiveWidgetLayoutSuccessCallback: function (data) {
  98. if (data.items[0]) {
  99. App.widgetMapper.map(data.items[0].WidgetLayoutInfo);
  100. App.widgetLayoutMapper.map(data);
  101. this.set('activeWidgetLayout', App.WidgetLayout.find().findProperty('layoutName', this.get('defaultLayoutName')));
  102. this.set('isWidgetsLoaded', true);
  103. }
  104. },
  105. /**
  106. * save layout after re-order widgets
  107. * return {$.ajax}
  108. */
  109. saveWidgetLayout: function (widgets) {
  110. var activeLayout = this.get('activeWidgetLayout');
  111. var data = {
  112. "WidgetLayoutInfo": {
  113. "display_name": activeLayout.get("displayName"),
  114. "id": activeLayout.get("id"),
  115. "layout_name": activeLayout.get("layoutName"),
  116. "scope": activeLayout.get("scope"),
  117. "section_name": activeLayout.get("sectionName"),
  118. "widgets": widgets.map(function (widget) {
  119. return {
  120. "id": widget.get('id')
  121. }
  122. })
  123. }
  124. };
  125. return App.ajax.send({
  126. name: 'widget.layout.edit',
  127. sender: this,
  128. data: {
  129. layoutId: activeLayout.get("id"),
  130. data: data
  131. }
  132. });
  133. }
  134. });