Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option if to use collection/publication/subscription #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 37 additions & 27 deletions seo.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SEO =
@SEO =
settings: {
title: ''
rel_author: ''
Expand All @@ -12,15 +12,43 @@ SEO =
twitter: true
og: true
set: ['description', 'url', 'title']
# if to use seo route settings from collection
use_collection: false
}

# e.g. ignore('meta', 'fragment')
ignore: (type, value) ->
@settings.ignore[type].push(value) if @settings.ignore[type] and _.indexOf(@settings.ignore[type], value) is -1

# storage of deps handles
deps: []

config: (settings) ->
self = @
_.extend(@settings, settings)

if @settings.use_collection is true
# Get seo settings depending on route
@deps.push Deps.autorun( ->
currentRouteName = self.getCurrentRouteName()
return unless currentRouteName
Meteor.subscribe('seoByRouteName', currentRouteName)
)

# Set seo settings depending on route
@deps.push Deps.autorun( ->
return unless SEO
currentRouteName = self.getCurrentRouteName()
settings = SeoCollection.findOne({route_name: currentRouteName}) or {}
SEO.set(settings)
)

else if @deps.length
# stop all deps
_.each @deps, (handle) ->
handle?.stop()
@deps = []

set: (options, clearBefore=true) ->
@clearAll() if clearBefore

Expand Down Expand Up @@ -130,7 +158,7 @@ SEO =
return

return unless content
content = escapeHtmlAttribute(content)
content = @escapeHtmlAttribute(content)

$('head').append("<meta #{attr} content='#{content}'>")

Expand All @@ -144,29 +172,11 @@ SEO =
removeMeta: (attr) ->
$("meta[#{attr}]").remove()

escapeHtmlAttribute: (string) ->
return ("" + string).replace(/'/g, "&apos;").replace(/"/g, "&quot;")

@SEO = SEO

escapeHtmlAttribute = (string) ->
return ("" + string).replace(/'/g, "&apos;").replace(/"/g, "&quot;")

getCurrentRouteName = ->
router = Router.current()
return unless router
routeName = router.route.name
return routeName

# Get seo settings depending on route
Deps.autorun( ->
currentRouteName = getCurrentRouteName()
return unless currentRouteName
Meteor.subscribe('seoByRouteName', currentRouteName)
)

# Set seo settings depending on route
Deps.autorun( ->
return unless SEO
currentRouteName = getCurrentRouteName()
settings = SeoCollection.findOne({route_name: currentRouteName}) or {}
SEO.set(settings)
)
getCurrentRouteName: ->
router = Router.current()
return unless router
routeName = router.route.name
return routeName