blob: 90d83504aed59fe31c8aa3c9198f4e22374c690c [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001$(document).ready(function() {
2 // mailcow alert box generator
3 window.mailcow_alert_box = function(message, type) {
4 msg = $('<span/>').text(message).text();
5 if (type == 'danger' || type == 'info') {
6 auto_hide = 0;
7 $('#' + localStorage.getItem("add_modal")).modal('show');
8 localStorage.removeItem("add_modal");
9 } else {
10 auto_hide = 5000;
11 }
12 $.notify({message: msg},{z_index: 20000, delay: auto_hide, type: type,placement: {from: "bottom",align: "right"},animate: {enter: 'animated fadeInUp',exit: 'animated fadeOutDown'}});
13 }
14
15 $(".generate_password").click(function( event ) {
16 event.preventDefault();
17 $('[data-hibp]').trigger('input');
18 if (typeof($(this).closest("form").data('pwgen-length')) == "number") {
19 var random_passwd = GPW.pronounceable($(this).closest("form").data('pwgen-length'))
20 }
21 else {
22 var random_passwd = GPW.pronounceable(8)
23 }
24 $(this).closest("form").find('[data-pwgen-field]').attr('type', 'text');
25 $(this).closest("form").find('[data-pwgen-field]').val(random_passwd);
26 });
27 function str_rot13(str) {
28 return (str + '').replace(/[a-z]/gi, function(s){
29 return String.fromCharCode(s.charCodeAt(0) + (s.toLowerCase() < 'n' ? 13 : -13))
30 })
31 }
32 $(".rot-enc").html(function(){
33 return str_rot13($(this).html())
34 });
35 // https://stackoverflow.com/questions/4399005/implementing-jquerys-shake-effect-with-animate
36 function shake(div,interval,distance,times) {
37 if(typeof interval === 'undefined') {
38 interval = 100;
39 }
40 if(typeof distance === 'undefined') {
41 distance = 10;
42 }
43 if(typeof times === 'undefined') {
44 times = 4;
45 }
46 $(div).css('position','relative');
47 for(var iter=0;iter<(times+1);iter++){
48 $(div).animate({ left: ((iter%2==0 ? distance : distance*-1))}, interval);
49 }
50 $(div).animate({ left: 0},interval);
51 }
52
53 // form cache
54 $('[data-cached-form="true"]').formcache({key: $(this).data('id')});
55
56 // tooltips
57 $(function () {
58 $('[data-toggle="tooltip"]').tooltip()
59 });
60
61 // remember last navigation pill
62 (function () {
63 'use strict';
64 if ($('a[data-toggle="tab"]').length) {
65 $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
66 if ($(this).data('dont-remember') == 1) {
67 return true;
68 }
69 var id = $(this).parents('[role="tablist"]').attr('id');
70 var key = 'lastTag';
71 if (id) {
72 key += ':' + id;
73 }
74 localStorage.setItem(key, $(e.target).attr('href'));
75 });
76 $('[role="tablist"]').each(function (idx, elem) {
77 var id = $(elem).attr('id');
78 var key = 'lastTag';
79 if (id) {
80 key += ':' + id;
81 }
82 var lastTab = localStorage.getItem(key);
83 if (lastTab) {
84 $('[href="' + lastTab + '"]').tab('show');
85 }
86 });
87 }
88 })();
89
90 // IE fix to hide scrollbars when table body is empty
91 $('tbody').filter(function (index) {
92 return $(this).children().length < 1;
93 }).remove();
94
95 // selectpicker
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020096 $('select').selectpicker({
97 'styleBase': 'btn btn-xs-lg',
98 'noneSelectedText': lang_footer.nothing_selected
99 });
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100100
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200101 // haveibeenpwned and passwd policy
102 $.ajax({
103 url: '/api/v1/get/passwordpolicy/html',
104 type: 'GET',
105 success: function(res) {
106 $(".hibp-out").after(res);
107 }
108 });
109 $('[data-hibp]').after('<p class="small haveibeenpwned"><i class="bi bi-shield-fill-exclamation"></i> ' + lang_footer.hibp_check + '</p><span class="hibp-out"></span>');
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100110 $('[data-hibp]').on('input', function() {
111 out_field = $(this).next('.haveibeenpwned').next('.hibp-out').text('').attr('class', 'hibp-out');
112 });
113 $('.haveibeenpwned:not(.task-running)').on('click', function() {
114 var hibp_field = $(this)
115 $(hibp_field).addClass('task-running');
116 var hibp_result = $(hibp_field).next('.hibp-out')
117 var password_field = $(this).prev('[data-hibp]')
118 if ($(password_field).val() == '') {
119 shake(password_field);
120 }
121 else {
122 $(hibp_result).attr('class', 'hibp-out label label-info');
123 $(hibp_result).text(lang_footer.loading);
124 var password_digest = $.sha1($(password_field).val())
125 var digest_five = password_digest.substring(0, 5).toUpperCase();
126 var queryURL = "https://api.pwnedpasswords.com/range/" + digest_five;
127 var compl_digest = password_digest.substring(5, 41).toUpperCase();
128 $.ajax({
129 url: queryURL,
130 type: 'GET',
131 success: function(res) {
132 if (res.search(compl_digest) > -1){
133 $(hibp_result).removeClass('label label-info').addClass('label label-danger');
134 $(hibp_result).text(lang_footer.hibp_nok)
135 } else {
136 $(hibp_result).removeClass('label label-info').addClass('label label-success');
137 $(hibp_result).text(lang_footer.hibp_ok)
138 }
139 $(hibp_field).removeClass('task-running');
140 },
141 error: function(xhr, status, error) {
142 $(hibp_result).removeClass('label label-info').addClass('label label-warning');
143 $(hibp_result).text('API error: ' + xhr.responseText)
144 $(hibp_field).removeClass('task-running');
145 }
146 });
147 }
148 });
149
150 // Disable disallowed inputs
151 $('[data-acl="0"]').each(function(event){
152 if ($(this).is("a")) {
153 $(this).removeAttr("data-toggle");
154 $(this).removeAttr("data-target");
155 $(this).removeAttr("data-action");
156 $(this).click(function(event) {
157 event.preventDefault();
158 });
159 }
160 if ($(this).is("select")) {
161 $(this).selectpicker('destroy');
162 $(this).replaceWith(function() {
163 return '<label class="control-label"><b>' + this.innerText + '</b></label>';
164 });
165 }
166 if ($(this).hasClass('btn-group')) {
167 $(this).find('a').each(function(){
168 $(this).removeClass('dropdown-toggle')
169 .removeAttr('data-toggle')
170 .removeAttr('data-target')
171 .removeAttr('data-action')
172 .removeAttr('id')
173 .attr("disabled", true);
174 $(this).click(function(event) {
175 event.preventDefault();
176 return;
177 });
178 });
179 $(this).find('button').each(function() {
180 $(this).attr("disabled", true);
181 });
182 } else if ($(this).hasClass('input-group')) {
183 $(this).find('input').each(function() {
184 $(this).removeClass('dropdown-toggle')
185 .removeAttr('data-toggle')
186 .attr("disabled", true);
187 $(this).click(function(event) {
188 event.preventDefault();
189 });
190 });
191 $(this).find('button').each(function() {
192 $(this).attr("disabled", true);
193 });
194 } else if ($(this).hasClass('form-group')) {
195 $(this).find('input').each(function() {
196 $(this).attr("disabled", true);
197 });
198 } else if ($(this).hasClass('btn')) {
199 $(this).attr("disabled", true);
200 } else if ($(this).attr('data-provide') == 'slider') {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200201 $(this).attr('disabled', true);
202 } else if ($(this).is(':checkbox')) {
203 $(this).attr("disabled", true);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100204 }
205 $(this).data("toggle", "tooltip");
206 $(this).attr("title", lang_acl.prohibited);
207 $(this).tooltip();
208 });
209
210 // disable submit after submitting form (not API driven buttons)
211 $('form').submit(function() {
212 if ($('form button[type="submit"]').data('submitted') == '1') {
213 return false;
214 } else {
215 $(this).find('button[type="submit"]').first().text(lang_footer.loading);
216 $('form button[type="submit"]').attr('data-submitted', '1');
217 function disableF5(e) { if ((e.which || e.keyCode) == 116 || (e.which || e.keyCode) == 82) e.preventDefault(); };
218 $(document).on("keydown", disableF5);
219 }
220 });
221 // Textarea line numbers
222 $(".textarea-code").numberedtextarea({allowTabChar: true});
223 // trigger container restart
224 $('#RestartContainer').on('show.bs.modal', function(e) {
225 var container = $(e.relatedTarget).data('container');
226 $('#containerName').text(container);
227 $('#triggerRestartContainer').click(function(){
228 $(this).prop("disabled",true);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200229 $(this).html('<i class="bi bi-arrow-repeat icon-spin"></i> ');
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100230 $('#statusTriggerRestartContainer').html(lang_footer.restarting_container);
231 $.ajax({
232 method: 'get',
233 url: '/inc/ajax/container_ctrl.php',
234 timeout: docker_timeout,
235 data: {
236 'service': container,
237 'action': 'restart'
238 }
239 })
240 .always( function (data, status) {
241 $('#statusTriggerRestartContainer').append(data);
242 var htmlResponse = $.parseHTML(data)
243 if ($(htmlResponse).find('span').hasClass('text-success')) {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200244 $('#triggerRestartContainer').html('<i class="bi bi-check-lg"></i> ');
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100245 setTimeout(function(){
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200246 $('#RestartContainer').modal('toggle');
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100247 window.location = window.location.href.split("#")[0];
248 }, 1200);
249 } else {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200250 $('#triggerRestartContainer').html('<i class="bi bi-slash-lg"></i> ');
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100251 }
252 })
253 });
254 })
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200255
256 // responsive tabs
257 $('.responsive-tabs').tabCollapse({
258 tabsClass: 'hidden-xs',
259 accordionClass: 'js-tabcollapse-panel-group visible-xs'
260 });
261 $(document).on("shown.bs.collapse shown.bs.tab", function (e) {
262 var target = $(e.target);
263 if($(window).width() <= 767) {
264 var offset = target.offset().top - 112;
265 $("html, body").stop().animate({
266 scrollTop: offset
267 }, 100);
268 }
269 if(target.hasClass('panel-collapse')){
270 var id = e.target.id.replace(/-collapse$/g, '');
271 if(id){
272 localStorage.setItem('lastTag', '#'+id);
273 }
274 }
275 });
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100276});