ref.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. Skip to content
  2. This repository
  3. Search
  4. Pull requests
  5. Issues
  6. Marketplace
  7. Gist
  8. @torkelo
  9. Sign out
  10. Unwatch 946
  11. Unstar 17,021
  12. Fork 2,862 grafana/grafana
  13. Code Issues 1,079 Pull requests 46 Projects 1 Wiki Settings Insights
  14. Branch: gridstack Find file Copy pathgrafana/public/app/core/components/dashgrid/dashgrid.ts
  15. a6bbcb8 on Jun 13
  16. @torkelo torkelo ux: gridstack poc
  17. 1 contributor
  18. RawBlameHistory
  19. 213 lines (181 sloc) 5.45 KB
  20. ///<reference path="../../../headers/common.d.ts" />
  21. import $ from 'jquery';
  22. import coreModule from '../../core_module';
  23. import 'jquery-ui';
  24. import 'gridstack';
  25. import 'gridstack.jquery-ui';
  26. const template = `
  27. <div gridstack gridstack-handler="ctrl.gridstack" class="grid-stack"
  28. options="ctrl.options"
  29. on-change="ctrl.onChange(event,items)"
  30. on-drag-start="ctrl.onDragStart(event,ui)"
  31. on-drag-stop="ctrl.onDragStop(event, ui)"
  32. on-resize-start="ctrl.onResizeStart(event, ui)"
  33. on-resize-stop="ctrl.onResizeStop(event, ui)">
  34. <div gridstack-item ng-repeat="panel in ctrl.panels"
  35. class="grid-stack-item"
  36. gs-item-id="panel.id"
  37. gs-item-x="panel.x"
  38. gs-item-y="panel.y"
  39. gs-item-width="panel.width"
  40. gs-item-height="panel.height"
  41. gs-item-autopos="1"
  42. on-item-added="ctrl.onItemAdded(item)"
  43. on-item-removed="ctrl.onItemRemoved(item)">
  44. <plugin-component type="panel" class="panel-margin grid-stack-item-content">
  45. </plugin-component>
  46. </div>
  47. </div>
  48. `;
  49. export class DashGridCtrl {
  50. options: any;
  51. /** @ngInject */
  52. constructor(private $rootScope) {
  53. this.options = {
  54. animate: true,
  55. };
  56. }
  57. onResizeStop() {
  58. this.$rootScope.$broadcast('render');
  59. }
  60. }
  61. export function dashGrid($timeout) {
  62. return {
  63. restrict: 'E',
  64. template: template,
  65. controller: DashGridCtrl,
  66. bindToController: true,
  67. controllerAs: 'ctrl',
  68. scope: {
  69. dashboard: "="
  70. },
  71. link: function(scope, elem, attrs, ctrl) {
  72. ctrl.panels = [];
  73. ctrl.dashboard.forEachPanel((panel, panelIndex, row, rowIndex) => {
  74. panel.width = 4;
  75. panel.height = 4;
  76. panel.x = panelIndex * 4;
  77. panel.y = rowIndex * 4;
  78. ctrl.panels.push(panel);
  79. });
  80. }
  81. };
  82. }
  83. /** @ngInject */
  84. coreModule.controller('GridstackController', ['$scope', function($scope) {
  85. var gridstack = null;
  86. this.init = function(element, options) {
  87. gridstack = element.gridstack(options).data('gridstack');
  88. return gridstack;
  89. };
  90. this.removeItem = function(element) {
  91. if (gridstack) {
  92. return gridstack.removeWidget(element, false);
  93. }
  94. return null;
  95. };
  96. this.addItem = function(element) {
  97. if (gridstack) {
  98. gridstack.makeWidget(element);
  99. return element;
  100. }
  101. return null;
  102. };
  103. }]);
  104. /** @ngInject */
  105. coreModule.directive('gridstack', ['$timeout', function($timeout) {
  106. return {
  107. restrict: "A",
  108. controller: 'GridstackController',
  109. scope: {
  110. onChange: '&',
  111. onDragStart: '&',
  112. onDragStop: '&',
  113. onResizeStart: '&',
  114. onResizeStop: '&',
  115. gridstackHandler: '=',
  116. options: '='
  117. },
  118. link: function (scope, element, attrs, controller, ngModel) {
  119. var gridstack = controller.init(element, scope.options);
  120. scope.gridstackHandler = gridstack;
  121. element.on('change', function (e, items) {
  122. $timeout(function() {
  123. scope.$apply();
  124. scope.onChange({event: e, items: items});
  125. });
  126. });
  127. element.on('dragstart', function(e, ui) {
  128. scope.onDragStart({event: e, ui: ui});
  129. });
  130. element.on('dragstop', function(e, ui) {
  131. $timeout(function() {
  132. scope.$apply();
  133. scope.onDragStop({event: e, ui: ui});
  134. });
  135. });
  136. element.on('resizestart', function(e, ui) {
  137. scope.onResizeStart({event: e, ui: ui});
  138. });
  139. element.on('resizestop', function(e, ui) {
  140. $timeout(function() {
  141. scope.$apply();
  142. scope.onResizeStop({event: e, ui: ui});
  143. });
  144. });
  145. }
  146. };
  147. }]);
  148. /** @ngInject */
  149. coreModule.directive('gridstackItem', ['$timeout', function($timeout) {
  150. return {
  151. restrict: "A",
  152. controller: 'GridstackController',
  153. require: '^gridstack',
  154. scope: {
  155. gridstackItem: '=',
  156. onItemAdded: '&',
  157. onItemRemoved: '&',
  158. gsItemId: '=',
  159. gsItemX: '=',
  160. gsItemY: '=',
  161. gsItemWidth: '=',
  162. gsItemHeight: '=',
  163. gsItemAutopos: '='
  164. },
  165. link: function (scope, element, attrs, controller) {
  166. $(element).attr('data-gs-id', scope.gsItemId);
  167. $(element).attr('data-gs-x', scope.gsItemX);
  168. $(element).attr('data-gs-y', scope.gsItemY);
  169. $(element).attr('data-gs-width', scope.gsItemWidth);
  170. $(element).attr('data-gs-height', scope.gsItemHeight);
  171. $(element).attr('data-gs-auto-position', scope.gsItemAutopos);
  172. var widget = controller.addItem(element);
  173. var item = element.data('_gridstack_node');
  174. $timeout(function() {
  175. scope.onItemAdded({item: item});
  176. });
  177. scope.$watch(function () { return $(element).attr('data-gs-id'); }, function (val) {
  178. scope.gsItemId = val;
  179. });
  180. scope.$watch(function(){ return $(element).attr('data-gs-x'); }, function(val) {
  181. scope.gsItemX = val;
  182. });
  183. scope.$watch(function(){ return $(element).attr('data-gs-y'); }, function(val) {
  184. scope.gsItemY = val;
  185. });
  186. scope.$watch(function(){ return $(element).attr('data-gs-width'); }, function(val) {
  187. scope.gsItemWidth = val;
  188. });
  189. scope.$watch(function(){ return $(element).attr('data-gs-height'); }, function(val) {
  190. scope.gsItemHeight = val;
  191. });
  192. element.bind('$destroy', function() {
  193. var item = element.data('_gridstack_node');
  194. scope.onItemRemoved({item: item});
  195. controller.removeItem(element);
  196. });
  197. }
  198. };
  199. }]);
  200. coreModule.directive('dashGrid', dashGrid);
  201. Contact GitHub API Training Shop Blog About
  202. © 2017 GitHub, Inc. Terms Privacy Security Status Help