Changeset 499961
- Timestamp:
- 02/03/2012 09:32:37 PM (14 years ago)
- Location:
- jazzy-forms/trunk
- Files:
-
- 7 edited
-
core/Parser.php (modified) (7 diffs)
-
core/Tokenizer.php (modified) (1 diff)
-
front/ctrl-shortcode.php (modified) (1 diff)
-
front/jazzy-forms.js (modified) (9 diffs)
-
front/tmpl-list.php (modified) (6 diffs)
-
jazzy-forms.php (modified) (1 diff)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
jazzy-forms/trunk/core/Parser.php
r491567 r499961 17 17 18 18 public function output() { 19 $result = $this-> sum();19 $result = $this->comparison(); 20 20 if($this->rest) { 21 21 throw new Exception('unexpected trailing symbols'); … … 24 24 } 25 25 26 private function comparison() { 27 return $this->operation('comparison', array('<=', '>=', '<', '>', '=', '<>'), 'sum'); 28 } 29 26 30 private function sum() { 27 if($this->ahead('-')) { 28 $this->consume(); 29 if($this->ahead('n')) { 30 $negative = $this->consume(); 31 return array(array('n', 0-$negative[1])); 32 } 33 $negative = $this->sum(); 34 if(!$negative) { 35 throw new Exception('negation without operand'); 36 } 37 return array(array('n', 0)) + $negative + array(array('o', '-')); 38 } 39 return $this->operation('sum', '+-', 'product'); 31 return $this->operation('sum', array('+', '-'), 'product'); 40 32 } 41 33 42 34 private function product() { 43 return $this->operation('product', '*/', 'exponentiation');35 return $this->operation('product', array('*', '/'), 'exponentiation'); 44 36 } 45 37 … … 65 57 } 66 58 67 private function ahead($ pattern, $skip=0) {59 private function ahead($tokens, $skip=0) { 68 60 if(count($this->rest) <= $skip) { 69 61 return false; 70 62 } 71 return strstr($pattern, $this->rest[$skip][0]); 63 $ahead = $this->rest[$skip][0]; 64 if(is_array($tokens)) { 65 foreach($tokens as $token) { 66 if($token == $ahead) { 67 return true; 68 } 69 } 70 return false; 71 } else { 72 return $tokens == $ahead; 73 } 72 74 } 73 75 … … 83 85 84 86 private function arguments() { 85 $result = $this->sum(); 87 $num = 0; 88 $result = $this->comparison(); 89 if($result) { 90 $num++; 91 } 86 92 while(true) { 87 93 if($this->ahead(')')) { … … 90 96 if($this->ahead(',')) { 91 97 $this->consume(); 92 $next = $this-> sum();98 $next = $this->comparison(); 93 99 $result = array_merge($result, $next); 100 $num++; 94 101 } 95 102 } 96 103 $this->consume(); // closing bracket 97 return $result;104 return array($num, $result); 98 105 } 99 106 … … 101 108 $id = $this->consume(); 102 109 $this->consume(); 103 $args= $this->arguments();104 return array_merge($args, array(array('f', $id[1], count($args))));110 list($num, $args) = $this->arguments(); 111 return array_merge($args, array(array('f', strtolower($id[1]), $num))); 105 112 } 106 113 107 114 private function variable() { 108 115 $id = $this->consume(); 109 return array(array('v', $id[1]));116 return array(array('v', strtolower($id[1]))); 110 117 } 111 118 112 119 private function association() { 113 120 $this->consume(); 114 $content = $this-> sum();121 $content = $this->comparison(); 115 122 if(!$content) { 116 123 throw new Exception('Empty bracket term'); … … 139 146 else if($this->ahead('-')) { 140 147 if($this->ahead('n', 1)) { 141 $this->negative();148 return $this->negative(); 142 149 } 143 150 } 144 else if($this->ahead( 'ns')) { // number or string151 else if($this->ahead(array('n', 's'))) { // number or string 145 152 return array($this->consume()); 146 153 } -
jazzy-forms/trunk/core/Tokenizer.php
r495032 r499961 35 35 36 36 private function operator() { 37 return $this->match(' [\-\+\*\/\^\%]');37 return $this->match('<=|>=|<>|[\-\+\*\/\^\,<>=]'); 38 38 } 39 39 -
jazzy-forms/trunk/front/ctrl-shortcode.php
r495032 r499961 27 27 } 28 28 $graph = jzzf_get_graph($form->elements); 29 $tpl->graph($ graph);29 $tpl->graph($form, $graph); 30 30 $tpl->script($form); 31 31 $tpl->head($form); -
jazzy-forms/trunk/front/jazzy-forms.js
r495032 r499961 1 (function($) { 2 3 jzzf_precision = 10^6; 1 function jazzy_forms($, form_id, jzzf_data, jzzf_types, jzzf_dependencies, jzzf_formulas) { 2 3 jzzf_precision = Math.pow(10,9); 4 function prcsn(x) { 5 return Math.round(x*jzzf_precision)/jzzf_precision; 6 } 7 4 8 5 9 $(function() { 6 10 bind(); 7 11 }); 12 13 function element(id) { 14 return $('#jzzf_' + form_id + '_' + id); 15 } 16 17 function element_id(element) { 18 var chunks = element.attr('id').split('_'); 19 return chunks[chunks.length - 1]; 20 } 8 21 9 22 function bind() { … … 13 26 switch(jzzf_types[id]) { 14 27 case 'r': 15 $('input:radio[name=jzzf_' + id + ']').bind('change ready', function() {16 update( $(this).attr('name').substring(5))28 element(id).find('input:radio').bind('change ready', function() { 29 update(element_id($(this).parents('.jzzf_radio'))); 17 30 }); 18 31 break; 19 32 default: 20 $('#jzzf_' +id).bind('change ready', function() {21 update( $(this).attr('id').substring(5))33 element(id).bind('change ready', function() { 34 update(element_id($(this))); 22 35 }); 23 36 … … 38 51 return; 39 52 } 40 $('#jzzf_' +id).val(Math.round(evaluate(id)*jzzf_precision)/jzzf_precision);53 element(id).val(Math.round(evaluate(id)*jzzf_precision)/jzzf_precision); 41 54 just_updated.push(id); 42 55 } … … 54 67 switch(jzzf_types[id]) { 55 68 case 'n': 56 return $('#jzzf_' +id).val() * jzzf_data[id];69 return element(id).val() * jzzf_data[id]; 57 70 case 'r': 58 var idx = $('input:radio[name=jzzf_' + id + ']:checked').index('input:radio[name=jzzf_' + id + ']');71 var idx = element(id).find('input:checked').parent().index(); 59 72 if(idx>=0) { 60 73 return jzzf_data[id][idx]; … … 63 76 } 64 77 case "c": 65 return $('#jzzf_' +id).is(':checked') ? jzzf_data[id][1] : jzzf_data[id][0];78 return element(id).is(':checked') ? jzzf_data[id][1] : jzzf_data[id][0]; 66 79 case 'd': 67 var idx = $('#jzzf_' + id + ' option:selected').index('#jzzf_' + id + ' option');80 var idx = element(id).find('option:selected').index(); 68 81 if(idx>=0) { 69 82 return jzzf_data[id][idx]; … … 76 89 return 0; 77 90 } 78 79 var functions = { 80 'abs': function(args) { 81 return Math.abs(args[0]); 82 } 83 } 84 91 85 92 function formula(id) { 86 93 var stack = []; … … 112 119 break; 113 120 case '^': 114 result = left ^ right; 121 result = Math.pow(left, right); 122 break; 123 case '<': 124 result = prcsn(left) < prcsn(right); 125 break; 126 case '>': 127 result = prcsn(left) > prcsn(right); 128 break; 129 case '<>': 130 result = prcsn(left) != prcsn(right); 131 break; 132 case '<=': 133 result = prcsn(left) <= prcsn(right); 134 break; 135 case '>=': 136 result = prcsn(left) >= prcsn(right); 137 break; 138 case '=': 139 result = prcsn(left) == prcsn(right); 115 140 break; 116 141 } … … 122 147 args.unshift(stack.pop()); 123 148 } 124 stack.push( functions[f[i][1]](args));149 stack.push(jzzf_functions(f[i][1], args)); 125 150 break; 126 151 } … … 129 154 } 130 155 131 })(jQuery) 156 } 157 158 function jzzf_functions(id, args) { 159 160 function arg(idx, def) { 161 if(idx >= args.length) { 162 return def; 163 } 164 return args[idx]; 165 } 166 167 var all = { 168 'abs': function() { 169 return Math.abs(arg(0)); 170 }, 171 'round': function() { 172 var digits = arg(1, 0); 173 var decimal = Math.pow(10, digits); 174 return Math.round(arg(0)*decimal)/decimal; 175 }, 176 'roundup': function() { 177 var digits = arg(1, 0); 178 var decimal = Math.pow(10, digits); 179 var x = arg(0); 180 return (x > 0) ? Math.ceil(arg(0)*decimal)/decimal : Math.floor(arg(0)*decimal)/decimal; 181 }, 182 'rounddown': function() { 183 var digits = arg(1, 0); 184 var decimal = Math.pow(10, digits); 185 var x = arg(0); 186 return (x > 0) ? Math.floor(arg(0)*decimal)/decimal : Math.ceil(arg(0)*decimal)/decimal; 187 }, 188 'sqrt': function() { 189 return Math.sqrt(arg(0)); 190 }, 191 'sin': function() { 192 return Math.sin(arg(0)); 193 }, 194 'cos': function() { 195 return Math.cos(arg(0)); 196 }, 197 'tan': function() { 198 return Math.tan(arg(0)); 199 }, 200 'asin': function() { 201 return Math.asin(arg(0)); 202 }, 203 'acos': function() { 204 return Math.acos(arg(0)); 205 }, 206 'atan': function() { 207 return Math.atan(arg(0)); 208 }, 209 'pi': function() { 210 return Math.PI; 211 }, 212 'not': function() { 213 return !arg(0); 214 }, 215 'and': function() { 216 return arg(0) && arg(1); 217 }, 218 'or': function() { 219 return arg(0) || arg(1); 220 }, 221 'if': function() { 222 return arg(0) ? arg(1) : arg(2); 223 }, 224 'true': function() { 225 return true; 226 }, 227 'false': function() { 228 return false; 229 } 230 231 }; 232 233 return (all[id])(); 234 } -
jazzy-forms/trunk/front/tmpl-list.php
r495032 r499961 2 2 3 3 class Jzzf_List_Template { 4 function __construct($form) {} 4 function __construct($form) { 5 $this->form = $form; 6 } 7 8 function id($element) { 9 echo esc_attr('jzzf_' . $this->form->id . '_' . $element->name); 10 } 11 5 12 6 13 function script($form) {?> … … 23 30 } 24 31 25 function graph($ graph) { extract($graph); ?>32 function graph($form, $graph) { extract($graph); ?> 26 33 <script type="text/javascript"> 27 jzzf_data = <?php echo json_encode($data) ?>; 28 jzzf_types = <?php echo json_encode($types) ?>; 29 jzzf_dependencies = <?php echo json_encode($dependencies) ?>; 30 jzzf_formulas = <?php echo json_encode($formulas) ?>; 34 jzzf_data_<?php echo $form->id ?> = <?php echo json_encode($data) ?>; 35 jzzf_types_<?php echo $form->id ?> = <?php echo json_encode($types) ?>; 36 jzzf_dependencies_<?php echo $form->id ?> = <?php echo json_encode($dependencies) ?>; 37 jzzf_formulas_<?php echo $form->id ?> = <?php echo json_encode($formulas) ?>; 38 jazzy_forms(jQuery, <?php echo $form->id ?>, jzzf_data_<?php echo $form->id ?>, jzzf_types_<?php echo $form->id ?>, jzzf_dependencies_<?php echo $form->id ?>, jzzf_formulas_<?php echo $form->id ?>); 31 39 </script> 32 40 <?php … … 51 59 function number($element) { ?> 52 60 <label class="jzzf_number_label jzzf_element_label" for="jzzf_<?php esc_attr_e($element->name) ?>"><?php esc_html_e($element->title) ?></label> 53 <input type="text" id=" jzzf_<?php esc_attr_e($element->name) ?>" value="<?php esc_attr_e($element->default) ?>">61 <input type="text" id="<?php $this->id($element) ?>" value="<?php esc_attr_e($element->default) ?>"> 54 62 <?php 55 63 } … … 57 65 function radio($element) { ?> 58 66 <label class="jzzf_radio_label jzzf_element_label"><?php esc_html_e($element->title) ?></label> 59 <ul class="jzzf_radio">67 <ul id="<?php $this->id($element) ?>" class="jzzf_radio"> 60 68 <?php $idx = 0; foreach($element->options as $option) { $idx++; ?> 61 69 <li> 62 <input type="radio" name=" jzzf_<?php esc_attr_e($element->name) ?>"<?php if($option->default): ?> checked="checked"<?php endif ?>>70 <input type="radio" name="<?php $this->id($element) ?>"<?php if($option->default): ?> checked="checked"<?php endif ?>> 63 71 <label class="jzzf_radio_option_label"><?php esc_html_e($option->title) ?></label> 64 72 </li> … … 69 77 70 78 function dropdown($element) { ?> 71 <label class="jzzf_element_label jzzf_dropdown_label" ><?php esc_html_e($element->title) ?></label>72 <select id=" jzzf_<?php esc_attr_e($element->name) ?>">79 <label class="jzzf_element_label jzzf_dropdown_label" for="<?php $this->id($element) ?>"><?php esc_html_e($element->title) ?></label> 80 <select id="<?php $this->id($element) ?>"> 73 81 <?php foreach($element->options as $option) : ?> 74 <option<?php if($option->default): ?> checked="checked"<?php endif ?>><?php esc_html_e($option->title) ?></option>82 <option<?php if($option->default): ?> selected="selected"<?php endif ?>><?php esc_html_e($option->title) ?></option> 75 83 <?php endforeach ?> 76 84 </select> … … 79 87 80 88 function checkbox($element) { ?> 81 <input type="checkbox" id=" jzzf_<?php esc_attr_e($element->name) ?>"<?php if($element->default): ?> checked="checked"<?php endif ?>>82 <label class="jzzf_checkbox_label" for=" jzzf_<?php esc_attr_e($element->name) ?>"><?php esc_html_e($element->title) ?>89 <input type="checkbox" id="<?php $this->id($element) ?>"<?php if($element->default): ?> checked="checked"<?php endif ?>> 90 <label class="jzzf_checkbox_label" for="<?php $this->id($element) ?>"><?php esc_html_e($element->title) ?> 83 91 <?php 84 92 } 85 93 86 94 function hidden($element) { ?> 87 <input type="hidden" id=" jzzf_<?php esc_attr_e($element->name) ?>" value="<?php esc_attr_e($element->value)?>">95 <input type="hidden" id="<?php $this->id($element) ?>" value="<?php esc_attr_e($element->value)?>"> 88 96 <?php 89 97 } 90 98 91 99 function output($element) { ?> 92 <label class="jzzf_element_label jzzf_output_label" for=" jzzf_<?php esc_attr_e($name) ?>"><?php esc_html_e($element->title) ?></label>93 <input type="text" readonly="readonly" id=" jzzf_<?php esc_attr_e($element->name) ?>"<?php if($element->invalid) : ?> value="Invalid formula"<?php endif ?>>100 <label class="jzzf_element_label jzzf_output_label" for="<?php $this->id($element) ?>"><?php esc_html_e($element->title) ?></label> 101 <input type="text" readonly="readonly" id="<?php $this->id($element) ?>"<?php if($element->invalid) : ?> value="Invalid formula"<?php endif ?>> 94 102 <?php 95 103 } 96 104 97 105 function update($element) { ?> 98 <input type="submit" id=" jzzf_<?php esc_attr_e($element->name) ?>" value="<?php esc_attr_e($element->title) ?>">106 <input type="submit" id="<?php $this->id($element) ?>" value="<?php esc_attr_e($element->title) ?>"> 99 107 <?php 100 108 } -
jazzy-forms/trunk/jazzy-forms.php
r495032 r499961 4 4 Plugin URI: http://www.jazzyforms.com/ 5 5 Description: Online form builder 6 Version: 0.9. 16 Version: 0.9.2 7 7 Author: Igor Prochazka 8 8 Author URI: http://www.l90r.com/ -
jazzy-forms/trunk/readme.txt
r495032 r499961 4 4 Tags: forms, form generator, calculator, price calculator, cost estimate 5 5 Requires at least: 3.2.1 6 Tested up to: 3.3 7 Stable tag: 0.9. 16 Tested up to: 3.3.1 7 Stable tag: 0.9.2 8 8 9 9 Jazzy Forms is an online form generator that performs instant calculations. It's ideal for inter-active price calculators. … … 104 104 == Changelog == 105 105 106 = 0.9.2 = 107 * Ignore uppercase/lowercase for variable and function names 108 * Allow multiple forms on one page 109 * Add logical functions (IF, AND, NOT, OR) and comparison operators 110 * Add basic maths functions (SQRT, trigonometric...) 111 * Fix numeric precision 112 * Fix default selection for dropdown menus 113 106 114 = 0.9.1 = 107 115 * Correctly handle character sets and collations (including Russian)
Note: See TracChangeset
for help on using the changeset viewer.