widget_section.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 user default layout name
  34. */
  35. userLayoutName: function () {
  36. var heatmapType;
  37. var loginName = App.router.get('loginName');
  38. if (this.get('content.serviceName')) {
  39. heatmapType = this.get('content.serviceName').toLowerCase();
  40. } else {
  41. heatmapType = "system";
  42. }
  43. return loginName + "_" + heatmapType + this.layoutNameSuffix;
  44. }.property('content.serviceName'),
  45. /**
  46. * UI section name
  47. */
  48. sectionName: function () {
  49. if (this.get('content.serviceName')) {
  50. return this.get('content.serviceName') + this.sectionNameSuffix;
  51. } else {
  52. return "SYSTEM" + this.sectionNameSuffix
  53. }
  54. }.property('content.serviceName'),
  55. /**
  56. * @type {Em.A}
  57. */
  58. widgetLayouts: function () {
  59. return App.WidgetLayout.find();
  60. }.property('isWidgetLayoutsLoaded'),
  61. /**
  62. * Does Service has widget descriptor defined in the stack
  63. * @type {boolean}
  64. */
  65. isServiceWithEnhancedWidgets: function () {
  66. var isServiceWithWidgetdescriptor;
  67. var serviceName = this.get('content.serviceName');
  68. if (serviceName) {
  69. isServiceWithWidgetdescriptor = App.StackService.find().findProperty('serviceName', serviceName).get('isServiceWithWidgets');
  70. } else if (this.get('sectionName') === 'SYSTEM_HEATMAPS') {
  71. isServiceWithWidgetdescriptor = true;
  72. }
  73. return isServiceWithWidgetdescriptor && (App.supports.customizedWidgets || this.sectionNameSuffix === "_HEATMAPS");
  74. }.property('content.serviceName'),
  75. /**
  76. * @Type {App.WidgetLayout}
  77. */
  78. activeWidgetLayout: {},
  79. /**
  80. * @type {Em.A}
  81. */
  82. widgets: function () {
  83. if (this.get('isWidgetsLoaded')) {
  84. if (this.get('activeWidgetLayout.widgets')) {
  85. return this.get('activeWidgetLayout.widgets').toArray();
  86. } else {
  87. return [];
  88. }
  89. }
  90. }.property('isWidgetsLoaded'),
  91. /**
  92. * load widgets defined by user
  93. * @returns {$.ajax}
  94. */
  95. getActiveWidgetLayout: function () {
  96. var sectionName = this.get('sectionName');
  97. var urlParams = 'WidgetLayoutInfo/section_name=' + sectionName;
  98. this.set('activeWidgetLayout', {});
  99. this.set('isWidgetsLoaded', false);
  100. if (this.get('isServiceWithEnhancedWidgets')) {
  101. return App.ajax.send({
  102. name: 'widgets.layouts.active.get',
  103. sender: this,
  104. data: {
  105. userName: App.router.get('loginName'),
  106. sectionName: sectionName,
  107. urlParams: urlParams
  108. },
  109. success: 'getActiveWidgetLayoutSuccessCallback'
  110. });
  111. } else {
  112. this.set('isWidgetsLoaded', true);
  113. }
  114. },
  115. /**
  116. * success callback of <code>getActiveWidgetLayout()</code>
  117. * @param {object|null} data
  118. */
  119. getActiveWidgetLayoutSuccessCallback: function (data) {
  120. var self = this;
  121. if (data.items[0]) {
  122. self.getWidgetLayoutSuccessCallback(data);
  123. } else {
  124. self.getAllActiveWidgetLayouts().done(function (activeWidgetLayoutsData) {
  125. self.getDefaultWidgetLayoutByName(self.get('defaultLayoutName')).done(function (defaultWidgetLayoutData) {
  126. self.createUserWidgetLayout(defaultWidgetLayoutData).done(function (userLayoutIdData) {
  127. var activeWidgetLayouts;
  128. var widgetLayouts = [];
  129. if (!!activeWidgetLayoutsData.items.length) {
  130. widgetLayouts = activeWidgetLayoutsData.items.map(function (item) {
  131. return {
  132. "id": item.WidgetLayoutInfo.id
  133. }
  134. });
  135. }
  136. widgetLayouts.push({id: userLayoutIdData.resources[0].WidgetLayoutInfo.id});
  137. activeWidgetLayouts = {
  138. "WidgetLayouts": widgetLayouts
  139. };
  140. self.saveActiveWidgetLayouts(activeWidgetLayouts).done(function () {
  141. self.getActiveWidgetLayout();
  142. });
  143. });
  144. });
  145. });
  146. }
  147. },
  148. getAllActiveWidgetLayouts: function () {
  149. return App.ajax.send({
  150. name: 'widgets.layouts.all.active.get',
  151. sender: this,
  152. data: {
  153. userName: App.router.get('loginName')
  154. }
  155. });
  156. },
  157. /**
  158. * success callback of <code>getWidgetLayout()</code>
  159. * @param {object|null} data
  160. */
  161. getWidgetLayoutSuccessCallback: function (data) {
  162. if (data) {
  163. App.widgetMapper.map(data.items[0].WidgetLayoutInfo);
  164. App.widgetLayoutMapper.map(data);
  165. this.set('activeWidgetLayout', App.WidgetLayout.find().findProperty('id', data.items[0].WidgetLayoutInfo.id));
  166. this.set('isWidgetsLoaded', true);
  167. }
  168. },
  169. getDefaultWidgetLayoutByName: function (layoutName) {
  170. var urlParams = 'WidgetLayoutInfo/layout_name=' + layoutName;
  171. return App.ajax.send({
  172. name: 'widget.layout.get',
  173. sender: this,
  174. data: {
  175. urlParams: urlParams
  176. }
  177. });
  178. },
  179. createUserWidgetLayout: function (defaultWidgetLayoutData) {
  180. var layout = defaultWidgetLayoutData.items[0].WidgetLayoutInfo;
  181. var layoutName = this.get('userLayoutName');
  182. var data = {
  183. "WidgetLayoutInfo": {
  184. "display_name": layout.display_name,
  185. "layout_name": layoutName,
  186. "scope": "USER",
  187. "section_name": layout.section_name,
  188. "user_name": App.router.get('loginName'),
  189. "widgets": layout.widgets.map(function (widget) {
  190. return {
  191. "id": widget.WidgetInfo.id
  192. }
  193. })
  194. }
  195. };
  196. return App.ajax.send({
  197. name: 'widget.layout.create',
  198. sender: this,
  199. data: {
  200. data: data
  201. }
  202. });
  203. },
  204. saveActiveWidgetLayouts: function (activeWidgetLayouts) {
  205. return App.ajax.send({
  206. name: 'widget.activelayouts.edit',
  207. sender: this,
  208. data: {
  209. data: activeWidgetLayouts,
  210. userName: App.router.get('loginName')
  211. }
  212. });
  213. },
  214. /**
  215. * save layout after re-order widgets
  216. * return {$.ajax}
  217. */
  218. saveWidgetLayout: function (widgets) {
  219. var activeLayout = this.get('activeWidgetLayout');
  220. var data = {
  221. "WidgetLayoutInfo": {
  222. "display_name": activeLayout.get("displayName"),
  223. "id": activeLayout.get("id"),
  224. "layout_name": activeLayout.get("layoutName"),
  225. "scope": activeLayout.get("scope"),
  226. "section_name": activeLayout.get("sectionName"),
  227. "widgets": widgets.map(function (widget) {
  228. return {
  229. "id": widget.get('id')
  230. }
  231. })
  232. }
  233. };
  234. return App.ajax.send({
  235. name: 'widget.layout.edit',
  236. sender: this,
  237. data: {
  238. layoutId: activeLayout.get("id"),
  239. data: data
  240. }
  241. });
  242. }
  243. });