component_class
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
component_class [2021/03/08 09:59] – sahl | component_class [2023/08/15 11:49] (current) – sahl | ||
---|---|---|---|
Line 22: | Line 22: | ||
</ | </ | ||
- | ===== 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 47: | Line 52: | ||
</ | </ | ||
- | Note how the configuration properties | + | Note how the configuration properties |
===== HTML output ===== | ===== HTML output ===== | ||
Line 54: | 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 and CSS handling | + | ==== Javascript and CSS loading |
- | Javascript and css files are included by overwriting the // | + | A component will typically need its own css-files and javascript to function properly. These can just be included |
<code php> | <code php> | ||
class textOutput extends Component { | class textOutput extends Component { | ||
- | | + | |
- | | + | static:: |
+ | | ||
+ | parent:: | ||
+ | ); | ||
} | } | ||
</ | </ | ||
- | The javascript file should always use the following template: | + | ==== Using CSS ==== |
- | <code js textoutput.js> | + | Using CSS is easy. Setting the ID of the component is accomplished using the '' |
- | addPlatformComponentHandlerFunction('CLASS_IN_LOWERCASE', function(item) { | + | |
- | // Do your stuff here! | + | ==== Using Javascript ==== |
- | }); | + | |
+ | All components have an associated javascript object named '' | ||
+ | |||
+ | <code js> | ||
+ | var component_object = $('# | ||
</ | </ | ||
- | 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: | + | This object have the following functions: |
+ | ^Function^Description^ | ||
+ | |redraw()|Redraw | ||
+ | |addIOForm(form, func, failfunc)|Set a form to be posted to the component backend. See below.| | ||
+ | |backendIO(values, func)|Send some information to the component backend (and get an answer). See below.| | ||
+ | |timedIO(values, | ||
+ | |removeTimedIO()|Stop sending information to the component backend at a timed interval.| | ||
- | <code js textoutput.js> | + | ==== Communicating between the frontend and backend ==== |
- | | + | |
- | // Do your stuff here! | + | Use the '' |
- | }); | + | |
+ | === Example === | ||
+ | |||
+ | <code js script.js> | ||
+ | mycomponent.backendIO({event: | ||
</ | </ | ||
- | The item variable passed is a Jquery-selector pointing to the DOM node of the component. | + | This will immediately pass the variables |
- | It is very important that all components are initiated before page output is started, as the javascript and css files will not be included otherwise. This also includes sub-components, so if a component uses other components they should also be initiated before the page is started. | + | <code php component.php> |
+ | public function handleIO() : array { | ||
+ | if ($_POST[' | ||
+ | else return [' | ||
+ | } | ||
+ | </ | ||
- | Therefore no component should be initiated in the render-functions. They can be initiated in the '' | ||
- | ===== Component event model ===== | + | ==== Passing forms into the backend |
- | Components are designed to work with the Javascript/ | + | Forms can also be passed into the backend, much like the example above. Here you just use the '' |
- | ^event^Effect^ | + | When the form is submitted it is directed to the '' |
- | |disable|Disables | + | |
- | |enable|Re-enables | + | === Example === |
- | |disable_others|Disable every other component | + | |
- | |enable_others|Re-enables every other component except this.| | + | <code js script.js> |
- | |redraw|Redraws | + | 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 | ||
+ | |properties|Update the component | ||
+ | |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 | ||
+ | |||
+ | <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() { | ||
+ | // Check if name is attached to component | ||
+ | if ($this-> | ||
+ | // Display text | ||
+ | echo 'You have entered ' | ||
+ | // 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 properties | ||
+ | $values = $this-> | ||
+ | $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 [' | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== 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 131: | 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 141: | 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.1615197562.txt.gz · Last modified: 2021/03/08 09:59 by sahl