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.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.toHtml = function() {
return '
Source
\n' +
this.toHtmlEdit() +
this.toHtmlTabs() +
'Demo
\n' +
this.toHtmlEmbed();
};
exports.Example.prototype.toHtmlEdit = function() {
var out = [];
out.push('\n');
return out.join('');
};
exports.Example.prototype.toHtmlTabs = function() {
var out = [],
self = this;
out.push('');
htmlTabs(this.html);
htmlTabs(this.css);
htmlTabs(this.js);
htmlTabs(this.unit);
htmlTabs(this.scenario);
out.push('
');
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(
'\n' +
'
\n' +
(isCss
? ('\n')
: ('\n') ) +
'
\n');
});
}
};
exports.Example.prototype.toHtmlEmbed = function() {
var out = [];
out.push('');
out.push('
');
return out.join('');
};
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
|
describe('Docs Syntax', function() {
beforeEach(module('bootstrap'));
describe('syntax', function() {
var id, element, document;
beforeEach(inject(function($compile, $rootScope, $document) {
document = $document[0];
//create the HTML elements missing in IE8 for this directive
document.createElement('nav');
element = angular.element(
'<div>' +
'<pre syntax ' +
'syntax-github="gh-url" ' +
'syntax-plunkr="pl-url" ' +
'syntax-fiddle="jf-url">' +
'</pre>' +
'</div>'
);
$compile(element)($rootScope);
$rootScope.$digest();
element = element[0];
document.body.appendChild(element);
}));
it("should properly prepare a github link in the page", function() {
var github = element.querySelector('.syntax-github');
expect(github.innerHTML).toMatch(/View on Github/i);
expect(github.getAttribute('href')).toBe('gh-url');
});
it("should properly prepare a plunkr link in the page", function() {
var plunkr = element.querySelector('.syntax-plunkr');
expect(plunkr.innerHTML).toMatch(/View on Plunkr/i);
expect(plunkr.getAttribute('href')).toBe('pl-url');
});
it("should properly prepare a jsfiddle link in the page", function() {
var jsfiddle = element.querySelector('.syntax-jsfiddle');
expect(jsfiddle.innerHTML).toMatch(/View on JSFiddle/i);
expect(jsfiddle.getAttribute('href')).toBe('jf-url');
});
});
});
|