Ver Fonte

enteredView/leftView hooks & test

Evan You há 12 anos atrás
pai
commit
9a132a9637
2 ficheiros alterados com 59 adições e 33 exclusões
  1. 19 6
      src/transition.js
  2. 40 27
      test/unit/specs/transition.js

+ 19 - 6
src/transition.js

@@ -42,7 +42,8 @@ var transition = module.exports = function (el, stage, changeState, compiler) {
         return applyTransitionClass(
             el,
             stage,
-            changeState
+            changeState,
+            compiler
         )
     } else {
         changeState()
@@ -56,7 +57,7 @@ transition.codes = codes
 /**
  *  Togggle a CSS class to trigger transition
  */
-function applyTransitionClass (el, stage, changeState) {
+function applyTransitionClass (el, stage, changeState, compiler) {
 
     if (!endEvent) {
         changeState()
@@ -78,6 +79,7 @@ function applyTransitionClass (el, stage, changeState) {
         classList.add(enterClass)
         // append
         changeState()
+        compiler.execHook('enteredView')
         // force a layout so transition can be triggered
         /* jshint unused: false */
         var forceLayout = el.clientHeight
@@ -96,6 +98,7 @@ function applyTransitionClass (el, stage, changeState) {
                 // actually remove node here
                 changeState()
                 classList.remove(leaveClass)
+                compiler.execHook('leftView')
             }
         }
         // attach transition end listener
@@ -120,20 +123,30 @@ function applyTransitionFunctions (el, stage, changeState, functionId, compiler)
 
     if (stage > 0) { // enter
         if (typeof enter !== 'function') {
-            changeState()
+            doEnter()
             return codes.JS_SKIP_E
         }
-        enter(el, changeState)
+        enter(el, doEnter)
         return codes.JS_E
     } else { // leave
         if (typeof leave !== 'function') {
-            changeState()
+            doLeave()
             return codes.JS_SKIP_L
         }
-        leave(el, changeState)
+        leave(el, doLeave)
         return codes.JS_L
     }
 
+    function doEnter () {
+        compiler.execHook('enteredView')
+        changeState()
+    }
+
+    function doLeave () {
+        compiler.execHook('leftView')
+        changeState()
+    }
+
 }
 
 /**

+ 40 - 27
test/unit/specs/transition.js

@@ -48,6 +48,7 @@ describe('UNIT: Transition', function () {
                     c.called = true
                     assert.ok(el.classList.contains(enterClass))
                 }),
+                compiler = mockCompiler(),
                 code,
                 cbCalled = false
             el.vue_trans_cb = function () {
@@ -56,7 +57,7 @@ describe('UNIT: Transition', function () {
             el.addEventListener(endEvent, el.vue_trans_cb)
 
             it('should add the class before calling changeState()', function () {
-                code = transition(el, 1, c.change, {})
+                code = transition(el, 1, c.change, compiler)
                 assert.ok(c.called)
             })
 
@@ -75,13 +76,18 @@ describe('UNIT: Transition', function () {
                 assert.strictEqual(code, codes.CSS_E)
             })
 
+            it('should have called enteredView hook', function () {
+                assert.ok(compiler.enteredView)
+            })
+
         })
 
         describe('leave', function () {
 
             var el = mockEl('css'),
                 c = mockChange(),
-                code = transition(el, -1, c.change, {})
+                compiler = mockCompiler(),
+                code = transition(el, -1, c.change, compiler)
 
             it('should attach an ontransitionend listener', function () {
                 assert.ok(typeof el.vue_trans_cb === 'function')
@@ -112,6 +118,10 @@ describe('UNIT: Transition', function () {
                 assert.strictEqual(code, codes.CSS_L)
             })
 
+            it('should have called leftView hook', function () {
+                assert.ok(compiler.leftView)
+            })
+
         })
 
     })
@@ -120,29 +130,19 @@ describe('UNIT: Transition', function () {
 
         it('should skip if correspinding option is not defined', function () {
             var c = mockChange(),
-                code = transition(mockEl('js'), 1, c.change, {
-                    getOption: function () {}
-                })
+                code = transition(mockEl('js'), 1, c.change, mockCompiler())
             assert.ok(c.called)
             assert.strictEqual(code, codes.JS_SKIP)
         })
 
         it('should skip if the option is given but the enter/leave func is not defined', function () {
             var c = mockChange(),
-                code = transition(mockEl('js'), 1, c.change, {
-                    getOption: function () {
-                        return {}
-                    }
-                })
+                code = transition(mockEl('js'), 1, c.change, mockCompiler({}))
             assert.ok(c.called)
             assert.strictEqual(code, codes.JS_SKIP_E)
 
             c = mockChange()
-            code = transition(mockEl('js'), -1, c.change, {
-                getOption: function () {
-                    return {}
-                }
-            })
+            code = transition(mockEl('js'), -1, c.change, mockCompiler({}))
             assert.ok(c.called)
             assert.strictEqual(code, codes.JS_SKIP_L)
         })
@@ -157,14 +157,11 @@ describe('UNIT: Transition', function () {
                         assert.strictEqual(el, element)
                         change()
                     }
-                }
+                },
+                compiler = mockCompiler(def)
 
             it('should call the enter function', function () {
-                code = transition(el, 1, c.change, {
-                    getOption: function () {
-                        return def
-                    }
-                })
+                code = transition(el, 1, c.change, compiler)
                 assert.ok(c.called)
             })
 
@@ -172,6 +169,10 @@ describe('UNIT: Transition', function () {
                 assert.strictEqual(code, codes.JS_E)
             })
 
+            it('should have called enteredView hook', function () {
+                assert.ok(compiler.enteredView)
+            })
+
         })
 
         describe('leave', function () {
@@ -184,14 +185,11 @@ describe('UNIT: Transition', function () {
                         assert.strictEqual(el, element)
                         change()
                     }
-                }
+                },
+                compiler = mockCompiler(def)
 
             it('should call the leave function', function () {
-                code = transition(el, -1, c.change, {
-                    getOption: function () {
-                        return def
-                    }
-                })
+                code = transition(el, -1, c.change, compiler)
                 assert.ok(c.called)
             })
 
@@ -199,6 +197,10 @@ describe('UNIT: Transition', function () {
                 assert.strictEqual(code, codes.JS_L)
             })
 
+            it('should have called leftView hook', function () {
+                assert.ok(compiler.leftView)
+            })
+
         })
 
     })
@@ -225,6 +227,17 @@ describe('UNIT: Transition', function () {
         return el
     }
 
+    function mockCompiler (opt) {
+        return {
+            getOption: function () {
+                return opt
+            },
+            execHook: function (hook) {
+                this[hook] = true
+            }
+        }
+    }
+
     function sniffTransitionEndEvent () {
         var el = document.createElement('vue'),
             defaultEvent = 'transitionend',