What would be more efficient, move DOM elements or clone them?

I have this dilemma, I have to make a menu, the basic estructure is four to six main menu items and each one can have N level 2 menus and each sub menu can also have subs.

I get from the server this html two ul like this one:

<ul>    <li> <a>Normal link</a></li>    <li>         <a> </a>        <ul> this is other level of menus</ul>    </li> </ul> 

The first list I get just contains the main menu items, the second one all the sub levels.

It has to be presented something like this: enter image description here

Each time the user click the main menu items, the sub level lists appear, when a sub level menu is clicked the next level appears below the active one.

This basic structure is not that complex I just take with Javascript all the “sub level N” items and each 3 menu items (in each level) I add a new <li> where i move all containing the next levels <ul> and show/hide them using related classes and attributes, something like:

<ul>   <li></li>   <li class="i-am-for-X></li>   <li></li>   <li class="here-are-my-next-level-items">       <ul></ul>       <ul class="i-am-x"></ul>       <ul></ul>   </li>   <li></li>   <li class="here-are-my-other-next-level-items"> </ul> 

My problem comes now, I was required that the menu class should had a method that changes the number of cols per sub menus (to be used in some viewport breakpoints). This means theoretically my i-am-x can be after the 3rd <li> as in my above example as it could be after the 4th or the Xth so I need the same content in multiple places, as far as I can see I have only two options: Clone them or move them.

If I clone them, I will do this after my eventlisteners are in place and after the page is loaded and let the css handle the show and hide with javascript just for change the classes needed.

If I move them, I have to make this after the page is loaded on each breakpoint. I think this could be more disturbing for the user and the responsiveness.

I am not sure If I am not seeing another option and/or more problems related to this options and I am not sure which one has better performance.

Thanks in advance for your input!

Theme Form Elements

I’m using the following to format field elements in my theme:

function hook_form_element($  element, $  value) {   $  t = get_t();     if ($  element['#type'] == 'checkbox') {      $  output = '<div class="checkbox">';    } elseif ($  element['#type'] == 'radio') {       $  output = '<div class="radio">';    } else {       $  output = '<div class="form-group">';    };     $  required = !empty($  element['#required']) ? '<span class="form-required" title="' . $  t('This field is required.') . '">*</span>' : '';     if (!empty($  element['#title'])) {      $  title = $  element['#title'];      if (!empty($  element['#id'])) {        $  output .= ' <label for="' . $  element['#id'] . '">' . $  t('!title: !required', array('!title' => filter_xss_admin($  title), '!required' => $  required)) . "</label>\n";      }      else {        $  output .= ' <label>' . $  t('!title: !required', array('!title' => filter_xss_admin($  title), '!required' => $  required)) . "</label>\n";      }    }    $  output .= " $  value\n";    if (!empty($  element['#description'])) {     $  output .= ' <div class="description">' . $  element['#description'] .    }    $  output .= "</div>\n";   return $  output; } 

This gives me the following markup:

<div class="form-group">   <label for="edit-field-phone-0-value">Phone Number:<span class="form-required" title="This field is required.">*</span></label> </div> <input type="text" name="field_phone[0][value]" id="edit-field-phone-0-value" value="" class="phone-field-textfield form-control"> 

I need <div class="form-group"></div> to wrap both the form label and input. I would also like the label to appear beneath the form element. Below is an example of the markup that I’m looking for:

<div class="form-group">   <input type="text" name="field_phone[0][value]" id="edit-field-phone-0-value" value="" class="phone-field-textfield form-control">   <label for="edit-field-phone-0-value">Phone Number:<span class="form-required" title="This field is required.">*</span></label> </div> 

Wrapping certain elements in an array with an array with PHP

I have a function that’s doing this correctly, but I feel as though this can be done (possibly) with minimal code, with recursion, but I’m just wracking my brain. Basically I have JSON that gets decoded to an array. What I want to do is turn this:

{   "parentView": {     "childViews": [       {         "type": "container-fluid",                "childViews": [                   {             "type": "row",               "childViews": [                           {                 "type": "slider"               },               {                 "type": "slider"               }             ]                       }                 ]       }     ]       } } 

Into this:

{   "parentView": {     "childViews": [       {         "type": "controller",         "childViews": [           {             "type": "container-fluid",                    "childViews": [                       {                 "type": "controller",                 "childViews": [                   {                     "type": "row",                     "childViews": [                                   {                         "type": "controller",                         "childViews": [                           {                             "type": "slider"                           }                         ]                       },                       {                         "type": "controller",                         "childViews": [                           {                             "type": "slider"                           }                         ]                       }                     ]                               }                         ]               }             ]           }         ]       }     ]       } } 

Here is the code I am using to make it work how I need. I am just looking for a cleaner method:

function createControls($  parentView) {     $  newArr = $  parentView;      foreach ($  parentView as $  k => $  v) {         $  newArr[$  k] = [             'type' => 'control',             'childViews' => $  v         ];          if ($  v['childViews']) {             foreach ($  v['childViews'] as $  kk => $  vv) {                 $  newArr[$  k]['childViews']['childViews'] = [                     'type' => 'control',                     'childViews' => $  vv                 ];                  if ($  vv['childViews']) {                     foreach ($  vv['childViews'] as $  _k => $  _v) {                         $  newArr[$  k]['childViews']['childViews']['childViews']['childViews'][$  _k] = [                             'type' => 'control',                             'childViews' => $  _v                         ];                     }                 }             }         }     }      return $  newArr; }  $  newArr = createControls($  arr['parentView']['childViews']); $  arr['parentView']['childViews'] = $  newArr; 

Thanks in advance! Kind regards!

JS Plot goes over other html elements

I’m using Plot.ly js library to draw an interactive plot.

I have a div and a textarea below like this:

<div id="myDiv"></div> <textarea rows = "5" cols = "50" name = "description"> </textarea>

And in the js I call Plotly.newPlot(graphDiv, data,layout, {responsive: true});

The result is this: enter image description here

As you can see, the plot overlays the textarea. I suppose this is because the plot is created in the js after the elements are loaded, and therefore at the beginning myDiv has size 0. How can I fix this? thanks

Valuation of congruent elements in a local division ring

Let $ K$ be a complete local division ring (note $ v$ its valuation). For $ x,y\in K$ ($ y\ne0$ ), one puts $ x^y=yxy^{-1}$ . Let $ r\in\mathbb N$ . Consider $ x,y\in K$ and $ a,b\in K^*$ such that $ v(x-y)\ge r$ and $ v(a-b)\ge r$ . Do we have $ v(x^a-y^b)\ge r$ ? In the commutative case, it is obvious but in the non-commutative case, I can not see the answer.

Thanks in advance

Computing quotients of group by elements of its lower exponent$-p$ central series

Let $ G$ be a finite $ p−$ group of number of generators $ d$ and exponent$ −p$ class $ c$ , that is $ c$ is the smallest integer satisfying $ P_c(G)=1$ in the series $ $ G=P_0(G)≥…≥P_{i−1}(G)≥P_i(G)≥… $ $ Where $ P_i(G)=[P_{i−1}(G),G]P{i−1}(G)^p$ .

1/ Can you show me how to calculate $ G/P_i(G)$ ´s using GAP system?

2/ Can you show me how to compute $ G/P_1(G)$ using abelianisation and row-echelonisation (by hand)?

Thanks in advance

Transform HTML elements in conjunction with OrbitControls so that they appear to rotate with the objects in the scene

I have created a Three.js scene in which there is a globe (with an Earth texture) that can be rotated using OrbitControls.

I want to have labels that are “stuck” to a position on the globe i.e. a “Australia” label that is pinned to the Latitude & Longitude of the centre of Australia, which then moves with the Earth object when OrbitControls are used so that the pin always stays above it’s location.

For various reasons (easier to content manage, easier to style etc…) I need these labels to be HTML elements (rather than a THREE.Sprite for example). This makes things a lot trickier (in my eyes) as Three.js objects would easily rotate together, but HTML elements will not.

The easiest way for you to understand what I want is to look at an example, so the one linked below is pretty much exactly how I want my globe to work. You can see that they use a Three.js globe with HTML labels.


I have looked through their code and see that they are transforming/translating the HTML elements, but I cannot figure out the calculation that causes the HTML elements to always stay in the position they start in, perfectly above their respective countries.

I am using the below calculation to convert Latitude & Longitude coordinates (from google maps) into X, Y, Z coordinates to use in my three.js scene, and this works perfectly (i.e. I can create a small SphereGeometry and place it at the generated coordinates and it will appear above the correct location)

function calcPosFromLatLonRad(lat, lon, radius){      let phi = (90 - (lat)) * (Math.PI / 180);     let theta = ((lon) + 180) * (Math.PI / 180);      let x = -((radius) * Math.sin(phi) * Math.cos(theta));     let z = ((radius) * Math.sin(phi) * Math.sin(theta));     let y = -((radius) * Math.cos(phi));      return [x, y, z];  } 

To start off I am just trying to get the HTML elements to rotate on the X axis. I came up with the following code but it doesn’t work correctly.

The below code is part of a function that sits inside the render function that uses RequestAnimationFrame.

...element.style.transform = translate($ {-Math.sin(camera.rotation.y) * 300}px,...

300 above being the radius of the globe (SphereGeometry).

Does anyone have any idea how I can achieve an affect like the one achieved in the code I linked to? i.e. no matter the rotation of the scene, the pins are always in the correct position.


Deleting elements from a list with a given condition

n = 2 list = Permutations[Range[-n, n], {2}]; list = {{-2, -1}, {-2, 0}, {-2, 1}, {-2, 2}, {-1, -2}, {-1, 0}, {-1, 1}, {-1,    2}, {0, -2}, {0, -1}, {0, 1}, {0, 2}, {1, -2}, {1, -1}, {1, 0}, {1,    2}, {2, -2}, {2, -1}, {2, 0}, {2, 1}} 

How can I delete the cases where Abs[a+b]>n and {a,b} are elements of the list? In the example above, I would like to delete {-2,-1} and {2,1} from the list.