react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / addons / transitions / ReactTransitionChildMapping.js
81155 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @typechecks static-only9* @providesModule ReactTransitionChildMapping10*/1112"use strict";1314var ReactChildren = require('ReactChildren');1516var ReactTransitionChildMapping = {17/**18* Given `this.props.children`, return an object mapping key to child. Just19* simple syntactic sugar around ReactChildren.map().20*21* @param {*} children `this.props.children`22* @return {object} Mapping of key to child23*/24getChildMapping: function(children) {25return ReactChildren.map(children, function(child) {26return child;27});28},2930/**31* When you're adding or removing children some may be added or removed in the32* same render pass. We want to show *both* since we want to simultaneously33* animate elements in and out. This function takes a previous set of keys34* and a new set of keys and merges them with its best guess of the correct35* ordering. In the future we may expose some of the utilities in36* ReactMultiChild to make this easy, but for now React itself does not37* directly have this concept of the union of prevChildren and nextChildren38* so we implement it here.39*40* @param {object} prev prev children as returned from41* `ReactTransitionChildMapping.getChildMapping()`.42* @param {object} next next children as returned from43* `ReactTransitionChildMapping.getChildMapping()`.44* @return {object} a key set that contains all keys in `prev` and all keys45* in `next` in a reasonable order.46*/47mergeChildMappings: function(prev, next) {48prev = prev || {};49next = next || {};5051function getValueForKey(key) {52if (next.hasOwnProperty(key)) {53return next[key];54} else {55return prev[key];56}57}5859// For each key of `next`, the list of keys to insert before that key in60// the combined list61var nextKeysPending = {};6263var pendingKeys = [];64for (var prevKey in prev) {65if (next.hasOwnProperty(prevKey)) {66if (pendingKeys.length) {67nextKeysPending[prevKey] = pendingKeys;68pendingKeys = [];69}70} else {71pendingKeys.push(prevKey);72}73}7475var i;76var childMapping = {};77for (var nextKey in next) {78if (nextKeysPending.hasOwnProperty(nextKey)) {79for (i = 0; i < nextKeysPending[nextKey].length; i++) {80var pendingNextKey = nextKeysPending[nextKey][i];81childMapping[nextKeysPending[nextKey][i]] = getValueForKey(82pendingNextKey83);84}85}86childMapping[nextKey] = getValueForKey(nextKey);87}8889// Finally, add the keys which didn't appear before any key in `next`90for (i = 0; i < pendingKeys.length; i++) {91childMapping[pendingKeys[i]] = getValueForKey(pendingKeys[i]);92}9394return childMapping;95}96};9798module.exports = ReactTransitionChildMapping;99100101