We are in the midst of implementing a labeling functionality exactly like gmail for our webapp - you can select the posts (checkboxes) and select which labels to apply/delete from a drop down list of 'labels' (which themselves are a set of checkboxes). The problem is "how to go about doing it?" I have a solution and before I tackle it that way I want to get an opinion on whether it is the right way and if it can be simplified using certain jquery/javascript constructs that I might not be aware of. I am not a JavaScript/jQuery pro by any means, yet. :)
Let: M = {Set of posts} N = {Set of Labels} M_N = many to many relation between M and N i.e., the set of posts that have at least one label from N
Output: Given a set of 'selected' posts and the set of 'selected' labels get a array of items for the JSON with the following values:
- Post_id, Label_id, action{add, delete}
Here's the approach that I have come up with (naive or optimal, I don't know):
- Get current number of selected posts: var selectionCount = 5 (say i.e., 5 posts selected)
- Capture the following data set for each item in the selection:
Label_id | numberOfLabelsInSelection| currentStateToShow | newState 4 | 3 | partialTick | ticked (add) 10 | 5 | ticked | none (delete) 12 | 1 | partialTick | partialTick (ignore) 14 | 0 | none | ticked (add)
Basically the above data structure is just capturing the conditions of display i.e., 5 posts are selected overall and only two have label "x" say, then the label list should show a 'partial tick mark' in the checkbox, if all posts have a label "y" then the drop down shows a "full tick". Labels not on the selected set are just unselected but can only toggle to a tick mark or 'none' but not to a partial state (i.e., on/off only. The partialTick has three states so to speak: on/off/partial)
The 'newState' column is basically what has been selected. The output action is based with what the previous state was (i.e., currentStateToShow):
- partial to tick implies add label to all posts that didn't have that label
- ticked to none implies delete that label from all posts
- partial to none implies delete only labels from those selected posts
- none to ticked implies add new label to all posts
- partial to partial implies ignore, i.e., no change.
Then I can iterate over this set and decide to send the following data to the server:
| Post_id | Label_id | Action | | 99 | 4 | add | | 23 | 10 | delete | ...
and so on.
So what's the issue? Well this is QUITE COMPLICATED!!! Javascript doesn't really have the map data structure (does it?) and it would entail too many sequential iterations and check each and every thing and then have a lot of if-else's to ascertain the value of the newState.
I'm not looking for "how to code it" but what can I do to make my life easier? Is there something out there that I can already use? Is the logic correct or is it a bit too convoluted? Any suggestions as to how to attack the problem or some built in data structures (or an external lib) that could make things less rough? Code samples :P ?
I'm working with javascript/jquery + AJAX and restlet/java/mysql and will be sending a JSON data structure for this but I'm quiteeeeeeeeeee confounded by this problem. It doesn't look as easy as I initially thought it to be (I mean I thought it was "easier" than what I'm facing now :)
I initially thought of sending all the data to the server and performing all this on the backend. But after an acknowledgment is received I still need to update the front end in a similar fashion so I was 'back to square one' so to speak since I'd have to repeat the same thing on the front end to decide which labels to hide and which to show. Hence, I thought it'd just be better to just do the whole thing on the client side.
I'm guessing this to be an easy 100-150+ lines of javascript/jquery code as per my 'expertise' so to speak, maybe off...but that's why I'm here :D
PS: I've looked at this post and the demo How can I implement a gmail-style label chooser? But that demo is only for one post at a time and it can be easily done. My problem is aggravated due to the selection set with these partial selections etc.,