-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjl-tether.coffee
46 lines (35 loc) · 1.37 KB
/
jl-tether.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
###
AngularJS wrapper for HubSpots's Tether
###
Tether = require 'tether'
prefix = 'jlTether'
optionsToEval = ['constraints'] # these tether options aren't mere strings
jlTetherDire = () ->
restrict: 'A'
link: (scope, element, attrs) ->
tetherOptions = {}
# pluck out relevant keys from the attributes and remove prefix
for own key, value of attrs
if key isnt prefix and (key.indexOf prefix) isnt -1
strippedKey = key.replace prefix, ''
optionKey = strippedKey[0].toLowerCase() + strippedKey.slice 1
if optionKey in optionsToEval
tetherOptions[optionKey] = scope.$eval value
else
tetherOptions[optionKey] = value
# extract out 'tetherOptions' attribute and merge to our options object
if tetherOptions.options?
evaledOptions = (scope.$eval tetherOptions.options) || {}
delete tetherOptions.options
for own key, value of evaledOptions
tetherOptions[key] = value
# the default element will be element[0]
if not tetherOptions.element?
tetherOptions.element = element[0]
tetherHandle = new Tether tetherOptions
tetherHandle.position() # initial reposition
# hsTether attribute exists, fill it with the Tether object
if attrs[prefix]?
scope[attrs[prefix]] = tetherHandle
module.exports = angular.module 'jlTether', []
.directive prefix, jlTetherDire