widget_section.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. }
  87. }
  88. return [];
  89. }.property('isWidgetsLoaded'),
  90. /**
  91. * load widgets defined by user
  92. * @returns {$.ajax}
  93. */
  94. getActiveWidgetLayout: function () {
  95. var sectionName = this.get('sectionName');
  96. var urlParams = 'WidgetLayoutInfo/section_name=' + sectionName;
  97. this.set('activeWidgetLayout', {});
  98. this.set('isWidgetsLoaded', false);
  99. if (this.get('isServiceWithEnhancedWidgets')) {
  100. return App.ajax.send({
  101. name: 'widgets.layouts.active.get',
  102. sender: this,
  103. data: {
  104. userName: App.router.get('loginName'),
  105. sectionName: sectionName,
  106. urlParams: urlParams
  107. },
  108. success: 'getActiveWidgetLayoutSuccessCallback'
  109. });
  110. } else {
  111. this.set('isWidgetsLoaded', true);
  112. }
  113. },
  114. /**
  115. * success callback of <code>getActiveWidgetLayout()</code>
  116. * @param {object|null} data
  117. */
  118. getActiveWidgetLayoutSuccessCallback: function (data) {
  119. var self = this;
  120. if (data.items[0]) {
  121. self.getWidgetLayoutSuccessCallback(data);
  122. } else {
  123. self.getAllActiveWidgetLayouts().done(function (activeWidgetLayoutsData) {
  124. self.getDefaultWidgetLayoutByName(self.get('defaultLayoutName')).done(function (defaultWidgetLayoutData) {
  125. self.createUserWidgetLayout(defaultWidgetLayoutData).done(function (userLayoutIdData) {
  126. var activeWidgetLayouts;
  127. var widgetLayouts = [];
  128. if (!!activeWidgetLayoutsData.items.length) {
  129. widgetLayouts = activeWidgetLayoutsData.items.map(function (item) {
  130. return {
  131. "id": item.WidgetLayoutInfo.id
  132. }
  133. });
  134. }
  135. widgetLayouts.push({id: userLayoutIdData.resources[0].WidgetLayoutInfo.id});
  136. activeWidgetLayouts = {
  137. "WidgetLayouts": widgetLayouts
  138. };
  139. self.saveActiveWidgetLayouts(activeWidgetLayouts).done(function () {
  140. self.getActiveWidgetLayout();
  141. });
  142. });
  143. });
  144. });
  145. }
  146. },
  147. getAllActiveWidgetLayouts: function () {
  148. return App.ajax.send({
  149. name: 'widgets.layouts.all.active.get',
  150. sender: this,
  151. data: {
  152. userName: App.router.get('loginName')
  153. }
  154. });
  155. },
  156. /**
  157. * success callback of <code>getWidgetLayout()</code>
  158. * @param {object|null} data
  159. */
  160. getWidgetLayoutSuccessCallback: function (data) {
  161. if (data) {
  162. App.widgetMapper.map(data.items[0].WidgetLayoutInfo);
  163. App.widgetLayoutMapper.map(data);
  164. this.set('activeWidgetLayout', App.WidgetLayout.find().findProperty('id', data.items[0].WidgetLayoutInfo.id));
  165. this.set('isWidgetsLoaded', true);
  166. }
  167. },
  168. getDefaultWidgetLayoutByName: function (layoutName) {
  169. var urlParams = 'WidgetLayoutInfo/layout_name=' + layoutName;
  170. return App.ajax.send({
  171. name: 'widget.layout.get',
  172. sender: this,
  173. data: {
  174. urlParams: urlParams
  175. }
  176. });
  177. },
  178. createUserWidgetLayout: function (defaultWidgetLayoutData) {
  179. var layout = defaultWidgetLayoutData.items[0].WidgetLayoutInfo;
  180. var layoutName = this.get('userLayoutName');
  181. var data = {
  182. "WidgetLayoutInfo": {
  183. "display_name": layout.display_name,
  184. "layout_name": layoutName,
  185. "scope": "USER",
  186. "section_name": layout.section_name,
  187. "user_name": App.router.get('loginName'),
  188. "widgets": layout.widgets.map(function (widget) {
  189. return {
  190. "id": widget.WidgetInfo.id
  191. }
  192. })
  193. }
  194. };
  195. return App.ajax.send({
  196. name: 'widget.layout.create',
  197. sender: this,
  198. data: {
  199. data: data
  200. }
  201. });
  202. },
  203. saveActiveWidgetLayouts: function (activeWidgetLayouts) {
  204. return App.ajax.send({
  205. name: 'widget.activelayouts.edit',
  206. sender: this,
  207. data: {
  208. data: activeWidgetLayouts,
  209. userName: App.router.get('loginName')
  210. }
  211. });
  212. },
  213. /**
  214. * save layout after re-order widgets
  215. * return {$.ajax}
  216. */
  217. saveWidgetLayout: function (widgets) {
  218. var activeLayout = this.get('activeWidgetLayout');
  219. var data = {
  220. "WidgetLayoutInfo": {
  221. "display_name": activeLayout.get("displayName"),
  222. "id": activeLayout.get("id"),
  223. "layout_name": activeLayout.get("layoutName"),
  224. "scope": activeLayout.get("scope"),
  225. "section_name": activeLayout.get("sectionName"),
  226. "widgets": widgets.map(function (widget) {
  227. return {
  228. "id": widget.get('id')
  229. }
  230. })
  231. }
  232. };
  233. return App.ajax.send({
  234. name: 'widget.layout.edit',
  235. sender: this,
  236. data: {
  237. layoutId: activeLayout.get("id"),
  238. data: data
  239. }
  240. });
  241. }
  242. });