How to easily document a component

SWAC makes it easy to document your components.
With the concept of functional documentation you get more as a simple documentation webpage from your documentation effort.

Generated documentation pages

You get automatically generated documentation pages from your component code. There are areas showing the expected or optional used data from the datasets given to your component. And the requirements to a template code are documented, too. Furthermore there is a section showing the available templates with highlighted code. And the dependencies of your component are listed, too.
And its easy to create a documentation just do:


<div id="compDescription" swa="EXPLAIN SWAC_edit"></div>
        

Checking datasets

But if you documented the data requirements of your component your documentation is not only used for an explanation page. When a component becomes data it is allways checked against the requirements. If there is a required attribute missing in your data swac will inform you.

Checking templates

Templates are checked, too. If there is a element missing wich you decleared as required then SWAC will give you an error of this.

Data delivery optimisation hints

If the data you give to a component is not completely used by it you get hints (with activated hint mode). So you can optimise your datasource or interface to not deliver more data than needed.

Automatic including dependencies

All files you documented as dependencies are automatically included for you, when the component loads. So you don't have to add script tags on your page.

Start documenting now

The way of documenting is included in the component template. Every documentation belonges to the "desc" attribute of your component.
These are the documentation attributs within the desc-Attribute:

NameAttributeDescription
dependsA file that is needed by the component. May be javascript or css file.
nameName of the dependency
pathPath to the dependend file
descDescription of the dependency
templatesA template that can be used for display of the component
nameName of the template (and the templates filename without fileending)
styleName of the style (and the css filename without fileending)
descDescription of the template
stylesA style can be set to display a component
selcSelector on which the style will be applied
descDescription of the style
reqPerTplA html element required in the template.
selcCSS selector for the element.
descDescription of the element
optPerTplA html element in template, that is optional. If available useually activates a extra function.
selcCSS selector for the element.
descDescription of the element, and which function it activates.
optPerPageA html element somewhere on the page (even out of the requestors scope)
selcCSS selector for the element.
descDescription of the element and what the component does with it.
reqPerSetA attribute that must be in every dataset given to the component.
nameName of the attribute
descDescription of the attribute and why it is needed.
optPerSetA attribute that, if contained in a dataset, has a special effect
nameName of the attribute
descDescription of the attribute and what effect it has, if its in the dataset
optsAn option of the component.
nameName of the option
descDescription of the option. (not their default value, this is get from the component class)
typeDatatype of the parameter. (determined automatically when default value or example is set)
exampleExample value for this option (default value will be documented from value set in class)
funcsA external useable function
nameFunctions name
descDescription of the function.
paramsArray of objects with name and description describing a param.
returnsObject with description of the return value. desc and type attributes are used.

/**
 * Sample component for development of own components
 */

class Sample extends Component {

    /*
     * Constructs a new component object and transfers the config to the
     * object
     */
    constructor(options = {}) {
        super(options);
        this.name = 'Sample';

        this.desc.text = 'Description of this component for documentation.';
        this.desc.depends[0] = {
            name: 'dependency.js',
            path: SWAC.config.swac_root + '/swac/components/Sample/libs/dependency.js',
            desc: 'Description for what the file is required.'
        };
        this.desc.templates[0] = {
            name: 'templatefilename',
            style: 'stylefilename',
            desc: 'Description of the template.'
        };
        this.desc.styles[0] = {
            selc: 'cssSelectorForTheStyle',
            desc: 'Description of the provided style.'
        };
        this.desc.reqPerTpl[0] = {
            selc: 'cssSelectorForRequiredElement',
            desc: 'Description why the element is expected in the template'
        };
        this.desc.optPerTpl[0] = {
            selc: 'cssSelectorForOptionalElement',
            desc: 'Description what is the expected effect, when this element is in the template.'
        };
        this.desc.optPerPage[0] = {
            selc: 'cssSelectorForOptionalElement',
            desc: 'Description what the component does with the element if its there.'
        };
        this.desc.reqPerSet[0] = {
            name: 'id',
            desc: 'The attribute id is required for the component to work properly.'
        };
        this.desc.optPerSet[0] = {
            name: 'nameOfTheAttributeOptionalInEachSet',
            desc: 'Description what is the expected effect, when this attribute is in the set.'
        };
        // opts ids over 1000 are reserved for Component independend options
        this.desc.opts[0] = {
            name: "OptionsName",
            desc: "This is the description of an option"
        };
        // Setting a default value, only applying when the options parameter does not contain this option
        if (!options.OptionsName)
            this.options.OptionsName = 'defaultvalue';
        // Sample for useing the general option showWhenNoData
        if (!options.showWhenNoData)
            this.options.showWhenNoData = true;
        // function ids over 1000 are reserved for Component independend functions
        this.desc.funcs[0] = {
            name: 'name of the function',
            desc: 'Functions description',
            params: [
                {
                    name: 'name of the parameter',
                    desc: 'Description of the parameter',
                    type: 'Expected type(s) of parameter'
                }
            ],
            returns: {
                desc: 'Description of the return value',
                type: 'WatchableSet'
            }
        };

        //Documentation for events the component can fire
        this.desc.events[0] = {
            name: 'swac_REQUESTOR_ID_sample_click',
            desc: 'An event fired when the user clicks on the component.',
            data: 'Delivers the JS event object of the click event.'
        }
    }

    /*
     * This method will be called when the component is complete loaded
     * At this thime the template code is loaded, the data inserted into the 
     * template and even plugins are ready to use.
     */ 
    init() {
        return new Promise((resolve, reject) => {

            // here we can do what we want with the data and template.
            
            // we can access the date over the data attrbute
            console.log('Data inside the sample component:');
            console.log(this.data);
            
            resolve();
        });
    }
}