]> WPIA git - gigi.git/blob - static/static/js/expert.js
upd: move bootstrap from 3.5.1 to 4.3.1
[gigi.git] / static / static / js / expert.js
1 (function() {
2         var modal = undefined;
3         
4         function showModal(content){
5                 var HTML = `<div class="modal fade" id="confirmation-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
6   <div class="modal-dialog" role="document">
7     <div class="modal-content">
8       <div class="modal-header">
9         <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&#x1F5D9;</span></button>
10         <h4 class="modal-title" id="myModalLabel">&nbsp;</h4>
11       </div>
12       <div class="modal-body">
13         Body
14       </div>
15       <div class="modal-footer">
16         <button type="button" class="btn btn-default btn-cancel" data-dismiss="modal">&#x1F5D9;</button>
17         <button type="button" class="btn btn-danger btn-confirm">&checkmark;</button>
18       </div>
19     </div>
20   </div>
21 </div>`;
22                 if(modal === undefined) {
23                         modal = $(HTML);
24                         modal.action = function(){};
25                         modal.appendTo("body");
26                         $(modal.get(0)).find(".modal-footer .btn-confirm").click(function(){
27                                 modal.action();
28                                 modal.modal("hide");
29                         });
30                 }
31                 var m = $(modal.get(0));
32                 m.find(".modal-body").text($(content).attr("data-confirm"));
33                 var reply = $(content).attr("data-reply").split(",");
34                 m.find(".modal-footer .btn-cancel").text(reply[0]);
35                 m.find(".modal-footer .btn-confirm").text(reply[1]);
36                 modal.action=function(){
37                         content.confirmed=true;
38                         $(content).click();
39                 };
40                 modal.modal("show");
41         }
42         function initConfirm() {
43                 $(".btn-confirm").click(function(){
44                         if(this.confirmed === true){
45                                 return true;
46                         }
47                         showModal(this);
48                         return false;
49                 });
50         }
51         function showExpert(isExpert)
52         {
53           var elements = document.getElementsByClassName("expert");
54           for(var i = 0; elements.length > i; i++)
55           {
56             if(!isExpert) {
57                 elements[i].setAttribute("class","expert experthidden");
58             } else {
59                 elements[i].setAttribute("class","expert");
60             }
61           }
62         }
63         function initCertForm() {
64                 if(document.getElementById("placeholderName") == null) {
65                         return;
66                 }
67                 function getHint(id){
68                         var elem = document.getElementById(id);
69                         if(elem === null) {
70                                 return null;
71                         }
72                         return $(elem).text();
73                 }
74                 $("select[name=profile]").off("change");
75                 $("textarea[name=SANs]").off("keydown");
76                 $("textarea[name=SANs]").get(0).modified=false;
77                 $("input[name=CN]").off("keydown");
78                 $("input[name=CN]").get(0).modified=false;
79
80                 var placeholderName = getHint("placeholderName");
81                 var defaultName = getHint("defaultName");
82                 var defaultEmail = getHint("defaultEmail");
83                 var defaultDomain = getHint("defaultDomain");
84                 if(defaultName === null) {
85                         return;
86                 }
87                 $("textarea[name=SANs]").on("keydown", function(){
88                         this.modified = this.value !== "";
89                 });
90                 $("input[name=CN]").on("keydown", function(){
91                         this.modified = this.value !== "";
92                 });
93
94                 var loginCheck = document.getElementById("login");
95                 $("select[name=profile]").change(function(){
96                         var val = this.value;
97                         var sans = $("textarea[name=SANs]").get(0);
98                         if(val.match(/client.*/)) {
99                                 loginCheck.checked = true;
100                                 loginCheck.disabled = false;
101                         } else {
102                                 loginCheck.checked = false;
103                                 loginCheck.disabled = true;
104                         }
105                         if(val.match(/client.*|mail.*/)) {
106                                 if(!sans.modified) {
107                                         sans.value = "email:"+defaultEmail;
108                                 }
109                         } else if(val.match(/server.*/)) {
110                                 if(!sans.modified) {
111                                         sans.value = defaultDomain === null ? "" : "dns:" + defaultDomain;
112                                 }
113                         }
114                         var cn = $("input[name=CN]").get(0);
115                         if(val.match(/.*-a/)) {
116                                 if(!cn.modified) {
117                                         cn.value = defaultName;
118                                 }
119                         }else{
120                                 if(!cn.modified) {
121                                         cn.value = placeholderName;
122                                 }
123                         }
124                 });
125                 var children = $("select[name=profile]").get(0).children;
126                 var target = "client-mail";
127                 for(var i=0; i < children.length; i++){
128                         if(children[i].value == "client-mail-a"){
129                                 target = "client-mail-a";
130                         }
131                 }
132
133                 $("select[name=profile]").get(0).value = target;
134                 $("select[name=profile]").trigger("change");
135
136         }
137         function init(){
138                 showExpert(false);
139                 initCertForm();
140                 var expert = document.getElementById("expertbox");
141                 if(expert !== null) {
142                         expert.onchange = (function(expert){return function(){showExpert(expert.checked)}})(expert);
143                 }
144                 $(".card-activatable").map(function() {
145                         var card = $(this);
146                         var refresh = function(){
147                                 var radio = this.type == "radio";
148                                 if(radio && this.form.currentRadios === undefined) {
149                                         this.form.currentRadios = {};
150                                 }
151                                 if(this.checked) {
152                                         card.find(".card-body").removeClass("d-none");
153                                         if(radio) {
154                                                 var rds = this.form.currentRadios;
155                                                 if(rds[this.name] !== undefined){
156                                                         $(rds[this.name]).trigger("change");
157                                                 }
158                                                 rds[this.name] = this;
159                                         }
160                                 } else {
161                                         card.find(".card-body").addClass("d-none");
162                                 }
163                         };
164                         card.find(".card-heading [type=\"checkbox\"]").map(refresh);
165                         card.find(".card-heading [type=\"checkbox\"]").change(refresh);
166                         card.find(".card-heading [type=\"radio\"]").map(refresh);
167                         card.find(".card-heading [type=\"radio\"]").change(refresh);
168                         return this.id;
169                 });
170                 initConfirm();
171         }
172         (function(oldLoad) {
173                 if (oldLoad == undefined) {
174                         window.onload = init;
175                 } else {
176                         window.onload = function() {
177                                 init();
178                                 oldLoad();
179                         }
180                 }
181         })(window.onload);
182
183 })();