8
votes
Fortnightly Programming Q&A Thread
General Programming Q&A thread! Ask any questions about programming, answer the questions of other users, or post suggestions for future threads.
Don't forget to format your code using the triple backticks or tildes:
Here is my schema:
```sql
CREATE TABLE article_to_warehouse (
article_id INTEGER
, warehouse_id INTEGER
)
;
```
How do I add a `UNIQUE` constraint?
I'm trying to figure out a proper HTML structure for the nodes in Intergrid.
The issue comes down to the fact that I'm using checkboxes to provide folding as part of the multi-layered node structure. What I currently have is:
Activating the checkbox makes the nearby
.children
element becomedisplay: none;
. Since it's done via CSS, it's quick, and I have no need to mess with any more JS than I have to.The issue here is minimizing the amount of HTML I have to use to display and make interactable the same thing: a node with some content (not necessarily a separate element), its children, and (if the node has children) the folding toggle (here: the checkbox) that lets me collapse and expand the subnodes.
The way I used to solve it was only having the
.node
and the.children
, and putting the folding inside.children
, then magic-numbering its position to the appropriate node. It's inelegant and creates its own issues when navigating the code, but it let me write and move around less HTML when manipulating the nodes.Right now, I'm looking at Web Components and trying to see whether it can solve my problem of using the fewest elements possible – or, at least, making their manipulation streamlined (i.e. using the fewest commands). Apparently, you can define a Web Component in a way that keeps it as a native HTML element while adding functionality? I'm still unclear on this one. I know there's a lot of libraries that allow for this, but I have no mental model on how to use them, let alone on how to use them well.
I've also considered using pure-JS folding, since the app I'm working on is very much JS to begin with. It might make the code management easier, but with potentially thousands of nodes in use at any given point, speed takes priority. Anywhere I can cut down on some performance issues without sacrificing much of code simplicity is somewhere I'd like to explore.
EDIT: I did consider using the native HTML
<details>
element, which provides folding perfectly on its own. Except... it would fold and unfold whenever I clicked on it. Which is an issue when you have an element within the incessant folder that's activated by clicking on it – which, given that it's a part of the<details>
, also triggers the<details>
click – and so, it folds.Not a good strategy, but one I had to try out.
Not sure that I have fully understood what you're trying to make, but here is an idea:
Demo: https://codepen.io/Ainar-G/pen/pooWgdZ.
Oh, I have the folding mechanism locked-down. It's all in working order. The goal right now is to minimize the markup I already use, which hopefully also simplifies the JS-side management of it: adding, moving, removing, and editing the HTML part of the content.
In other words, what I'm trying to figure out is how to turn the markup from my comment into the markup from your comment (which looks much simpler, the issues though it has) without making a mess out of it.
Kudos for the effort, nevertheless.
What does your current mark-up do that something like mine can't do? I'm asking because I still don't get what you're trying to achieve and why your current solution isn't good enough, but it sounds interesting enough for a Tuesday evening puzzle :-)
I have a steady feeling that what I have can be further reduced by number of elements involved. I'm looking for whether I can do the same thing with fewer parts.
My solution seems to be the 20% of the effort that does 80% of the work. I'm looking into whether I can go the extra mile and refactor the HTML into something smaller and more efficient.
I can maybe move the toggle functionality onto the
.node
itself somehow while still utilizing the CSS to hide the.children
. Like I said, HTML+CSS is preferably to adding any more JS, and[type="checkbox"]
is a good solution in this regard.Is that clear? I feel like I'm not expressing my intent clearly here. If you need further elaboration, just say the word.
I'm probably too tired after a day of work at the office. If you like your current mark-up and you just want to reduce it then I don't really see why you need the
.wrapper
class in the first place, since most of your.children
blocks will probably only contain one.wrapper
block?.wrapper
is there to host the.node
's checkbox. That way – within an external container – the checkbox could be safely positioned just outside the.node
so I couldposition: absolute;
the checkbox and be sure it will "land" (end up within the box of its container) just about on the same baseline as.node
.It's about both the positioning of the checkbox and the fact that it's "attached" to the
.node
it serves the folding for.Without the
.wrapper
, moving the elements around (which is a thing in Intergrid) would be more complicated. I'd have to account for whether there's a checkbox before.node
if I'm moving the.node
up the chain etc.You mean something like this? Why not
float
?You mean, just like what
fieldset
does, except with a hint of semanticity? ;)What's the benefit of
float
overposition: absolute
in this scenario?Well, you are trying to reduce the number of moving parts, and
float
allows you to throw the wrapper away. It also seems more semantically correct to me, after all, what you need is for other content to wrap around it. And thirdly,position: absolute
is almost always a hack, at least in “interactive documents” as opposed to “web applications”.I don't think you'll be able to reduce it much beyond that. The CSS framework that I use as a base for Tildes (Spectre.css) usually does a good job with its HTML, and has a similar sort of functionality with its "Accordions". Its HTML looks very similar to yours, except that it's hiding the checkbox and using an icon:
The main difference is probably that it's using a
<label>
, which would be more correct if you want people to be able to click on the node's content to fold/unfold.I used to have
<label>
as part of the structure. Removed it in an effort to reduce the quantity of elements, replacing it with a[type="checkbox"]:before
displaying a + or a −, depending on the folding status.Would you be surprised to learn that while it worked fine in Chrome, Firefox took none of my shit and refused to display the pseudo-element?
I might be going back to
<label>
.Is there a way to see if two MySQL queries are equivalent after an optimizer pass? There's a particular bizarre little expression that keeps popping up in an awful query that I'm editing and I suspect it just gets optimized out anyway. I'm just curious, and I'm not really planning on basing my work on the answer, I'm just wondering if there's a way for me to check.
Using an EXPLAIN statement might be useful for this.
Vim question: If I set my
$TERM
toxterm-mono
then my Vim doesn't show menus, e.g. for<C-x><C-n>
and other auto-complete stuff. Is there a way to make it show the menu again?EDIT: Apparently, if your terminal has less than eight colours, you're kind of screwed:
Bother. Looking for a way to force it.