-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathangular-social-links.coffee
133 lines (113 loc) · 4 KB
/
angular-social-links.coffee
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
sharedScopeDefinition = {
handler: '&customHandler'
socialWidth: '@'
socialHeight: '@'
}
angular.module 'socialLinks', []
.factory 'socialLinker', ['$window', '$location', ($window, $location) ->
(urlFactory) ->
(scope, element, attrs) ->
popupWinAttrs = "
status=no,
width=#{scope.socialWidth || 640},
height=#{scope.socialHeight || 480},
resizable=yes,
toolbar=no,
menubar=no,
scrollbars=no,
location=no,
directories=no
"
getCurrentUrl = () ->
attrs.customUrl or $location.absUrl()
attrs.$observe 'customUrl', () ->
url = urlFactory(scope, getCurrentUrl())
if element[0].nodeName is 'A' and (!attrs.href? or attrs.href is '')
element.attr 'href', url
element.attr 'rel', 'nofollow'
handler = (e)->
e.preventDefault()
url = urlFactory(scope, getCurrentUrl())
win = $window.open(
url,
'popupwindow',
popupWinAttrs
).focus()
if attrs.customHandler?
element.on 'click', handler = (event) ->
url = urlFactory(scope, getCurrentUrl())
# Set the href in case we want the default link behaviour
element.attr('href', url)
scope.handler($event: event, $url: url)
else
element.on 'click', handler
scope.$on '$destroy', ->
element.off 'click', handler
]
.directive 'socialFacebook', ['socialLinker', (linker) ->
restrict: 'ACEM',
scope: sharedScopeDefinition,
link: linker (scope, url) ->
shareUrl = ["https://facebook.com/sharer.php?"]
shareUrl.push("u=#{encodeURIComponent(url)}")
shareUrl.join('')
]
.directive 'socialTwitter', ['socialLinker', (linker) ->
restrict: 'ACEM',
scope: angular.extend(status: '@status', sharedScopeDefinition),
link: linker (scope, url) ->
scope.status ||= "Check this out! - #{url}"
"https://twitter.com/intent/tweet?text=#{encodeURIComponent(scope.status)}"
]
.directive 'socialGplus', ['socialLinker', (linker) ->
restrict: 'ACEM',
scope: angular.extend(text: '@text', sharedScopeDefinition),
link: linker (scope, url) ->
shareUrl = ["https://plus.google.com/share?url=#{encodeURIComponent(url)}"]
if scope.text then shareUrl.push("text=#{encodeURIComponent(scope.text)}")
shareUrl.join('&')
]
.directive 'socialPinterest', ['socialLinker', (linker) ->
restrict: 'ACEM',
scope: angular.extend(media: '@media', description: '@description', sharedScopeDefinition),
link: linker (scope, url) ->
"http://pinterest.com/pin/create/button/?url=#{encodeURIComponent(url)}&media=#{encodeURIComponent(scope.media)}&description=#{encodeURIComponent(scope.description)}"
]
.directive 'socialStumbleupon', ['socialLinker', (linker) ->
restrict: 'ACEM',
scope: sharedScopeDefinition,
link: linker (scope, url) ->
"https://stumbleupon.com/submit?url=#{encodeURIComponent(url)}"
]
.directive 'socialLinkedin', ['socialLinker', (linker) ->
restrict: 'ACEM',
scope: angular.extend(title: '@title', sharedScopeDefinition),
link: linker (scope, url) ->
shareUrl = ["https://linkedin.com/shareArticle?url=#{encodeURIComponent(url)}"]
if scope.title then shareUrl.push("title=#{encodeURIComponent(scope.title)}")
shareUrl.join('&')
]
.directive 'socialReddit', ['socialLinker', (linker) ->
restrict: 'ACEM',
scope: sharedScopeDefinition,
link: linker (scope, url) ->
"https://www.reddit.com/submit?url=#{encodeURIComponent(url)}"
]
.directive 'socialVk', ['socialLinker', (linker) ->
restrict: 'ACEM',
scope: sharedScopeDefinition,
link: linker (scope, url) ->
"http://vkontakte.ru/share.php?url=#{encodeURIComponent(url)}"
]
.directive 'socialOk', ['socialLinker', (linker) ->
restrict: 'ACEM',
scope: sharedScopeDefinition,
link: linker (scope, url) ->
"http://www.odnoklassniki.ru/dk?st.cmd=addShare&st._surl=#{encodeURIComponent(url)}"
]
.directive 'socialXing', ['socialLinker', (linker) ->
restrict: 'ACEM',
scope: sharedScopeDefinition,
link: linker (scope, url) ->
"https://www.xing.com/spi/shares/new?url=#{encodeURIComponent(url)}"
]