This value is entirely a front-end thing and does not need cookies.

Also using localStorage mean we can listen to events and react in background tabs. Open a "manage fields" page for some content type, then open another tab/window in the same or another "manage fields" page. Click "Show row weight" in one tab and look at the other one, the change propagated.

Tested in IE8/9 and the rest.

Comments

sun’s picture

Status: Needs review » Needs work
+++ b/core/misc/tabledrag.js
@@ -112,6 +112,7 @@ Drupal.tableDrag = function (table, tableSettings) {
+  $(window).bind('storage', $.proxy(this.toggleColumns, this));

Could use an inline comment explaining the event magic.

+++ b/core/misc/tabledrag.js
@@ -163,20 +164,21 @@ Drupal.tableDrag.prototype.initColumns = function () {
+Drupal.tableDrag.prototype.toggleColumns = function () {

Let's add some JSDoc here.

+++ b/core/misc/tabledrag.js
@@ -163,20 +164,21 @@ Drupal.tableDrag.prototype.initColumns = function () {
+  // Now hide cells and reduce colspans unless localStorage value indicates
+  // previous choice. Set a value if it is not already present.
+  if (showWeight === null) {
+    localStorage.setItem('Drupal.tableDrag.showWeight', '0');

I don't think it is a good practice to store default values on the client-side.

We should only store default overrides. In turn, that would mean to actually delete the setting when toggling the button back to its default.

wim leers’s picture

Issue tags: +WPO

+1 for patch.
+1 sun's review, especially the last part.

Also:
- this is good for WPO. Cookies make HTTP requests slower.
- browser support is solid: http://caniuse.com/namevalue-storage. Only Opera Mini is missing. I doubt this functionality works well (as in usable) in Opera Mini in the first place, so we should be okay.

nod_’s picture

I'm reworking a couple of things in addition to the review,

That's right, tabledrag is pretty much busted on mobile so that's not a big concern for now.

nod_’s picture

Status: Needs work » Needs review
StatusFileSize
new5.05 KB

patch add

Drupal.tableDrag.prototype.displayColumns
Drupal.tableDrag.prototype.toggleColumns

and a few things reworked. changing the type of showWeight is not an issue, the only thing reacting to columnschange is tableheader and that does not care about the actual type of the value.

nod_’s picture

StatusFileSize
new4.56 KB

reroll.

I like it so much I waste time playing with the hide/show weights link :p

sun’s picture

Status: Needs review » Reviewed & tested by the community
droplet’s picture

+++ b/core/misc/tabledrag.jsundefined
@@ -3,6 +3,12 @@
+var showWeight = JSON.parse(localStorage.getItem('Drupal.tableDrag.showWeight'));

why use JSON.parse here?

nod_’s picture

since true/false get stored as strings, without it showWeight would be either 'true' or 'false' and they would both be true in the toggle condition.

dries’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/misc/tabledrag.jsundefined
@@ -163,25 +173,42 @@ Drupal.tableDrag.prototype.initColumns = function () {
+    // Reset the value to it's default.
+    localStorage.removeItem('Drupal.tableDrag.showWeight');

Should be 'its'.

Looks like a nice clean-up to me.

nod_’s picture

Status: Needs work » Needs review
StatusFileSize
new4.56 KB

reroll

nod_’s picture

Status: Needs review » Reviewed & tested by the community

testbot happy, back to RTBC, it was only a comment change.

corbacho’s picture

Status: Reviewed & tested by the community » Needs work

+1 This is very creative way of using localstorage. I like it a lot

I'm worried about raw use of localstorage API and browser support.

IE8 doesn't trigger 'storage' event, but 'onstorage'. Has been tested there? (At least I've search for 'storage' in last jquery source code without results)

Also there is a jQuery plugin http://www.jstorage.info/ that is hosted in github, so we can see what kind of fixes brings to the table, and see in the issue queue that localstorage API has some edge cases, for example errors in (ios 5 ? What about using a wrapper ? Are we using localstorage any other place in Drupal 8 ?

nod_’s picture

Status: Needs work » Reviewed & tested by the community

Please change status only if you have real issues with the patch, I'm only seeing speculative issues from your post:

The event is handled by jQuery, it takes care of cross-browser things. Also, see OP: tested successfully in IE8/9.
This is a very straightforward way of using localStorage. There is no edge case going on here. Also the browser support for mobile is not defined just yet. I can't test iphone/ipad browsers. If your testing shows a problem with this patch please set it back to NW.
The issue queue you're linking to contains jstorage bugs (i've read them). not localStorage bugs.
Wrapper for this seems overkill, if you want to play with the idea please see #1172670: Evaluate standardized usage of HTML5 Storage API through a library
We're not using localStorage anywhere else.

This patch shouldn't be held up by that. In case the other issue goes somewhere, the refactor would be trivial.

Back to RTBC

corbacho’s picture

Thanks for the detailed answers! nothing to add
Sorry about that speculative status change, wasn't my intention to stop it ;)

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 8.x. Thanks.

nod_’s picture

Title: Use localStorage to store tableDrag.showWeight value » Follow-up: Use localStorage to store tableDrag.showWeight value
Status: Fixed » Reviewed & tested by the community
StatusFileSize
new654 bytes

Quick follow-up, I missed a bit during my reroll in #5, there is a leftover $.cookie(), this patch removes it like it's supposed to.

Nothing is visibly broken though.

Thanks.

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed the follow-up patch. Thanks.

Automatically closed -- issue fixed for 2 weeks with no activity.

Marko B’s picture

When can this be roled to Drupal 7? Reason is, this cookie is breaking Varnish caching unless excluded by code in .vcl file.

nod_’s picture

won't make it to D7, it's an API change.