gen_form_wsecl.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*##############################################################################
  2. # HPCC SYSTEMS software Copyright (C) 2015 HPCC Systems.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################## */
  16. // for test purpose
  17. function getAttributes(ctrl)
  18. {
  19. var results = "";
  20. var attrs = ctrl.attributes;
  21. for (var i = 0; i < attrs.length; i++) {
  22. var attr = attrs[i];
  23. results += attr.nodeName + '=' + attr.nodeValue + ' (' + attr.specified + ')<BR>';
  24. }
  25. return results;
  26. }
  27. //==================================================================
  28. // Restore dynamically generate content, and user input values(non-IE browsers only)
  29. function restoreDataFromCache()
  30. {
  31. var vals = document.getElementById("esp_vals_").value;
  32. if (vals && vals!="")
  33. {
  34. // alert("esp_vals_ = "+vals);
  35. var end = vals.indexOf('|');
  36. while (end>0)
  37. {
  38. var name = vals.substring(0,end);
  39. vals = vals.substring(end+1);
  40. end = vals.indexOf("|");
  41. if (end<0) break;
  42. var ctrl = document.getElementsByName(name)[0];
  43. if (ctrl)
  44. {
  45. if (ctrl.type == 'checkbox')
  46. {
  47. ctrl.checked = vals.substring(0,end)=='1';
  48. // alert("Name = "+name+", string = "+ vals.substring(0,end) + ", ctrl.checked = "+ ctrl.checked);
  49. }
  50. else if (ctrl.type == 'text' || ctrl.type == 'textarea')
  51. ctrl.value = decodeURI(vals.substring(0,end));
  52. else if(ctrl.type == 'radio')
  53. {
  54. //alert("Name = "+name+", string = "+ vals.substring(0,end) + ", ctrl.checked = "+ ctrl.checked);
  55. if(vals.substring(0,end) == '0')
  56. {
  57. ctrl = document.getElementsByName(name)[1];
  58. }
  59. if(ctrl)
  60. ctrl.checked = true;
  61. }
  62. else if(ctrl.type == 'select-one')
  63. {
  64. //alert("Select: name="+ctrl.name+"; value="+vals.substring(0,end));
  65. ctrl.options[vals.substring(0,end)].selected = true;
  66. }
  67. //TODO: more types
  68. }
  69. vals = vals.substring(end+1);
  70. end = vals.indexOf("|");
  71. }
  72. }
  73. }
  74. function disableAllInputs(self)
  75. {
  76. var toEnable = self.checked ? 1 : 0;
  77. var form = document.forms['esp_form'];
  78. var ctrls = form.elements;
  79. for (var idx=0; idx<ctrls.length; idx++)
  80. {
  81. var c = ctrls[idx];
  82. if ( (c.id!='') && (c.id.substr(0,3) == '$V.'))
  83. {
  84. if ( (c.checked && !toEnable) || (!c.checked && toEnable))
  85. c.click();
  86. }
  87. }
  88. }
  89. function disableInputControls(form)
  90. {
  91. var ctrls = form.elements;
  92. for (var idx=0; idx<ctrls.length; idx++)
  93. {
  94. var c = ctrls[idx];
  95. if ( (c.id!='') && (c.id.substr(0,3) == '$V.') && !c.checked)
  96. {
  97. var id = c.id.substring(3);
  98. var ctrl = document.getElementById(id);
  99. if (ctrl) // struct name has no id
  100. disableInputControl(ctrl,true);
  101. var label = document.getElementById('$L.'+id)
  102. disableInputLabel(label,true);
  103. }
  104. }
  105. }
  106. function onPageLoad()
  107. {
  108. var ctrl = document.getElementById('esp_html_');
  109. //alert("onPageLoad(): ctrl="+ctrl+"; length="+ctrl.value.length+";value='"+ctrl.value+"'");
  110. if (ctrl && ctrl.value != undefined && ctrl.value!="")
  111. {
  112. //alert("Restore ctrol value: " + ctrl.value);
  113. document.forms['esp_form'].innerHTML = ctrl.value;
  114. }
  115. var form = document.forms['esp_form'];
  116. initFormValues(form, getUrlFormValues(top.location.href));
  117. disableInputControls(form);
  118. // IE seems need this now too
  119. //if (isIE) return true;
  120. // FF 1.5 history cache works, but seems to stop working afterwards
  121. restoreDataFromCache();
  122. return true;
  123. }
  124. function getUrlFormValues(url)
  125. {
  126. var idx = url.indexOf('?');
  127. if (idx>0)
  128. url = url.substring(idx+1);
  129. var a = url.split('&');
  130. var ps = new Hashtable();
  131. for (var i=0; i<a.length; i++)
  132. {
  133. idx = a[i].indexOf('=');
  134. if (a[i].charAt(0) == '.' && idx>0)
  135. {
  136. var key = a[i].substring(0,idx);
  137. var val = a[i].substring(idx+1);
  138. if (val != '')
  139. ps.put(key, val);
  140. }
  141. }
  142. return ps;
  143. }
  144. function getUrlEspFlags(url)
  145. {
  146. var idx = url.indexOf('?');
  147. if (idx>0)
  148. url = url.substring(idx+1);
  149. var a = url.split('&');
  150. var ps = new Hashtable();
  151. for (var i=0; i<a.length; i++)
  152. {
  153. if (a[i].charAt(0) != '.')
  154. {
  155. idx = a[i].indexOf('=');
  156. if (idx>0)
  157. {
  158. var key = a[i].substring(0,idx);
  159. var val = a[i].substring(idx+1);
  160. ps.put(key,val);
  161. } else
  162. ps.put(a[i],"");
  163. }
  164. }
  165. return ps;
  166. }
  167. function createArray(ps)
  168. {
  169. var remains = new Hashtable();
  170. ps.moveFirst();
  171. while (ps.next())
  172. {
  173. var name = ps.getKey();
  174. var val = ps.getValue();
  175. // alert(name + ": " + val);
  176. if (val > 0 && name.substring(name.length-11)==".itemcount!")
  177. {
  178. var id = name.substring(1, name.length-10) + '_AddBtn';
  179. var ctrl = document.getElementById(id);
  180. if (ctrl)
  181. {
  182. // alert("name: " + ctrl.tagName + ", type " + ctrl.type);
  183. for (var i=0; i<val; i++)
  184. ctrl.click();
  185. }
  186. else {
  187. //alert("Can not find control: " + id);
  188. remains.put(name,val);
  189. }
  190. }
  191. }
  192. if (remains.size()>0)
  193. return remains;
  194. else
  195. return null;
  196. }
  197. function initFormValues(form, ps)
  198. {
  199. // create array controls
  200. // Implementation NOTE: The Add order is important: if array A contains array B, item in A must be created first before B can be created.
  201. var working = ps;
  202. do
  203. {
  204. working = createArray(working);
  205. //alert("Left: " + working);
  206. } while (working!=null);
  207. // init values
  208. ps.moveFirst();
  209. while (ps.next())
  210. {
  211. var name = ps.getKey();
  212. if (name.charAt(0) != '.')
  213. {
  214. //alert("Skip " + name);
  215. continue;
  216. }
  217. name = name.substring(1);
  218. var val = ps.getValue();
  219. ctrl = document.getElementsByName(name)[0];
  220. // alert("Set value for " + name + ": " + val + ". Ctrl type: " + ctrl.type);
  221. if (ctrl)
  222. {
  223. if (ctrl.type == 'checkbox') {
  224. ctrl.checked = val =='1';
  225. }
  226. else if (ctrl.type == 'text') {
  227. ctrl.value = decodeURIComponent(val); // decodeURI(vals.substring(0,end)); //TODO: do we need encoding
  228. }
  229. else if (ctrl.type == 'textarea') {
  230. ctrl.value = decodeURIComponent(val);
  231. }
  232. else if(ctrl.type == 'radio') {
  233. if(val == '0')
  234. ctrl = document.getElementsByName(name)[1];
  235. if(ctrl)
  236. ctrl.checked = true;
  237. }
  238. else if (ctrl.type=='select-one') {
  239. //alert("Set select value: " + val);
  240. ctrl.options[val].selected=true;
  241. }
  242. }
  243. else
  244. alert("failed to find contrl: " + name);
  245. }
  246. }
  247. function doBookmark(form)
  248. {
  249. var ps = getUrlEspFlags(form.action);
  250. var ctrls = form.elements;
  251. for (var idx=0; idx<ctrls.length; idx++)
  252. {
  253. var c = ctrls[idx];
  254. if ( (c.name!='') && (c.value != '') )
  255. {
  256. if (c.tagName == 'TEXTAREA') {
  257. ps.put('.'+c.name,encodeURIComponent(c.value));
  258. } else if (c.tagName == "SELECT") {
  259. ps.put('.'+c.name, c.selectedIndex); // use the index
  260. } else if (c.tagName == 'INPUT') {
  261. if ( c.type == 'text' || c.type=='password') {
  262. ps.put('.'+c.name,encodeURIComponent(c.value)); // existing one is overwrotten
  263. } else if (c.type == 'radio' && c.checked) {
  264. if (c.id.substring(c.id.length-5) == '.true')
  265. ps.put('.'+c.name,"1");
  266. else if (c.id.substring(c.id.length-6) == '.false')
  267. ps.put('.'+c.name,"0");
  268. } else if ( c.type=='hidden') {
  269. // alert("hidden:"+c.name+", value " + c.value + ", sub = " + c.name.substring(c.name.length-10));
  270. if (c.value!='0' && c.name.substring(c.name.length-11)=='.itemcount!')
  271. ps.put('.'+c.name,c.value);
  272. }
  273. }
  274. }
  275. }
  276. var idx = form.action.indexOf('?');
  277. var action = (idx>0) ? form.action.substring(0,idx) : form.action;
  278. action += "?form";
  279. var parm = "";
  280. ps.moveFirst();
  281. while (ps.next())
  282. parm += '&' + ps.getKey() + '=' + ps.getValue();
  283. //alert("parm="+parm);
  284. /*
  285. // TODO: make inner frame work
  286. var url = "/?inner=.." + path + "%3Fform";
  287. top.location.href = url + parm;
  288. */
  289. top.location.href = action + parm;
  290. }
  291. //==================================================================
  292. // Save dynamically generate content, and user input values(non-IE browsers only)
  293. function onSubmit(reqType) // reqType: 0: regular form, 1: soap, 2: form param passing
  294. {
  295. var form = document.forms['esp_form'];
  296. if (!form) return false;
  297. // remove "soap_builder_" (somehow FF (not IE) remembers this changed form.action )
  298. if (reqType != 1)
  299. {
  300. var action = form.action;
  301. var idx = action.indexOf('soap_builder_');
  302. if (idx>0)
  303. {
  304. if (action.length <= idx + 13) // no more char after 'soap_builder_'
  305. {
  306. var ch = action.charAt(idx-1);
  307. if (ch == '&' || ch == '?')
  308. action = action.substring(0,idx-1);
  309. } else {
  310. var ch = action.charAt(idx+13) // the char after 'soap_builder_';
  311. if (ch == '&')
  312. action = action.substring(0,idx) + action.substring(idx+13);
  313. }
  314. // alert("Old action: " + form.action + "\nNew action: " + action);
  315. form.action = action;
  316. }
  317. }
  318. // -- change action if user wants to
  319. var dest = document.getElementById('esp_dest');
  320. if (dest && dest.checked)
  321. {
  322. form.action = document.getElementById('dest_url').value;
  323. }
  324. if (reqType==1)
  325. {
  326. if (form.action.indexOf('soap_builder_')<0) // add only if does not exist already
  327. {
  328. var c = (form.action.indexOf('?')>0) ? '&' : '?';
  329. form.action += c + "soap_builder_";
  330. }
  331. }
  332. else if (reqType==2)
  333. {
  334. doBookmark(form);
  335. }
  336. if (reqType==3)
  337. {
  338. if (form.action.indexOf('roxie_builder_')<0) // add only if does not exist already
  339. {
  340. var c = (form.action.indexOf('?')>0) ? '&' : '?';
  341. form.action += c + "roxie_builder_";
  342. }
  343. }
  344. if (reqType==4)
  345. {
  346. if (form.action.indexOf('json_builder_')<0) // add only if does not exist already
  347. {
  348. var c = (form.action.indexOf('?')>0) ? '&' : '?';
  349. form.action += c + "json_builder_";
  350. }
  351. }
  352. // alert("Form action = " + form.action);
  353. // firefox now save input values (version 1.5)
  354. saveInputValues(form);
  355. return true;
  356. }
  357. //==================================================================
  358. // Save dynamically generate content, and user input values(non-IE browsers only)
  359. function onWsEcl2Submit(path) // reqType: 0: regular form, 1: soap, 2: form param passing, 3: roxiexml
  360. {
  361. var form = document.forms['esp_form'];
  362. if (!form) return false;
  363. var dest = document.getElementById('esp_dest');
  364. if (dest && dest.checked)
  365. {
  366. form.action = document.getElementById('dest_url').value;
  367. }
  368. else if (path=="bookmark")
  369. {
  370. doBookmark(form);
  371. }
  372. else
  373. {
  374. form.action = path;
  375. }
  376. alert("Form action = " + form.action);
  377. // firefox now save input values (version 1.5)
  378. saveInputValues(form);
  379. return true;
  380. }
  381. function saveInputValues(form)
  382. {
  383. // -- save values in input for browser
  384. var ctrl = document.getElementById('esp_html_');
  385. // IE seems to need this too
  386. //if (isIE || !ctrl) return true;
  387. ctrl.value=form.innerHTML;
  388. // save all user input
  389. var ctrls = form.elements;
  390. var items = ctrls.length;
  391. var inputValues = "";
  392. for (var idx=0; idx<ctrls.length; idx++)
  393. {
  394. var item = ctrls[idx];
  395. var name = item.name;
  396. if (!name) continue;
  397. //NOTE: we can not omit empty value since it can be different from the default
  398. if (item.type == 'checkbox')
  399. inputValues += name+"|"+(item.checked ? "1" : "0")+"|";
  400. else if (item.type =='text' || item.type=='textarea')
  401. {
  402. inputValues += name+"|" + encodeURI(item.value) +"|";
  403. //alert("value added: "+inputValues[inputValues.length-1] +", input items: " + inputValues.length+", values="+inputValues.toString());
  404. }
  405. else if (item.type == 'radio')
  406. {
  407. //if(item.checked) // the unchecked value can be different from the default
  408. inputValues += name+"|"+item.value+"|";
  409. }
  410. else if (item.type == 'select-one')
  411. {
  412. inputValues += name+"|"+item.selectedIndex+"|";
  413. //alert("inputValues=" + inputValues + "; index="+item.selectedIndex);
  414. }
  415. // TODO: other control types
  416. }
  417. document.getElementById("esp_vals_").value = inputValues;
  418. }
  419. //==================================================================
  420. // Reset all values to orginal (all dynamically generated arrays are removed)
  421. function onClearAll()
  422. {
  423. // reset dynamic generated content
  424. var reqCtrl = document.getElementById('esp_dyn');
  425. reqCtrl.innerHTML = getRequestFormHtml();;
  426. // clear cache
  427. var ctrl = document.getElementById('esp_html_');
  428. if (ctrl)
  429. ctrl.value = "";
  430. ctrl = document.getElementById("esp_vals_");
  431. if (ctrl)
  432. ctrl.value = "";
  433. }
  434. // Exclusive selectable
  435. function onClickSort(chked)
  436. {
  437. if (chked) {
  438. document.getElementById("esp_validate").checked = false;
  439. }
  440. }
  441. function onClickValidate(chked)
  442. {
  443. if (chked) {
  444. document.getElementById("esp_sort_result").checked = false;
  445. }
  446. }