---
layout: default
---

<script>

  // Main Library-printing function
  function printLibrary(sheet) {
      const store = new SteinStore(
          "https://api.steinhq.com/v1/storages/5e8b937ab88d3d04ae0816a5"
      );
      var rules = "";
      store.read(sheet, { limit: 0, offset: 0 }).then(data => {
          rules = data.reverse();
          libHTML = "";
          // iterating over all the rules
          for (var i = 0; i < rules.length; i++) {
              libHTML += "<div class='library-item'>\n";
              // icon if there is one
              var icon = rules[i]["icon"];
              var iconHTML = "";
              if (icon != null) {
                  iconHTML = "<img src='/assets/elements/" + icon + "' />";
              }
              // first the titles
              var title = rules[i]["communityname"];
              if (title == null) { title = "Untitled"; }
              libHTML += "<h2 class='library-rule-name'>" + 
                  "<a href='/create/?r=" + rules[i]["ruleID"] + "'>" + iconHTML +
                  title + "</a></h2>\n";
              // then the authors
              var author = rules[i]["author-text"];
              if (author != null) {
                  libHTML += "<div class='library-rule-author'>" + author + "</div>";
              }
              // then the structures
              var structure = rules[i]["structure"];
              if (structure != null) {
                  libHTML += "<p class='library-rule-structure'>" + structure;
                  libHTML += "</p>\n\n";
              }
              // then the modules
              var modulesRaw = rules[i]["modules"];
              if (modulesRaw != null) {
                  libHTML += "<div class='library-module-list'>";
                  var modulesDOM = document.createElement("DIV");
                  modulesDOM.innerHTML = "<div>" + modulesRaw + "</div>";
                  var modules = modulesDOM.getElementsByClassName("module");
                  for (var x = 0; x < modules.length; x++) {
                      var moduleName = modules[x].children.item("module-name").innerHTML;
                      libHTML += "<div class='module library-module'>" +
                          moduleName + "</div>\n";
                  }
                  moduleName + "</div>\n";
              }
              libHTML += "</div>\n</div>\n";
          }
          libHTML += "</div>\n";
          document.getElementById("librarylist").innerHTML = libHTML;
      });
  }

  // allows for using alt sheets
  var source = "";
  window.onload = function() {
      if ((window.location.href.indexOf("/library/") != -1)) {
          source = "library";
      } else if ((window.location.href.indexOf("/templates/") != -1)) {
          source = "templates";
      }
      printLibrary(source);
  }
</script>


<article class="post">

  <header class="post-header">
    <h1 class="post-title">{{ page.title }}</h1>
  </header>

  <div class="post-content">
    {{ content }}
    
    <div id="librarylist">
    </div>    
  </div>

</article>
