Can I get {{link-to}} to specify which animation to use with ember fire? - ember.js

I have left/right arrows on a page and I want to pick the animation without having to define a relationship between all the routes. Is it possible to set it on the {{link-to}}? Right now it's pretty brittle.

I have been looking, and I don't think it's possible to know what link was clicked from the transition it caused. However, I can think of two different ways to tackle your use case.
Solution 1: metaprogramming
Make a list of your routes and generate transitions dynamically from it. Something like this:
// app/transitions.js
export default function() {
const orderedRoutes = [
'left-route',
'center-route',
'right-route',
];
// See https://github.com/coleww/each-cons
// where I pinched this from
function eachCons(a, n) {
var r = []
for (var i = 0; i < a.length - n + 1; i++) {
r.push(range(a, i, n))
}
return r
}
function range (a, i, n) {
var r = []
for (var j = 0; j < n; j++) {
r.push(a[i + j])
}
return r
}
eachCons(orderedRoutes, 2).forEach(pair => {
// `pair` will be each pair of consecutive routes
// on our `orderedRoutes` list
const left = pair[0];
const right = pair[1];
// For each pair, define a transition
this.transition(
this.fromRoute(left),
this.toRoute(right),
this.use('toLeft'),
this.reverse('toRight')
);
});
}
Note that I only define transitions for adjacent routes. If you want to define a transition between left-route and center-route, you'll need to alter the algorithm to define new combinations.
Solution 2: callback to fromRoute
The function fromRoute can not only take a string, but also a function. This function receives two parameters: the names of the initial and the final routes of a transition. In this function you can return true if the transition should apply, and false otherwise. See here:
http://ember-animation.github.io/liquid-fire/#/transition-map/route-constraints
You can use this function to decide whether you should be going left or right (as per your use case). See this:
// app/transitions.js
export default function() {
// Names of the routes in the left-right succession
const orderedRoutes = [
'left-route',
'center-route',
'right-route',
];
function isLeft(initial, destination) {
const i0 = orderedRoutes.indexOf(initial);
const i1 = orderedRoutes.indexOf(destination);
if (i0 === -1 || i1 === -1) {
// This is not one of the transitions
// in the left-right succession
return false;
}
if (i0 === i1) {
// They are the same route
return false;
}
// This will be `true` if the initial route
// is "to the left" of the destination route
return i0 < i1;
}
this.transition(
this.fromRoute(isLeft),
this.use('toLeft')
this.reverse('toRight')
);
}
In this example, for each transition we check the initial and the destination route. We see if they belong to the left-right succession, and whether the transition corresponds to a "left" or a "right". If it's a "left" we return true in the "toLeft" case. If it's a "right", we return true in the "toRight" case.

Related

IntroJs Hints if parent not Visible

IntroJs hints
How can I skip or hide when a parent element is not visible? For some reason only inline seems to be working for my hints.I have the data-hints in spans on the HTML and I need to check if the nearest element is visible or parent/child element.
var hints = false;
var all = document.getElementsByTagName("*");
function introFunction() {
for (var i = 0, max = all.length; i < max; i++) {
if ((isHidden(all[i]) && hints));
document.getElementById("#helpFunc").html("Show Help");
introJs().hideHints();
} else {
document.getElementById("#helpFunc").html("Hide Help");
introJs().showHints();
}
hints = !hints;
function isHidden(el) {
var style = window.getComputedStyle(el);
return ((style.display === 'none') || (style.visibility === 'hidden'));
}
}
Instead of trying to get the computed style of the element, try checking the element bounding rectangles - if there aren't any, it means the element is not visible.
!!el.getClientRects().length; // false means el or its parent(s) is hidden
LE: that doesn't work for "visibility: hidden;" though. So maybe you can combine the above with what you already have.

CouchDB view reduce one doc per key

I'm trying to solve what seems like a fairly simple problem with a couchDb view, but I'm not even getting close to the target with my result set.
Rather than updating documents, I'm creating a new document every time as my versioning strategy, and tying those documents together with a versioning field called ver. The very first document in a version chain will see the ver field and the _id field having the same value. All subsequent documents in the chain will have the same ver field as previous docs in the chain, but will have a unique _id field. These documents also have a createdTime field which is my way of knowing which document is the latest.
Here's my documents:
{
"_id": "abcd-1234-efgh-9876",
"ver": "abcd-1234-efgh-9876",
"createdTime": "2020-01-12 01:15:00 PM -0600",
...
},
{
"_id": "uopa-3849-pmdi-1935",
"ver": "abcd-1234-efgh-9876",
"createdTime": "2020-02-16 02:39:00 PM -0600",
...
}
Here's my map function:
function (doc) {
emit(doc.ver, doc);
}
Here's my reduce function:
function(keys, values, rereduce) {
var latestVersions = {};
for (var i = 0; i < keys.length; i++) {
var found = latestVersions[keys[i][0]];
if (!found || found.createdTime < values[i].createdTime) {
latestVersions[keys[i][0]] = values[i];
}
}
return latestVersions;
}
And finally, here's my desired output from the view (just the doc that I want):
{
"_id": "uopa-3849-pmdi-1935",
"ver": "abcd-1234-efgh-9876",
"createdTime": "2020-02-16 02:39:00 PM -0600",
...
}
What am I missing here? The reduce function is returning both records, which is not what I want. Is what I'm trying to achieve possible or is there a better way to go about this?
Update
I was able to get this to work when a single key is used to access the view, which is one of my use cases.
function (keys, values, rereduce) {
var toReturn = values[0];
for (var i = 1; i < values.length; i++) {
if (values[i].createdTime > toReturn.createdTime) {
toReturn = values[i];
}
}
return toReturn;
}
I have another use case that will be returning all of the data in the view, however. The desired result there is the same as above, but the function I'm using for single keys will only ever return one result. How do I filter multiple values with a shared key such that 1 "shared" key:n values -> 1 key:1 value.
I was finally able to resolve this when I stumbled upon this couchbase article. It was much more articulate than some of the other dry computer-science documentation.
I still do not understand why certain items are grouped in a reduce method and other ones are not. For example, reduce was called 5 times for 6 items that shared an identical key; only one of the keys had actually grouped anything -- an array of two documents. It probably has something to do with those dry computer-science B-tree documents I glossed over.
Anyway, I was able to determine that all I needed to do was group the values by the ver field in both scenarios (the only difference being that rereduce had a 2 dimensional array). Here's what my reduce function ended up looking like:
function (keys, values, rereduce) {
var toValues = function(myMap) {
return Object.keys(myMap).map(function(key) {
return myMap[key];
});
}
if (rereduce) {
// values should look like [[{...}, {...}], [{...}]]
var outputMap = {};
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[i].length; j++) {
var currentEl = values[i][j];
var found = outputMap[currentEl.ver];
if ((found && found.createdDate < currentEl.createdDate) || !found) {
outputMap[currentEl.ver] = currentEl;
}
}
}
return toValues(outputMap);
} else {
var outputMap = {};
for (var i = 0; i < values.length; i++) {
var found = outputMap[values[i].ver];
if ((found && found.createdDate < values[i].createdDate) || !found) {
outputMap[values[i].ver] = values[i];
}
}
return toValues(outputMap);
}
}

IF block inside render function ReactJS

Am newbie to ReactJS. I want to use a IF block inside the render function. While search for this I got result like "You should use ternary operator instead of IF statement". But if I want to use something like,
$.each(array, function(i, x) {
var flag = 0
if({x.a1} || !{x.a2}) {
flag = 1;
<p>true flag enabled</p>
}
<p>...</p>
});
How to convert this statement into JSX syntax or how to use it in React render fucntion.
Thanks in advance.
This link will help you
https://facebook.github.io/react/tips/if-else-in-JSX.html
But I'd use something like this, as its slightly easier to read (IMHO). Note, your array is a prop - passed into the component (or could be a state). I'd use lodash for mapping etc, cause its so useful all over the place (https://lodash.com/)
_renderElements: function(){
return _.map(this.props.array, function(el){
var flag = 0;
return el.a1 || el.a2 ? <p>{'true 1 enabled'}</p> : <p>...</p>;
})
},
render: function () {
return (
{this._renderElements()}
}
);
}
Hope that's helpful.
I do this in one of two ways, depending mostly on how big the if statement is.
one scenario, I don't know if I'm going to render an element or not:
Component = React.createClass({
render() {
var elem;
if (something) {
elem = (<SomeOtherComponent />);
}
return (
<div>{elem}</div>
);
}
});
This is basically a way to either show the element/component or not. If I'm going to map something I would use a separate method and call it:
Component = React.createClass({
mapIt() {
return this.props.items.map(item => {
... do your stuff ...
return (
<SomeOtherComponent prop1={item.value} ... />
);
});
},
render() {
return (
{this.mapIt()}
);
}
});
This to me is a nice clean way of handling.
You want to have your render function look something like this:
render: function () {
return (
{
array.map(function (el, i) {
var flag = 0;
if (el.a1 || el.a2) {
flag = 1;
return <p>true flag enabled</p>;
} else {
return <p>...</p>;
}
}
}
);
}
React allows you to return an array of React elements, so you can map your array and return a JSX element for every element of the array.

Sidebar to show currently displayed markers - combined IF statement

I would really appreciate some help with this problem. It may be easy to solve but I just can't figure out how to go about coding it. I have a map based on this Geocodezip Example Categories which displays markers and has a sidebar next to it. The markers belong to one of three categories and based on whether the checkboxes are checked, the markers will appear on the map.
function show(category) {
for (var i=0; i<gmarkers.length; i++) {
if (gmarkers[i].mycategory == category) {
gmarkers[i].setVisible(true);
}
}
document.getElementById(category+"box").checked = true;
}
function hide(category) {
for (var i=0; i<gmarkers.length; i++) {
if (gmarkers[i].mycategory == category) {
gmarkers[i].setVisible(false);
}
}
document.getElementById(category+"box").checked = false;
infowindow.close();
}
At the same time, I am trying to display the markers' info in the sidebar but under two conditions:
the marker category must be checked &
the marker must be contained in the map viewport/bounds
I then added one code for the sidebar that only displays the markers for checked categories and one where the info is shown in the sidebar when the markers are in the viewport. However, I can only do these separately (for to completely different codes) and cannot get both conditions to work together. These are the two examples that I am trying to merge:
Include in sidebar if box is checked
function makeSidebar() {
var side_bar_html = "";
for (var i=0; i<gmarkers.length; i++) {
if (gmarkers[i].getVisible()) {
side_bar_html += '<a href="javascript:myclick(' + i + ')">' + gmarkers[i].myname + '<\/a><br>';
}
}
document.getElementById("side_bar").innerHTML = side_bar_html;
}
(when bounds change) Include in sidebar if marker is contained within bounds
function makeSidebar() {
google.maps.event.addListener(map, 'bounds_changed', function() {
var side_bar_html = "";
var bounds = map.getBounds();
for (var i=0; i<gmarkers.length; i++) {
if (bounds.contains(gmarkers[i].position)) {
side_bar_html += '<a href="javascript:myclick(' + i + ')">' + gmarkers[i].myname + '<\/a><br>';
}
}
document.getElementById("side_bar").innerHTML = side_bar_html;
});
}
I tried putting the two together in a combined if statement (&&) within the if bounds.contains statement but the sidebar won't change if a box is checked/unchecked.
Add the check to makeSidebar only when the map bounds is available:
// == rebuilds the sidebar to match the markers currently displayed ==
function makeSidebar() {
var html = "";
for (var i=0; i<gmarkers.length; i++) {
if (gmarkers[i].getVisible() &&
map.getBounds &&
map.getBounds() &&
map.getBounds().contains(gmarkers[i].getPosition())) {
html += '<a href="javascript:myclick(' + i + ')">' + gmarkers[i].myname + '<\/a><br>';
}
}
document.getElementById("side_bar").innerHTML = html;
}
execute makeSidebar when the bounds_changed event fires:
google.maps.event.addListener(map, 'bounds_changed', makeSidebar);
working fiddle

State machine: Use transition matrix instead of nested switch cases C++

I am having difficulties with my state machine. I use a function that returns the new state based on input parameters oldState and two input parameters.
In this function I have a lot of nested switch cases. I'd rather use a 2x2 transition matrix but have no idea how to use it. I did make a transition table from the state diagram with sates and inputs.
But how exaclty do I use the 2 dim. array transition_table[3][4]?
You stated you currently have something like this:
StateType transition (StateType old, InputType one, InputType two) {
//... nested switch statements
return new_state;
}
So, it seems what you need is a 3-dimensional array:
#define MAX_STATES 12
#define MAX_INPUT_VAL 2
StateType transitionTable[MAX_STATES][MAX_INPUT_VAL][MAX_INPUT_VAL] = {
{ { StateA, StateB },
{ StateC, StateD } },
{ { StateE, StateF },
{ StateG, StateH } },
{ { StateI, StateJ },
{ StateK, StateL } },
//...
};
Then you would transition like this:
new_state = transitionTable[StateIndex(old)][one][two];
So, assuming that StateIndex(StateC) returns 2, then:
old = StateC;
new_state = transitionTable[StateIndex(old)][1][0];
assert(new_state == StateK);
would result in new_state holding StateK.
Given a matrix like this:
state1_input1 state1_input2 state1_input3
state2_input1 state2_input2 state2_input3
state3_input1 state3_input2 state3_input3
When you are in state n and receive input m, you look at row n, column m to find out the new state. Assuming you have 3 possible states and 4 possible inputs, all you need to do is:
state = transition_table[state][input]
Based on your description, you don't need a 2-dimentional array, 1 dimension is fine. It should be made this way:
void foo()
{
int States[2] = {1,2};
int currentState = 1;///initial state, let's say
int oldState;///prev. state
while(true)
{
if(currentState == 1 && *add any other condition that you need*)
{
<...>do something<...>
oldState = currentState;//saving the old state, in case you need it.
currentState = states[currentState]; //changing the state
}
else if( currentState == 2 && *add any other condition that you need*)
{
<...>some other code<...>
}
}
So you have an array of states. You then calculate the index of that array based on your input parameters (you said you use the old state and something else for it). After that you simply get the new state from the array by that index.
My explanation is a bit messy, so leave a comment if you need a clarification of some part.