Problem/Motivation

When displaying an addthis in e.g. a dialog, it is not automatically loaded (except when the addthis library is added the first time).

Proposed resolution

Add a behavior that checks if the newly loaded context has any .addthis_toolbar, then load it with addthis.toolbox('.addthis_toolbox');.

Avoid to be called the first time the behavior is called, I think you can check for that with context == document, since it happens automatically then. Might not be worth it, though.

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir created an issue. See original summary.

sasanikolic’s picture

Status: Active » Needs review
FileSize
1.17 KB
miro_dietiker’s picture

Status: Needs review » Needs work
  1. +++ b/js/sharemessage.js
    @@ -0,0 +1,24 @@
    +      // This is used for special cases when the scripts are added using AJAX, for example if sharemessage is rendered
    

    80 col wrapping. :-)

  2. +++ b/js/sharemessage.js
    @@ -0,0 +1,24 @@
    +      // in a dialog. In that case the adthis library is loaded after sharemessage.
    

    adthis => addthis... nice typo!
    sharemessage => ShareMessage
    And similar with the other line.

  3. +++ b/js/sharemessage.js
    @@ -0,0 +1,24 @@
    +      var interval = setInterval(function(){
    ...
    +      }, 50);
    

    That's really an ugly race condition problem/ workaround with this polling. We should try to avoid this kind of dirty fix as much as possible.

    Is there no other way?

    If not, note that this Interval triggering is also fired and eating up browser performance for the non-ajax cases. And in case .addthis_toolbox isn't found, it keeps firing forever... At least fix the checks and sequence.

pivica’s picture

That's really an ugly race condition problem/ workaround with this polling. We should try to avoid this kind of dirty fix as much as possible.

Is there no other way?

In core/misc/dialog.js in openDialog() method code is triggering couple of events, the one we probably can reuse are dialog:beforecreate and dialog:aftercreate. So the code can look like this

$(window).on('dialog:beforecreate', function (e, dialog, $element, settings) {
  // do the stuff from interval function...
}

Not sure which event is better to use, probably both are OK.

Berdir’s picture

That works with dialog, but what about other ajax updates, for example some views endless scroll thing with share messages that need to be loaded/added?

sasanikolic’s picture

Status: Needs work » Needs review
FileSize
1.25 KB
1.31 KB

Temporary fix until we find a better solution...

pivica’s picture

Status: Needs review » Needs work

OK I misunderstood original problem which is actually a case that we can load addthis js dynamically later on some ajax event and then we need to execute it when actual js is loaded.

So addthis API is triggering event when it is ready (http://support.addthis.com/customer/portal/articles/1365497-addthis-java...), and it looks like this

// Listen for the ready event
addthis.addEventListener('addthis.ready', addthisReady);

And maybe we should use this also, but this again needs global addthis var to be initialized.

I have checked quickly addthis js code but could not found any place where addthis is triggering some event on load.

There is a way to detect when some js code is loaded, for example like explained on http://stackoverflow.com/questions/8718303/javascript-to-detect-when-ext..., but two problems here
1. Drupal is controling loading of JS code so not sure can we use this at all
2. Then not sure how much browsers support this script load event

So on the end using interval is not that bad but as Miro already said we should first check is addthis exist and if not then start that interval.

sasanikolic’s picture

Status: Needs work » Needs review
FileSize
1.21 KB
1001 bytes

Ok, moved also the check for the addthis up.

miro_dietiker’s picture

Status: Needs review » Needs work
+++ b/js/sharemessage.js
@@ -0,0 +1,25 @@
+            clearInterval(interval);

Still needs to be outside the .each().

sasanikolic’s picture

Status: Needs work » Needs review
FileSize
1.21 KB
444 bytes

Moved...

pivica’s picture

Status: Needs review » Needs work
  1. +++ b/js/sharemessage.js
    @@ -0,0 +1,25 @@
    +//
    

    Empty comment line? This should be just empty line.

  2. +++ b/js/sharemessage.js
    @@ -0,0 +1,25 @@
    +  "use strict";
    

    Missing empty like after "use strict" and Drupal.behaviors.sharemessage should have doc comment.

  3. +++ b/js/sharemessage.js
    @@ -0,0 +1,25 @@
    +    attach: function (context, addthis) {
    

    What is this addthis doing here?

  4. +++ b/js/sharemessage.js
    @@ -0,0 +1,25 @@
    +      if ($(context).find('.addthis_toolbox') && window.addthis !== undefined) {
    +        var interval = setInterval(function(){
    

    Hmm maybe I am missing something obvious here but as I see it we should do two separate if here.

    First if should check if there is some element in context which has addthis_toolbox class. If there are no such elements we don't have nothing to do. But if there is such a element we then continue to second if.

    Second if should then check if window.addthis exist, if it exist then we can call toolbox for that element. If it does not exist then we can use setInterval approach and wait that addthis global is initialized.

    Also current if is not OK for a case when addthis_toolbox elements exist but addthis is undefined - basically you will never enter inside and get to the setInterval code.

  5. +++ b/js/sharemessage.js
    @@ -0,0 +1,25 @@
    +        var interval = setInterval(function(){
    

    Space is missing after function().

  6. +++ b/js/sharemessage.js
    @@ -0,0 +1,25 @@
    +          $(context).find('.addthis_toolbox').each(function () {
    

    Here again we are doing $(context).find('.addthis_toolbox') like in outer if, not optimal.

  7. +++ b/js/sharemessage.js
    @@ -0,0 +1,25 @@
    +            window.addthis.toolbox('.addthis_toolbox');
    

    No need to write window.addthis.toolbox, global vars you can always access directly in any scope, so addthis.toolbox is enough.

  8. +++ b/js/sharemessage.js
    @@ -0,0 +1,25 @@
    +          clearInterval(interval);
    

    Not sure currently where this call should be but we should call clearInterval(interval) only when we detect that addthis is defined.

  9. +++ b/js/sharemessage.js
    @@ -0,0 +1,25 @@
    +})(jQuery);
    

    Because we are using Drupal global var inside you should add it here also in call and before in function definition, something like

    (function ($, Drupal) {
    // ....
    })(jQuery, Drupal);

pivica’s picture

+++ b/js/sharemessage.js
@@ -0,0 +1,25 @@
+            window.addthis.toolbox('.addthis_toolbox');

Can't we just say here

addthis.toolbox(this);

When you use class selector I guess addthis will first select all elements on page, and we should limit this to current context, and then try to init toolbox on them.

Check API doc on http://support.addthis.com/customer/portal/articles/1365325-rendering-to... which says that for target(s) we can also use HTML element references.

sasanikolic’s picture

Status: Needs work » Needs review
FileSize
1.38 KB
1.51 KB

Made the changes for the comments above. I am only not sure about how to change the $(context).find('.addthis_toolbox') for comment #6. Any suggestion?

pivica’s picture

No point 4. i was thinking something else. OK maybe it will be faster to create a patch ;)

Didn't test this at all just rewrite it how i guess it should work.

@sasanikolic can you do full test of this and check that everything works properly?

miro_dietiker’s picture

Now it looks pretty solid. ;-)

sasanikolic’s picture

Status: Needs review » Reviewed & tested by the community

Tested with IE, Firefox, Chrome and Safari. Works fine. I think we're good to go with this finally. :)

Berdir’s picture

Status: Reviewed & tested by the community » Fixed

Great, committed.

  • Berdir committed d1111f8 on 8.x-1.x authored by sasanikolic
    Issue #2557075 by sasanikolic, pivica: Add JS behavior to automatically...

Status: Fixed » Closed (fixed)

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