When farbtastic was converted to use:

(function($) {
}) (jQuery);

a bug was introduced.

The actual starting farbtastic code in drupal 7.x is as follows:

// Farbtastic 1.2
(function($) {

$.farbtastic = function (callback) {
  $.farbtastic(this, callback);
  return this;
};
....

But the original farbtastic code is as follows:

// Farbtastic 1.2

jQuery.fn.farbtastic = function (callback) {
 $.farbtastic(this, callback);
 return this;
};
... 

So I think the correct code in 7.x would be:

// Farbtastic 1.2
(function($) {

$.fn.farbtastic = function (callback) {
  $.farbtastic(this, callback);
  return this;
};
....

The way, as an example, Garland uses Farbtastic is not affected by this bug, but affects other use cases.

Comments

manfer’s picture

As an example of when it would fail. The easy way to use farbtastic:

  1. Include farbtastic.js and farbtastic.css in your HTML:
       <script type="text/javascript" src="farbtastic.js"></script>
       <link rel="stylesheet" href="farbtastic.css" type="text/css" />
    
  2. Add a placeholder div and a text field to your HTML, and give each an ID:
      <form><input type="text" id="color" name="color" value="#123456" /></form>
      <div id="colorpicker"></div>
    
  3. Add a ready() handler to the document which initializes the color picker and link it to the text field with the following syntax:
      <script type="text/javascript">
        $(document).ready(function() {
          $('#colorpicker').farbtastic('#color');
        });
      </script>
    

won't work.

damien tournoud’s picture

Makes sense, can you roll a patch?

manfer’s picture

StatusFileSize
new483 bytes

Here is the patch.

manfer’s picture

I would like to make a question because I don't know how the drupal core process works.

The first question is if I named the patch correctly or maybe I should have named it only 623292.patch. In other issues I see the patch is tested with some kind of automatic system. Is that something a normal user can do or something automatically done if the patch name is correct or just something done by drupal developers?

The second is only to know if this won't be forgotten because for me is important to be corrected. Of course, I suppose it is only time needed to proceed with this change because should be lots and lots of things with higher priority.

Anyway I'm going to take the opportunity to present a drupal 7 style example that is going to fail if it is not corrected:

my_form ($form, &$form_state) {

  $form['custom_color'] = array(
    '#type' => '#textfield',
    '#title' => t('Custom color'),
    '#required' => TRUE,
    '#prefix' => '<div id="custom-color-picker"></div>',
    '#default_value' => '#000000',
    '#maxlength' => 7,
    '#size' => 8,
  )

  $form['custom_color']['#attached']['library'][] = array(
    'system',
    'farbtastic',
  );

  $form['custom_color']['#attached']['js'][] = array(
    'data' => '(function($) { $(document).ready(function() { $("#custom-color-picker").farbtastic("#edit-custom-color"); }); }) (jQuery);',
    'type' => 'inline',
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  return $form;
}

If the farbtastic.js is not corrected this kind of use is going to trigger a javascript error. In the above code example the error will be:

$("#custom-color-picker").farbtastic is not a function
damien tournoud’s picture

Status: Active » Needs review

You need to set the status to "needs review" if you want this patch to be reviewed (both by our test bot and by human reviewer).

The patch looks fine, but you need to roll it from the Drupal root (the test bot will complain that it doesn't apply). Please see http://drupal.org/patch/create for additional instructions.

Status: Needs review » Needs work

The last submitted patch failed testing.

manfer’s picture

Status: Needs work » Needs review
StatusFileSize
new605 bytes

I hope I have done correctly now. Sorry if I mistake again, it is first time.

Thanks very much to point me to doc.

Status: Needs review » Needs work

The last submitted patch failed testing.

manfer’s picture

Status: Needs work » Needs review
StatusFileSize
new531 bytes

The one in #7 was made from netbeans IDE. I thought I was doing it from root but it seems I don't know how to do a correct diff from that IDE.

Sorry, one more try now from terminal and I think it is done from root. I have to finally learn how to do this correctly.

mfer’s picture

If you're rolling a patch against farbtastic you might want to roll it against http://code.google.com/p/farbtastic.

manfer’s picture

The problem is not present on farbtastic 1.2 original code. I don't see it in farbtastic 1.3 (or 2.0 I have not clear) development you suggest me to look either.

So I can't know when that bug was introduced in the code included with drupal but it is present there not in any farbtastic code.

Including that newer version of farbtastic (1.3 or 2.0 whatever it will be) that conforms to drupal 7:

JavaScript should be compatible with other libraries than jQuery

would solve the issue too.

mfer’s picture

Status: Needs review » Needs work

@manfer we need to update to newest code in farbtastic 1.x before we release D7. Farbtastic 1.3 is not released yet so I have been holding off.

Let me see about a farbtastic release. It would be nice to just drop the minified farbtastic 1.3 n D7.

manfer’s picture

Thanks for looking on it.

Anyway now that I looked a little more on farbtastic it won't be a problem for me to change code like this:

$("#custom-color-picker").farbtastic("#edit-custom-color");

to:

$.farbtastic("#custom-color-picker", "#edit-custom-color");

or:

$.farbtastic("#custom-color-picker").linkTo("#edit-custom-color");

and it will work even with that bug present. I'm using farbtastic picker for a very basic use and don't need the farbtastic object nor the farbtastic placeholder after creating it and any of those 3 methods works for me (the first method only with the bug corrected).

It is nice to know it will be fixed. Thanks.

mfer’s picture

Assigned: Unassigned » mfer
Status: Needs work » Needs review
StatusFileSize
new11.88 KB

Farbtastic, as it sits in Drupal, starts out:

// $Id: farbtastic.js,v 1.6 2008/10/22 18:27:51 dries Exp $
// Farbtastic 1.2
(function($) {

$.farbtastic = function (callback) {
  $.farbtastic(this, callback);
  return this;
};

$.farbtastic = function (container, callback) {
  var container = $(container).get(0);
  return container.farbtastic || (container.farbtastic = new $._farbtastic(container, callback));
};

$._farbtastic = function (container, callback) {

The first of these should start $.fn.farbtastic so it can be used in chaining.

The attached patch fixes that, updates the code to head from the farbtastic 1 branch at http://code.google.com/p/farbtastic, and uses the minified version (via Google closure compiler which jQuery is now using as well). The uncompressed version can be seen at http://code.google.com/p/farbtastic/source/browse/branches/farbtastic-1/...

This has been tested with jQuery 1.4 as well and it works.

mfer’s picture

Priority: Normal » Critical
robloach’s picture

Status: Needs review » Reviewed & tested by the community

Applied patch and Color module worked quite nicely. The testing bot also liked this one so I'm hitting the go button.

robloach’s picture

Title: farbtastic code has a bug » Update Farbtastic
Issue tags: +JavaScript, +color, +farbtastic

Let's use a more appropriate title and apply some tags, shall we?

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks.

Status: Fixed » Closed (fixed)
Issue tags: -JavaScript, -color, -farbtastic

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