Developer Wiki

In-depth guide for developers to create stencils and templates

External JavaScript

A shape may contain a complex and long JavaScript code for calculating behaviors value, moreover, other shapes may also contains this exactly the same code. So it takes time to review and modify shapes. To be convenient, these code should be brought out of shapes and put into <Script></Script> tag that at the collection level.

  <Script></Script> <!-- Share 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: "",
            x: 10,
            y: lineHeight,
            _text : title,
            style: css
            _name: "path",
            _uri: "",
            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=""
    <g id="content" />

As you may notice, in the context of JavaScript execution withtin a stencil behavior, the collection object is bound to the current collection that owns the stencil. The way share 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.