-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstyle.coffee
69 lines (67 loc) · 2.17 KB
/
style.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
prefixes = ["Webkit", "Moz", "ms"]
{camelize, capitalize, isArray, isFunction,clone, isObject} = require "./_helpers"
module.exports =
_name: "style"
_v: 1
_rebind: "$style"
_prio: 700
_mergers: [
require("./_merger").concat(source: "initStyle")
require("./_merger").concat(source: "computedStyle")
]
_attrLookup:
style:
"#": (o) -> @$computed.orWatch o.value, (val) -> @$style.set o.el, val
mixins: [
require "./parseElement"
]
methods:
$style:
normalize: (prop, el = @) ->
prop = camelize(prop)
el = @$parseElement.byString(el)
return prop if el.style[prop]?
prop = capitalize(prop)
for prefix in prefixes
prefixed = prefix+prop
return prefixed if el.style[prefixed]?
return null
normalizeObj: (obj,el) ->
tmp = {}
normalize = @$style.normalize
for k,v of obj
key = normalize(k,el)
tmp[key] = v if key
return tmp
setNormalized: (el, obj) ->
el = @$parseElement.byString(el)
for k,v of obj
if isArray(v) and v[0]?
el.style[k] = v.join("")
else
el.style[k] = v
set: (el, obj) ->
unless obj?
obj = el
el = @
@$style.setNormalized(el,@$style.normalizeObj(obj,el))
connectedCallback: ->
if @_isFirstConnect
for ins in @initStyle
unless isObject(ins[Object.keys(ins)[0]])
ins = this: ins
for el, s of ins
@$style.set el, s
for cs in @computedStyle
for el, c of cs
@$computed.parseAndInit c, cbs: ((el, val) -> @$style.set(el, val)).bind(@,el)
test module.exports, {}, (el) ->
it "should normalize style prop", ->
el.$style.normalize("background-color").should.equal "backgroundColor"
it "should normalize style obj", ->
obj = el.$style.normalizeObj({"background-color":true,position:true})
obj.backgroundColor.should.be.true
obj.position.should.be.true
it "should set style obj on element", ->
el.$style.set({"background-color":"blue",position:"absolute"})
el.should.have.attr "style", "background-color: blue; position: absolute;"