Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorBreakfast committed Jan 28, 2015
0 parents commit c11ec95
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 0 deletions.
Empty file added .gitignore
Empty file.
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2015 Josef Brandl (MajorBreakfast)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Usage

The `segue-container-component` can animate between your ember components.

Example:
``` Handlebars
{{segue-container
component-name = componentName
segue-for = segueFor
blogPost—pinned-to-blog-post-pane = blogPost
areCommentsShown-from-blog-post—pane = areBlogPostCommentsShown
searchTerm-to-search-pane = searchTerm
}}
```

- `component-name` defines which component should be shown
- `segue-for` lets you define the transition between the old and the new component
- `[property name]-{pinned-to,to,from}-[component name]` define properties on the component
- `pinned-to`: Change triggers animation, passed into `.create()` as inital value
- `to`: Two-way binding (passed-in value wins), passed into `.create()` as inital value
- `from`: Two-way binding (component's value wins)

# Video

I'll do a talk on January 29th at the Ember Munich Meetup about this. This talk will be available on YouTube

# Q&A

** Can I contribute? **

Sure :)

** Why JavaScript controlled animations? **

I'm thinking about adding swipe based switching between components similiar to the back gesture on iOS. That's why I chose to define the animations via JS in favor of CSS animations or transitions.

** Why call it "segue", not "transition"? **

The word "transition" already refers to a route transition in the emberverse. "Animated transition" is too long, so I'm calling it "segue" like it's called in [Cocoa Touch](https://developer.apple.com/technologies/ios/cocoa-touch.html).

** Is ember-cli supported? **

No. Adding compatibility should be straightforward, however. Contributions are welcome!
26 changes: 26 additions & 0 deletions lib/animation-loop-mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Ember from 'ember'

/**
Calls the `update()` method while the view's element exists.
@mixin AnimationLoopMixin
*/
export default Ember.Mixin.create({
startAnimationLoop: function() {
var lastTime
var raf = Ember.run.bind(this, function(time) {
if (lastTime) {
var dt = time - lastTime
// Expected dt is 16ms, don't allow value greater than 100ms
this.trigger('update', Math.min(dt, 100))
}
lastTime = time
this.rafId = requestAnimationFrame(raf) // Next frame
})
this.rafId = requestAnimationFrame(raf)
}.on('didInsertElement'),

stopAnimationLoop: function() {
cancelAnimationFrame(this.rafId)
}.on('willDestroyElement')
})
189 changes: 189 additions & 0 deletions lib/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import Ember from 'ember'
import './style.css!'
import AnimationLoopMixin from './animation-loop-mixin'

// ToDo: Make it a real Component, not just a ContainerView :)
export default Ember.ContainerView.extend(AnimationLoopMixin, {
classNames: ['segue-container-component'],
attributeBindings: ['style'],

componentName: Ember.computed.alias('component-name'),
segueFor: Ember.computed.alias('segue-for'),

init() {
this._super()

this.currentMeta = this.createMetaFor(undefined)
this.pushObject(this.currentMeta.component)
},

isDirty: false,
makeDirty() { this.isDirty = true },

observedProperties: [],
didChangeComponentName: function() {
this.makeDirty()

// Remove old observers
for (var name of this.observedProperties) {
this.removeObserver(name, this, 'makeDirty')
}
this.observedProperties = []

// Add observers
var props = this.extractMatchingProperties(this.get('componentName'))
for (var { name, type } of props) {
if (type == 'pinned-to') {
this.observedProperties.push(name)
this.addObserver(name, this, 'makeDirty')
}
}
}.observes('componentName').on('init'),

extractMatchingProperties(componentName) {
var props = []

var propRegExp = new RegExp(`^(.*?)-(pinned-to|to|from)-${componentName}$`)
var isPropNameABindingDefinitionRegExp = /Binding$/

for (var name in this) {
var [matches, foreignName, type] = propRegExp.exec(name) || []
matches = matches && !foreignName.match(isPropNameABindingDefinitionRegExp)
if (matches) { props.push({ name, foreignName, type }) }
}

return props
},

/*
Creates a meta object. A meta is a simple object with the basic information
for a component:
- componentName
- component
- bindings
@method createMetaFor
@private
*/
createMetaFor(componentName) {
var options = { showcaseContainer: this }
var bindings = []

if (componentName) {
var props = this.extractMatchingProperties(componentName)
for (var { name, foreignName, type } of props) {
switch(type) {
case 'to': // To: Add to options and bindings
options[foreignName] = this.get(name)
bindings.push(Ember.Binding.from(`showcaseContainer.${name}`).to(foreignName))
break
case 'from': // From: Add to bindings with inverse direction
bindings.push(Ember.Binding.from(foreignName).to(`showcaseContainer.${name}`))
break
case 'pinned-to': // Pinned to: Add to options, don't bind
options[foreignName] = this.get(name)
break
}
}

var containerLookup = this.container.lookup('component-lookup:main')
var Component = containerLookup.lookupFactory(componentName)

Ember.assert(`Didn't find component with name "${componentName}"`, Component)
} else {
var Component = Ember.Component
}

var component = this.createChildView(Component, options)

return { componentName, component, bindings }
},

/*
Animation states: 'idle', 'segueing'
```
--- startSegueTo(bMeta) --> ---------.
'idle' 'segueing' updateSegue()
<------- endSegue() ------- <--------.
```
@property animationState
@private
*/
animationState: 'idle',

startSegueTo(bMeta) {
Ember.assert('Expected animation state "idle"', this.animationState === 'idle')

this.animationState = 'segueing'

var aMeta = this.currentMeta

this.aMeta = aMeta
this.bMeta = bMeta
this.currentSegue = this.get('segueFor')(aMeta, bMeta)
this.currentMeta = null
this.t = 0

// A
aMeta.component.trigger('deactivate')
for (var binding of aMeta.bindings) { binding.disconnect(aMeta.component) }

// B
this.pushObject(bMeta.component)
bMeta.component.one('didInsertElement', this, 'updateSegue')
},

endSegue() {
Ember.assert('Expected animation state "segueing"', this.animationState === 'segueing')

this.animationState = 'idle'

var aMeta = this.aMeta
var bMeta = this.bMeta

// B
for (var binding of bMeta.bindings) { binding.connect(bMeta.component) }
bMeta.component.trigger('activate')

// A
this.shiftObject()

this.aMeta = null
this.bMeta = null
this.currentSegue = null
this.currentMeta = bMeta
this.t = undefined
},

updateSegue() {
Ember.assert('Expected animation state "segueing"', this.animationState === 'segueing')

var t_ = this.currentSegue.curve(this.t)
this.currentSegue.animate(t_, this.aMeta, this.bMeta)
},

/*
Called once per frame.
@method update
@private
*/
update(dt) {
switch(this.animationState) {
case 'idle':
if (this.isDirty) {
this.isDirty = false
var meta = this.createMetaFor(this.get('componentName'))
this.startSegueTo(meta)
}
break
case 'segueing':
this.t = Math.min(1, this.t + dt / this.currentSegue.duration)
this.updateSegue()
if (this.t === 1) { this.endSegue() } // End
break
}
}
})
10 changes: 10 additions & 0 deletions lib/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.segue-container-component {
-webkit-perspective: 1000px;
perspective: 1000px;
position: absolute; top: 0; right: 0; bottom: 0; left: 0;
overflow: hidden;
}

.segue-container-component > * {
position: absolute; top: 0; right: 0; bottom: 0; left: 0;
}
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "ember-segue-container",
"version": "0.9.0",
"jspm": {
"main": "lib/component",
"dependencies": {
"css": "^0.1.0"
},
"buildConfig": {
"transpileES6": true,
"minify": true
}
}
}

0 comments on commit c11ec95

Please sign in to comment.