Changeset 6621
- Timestamp:
- 12/20/07 16:59:42 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfExtjs2Plugin/trunk/config/config.php
r6585 r6621 1 1 <?php 2 sfConfig::set('sf_extjs2_version', 'v0. 30');2 sfConfig::set('sf_extjs2_version', 'v0.50'); 3 3 sfConfig::set('sf_extjs2_comment', true); 4 #5 # array values that don't need quotes6 #7 sfConfig::set('sf_extjs2_quote_except',8 array(9 'value' => array('true', 'false', 'new Ext.', 'function', 'Ext.'),10 'key' => array('renderer', 'store', 'defaults', 'plugins', 'cm', 'ds', 'view', 'tbar', 'bbar')11 )12 );13 4 # 14 5 # adapters … … 59 50 # attributes which must handled as array 60 51 # 61 sfConfig::set('sf_extjs2_list_attributes', array('items', 'tbar', 'buttons', 'plugins', 'view', 'tbar', 'bbar')); 52 sfConfig::set('sf_extjs2_list_attributes', array('items', 'tbar', 'buttons', 'plugins', 'view', 'tbar', 'bbar', 'fields')); 53 # 54 # array values that don't need quotes 55 # 56 sfConfig::set('sf_extjs2_quote_except', 57 array( 58 'value' => array('true', 'false', 'new Ext.', 'function', 'Ext.'), 59 'key' => array('renderer', 'store', 'defaults', 'plugins', 'cm', 'ds', 'view', 'tbar', 'bbar') 60 ) 61 ); 62 62 # 63 63 # mapping plugin method against class … … 66 66 array( 67 67 // data 68 'JsonReader' => 'Ext.data.JsonReader',69 'Store' => 'Ext.data.Store',70 'HttpProxy' => 'Ext.data.HttpProxy',68 'JsonReader' => 'Ext.data.JsonReader', 69 'Store' => 'Ext.data.Store', 70 'HttpProxy' => 'Ext.data.HttpProxy', 71 71 // widgets 72 'BoxComponent' => 'Ext.BoxComponent', 73 'Button' => 'Ext.Button', 74 'GridPanel' => 'Ext.grid.GridPanel', 75 'ColumnModel' => 'Ext.grid.ColumnModel', 76 'Panel' => 'Ext.Panel', 77 'TabPanel' => 'Ext.TabPanel', 78 'Viewport' => 'Ext.Viewport', 79 'Window' => 'Ext.Window', 80 'FormPanel' => 'Ext.FormPanel', 81 'DateField' => 'Ext.form.DateField', 82 'TextField' => 'Ext.form.TextField', 83 'TimeField' => 'Ext.form.TimeField', 84 'HtmlEditor' => 'Ext.form.HtmlEditor', 85 'Menu' => 'Ext.menu.Menu', 86 'Item' => 'Ext.menu.Item', 87 'CheckItem' => 'Ext.menu.CheckItem', 88 'MenuButton' => 'Ext.Toolbar.MenuButton' 72 'BoxComponent' => 'Ext.BoxComponent', 73 'Button' => 'Ext.Button', 74 'GridPanel' => 'Ext.grid.GridPanel', 75 'ColumnModel' => 'Ext.grid.ColumnModel', 76 'Panel' => 'Ext.Panel', 77 'TabPanel' => 'Ext.TabPanel', 78 'Viewport' => 'Ext.Viewport', 79 'Window' => 'Ext.Window', 80 'FormPanel' => 'Ext.FormPanel', 81 'DateField' => 'Ext.form.DateField', 82 'TextField' => 'Ext.form.TextField', 83 'TimeField' => 'Ext.form.TimeField', 84 'HtmlEditor' => 'Ext.form.HtmlEditor', 85 'Menu' => 'Ext.menu.Menu', 86 'Item' => 'Ext.menu.Item', 87 'CheckItem' => 'Ext.menu.CheckItem', 88 'MenuButton' => 'Ext.Toolbar.MenuButton', 89 'KeyMap' => 'Ext.KeyMap' 89 90 ) 90 91 ); … … 255 256 ); 256 257 258 259 sfConfig::set('Ext.KeyMap', 260 array( 261 'class' => 'Ext.KeyMap', 262 'attributes' => array() 263 ) 264 ); 265 266 267 sfConfig::set('anonymousClass', 268 array( 269 'class' => 'anonymousClass', 270 'attributes' => array() 271 ) 272 ); 273 274 257 275 ?> plugins/sfExtjs2Plugin/trunk/lib/helper/sfExtjs2Helper.php
r6585 r6621 4 4 * @plugin sfExtjs2Plugin 5 5 * @description sfExtjs2Plugin is a symfony plugin that provides an easy to use wrapper for the Ext javascript library 6 * @author Benjamin Runnels<benjamin.r.runnels [at] citi [dot] com>, Leon van der Ree, Wolfgang Kubens<wolfgang.kubens [at] gmx [dot] net> 7 * @version 0.0.30 8 * @last modified 12.18.2007 Wolfgang 6 * @author Benjamin Runnels<benjamin.r.runnels [at] citi [dot] com>, Leon van der Ree, Wolfgang Kubens<wolfgang.kubens [at] gmx [dot] net> 7 * @version 0.0.50 8 * @last modified 12.19.2007 Wolfgang 9 * - Added method asVar 10 * - Added logic for anonymousClass 11 * - Changed parameters handling 12 * 12.18.2007 Wolfgang 9 13 * - Added sf_extjs2_comment 10 14 * 12.17.2007 Leon: … … 15 19 * - Fixed quoting logic for beginApplication 16 20 * 12.15.2007 Kubens: 17 * - Overworked quoting logic 21 * - Overworked quoting logic 18 22 * 11.22.2007 Kubens: 19 * - Added features to create application 23 * - Added features to create application 20 24 * - Added parameters support for Ext.object constructors 21 25 * 11.17.2007 Kubens: 22 26 * - Added features to create custom classes and custom methods 23 * 11.12.2007 Kubens: 24 * - Fixed loading order of adapters. If adapters are used then it is important to load 27 * 11.12.2007 Kubens: 28 * - Fixed loading order of adapters. If adapters are used then it is important to load 25 29 * adapters and coresponding files before ext-all.js 26 30 * - Overworked: load method. Adapters and themes are setuped in config.php 27 31 * - Overworked: constructor. If no adapter or theme is passed then default 28 * settings from config.php will used 29 * 11.07.2007 Benjamin: 32 * settings from config.php will used 33 * 11.07.2007 Benjamin: 30 34 * - Fixed the adapter includes to load all required files in the correct order 31 35 * moved ext-base into adapters, pass ext as adapter for standalone 32 36 * changed all javascript to load first so they will come before files specified in view.yml 33 * 07.15.2007 Kubens: 37 * 07.15.2007 Kubens: 34 38 * - created 35 39 */ … … 39 43 const LBR_CM = ",\n"; 40 44 const LBR_SM = ";\n"; 41 45 42 46 private $items = array(); 43 47 private $adapter = ''; // current adapter 44 48 private $theme = ''; // current theme 45 49 private $namespace = ''; // current namespace 46 50 47 51 /** 48 52 * Creates an instance of sfExtjs2Plugin. 49 * 53 * 50 54 * Usage: 51 * 55 * 52 56 * $sfExtjs2Plugin = new sfExtjs2Plugin( 53 57 * array … … 67 71 68 72 /** 69 * If method does not exists and method is listed in 70 * config.sf_extjs2_classes then Extjs2.class.constructor will rendered. 71 * 73 * If method does not exists and method is listed in 74 * config.sf_extjs2_classes then Extjs2.class.constructor will rendered. 75 * 72 76 * Usage: 73 * 77 * 74 78 * $sfExtjs2Plugin = new sfExtjs2Plugin( 75 79 * array … … 88 92 * ) 89 93 * ); 90 * 94 * 91 95 * @param string class 92 96 * @param array attributes … … 105 109 /** 106 110 * Creates Javascript source for Extjs2.class 107 * 111 * 108 112 * Usage: 109 * 113 * 110 114 * Syntax A = short form without any options 111 115 * $sfExtjs2Plugin->Object(array 112 116 * ( 113 117 * 'id' => 'id', 114 * 'renderTo' => 'document.body',118 * 'renderTo' => $sfExtjs2Plugin->asVar('document.body'), 115 119 * 'items' => array 116 120 * ( 117 * $sfExtjs2Plugin->Object(array('title'=>' "Object A"')),118 * $sfExtjs2Plugin->Object(array('title'=>' "Object B"'))121 * $sfExtjs2Plugin->Object(array('title'=>'Object A')), 122 * $sfExtjs2Plugin->Object(array('title'=>'Object B')) 119 123 * ) 120 124 * )); 125 * 126 * => new Object({id: 'id', renderTo: document.body, items: [new Object(title: 'Object A'), new Object(title: 'Object B')]}) 127 * 121 128 * 122 129 * Syntax B = long form with additional options … … 124 131 * ( 125 132 * 'name' => 'string', // option to render Javascript variable 133 * 'parameters' => array 134 * ( 135 * 'parameter1', 136 * 'parameter2' 137 * ), 126 138 * 'attributes' => array // attributes for Ext constructor 127 139 * ( … … 130 142 * 'items' => array 131 143 * ( 132 * $sfExtjs2Plugin->Object(array('title' => ' "Object A"')),133 * $sfExtjs2Plugin->Object(array('title' => ' "Object B"'))144 * $sfExtjs2Plugin->Object(array('title' => 'Object A')), 145 * $sfExtjs2Plugin->Object(array('title' => 'Object B')) 134 146 * ) 135 * ), 136 * 'parameters' => array 137 * ( 138 * 'parameter1', 139 * 'parameter2' 140 * ) 147 * ) 141 148 * )); 149 * 150 * => new Object(parameter1, parameter2, {id: 'id', renderTo: document.body, items: [new Object(title: 'Object A'), new Object(title: 'Object B')]}) 142 151 * 143 152 * @param string class … … 154 163 unset($attributes['parameters']); 155 164 } 156 165 157 166 # syntax A is a shortform of syntax B 158 167 # if syntax A is used then convert syntax A to syntax B … … 163 172 $attributes['attributes'] = $tmp; 164 173 } 165 174 166 175 # list attributes must defined as an Javascript array 167 176 # therefore all list attributes must be rendered as [attributeA, attributeB, attributeC] 168 foreach (sfConfig::get('sf_extjs2_list_attributes') as $attribute) 169 { 170 if (array_key_exists($attribute, $attributes['attributes']) && !$attributes['attributes'][$attribute] instanceof sfExtjs2Var)177 foreach (sfConfig::get('sf_extjs2_list_attributes') as $attribute) 178 { 179 if (array_key_exists($attribute, $attributes['attributes']) && !$attributes['attributes'][$attribute] instanceof sfExtjs2Var) 171 180 { 172 181 $attributes['attributes'][$attribute] = sprintf('[%s]', sfExtjs2Plugin::_build_attributes($attributes['attributes'][$attribute])); 173 182 } 174 183 } 175 184 176 185 // get source of component 177 186 $source = call_user_func(array('sfExtjs2Plugin', 'getExtObjectComponent'), $attributes['attributes'], sfConfig::get($class), $parameters); 178 187 179 188 // if 'name' is assigned then we must render 180 189 // either a Javascript variable or an attribute of this … … 189 198 ); 190 199 } 191 200 192 201 // if 'lbr' assigned then we must render a line break 193 202 if (is_array($attributes) && array_key_exists('lbr', $attributes)) … … 195 204 $source .= $attributes['lbr']; 196 205 } 197 206 198 207 return $source; 199 208 } … … 201 210 /** 202 211 * Creates Javascript source for Extjs2.class 203 * 212 * 204 213 * @param array attributes 205 214 * @param array config … … 211 220 $attributes = sfExtjs2Plugin::_build_attributes($attributes, $config['attributes']); 212 221 $attributes = sprintf('%s', $attributes != '' ? '{'.$attributes.'}' : ''); 213 222 214 223 $parameters = implode(',', $parameters); 215 $parameters = sprintf('%s%s', $attributes != '' && $parameters != '' ? ',' : '', $parameters); 216 217 $source = sprintf( 218 'new %s (%s%s)', 219 $config['class'], 220 $attributes, 221 $parameters 222 ); 223 224 return $source; 224 225 switch ($config['class']) 226 { 227 case 'anonymousClass': 228 $source = sprintf( 229 '%s%s', 230 $parameters != '' ? $parameters . ',' : '', 231 $attributes 232 ); 233 return $source; 234 235 case 'customClass': 236 $source = sprintf( 237 '{%s}', 238 $attributes 239 ); 240 return $source; 241 242 default: 243 $source = sprintf( 244 'new %s (%s%s%s)', 245 $config['class'], 246 $parameters != '' ? $parameters : '', 247 $parameters != '' && $attributes != '' ? ',' : '', 248 $attributes 249 ); 250 return $source; 251 } 252 225 253 } 226 254 … … 265 293 $source .= sfExtjs2Plugin::_comment(sprintf("%s// sfExtjs2Helper: %s%s", sfExtjs2Plugin::LBR, sfConfig::get('sf_extjs2_version'), sfExtjs2Plugin::LBR)); 266 294 $source .= sprintf("Ext.BLANK_IMAGE_URL = '%s'%s", sfConfig::get('sf_extjs2_spacer'), sfExtjs2Plugin::LBR_SM); 267 295 268 296 echo $source; 269 297 } … … 294 322 { 295 323 $source = ''; 296 324 297 325 // write namespace directive 298 326 // prevent double output of namespace directive … … 300 328 { 301 329 $this->namespace = $namespace; 302 $source .= sfExtjs2Plugin::_comment(sprintf("%s// namespace: %s%s", sfExtjs2Plugin::LBR, $namespace, sfExtjs2Plugin::LBR)); 330 $source .= sfExtjs2Plugin::_comment(sprintf("%s// namespace: %s%s", sfExtjs2Plugin::LBR, $namespace, sfExtjs2Plugin::LBR)); 303 331 $source .= sprintf("Ext.namespace('%s')%s", $namespace, sfExtjs2Plugin::LBR_SM); 304 332 } 305 333 306 334 // write class tag 307 335 $source .= sfExtjs2Plugin::_comment(sprintf("%s// class: %s.%s%s", sfExtjs2Plugin::LBR, $namespace, $classname, sfExtjs2Plugin::LBR)); 308 336 $source .= sprintf("%s.%s = Ext.extend(%s, {%s", $namespace, $classname, $extend, sfExtjs2Plugin::LBR); 309 337 310 338 // write attributes 311 339 $source .= sfExtjs2Plugin::_build_attributes($attributes); 312 340 313 341 echo $source; 314 342 } … … 335 363 * @return string source 336 364 */ 337 public function beginApplication($attributes = array())338 {339 // private attributes340 $sourcePrivate = '';341 if (array_key_exists('private', $attributes))342 {343 foreach ($attributes['private'] as $key => $value)344 {345 $sourcePrivate .= sprintf('%svar %s = %s;', sfExtjs2Plugin::LBR, $key, sfExtjs2Plugin::_quote($key, $value));346 }347 }348 349 // public attributes350 $sourcePublic = '';351 if (array_key_exists('public', $attributes))352 {353 // write attributes354 $sourcePublic .= sfExtjs2Plugin::_build_attributes($attributes['public']);355 }356 357 // write application syntax358 $source = '';359 $source .= sfExtjs2Plugin::_comment(sprintf("%s// application: %s%s", sfExtjs2Plugin::LBR, $attributes['name'], sfExtjs2Plugin::LBR));360 $source .= sprintf(361 'var %s = function() { %sreturn {%s%s %s %s',362 $attributes['name'],363 sfExtjs2Plugin::LBR,364 sfExtjs2Plugin::LBR,365 $sourcePrivate,366 $sourcePrivate != '' ? sfExtjs2Plugin::LBR : '',367 $sourcePublic,368 $sourcePublic != '' ? sfExtjs2Plugin::LBR : ''369 );370 371 echo $source;372 }365 public function beginApplication($attributes = array()) 366 { 367 // private attributes 368 $sourcePrivate = ''; 369 if (array_key_exists('private', $attributes)) 370 { 371 foreach ($attributes['private'] as $key => $value) 372 { 373 $sourcePrivate .= sprintf('%svar %s = %s;', sfExtjs2Plugin::LBR, $key, sfExtjs2Plugin::_quote($key, $value)); 374 } 375 } 376 377 // public attributes 378 $sourcePublic = ''; 379 if (array_key_exists('public', $attributes)) 380 { 381 // write attributes 382 $sourcePublic .= sfExtjs2Plugin::_build_attributes($attributes['public']); 383 } 384 385 // write application syntax 386 $source = ''; 387 $source .= sfExtjs2Plugin::_comment(sprintf("%s// application: %s%s", sfExtjs2Plugin::LBR, $attributes['name'], sfExtjs2Plugin::LBR)); 388 $source .= sprintf( 389 'var %s = function() { %sreturn {%s%s %s %s', 390 $attributes['name'], 391 sfExtjs2Plugin::LBR, 392 sfExtjs2Plugin::LBR, 393 $sourcePrivate, 394 $sourcePrivate != '' ? sfExtjs2Plugin::LBR : '', 395 $sourcePublic, 396 $sourcePublic != '' ? sfExtjs2Plugin::LBR : '' 397 ); 398 399 echo $source; 400 } 373 401 374 402 /** … … 381 409 $source = ''; 382 410 $source .= sprintf("%s}}()%s", sfExtjs2Plugin::LBR, sfExtjs2Plugin::LBR_SM); 383 411 384 412 echo $source; 385 413 } … … 398 426 399 427 return $source; 428 } 429 430 /** 431 * returns source of custom class 432 * 433 * @param string classname 434 * @package array attributes 435 * @return string source 436 */ 437 public function anonymousClass($attributes = array()) 438 { 439 $source = ''; 440 $source .= $this->getExtObject('anonymousClass', $attributes); 441 442 return $source; 443 } 444 445 /** 446 * returns string as instance of sfExtjs2Var 447 * 448 * @param string var 449 * @return sfExtjs2Var var 450 */ 451 public static function asVar($var) 452 { 453 return new sfExtjs2Var($var); 454 } 455 456 /** 457 * returns source for method 458 * including output of evaled php code 459 * 460 * @param array attributes 461 * @return string source 462 */ 463 public static function method($attributes = array()) 464 { 465 $source = is_array($attributes) && array_key_exists('source', $attributes) ? $attributes['source'] : $attributes; 466 $source = preg_replace_callback( 467 '/(\<\?php)(.*?)(\?>)/si', 468 array('self', '_methodEvalPHP'), 469 $source 470 ); 471 $source = sprintf("function (%s) { %s }", is_array($attributes) && array_key_exists('parameters', $attributes) ? $attributes['parameters'] : '', $source); 472 473 return new sfExtjs2Var($source); 400 474 } 401 475 … … 413 487 $source = ob_get_contents(); 414 488 ob_end_clean(); 415 489 416 490 return $source; 417 }418 419 /**420 * returns source for method421 * including output of evaled php code422 *423 * @param array attributes424 * @return string source425 */426 public static function method($attributes = array())427 {428 $source = is_array($attributes) && array_key_exists('source', $attributes) ? $attributes['source'] : $attributes;429 $source = preg_replace_callback(430 '/(\<\?php)(.*?)(\?>)/si',431 array('self', '_methodEvalPHP'),432 $source433 );434 $source = sprintf("function (%s) { %s }", is_array($attributes) && array_key_exists('parameters', $attributes) ? $attributes['parameters'] : '', $source);435 436 return new sfExtjs2Var($source);437 491 } 438 492 … … 458 512 { 459 513 $attributes = ''; 460 514 461 515 $merged_attributes = $default_attributes; 462 516 if (is_array($custom_attributes) && is_array($default_attributes)) … … 464 518 $merged_attributes = array_merge($default_attributes, $custom_attributes); 465 519 } 466 520 467 521 foreach ($merged_attributes as $key => $value) 468 522 { … … 470 524 { 471 525 $attributes .= sprintf('%s%s:%s', ($attributes === '' ? '' : ','), $key, sfExtjs2Plugin::_quote($key, $value)); 472 } 473 else 526 } 527 else 474 528 { 475 529 $attributes .= sprintf('%s%s', ($attributes === '' ? '' : ','), sfExtjs2Plugin::_quote($key, $value)); 476 530 } 477 531 } 478 532 479 533 return $attributes; 480 534 } 481 535 482 536 /** 483 537 * quotes everything except: … … 485 539 * values that are sfExtjs2Var 486 540 * values and keys that are listed in sf_extjs2_quote_except 487 * 541 * 488 542 * @param string key 489 543 * @param string value 490 544 * @return string attribute 491 */ 545 */ 492 546 private static function _quote($key, $value) 493 547 { 494 if (is_array($value)) 548 if (is_array($value)) 495 549 { 496 550 $attribute = ''; 497 551 foreach ($value as $k => $v) 498 552 { 499 $attribute .= sprintf('%s%s:%s', ($attribute === '' ? '' : ','), $k, sfExtjs2Plugin::_quote($k, $v)); 553 if (!is_numeric($k)) 554 { 555 $attribute .= sprintf('%s%s:%s', ($attribute === '' ? '' : ','), $k, sfExtjs2Plugin::_quote($k, $v)); 556 } 557 else 558 { 559 $attribute .= sprintf('%s%s', ($attribute === '' ? '' : ','), sfExtjs2Plugin::_quote($k, $v)); 560 } 500 561 } 501 562 502 563 $attribute = sprintf('{%s}', $attribute); 503 564 return $attribute; 504 565 } 505 506 if (is_bool($value )) 566 567 if (is_bool($value )) 507 568 { 508 569 $attribute = $value ? 'true' : 'false'; 509 570 return $attribute; 510 571 } 511 512 if (!$value instanceof sfExtjs2Var && sfExtjs2Plugin::_quote_except($key, $value)) 572 573 if (!$value instanceof sfExtjs2Var && sfExtjs2Plugin::_quote_except($key, $value)) 513 574 { 514 575 $attribute = '\''.$value.'\''; 515 576 return $attribute; 516 577 } 517 578 518 579 $attribute = $value; 519 580 return $attribute; … … 524 585 * @param string value 525 586 * @return boolean quote 526 */ 587 */ 527 588 private static function _quote_except($key, $value) 528 589 { 529 590 $quoteExcept = sfConfig::get('sf_extjs2_quote_except'); 530 591 531 592 if (is_int($key) || is_int($value)) 532 593 { 533 594 return false; 534 595 } 535 596 536 597 $listAttributes = sfConfig::get('sf_extjs2_list_attributes'); 537 if (in_array($key, $listAttributes)) 598 if (in_array($key, $listAttributes)) 538 599 { 539 600 return false; 540 601 } 541 602 542 603 foreach ($quoteExcept['key'] as $except) 543 604 { … … 547 608 } 548 609 } 549 610 550 611 foreach ($quoteExcept['value'] as $except) 551 612 { … … 555 616 } 556 617 } 557 618 558 619 return true; 559 620 } … … 562 623 * @param string comment 563 624 * @return string comment 564 */ 625 */ 565 626 private static function _comment($comment) 566 627 { … … 569 630 return $comment; 570 631 } 571 else 632 else 572 633 { 573 634 return ''; … … 575 636 } 576 637 577 } 638 } 578 639 579 640 /** 580 641 * @class sfExtjs2Var 581 * @description sfExtjs2Var is used by quoting logic which ignores variables of this class 642 * @description sfExtjs2Var is used by quoting logic which ignores variables of this class 582 643 * @author Leon van der Ree 583 644 * @version 0.0.01 584 * @last modified 12.13.2007 Leon: 645 * @last modified 12.13.2007 Leon: 585 646 * - created 586 647 */ … … 593 654 $this->var = $var; 594 655 } 595 656 596 657 public function __toString() 597 658 { 598 659 return $this->var; 599 660 } 600 661 601 662 } 602 663 603 664 665 604 666 ?>