i would like to modify each of the grid to my own content. my question is how do i modify the grid to show individual surfaces? for example instead of the array items just have surface1, surface2, surface3 etc..
var Engine = require('famous/core/Engine');
var Surface = require('famous/core/Surface');
var HeaderFooterLayout = require("famous/views/HeaderFooterLayout");
var GridLayout = require("famous/views/GridLayout");
var mainContext = Engine.createContext();
var layout;
createLayout();
addHeader();
addContent();
addFooter();
function createLayout() {
layout = new HeaderFooterLayout({
headerSize: 100,
footerSize: 50
});
mainContext.add(layout);
}
function addHeader() {
layout.header.add(new Surface({
content: "Header",
classes: ["grey-bg"],
properties: {
lineHeight: "100px",
textAlign: "center"
}
}));
}
function addContent() {
layout.content.add(createGrid( 'content', [2, 1] ));
}
function addFooter() {
layout.footer.add(createGrid( 'footer', [3, 1] ));
}
function createGrid( section, dimensions ) {
var grid = new GridLayout({
dimensions: dimensions
});
var surfaces = [];
grid.sequenceFrom(surfaces);
for(var i = 0; i < dimensions[0]; i++) {
surfaces.push(new Surface({
content: section + ' ' + (i + 1),
size: [undefined, undefined],
properties: {
backgroundColor: "hsl(" + (i * 360 / 8) + ", 100%, 50%)",
color: "#404040",
textAlign: 'center'
}
}));
}
return grid;
}
You will always need the array of surfaces to use the generic GridLayout class. I may be missing something, but it seems you are just looking to not use a loop and define the surfaces individually.
var Engine = require('famous/core/Engine');
var Surface = require('famous/core/Surface');
var HeaderFooterLayout = require("famous/views/HeaderFooterLayout");
var GridLayout = require("famous/views/GridLayout");
var mainContext = Engine.createContext();
var layout;
var surface1;
var surface2;
createLayout();
addHeader();
addContent();
addFooter();
function createLayout() {
layout = new HeaderFooterLayout({
headerSize: 100,
footerSize: 50
});
mainContext.add(layout);
}
function addHeader() {
layout.header.add(new Surface({
content: "Header",
classes: ["grey-bg"],
properties: {
lineHeight: "100px",
textAlign: "center"
}
}));
}
function addContent() {
layout.content.add(createGrid( 'content', [2, 1] ));
}
function addFooter() {
layout.footer.add(createGrid( 'footer', [3, 1] ));
}
function createGrid( section, dimensions ) {
var grid = new GridLayout({
dimensions: dimensions
});
var surfaces = [];
grid.sequenceFrom(surfaces);
surface1 = new Surface({
content: "surface 1 content",
size: [undefined, undefined],
properties: {
backgroundColor: "hsl(" + (i * 360 / 8) + ", 100%, 50%)",
color: "#404040",
textAlign: 'center'
}
});
surfaces.push(surface1);
surface2 = new Surface({
content: "surface 2 content",
size: [undefined, undefined],
properties: {
backgroundColor: "hsl(" + (i * 360 / 8) + ", 100%, 50%)",
color: "#404040",
textAlign: 'center'
}
});
surfaces.push(surface2);
// etc
return grid;
}
Related
I want to use GridLayout in ScrollView context and be able to translate surfaces of GridLayout in Z-direction. The problem is that for some reason Z-translate is ignored in GridLayout. Seems like GridLayout has zero perspective because translate in x and y directions just work. How to fix that?
Here is example where z-translate just does nothing :(
define(function(require, exports, module) {
var Engine = require("famous/core/Engine");
var Surface = require("famous/core/Surface");
var Scrollview = require("famous/views/Scrollview");
var View = require("famous/core/View");
var ContainerSurface = require("famous/surfaces/ContainerSurface");
var MouseSync = require("famous/inputs/MouseSync");
var TouchSync = require("famous/inputs/TouchSync");
var GenericSync = require("famous/inputs/GenericSync");
GenericSync.register({ 'mouse': MouseSync, 'touch': TouchSync });
var syncX = new GenericSync(['mouse', 'touch'], { direction: 0 });
var GridLayout = require("famous/views/GridLayout");
var StateModifier = require('famous/modifiers/StateModifier');
var Transform = require("famous/core/Transform");
var mainContext = Engine.createContext();
mainContext.setPerspective(1000);
var grid = new GridLayout({
dimensions: [6, 2]
});
var scrollview = new Scrollview({direction: 0});
syncX.pipe(scrollview);
var surfaces = [];
var mods = [];
var views = [];
for (var i = 0, temp; i < 12; i++) {
view = new View();
mod = new StateModifier();
temp = new Surface({
content: "Surface: " + (i + 1),
size: [200, 200],
properties: {
backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
lineHeight: "200px",
textAlign: "center"
}
});
temp.pipe(syncX);
surfaces.push(temp);
mods.push(mod);
view.add(mod).add(temp);
views.push(view);
}
grid.sequenceFrom(views);
var container = new ContainerSurface({size:[1200,400]});
container.context.setPerspective(1000); ///<----------
container.add(grid);
scrollview.sequenceFrom([container]);
Engine.on('click', function(){
mods[0].setTransform(Transform.translate(2,2,999), {duration:1000});
});
mainContext.add(scrollview);
});
BTW I did test it in Chrome
The GridLayout should not ignore the z-translate as you can see if you run the code below. There are some things missing and extra pieces you do not need in the code you supplied above.
Note: Putting a modifier on the view in the GridLayout the way you are using it may not end up giving you the desired result you want, but the example does show how it works.
Example jsBin
var Engine = require("famous/core/Engine");
var Surface = require("famous/core/Surface");
var Scrollview = require("famous/views/Scrollview");
var View = require("famous/core/View");
var ContainerSurface = require("famous/surfaces/ContainerSurface");
var GridLayout = require("famous/views/GridLayout");
var StateModifier = require('famous/modifiers/StateModifier');
var Transform = require("famous/core/Transform");
var mainContext = Engine.createContext();
mainContext.setPerspective(1000);
var grid = new GridLayout({
dimensions: [6, 2]
});
var scrollview = new Scrollview({direction: 0});
var surfaces = [];
var mods = [];
var views = [];
for (var i = 0; i < 12; i++) {
var view = new View();
var mod = new StateModifier({
size: [200, 200],
transform: Transform.translate(0,0,0.001)
});
var temp = new Surface({
content: "Surface: " + (i + 1),
properties: {
backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
lineHeight: "200px",
textAlign: "center"
}
});
surfaces.push(temp);
mods.push(mod);
temp.pipe(view);
view.add(mod).add(temp);
views.push(view);
temp.on('click', translateOut.bind(temp, i));
}
grid.sequenceFrom(views);
var container = new ContainerSurface({size:[1200,400]});
container.add(grid);
grid.pipe(container);
scrollview.sequenceFrom([container]);
container.pipe(scrollview);
function translateOut(index){
if (!this.moved) {
mods[index].setTransform(Transform.translate(0,0,400), {duration:1000});
} else {
mods[index].setTransform(Transform.translate(0, 0,0.001), {duration:1000});
}
this.moved =!this.moved;
}
mainContext.add(scrollview);
This is due to a newly introduced chrome bug. It looks like Chrome tries to fast path some of its rendering when everything is initially at a z translation of 0. If you add a Surface to the scene at a no zero z with an opacity of 0 you can trick Chrome into drawing correctly.
mainContext
.add(new StateModifier({
transform: Transform.translate(0, 0, 100000000),
size: [1, 1],
opacity: 0
})).add(new Surface({
properties: {backgroundColor: 'chromeHack'}
}));
I will edit this post when I make a simpler example and file as a chrome bug.
Assuming I've a scrollview with direction 1 (vertical). While populating the scrollView, the items do appear and are aligned from top to bottom.
I would like to know if there is a way to reverse this ordering i.e. to make the scrollView be populated from the bottom to the top. A good example of usage is a message list. You want the last message to always appear on the bottom of the list. By default, the message would be aligned on top, which is not convenient.
[EDIT]
I just found we can easily set our own transform according to an offset overriding the outputFrom function callback. For example the following will invert the scroll content:
scrollview.outputFrom(function(offset)
{
return Transform.translate(0, -offset)
});
Did it like you suggested, it works just fine while rotating all by 180˚. Example: http://jsfiddle.net/tamtamchik/LA49a/3/
Famous.loaded(function () {
var Engine = Famous.Core.Engine;
var Surface = Famous.Core.Surface;
var Scrollview = Famous.Views.Scrollview;
var Timer = Famous.Utilities.Timer;
var Transform = Famous.Core.Transform;
var StateModifier = Famous.Modifiers.StateModifier;
var ContainerSurface = Famous.Surfaces.ContainerSurface;
var mainContext = Engine.createContext();
var scrollview = new Scrollview();
var surfaces = [];
var i = 0;
var surfaceHeight = 50;
scrollview.sequenceFrom(surfaces);
Timer.setInterval(function () {
var container = new ContainerSurface({
size: [undefined, surfaceHeight]
});
var temp = new Surface({
content: "Surface: " + (i + 1),
size: [undefined, surfaceHeight],
properties: {
backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
lineHeight: "50px",
textAlign: "center"
}
});
var surfaceRotate = new StateModifier({
transform: Transform.rotateZ(Math.PI)
});
var surfacePush = new StateModifier({
transform: Transform.translate(window.innerWidth, surfaceHeight)
});
container.add(surfacePush).add(surfaceRotate).add(temp);
container.pipe(scrollview);
temp.pipe(scrollview);
surfaces.unshift(container);
i++;
}, 400);
var rotate = new StateModifier({
transform: Transform.rotateZ(Math.PI)
});
var push = new StateModifier({
transform: Transform.translate(window.innerWidth, window.innerHeight)
});
mainContext.add(push).add(rotate).add(scrollview);
});
Well as famo.us documentation says: "Scrollview will lay out a collection of renderables sequentially in the specified direction", so it will order the surfaces in the order you provide.
My advice is to add the surfaces to a list and then just add them to the ScrollView in reverse.
What you are looking for is unshift(). You can view the following code run at codefamo.us
var Engine = require('famous/core/Engine');
var Surface = require('famous/core/Surface');
var StateModifier = require('famous/modifiers/StateModifier');
var Transform = require('famous/core/Transform');
var Easing = require('famous/transitions/Easing');
var Scrollview = require('famous/views/Scrollview');
var mainContext = Engine.createContext();
var items = [];
var scroll = new Scrollview();
mainContext.add(scroll);
var counter=0;
scroll.sequenceFrom(items);
function addOne() {
setTimeout(function() {
var surface = new Surface({
size: [100, 100],
content: 'surf: '+counter++,
properties: {
textAlign: 'center',
lineHeight: '20px'
}
});
items.unshift(surface);
if (counter<10)
addOne();
},2000);
}
addOne();
I'm very new to famo.us. I'm trying to extend this previous question about swiping items in a scrollview.
I want my version to have background surfaces behind the draggable which are revealed as the user swipes. I also want the draggable to snap back if the user does not complete a full swipe.
The two problems with it right now are:
1) The surfaces which I want to be behind the draggable are not behind the draggable. They should be hidden by it until the user swipes (I've made them transparent so the draggable can be seen).
2) the setPosition w/ transition causes an error: Uncaught TypeError: Cannot read property 'SUPPORTS_MULTIPLE' of undefined. If I don't provide the transition it works, but a transition would be preferred.
I should note also that I don't know if using a ContainerSurface is a good way of doing this so that could be part of the problem.
Code:
using: http://code.famo.us/famous/0.2/famous.js
var Engine = require("famous/core/Engine");
var Surface = require("famous/core/Surface");
var Scrollview = require("famous/views/Scrollview");
var RenderNode = require('famous/core/RenderNode');
var Transform = require('famous/core/Transform');
var Draggable = require('famous/modifiers/Draggable');
var ContainerSurface = require("famous/surfaces/ContainerSurface");
var StateModifier = require('famous/modifiers/StateModifier');
var mainContext = Engine.createContext();
var scrollview = new Scrollview();
var surfaces = [];
scrollview.sequenceFrom(surfaces);
var inFrontModifier = new StateModifier({
transform: Transform.translate(0, 0, 1)
});
for (var i = 0; i < 15; i++) {
var container = new ContainerSurface({
size: [undefined, 50],
properties: {
overflow: 'hidden'
}
});
var draggable = new Draggable( {
xRange: [-160, 160],
yRange: [0, 0]
});
draggable.dragId = i;
draggable.on('end', function(e) {
if (e.position[0] == 160) {
console.log('yes surface', this.dragId);
}
else if (e.position[0] == -160) {
console.log('no surface', this.dragId);
}
else {
this.setPosition([0,0,0], {
method: 'snap',
period: 300
});
}
});
var item = new Surface({
content: "Item: " + (i + 1),
size: [undefined, 50],
properties: {
backgroundColor: "lightgrey",
borderBottom: "1px solid grey",
lineHeight: "50px",
textAlign: "center"
}
});
var backgroundYesModifier = new StateModifier({
//on the left
origin: [0,0]
});
var backgroundYes = new Surface({
content: "Yes",
size: [160, 50],
properties: {
backgroundColor: "rgba(0,255,0,0.2)",
lineHeight: "50px",
textAlign: "center"
}
});
var backgroundNoModifier = new StateModifier({
//on the right
origin: [1,0]
});
var backgroundNo = new Surface({
content: "No",
size: [160, 50],
properties: {
backgroundColor: "rgba(255,0,0,0.2)",
lineHeight: "50px",
textAlign: "center"
}
});
var node = new RenderNode(draggable);
node.add(item);
//try to put the draggable in front of the background
container.add(inFrontModifier).add(node);
//add the background
container.add(backgroundNoModifier).add(backgroundNo);
container.add(backgroundYesModifier).add(backgroundYes);
item.pipe(draggable);
item.pipe(scrollview);
surfaces.push(container);
}
mainContext.add(scrollview);
The "on top of" can be solved simply by adding a zIndex=4 (4 is an arbitrary choice above your other zIndexes.)
I added the transition by using a slightly different form of the transition.
And while it is known that you don't want to create very many ContainerSurfaces in famo.us, I don't know what "too many" is and I don't know how to get the "hidden" to work otherwise.
Here is my version which I think does what you want:
var Engine = require("famous/core/Engine");
var Surface = require("famous/core/Surface");
var Scrollview = require("famous/views/Scrollview");
var RenderNode = require('famous/core/RenderNode');
var Transform = require('famous/core/Transform');
var Draggable = require('famous/modifiers/Draggable');
var ContainerSurface = require("famous/surfaces/ContainerSurface");
var StateModifier = require('famous/modifiers/StateModifier');
var Easing = require('famous/transitions/Easing');
var mainContext = Engine.createContext();
var scrollview = new Scrollview();
var surfaces = [];
scrollview.sequenceFrom(surfaces);
var inFrontModifier = new StateModifier({
transform: Transform.translate(0, 0, 1)
});
for (var i = 0; i < 15; i++) {
var container = new ContainerSurface({
size: [undefined, 50],
properties: {
overflow: 'hidden'
}
});
var draggable = new Draggable( {
xRange: [-160, 160],
yRange: [0, 0]
});
draggable.dragId = i;
draggable.on('end', function(e) {
if (e.position[0] == 160) {
console.log('yes surface', this.dragId);
}
else if (e.position[0] == -160) {
console.log('no surface', this.dragId);
}
else {
this.setPosition([0,0,0], {
curve: Easing.outBounce,
duration: 300
});
}
});
var item = new Surface({
content: "Item: " + (i + 1),
size: [undefined, 50],
properties: {
backgroundColor: "lightgrey",
borderBottom: "1px solid grey",
lineHeight: "50px",
textAlign: "center",
zIndex:4
}
});
var backgroundYesModifier = new StateModifier({
//on the left
origin: [0,0]
});
var backgroundYes = new Surface({
content: "Yes",
size: [160, 50],
properties: {
backgroundColor: "rgba(0,255,0,0.2)",
lineHeight: "50px",
textAlign: "center"
}
});
var backgroundNoModifier = new StateModifier({
//on the right
origin: [1,0]
});
var backgroundNo = new Surface({
content: "No",
size: [160, 50],
properties: {
backgroundColor: "rgba(255,0,0,0.2)",
lineHeight: "50px",
textAlign: "center"
}
});
var node = new RenderNode(draggable);
node.add(item);
//try to put the draggable in front of the background
container.add(inFrontModifier).add(node);
//add the background
container.add(backgroundNoModifier).add(backgroundNo);
container.add(backgroundYesModifier).add(backgroundYes);
item.pipe(draggable);
item.pipe(scrollview);
surfaces.push(container);
}
mainContext.add(scrollview);
I recently started to play with Famo.us framework, and trying to build a simple HeaderFooterLayout for switching between different Scrollviews.
The issue I have is when page is loading we can see the Scrollview pass in front of the bottom TabBar.
Another strange thing is that this issue occur once two.
In fact it's not really a TabBar widget but a GridLayout where I push Surfaces which will act as buttons.
I know with Famo.us the order in which you instantiate your Surfaces can affect z-index but I've already verify this point.
I surely misunderstand something with the RenderTree usage but I can't figure out what.
Here's my code :
define(function(require, exports, module) {
var Engine = require("famous/core/Engine");
var RenderNode = require('famous/core/RenderNode');
var Surface = require("famous/core/Surface");
var ViewSequence = require("famous/core/ViewSequence");
var StateModifier = require('famous/modifiers/StateModifier');
var Utility = require("famous/utilities/Utility");
var EdgeSwapper = require("famous/views/EdgeSwapper");
var GridLayout = require("famous/views/GridLayout");
var HeaderFooterLayout = require("famous/views/HeaderFooterLayout");
var Scrollview = require("famous/views/Scrollview");
var NavigationBar = require("famous/widgets/NavigationBar");
var TabBar = require("famous/widgets/TabBar");
var ToggleButton = require("famous/widgets/ToggleButton");
var mainContext = Engine.createContext();
var layout = new HeaderFooterLayout({ headerSize: 50, footerSize: 50 });
var edgeswapper = new EdgeSwapper({
options: {
size: [undefined, window.innerHeight - 100]
}
});
var homeScrollview = new Scrollview();
var bookmarksScrollview = new Scrollview();
var messagesScrollview = new Scrollview();
var profileScrollview = new Scrollview();
var homeScrollviewSurfaces = [];
homeScrollview.sequenceFrom(homeScrollviewSurfaces);
for (var i = 0, temp; i < 50; i++) {
temp = new Surface({
content: "Surface: " + (i + 1),
size: [undefined, 50],
properties: {
backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
lineHeight: "50px",
textAlign: "center"
}
});
temp.pipe(homeScrollview);
homeScrollviewSurfaces.push(temp);
}
var bookmarksScrollviewSurfaces = [];
bookmarksScrollview.sequenceFrom(bookmarksScrollviewSurfaces);
for (var i = 0, temp; i < 50; i++) {
temp = new Surface({
content: "Surface: " + (i + 1),
size: [undefined, 50],
properties: {
backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
lineHeight: "50px",
textAlign: "center"
}
});
temp.pipe(bookmarksScrollview);
bookmarksScrollviewSurfaces.push(temp);
}
var messagesScrollviewSurfaces = [];
messagesScrollview.sequenceFrom(messagesScrollviewSurfaces);
for (var i = 0, temp; i < 50; i++) {
temp = new Surface({
content: "Surface: " + (i + 1),
size: [undefined, 50],
properties: {
backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
lineHeight: "50px",
textAlign: "center"
}
});
temp.pipe(messagesScrollview);
messagesScrollviewSurfaces.push(temp);
}
var profileScrollviewSurfaces = [];
profileScrollview.sequenceFrom(profileScrollviewSurfaces);
for (var i = 0, temp; i < 50; i++) {
temp = new Surface({
content: "Surface: " + (i + 1),
size: [undefined, 50],
properties: {
backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
lineHeight: "50px",
textAlign: "center"
}
});
temp.pipe(profileScrollview);
profileScrollviewSurfaces.push(temp);
}
var navigationBar = new NavigationBar({
size: [undefined, layout.headerSize],
backClasses: ["back"],
backContent: "",
classes: ["navbar"],
content: 'NavigationBar',
moreClasses: ["more"],
moreContent: ""
});
var tabBar = new GridLayout({
dimensions: [4, 1],
cellSize: [undefined, 50],
transition: false
});
var homeButton = new Surface({
size: [undefined, 50],
content: '<span class="tabbar-icon">'+
'<div class="glyphicon glyphicon-leaf"></div>'+
'</span>'+
'<span class="tabbar-title">Home</span>'
});
var bookmarksButton = new Surface({
size: [undefined, 50],
content: '<span class="tabbar-icon">'+
'<div class="glyphicon glyphicon-heart"></div>'+
'</span>'+
'<span class="tabbar-title">Bookmarks</span>'
});
var messagesButton = new Surface({
size: [undefined, 50],
content: '<span class="tabbar-icon">'+
'<div class="glyphicon glyphicon-envelope"></div>'+
'</span>'+
'<span class="tabbar-title">Messages</span>'
});
var profileButton = new Surface({
size: [undefined, 50],
content: '<span class="tabbar-icon">'+
'<div class="glyphicon glyphicon-user"></div>'+
'</span>'+
'<span class="tabbar-title">Profile</span>'
});
tabBar.sequenceFrom([
homeButton,
bookmarksButton,
messagesButton,
profileButton
]);
layout.content.add(edgeswapper);
layout.header.add(navigationBar);
layout.footer.add(tabBar);
edgeswapper.show(homeScrollview);
homeButton.on("click", function() { edgeswapper.show(homeScrollview); });
bookmarksButton.on("click", function() { edgeswapper.show(bookmarksScrollview); });
messagesButton.on("click", function() { edgeswapper.show(messagesScrollview); });
profileButton.on("click", function() { edgeswapper.show(profileScrollview); });
mainContext.add(layout);
});
What was happening is that for the other scrollviews that weren't shown, the DOM elements associated with their Surfaces had not yet been created but the tab bar's had. When you then showed the other Scrollviews, new DOM elements are created and by default in DOM, when DOM elements have the same CSS zIndex and the same z-transform, newer DOM elements are layered on top. To combat this, try adding a modifier to your tab bar to explicitly transform the footer forwards in Z space.
Try this:
layout.footer.add(new Modifier({
transform: Transform.translate(0, 0, 1)
})).add(tabBar);
Using default DOM layering gets really hairy so it is generally useful to understand how and when to explicitly do layering with z translations or css zIndex.
I'm looking to implement similar functionality to Mailbox (http://www.mailboxapp.com/) where you can swipe an individual item within a list in order to action it, using Famous (http://famo.us).
I tried to add the 'Draggable' modifier to each list item, however it seems that you can't add modifiers to surfaces that are part of a Scrollview.
Anyone (from Famous or otherwise) know how I might do this?
Figured it out. In order to modifiers to surfaces inside a scrollview, they need to be wrapped in a RenderNode.
var Engine = require("famous/core/Engine");
var Surface = require("famous/core/Surface");
var Scrollview = require("famous/views/Scrollview");
var RenderNode = require('famous/core/RenderNode');
var Transform = require('famous/core/Transform');
var Draggable = require('famous/modifiers/Draggable');
var mainContext = Engine.createContext();
var scrollview = new Scrollview();
var surfaces = [];
scrollview.sequenceFrom(surfaces);
for (var i = 0, temp; i < 40; i++) {
draggable = new Draggable( {
xRange: [-220, 220],
yRange: [0, 0],
});
item = new Surface({
content: "Surface: " + (i + 1),
size: [undefined, 200],
properties: {
backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
lineHeight: "200px",
textAlign: "center"
}
});
node = new RenderNode(draggable)
node.add(item);
item.pipe(draggable);
item.pipe(scrollview);
surfaces.push(node);
}
mainContext.add(scrollview);
via # (markmarijnissen) Famo.us Scrollview height)
Explanation: In a Scrollview, the sequenceFrom method creates a ViewSequence from the array. As long as the items being added can return a getSize(), the rendering will work correctly. The RenderNode meets this criteria. You can also create a custom View because it extends RenderNode also.
Below is a code snippet that uses a custom View that allows the surface to drag and shows a response to the drag direction based on the final position criteria.
define('main', function(require, exports, module) {
var Engine = require("famous/core/Engine");
var Scrollview = require("famous/views/Scrollview");
var DragView = require('DragView');
var mainContext = Engine.createContext();
var scrollview = new Scrollview();
var views = [];
scrollview.sequenceFrom(views);
for (var i = 0; i < 40; i++) {
var view = new DragView({
size: [undefined, 200],
content: "Surface: " + (i + 1),
backgroundColor: "hsl(" + (i * 360 / 20) + ", 100%, 70%)"
});
view._eventOutput.pipe(scrollview);
views.push(view);
}
mainContext.add(scrollview);
});
// Custom Drag View Item
define('DragView', function(require, exports, module) {
var Surface = require("famous/core/Surface");
var Scrollview = require("famous/views/Scrollview");
var RenderNode = require('famous/core/RenderNode');
var Modifier = require('famous/core/Modifier');
var View = require('famous/core/View');
var Transform = require('famous/core/Transform');
var Draggable = require('famous/modifiers/Draggable');
var TransitionableTransform = require('famous/transitions/TransitionableTransform');
var RenderController = require('famous/views/RenderController');
function _updatingDrag(e) {
var pos = e.position;
this.surface.setContent('Draggable Position is ' + pos);
}
function _endDrag(e) {
var pos = e.position;
this.surface.setContent('Draggable End Position is ' + pos);
this.draggable.setPosition([0, 0], {
duration: 300
}, function() {
this.renderer.hide();
}.bind(this));
if (pos[0] > 200) {
console.log('showing OK');
this.renderer.show(this.ok);
}
if (pos[0] < -200) {
console.log('showing Not OK');
this.renderer.show(this.not);
}
}
function DragView() {
View.apply(this, arguments);
var draggable = new Draggable({
xRange: [-220, 220],
yRange: [0, 0],
});
var item = new Surface({
content: this.options.content,
size: [undefined, 200],
properties: {
backgroundColor: this.options.backgroundColor,
lineHeight: "200px",
textAlign: "center"
}
});
var okItem = new Surface({
content: String.fromCharCode(10004),
size: [220, 200],
properties: {
color: "white",
backgroundColor: "green",
lineHeight: "200px",
fontSize: "100px",
textAlign: "center"
}
});
var notOkItem = new Surface({
content: String.fromCharCode(10006),
size: [220, 200],
properties: {
color: "white",
backgroundColor: "red",
lineHeight: "200px",
fontSize: "100px",
textAlign: "center"
}
});
var renderer = new RenderController();
draggable.subscribe(item);
draggable.on('update', _updatingDrag.bind({
surface: item
}));
draggable.on('end', _endDrag.bind({
surface: item,
draggable: draggable,
renderer: renderer,
ok: okItem,
not: notOkItem
}));
item.pipe(this._eventOutput);
this.add(draggable).add(item)
this.add(renderer);
}
DragView.prototype = Object.create(View.prototype);
DragView.prototype.constructor = DragView;
module.exports = DragView;
});
// Start Main App
require(['main']);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://requirejs.org/docs/release/2.1.16/minified/require.js"></script>
<script src="http://code.famo.us/lib/requestAnimationFrame.js"></script>
<script src="http://code.famo.us/lib/classList.js"></script>
<script src="http://code.famo.us/lib/functionPrototypeBind.js"></script>
<link rel="stylesheet" type="text/css" href="http://code.famo.us/famous/0.3.5/famous.css" />
<script src="http://code.famo.us/famous/0.3.5/famous.min.js"></script>