User Tools

Site Tools


component_class

This is an old revision of the document!


Component class

The component class is a base UI class providing a close binding between PHP and Javascript. The class is intended to be subclassed.

Simple component

In order to create a new, simple component just override the renderContent-function, such as:

class helloWorld extends Component {
 
  public function renderContent() {
    echo 'Hello world';
  }
}
 
$mycomponent = new helloWorld();
$mycomponent->render();

Component configuration

To make your component configurable, override the $properties array with some default values. To extend the component from above to make the text and text color configurable, do this:

class textOutput extends Component {
 
  protected $properties = array(
    'text' => 'Hello world',
    'color' => 'black'
  );
 
  public function renderContent() {
    echo '<span style="color:'.$this->color.';">';
    echo $this->text;
    echo '</span>';
  }
}
 
$mycomponent = new textOutput();
$mycomponent->text = 'Platform4PHP is cool';
$mycomponent->color = 'red';
$mycomponent->render();

Note how the configuration properties is easily set and read.

HTML and Javascript bindings

The component will always render in a double div with the following properties:

An outer div with a static class platform_component and an inner div with a class based on the class name of the component, so if the component is named textOutput, the class will be named platform_component_textoutput.

Other classes can also be added for internal handling.

The outer div will have an auto-generated id or you can set the id with the setID-function.

Example

index.php
class textOutput extends Component {
 
  public $configuration = array(
    'text' => 'Hello world',
    'color' => 'black'
  );
 
  public function renderContent() {
    echo '<span style="color:'.$this->color.';">';
    echo $this->text;
    echo '</span>';
  }
}
 
$mycomponent = new textOutput();
$mycomponent->text = 'Platform4PHP is cool';
$mycomponent->color = 'red';
$mycomponent->setID('example_output');
$mycomponent->render();
output.html
<div class="platform_component" id="example_output" data-configuration="...">
<div class="platform_component_textoutput">
<span style="color:red;">Platform4PHP is cool</span>
</div>
</div>

Other javascript functionality

There are some build-in javascript functionality, which can be triggered with events.

eventEffect
disableDisables the component, by rendering a black square over it.
enableRe-enables the component.
disable_othersDisable every other component except this.
enable_othersRe-enables every other component except this.
redrawRedraws the component by fetching its content from the server

Prevent disabling

Some components are purely visual and shouldn't be allowed to be disabled. To prevent a component from becoming disabled, set the static $can_disable variable to false in the php file.

component_class.1593028826.txt.gz · Last modified: 2020/06/24 20:00 by sahl

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki