/** * DOM generation class */ exports.DOM = DOM; exports.htmlEscape = htmlEscape; ////////////////////////////////////////////////////////// function htmlEscape(text){ return text.replace(/&/g, '&').replace(//g, '>'); } function DOM(){ this.out = []; this.headingDepth = 0; } var INLINE_TAGS = { i: true, b: true }; DOM.prototype = { toString: function() { return this.out.join(''); }, text: function(content) { if (typeof content == "string") { this.out.push(htmlEscape(content)); } else if (typeof content == 'function') { content.call(this, this); } else if (content instanceof Array) { this.ul(content); } }, html: function(html) { if (html) { var headingDepth = this.headingDepth; for ( var i = 10; i > 0; --i) { html = html .replace(new RegExp('(<\/?h)' + i + '(>)', 'gm'), function(all, start, end){ return start + (i + headingDepth) + end; }); } this.out.push(html); } }, tag: function(name, attr, text) { if (!text) { text = attr; attr = {}; if (name == 'code') attr['ng:non-bindable'] = ''; } this.out.push('<' + name); for(var key in attr) { this.out.push(" " + key + '="' + attr[key] + '"'); } this.out.push('>'); this.text(text); this.out.push(''); if (!INLINE_TAGS[name]) this.out.push('\n'); }, code: function(text) { this.tag('div', {'ng:non-bindable':''}, function(){ this.tag('pre', {'class':"brush: js; html-script: true;"}, text); }); }, h: function(heading, content, fn){ if (content==undefined || (content instanceof Array && content.length == 0)) return; this.headingDepth++; this.tag('h' + this.headingDepth, heading); var className = typeof heading == 'string' ? {'class': heading.toLowerCase().replace(/[^\d\w_]/mg, '-').replace(/-+/gm, '-')} : null; if (content instanceof Array) { this.ul(content, className, fn); } else if (fn) { this.tag('div', className, function(){ fn.call(this, content); }); } else { this.tag('div', className, content); } this.headingDepth--; }, h1: function(attr, text) { this.tag('h1', attr, text); }, h2: function(attr, text) { this.tag('h2', attr, text); }, h3: function(attr, text) { this.tag('h3', attr, text); }, p: function(attr, text) { this.tag('p', attr, text); }, ul: function(list, attr, fn) { if (typeof attr == 'function') { fn = attr; attr = {}; } this.tag('ul', attr, function(dom){ list.forEach(function(item){ dom.out.push('
  • '); dom.text(fn ? fn(item) : item); dom.out.push('
  • \n'); }); }); } }; /angular.js/tree/docs/src?h=v1.2.14&id=8864e54f1f94883f3e22d7ce7ed87a699467a217'>src/example.js
    blob: 9471b3faa3305620c8830edb3922e630051fd11c (plain)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    
    var seqCount = 0;
    var usedIds = {};
    var makeUnique = {
      'index.html': true,
      'style.css': true,
      'script.js': true,
      'unit.js': true,
      'spec.js': true,
      'scenario.js': true
    }
    
    function ids(list) {
      return list.map(function(item) { return item.id; }).join(' ');
    };
    
    
    exports.Example = function(scenarios) {
      this.module = '';
      this.deps = ['angular.js'];
      this.html = [];
      this.css = [];
      this.js = [];
      this.json = [];
      this.unit = [];
      this.scenario = [];
      this.scenarios = scenarios;
    }
    
    exports.Example.prototype.setModule = function(module) {
      if (module) {
        this.module = module;
      }
    };
    
    exports.Example.prototype.addDeps = function(deps) {
      deps && deps.split(/[\s\,]/).forEach(function(dep) {
        if (dep) {
          this.deps.push(dep);
        }
      }, this);
    };
    
    exports.Example.prototype.addSource = function(name, content) {
      var ext = name == 'scenario.js' ? 'scenario' : name.split('.')[1],
          id = name;
    
      if (makeUnique[name] && usedIds[id]) {
        id = name + '-' + (seqCount++);
      }
      usedIds[id] = true;
      
      this[ext].push({name: name, content: content, id: id});
      if (name.match(/\.js$/) && name !== 'spec.js' && name !== 'unit.js' && name != 'scenario.js') {
        this.deps.push(name);
      }
      if (ext == 'scenario') {
        this.scenarios.push(content);
      }
    };
    
    exports.Example.prototype.enableAnimations = function() {
      this.animations = true;
    };
    
    exports.Example.prototype.disableAnimations = function() {
      this.animations = false;
    };
    
    exports.Example.prototype.toHtml = function() {
      var html = "<h2>Source</h2>\n";
      html += this.toHtmlEdit();
      html += this.toHtmlTabs();
      if(this.animations) {
        html += '<div class="pull-right">';
        html += ' <button class="btn btn-primary" ng-click="animationsOff=true" ng-hide="animationsOff">Animations on</button>';
        html += ' <button class="btn btn-primary disabled" ng-click="animationsOff=false" ng-show="animationsOff">Animations off</button>';
        html += '</div>';
      }
      html += "<h2>Demo</h2>\n";
      html += this.toHtmlEmbed();
      return html;
    };
    
    
    exports.Example.prototype.toHtmlEdit = function() {
      var out = [];
      out.push('<div source-edit="' + this.module + '"');
      out.push(' source-edit-deps="' + this.deps.join(' ') + '"');
      out.push(' source-edit-html="' + ids(this.html) + '"');
      out.push(' source-edit-css="' + ids(this.css) + '"');
      out.push(' source-edit-js="' + ids(this.js) + '"');
      out.push(' source-edit-json="' + ids(this.json) + '"');
      out.push(' source-edit-unit="' + ids(this.unit) + '"');
      out.push(' source-edit-scenario="' + ids(this.scenario) + '"');
      out.push('></div>\n');
      return out.join('');
    };
    
    exports.Example.prototype.toHtmlTabs = function() {
      var out = [],
          self = this;
    
      out.push('<div class="tabbable">');
      htmlTabs(this.html);
      htmlTabs(this.css);
      htmlTabs(this.js);
      htmlTabs(this.json);
      htmlTabs(this.unit);
      htmlTabs(this.scenario);
      out.push('</div>');
      return out.join('');
    
      function htmlTabs(sources) {
        sources.forEach(function(source) {
          var wrap = '',
              isCss = source.name.match(/\.css$/),
              name = source.name;
    
          if (name === 'index.html') {
            wrap = ' ng-html-wrap="' + self.module + ' ' + self.deps.join(' ') + '"';
          }
          if (name == 'scenario.js') name = 'End to end test';
    
          out.push(
            '<div class="tab-pane" title="' + name + '">\n' +
              '<pre class="prettyprint linenums" ng-set-text="' + source.id + '"' + wrap + '></pre>\n' +
              (isCss
                 ? ('<style type="text/css" id="' + source.id + '">' + source.content + '</style>\n')
                 : ('<script type="text/ng-template" id="' + source.id + '">' + source.content + '</script>\n') ) +
            '</div>\n');
        });
      }
    };
    
    exports.Example.prototype.toHtmlEmbed = function() {
      var out = [];
      out.push('<div class="well doc-example-live animate-container"');
      if(this.animations) {
        out.push(" ng-class=\"{'animations-off':animationsOff == true}\"");
      }
      out.push(' ng-embed-app="' + this.module + '"');
      out.push(' ng-set-html="' + this.html[0].id + '"');
      out.push(' ng-eval-javascript="' + ids(this.js) + '">');
      out.push('</div>');
      return out.join('');
    };