We have moved to Github. Please open tickets there.

Opened 3 years ago

Closed 2 years ago

#2143 closed Enhancement (fixed)

DRY up input views

Reported by: ewinslow Owned by:
Priority: high Milestone: Elgg 1.8.0
Component: UI/UX Version: 1.7
Severity: major Keywords:
Cc: brettp, ewinslow Difficulty: easy

Description

There is so much repeated code -- it should not be so!

My suggestion would be to add an input/input view that can take all relevant parameters. Then refactor the others to feed into that one.

I hope you find that the patch I submitted gets you most of the way there.

Attachments (3)

dry_inputs_5918_17.diff (14.1 KB) - added by ewinslow 3 years ago.
This patch is against the 1.7 branch, so it might not be completely compatible with 1.8 in terms of class defaults
text.php (1.4 KB) - added by cash 3 years ago.
dry_inputs.diff (10.9 KB) - added by ewinslow 3 years ago.
Initial patch against 1.7.1 using Cash's attribute technique

Download all attachments as: .zip

Change History (36)

Changed 3 years ago by ewinslow

This patch is against the 1.7 branch, so it might not be completely compatible with 1.8 in terms of class defaults

comment:1 Changed 3 years ago by cash

I should have mentioned that I started working this week on something similar. It takes in all the parameters and puts them in an attribute[] array so that the output code looks like this:

<input 
<?php 
    foreach($attributes as $key=>$value) {
        echo "$key=\"$value\" ";
    }
?>
>

Each view sets its defaults in attributes first and then I cycle through what I was passed in. An advantage of this is that we wouldn't need to add new parameters explicitly like your suggestion of placeholder.

comment:2 Changed 3 years ago by ewinslow

I like it. It also means you can do more natural things like

'onclick' => '...code...'

rather than

'js' => 'onclick="...code..."'

I suppose my suggestion (and patch) was a step in that direction, but we might as well take it all the way there!

comment:3 Changed 3 years ago by brettp

  • Severity changed from minor to major

Sounds like a good idea to me. As always, we'll need to be mindful of backward compatibility...

comment:4 Changed 3 years ago by cash

This hasn't been DRYed up at all but it illustrates where I was headed (obviously cleaning up some of the stuff that elgg_view() throws in there would make this cleaner).

Changed 3 years ago by cash

comment:5 Changed 3 years ago by ewinslow

Cash, this looks great. A few comments/questions:

  • Is 'js' still supported just for backward compatibility?
  • I'd be very happy if elgg_view no longer dumped a ton of stuff into $vars.
  • Might be good to sort the attributes alphabetically (makes gzipping more efficient).
  • I'd be happy to implement this for the rest of the input views in a DRY fashion if you haven't already.
  • A convenience function that unsets the default $vars parameters might be nice.

comment:6 Changed 3 years ago by ewinslow

Oh, and we should do addslashes($v) to make sure that

'onclick' => 'call("me")'

outputs what it's expected to output. I.e., not this:

<input onclick="call("me")" />

comment:7 Changed 3 years ago by cash

  • js is there for backward compatibility. We can deprecate it in 1.8 and following the normal deprecation path remove it for 2.0.
  • I definitely want to remove everything that elgg_view() current adds except for maybe user and url since they are used so often. I have a ticket open on this. If we remove this, we can pass in 'id' and 'name' instead of 'internalid'...
  • Because of the small number of keys, we are unlikely to see much compression gain from sorting. Doesn't add enough local redundancy like you would get if sorting a long list of vocabulary words. Plus, for those who still look at raw source, it's nice to have it in order that it was passed - more or less.
  • Good point on the add slashes.
  • If you want to code this up, that would be great! As an FYI, Brett started adding my sticky form code into the views in 1.8. I'm still working through some ideas related to that - may involve pulling the sticky form code out of the input views and put into a simple form builder.

comment:8 Changed 3 years ago by ewinslow

With regards to the sorting thing, I was going off this recommendation from Google:

Specify HTML attributes in the same order , i.e. alphabetize them. Put href first for links (since it is most common), then alphabetize the rest. For example, on Google's search results page, when HTML attributes were alphabetized, a 1.5% reduction in the size of the gzipped output resulted.

It's not much, but it's there. Perhaps it's best if we just leave it as a best practice for developers rather than enforcing it in the views.

One more question: with the kind of generality you're getting at here, we're really only one step away from providing a view that outputs an arbitrary html tag. Would there be any usefulness for such a view?

Changed 3 years ago by ewinslow

Initial patch against 1.7.1 using Cash's attribute technique

comment:9 Changed 3 years ago by cash

Quick patch!

I see these views serving two purposes:

  • convenience - less typing to get an input tag (not true for some views)
  • uniformity - a site can use all sorts of plugins and shouldn't have to worry about the HTML of the input tags

I don't see any advantage to a generic html tag view - especially given the overhead of the view system.

comment:10 Changed 3 years ago by brettp

  • Difficulty set to moderate
  • Priority changed from normal to high

comment:11 Changed 3 years ago by brettp

  • Difficulty changed from moderate to easy

comment:12 Changed 3 years ago by cash

  • Component changed from Core to UI/UX

comment:13 Changed 3 years ago by ewinslow

  • Owner set to ewinslow
  • Status changed from new to assigned

comment:14 Changed 3 years ago by cash

(In [svn:7334]) Refs #2143 I need ids for urls for the widget code right now

comment:15 Changed 3 years ago by ewinslow

(In [svn:7354]) Refs #2143: Added elgg_format_attributes() for generating an attribute string from an associative array. DRYed up input/output url

comment:16 Changed 3 years ago by ewinslow

(In [svn:7356]) Refs #2143: DRYed up input/form

comment:17 Changed 3 years ago by ewinslow

(In [svn:7357]) Refs #2143: DRY up input/text

comment:18 Changed 3 years ago by ewinslow

(In [svn:7358]) Refs #2143: DRY up input/email

comment:19 Changed 3 years ago by ewinslow

(In [svn:7359]) Refs #2143: DRY up input/tags

comment:20 Changed 3 years ago by ewinslow

(In [svn:7360]) Refs #2143: Cleaned up input/submit

comment:21 Changed 3 years ago by ewinslow

(In [svn:7364]) Refs #2143: DRY up button input views (button, reset, submit). Changed core uses of button to reflect the fact that it no longer defaults to submit

comment:22 Changed 3 years ago by cash

(In [svn:7391]) Refs #2143 fix for commit [svn:7354] as ampersands were being doubly encoded

comment:23 Changed 2 years ago by ewinslow

  • Owner ewinslow deleted
  • Status changed from assigned to new

comment:24 Changed 2 years ago by cash

Still to be DRYed up:

  • input/access
  • input/(pulldown, dropdown, select or whatever we're going to call it)
  • input/file
  • input/hidden
  • input/longtext and plaintext
  • input/password
  • input/radio

comment:25 Changed 2 years ago by ewinslow

(In [svn:8141]) Refs #2143: DRYed up input/file

comment:26 Changed 2 years ago by ewinslow

(In [svn:8142]) Refs #2143: DRYed up input/password

comment:27 Changed 2 years ago by ewinslow

(In [svn:8143]) Refs #2143: DRY up input/hidden

comment:28 Changed 2 years ago by ewinslow

(In [svn:8144]) Refs #2143: DRYed up input/dropdown -- probably could be better still

comment:29 Changed 2 years ago by ewinslow

(In [svn:8145]) Refs #2143: DRYed up input/plaintext

comment:30 Changed 2 years ago by ewinslow

(In [svn:8146]) Refs #2143: DRYed up input/longtext -- maybe I missed it, but was there a difference b/w plaintext and longtext?

comment:31 Changed 2 years ago by ewinslow

(In [svn:8147]) Refs #2143: DRYed up input/access -- wow, that feels good

comment:32 Changed 2 years ago by ewinslow

(In [svn:8148]) Refs #2143: General cleanup of input views

comment:33 Changed 2 years ago by ewinslow

  • Resolution set to fixed
  • Status changed from new to closed

(In [svn:8149]) Fixes #2143: DRYed up input/radio -- other views still should be addressed, but not because they aren't DRY, therefore I'm calling this ticket closed

Note: See TracTickets for help on using tickets.