component_class
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
component_class [2020/08/07 12:18] – 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 |
- | To make your component configurable, override | + | In order for components to work correctly, all variables needed for configuring |
+ | |||
+ | 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 { | ||
- | | + | |
- | ' | + | |
- | ' | + | ' |
+ | ' | ||
+ | ); | ||
+ | parent:: | ||
); | ); | ||
Line 44: | Line 52: | ||
</ | </ | ||
- | Note how the configuration properties | + | Note how the configuration properties |
===== HTML output ===== | ===== HTML output ===== | ||
Line 51: | Line 59: | ||
* The id will match the id set by the '' | * The id will match the id set by the '' | ||
- | * It will add a class // | + | * It will add three data-elements: |
- | * It will add a class // | + | |
- | * It will add up to three data-elements: | + | |
==== Example ==== | ==== Example ==== | ||
<code html output.html> | <code html output.html> | ||
- | < | + | <div id=" |
<span style=" | <span style=" | ||
</ | </ | ||
</ | </ | ||
- | ==== Javascript | + | ==== Javascript |
- | Javascript for the component can be included | + | A component |
<code php> | <code php> | ||
class textOutput extends Component { | class textOutput extends Component { | ||
- | // ... | ||
- | | ||
public function __construct() { | public function __construct() { | ||
- | | + | |
+ | static:: | ||
+ | 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 | ||
+ | } | ||
+ | } | ||
} | } | ||
</ | </ | ||
- | The javascript file should always use the following template: | + | ==== 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. | ||
+ | |||
+ | < | ||
+ | | ||
+ | | ||
+ | } | ||
</ | </ | ||
- | CLASS_IN_LOWERCASE should be exchanged with the class name of the PHP class in lowercase, so for the class textOutput, it should read like this: | + | If you change component properties in the '' |
- | < | + | < |
- | | + | |
- | | + | |
- | }); | + | } |
</ | </ | ||
- | The item variable passed is a Jquery-selector pointing to the DOM node of the component. This will always point to a single node, so there is no need to iterate it. If several components of the same type exists, the function will be called multiple times. | + | If the component is redrawable, the redraw can also be executed by returning true in the // |
- | ===== Component event model ===== | + | <code php> |
+ | public function handleIO() { | ||
+ | return [' | ||
+ | } | ||
+ | </ | ||
- | Components are designed to work with the Javascript/ | + | ==== Putting it all together ==== |
- | ^event^Effect^ | + | In this example we will make a component |
- | |disable|Disables the component, by rendering | + | |
- | |enable|Re-enables | + | <code php example.php> |
- | |disable_others|Disable every other component | + | class myExample extends Component { |
- | |enable_others|Re-enables every other component | + | |
- | |redraw|Redraws | + | public $form = null; |
+ | |||
+ | public function __construct() { | ||
+ | // Set component properties | ||
+ | $this-> | ||
+ | ' | ||
+ | ' | ||
+ | ]); | ||
+ | |||
+ | // Construct form | ||
+ | $this-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | |||
+ | // Attach form to component | ||
+ | $this-> | ||
+ | |||
+ | // Redirect | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | public function renderContent() { | ||
+ | // Check if name is attached to component | ||
+ | if ($this-> | ||
+ | // Display text | ||
+ | echo 'You have entered '.$this-> | ||
+ | // Display a link to reset the component | ||
+ | $menuitem = new MenuItem(' | ||
+ | $menuitem-> | ||
+ | } else { | ||
+ | // Display the form | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public function handleIO() { | ||
+ | // Check if form is submitted. | ||
+ | if ($this-> | ||
+ | // Validate form | ||
+ | $result = $this-> | ||
+ | // Send error if not validated | ||
+ | if (! $result) return [' | ||
+ | // Get values and transfer to component | ||
+ | $values = $this-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | // Return status, updated properties and a redraw request. | ||
+ | | ||
+ | } | ||
+ | // Check if reset was triggered | ||
+ | if ($_POST[' | ||
+ | // Clear properties | ||
+ | $this-> | ||
+ | $this-> | ||
+ | // Return updated properties and a redraw request. | ||
+ | return [' | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Components in headless scripts ===== | ||
+ | |||
+ | If you use components in headless scripts, which are scripts that doesn' | ||
+ | |||
+ | ===== 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); | ||
+ | </ | ||
===== Automatic component redraw ===== | ===== Automatic component redraw ===== | ||
- | 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: | + | 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: |
<code php> | <code php> | ||
Line 129: | Line 326: | ||
</ | </ | ||
- | If a component is to redraw itself, it needs to know if it should validate itself for security reasons. By default it will validate the active user and fail to redraw if it can't detect a valid session. | + | 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. |
To skip this check, then overwrite the static variable: | To skip this check, then overwrite the static variable: | ||
Line 139: | Line 336: | ||
In this case everyone can replay the Ajax redraw request and see the content of the component. | In this case everyone can replay the Ajax redraw request and see the content of the component. | ||
+ | ===== Extending the component javascript class ===== | ||
- | ==== Prevent disabling | + | 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.1596802715.txt.gz · Last modified: 2020/08/07 12:18 by sahl