Skip to content

Commit 9c1e425

Browse files
committed
seo improvements
1 parent 50ef5e2 commit 9c1e425

File tree

15 files changed

+429
-20
lines changed

15 files changed

+429
-20
lines changed

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build-config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ var config = {
3232
name: 'github-logo',
3333
alias: 'githubLogo'
3434
},
35+
{
36+
name: 'angular-seo',
37+
alias: 'seo'
38+
},
3539
{
3640
name: 'app'
3741
}

gulpfile.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ gulp.task('app-copy-readme', ['app-copy-clean'], function () {
2424
.pipe(gulp.dest(path.join(config.folders.dest, 'app')));
2525
});
2626

27+
gulp.task('sitemap', [], function () {
28+
return gulp.src(path.join(config.folders.src, 'sitemap.xml'))
29+
.pipe(gulp.dest(config.folders.dest));
30+
});
31+
2732
var appCopy = gulp.tasks['app-copy'];
2833
appCopy.dep.push('app-copy-readme');
34+
appCopy.dep.push('sitemap');
2935

3036
// - - - - 8-< - - - - - - - - - - - - - - - - - - -
3137

karma.conf.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ module.exports = function(config) {
2323
'bower_components/angular-mocks/angular-mocks.js',
2424
'bower_components/lodash/dist/lodash.compat.js',
2525
path.join(buildConfig.folders.src, 'angular-form-gen/angular-form-gen.js'),
26-
// path.join(buildConfig.folders.dest, 'angular-form-gen/angular-form-gen-templates.js'),
27-
path.join(buildConfig.folders.src, 'angular-form-gen/**/*.js')
26+
path.join(buildConfig.folders.src, 'angular-form-gen/**/*.js'),
27+
path.join(buildConfig.folders.src, 'angular-seo/angular-seo.js'),
28+
path.join(buildConfig.folders.src, 'angular-seo/**/*.js')
2829
],
2930

3031

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// ATTENTION!
2+
// DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY
3+
// SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED
4+
angular.module('seo').run(['$templateCache', function($templateCache){
5+
6+
}]);

src/angular-seo/angular-seo.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
var seo = angular.module('seo', []);
2+
3+
seo.config(function ($locationProvider) {
4+
$locationProvider.hashPrefix('!');
5+
});
6+
7+
seo.constant('seoDefaults', {
8+
title: '',
9+
keywords: '',
10+
description: ''
11+
});
12+
13+
seo.factory('seoPageState', function(seoDefaults) {
14+
15+
var state = {};
16+
17+
state._reset = function() {
18+
angular.extend(state, seoDefaults);
19+
};
20+
21+
state._reset();
22+
23+
return state;
24+
25+
});
26+
27+
seo.factory('seoPage', function(seoPageState, $rootScope) {
28+
29+
function reset() {
30+
seoPageState._reset();
31+
}
32+
33+
$rootScope.$on('$routeChangeSuccess', function() {
34+
reset();
35+
});
36+
37+
return {
38+
reset: reset,
39+
title: function(value) {
40+
seoPageState.title = value;
41+
},
42+
keywords: function(value) {
43+
seoPageState.keywords = value;
44+
},
45+
description: function(value) {
46+
seoPageState.description = value;
47+
},
48+
addKeywords: function(value) {
49+
seoPageState.keywords += ', ' + value;
50+
}
51+
};
52+
});
53+
54+
function stateDirective(name) {
55+
56+
var directiveName = 'seoPage' + name.charAt(0).toUpperCase() + name.substring(1);
57+
58+
seo.directive(directiveName, function(seoPage) {
59+
60+
return {
61+
restrict: 'EA',
62+
link: function($scope, $element, $attrs) {
63+
$attrs.$observe(directiveName, function(value) {
64+
if(value) {
65+
seoPage[name](value);
66+
}
67+
});
68+
}
69+
};
70+
71+
});
72+
}
73+
74+
stateDirective('title');
75+
stateDirective('keywords');
76+
stateDirective('description');
77+
78+
seo.directive('seoPageKeywordsAdd', function(seoPage) {
79+
80+
return {
81+
restrict: 'EA',
82+
link: function($scope, $element, $attrs) {
83+
$attrs.$observe('seoPageKeywordsAdd', function(value) {
84+
if(value) {
85+
seoPage.addKeywords(value);
86+
}
87+
});
88+
}
89+
};
90+
91+
});

src/angular-seo/angular-seo.test.js

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
describe('angular-seo', function () {
2+
3+
describe('seoPageState', function () {
4+
5+
var seoDefaults;
6+
7+
beforeEach(function () {
8+
9+
module('seo');
10+
11+
inject(function (_seoDefaults_) {
12+
seoDefaults = _seoDefaults_;
13+
});
14+
15+
});
16+
17+
it('should have the seoDefault values', function () {
18+
19+
// Arrange
20+
21+
var seoPageState = {};
22+
23+
seoDefaults.title = 'my title';
24+
seoDefaults.keywords = 'my keywords';
25+
seoDefaults.description = 'my description';
26+
27+
// Act
28+
29+
inject(function (_seoPageState_) {
30+
seoPageState = _seoPageState_;
31+
});
32+
33+
// Assert
34+
35+
expect(seoPageState.title).toBe(seoDefaults.title);
36+
expect(seoPageState.keywords).toBe(seoDefaults.keywords);
37+
expect(seoPageState.description).toBe(seoDefaults.description);
38+
39+
});
40+
41+
});
42+
43+
describe('seoPage service', function () {
44+
45+
var seoPage, seoPageState, seoDefaults, $rootScope;
46+
47+
beforeEach(function () {
48+
49+
module('seo');
50+
51+
inject(function (_seoPage_, _seoPageState_, _seoDefaults_, _$rootScope_) {
52+
seoPage = _seoPage_;
53+
seoPageState = _seoPageState_;
54+
seoDefaults = _seoDefaults_;
55+
$rootScope = _$rootScope_;
56+
});
57+
58+
});
59+
60+
it('should reset the state to defaults', function () {
61+
62+
// Arrange
63+
64+
seoDefaults.title = 'my title';
65+
seoDefaults.keywords = 'my keywords';
66+
seoDefaults.description = 'my description';
67+
68+
seoPageState.title = 'different title';
69+
seoPageState.keywords = 'different keywords';
70+
seoPageState.description = 'different description';
71+
72+
// Act
73+
74+
seoPage.reset();
75+
76+
// Assert
77+
78+
expect(seoPageState.title).toBe(seoDefaults.title);
79+
expect(seoPageState.keywords).toBe(seoDefaults.keywords);
80+
expect(seoPageState.description).toBe(seoDefaults.description);
81+
});
82+
83+
it('should set properties on the state', function () {
84+
85+
// Arrange
86+
87+
seoPageState.title = 'wrong title';
88+
seoPageState.keywords = 'wrong keyword';
89+
seoPageState.description = 'wrong description';
90+
91+
// Act
92+
93+
seoPage.title('correct title');
94+
seoPage.keywords('correct keywords');
95+
seoPage.description('correct description');
96+
97+
// Assert
98+
99+
expect(seoPageState.title).toBe('correct title');
100+
expect(seoPageState.keywords).toBe('correct keywords');
101+
expect(seoPageState.description).toBe('correct description');
102+
103+
});
104+
105+
it('should reset the properties to default on route change event', function () {
106+
107+
// Arrange
108+
109+
seoPageState.title = seoPageState.keywords = seoPageState.description = 'before route change event';
110+
seoDefaults.title = seoDefaults.keywords = seoDefaults.description = 'after route change event';
111+
112+
// Act
113+
114+
$rootScope.$broadcast('$routeChangeSuccess');
115+
116+
// Assert
117+
118+
expect(seoPageState.title).toBe(seoDefaults.title);
119+
expect(seoPageState.keywords).toBe(seoDefaults.keywords);
120+
expect(seoPageState.description).toBe(seoDefaults.description);
121+
122+
});
123+
124+
it('should add a keywords', function() {
125+
126+
// Arrange
127+
128+
seoPageState.keywords = "current, keywords";
129+
130+
131+
// Act
132+
133+
seoPage.addKeywords('added, new');
134+
135+
// Assert
136+
137+
expect(seoPageState.keywords).toBe("current, keywords, added, new");
138+
139+
});
140+
});
141+
142+
describe('seoPage directives', function () {
143+
144+
var seoPage, seoPageState, seoDefaults, $compile, $scope;
145+
146+
beforeEach(function () {
147+
148+
module('seo');
149+
150+
inject(function (_seoPage_, _seoPageState_, _seoDefaults_, _$compile_, _$rootScope_) {
151+
seoPage = _seoPage_;
152+
seoPageState = _seoPageState_;
153+
seoDefaults = _seoDefaults_;
154+
$compile = _$compile_;
155+
$scope = _$rootScope_.$new();
156+
});
157+
158+
});
159+
160+
it('should set the state properties', function() {
161+
162+
// Arrange
163+
164+
var template =
165+
'<div seo-page-title="title via directive" seo-page-keywords="keywords via directive" seo-page-description="description via directive"></div>';
166+
167+
seoPageState.title = seoPageState.keywords = seoPageState.description = 'unchanged';
168+
169+
// Act
170+
171+
$compile(template)($scope);
172+
$scope.$digest();
173+
174+
// Assert
175+
176+
expect(seoPageState.title).toBe('title via directive');
177+
expect(seoPageState.description).toBe('description via directive');
178+
expect(seoPageState.keywords).toBe('keywords via directive');
179+
180+
});
181+
182+
it('should not set the unspecified state properties', function() {
183+
184+
// Arrange
185+
186+
var template = '<div seo-page-keywords="keywords"></div>';
187+
seoPageState.title = seoPageState.keywords = seoPageState.description = 'unchanged';
188+
189+
// Act
190+
191+
$compile(template)($scope);
192+
$scope.$digest();
193+
194+
// Assert
195+
196+
expect(seoPageState.title).toBe('unchanged');
197+
expect(seoPageState.description).toBe('unchanged');
198+
199+
});
200+
201+
it('should not set empty state properties', function() {
202+
203+
// Arrange
204+
205+
var template = '<div seo-page-title=""></div>';
206+
seoPageState.title = seoPageState.keywords = seoPageState.description = 'unchanged';
207+
208+
// Act
209+
210+
$compile(template)($scope);
211+
$scope.$digest();
212+
213+
// Assert
214+
215+
expect(seoPageState.title).toBe('unchanged');
216+
217+
});
218+
219+
it('should add keywords', function() {
220+
221+
// Arrange
222+
223+
var template = '<div seo-page-keywords-add="my, other, keywords"></div>';
224+
seoPageState.keywords = "my, initial, keywords";
225+
226+
// Act
227+
228+
$compile(template)($scope);
229+
$scope.$digest();
230+
231+
// Assert
232+
233+
expect(seoPageState.keywords).toBe('my, initial, keywords, my, other, keywords');
234+
235+
});
236+
});
237+
238+
});

0 commit comments

Comments
 (0)