row_ctrl.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import config from 'app/core/config';
  4. import {coreModule, appEvents} from 'app/core/core';
  5. import './options';
  6. import './add_panel';
  7. export class DashRowCtrl {
  8. dashboard: any;
  9. row: any;
  10. dropView: number;
  11. /** @ngInject */
  12. constructor(private $scope, private $rootScope, private $timeout, private uiSegmentSrv, private $q) {
  13. this.row.title = this.row.title || 'Row title';
  14. if (this.row.isNew) {
  15. this.dropView = 1;
  16. delete this.row.isNew;
  17. }
  18. }
  19. onDrop(panelId, dropTarget) {
  20. var dragObject;
  21. // if string it's a panel type
  22. if (_.isString(panelId)) {
  23. // setup new panel
  24. dragObject = {
  25. row: this.row,
  26. panel: {
  27. title: config.new_panel_title,
  28. type: panelId,
  29. id: this.dashboard.getNextPanelId(),
  30. },
  31. isNew: true,
  32. };
  33. } else {
  34. dragObject = this.dashboard.getPanelInfoById(panelId);
  35. }
  36. if (dropTarget) {
  37. dropTarget = this.dashboard.getPanelInfoById(dropTarget.id);
  38. // if draging new panel onto existing panel split it
  39. if (dragObject.isNew) {
  40. dragObject.panel.span = dropTarget.panel.span = dropTarget.panel.span/2;
  41. // insert after
  42. dropTarget.row.panels.splice(dropTarget.index+1, 0, dragObject.panel);
  43. } else if (this.row === dragObject.row) {
  44. // just move element
  45. this.row.movePanel(dragObject.index, dropTarget.index);
  46. } else {
  47. // split drop target space
  48. dragObject.panel.span = dropTarget.panel.span = dropTarget.panel.span/2;
  49. // insert after
  50. dropTarget.row.panels.splice(dropTarget.index+1, 0, dragObject.panel);
  51. // remove from source row
  52. dragObject.row.removePanel(dragObject.panel, false);
  53. }
  54. } else {
  55. dragObject.panel.span = 12 - this.row.span;
  56. this.row.panels.push(dragObject.panel);
  57. // if not new remove from source row
  58. if (!dragObject.isNew) {
  59. dragObject.row.removePanel(dragObject.panel, false);
  60. }
  61. }
  62. this.dropView = 0;
  63. this.row.panelSpanChanged();
  64. this.$timeout(() => {
  65. this.$rootScope.$broadcast('render');
  66. });
  67. }
  68. setHeight(height) {
  69. this.row.height = height;
  70. this.$scope.$broadcast('render');
  71. }
  72. moveRow(direction) {
  73. var rowsList = this.dashboard.rows;
  74. var rowIndex = _.indexOf(rowsList, this.row);
  75. var newIndex = rowIndex + direction;
  76. if (newIndex >= 0 && newIndex <= (rowsList.length - 1)) {
  77. _.move(rowsList, rowIndex, newIndex);
  78. }
  79. }
  80. toggleCollapse() {
  81. this.closeDropView();
  82. this.row.collapse = !this.row.collapse;
  83. }
  84. showAddPanel() {
  85. this.row.collapse = false;
  86. if (this.dropView === 1) {
  87. this.closeDropView();
  88. } else {
  89. this.dropView = 1;
  90. }
  91. }
  92. showRowOptions() {
  93. if (this.dropView === 2) {
  94. this.closeDropView();
  95. } else {
  96. this.dropView = 2;
  97. }
  98. }
  99. closeDropView() {
  100. this.dropView = 0;
  101. }
  102. onMenuDeleteRow() {
  103. this.dashboard.removeRow(this.row);
  104. }
  105. }
  106. coreModule.directive('dashRow', function($rootScope) {
  107. return {
  108. restrict: 'E',
  109. templateUrl: 'public/app/features/dashboard/row/row.html',
  110. controller: DashRowCtrl,
  111. bindToController: true,
  112. controllerAs: 'ctrl',
  113. scope: {
  114. dashboard: "=",
  115. row: "=",
  116. },
  117. link: function(scope, element) {
  118. scope.$watchGroup(['ctrl.row.collapse', 'ctrl.row.height'], function() {
  119. element.find('.panels-wrapper').css({minHeight: scope.ctrl.row.collapse ? '5px' : scope.ctrl.row.height});
  120. });
  121. $rootScope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  122. var hasPanel = _.find(scope.ctrl.row.panels, {id: info.panelId});
  123. if (!hasPanel) {
  124. element.hide();
  125. }
  126. }, scope);
  127. $rootScope.onAppEvent('panel-fullscreen-exit', function() {
  128. element.show();
  129. }, scope);
  130. }
  131. };
  132. });
  133. coreModule.directive('panelWidth', function($rootScope) {
  134. return function(scope, element) {
  135. var fullscreen = false;
  136. function updateWidth() {
  137. if (!fullscreen) {
  138. element[0].style.width = ((scope.panel.span / 1.2) * 10) + '%';
  139. }
  140. }
  141. $rootScope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  142. fullscreen = true;
  143. if (scope.panel.id !== info.panelId) {
  144. element.hide();
  145. } else {
  146. element[0].style.width = '100%';
  147. }
  148. }, scope);
  149. $rootScope.onAppEvent('panel-fullscreen-exit', function(evt, info) {
  150. fullscreen = false;
  151. if (scope.panel.id !== info.panelId) {
  152. element.show();
  153. }
  154. updateWidth();
  155. }, scope);
  156. scope.$watch('panel.span', updateWidth);
  157. if (fullscreen) {
  158. element.hide();
  159. }
  160. };
  161. });
  162. coreModule.directive('panelDropZone', function($timeout) {
  163. return function(scope, element) {
  164. var row = scope.ctrl.row;
  165. var dashboard = scope.ctrl.dashboard;
  166. var indrag = false;
  167. var textEl = element.find('.panel-drop-zone-text');
  168. function showPanel(span, text) {
  169. element.find('.panel-container').css('height', row.height);
  170. element[0].style.width = ((span / 1.2) * 10) + '%';
  171. textEl.text(text);
  172. element.show();
  173. }
  174. function hidePanel() {
  175. element.hide();
  176. }
  177. function updateState() {
  178. if (scope.ctrl.dashboard.editMode) {
  179. if (row.panels.length === 0 && indrag === false) {
  180. return showPanel(12, 'Empty Space');
  181. }
  182. var dropZoneSpan = 12 - row.span;
  183. if (dropZoneSpan > 0) {
  184. if (indrag) {
  185. return showPanel(dropZoneSpan, 'Drop Here');
  186. } else {
  187. return showPanel(dropZoneSpan, 'Empty Space');
  188. }
  189. }
  190. }
  191. if (indrag === true) {
  192. var dropZoneSpan = 12 - row.span;
  193. if (dropZoneSpan > 1) {
  194. return showPanel(dropZoneSpan, 'Drop Here');
  195. }
  196. }
  197. hidePanel();
  198. }
  199. row.events.on('span-changed', updateState, scope);
  200. dashboard.events.emit('edit-mode-changed', updateState, scope);
  201. scope.$on("ANGULAR_DRAG_START", function() {
  202. indrag = true;
  203. updateState();
  204. });
  205. scope.$on("ANGULAR_DRAG_END", function() {
  206. indrag = false;
  207. updateState();
  208. });
  209. };
  210. });