|
1 | | -(function($) { |
2 | | - 'use strict'; |
3 | | - var config_resource = AJS.contextPath() + "/rest/prnfb-admin/1.0/"; |
4 | | - $(document).ready(function() { |
5 | | - function getEmpties($headers) { |
6 | | - var empties = []; |
7 | | - $('.header', $headers).each(function(iheader, $header) { |
8 | | - var allValue = ""; |
9 | | - $('input[type="text"]', $header).each(function(iinput, $input) { |
10 | | - allValue += $input.value.trim(); |
11 | | - }); |
12 | | - if (allValue === "") { |
13 | | - empties.push($header); |
14 | | - } |
15 | | - }); |
16 | | - return empties; |
17 | | - } |
18 | | - |
19 | | - function adjustHeaders($headers) { |
20 | | - var empties = getEmpties($headers); |
21 | | - if (empties.length === 0) { |
22 | | - $headers.append($(".prnfb-template .header")[0].outerHTML); |
23 | | - } |
24 | | - |
25 | | - if (empties.length > 1) { |
26 | | - empties[1].remove(); |
27 | | - } |
28 | | - } |
29 | | - |
30 | | - function setEvents() { |
31 | | - $('input[name="delete"]').click(function(e) { |
32 | | - var $form = $(this).closest('form'); |
33 | | - var formIdentifier = $('input[name="FORM_IDENTIFIER"]', $form).val(); |
34 | | - $.ajax({ |
35 | | - url: config_resource + formIdentifier, |
36 | | - dataType: "json", |
37 | | - type: "DELETE", |
38 | | - error: function(xhr, data, error) { |
39 | | - console.log(xhr); |
40 | | - console.log(data); |
41 | | - console.log(error); |
42 | | - }, |
43 | | - success: function(data, text, xhr) { |
44 | | - $form.remove(); |
45 | | - } |
46 | | - }); |
47 | | - }); |
48 | | - |
49 | | - $('.expandable').each(function(index, el) { |
50 | | - var $element = $(el); |
51 | | - $element.find('.toggle').click(function() { |
52 | | - $element.toggleClass('expanded'); |
53 | | - }); |
54 | | - }); |
55 | | - //If there are only a few triggers configured, they can be expanded by default without confusion. |
56 | | - if ($('.expandable').length < 4) { |
57 | | - $('.expandable').addClass('expanded'); |
58 | | - } |
59 | | - |
60 | | - $('.headers').keyup(function(e) { |
61 | | - var $headers = $(this); |
62 | | - adjustHeaders($headers); |
63 | | - }); |
64 | | - |
65 | | - $('input[name="save"]').click(function(e) { |
66 | | - var $form = $(this).closest('form'); |
67 | | - $(".post", $form).html("Saving..."); |
68 | | - $.ajax({ |
69 | | - url: config_resource, |
70 | | - dataType: "json", |
71 | | - type: "POST", |
72 | | - contentType: "application/json", |
73 | | - data: JSON.stringify($form.serializeArray(), null, 2), |
74 | | - processData: false, |
75 | | - error: function(xhr, data, error) { |
76 | | - $(".error." + xhr.responseJSON.field, $form).html(xhr.responseJSON.error); |
77 | | - if (xhr.responseJSON.field) { |
78 | | - $(".post", $form).html("There were errors, form not saved!"); |
79 | | - } else { |
80 | | - $(".post", $form).html(xhr.responseText); |
81 | | - } |
82 | | - }, |
83 | | - success: function(data, text, xhr) { |
84 | | - getAll(); |
85 | | - } |
86 | | - }); |
87 | | - }); |
88 | | - } |
89 | | - |
90 | | - function addNewForm(formType) { |
91 | | - var $template = $(".prnfb-template-" + formType).clone(); |
92 | | - $('input[name="delete"]', $template).remove(); |
93 | | - $('input[name=method][value=GET]', $template).attr('checked', 'checked'); |
94 | | - $('.expandable', $template).addClass('expanded'); |
95 | | - $(".prnfb-" + formType).append($template.html()); |
96 | | - } |
| 1 | +define('plugin/prnfb/admin', [ |
| 2 | + 'jquery', |
| 3 | + 'aui', |
| 4 | + 'plugin/prnfb/utils' |
| 5 | +], function($, AJS, common) { |
97 | 6 |
|
98 | | - function getAll() { |
99 | | - $.ajax({ |
100 | | - url: config_resource, |
101 | | - dataType: "json" |
102 | | - }).done(function(configs) { |
103 | | - $(".prnfb-TRIGGER_CONFIG_FORM").html(""); |
104 | | - $(".prnfb-BUTTON_CONFIG_FORM").html(""); |
105 | | - $(".prnfb-GLOBAL_SETTINGS").html(""); |
106 | | - $.each(configs, function(index, config) { |
107 | | - var formType = 'TRIGGER_CONFIG_FORM'; |
108 | | - $.each(config, function(fieldIndex, field_map) { |
109 | | - if (field_map.name === 'FORM_TYPE') { |
110 | | - formType = field_map.value; |
111 | | - } |
112 | | - }); |
113 | | - |
114 | | - var $template = $(".prnfb-template-" + formType).clone(); |
115 | | - |
116 | | - $.each(config, function(fieldIndex, field_map) { |
117 | | - var safe_value = field_map.value.replace(/[^a-zA-Z\_]/g, ''); |
118 | | - $('.variable[data-variable="' + field_map.name + '"]', $template).html(field_map.value); |
119 | | - $('input[type="text"][name="' + field_map.name + '"]', $template).attr('value', field_map.value); |
120 | | - $('input[type="password"][name="' + field_map.name + '"]', $template).attr('value', field_map.value); |
121 | | - $('textarea[name="' + field_map.name + '"]', $template).text(field_map.value); |
122 | | - $('input[type="hidden"][name="' + field_map.name + '"]', $template).attr('value', field_map.value); |
123 | | - $('input[type="checkbox"][name="' + field_map.name + '"][value="' + safe_value + '"]', $template).attr('checked', 'checked'); |
124 | | - $('input[type="radio"][name="' + field_map.name + '"][value="' + safe_value + '"]', $template).attr('checked', 'checked'); |
125 | | - $('.visibleif.' + field_map.name + '_' + safe_value, $template).show(); |
126 | | - }); |
127 | | - |
128 | | - var header_names = []; |
129 | | - var header_values = []; |
130 | | - $.each(config, function(fieldIndex, field_map) { |
131 | | - if (field_map.name === 'header_name') { |
132 | | - header_names.push(field_map.value); |
133 | | - } else if (field_map.name === 'header_value') { |
134 | | - header_values.push(field_map.value); |
135 | | - } |
136 | | - }); |
137 | | - for (var i = 0; i < header_names.length; i++) { |
138 | | - $('input[type="text"][name="header_name"]', $template).last().attr('value', header_names[i]); |
139 | | - $('input[type="text"][name="header_value"]', $template).last().attr('value', header_values[i]); |
140 | | - adjustHeaders($(".headers", $template)); |
141 | | - } |
142 | | - |
143 | | - if (!$('input[name=method]:checked', $template).val()) { |
144 | | - $('input[name=method][value=GET]', $template).attr('checked', 'checked'); |
145 | | - } |
146 | | - |
147 | | - $(".prnfb-" + formType).append($template.html()); |
148 | | - }); |
149 | | - addNewForm('TRIGGER_CONFIG_FORM'); |
150 | | - addNewForm('BUTTON_CONFIG_FORM'); |
151 | | - if ($('[name="FORM_TYPE"][value="GLOBAL_SETTINGS"]').length < 2) { |
152 | | - addNewForm('GLOBAL_SETTINGS'); |
153 | | - } |
154 | | - setEvents(); |
155 | | - }); |
156 | | - } |
157 | | - |
158 | | - getAll(); |
| 7 | + $(document).ready(function() { |
| 8 | + var globalRepoAdminUrl = AJS.contextPath() + "/rest/prnfb-admin/1.0"; |
| 9 | + $.getJSON(globalRepoAdminUrl, function(data) { |
| 10 | + common.setupRepoSettingsForm(data); |
| 11 | + }); |
159 | 12 | }); |
160 | | -})(AJS.$ || jQuery); |
| 13 | +}); |
| 14 | + |
| 15 | +AJS.$(document).ready(function() { |
| 16 | + require('plugin/prnfb/admin'); |
| 17 | +}); |
0 commit comments