1
0

agent-edit-page.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. (function () {
  2. let formatAgentForSelect = undefined;
  3. const Cls = (this.AgentEditPage = class AgentEditPage {
  4. constructor() {
  5. this.invokeDryRun = this.invokeDryRun.bind(this);
  6. $("#agent_source_ids").on("change", this.showEventDescriptions);
  7. this.showCorrectRegionsOnStartup();
  8. $("form.agent-form").on("submit", () => this.updateFromEditors());
  9. // Validate agents_options Json on form submit
  10. $("form.agent-form").submit(function (e) {
  11. for (const textarea of $("textarea.live-json-editor").toArray()) {
  12. try {
  13. JSON.parse($(textarea).val());
  14. } catch (err) {
  15. e.preventDefault();
  16. alert(
  17. "Sorry, there appears to be an error in your JSON input. Please fix it before continuing."
  18. );
  19. return false;
  20. }
  21. }
  22. if (
  23. $(".link-region").length &&
  24. $(".link-region").data("can-receive-events") === false
  25. ) {
  26. $(".link-region .select2-linked-tags option:selected").removeAttr(
  27. "selected"
  28. );
  29. }
  30. if (
  31. $(".control-link-region").length &&
  32. $(".control-link-region").data("can-control-other-agents") === false
  33. ) {
  34. $(
  35. ".control-link-region .select2-linked-tags option:selected"
  36. ).removeAttr("selected");
  37. }
  38. if (
  39. $(".event-related-region").length &&
  40. $(".event-related-region").data("can-create-events") === false
  41. ) {
  42. return $(
  43. ".event-related-region .select2-linked-tags option:selected"
  44. ).removeAttr("selected");
  45. }
  46. });
  47. $("#agent_name").each(function () {
  48. // Select the number suffix if this is a cloned agent.
  49. let matches;
  50. if ((matches = this.value.match(/ \(\d+\)$/))) {
  51. this.focus();
  52. if (this.selectionStart != null) {
  53. this.selectionStart = matches.index;
  54. return (this.selectionEnd = this.value.length);
  55. }
  56. }
  57. });
  58. // The type selector is only available on the new agent form.
  59. if ($("#agent_type").length) {
  60. $("#agent_type").on("change", () => this.handleTypeChange(false));
  61. this.handleTypeChange(true);
  62. // Update the dropdown to match agent description as well as agent name
  63. $("select#agent_type").select2({
  64. width: "resolve",
  65. templateResult: (agent) => {
  66. if (!agent.element || !agent.title) return agent.text;
  67. return [
  68. ...$(document.createElement('strong')).text(agent.text),
  69. document.createElement('br'),
  70. ...$.parseHTML(agent.title),
  71. ];
  72. },
  73. matcher: (params, data) => {
  74. const term = params.term;
  75. if (term == null) return data;
  76. const upperTerm = term.toUpperCase();
  77. return data.text.toUpperCase().indexOf(upperTerm) >= 0 ||
  78. data.title.toUpperCase().indexOf(upperTerm) >= 0
  79. ? data
  80. : null;
  81. },
  82. });
  83. } else {
  84. this.enableDryRunButton();
  85. this.buildAce();
  86. }
  87. }
  88. handleTypeChange(firstTime) {
  89. $(".event-descriptions").html("").hide();
  90. const type = $("#agent_type").val();
  91. if (type === "Agent") {
  92. $(".agent-settings").hide();
  93. return $(".description").hide();
  94. } else {
  95. $(".agent-settings").show();
  96. $("#agent-spinner").fadeIn();
  97. if (!firstTime) {
  98. $(".model-errors").hide();
  99. }
  100. return $.getJSON("/agents/type_details", { type }, (json) => {
  101. if (json.can_be_scheduled) {
  102. if (firstTime) {
  103. this.showSchedule();
  104. } else {
  105. this.showSchedule(json.default_schedule);
  106. }
  107. } else {
  108. this.hideSchedule();
  109. }
  110. if (json.can_receive_events) {
  111. this.showLinks();
  112. } else {
  113. this.hideLinks();
  114. }
  115. if (json.can_control_other_agents) {
  116. this.showControlLinks();
  117. } else {
  118. this.hideControlLinks();
  119. }
  120. if (json.can_create_events) {
  121. this.showEventCreation();
  122. } else {
  123. this.hideEventCreation();
  124. }
  125. if (json.description_html != null) {
  126. $(".description").show().html(json.description_html);
  127. }
  128. if (!firstTime) {
  129. if (json.oauthable != null) {
  130. $(".oauthable-form").html(json.oauthable);
  131. }
  132. if (json.form_options != null) {
  133. $(".agent-options").html(json.form_options);
  134. }
  135. window.jsonEditor = setupJsonEditor()[0];
  136. }
  137. this.enableDryRunButton();
  138. this.buildAce();
  139. window.initializeFormCompletable();
  140. return $("#agent-spinner").stop(true, true).fadeOut();
  141. });
  142. }
  143. }
  144. hideSchedule() {
  145. $(".schedule-region .can-be-scheduled").hide();
  146. return $(".schedule-region .cannot-be-scheduled").show();
  147. }
  148. showSchedule(defaultSchedule = null) {
  149. if (defaultSchedule != null) {
  150. $(".schedule-region select").val(defaultSchedule).change();
  151. }
  152. $(".schedule-region .can-be-scheduled").show();
  153. return $(".schedule-region .cannot-be-scheduled").hide();
  154. }
  155. hideLinks() {
  156. $(".link-region .select2-container").hide();
  157. $(".link-region .propagate-immediately").hide();
  158. $(".link-region .cannot-receive-events").show();
  159. return $(".link-region").data("can-receive-events", false);
  160. }
  161. showLinks() {
  162. $(".link-region .select2-container").show();
  163. $(".link-region .propagate-immediately").show();
  164. $(".link-region .cannot-receive-events").hide();
  165. $(".link-region").data("can-receive-events", true);
  166. return this.showEventDescriptions();
  167. }
  168. hideControlLinks() {
  169. $(".control-link-region").hide();
  170. return $(".control-link-region").data("can-control-other-agents", false);
  171. }
  172. showControlLinks() {
  173. $(".control-link-region").show();
  174. return $(".control-link-region").data("can-control-other-agents", true);
  175. }
  176. hideEventCreation() {
  177. $(".event-related-region .select2-container").hide();
  178. $(".event-related-region .cannot-create-events").show();
  179. return $(".event-related-region").data("can-create-events", false);
  180. }
  181. showEventCreation() {
  182. $(".event-related-region .select2-container").show();
  183. $(".event-related-region .cannot-create-events").hide();
  184. return $(".event-related-region").data("can-create-events", true);
  185. }
  186. showEventDescriptions() {
  187. if ($("#agent_source_ids").val()) {
  188. return $.getJSON(
  189. "/agents/event_descriptions",
  190. { ids: $("#agent_source_ids").val().join(",") },
  191. (json) => {
  192. if (json.description_html != null) {
  193. return $(".event-descriptions")
  194. .show()
  195. .html(json.description_html);
  196. } else {
  197. return $(".event-descriptions").hide();
  198. }
  199. }
  200. );
  201. } else {
  202. return $(".event-descriptions").html("").hide();
  203. }
  204. }
  205. showCorrectRegionsOnStartup() {
  206. if ($(".schedule-region")) {
  207. if ($(".schedule-region").data("can-be-scheduled") === true) {
  208. this.showSchedule();
  209. } else {
  210. this.hideSchedule();
  211. }
  212. }
  213. if ($(".link-region")) {
  214. if ($(".link-region").data("can-receive-events") === true) {
  215. this.showLinks();
  216. } else {
  217. this.hideLinks();
  218. }
  219. }
  220. if ($(".control-link-region")) {
  221. if (
  222. $(".control-link-region").data("can-control-other-agents") === true
  223. ) {
  224. this.showControlLinks();
  225. } else {
  226. this.hideControlLinks();
  227. }
  228. }
  229. if ($(".event-related-region")) {
  230. if ($(".event-related-region").data("can-create-events") === true) {
  231. return this.showEventCreation();
  232. } else {
  233. return this.hideEventCreation();
  234. }
  235. }
  236. }
  237. buildAce() {
  238. return $(".ace-editor").each(function () {
  239. if (!$(this).data("initialized")) {
  240. const $this = $(this);
  241. $this.data("initialized", true);
  242. const $source = $($this.data("source")).hide();
  243. const editor = ace.edit(this);
  244. $this.data("ace-editor", editor);
  245. const session = editor.getSession();
  246. session.setTabSize(2);
  247. session.setUseSoftTabs(true);
  248. session.setUseWrapMode(false);
  249. const setSyntax = function () {
  250. let mode, theme;
  251. if ((mode = $this.data("mode"))) {
  252. session.setMode("ace/mode/" + mode);
  253. }
  254. if ((theme = $this.data("theme"))) {
  255. editor.setTheme("ace/theme/" + theme);
  256. }
  257. if ((mode = $("[name='agent[options][language]']").val())) {
  258. switch (mode) {
  259. case "JavaScript":
  260. return session.setMode("ace/mode/javascript");
  261. case "CoffeeScript":
  262. return session.setMode("ace/mode/coffee");
  263. default:
  264. return session.setMode("ace/mode/" + mode);
  265. }
  266. }
  267. };
  268. $("[name='agent[options][language]']").on("change", setSyntax);
  269. setSyntax();
  270. return session.setValue($source.val());
  271. }
  272. });
  273. }
  274. updateFromEditors() {
  275. return $(".ace-editor").each(function () {
  276. const $source = $($(this).data("source"));
  277. return $source.val($(this).data("ace-editor").getSession().getValue());
  278. });
  279. }
  280. enableDryRunButton() {
  281. return $(".agent-dry-run-button")
  282. .prop("disabled", false)
  283. .off()
  284. .on("click", this.invokeDryRun);
  285. }
  286. disableDryRunButton() {
  287. return $(".agent-dry-run-button").prop("disabled", true);
  288. }
  289. invokeDryRun(e) {
  290. e.preventDefault();
  291. this.updateFromEditors();
  292. return Utils.handleDryRunButton(e.currentTarget);
  293. }
  294. });
  295. return Cls;
  296. })();
  297. $(() => Utils.registerPage(AgentEditPage, { forPathsMatching: /^agents/ }));