panel-tools.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. /**
  3. * Add several features to panels.
  4. */
  5. app.directive('ctPaneltool', function () {
  6. var templates = {
  7. /* jshint multistr: true */
  8. collapse: "<a href='#' class='btn btn-transparent btn-sm' panel-collapse='' tooltip-placement='top' tooltip='Collapse' ng-click='{{panelId}} = !{{panelId}}' ng-init='{{panelId}}=false'>" + "<i ng-if='{{panelId}}' class='ti-plus'></i>" + "<i ng-if='!{{panelId}}' class='ti-minus'></i>" + "</a>",
  9. dismiss: "<a href='#' class='btn btn-transparent btn-sm' panel-dismiss='' tooltip-placement='top' tooltip='Close'>" + "<i class='ti-close'></i>" + "</a>",
  10. refresh: "<a href='#' class='btn btn-transparent btn-sm' panel-refresh='' tooltip-placement='top' tooltip='Refresh' data-spinner='{{spinner}}'>" + "<i class='ti-reload'></i>" + "</a>"
  11. };
  12. return {
  13. restrict: 'E',
  14. template: function (elem, attrs) {
  15. var temp = '';
  16. if (attrs.toolCollapse)
  17. temp += templates.collapse.replace(/{{panelId}}/g, (elem.parent().parent().attr('id')));
  18. if (attrs.toolDismiss)
  19. temp += templates.dismiss;
  20. if (attrs.toolRefresh)
  21. temp += templates.refresh.replace(/{{spinner}}/g, attrs.toolRefresh);
  22. return temp;
  23. }
  24. };
  25. });
  26. app.directive('panelDismiss', function () {
  27. 'use strict';
  28. return {
  29. restrict: 'A',
  30. controller: ["$scope", "$element", function ($scope, $element) {
  31. var removeEvent = 'panel-remove', removedEvent = 'panel-removed';
  32. $element.on('click', function () {
  33. var parent = $(this).closest('.panel');
  34. destroyPanel();
  35. function destroyPanel() {
  36. var col = parent.parent();
  37. parent.fadeOut(300, function () {
  38. $(this).remove();
  39. if (col.is('[class*="col-"]') && col.children('*').length === 0) {
  40. col.remove();
  41. }
  42. });
  43. }
  44. });
  45. }]
  46. };
  47. })
  48. .directive('panelRefresh', function () {
  49. 'use strict';
  50. return {
  51. restrict: 'A',
  52. controller: ["$scope", "$element", function ($scope, $element) {
  53. var refreshEvent = 'panel-refresh', csspinnerClass = 'csspinner', defaultSpinner = 'load1';
  54. // method to clear the spinner when done
  55. function removeSpinner() {
  56. this.removeClass(csspinnerClass);
  57. }
  58. // catch clicks to toggle panel refresh
  59. $element.on('click', function () {
  60. var $this = $(this), panel = $this.parents('.panel').eq(0), spinner = $this.data('spinner') || defaultSpinner;
  61. // start showing the spinner
  62. panel.addClass(csspinnerClass + ' ' + spinner);
  63. // attach as public method
  64. panel.removeSpinner = removeSpinner;
  65. // Trigger the event and send the panel object
  66. $this.trigger(refreshEvent, [panel]);
  67. });
  68. }]
  69. };
  70. });
  71. (function ($, window, document) {
  72. 'use strict';
  73. $(document).on('panel-refresh', '.panel', function (e, panel) {
  74. // perform any action when a .panel triggers a the refresh event
  75. setTimeout(function () {
  76. // when the action is done, just remove the spinner class
  77. panel.removeSpinner();
  78. }, 3000);
  79. });
  80. }(jQuery, window, document));