component_class
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
component_class [2019/11/26 21:35] – created sahl | component_class [2023/08/15 11:49] (current) – sahl | ||
---|---|---|---|
Line 16: | Line 16: | ||
$mycomponent = new helloWorld(); | $mycomponent = new helloWorld(); | ||
+ | |||
+ | // Start page | ||
+ | |||
$mycomponent-> | $mycomponent-> | ||
</ | </ | ||
- | ===== Component | + | ===== Component |
+ | |||
+ | In order for components to work correctly, all variables needed for configuring the component should be implemented as // | ||
- | To make your component configurable, | + | To extend the component from above to make the text and text color configurable, |
<code php> | <code php> | ||
class textOutput extends Component { | class textOutput extends Component { | ||
- | public $configuration = array( | + | public |
- | ' | + | |
- | ' | + | ' |
+ | ' | ||
+ | ); | ||
+ | parent:: | ||
); | ); | ||
Line 44: | Line 52: | ||
</ | </ | ||
- | Note how the configuration properties | + | Note how the configuration properties |
- | ===== HTML and Javascript bindings | + | ===== HTML output |
- | The component will always | + | The base component will render a div with the following properties: |
- | A static class // | + | * The id will match the id set by the '' |
+ | * It will add three data-elements: | ||
- | A class based on the class name of the component, so if the component is named textOutput, the class will be named // | + | ==== Example ==== |
- | Other classes can also be added for internal handling. | + | <code html output.html> |
+ | <div id=" | ||
+ | <span style=" | ||
+ | </ | ||
+ | </ | ||
- | The div will have an auto-generated id or you can set the id with the '' | + | ==== Javascript and CSS loading ==== |
- | The div will have a data-field named configuration which contain the complete component configuration as json, so it can be accessed directly from javascript. | + | A component |
- | ===== Example ===== | + | <code php> |
- | + | ||
- | < | + | |
class textOutput extends Component { | class textOutput extends Component { | ||
- | public | + | public |
- | 'text' | + | |
- | 'color' => 'black' | + | |
+ | parent:: | ||
); | ); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Using CSS ==== | ||
+ | |||
+ | Using CSS is easy. Setting the ID of the component is accomplished using the '' | ||
+ | |||
+ | ==== Using Javascript ==== | ||
+ | |||
+ | All components have an associated javascript object named '' | ||
+ | |||
+ | <code js> | ||
+ | var component_object = $('# | ||
+ | </ | ||
+ | |||
+ | This object have the following functions: | ||
+ | ^Function^Description^ | ||
+ | |redraw()|Redraw the component.| | ||
+ | |addIOForm(form, | ||
+ | |backendIO(values, | ||
+ | |timedIO(values, | ||
+ | |removeTimedIO()|Stop sending information to the component backend at a timed interval.| | ||
+ | |||
+ | ==== Communicating between the frontend and backend ==== | ||
+ | |||
+ | Use the '' | ||
+ | |||
+ | === Example === | ||
+ | |||
+ | <code js script.js> | ||
+ | mycomponent.backendIO({event: | ||
+ | </ | ||
+ | |||
+ | This will immediately pass the variables to the '' | ||
+ | |||
+ | <code php component.php> | ||
+ | public function handleIO() : array { | ||
+ | if ($_POST[' | ||
+ | else return [' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Passing forms into the backend ==== | ||
+ | |||
+ | Forms can also be passed into the backend, much like the example above. Here you just use the '' | ||
+ | |||
+ | When the form is submitted it is directed to the '' | ||
+ | |||
+ | === Example === | ||
+ | |||
+ | <code js script.js> | ||
+ | mycomponent.addIOForm($('# | ||
+ | </ | ||
+ | |||
+ | When the form is submitted (and validates in the frontend), it will be intercepted by the component and the form output will end up in the '' | ||
+ | |||
+ | <code php component.php> | ||
+ | public function handleIO() : array { | ||
+ | $form = new \Platform\Form(); | ||
+ | if (! $form-> | ||
+ | return [' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | There are some requirements to the return value when handling forms. First you must return a value //status// in your return array. This should be 0 if form validation failed, and 1 if form validation succeeded. If the form failed you should also return all form errors in the // | ||
+ | |||
+ | In addition to this, you are free to return whatever values you want. These will be returned to the functions in javascript. | ||
+ | |||
+ | === Form handling without javascript === | ||
+ | |||
+ | For basic form handling, you can do this without using javascript. | ||
+ | |||
+ | <code php component.php> | ||
+ | class myComponent { | ||
+ | ... | ||
+ | $form = new \Platform\Form(); | ||
+ | $this-> | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | This will ensure that the form is handled by the component '' | ||
+ | |||
+ | ==== Sending events directly to the backend ==== | ||
+ | |||
+ | Typically events are handled using the '' | ||
+ | |||
+ | <code php component.php> | ||
+ | class MyComponent { | ||
+ | ... | ||
+ | | ||
+ | |||
+ | | ||
+ | if ($_POST[' | ||
+ | // Handle the custom event | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Special return instructions (reserved words) ==== | ||
+ | |||
+ | When returning an array from the '' | ||
+ | |||
+ | ^Keyword^Description^ | ||
+ | |data|Attach data attributes to the component.| | ||
+ | |destroy|Destroy the component in the DOM| | ||
+ | |properties|Update the component properties in the frontend.| | ||
+ | |redirect / target|Redirect the user to another URL. An optional target can be provided| | ||
+ | |script|Javascript that will be executed in the frontend.| | ||
+ | |trigger|Trigger an event on the component, which will bubble if the component doesn' | ||
+ | |||
+ | === Example === | ||
+ | |||
+ | You can execute generic javascript. | ||
+ | |||
+ | <code php> | ||
+ | public function handleIO() { | ||
+ | return [' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | If you change component properties in the '' | ||
+ | |||
+ | <code php> | ||
+ | public function handleIO() { | ||
+ | return [' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | If the component is redrawable, the redraw can also be executed by returning true in the // | ||
+ | |||
+ | <code php> | ||
+ | public function handleIO() { | ||
+ | return [' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Putting it all together ==== | ||
+ | |||
+ | In this example we will make a component which prompts the user for a name and age, and then displays this information afterwards. This is obtained 100 % in PHP: | ||
+ | |||
+ | <code php example.php> | ||
+ | class myExample extends Component { | ||
+ | |||
+ | public $form = null; | ||
+ | | ||
+ | public function __construct() { | ||
+ | // Set component properties | ||
+ | $this-> | ||
+ | ' | ||
+ | ' | ||
+ | ]); | ||
+ | | ||
+ | // Construct form | ||
+ | $this-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | | ||
+ | // Attach form to component | ||
+ | $this-> | ||
+ | | ||
+ | // Redirect the resetcomponent event to the backend | ||
+ | $this-> | ||
+ | } | ||
+ | | ||
public function renderContent() { | public function renderContent() { | ||
- | echo '<span style=" | + | |
- | | + | if ($this-> |
- | | + | // Display text |
+ | | ||
+ | // Display a link to reset the component | ||
+ | $menuitem = new MenuItem(' | ||
+ | $menuitem-> | ||
+ | | ||
+ | // Display the form | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | public function handleIO() { | ||
+ | // Check if form is submitted. | ||
+ | if ($this-> | ||
+ | // Validate form | ||
+ | $result = $this-> | ||
+ | // Send error if not validated | ||
+ | if (! $result) return ['status' | ||
+ | | ||
+ | $values = $this->form-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | // Return status, updated properties and a redraw request. | ||
+ | return [' | ||
+ | } | ||
+ | // Check if reset was triggered | ||
+ | if ($_POST[' | ||
+ | // Clear properties | ||
+ | $this-> | ||
+ | $this-> | ||
+ | // Return updated properties and a redraw request. | ||
+ | return [' | ||
+ | } | ||
} | } | ||
} | } | ||
+ | </ | ||
- | $mycomponent | + | ===== Components in headless scripts ===== |
- | $mycomponent-> | + | |
- | $mycomponent-> | + | If you use components in headless scripts, which are scripts that doesn' |
- | $mycomponent-> | + | |
- | $mycomponent-> | + | ===== Adding to the DOM using javascript ===== |
+ | |||
+ | If you load components onto your page using javascript (by loading a PHP script which generate components), | ||
+ | |||
+ | <code php> | ||
+ | var html = SOME HTML CONTAINING COMPONENTS. | ||
+ | |||
+ | var node = domnode.html(html); | ||
+ | |||
+ | Platform.apply(node); | ||
</ | </ | ||
- | <code html output.html> | + | ===== Automatic component redraw ===== |
- | <div class=" | + | |
- | <span style=" | + | It is possible for Platform-components to automatically redraw themselves if they are designed correctly. The condition for a component to be able to redraw itself, is that it must be able to do so based solely on the content of its properties. In other words, the component should be able to render by doing just the following: |
- | </div> | + | |
+ | <code php> | ||
+ | $component | ||
+ | $component->setPropertyMap(PROPERTIES NEEDED TO RENDER COMPONENT); | ||
+ | $component->render(); | ||
</ | </ | ||
- | <code javascript example.js> | + | In this case, you can force the component to redraw, by just triggering the Javascript //redraw// event on it. |
- | var configuration | + | |
- | alert ('The text configuration was: ' | + | ==== Advanced redrawing ==== |
+ | |||
+ | If you make a component which isn't able to redraw itself, then you should overwrite | ||
+ | |||
+ | <code php> | ||
+ | protected static $can_redraw = false; | ||
</ | </ | ||
- | ===== Other javascript functionality ===== | + | If a component is to redraw itself, it needs to know if it should validate itself for security reasons. By default it will fail to redraw if it can't detect a valid session. |
- | There are some build-in javascript functionality, which can be triggered with events. | + | To skip this check, then overwrite the static variable: |
- | ^event^Effect^ | + | <code php> |
- | |disable|Disables the component, by rendering a black square over it.| | + | protected static $is_secure = false; |
- | |enable|Re-enables the component.| | + | </ |
- | |disable_others|Disable every other component except this.| | + | |
- | |enable_others|Re-enables every other component except this.| | + | |
- | ==== Prevent disabling | + | In this case everyone can replay the Ajax redraw request and see the content of the component. |
+ | |||
+ | ===== Extending the component javascript class ===== | ||
+ | |||
+ | If you want to extend the javascript object associated with your custom component, this can be done in three easy steps. | ||
+ | |||
+ | ==== 1. Give the component an unique class name ==== | ||
+ | |||
+ | You first need to give your component a unique DOM class name, which is accomplished by overwriting the '' | ||
+ | |||
+ | <code php> | ||
+ | protected static $component_class = ' | ||
+ | </ | ||
+ | |||
+ | ==== 2. Create a new javascript class for your component ==== | ||
+ | |||
+ | Now create a new javascript class for your component. It must extend the Platform.Component class: | ||
+ | |||
+ | <code js> | ||
+ | class MyCustomPlatformComponentClass extends Platform.Component { | ||
+ | |||
+ | // Code goes here | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== 3. Associate the DOM class with the javascript class ==== | ||
+ | |||
+ | This is done like this in javascript: | ||
+ | |||
+ | <code js> | ||
+ | Platform.Component.bindClass(' | ||
+ | </ | ||
+ | | ||
- | Some components are purely visual and shouldn' |
component_class.1574804102.txt.gz · Last modified: 2019/11/26 21:35 by sahl