xonecas.io
818
Du

dustymarkdown / files

dustymarkdown / node_modules / dust / docs / index.js

4.3 KB
1jsDump.parsers['function'] = function(fn) {
2  return fn.toString();
3}
4
5function renderDemo() {
6  var tmpl = dust.cache["demo"],
7      source = $('#input-context').val();
8
9  $('#output-text').empty();
10
11  if (tmpl && source) {
12    setPending('#input-context');
13    setPending('#output-text');
14    try {
15      eval("var context = " + source + ";");
16      if (typeof context === 'function') {
17        context = context();
18      }
19      dust.stream("demo", context)
20          .on('data', function(data) {
21            $('#output-text').append(dust.escapeHtml(data));
22          })
23          .on('end', function() {
24            setOkay('#input-context');
25            setOkay('#output-text');
26          })
27          .on('error', function(err) {
28            setError('#input-context', err);
29          });
30    } catch(err) {
31      setError('#input-context', err);
32    }
33  }
34}
35
36function setOkay(sel) {
37  $(sel).next()
38  .removeClass('pending')
39  .addClass('ok')
40  .html('Ready');
41}
42
43function setPending(sel) {
44  $(sel).next()
45  .removeClass('ok')
46  .removeClass('error')
47  .addClass('pending')
48  .html('Pending');
49}
50
51function setError(sel, err) {
52  $(sel).next()
53  .removeClass('pending')
54  .addClass('error')
55  .html(err.toString());
56}
57
58function dump(obj) {
59  return js_beautify(jsDump.parse(obj), {
60    indent_size: 2
61  });
62}
63
64function runSuite() {
65  var suite = new uutest.Suite({
66    start: function() {
67      $('#test-console').empty();
68    },
69    pass: function() {
70      $('#test-console').append(".");
71    },
72    fail: function() {
73      $('#test-console').append("F");
74    },
75    done: function(passed, failed, elapsed) {
76      $('#test-console').append("\n");
77      $('#test-console').append(passed + " passed " + failed + " failed " + "(" + elapsed + "ms)");
78      this.errors.forEach(function(err) {
79        $('#test-console').append("\n");
80        $('#test-console').append(dust.escapeHtml(dumpError(err)));
81      });
82    }
83  });
84  coreSetup(suite, dustExamples.slice(1), dust);
85  suite.run();
86}
87
88function dumpError(err) {
89  var out = err.testName + " -> ";
90  if (!err.message) {
91    err.message = jsDump.parse(err.expected)
92    + " " + err.operator + " " + jsDump.parse(err.actual);
93  }
94  return out + err.stack;
95}
96
97$(document).ready(function() {
98
99  dustExamples.forEach(function(ex) {
100    dust.loadSource(dust.compile(ex.source, ex.name));
101  });
102
103  runSuite();
104
105  $('#tagline').empty().show().css({left: ($(window).width() * .02) + 125});
106  dust.loadSource(dust.compile(dustExamples[0].source, "intro"));
107  dust.stream("intro", dustExamples[0].context())
108      .on('data', function(data) {
109        $('#tagline').append(data);
110      })
111      .on('end', function() {
112        $('#tagline').delay(500).fadeOut('slow');
113      });
114
115  dust.render("select", {
116    examples: dustExamples,
117    selected: function(chk, ctx) {
118      if (ctx.current().name === "replace") return "selected";
119    }
120  }, function(err, output) {
121    $('#select').html(output);
122  });
123
124  $('#select > select').change(function() {
125    var idx = $(this).val();
126    $('#input-source').val(dustExamples[idx].source);
127    $('#input-context').val(dump(dustExamples[idx].context));
128    $('#input-source').change();
129  });
130
131  $('#input-source').change(function() {
132    setPending('#input-source');
133    try {
134      var compiled = dust.compile($(this).val(), "demo");
135      dust.loadSource(compiled);
136      $('#output-js').text(js_beautify(compiled, {
137        indent_size: 2
138      }));
139      setOkay('#input-source');
140    } catch(err) {
141      setError('#input-source', err);
142      return;
143    }
144    renderDemo();
145  });
146
147  $('#input-context').change(renderDemo);
148
149  var sections = $("body > div");
150  var cur_id;
151
152  $(window).scroll(function() {
153    var scrollTop = $(window).scrollTop();
154
155    if (scrollTop === 0) {
156      cur_id = undefined;
157      $('.docked').remove();
158      return;
159    }
160
161    sections.each(function(idx, section) {
162      var sectionTop = section.offsetTop,
163          sectionBottom = sectionTop + section.offsetHeight;
164
165      if (scrollTop >= sectionTop && scrollTop < sectionBottom) {
166        var $hdr = $(section).find('.header').clone();
167        if (section.id !== cur_id) {
168          cur_id = section.id;
169          $('.docked').remove();
170          $hdr.appendTo('body');
171          $hdr.addClass('docked');
172        }
173        return false;
174      }
175    });
176  });
177
178  $('#select > select').change();
179  $(window).scroll();
180
181});