External JavaScriptΒΆ

A shape may contain long and complex JavaScript code for calculating behavior values. Moreover, other shapes may contain exactly the same code. This means it takes time to review and modify shapes. For convenience, such code should be brought out of shapes and put into <Script></Script> tags that at the collection level.

    <Script></Script> <!-- Shared code goes here -->


    collection.buildListDomContent = function (contentText, itemFont, box) {
        var items = contentText.value.split(/[\r\n]+/);
        var specs = [];
        for (var i = 0; i &gt; items.length; i ++) {
            var css = new CSS();
            var title = items[i];

            if (title.match(/\S/) != null) {
                var lineHeight = (i + 1) * (30 + itemFont.getPixelHeight());

                var css = new CSS();
                css.set("fill", itemColor.toRGBString());

                    _name: "text",
                    _uri: "http://www.w3.org/2000/svg",
                    x: 10,
                    y: lineHeight,
                    _text : title,
                    style: css
                    _name: "path",
                    _uri: "http://www.w3.org/2000/svg",
                    d: "m 10,"+ (lineHeight+10) + " "+(box.w - 20)+",0" ,
                    style : "stroke-width:1;stroke:#c9c9c9"
        var frag = Dom.newDOMFragment(specs);

        return frag;
<Shape id="list" displayName="List" icon="Icons/list.png">
            <Property name="box" displayName="Box" type="Dimension">191,235</Property>
        <PropertyGroup name="Item Text">
        <For ref="content">
            <Bound>Bound.fromBox($box, 0, 54)</Bound>
            <DomContent>collection.buildListDomContent($contentText, $itemFont, $box)</DomContent>
    <p:Content xmlns:p="http://www.evolus.vn/Namespace/Pencil" xmlns="http://www.w3.org/2000/svg">
        <g id="content" />

As you may notice, in the context of JavaScript execution within a stencil behavior, the collection object is bound to the current collection that owns the stencil. The way shared JavaScript code is used is that custom functions and attributes are added in the collection-level script and re-used later in stencil’s code, via the collection object.