|
| 1 | +/* |
| 2 | + * Copyright (C) 2006-2009 Citrix Systems Inc. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published |
| 6 | + * by the Free Software Foundation; version 2.1 only. with the special |
| 7 | + * exception on linking described in file LICENSE. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Lesser General Public License for more details. |
| 13 | + */ |
| 14 | + |
| 15 | +/* Api call window */ |
| 16 | + |
| 17 | +$(document).ready(function() {$('#topbar').append($($.create("button",{ns:xhtmlns,id:"apibutton","class":"topbutton"},["API call"])).click(test));}); |
| 18 | + |
| 19 | +var apiwin; |
| 20 | +var mycallparams={}; |
| 21 | + |
| 22 | +function create_ty(number,param,callparams) { |
| 23 | + var callback=(function (number) { return (function(evt) {callparams[number]=$(evt.target).val()})})(number); |
| 24 | + var cb2=(function (number) { return (function(evt,data,formatted){callparams[number]=data[0].split(" ")[0];});})(number); |
| 25 | + var cb3=(function (number) { return (function(evt){callparams[number]=evt.target.checked})})(number); |
| 26 | + var common=["label",{ns:xhtmlns,"class":"apicalllab"},[param.name]]; |
| 27 | + var i; |
| 28 | + |
| 29 | + switch(param.ty.ty) { |
| 30 | + case "string": |
| 31 | + case "int": |
| 32 | + case "float": |
| 33 | + return $($.create("input",{ns:xhtmlns,"class":"apicallinput"},[])).keyup(callback).change(callback); |
| 34 | + case "bool": |
| 35 | + callparams[number]=false; |
| 36 | + var ret=$($.create("input",{ns:xhtmlns,type:"checkbox","class":"apicallcheck"},[])).autocomplete(tmp,{matchContains:true}).result(cb3); |
| 37 | + return ret; |
| 38 | + case "datetime": |
| 39 | + callback=(function (number) { return (function(evt) {callparams[number]=new Date(Date.parse($(evt.target).val()))})})(number); |
| 40 | + return $($.create("input",{ns:xhtmlns,"class":"apicallinput"},[])).keyup(callback).change(callback); |
| 41 | + case "enum": |
| 42 | + var tmp=param.ty.values; |
| 43 | + callparams[number]=tmp[0]; |
| 44 | + var ret=$($.create("input",{ns:xhtmlns,value:tmp[0],"class":"apicallinput"},[])).autocomplete(tmp,{matchContains:true}).result(cb2); |
| 45 | + return ret; |
| 46 | + case "set": |
| 47 | + return $(''); |
| 48 | + case "map": |
| 49 | + return $(''); |
| 50 | + case "record": |
| 51 | + return $(''); |
| 52 | + case "ref": |
| 53 | + var tmp=[]; |
| 54 | + var val=""; |
| 55 | + var cls=param.ty.class.toLowerCase(); |
| 56 | + if(cls=="session") { |
| 57 | + tmp[0]=$xapi.session; |
| 58 | + val=$xapi.session; |
| 59 | + callparams[number]=val; |
| 60 | + } else { |
| 61 | + var objs=$xapi.xo[cls]; |
| 62 | + var j=0; |
| 63 | + for(i in objs) { |
| 64 | + var name = i; |
| 65 | + if(objs[i].name_label) |
| 66 | + name=name+" ("+objs[i].name_label+")"; |
| 67 | + if(objs[i].uuid) |
| 68 | + name=name+" ("+objs[i].uuid+")"; |
| 69 | + tmp[j++]=name; |
| 70 | + } |
| 71 | + } |
| 72 | + var ret=$($.create("input",{ns:xhtmlns,value:val,"class":"apicallinput"},[])).autocomplete(tmp,{matchContains:true}).result(cb2); |
| 73 | + return ret; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function api_result(event,data,formatted) { |
| 78 | + $('#msgresdiv').hide().empty(); |
| 79 | + var params=messages[data].params; |
| 80 | + $('#msgdoc').empty().append($($.create("p",{ns:xhtmlns},[messages[data].doc]))); |
| 81 | + mycallparams=[]; |
| 82 | + var form=$('#msgargsform').empty(); |
| 83 | + for(i=0; i<params.length; i++) { |
| 84 | + var arg=create_ty(i+1,params[i],mycallparams).focus( |
| 85 | + (function (paramnum) {return ( |
| 86 | + function (evt) { |
| 87 | + $('#msgargname').empty().text(params[paramnum].name) |
| 88 | + $('#msgargdoc').empty().text(params[paramnum].doc) |
| 89 | + } |
| 90 | + )})(i)); |
| 91 | + if(i==0) |
| 92 | + arg.focus(); |
| 93 | + var labinner=$($.create("span",{ns:xhtmlns,"class":"apicallspan"},[params[i].name])) |
| 94 | + var label=$($.create("label",{ns:xhtmlns,"class":"apicalllab"},[])).append(labinner).append(arg); |
| 95 | + form.append(label); |
| 96 | + form.append($($.create("br",{ns:xhtmlns},[]))); |
| 97 | + |
| 98 | + } |
| 99 | + var button=$('button',form.parent()); |
| 100 | + if(button.length==0) { |
| 101 | + button=$($.create("button",{ns:xhtmlns},["apply"])); |
| 102 | + button.appendTo(form.parent()); |
| 103 | + } |
| 104 | + button.unbind().click(function() { |
| 105 | + if (typeof netscape != "undefined") { |
| 106 | + //netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalBrowserRead"); |
| 107 | + } |
| 108 | + var curobj=$xapi.xapi; |
| 109 | + var hierarchy=data[0].split("."); |
| 110 | + for(i=0; i<hierarchy.length; i++) |
| 111 | + curobj=curobj[hierarchy[i]]; |
| 112 | + mycallparams[0]=function(res) { |
| 113 | + var contents; |
| 114 | + var n={ns:xhtmlns}; |
| 115 | + contents=["tr",n,["td",n,["Status"],"td",n,[res.result.status]]]; |
| 116 | + if(res.result.Status=="Failure") { |
| 117 | + contents=contents.concat(["tr",n,["td",n,["Error"],"td",n,[res.result.ErrorDescription[0]]]]); |
| 118 | + for(var i=1; i<res.result.ErrorDescription.length; i++) { |
| 119 | + contents=contents.concat(["tr",n,["td",n,["Error parameter "+i],"td",n,[res.result.ErrorDescription[i]]]]); |
| 120 | + } |
| 121 | + $('#msgresdiv').append($($.create("h3",n,["Result"],"table",n,contents))).show(); |
| 122 | + } else if(res.result.Status=="Success") { |
| 123 | + $('#msgresdiv').append($($.create("h3",n,["Result"],"table",n,contents,"textarea",{ns:xhtmlns,style:"width:500px"},[res.result.Value]))).show(); |
| 124 | + } |
| 125 | + } |
| 126 | + curobj.apply($xapi,mycallparams); |
| 127 | + }); |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +function create_api_choice() { |
| 132 | + var choices=[]; |
| 133 | + var i=0; |
| 134 | + var message; |
| 135 | + for(message in messages) { |
| 136 | + choices[i++]=message; |
| 137 | + } |
| 138 | + var ret=$($.create("label",{ns:xhtmlns,style:"text-weight:bold"},["API Call:"],"input",{ns:xhtmlns,id:"apicallinput"},[])); |
| 139 | + $(ret[1]).autocomplete(choices,{matchContains:true}).result(api_result); |
| 140 | + return ret; |
| 141 | +} |
| 142 | + |
| 143 | +function test() { |
| 144 | + var win=$dw.createWindow("mytest","mytest"); |
| 145 | + $dw.changeWindowDimensions(win,600); |
| 146 | + var content=$('.content',win).css("padding","5px"); |
| 147 | + var form=content.append($($.create( |
| 148 | + "form",{ns:xhtmlns,id:"msgnameform"},[], |
| 149 | + "hr",{ns:xhtmlns},[], |
| 150 | + "div",{ns:xhtmlns},[ |
| 151 | + "h3",{ns:xhtmlns},["Call documentation"], |
| 152 | + "div",{ns:xhtmlns,id:"msgdoc"},[]], |
| 153 | + "hr",{ns:xhtmlns},[], |
| 154 | + "div",{ns:xhtmlns},[ |
| 155 | + "h3",{ns:xhtmlns},["Parameters"], |
| 156 | + "form",{ns:xhtmlns,id:"msgargsform"},[], |
| 157 | + "h3",{ns:xhtmlns,id:"msgargname"},[], |
| 158 | + "p",{ns:xhtmlns,id:"msgargdoc"},[], |
| 159 | + "br",{ns:xhtmlns,style:"clear:both"},[] |
| 160 | + ], |
| 161 | + "hr",{ns:xhtmlns},[], |
| 162 | + "div",{ns:xhtmlns,id:"msgresdiv",style:"display:none"},[ |
| 163 | + "h3",{ns:xhtmlns},["Result"] |
| 164 | + ] |
| 165 | + ))); |
| 166 | + var api=create_api_choice(); |
| 167 | + $('#msgnameform',form).append(api); |
| 168 | + apiwin=win; |
| 169 | +} |
| 170 | + |
0 commit comments