]>
git.proxmox.com Git - pve-http-server.git/blob - PVE/APIServer/Formatter/Bootstrap.pm
1 package PVE
::APIServer
::Formatter
::Bootstrap
;
9 # FIXME: remove console code??
11 # Helpers to generate simple html pages using Bootstrap markup.
14 PVE.open_vm_console = function(node, vmid) {
15 console.log("open vm " + vmid + " on node " + node);
17 var downloadWithName = function(uri, name) {
18 var link = jQuery('#pve_console_anchor');
19 link.attr("href", uri);
21 // Note: we need to tell android the correct file name extension
22 // but we do not set 'download' tag for other environments, because
23 // It can have strange side effects (additional user prompt on firefox)
24 var andriod = navigator.userAgent.match(/Android/i) ? true : false;
26 link.attr("download", name);
29 if (document.createEvent) {
30 var evt = document.createEvent("MouseEvents");
31 evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
32 link.get(0).dispatchEvent(evt);
34 link.get(0).fireEvent('onclick');
38 jQuery.ajax("/api2/json/console", {
39 data: { vmid: vmid, node: node },
40 headers: { CSRFPreventionToken: PVE.CSRFPreventionToken },
43 error: function(jqXHR, textStatus, errorThrown) {
44 // fixme: howto view JS errors ?
45 console.log("ERROR " + textStatus + ": " + errorThrown);
47 success: function(data) {
48 var raw = "[virt-viewer]\\n";
49 jQuery.each(data.data, function(k, v) {
50 raw += k + "=" + v + "\\n";
52 var url = 'data:application/x-virt-viewer;charset=UTF-8,' +
53 encodeURIComponent(raw);
55 downloadWithName(url, "pve-spice.vv");
62 my ($class, $res, $url, $auth, $config) = @_;
66 title
=> $config->{title
},
67 cookie_name
=> $config->{cookie_name
},
71 if (my $username = $auth->{userid
}) {
72 $self->{csrftoken
} = $config->{csrfgen_func
}->($username);
79 my ($self, $html) = @_;
81 my $jssetup = "PVE = {};\n\n"; # create namespace
83 if ($self->{csrftoken
}) {
84 $jssetup .= "PVE.CSRFPreventionToken = '$self->{csrftoken}';\n";
87 $jssetup .= "PVE.delete_auth_cookie = function() {\n";
89 if ($self->{cookie_name
}) {
90 $jssetup .= " document.cookie = \"$self->{cookie_name}=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/; secure;\";\n";
98 <meta charset="utf-8">
99 <meta http-equiv="X-UA-Compatible" content="IE=edge">
100 <meta name="viewport" content="width=device-width, initial-scale=1">
101 <title>$self->{title}</title>
104 <link href="/js/bootstrap/css/bootstrap.min.css" rel="stylesheet">
106 <script type="text/javascript">
117 <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
118 <script src="/js/jquery/jquery.min.js"></script>
119 <!-- Include all compiled plugins (below), or include individual files as needed -->
120 <script src="/js/bootstrap/js/bootstrap.min.js"></script>
124 <a class="hidden" id="pve_console_anchor"></a>
126 <script type="text/javascript">
134 my $comp_id_counter = 0;
137 my ($self, %param) = @_;
139 $param{tag
} = 'div' if !$param{tag
};
143 my $html = "<$param{tag}";
147 $id = "pveid$comp_id_counter";
167 foreach my $attr (keys %param) {
168 next if $skip->{$attr};
169 my $v = $noescape->{$attr} ?
$param{$attr} : uri_escape_utf8
($param{$attr},"[^\/\ A-Za-z0-9\-\._~]");
170 next if !defined($v);
171 if ($boolattr->{$attr}) {
172 $html .= " $attr" if $v;
174 $html .= " $attr=\"$v\"";
181 if (my $cn = $param{cn}) {
182 if(ref($cn) eq 'ARRAY'){
183 foreach my $rec (@$cn) {
184 $html .= $self->el(%$rec);
187 $html .= $self->el(%$cn);
189 } elsif ($param{html}) {
190 $html .= $param{html};
191 } elsif ($param{text}) {
192 $html .= encode_entities($param{text});
195 $html .= "</$param{tag
}>";
197 return wantarray ? ($html, $id) : $html;
201 my ($self, %param) = @_;
203 return $self->el(class => "alert alert-danger
", %param);
207 my ($self, $js) = @_;
209 $self->{js} .= $js . "\n";
212 my $format_event_callback = sub {
215 my $pstr = encode_json($info->{param});
216 return "function
(e
){$info->{fn
}.apply
(e
, $pstr);}";
220 my ($self, %param) = @_;
222 $param{tag} = 'button';
223 $param{class} = "btn btn-default btn-xs
";
225 if (my $click = delete $param{click}) {
226 my ($html, $id) = $self->el(%param);
227 my $cb = &$format_event_callback($click);
228 $self->add_js("jQuery
('#$id').on
('click', $cb);");
231 return $self->el(%param);