exportCommandApiDialog.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. (function () {
  2. 'use strict';
  3. angular.module('ariaNg').directive('ngExportCommandApiDialog', ['clipboard', 'ariaNgCommonService', 'ariaNgNativeElectronService', function (clipboard, ariaNgCommonService, ariaNgNativeElectronService) {
  4. return {
  5. restrict: 'E',
  6. templateUrl: 'views/export-command-api-dialog.html',
  7. replace: true,
  8. scope: {
  9. options: '='
  10. },
  11. link: function (scope, element, attrs) {
  12. scope.context = {
  13. trueFalseOptions: [{name: 'Enabled', value: true}, {name: 'Disabled', value: false}],
  14. baseUrl: 'http://ariang.mayswind.net/latest',
  15. commandAPIUrl: null,
  16. pauseOnAdded: true,
  17. isCopied: false
  18. };
  19. var getBaseUrl = function () {
  20. var baseUrl = scope.context.baseUrl;
  21. if (baseUrl.indexOf('#') >= 0) {
  22. baseUrl = baseUrl.substring(0, baseUrl.indexOf('#'));
  23. }
  24. return baseUrl;
  25. };
  26. var getNewTaskCommandAPIUrl = function (task) {
  27. var commandAPIUrl = getBaseUrl() + '#!/new/task?' +
  28. 'url=' + ariaNgCommonService.base64UrlEncode(task.urls[0]);
  29. if (scope.context.pauseOnAdded) {
  30. commandAPIUrl += '&pause=true';
  31. }
  32. if (task.options) {
  33. for (var key in task.options) {
  34. if (!task.options.hasOwnProperty(key)) {
  35. continue;
  36. }
  37. commandAPIUrl += '&' + key + '=' + task.options[key];
  38. }
  39. }
  40. return commandAPIUrl;
  41. };
  42. var getNewTasksCommandAPIUrl = function (tasks) {
  43. var commandAPIUrls = '';
  44. for (var i = 0; i < tasks.length; i++) {
  45. if (i > 0) {
  46. commandAPIUrls += '\n';
  47. }
  48. commandAPIUrls += getNewTaskCommandAPIUrl(tasks[i]);
  49. }
  50. return commandAPIUrls;
  51. };
  52. var getSettingCommandAPIUrl = function (setting) {
  53. var commandAPIUrl = getBaseUrl() + '#!/settings/rpc/set?' +
  54. 'protocol=' + setting.protocol +
  55. '&host=' + setting.rpcHost +
  56. '&port=' + setting.rpcPort +
  57. '&interface=' + setting.rpcInterface;
  58. if (setting.secret) {
  59. commandAPIUrl += '&secret=' + ariaNgCommonService.base64UrlEncode(setting.secret);
  60. }
  61. return commandAPIUrl;
  62. };
  63. scope.generateCommandAPIUrl = function () {
  64. if (!scope.options) {
  65. return;
  66. }
  67. if (scope.options.type === 'new-task') {
  68. scope.context.commandAPIUrl = getNewTasksCommandAPIUrl(scope.options.data);
  69. } else if (scope.options.type === 'setting') {
  70. scope.context.commandAPIUrl = getSettingCommandAPIUrl(scope.options.data);
  71. }
  72. scope.context.isCopied = false;
  73. };
  74. scope.copyCommandAPI = function () {
  75. clipboard.copyText(scope.context.commandAPIUrl, {
  76. container: angular.element(element)[0]
  77. });
  78. scope.context.isCopied = true;
  79. };
  80. angular.element(element).on('show.bs.modal', function () {
  81. ariaNgNativeElectronService.updateTitleBarBackgroundColorWithModalOverlay();
  82. });
  83. angular.element(element).on('hide.bs.modal', function () {
  84. ariaNgNativeElectronService.updateTitleBarBackgroundColor();
  85. });
  86. angular.element(element).on('hidden.bs.modal', function () {
  87. scope.$apply(function () {
  88. scope.options = null;
  89. scope.context.commandAPIUrl = null;
  90. scope.context.isCopied = false;
  91. });
  92. });
  93. scope.$watch('options', function (options) {
  94. if (options) {
  95. scope.generateCommandAPIUrl();
  96. scope.context.isCopied = false;
  97. angular.element(element).modal('show');
  98. }
  99. }, true);
  100. }
  101. };
  102. }]);
  103. }());