Move a layer in the layers list.

@param {Object} state The redux state.
@param {Object} layer The layer object to be moved.
@param {string} targetId The id of the layer
at the position where layer will be moved.

@returns {Object} The new state object.
function placeLayer(state, layer, targetId) {
  const new_layers = state.layers.slice();
  const idx1 = getLayerIndexById(new_layers, layer.id);
  const idx2 = getLayerIndexById(new_layers, targetId);
  if (idx1 !== -1) {
    new_layers.splice(idx1, 1);
  }
  const newIndex = (targetId && idx2 !== -1) ? idx2 : new_layers.length;
  new_layers.splice(newIndex, 0, layer);
  return Object.assign({}, state, {
    layers: new_layers,
  }, incrementVersion(state.metadata, LAYER_VERSION_KEY));
}