Update JSON API
[src/app-framework-demo.git] / afm-client / bower_components / tether / docs / js / intro.js
1 (function() {
2   var OUTPUT_HTML, SETUP_JS, activate, deactivate, getOutput, init, run, setupBlock, tethers, uniqueId;
3
4   uniqueId = Tether.Utils.uniqueId;
5
6   SETUP_JS = "yellowBox = $('.yellow-box', $output);\ngreenBox = $('.green-box', $output);\nscrollBox = $('.scroll-box', $output);";
7
8   OUTPUT_HTML = function(key) {
9     return "<div class=\"scroll-box\">\n  <div class=\"scroll-content\">\n    <div class=\"yellow-box\" data-example=\"" + key + "\"></div>\n    <div class=\"green-box\" data-example=\"" + key + "\"></div>\n  </div>\n</div>";
10   };
11
12   tethers = {};
13
14   getOutput = function($block) {
15     var key;
16     key = $block.data('example');
17     if (key && typeof key === 'string') {
18       return $("output[data-example='" + key + "']");
19     } else {
20       return $block.parents('pre').nextAll('output').first();
21     }
22   };
23
24   run = function(key) {
25     var $block, $output, code;
26     if (typeof key === 'string') {
27       $block = $("code[data-example='" + key + "']");
28     } else {
29       $block = key;
30     }
31     key = $block.attr('data-example');
32     $output = getOutput($block);
33     code = $block.text();
34     code = SETUP_JS + code;
35     window.$output = $output;
36     return tethers[key] = eval(code);
37   };
38
39   setupBlock = function($block) {
40     var $output, $scrollBox, $scrollContent, key;
41     key = $block.data('example');
42     $output = getOutput($block);
43     if (!key) {
44       key = uniqueId();
45       $block.attr('data-example', key);
46       $output.attr('data-example', key);
47       $output.find('.tether-element').attr('data-example', key);
48     }
49     $output.html(OUTPUT_HTML(key));
50     $scrollBox = $output.find('.scroll-box');
51     $scrollContent = $scrollBox.find('.scroll-content');
52     $scrollBox.scrollTop(parseInt($scrollContent.css('height')) / 2 - $scrollBox.height() / 2);
53     $scrollBox.scrollLeft(parseInt($scrollContent.css('width')) / 2 - $scrollBox.width() / 2);
54     setTimeout(function() {
55       return $scrollBox.on('scroll', function() {
56         return $output.addClass('scrolled');
57       });
58     });
59     $scrollBox.css('height', "" + ($block.parent().outerHeight()) + "px");
60     if ($output.attr('deactivated') == null) {
61       return run($block);
62     }
63   };
64
65   $(document.body).on('click', function(e) {
66     if ($(e.target).is('output[deactivated]')) {
67       activate($(e.target));
68       return false;
69     } else if ($(e.target).is('output[activated]')) {
70       deactivate($(e.target));
71       return false;
72     }
73   });
74
75   activate = function($output) {
76     var $block, key;
77     $block = $output.prev().find('code');
78     run($block);
79     $output.find('.tether-element').show();
80     key = $output.data('example');
81     $(tethers[key].element).show();
82     tethers[key].enable();
83     $output.removeAttr('deactivated');
84     return $output.attr('activated', true);
85   };
86
87   deactivate = function($output) {
88     var $block, $el, key;
89     $block = $output.prev().find('code');
90     key = $output.data('example');
91     tethers[key].disable();
92     $el = $(tethers[key].element);
93     $el.detach();
94     $output.find('.scroll-content').append($el);
95     $el.hide();
96     $output.removeAttr('activated');
97     return $output.attr('deactivated', true);
98   };
99
100   init = function() {
101     var $blocks, block, _i, _len, _results;
102     $blocks = $('code[data-example]');
103     _results = [];
104     for (_i = 0, _len = $blocks.length; _i < _len; _i++) {
105       block = $blocks[_i];
106       _results.push(setupBlock($(block)));
107     }
108     return _results;
109   };
110
111   window.EXECUTR_OPTIONS = {
112     codeSelector: 'code[executable]'
113   };
114
115   $(init);
116
117 }).call(this);