/apps/request/templates/filter/edit_body.tpl

http://zoop.googlecode.com/ · Smarty Template · 359 lines · 311 code · 48 blank · 0 comment · 16 complexity · 805627c9e103d6dc469a6d6288a24469 MD5 · raw file

  1. <script>
  2. var config = {json var=$config};
  3. {literal}
  4. $(document).ready(function() {
  5. initTemplate();
  6. });
  7. function initTemplate()
  8. {
  9. if(content)
  10. initTemplateBuild();
  11. else
  12. initTemplateDefault();
  13. }
  14. function initTemplateBuild()
  15. {
  16. // initialize the first menu
  17. var menu = $('#template .fieldMenu');
  18. for(var fieldName in config.fields)
  19. menu.append('<option value="' + fieldName + '">' + config.fields[fieldName].display + '</option>');
  20. updateOperatorMenu($('#template .rowWrapper:first'));
  21. updateOperandField($('#template .rowWrapper:first'));
  22. buildTemplate(content, $('#editable'), 1);
  23. // disable all of it's buttons
  24. $('#editable .anyAllWrapper:first button').attr({disabled: 'true'});
  25. // newSub.find('.minusButton').attr({disabled: 'true'});
  26. }
  27. function buildTemplate(curContent, container, depth)
  28. {
  29. // get a copy of the first subWrapper row
  30. var newSub = $('#template .subWrapper:first').clone();
  31. newSub.find('.anyall').val(curContent.anyall);
  32. for(var index in curContent.fields)
  33. {
  34. var newRow = $('#template .rowWrapper:first').clone();
  35. if(index == 0)
  36. newRow.find('.minusButton:first').attr({disabled: 'true'});
  37. var rowContainer = newSub.find('.rowContainer:first');
  38. rowContainer.append(newRow);
  39. // set up the first operand (field) menu
  40. var firstMenu = newRow.find('.fieldMenu:first');
  41. firstMenu.val(curContent.fields[index].field);
  42. updateOperatorMenu(newRow);
  43. // set up operator menu
  44. var operatorMenu = newRow.find('.operatorMenu:first');
  45. operatorMenu.val(curContent.fields[index].operator);
  46. updateOperandField(newRow);
  47. // set up operand menu
  48. var operandMenu = newRow.find('.operand:first');
  49. updateOperandMenu(operandMenu, curContent.fields[index].operandValues);
  50. }
  51. for(var index in curContent.subs)
  52. {
  53. buildTemplate(curContent.subs[index], rowContainer, depth + 1);
  54. }
  55. // add the subWrapper to the outer container
  56. container.append(newSub);
  57. }
  58. function updateOperandMenu(menu, values)
  59. {
  60. menu.val(values.menu);
  61. }
  62. function initTemplateDefault()
  63. {
  64. // initialize the first menu
  65. var menu = $('#template .fieldMenu');
  66. for(var fieldName in config.fields)
  67. menu.append('<option value="' + fieldName + '">' + config.fields[fieldName].display + '</option>');
  68. // initialize the second menu
  69. updateOperatorMenu($('.rowWrapper:first'));
  70. // initialize the operand field
  71. updateOperandField($('.rowWrapper:first'));
  72. // add the first row to the real table
  73. var firstRow = $('#template .subWrapper:first').clone();
  74. addDefaultRowToSub(firstRow);
  75. firstRow.find('.minusButton').attr({disabled: 'true'});
  76. $('#editable').append(firstRow);
  77. }
  78. function fieldMenuChanged(fieldMenu)
  79. {
  80. var row = $(fieldMenu).parents('.rowWrapper');
  81. updateOperatorMenu(row);
  82. updateOperandField(row);
  83. }
  84. function operatorMenuChanged(fieldMenu)
  85. {
  86. var row = $(fieldMenu).parents('.rowWrapper');
  87. updateOperandField(row);
  88. }
  89. function updateOperatorMenu(row)
  90. {
  91. var fieldMenu = row.find('.fieldMenu');
  92. var fieldName = fieldMenu.val();
  93. var operatorMenu = row.find('.operatorMenu');
  94. operatorMenu.empty();
  95. var type = config.fields[fieldName].type;
  96. for(var i in config.types[type].operators)
  97. {
  98. var operatorName = config.types[type].operators[i];
  99. var operatorDisplayName = config.operators[operatorName].display;
  100. operatorMenu.append('<option value="' + operatorName + '">' + operatorDisplayName + '</option>');
  101. }
  102. }
  103. function updateOperandField(row)
  104. {
  105. // get jQuery references to all of the desired elements
  106. var fieldMenu = row.find('.fieldMenu');
  107. var fieldName = fieldMenu.val();
  108. var operatorMenu = row.find('.operatorMenu');
  109. var operatorName = operatorMenu.val();
  110. var typeName = config.fields[fieldName].type;
  111. var widgitName = config.type_operator_map[typeName][operatorName].widgit;
  112. var templateCell = $('#template .' + widgitName);
  113. var targetCell = row.find('.operandBox');
  114. // put in the correct element
  115. targetCell.empty();
  116. targetCell.append( templateCell.html() );
  117. setOperandOptions(fieldName, widgitName, targetCell);
  118. }
  119. function setOperandOptions(fieldName, widgitName, targetCell)
  120. {
  121. var option;
  122. if(widgitName == 'int_menu')
  123. options = config.widgits[widgitName].list;
  124. else
  125. options = config.fields[fieldName].list;
  126. var target = targetCell.find('select');
  127. if(options && target)
  128. {
  129. for(var key in options)
  130. target.append('<option value="' + key + '">' + options[key] + '</option>');
  131. target.css({width: '100%'});
  132. }
  133. }
  134. function newRow(theButton)
  135. {
  136. var newRow = $('#template .rowWrapper:first').clone();
  137. var theRow = $(theButton).parents('.rowWrapper:first');
  138. theRow.after( newRow );
  139. }
  140. function newSubRow(theButton)
  141. {
  142. var newRow = $('#template .rowWrapper:first').clone();
  143. var theRow = $(theButton).parents('.subWrapper:first');
  144. theRow.after( newRow );
  145. }
  146. function addDefaultRowToSub(sub)
  147. {
  148. var newRow = $('#template .rowWrapper:first').clone();
  149. newRow.find('.minusButton:first').attr({disabled: 'true'});
  150. updateOperatorMenu(newRow);
  151. updateOperandField(newRow);
  152. sub.find('.rowContainer:first').append(newRow);
  153. }
  154. function removeRow(theButton)
  155. {
  156. $(theButton).parents('.rowWrapper:first').remove();
  157. }
  158. function newSub(theButton)
  159. {
  160. var newRow = $('#template .subWrapper:first').clone();
  161. addDefaultRowToSub(newRow);
  162. var theRow = $(theButton).parents('.rowWrapper:first');
  163. theRow.after( newRow );
  164. }
  165. function newSubSub(theButton)
  166. {
  167. var newRow = $('#template .subWrapper:first').clone();
  168. var theRow = $(theButton).parents('.subWrapper:first');
  169. theRow.after( newRow );
  170. }
  171. function removeSub(theButton)
  172. {
  173. $(theButton).parents('.subWrapper:first').remove();
  174. }
  175. function saveFilterData(showAlert)
  176. {
  177. var data = {};
  178. // var curContainer = $('#editable .rowContainer:first');
  179. var curContainer = $('#editable');
  180. gatherFilterData(data, curContainer, 1);
  181. var stringed = JSON.stringify(data);
  182. if(showAlert)
  183. alert(stringed);
  184. $('#filterData').val(stringed);
  185. }
  186. function getFilterData()
  187. {
  188. var data = {};
  189. var curContainer = $('#editable');
  190. // var curContainer = $('#editable .rowContainer:first');
  191. gatherFilterData(data, curContainer, 1);
  192. return JSON.stringify(data);
  193. }
  194. function gatherFilterData(data, cur, depth)
  195. {
  196. // console.log('depth start = ' + depth);
  197. data.subs = [];
  198. data.fields = [];
  199. cur.children('.subWrapper:first').each(function(i, e) {
  200. var newNode = {anyall: $(this).find('.anyAllWrapper:first .anyall:first').val()};
  201. data.subs.push(newNode);
  202. console.log('calling: ' + ' ' + depth + ' ' + i);
  203. gatherFilterData(newNode, $(this).find('.rowContainer:first'), depth + 1);
  204. console.log('called: ' + ' ' + depth + ' ' + i);
  205. });
  206. // console.log('back to = ' + depth);
  207. cur.children('.rowWrapper').each(function(i, e) {
  208. // console.log('depth = ' + depth + ' i = ' + i);
  209. if(true || depth < 3)
  210. {
  211. var operandValues = {};
  212. $(this).find('.operandBox:first .operand').each(function() {
  213. operandValues[$(this).attr('name')] = $(this).val();
  214. });
  215. data.fields.push({
  216. field: $(this).find('.fieldMenu:first').val(),
  217. operator: $(this).find('.operatorMenu:first').val(),
  218. operandValues: operandValues
  219. });
  220. }
  221. });
  222. // console.log('depth end = ' + depth);
  223. }
  224. {/literal}
  225. </script>
  226. <style>
  227. {literal}
  228. {/literal}
  229. </style>
  230. <div id="template" style="border: 1px solid black; display: none">
  231. <div class="subWrapper" style="border: 0px solid red">
  232. <div class="anyAllWrapper" style="border: 0px solid green">
  233. <div style="float: left">
  234. <select class="anyall">
  235. <option value="all">all</option>
  236. <option value="any">any</option>
  237. </select> of the following rules:
  238. </div>
  239. <div style="float: right">
  240. <table>
  241. <tr>
  242. <td>
  243. <table border="0" width="100%">
  244. <tr>
  245. <td width="30px"><div style="float: left; width: 30px"><button class="minusButton" onclick="removeSub(this); return false;">-</button></div></td>
  246. <td width="30px"><div style="float: left; width: 30px"><button class="plusButton" onclick="newSubRow(this); return false;">+</button></div></td>
  247. <td width="30px"><div style="float: left; width: 30px"><button class="subButton" onclick="newSubSub(this); return false;">..</button></div></td>
  248. </tr>
  249. </table>
  250. </td>
  251. </tr>
  252. </table>
  253. </div>
  254. <br style="clear: both"/>
  255. </div>
  256. <div style="border: 0px solid orange; margin-left: 20px" class="rowContainer">
  257. </div>
  258. </div>
  259. <div class="rowWrapper" style="border: 0px solid blue;">
  260. <table border="0" width="100%">
  261. <tr>
  262. <td>
  263. <table border="0" width="100%">
  264. <tr>
  265. <td width="20%"><select class="fieldMenu" style="width: 100%" onchange="fieldMenuChanged(this); return false;"></select></td>
  266. <td width="20%"><select class="operatorMenu" style="width: 100%" onchange="operatorMenuChanged(this); return false;"></select></td>
  267. </tr>
  268. </table>
  269. </td>
  270. <td>
  271. <table border="0" width="100%">
  272. <tr>
  273. <td class="operandBox"></td>
  274. <td width="30px"><div style="float: left; width: 30px"><button class="minusButton" onclick="removeRow(this); return false;">-</button></div></td>
  275. <td width="30px"><div style="float: left; width: 30px"><button class="plusButton" onclick="newRow(this); return false;">+</button></div></td>
  276. <td width="30px"><div style="float: left; width: 30px"><button class="subButton" onclick="newSub(this); return false;">..</button></div></td>
  277. </tr>
  278. </table>
  279. </td>
  280. </tr>
  281. </table>
  282. </div>
  283. <div class="date">
  284. <input class="operand" name="date">
  285. </div>
  286. <div class="date_range">
  287. <input class="operand" name="date1">
  288. <input class="operand" name="date2">
  289. </div>
  290. <div class="select">
  291. <select class="operand" name="menu"></select>
  292. </div>
  293. <div class="select_range">
  294. <select class="operand" name="menu1"></select>
  295. <select class="operand" name="menu2"></select>
  296. </div>
  297. <div class="select_multiple">
  298. <select class="operand" name="multi" multiple="multiple"></select>
  299. </div>
  300. <div class="int_menu">
  301. <table width="100%"><tr><td width="10%">
  302. <input class="operand" name="amount" type="text" size="4">
  303. </td>
  304. <td>
  305. <select class="operand" name="units"></select>
  306. </td></tr></table>
  307. </div>
  308. </div>
  309. <div style="border: 1px; width: 100%" id="editable">
  310. </div>
  311. <input type="hidden" name="filterData" id="filterData" value="asdfff">