Sunday, 2019-12-15

I went a home party to Minami-senju.

Host family reserved shared room in their condo.

The room was big.

The room could have a class for twenty people.

We were three families and one single guy, it's me.

There were two kids and one baby.

We have enough space.

Kids were running round and round following radio‐controlled model cars., never stop.

The party was fun and peaceful.

Usage を書いた

Jhtrans の Usage をがんばって書いた。
ソースの見やすさの点では、大分見にくく、残念だ。

GitHub - quwahara/jhtrans: Translate JavaScript array to HTML DOM object. Templating and replace the placeholder with a template.

define(function (require) {
  "use strict";

  // This usage shows preparing template and generating Element node.
  // This code assumes there is a HTML file that has a tag having `class="mount"` attribute.
  // The tag is a mount point for the generated Element node.

  // require Jhtrans
  var Jhtrans = require('../libs/jhtrans');

  // prepare an instance of Jhtrans
  const jht = new Jhtrans();

  // The function putTemplateAll puts templates to the instance.
  // Put templates are reused in translation.
  // 
  // The Object key represents a name of template.
  // The Object value represents declaration of Element node.
  // It calls "Element declaration".
  jht.putTemplateAll({
    // The "row" is template name.
    "row":
      // This Element declaration becomes: `<div class="row">@</div>`.
      [
        // The items of Element declaration represents: 
        "div",                       // -- Tag name
        { "class": "row" },     // -- Attributes, you can put null or omit.
        "@"                            // -- Contents, you can put String, null or omit.
      ],
    "col-4": ["div", { "class": "col-4" }, "@"],
    "two-spans": ["div", null,
      // You can put Array to contents.
      [
        // The items are declaration.
        // They become child element nodes.
        ["span", null, "@1"],
        ["span", null, "@2"]
      ]]
  });

  // The function translate produces Element node by
  // using Translation declarations that is an argument.
  //
  // The value for the "#" is a template name.
  // The value is translated to Element node in templates
  // that is matching with the name.
  //
  // The value for the "@" and/or "@XXX" is a replacement name.
  // Element node can have "@" and/or "@XXX" in contents.
  // For instance, `<div class="row">@</div>`.
  // The function picks the "@" in the Element node.
  // Next, it searches in the declaration.
  // The declaration is like:`{"@": "Hello"}`.
  // Then "@" will be found and it replaces the "@" to "Hello".
  // The Element node becomes `<div class="row">Hello</div>`.
  //
  const translated = jht.translate(
    // Below is Translation declarations.
    // Translation declaration must have a key, "#".
    {
      // The value "row" is translated to an Element node because it is a template name.
      "#": "row",
      // The value for "@" will be replace with "@" in the "row" Element node content.
      "@":
        // A number of Element nodes are generated if the value for "@" was an Array.
        [
          // It translated as a template if the String was a template name.
          "col-4",
          // It is transated recursively if its type is Object.
          {
            "#": "col-4", "@": [
              {
                "#": "two-spans",
                // It translated as a literal string if the String was not a template name.
                "@1": "Hello",
                "@2": ", World!"
              }
            ]
          },
          "col-4"
        ]
    });

  /*
      Above declaration will be transated to like:
      ```
      <div class="row">
        <div class="col-4">@</div>
        <div class="col-4">
          <div><span>Hello</span><span>, World!</span></div>
        </div>
        <div class="col-4">@</div>
      </div>
      ```
  */

  // You can put the translated Elenment node where you want.
  document.querySelector(".mount").appendChild(translated);
});

Jhtrans所感と目標

Translate のデータ構造が決まった。 現状で何ができるのかを明文化したい。

  • JSデータ構造から、DOM Node を生成し、テンプレートとして管理できる。
  • JSデータ構造から、管理しているテンプレートを組み合わせて、新しいDOM Nodeを生成できる。

次の点について明文化したい。

  • テンプレート用のデータ構造
  • 生成用のデータ構造
  • と、その役割、
  • 各用語

次の目標

現状は Text node のtextContentまるまるを、テンプレート置換用のプレースホルダーKeyとみなしている。
できれば、Parseして、複数のプレースホルダーKeyとみなせるようにしたい。

属性の置換にも対応したい。属性名や属性値にもプレースホルダーKeyを記述できるようにしたい。