Skip to content

Commit 1270139

Browse files
author
Evan You
committed
transition classes now defaults to sd-enter and sd-leave, also configurable.
1 parent a617315 commit 1270139

File tree

10 files changed

+56
-105
lines changed

10 files changed

+56
-105
lines changed

src/compiler.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,7 @@ CompilerProto.compile = function (node, root) {
200200
// special attributes to check
201201
var repeatExp,
202202
componentId,
203-
partialId,
204-
transId,
205-
transClass
203+
partialId
206204

207205
// It is important that we access these attributes
208206
// procedurally because the order matters.
@@ -238,12 +236,12 @@ CompilerProto.compile = function (node, root) {
238236
}
239237

240238
} else {
241-
242-
partialId = utils.attr(node, 'partial')
243-
transId = utils.attr(node, 'transition')
244-
transClass = utils.attr(node, 'transition-class')
245239

240+
// check transition property
241+
node.sd_trans = utils.attr(node, 'transition')
242+
246243
// replace innerHTML with partial
244+
partialId = utils.attr(node, 'partial')
247245
if (partialId) {
248246
var partial = compiler.getOption('partials', partialId)
249247
if (partial) {
@@ -252,16 +250,6 @@ CompilerProto.compile = function (node, root) {
252250
}
253251
}
254252

255-
// Javascript transition
256-
if (transId) {
257-
node.sd_trans = transId
258-
}
259-
260-
// CSS class transition
261-
if (transClass) {
262-
node.sd_trans_class = utils.split(transClass)
263-
}
264-
265253
// finally, only normal directives left!
266254
compiler.compileNode(node)
267255
}

src/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module.exports = {
33
prefix : 'sd',
44
debug : false,
55
silent : false,
6+
enterClass : 'sd-enter',
7+
leaveClass : 'sd-leave',
68
attrs : {}
79

810
}

src/directives/repeat.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Observer = require('../observer'),
22
Emitter = require('../emitter'),
33
utils = require('../utils'),
4+
config = require('../config'),
45
transition = require('../transition'),
56
ViewModel // lazy def to avoid circular dependency
67

@@ -95,9 +96,7 @@ module.exports = {
9596
self.ChildVM = self.compiler.getOption('components', componentId) || ViewModel
9697

9798
// extract transition information
98-
self.trans = utils.attr(el, 'transition')
99-
self.transClass = utils.attr(el, 'transition-class')
100-
self.hasTrans = self.trans || self.transClass
99+
self.hasTrans = el.hasAttribute(config.attrs.transition)
101100

102101
// create a comment node as a reference node for DOM insertions
103102
self.ref = document.createComment('sd-repeat-' + self.arg)
@@ -157,10 +156,6 @@ module.exports = {
157156
scope = {},
158157
ref, item
159158

160-
// add transition info
161-
node.sd_trans = this.trans
162-
node.sd_trans_class = utils.split(this.transClass)
163-
164159
// append node into DOM first
165160
// so sd-if can get access to parentNode
166161
if (data) {

src/main.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ var specialAttributes = [
138138
'repeat',
139139
'partial',
140140
'component',
141-
'transition',
142-
'transition-class'
141+
'transition'
143142
]
144143

145144
function updatePrefix () {

src/transition.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
var endEvent = sniffTransitionEndEvent(),
2-
codes = {
1+
var endEvent = sniffTransitionEndEvent(),
2+
config = require('./config'),
3+
enterClass = config.enterClass,
4+
leaveClass = config.leaveClass,
5+
// exit codes for testing
6+
codes = {
37
CSS_E : 1,
48
CSS_L : 2,
59
JS_E : 3,
@@ -24,23 +28,21 @@ var transition = module.exports = function (el, stage, changeState, compiler) {
2428
return codes.INIT
2529
}
2630

27-
var transitionFunctionId = el.sd_trans,
28-
transitionClass = el.sd_trans_class
31+
var transitionId = el.sd_trans
2932

30-
if (transitionFunctionId) {
33+
if (transitionId) {
3134
return applyTransitionFunctions(
3235
el,
3336
stage,
3437
changeState,
35-
transitionFunctionId,
38+
transitionId,
3639
compiler
3740
)
38-
} else if (transitionClass) {
41+
} else if (transitionId === '') {
3942
return applyTransitionClass(
4043
el,
4144
stage,
42-
changeState,
43-
transitionClass
45+
changeState
4446
)
4547
} else {
4648
changeState()
@@ -54,51 +56,46 @@ transition.codes = codes
5456
/**
5557
* Togggle a CSS class to trigger transition
5658
*/
57-
function applyTransitionClass (el, stage, changeState, classes) {
59+
function applyTransitionClass (el, stage, changeState) {
5860

5961
if (!endEvent) {
6062
changeState()
6163
return codes.CSS_SKIP
6264
}
6365

6466
var classList = el.classList,
65-
lastLeaveCallback = el.sd_trans_cb,
66-
className
67+
lastLeaveCallback = el.sd_trans_cb
6768

6869
if (stage > 0) { // enter
6970

70-
className = classes[0]
71-
7271
// cancel unfinished leave transition
7372
if (lastLeaveCallback) {
7473
el.removeEventListener(endEvent, lastLeaveCallback)
7574
el.sd_trans_cb = null
7675
}
7776

7877
// set to hidden state before appending
79-
classList.add(className)
78+
classList.add(enterClass)
8079
// append
8180
changeState()
8281
// force a layout so transition can be triggered
8382
/* jshint unused: false */
8483
var forceLayout = el.clientHeight
8584
// trigger transition
86-
classList.remove(className)
85+
classList.remove(enterClass)
8786
return codes.CSS_E
8887

8988
} else { // leave
9089

91-
className = classes[classes.length > 1 ? 1 : 0]
92-
9390
// trigger hide transition
94-
classList.add(className)
91+
classList.add(leaveClass)
9592
var onEnd = function (e) {
9693
if (e.target === el) {
9794
el.removeEventListener(endEvent, onEnd)
9895
el.sd_trans_cb = null
9996
// actually remove node here
10097
changeState()
101-
classList.remove(className)
98+
classList.remove(leaveClass)
10299
}
103100
}
104101
// attach transition end listener

src/utils.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ function makeHash () {
1313
return Object.create(null)
1414
}
1515

16-
/**
17-
* trim for map
18-
*/
19-
function trim (str) {
20-
return str.trim()
21-
}
22-
2316
var utils = module.exports = {
2417

2518
hash: makeHash,
@@ -36,7 +29,7 @@ var utils = module.exports = {
3629
attr: function (el, type) {
3730
var attr = attrs[type],
3831
val = el.getAttribute(attr)
39-
if (val) el.removeAttribute(attr)
32+
if (val !== null) el.removeAttribute(attr)
4033
return val
4134
},
4235

@@ -54,15 +47,6 @@ var utils = module.exports = {
5447
})
5548
},
5649

57-
/**
58-
* split a transition class string into array
59-
*/
60-
split: function (classString) {
61-
if (classString) {
62-
return classString.split(',').map(trim)
63-
}
64-
},
65-
6650
/**
6751
* Accurate type check
6852
* internal use only, so no need to check for NaN

test/functional/fixtures/transition.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@
1313
padding: 10px;
1414
border: 1px solid #000;
1515
overflow: hidden;
16-
transition: all .2s ease;
16+
transition: all 1s ease;
1717
-moz-transition: all .2s ease;
1818
-webkit-transition: all .2s ease;
1919
}
20-
.test.enter, .test.leave {
20+
.sd-enter, .sd-leave {
2121
height: 0;
2222
padding-top: 0;
2323
padding-bottom: 0;
2424
border-top-width: 0;
2525
border-bottom-width: 0;
2626
}
27-
.test.enter {
27+
.sd-enter {
2828
background-color: #00f;
2929
}
30-
.test.leave {
30+
.sd-leave {
3131
background-color: #0f0;
3232
}
3333
</style>
@@ -46,15 +46,15 @@
4646
class="test"
4747
sd-repeat="item:items"
4848
sd-if="filter(item)"
49-
sd-transition-class="enter, leave"
49+
sd-transition
5050
sd-attr="data-id:item.a">
5151
{{item.a}}
5252
</div>
5353
<div
5454
class="test"
5555
sd-repeat="item:items"
5656
sd-show="filter(item)"
57-
sd-transition-class="enter, leave"
57+
sd-transition
5858
sd-attr="data-id:item.a">
5959
{{item.a}}
6060
</div>

test/functional/specs/transition.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ casper.test.begin('Transition', 23, function (test) {
1616
.thenClick('.button-1')
1717
.wait(minWait, function () {
1818
test.assertElementCount('.test', 4)
19-
test.assertElementCount('.test.leave', 2)
19+
test.assertElementCount('.test.sd-leave', 2)
2020
})
2121
.wait(transDuration, function () {
2222
test.assertElementCount('.test', 3)
23-
test.assertElementCount('.test.leave', 0)
23+
test.assertElementCount('.test.sd-leave', 0)
2424
test.assertNotVisible('.test[data-id="1"]')
2525
})
2626
.thenClick('.button-2')
2727
.wait(minWait, function () {
2828
test.assertElementCount('.test', 3)
29-
test.assertElementCount('.test.leave', 2)
29+
test.assertElementCount('.test.sd-leave', 2)
3030
})
3131
.wait(transDuration, function () {
3232
test.assertElementCount('.test', 2)
@@ -41,7 +41,7 @@ casper.test.begin('Transition', 23, function (test) {
4141
.thenClick('.pop')
4242
.wait(minWait, function () {
4343
test.assertElementCount('.test', 4)
44-
test.assertElementCount('.test.leave', 2)
44+
test.assertElementCount('.test.sd-leave', 2)
4545
})
4646
.wait(transDuration, function () {
4747
test.assertElementCount('.test', 2)

test/unit/specs/transition.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
describe('UNIT: Transition', function () {
22

33
var transition = require('seed/src/transition'),
4+
config = require('seed/src/config'),
45
codes = transition.codes,
5-
endEvent = sniffTransitionEndEvent()
6+
endEvent = sniffTransitionEndEvent(),
7+
enterClass = config.enterClass,
8+
leaveClass = config.leaveClass
69

710
describe('General', function () {
811

@@ -43,7 +46,7 @@ describe('UNIT: Transition', function () {
4346
var el = mockEl('css'),
4447
c = mockChange(function () {
4548
c.called = true
46-
assert.ok(el.classList.contains('enter'))
49+
assert.ok(el.classList.contains(enterClass))
4750
}),
4851
code,
4952
cbCalled = false
@@ -65,7 +68,7 @@ describe('UNIT: Transition', function () {
6568
})
6669

6770
it('should remove the class afterwards', function () {
68-
assert.notOk(el.classList.contains('enter'))
71+
assert.notOk(el.classList.contains(enterClass))
6972
})
7073

7174
it('should return correct code', function () {
@@ -85,7 +88,7 @@ describe('UNIT: Transition', function () {
8588
})
8689

8790
it('should add the class', function () {
88-
assert.ok(el.classList.contains('leave'))
91+
assert.ok(el.classList.contains(leaveClass))
8992
})
9093

9194
it('should call changeState on transitionend', function () {
@@ -102,7 +105,7 @@ describe('UNIT: Transition', function () {
102105
})
103106

104107
it('should remove the class after called', function () {
105-
assert.notOk(el.classList.contains('leave'))
108+
assert.notOk(el.classList.contains(leaveClass))
106109
})
107110

108111
it('should return correct code', function () {
@@ -215,7 +218,7 @@ describe('UNIT: Transition', function () {
215218
function mockEl (type) {
216219
var el = document.createElement('div')
217220
if (type === 'css') {
218-
el.sd_trans_class = ['enter', 'leave']
221+
el.sd_trans = ''
219222
} else if (type === 'js') {
220223
el.sd_trans = 'test'
221224
}

0 commit comments

Comments
 (0)