network-throttle.client.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. (function (angular) {
  2. const SECTION_NAME = "network-throttle";
  3. angular
  4. .module("BrowserSync")
  5. .controller("NetworkThrottleController", [
  6. "options",
  7. "pagesConfig",
  8. "Socket",
  9. "$scope",
  10. NetworkThrottleController
  11. ]);
  12. /**
  13. * @param options
  14. * @param pagesConfig
  15. * @param Socket
  16. * @param $scope
  17. */
  18. function NetworkThrottleController (options, pagesConfig, Socket, $scope) {
  19. var ctrl = this;
  20. ctrl.section = pagesConfig[SECTION_NAME];
  21. ctrl.options = options.bs;
  22. ctrl.uiOptions = options.ui;
  23. ctrl.clientFiles = options.ui.clientFiles || {};
  24. ctrl.section = pagesConfig[SECTION_NAME];
  25. ctrl.throttle = ctrl.uiOptions[SECTION_NAME];
  26. ctrl.selected = ctrl.throttle.targets[0].id;
  27. ctrl.servers = ctrl.throttle.servers;
  28. ctrl.port = "";
  29. ctrl.portEntry = "auto";
  30. ctrl.serverCount = Object.keys(ctrl.servers).length;
  31. ctrl.blurs = [];
  32. ctrl.state = {
  33. success: false,
  34. waiting: false,
  35. classname: "ready"
  36. };
  37. ctrl.createServer = function (selected, event) {
  38. if (ctrl.blurs.indexOf(event.target) === -1) {
  39. ctrl.blurs.push(event.target);
  40. }
  41. var item = getByProp(ctrl.throttle.targets, "id", ctrl.selected);
  42. if (ctrl.portEntry === "auto") {
  43. return send("");
  44. }
  45. if (!ctrl.port || !ctrl.port.length) {
  46. setError();
  47. return;
  48. }
  49. if (!ctrl.port.match(/\d{4,5}/)) {
  50. setError();
  51. return;
  52. }
  53. var port = parseInt(ctrl.port, 10);
  54. if (port < 1024 || port > 65535) {
  55. setError();
  56. return;
  57. }
  58. send(ctrl.port);
  59. function setError() {
  60. ctrl.state.waiting = false;
  61. ctrl.state.portError = true;
  62. }
  63. function send (port) {
  64. ctrl.state.classname = "waiting";
  65. ctrl.state.waiting = true;
  66. Socket.uiEvent({
  67. namespace: SECTION_NAME,
  68. event: "server:create",
  69. data: {
  70. speed: item,
  71. port: port
  72. }
  73. });
  74. }
  75. };
  76. ctrl.destroyServer = function (item, port) {
  77. Socket.uiEvent({
  78. namespace: SECTION_NAME,
  79. event: "server:destroy",
  80. data: {
  81. speed: item,
  82. port: port
  83. }
  84. });
  85. };
  86. ctrl.toggleSpeed = function (item) {
  87. if (!item.active) {
  88. item.urls = [];
  89. }
  90. };
  91. ctrl.update = function (data) {
  92. ctrl.servers = data.servers;
  93. ctrl.serverCount = Object.keys(ctrl.servers).length;
  94. if (data.event === "server:create") {
  95. updateButtonState();
  96. }
  97. $scope.$digest();
  98. };
  99. function updateButtonState() {
  100. ctrl.state.success = true;
  101. ctrl.state.classname = "success";
  102. setTimeout(function () {
  103. ctrl.blurs.forEach(function (elem) {
  104. elem.blur();
  105. });
  106. setTimeout(function () {
  107. ctrl.state.success = false;
  108. ctrl.state.waiting = false;
  109. ctrl.state.classname = "ready";
  110. $scope.$digest();
  111. }, 500);
  112. }, 300);
  113. }
  114. /**
  115. * @param collection
  116. * @param prop
  117. * @returns {*}
  118. */
  119. function getByProp (collection, prop, name) {
  120. var match = collection.filter(function (item) {
  121. return item[prop] === name;
  122. });
  123. if (match.length) {
  124. return match[0];
  125. }
  126. return false;
  127. }
  128. Socket.on("ui:network-throttle:update", ctrl.update);
  129. $scope.$on("$destroy", function () {
  130. Socket.off("ui:network-throttle:update", ctrl.update);
  131. });
  132. }
  133. /**
  134. * Display the snippet when in snippet mode
  135. */
  136. angular
  137. .module("BrowserSync")
  138. .directive("throttle", function () {
  139. return {
  140. restrict: "E",
  141. replace: true,
  142. scope: {
  143. "target": "=",
  144. "options": "="
  145. },
  146. templateUrl: "network-throttle.directive.html",
  147. controller: ["$scope", "Socket", throttleDirectiveControlller],
  148. controllerAs: "ctrl"
  149. };
  150. });
  151. /**
  152. * @param $scope
  153. */
  154. function throttleDirectiveControlller ($scope) {
  155. var ctrl = this;
  156. ctrl.throttle = $scope.options[SECTION_NAME];
  157. }
  158. })(angular);