dashgrid.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import $ from 'jquery';
  3. import coreModule from 'app/core/core_module';
  4. import {DashboardModel, CELL_HEIGHT, CELL_VMARGIN} from '../model';
  5. import 'jquery-ui';
  6. import 'gridstack';
  7. import 'gridstack.jquery-ui';
  8. const template = `
  9. <div class="grid-stack">
  10. <dash-grid-item ng-repeat="panel in ctrl.row.panels track by panel.id"
  11. class="grid-stack-item"
  12. grid-ctrl="ctrl"
  13. panel="panel">
  14. <plugin-component type="panel" class="grid-stack-item-content">
  15. </plugin-component>
  16. </dash-grid-item>
  17. </div>
  18. `;
  19. export class GridCtrl {
  20. options: any;
  21. row: any;
  22. dashboard: any;
  23. panels: any;
  24. gridstack: any;
  25. gridElem: any;
  26. /** @ngInject */
  27. constructor(private $rootScope, private $element, private $timeout) {
  28. }
  29. init() {
  30. this.gridElem = this.$element.find('.grid-stack');
  31. this.gridstack = this.gridElem.gridstack({
  32. animate: true,
  33. cellHeight: CELL_HEIGHT,
  34. verticalMargin: CELL_VMARGIN,
  35. acceptWidgets: '.grid-stack-item',
  36. handle: '.panel-header'
  37. }).data('gridstack');
  38. this.gridElem.on('added', (e, items) => {
  39. for (let item of items) {
  40. this.onGridStackItemAdded(item);
  41. }
  42. });
  43. this.gridElem.on('removed', (e, items) => {
  44. for (let item of items) {
  45. this.onGridStackItemRemoved(item);
  46. }
  47. });
  48. this.gridElem.on('change', (e, items) => {
  49. this.$timeout(() => this.onGridStackItemsChanged(items), 50);
  50. });
  51. }
  52. onGridStackItemAdded(item) {
  53. console.log('item added', item);
  54. if (this.dashboard.tempPanel) {
  55. //this.gridstack.removeWidget(item.el, false);
  56. this.$timeout(() => {
  57. this.row.panels.push(this.dashboard.tempPanel);
  58. });
  59. }
  60. }
  61. onGridStackItemRemoved(item) {
  62. console.log('item removed', item.id);
  63. let panel = this.dashboard.getPanelById(parseInt(item.id));
  64. this.dashboard.tempPanel = panel;
  65. this.$timeout(() => {
  66. this.row.removePanel(panel, false);
  67. });
  68. }
  69. onGridStackItemsChanged(items) {
  70. for (let item of items) {
  71. var panel = this.dashboard.getPanelById(parseInt(item.id));
  72. panel.x = item.x;
  73. panel.y = item.y;
  74. panel.width = item.width;
  75. panel.height = item.height;
  76. }
  77. this.$rootScope.$broadcast('render');
  78. }
  79. bindItem(element) {
  80. if (this.gridstack) {
  81. this.gridstack.makeWidget(element);
  82. }
  83. }
  84. itemScopeDestroyed(element) {
  85. console.log('itemScopeDestroyed');
  86. if (this.gridstack) {
  87. this.gridstack.removeWidget(element, false);
  88. }
  89. }
  90. destroy() {
  91. this.gridstack.destroy();
  92. this.gridstack = null;
  93. }
  94. }
  95. /** @ngInject **/
  96. export function dashGrid($timeout) {
  97. return {
  98. restrict: 'E',
  99. template: template,
  100. controller: GridCtrl,
  101. bindToController: true,
  102. controllerAs: 'ctrl',
  103. scope: {
  104. row: "=",
  105. dashboard: "=",
  106. },
  107. link: function(scope, elem, attrs, ctrl) {
  108. $timeout(function() {
  109. ctrl.init();
  110. });
  111. scope.$on('$destroy', () => {
  112. ctrl.destroy();
  113. });
  114. }
  115. };
  116. }
  117. /** @ngInject **/
  118. export function dashGridItem($timeout, $rootScope) {
  119. return {
  120. restrict: "E",
  121. scope: {
  122. panel: '=',
  123. gridCtrl: '='
  124. },
  125. link: function (scope, element, attrs) {
  126. let gridCtrl = scope.gridCtrl;
  127. let panel = scope.panel;
  128. element.attr({
  129. 'data-gs-id': panel.id,
  130. 'data-gs-x': panel.x,
  131. 'data-gs-y': panel.y,
  132. 'data-gs-width': panel.width,
  133. 'data-gs-height': panel.height,
  134. });
  135. $rootScope.onAppEvent('panel-fullscreen-exit', (evt, payload) => {
  136. if (panel.id !== payload.panelId) {
  137. return;
  138. }
  139. element.removeClass('panel-fullscreen');
  140. }, scope);
  141. $rootScope.onAppEvent('panel-fullscreen-enter', (evt, payload) => {
  142. if (panel.id !== payload.panelId) {
  143. return;
  144. }
  145. element.addClass('panel-fullscreen');
  146. }, scope);
  147. scope.$on('$destroy', () => {
  148. gridCtrl.itemScopeDestroyed(element);
  149. });
  150. // scope.onItemRemoved({item: item});
  151. // ctrl.removeItem(element);
  152. //var item = element.data('_gridstack_node');
  153. //console.log('link item', item);
  154. //gridCtrl.bindItem(element);
  155. // element.bind('$destroy', function() {
  156. // var item = element.data('_gridstack_node');
  157. // scope.onItemRemoved({item: item});
  158. // ctrl.removeItem(element);
  159. // });
  160. }
  161. };
  162. }
  163. coreModule.directive('dashGrid', dashGrid);
  164. coreModule.directive('dashGridItem', dashGridItem);