~holger-seelig/cobweb.js/trunk

« back to all changes in this revision

Viewing changes to src/lib/bezierjs/js/loader.js

  • Committer: Holger Seelig
  • Date: 2017-08-22 04:53:24 UTC
  • Revision ID: holger.seelig@yahoo.de-20170822045324-4of4xxgt79669gbt
Switched to npm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
NodeList.prototype.array = function() {
 
2
  return Array.prototype.slice.call(this);
 
3
};
 
4
 
 
5
function find(qs) {
 
6
  return document.querySelectorAll(qs).array();
 
7
}
 
8
 
 
9
var open = false;
 
10
 
 
11
function loadAll() {
 
12
  var list = find("figure script[type='text/beziercode']");
 
13
  list.forEach(function(e, idx) {
 
14
    var figure = e.parentNode;
 
15
    var code = e.textContent.substring(1).split("\n");
 
16
    e.parentNode.removeChild(e);
 
17
    var indent = "";
 
18
    code[0].replace(/^(\s+)/, function(a,b) { indent = b; });
 
19
    var len = code.length;
 
20
    code = "var curve = " + code.map(function(l) { return l.replace(indent,''); }).join("\n");
 
21
 
 
22
    var content = code + "\n      draw();";
 
23
    content = content + "\n      handleInteraction(getCanvas(), curve).onupdate = function() { reset(); draw(); }";
 
24
    content = "\n    with(Math) { " + content + "\n    }";
 
25
    content = "\n  with(drawfunctions) { " + content + "\n  }";
 
26
    content = "(function(drawfunctions) { " + content + "\n} (bindDrawFunctions( " + idx + " )) );\n";
 
27
 
 
28
    var codearea = document.createElement("div");
 
29
    codearea.classList.add("textarea");
 
30
    codearea.textContent = code;
 
31
    codearea.setAttribute("style", "height: " + (16*(len-1)) + "px!important;");
 
32
    figure.appendChild(codearea);
 
33
    var button = document.createElement("button");
 
34
    button.textContent = "view source";
 
35
    figure.appendChild(button);
 
36
 
 
37
    button.onclick = function(evt) {
 
38
      if(open && open!==codearea) { open.classList.remove("showcode"); }
 
39
      if(codearea.classList.contains("showcode")) {
 
40
        codearea.classList.remove("showcode");
 
41
      } else {
 
42
        codearea.classList.add("showcode");
 
43
        open = codearea;
 
44
      }
 
45
      evt.stopPropagation();
 
46
    };
 
47
 
 
48
    document.addEventListener("click", function() {
 
49
      if(codearea.classList.contains("showcode")) {
 
50
        codearea.classList.remove("showcode");
 
51
      }
 
52
    });
 
53
 
 
54
    var ns = document.createElement("script");
 
55
    ns.textContent = content;
 
56
    document.querySelector("head").appendChild(ns);
 
57
  });
 
58
}
 
59
 
 
60
document.addEventListener("DOMContentLoaded", loadAll);