Development

Changeset 18361

You must first sign up to be able to contribute.

Changeset 18361

Show
Ignore:
Timestamp:
05/17/09 02:49:34 (4 years ago)
Author:
Leon.van.der.Ree
Message:

added new functional sfExtjs3 classes, that can render themselves (recursively) as Javascript

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfExtjs3Plugin/trunk/lib/model/sfExtjs3Function.class.php

    • Property svn:mergeinfo set
    r18315 r18361  
    22/** 
    33 * sfExtjs3Function 
    4  *  
     4 * 
    55 * Instances of this class can render themself as JavaScript functions 
    66 * 
     
    1111   $arguments, 
    1212   $content; 
    13     
     13 
    1414  /** 
    1515   * Creates a new JsObject 
    1616   * 
    17    * @param array $arguments     
    18    * @param string $content      
     17   * @param array $arguments  the arguments that this functions should accept 
     18   * @param mixed $content    anything that can be rendered as a string (arrays will be imploded) 
    1919   */ 
    2020  public function __construct($arguments, $content) 
     
    2424      throw new Exception('arguments should be an array'); 
    2525    } 
    26      
     26 
    2727    $this->arguments = $arguments; 
    2828    $this->content   = $content; 
    2929  } 
    30    
     30 
    3131  /** 
    3232   * Renders this function as a js-text 
     
    3636  public function render() 
    3737  { 
     38    $content = $this->content; 
     39 
     40    // convert array to string by imploding it 
     41    if (is_array($content)) 
     42    { 
     43      $content = implode("\n", $content); 
     44    } 
     45 
    3846    $js  = 'function('; 
    39      
    4047    $js .= implode(', ', $this->arguments); 
    41      
    4248    $js .= ') {'; 
    43      
    44     $js .= $this->content;  
    45      
     49    if ($content) 
     50    { 
     51      $js .= "\n"; 
     52    } 
     53    $js .= $content; 
     54      if ($content) 
     55    { 
     56      $js .= "\n"; 
     57    } 
    4658    $js .= '}'; 
    47      
     59 
    4860    return $js; 
    4961  } 
    50    
     62 
    5163  /** 
    5264   * @see render() 
     
    5668    return $this->render(); 
    5769  } 
    58    
     70 
    5971} 
    6072 
  • plugins/sfExtjs3Plugin/trunk/lib/model/sfExtjs3Object.class.php

    • Property svn:mergeinfo set
    r18315 r18361  
    22/** 
    33 * sfExtjs3Object 
    4  *  
     4 * 
    55 * Instances of this class can render themself as JavaScript (definitions and instances) 
    66 * 
     
    99{ 
    1010  const FUNCTION_SEPARATOR = ' : '; 
    11    
     11 
    1212  protected 
    1313   $objectName, 
    1414   $baseObject, 
    1515   $functions = array(); 
    16     
     16 
    1717  /** 
    1818   * Creates a new JsObject 
    1919   * 
    20    * @param string $name      the name of the new class to be created (can be preceded with dotted namespace 
    21    * @param string $base      the base object which this new class should extend 
    22    * @param array $functions   an associative array of function-names and the sfExtjs3Function itself  
     20   * @param string $objectName the name of the new class to be created (can be preceded with dotted namespace 
     21   * @param string $baseObject the base object which this new class should extend 
     22   * @param array $functions   an associative array of function-names and the sfExtjs3Function itself 
    2323   */ 
    2424  public function __construct($objectName, $baseObject, $functions = array()) 
     
    2626    $this->objectName = trim($objectName); 
    2727    $this->baseObject = trim($baseObject); 
    28      
     28 
    2929    if (!is_array($functions)) 
    3030    { 
     
    3333    foreach ($functions as $functionName => $function) 
    3434    { 
    35       $this->addFunction($functionName, $function);  
     35      $this->addFunction($functionName, $function); 
    3636    } 
    3737  } 
    38    
     38 
    3939  public function getFunctions() 
    4040  { 
    4141    return $this->functions; 
    4242  } 
    43    
     43 
    4444  /** 
    4545   * Adds a function to the extjs-object 
     
    5454      throw new Exception('The function "'.$functionName.'" is already defined for object "'.$this->objectName.'".'); 
    5555    } 
    56      
     56 
    5757    $this->functions[$functionName] = $function; 
    5858  } 
    59    
     59 
    6060  /** 
    6161   * returns the name of this object 
     
    6666    return $this->objectName; 
    6767  } 
    68    
     68 
    6969  /** 
    7070   * renders the construction for a new instance for this object 
     
    7676  { 
    7777    $args = implode(', ', func_get_args()); 
    78      
     78 
    7979    $js  = 'new '.$this->getName().'('; 
    8080    $js .= $args; 
    8181    $js .= ')'; 
    82      
     82 
    8383    return $js; 
    8484  } 
    85    
     85 
    8686  /** 
    8787   * Renders the calling of a function for this object 
     
    9797      throw new Exception('arguments should be an array'); 
    9898    } 
    99      
     99 
    100100    $js  = $this->getName().'.'.$functionName.'('; 
    101      
    102101    $js .= implode(', ', $arguments); 
    103      
    104     $js .= ');'; 
    105      
     102    $js .= ')'; 
     103 
    106104    return $js; 
    107105  } 
    108    
     106 
    109107  /** 
    110108   * text representation of all functions defined for this object 
     
    115113  { 
    116114    $js  = '{'; 
    117      
     115 
    118116    $functions = $this->getFunctions(); 
    119117    if (count($functions)) 
    120118    { 
    121       $firstFunctionName = current(array_keys($functions)); 
    122       $firstFunction = array_shift($functions); 
    123        
    124       $js .= $firstFunctionName.self::FUNCTION_SEPARATOR.$firstFunction; 
    125        
    126       foreach ($functions as $functionName => $function) 
     119      list($firstFunctionName, $firstFunction) = each($functions); 
     120 
     121      $js .= "\n".$firstFunctionName.self::FUNCTION_SEPARATOR.$firstFunction; 
     122 
     123      while (list($functionName, $function) = each($functions)) 
    127124      { 
    128         $js .= ', '.$functionName.self::FUNCTION_SEPARATOR.$function; 
     125        $js .= ",\n".$functionName.self::FUNCTION_SEPARATOR.$function; 
    129126      } 
    130        
    131       $js .= ' ';  
     127 
     128      $js .= "\n"; 
    132129    } 
    133      
     130 
    134131    $js .= '}'; 
    135      
     132 
    136133    return $js; 
    137134  } 
    138    
     135 
    139136  /** 
    140137   * renders the object definition 
     
    145142  { 
    146143    $js = $this->getName(); 
    147      
     144 
    148145    $js .= ' = Ext.extend('.$this->baseObject.', '; 
    149      
     146 
    150147    $js .= $this->renderFunctionDefinitions(); 
    151      
     148 
    152149    $js .= ');'; 
    153      
     150 
    154151    return $js; 
    155152  } 
    156    
     153 
    157154  /** 
    158155   * @see renderDefinition() 
     
    162159    return $this->renderDefinition(); 
    163160  } 
    164     
     161 
    165162} 
    166163